TextFieldsEditorView.java revision e598332967106e3db63b73c701f21902d169efef
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.contacts.editor; 18 19import com.android.contacts.ContactsUtils; 20import com.android.contacts.R; 21import com.android.contacts.model.AccountType.EditField; 22import com.android.contacts.model.DataKind; 23import com.android.contacts.model.EntityDelta; 24import com.android.contacts.model.EntityDelta.ValuesDelta; 25import com.android.contacts.util.NameConverter; 26import com.android.contacts.util.PhoneNumberFormatter; 27 28import android.content.Context; 29import android.content.Entity; 30import android.graphics.Rect; 31import android.graphics.Typeface; 32import android.os.Parcel; 33import android.os.Parcelable; 34import android.provider.ContactsContract.CommonDataKinds.StructuredName; 35import android.telephony.PhoneNumberFormattingTextWatcher; 36import android.text.Editable; 37import android.text.InputType; 38import android.text.Spannable; 39import android.text.TextUtils; 40import android.text.TextWatcher; 41import android.text.style.StyleSpan; 42import android.util.AttributeSet; 43import android.view.Gravity; 44import android.view.View; 45import android.view.ViewGroup; 46import android.widget.EditText; 47import android.widget.ImageView; 48import android.widget.LinearLayout; 49 50import java.util.Map; 51 52/** 53 * Simple editor that handles labels and any {@link EditField} defined for the 54 * entry. Uses {@link ValuesDelta} to read any existing {@link Entity} values, 55 * and to correctly write any changes values. 56 */ 57public class TextFieldsEditorView extends LabeledEditorView { 58 private EditText[] mFieldEditTexts = null; 59 private ViewGroup mFields = null; 60 private View mExpansionViewContainer; 61 private ImageView mExpansionView; 62 private boolean mHideOptional = true; 63 private boolean mHasShortAndLongForms; 64 private int mMinFieldHeight; 65 66 public TextFieldsEditorView(Context context) { 67 super(context); 68 } 69 70 public TextFieldsEditorView(Context context, AttributeSet attrs) { 71 super(context, attrs); 72 } 73 74 public TextFieldsEditorView(Context context, AttributeSet attrs, int defStyle) { 75 super(context, attrs, defStyle); 76 } 77 78 /** {@inheritDoc} */ 79 @Override 80 protected void onFinishInflate() { 81 super.onFinishInflate(); 82 83 setDrawingCacheEnabled(true); 84 setAlwaysDrawnWithCacheEnabled(true); 85 86 mMinFieldHeight = mContext.getResources().getDimensionPixelSize( 87 R.dimen.editor_min_line_item_height); 88 mFields = (ViewGroup) findViewById(R.id.editors); 89 mExpansionView = (ImageView) findViewById(R.id.expansion_view); 90 mExpansionViewContainer = findViewById(R.id.expansion_view_container); 91 mExpansionViewContainer.setOnClickListener(new OnClickListener() { 92 @Override 93 public void onClick(View v) { 94 // Save focus 95 final View focusedChild = getFocusedChild(); 96 final int focusedViewId = focusedChild == null ? -1 : focusedChild.getId(); 97 98 // Reconfigure GUI 99 mHideOptional = !mHideOptional; 100 onOptionalFieldVisibilityChange(); 101 rebuildValues(); 102 103 // Restore focus 104 View newFocusView = findViewById(focusedViewId); 105 if (newFocusView == null || newFocusView.getVisibility() == GONE) { 106 // find first visible child 107 newFocusView = TextFieldsEditorView.this; 108 } 109 newFocusView.requestFocus(); 110 } 111 }); 112 } 113 114 @Override 115 public void setEnabled(boolean enabled) { 116 super.setEnabled(enabled); 117 118 if (mFieldEditTexts != null) { 119 for (int index = 0; index < mFieldEditTexts.length; index++) { 120 mFieldEditTexts[index].setEnabled(!isReadOnly() && enabled); 121 } 122 } 123 mExpansionView.setEnabled(!isReadOnly() && enabled); 124 } 125 126 /** 127 * Creates or removes the type/label button. Doesn't do anything if already correctly configured 128 */ 129 private void setupExpansionView(boolean shouldExist, boolean collapsed) { 130 if (shouldExist) { 131 mExpansionViewContainer.setVisibility(View.VISIBLE); 132 mExpansionView.setImageResource(collapsed 133 ? R.drawable.ic_menu_expander_minimized_holo_light 134 : R.drawable.ic_menu_expander_maximized_holo_light); 135 } else { 136 mExpansionViewContainer.setVisibility(View.GONE); 137 } 138 } 139 140 @Override 141 protected void requestFocusForFirstEditField() { 142 if (mFieldEditTexts != null && mFieldEditTexts.length != 0) { 143 EditText firstField = null; 144 boolean anyFieldHasFocus = false; 145 for (EditText editText : mFieldEditTexts) { 146 if (firstField == null && editText.getVisibility() == View.VISIBLE) { 147 firstField = editText; 148 } 149 if (editText.hasFocus()) { 150 anyFieldHasFocus = true; 151 break; 152 } 153 } 154 if (!anyFieldHasFocus && firstField != null) { 155 firstField.requestFocus(); 156 } 157 } 158 } 159 160 @Override 161 public void setValues(DataKind kind, ValuesDelta entry, EntityDelta state, boolean readOnly, 162 ViewIdGenerator vig) { 163 super.setValues(kind, entry, state, readOnly, vig); 164 // Remove edit texts that we currently have 165 if (mFieldEditTexts != null) { 166 for (EditText fieldEditText : mFieldEditTexts) { 167 mFields.removeView(fieldEditText); 168 } 169 } 170 boolean hidePossible = false; 171 172 int fieldCount = kind.fieldList.size(); 173 mFieldEditTexts = new EditText[fieldCount]; 174 for (int index = 0; index < fieldCount; index++) { 175 final EditField field = kind.fieldList.get(index); 176 final EditText fieldView = new EditText(mContext); 177 fieldView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 178 field.isMultiLine() ? LayoutParams.WRAP_CONTENT : mMinFieldHeight)); 179 fieldView.setTextAppearance(getContext(), kind.textAppearanceResourceId); 180 fieldView.setGravity(Gravity.TOP); 181 mFieldEditTexts[index] = fieldView; 182 fieldView.setId(vig.getId(state, kind, entry, index)); 183 if (field.titleRes > 0) { 184 fieldView.setHint(field.titleRes); 185 } 186 int inputType = field.inputType; 187 fieldView.setInputType(inputType); 188 if (inputType == InputType.TYPE_CLASS_PHONE) { 189 PhoneNumberFormatter.setPhoneNumberFormattingTextWatcher(mContext, fieldView); 190 } 191 fieldView.setMinLines(field.minLines); 192 193 // Read current value from state 194 final String column = field.column; 195 final String value = entry.getAsString(column); 196 fieldView.setText(value); 197 198 // Show the delete button if we have a non-null value 199 setDeleteButtonVisible(value != null); 200 201 // Prepare listener for writing changes 202 fieldView.addTextChangedListener(new TextWatcher() { 203 @Override 204 public void afterTextChanged(Editable s) { 205 // Trigger event for newly changed value 206 onFieldChanged(column, s.toString()); 207 } 208 209 @Override 210 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 211 } 212 213 @Override 214 public void onTextChanged(CharSequence s, int start, int before, int count) { 215 } 216 }); 217 218 fieldView.setEnabled(isEnabled() && !readOnly); 219 220 if (field.shortForm) { 221 hidePossible = true; 222 mHasShortAndLongForms = true; 223 fieldView.setVisibility(mHideOptional ? View.VISIBLE : View.GONE); 224 } else if (field.longForm) { 225 hidePossible = true; 226 mHasShortAndLongForms = true; 227 fieldView.setVisibility(mHideOptional ? View.GONE : View.VISIBLE); 228 } else { 229 // Hide field when empty and optional value 230 final boolean couldHide = (!ContactsUtils.isGraphic(value) && field.optional); 231 final boolean willHide = (mHideOptional && couldHide); 232 fieldView.setVisibility(willHide ? View.GONE : View.VISIBLE); 233 hidePossible = hidePossible || couldHide; 234 } 235 236 mFields.addView(fieldView); 237 } 238 239 // When hiding fields, place expandable 240 setupExpansionView(hidePossible, mHideOptional); 241 mExpansionView.setEnabled(!readOnly && isEnabled()); 242 } 243 244 @Override 245 public boolean isEmpty() { 246 for (int i = 0; i < mFields.getChildCount(); i++) { 247 EditText editText = (EditText) mFields.getChildAt(i); 248 if (!TextUtils.isEmpty(editText.getText())) { 249 return false; 250 } 251 } 252 return true; 253 } 254 255 /** 256 * Returns true if the editor is currently configured to show optional fields. 257 */ 258 public boolean areOptionalFieldsVisible() { 259 return !mHideOptional; 260 } 261 262 public boolean hasShortAndLongForms() { 263 return mHasShortAndLongForms; 264 } 265 266 /** 267 * Populates the bound rectangle with the bounds of the last editor field inside this view. 268 */ 269 public void acquireEditorBounds(Rect bounds) { 270 if (mFieldEditTexts != null) { 271 for (int i = mFieldEditTexts.length; --i >= 0;) { 272 EditText editText = mFieldEditTexts[i]; 273 if (editText.getVisibility() == View.VISIBLE) { 274 bounds.set(editText.getLeft(), editText.getTop(), editText.getRight(), 275 editText.getBottom()); 276 return; 277 } 278 } 279 } 280 } 281 282 /** 283 * Saves the visibility of the child EditTexts, and mHideOptional. 284 */ 285 @Override 286 protected Parcelable onSaveInstanceState() { 287 Parcelable superState = super.onSaveInstanceState(); 288 SavedState ss = new SavedState(superState); 289 290 ss.mHideOptional = mHideOptional; 291 292 final int numChildren = mFieldEditTexts == null ? 0 : mFieldEditTexts.length; 293 ss.mVisibilities = new int[numChildren]; 294 for (int i = 0; i < numChildren; i++) { 295 ss.mVisibilities[i] = mFieldEditTexts[i].getVisibility(); 296 } 297 298 return ss; 299 } 300 301 /** 302 * Restores the visibility of the child EditTexts, and mHideOptional. 303 */ 304 @Override 305 protected void onRestoreInstanceState(Parcelable state) { 306 SavedState ss = (SavedState) state; 307 super.onRestoreInstanceState(ss.getSuperState()); 308 309 mHideOptional = ss.mHideOptional; 310 311 int numChildren = Math.min(mFieldEditTexts.length, ss.mVisibilities.length); 312 for (int i = 0; i < numChildren; i++) { 313 mFieldEditTexts[i].setVisibility(ss.mVisibilities[i]); 314 } 315 } 316 317 private static class SavedState extends BaseSavedState { 318 public boolean mHideOptional; 319 public int[] mVisibilities; 320 321 SavedState(Parcelable superState) { 322 super(superState); 323 } 324 325 private SavedState(Parcel in) { 326 super(in); 327 mVisibilities = new int[in.readInt()]; 328 in.readIntArray(mVisibilities); 329 } 330 331 @Override 332 public void writeToParcel(Parcel out, int flags) { 333 super.writeToParcel(out, flags); 334 out.writeInt(mVisibilities.length); 335 out.writeIntArray(mVisibilities); 336 } 337 338 @SuppressWarnings({"unused", "hiding" }) 339 public static final Parcelable.Creator<SavedState> CREATOR 340 = new Parcelable.Creator<SavedState>() { 341 @Override 342 public SavedState createFromParcel(Parcel in) { 343 return new SavedState(in); 344 } 345 346 @Override 347 public SavedState[] newArray(int size) { 348 return new SavedState[size]; 349 } 350 }; 351 } 352 353 @Override 354 public void clearAllFields() { 355 if (mFieldEditTexts != null) { 356 for (EditText fieldEditText : mFieldEditTexts) { 357 // Update UI (which will trigger a state change through the {@link TextWatcher}) 358 fieldEditText.setText(""); 359 } 360 } 361 } 362} 363