TextFieldsEditorView.java revision ba48d21f2ab25c8ffb30198a752e5cd215553728
1392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann/* 2392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * Copyright (C) 2010 The Android Open Source Project 3392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * 4392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * Licensed under the Apache License, Version 2.0 (the "License"); 5392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * you may not use this file except in compliance with the License. 6392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * You may obtain a copy of the License at 7392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * 8392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * http://www.apache.org/licenses/LICENSE-2.0 9392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * 10392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * Unless required by applicable law or agreed to in writing, software 11392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * distributed under the License is distributed on an "AS IS" BASIS, 12392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * See the License for the specific language governing permissions and 14392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * limitations under the License. 15392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann */ 16392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 1718ffaa2561cc7dd2e3ef81737e6537931c0a9a11Dmitri Plotnikovpackage com.android.contacts.editor; 18392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 19392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmannimport android.content.Context; 20392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmannimport android.graphics.Rect; 21392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmannimport android.os.Parcel; 22392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmannimport android.os.Parcelable; 23392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmannimport android.text.Editable; 24392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmannimport android.text.InputType; 251b22073590c1801c3e6c7bde2f4636632a049f6cKatherine Kuanimport android.text.TextUtils; 26392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmannimport android.text.TextWatcher; 27392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmannimport android.util.AttributeSet; 2826918da1650c1ff7b6f92deb978ab976525c811aJosh Gargusimport android.util.Log; 29392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmannimport android.view.Gravity; 30392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmannimport android.view.View; 3163ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuanimport android.view.ViewGroup; 320af02581eecabbe234e54364bdbbd65090518d7cKatherine Kuanimport android.view.inputmethod.EditorInfo; 3326918da1650c1ff7b6f92deb978ab976525c811aJosh Gargusimport android.view.inputmethod.InputMethodManager; 34392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmannimport android.widget.EditText; 3512a93637e406ba7f3da4db8d53cd035c2c1d6e6eKatherine Kuanimport android.widget.ImageView; 36392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmannimport android.widget.LinearLayout; 37da5bf1cf60beef3de5e651a569fa544293683926Dave Santoro 38e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.ContactsUtils; 39e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.R; 40851222a96b5d68602fb361ea3527101e893f67e3Maurice Chuimport com.android.contacts.model.RawContactDelta; 41738ff8623dc77dd91a1b9023861e924ba5e4c27eChiao Chengimport com.android.contacts.common.model.ValuesDelta; 42428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.account.AccountType.EditField; 43428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.dataitem.DataKind; 44720ac2f99fc6cb4b92852a1a54ccd8c7c796c7d2Chiao Chengimport com.android.contacts.common.util.PhoneNumberFormatter; 45e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Cheng 46392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann/** 4763ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan * Simple editor that handles labels and any {@link EditField} defined for the 48851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu * entry. Uses {@link ValuesDelta} to read any existing {@link RawContact} values, 4963ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan * and to correctly write any changes values. 50392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann */ 51392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmannpublic class TextFieldsEditorView extends LabeledEditorView { 5226918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus private static final String TAG = TextFieldsEditorView.class.getSimpleName(); 5326918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus 54392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann private EditText[] mFieldEditTexts = null; 5563ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan private ViewGroup mFields = null; 5612a93637e406ba7f3da4db8d53cd035c2c1d6e6eKatherine Kuan private View mExpansionViewContainer; 5712a93637e406ba7f3da4db8d53cd035c2c1d6e6eKatherine Kuan private ImageView mExpansionView; 58392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann private boolean mHideOptional = true; 59392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann private boolean mHasShortAndLongForms; 601ebbd5bbe71814123b80df702b474bcd7196cbb7Katherine Kuan private int mMinFieldHeight; 61ba48d21f2ab25c8ffb30198a752e5cd215553728Yorke Lee private int mPreviousViewHeight; 62392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 63392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public TextFieldsEditorView(Context context) { 64392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann super(context); 65392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 66392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 67392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public TextFieldsEditorView(Context context, AttributeSet attrs) { 68392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann super(context, attrs); 69392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 70392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 71392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public TextFieldsEditorView(Context context, AttributeSet attrs, int defStyle) { 72392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann super(context, attrs, defStyle); 73392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 74392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 7563ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan /** {@inheritDoc} */ 7691d8e892d549bbeba721cb434163a83bc99330a9Dmitri Plotnikov @Override 7763ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan protected void onFinishInflate() { 7863ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan super.onFinishInflate(); 7963ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan 8063ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan setDrawingCacheEnabled(true); 8163ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan setAlwaysDrawnWithCacheEnabled(true); 8263ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan 831ebbd5bbe71814123b80df702b474bcd7196cbb7Katherine Kuan mMinFieldHeight = mContext.getResources().getDimensionPixelSize( 841ebbd5bbe71814123b80df702b474bcd7196cbb7Katherine Kuan R.dimen.editor_min_line_item_height); 8563ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan mFields = (ViewGroup) findViewById(R.id.editors); 8612a93637e406ba7f3da4db8d53cd035c2c1d6e6eKatherine Kuan mExpansionView = (ImageView) findViewById(R.id.expansion_view); 8712a93637e406ba7f3da4db8d53cd035c2c1d6e6eKatherine Kuan mExpansionViewContainer = findViewById(R.id.expansion_view_container); 8812a93637e406ba7f3da4db8d53cd035c2c1d6e6eKatherine Kuan mExpansionViewContainer.setOnClickListener(new OnClickListener() { 8963ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan @Override 9063ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan public void onClick(View v) { 91ba48d21f2ab25c8ffb30198a752e5cd215553728Yorke Lee mPreviousViewHeight = mFields.getHeight(); 92ba48d21f2ab25c8ffb30198a752e5cd215553728Yorke Lee 9363ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan // Save focus 9463ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan final View focusedChild = getFocusedChild(); 9563ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan final int focusedViewId = focusedChild == null ? -1 : focusedChild.getId(); 9663ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan 9763ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan // Reconfigure GUI 9863ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan mHideOptional = !mHideOptional; 9963ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan onOptionalFieldVisibilityChange(); 10063ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan rebuildValues(); 10163ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan 10263ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan // Restore focus 10363ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan View newFocusView = findViewById(focusedViewId); 10463ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan if (newFocusView == null || newFocusView.getVisibility() == GONE) { 10563ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan // find first visible child 10663ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan newFocusView = TextFieldsEditorView.this; 107392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 10863ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan newFocusView.requestFocus(); 109ba48d21f2ab25c8ffb30198a752e5cd215553728Yorke Lee 110ba48d21f2ab25c8ffb30198a752e5cd215553728Yorke Lee EditorAnimator.getInstance().slideAndFadeIn(mFields, mPreviousViewHeight); 111392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 11263ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan }); 113392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 114392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 115392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann @Override 11626918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus public void editNewlyAddedField() { 11726918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus // Some editors may have multiple fields (eg: first-name/last-name), but since the user 11826918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus // has not selected a particular one, it is reasonable to simply pick the first. 11926918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus final View editor = mFields.getChildAt(0); 12026918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus 12126918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus // Show the soft-keyboard. 12226918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus InputMethodManager imm = 12326918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 12426918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus if (imm != null) { 12526918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus if (!imm.showSoftInput(editor, InputMethodManager.SHOW_IMPLICIT)) { 12626918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus Log.w(TAG, "Failed to show soft input method."); 12726918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus } 12826918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus } 12926918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus } 13026918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus 13126918da1650c1ff7b6f92deb978ab976525c811aJosh Gargus @Override 132392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public void setEnabled(boolean enabled) { 133392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann super.setEnabled(enabled); 134392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 135392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann if (mFieldEditTexts != null) { 136392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann for (int index = 0; index < mFieldEditTexts.length; index++) { 13702bb1252aa990813fa9e86ad75c458acc15e6801Dmitri Plotnikov mFieldEditTexts[index].setEnabled(!isReadOnly() && enabled); 138392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 139392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 14012a93637e406ba7f3da4db8d53cd035c2c1d6e6eKatherine Kuan mExpansionView.setEnabled(!isReadOnly() && enabled); 141392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 142392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 143392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann /** 144392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * Creates or removes the type/label button. Doesn't do anything if already correctly configured 145392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann */ 14612a93637e406ba7f3da4db8d53cd035c2c1d6e6eKatherine Kuan private void setupExpansionView(boolean shouldExist, boolean collapsed) { 147392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann if (shouldExist) { 14812a93637e406ba7f3da4db8d53cd035c2c1d6e6eKatherine Kuan mExpansionViewContainer.setVisibility(View.VISIBLE); 14912a93637e406ba7f3da4db8d53cd035c2c1d6e6eKatherine Kuan mExpansionView.setImageResource(collapsed 150915ffc5ae1d10a7ed06d5541fe81da023fa3228fDaniel Lehmann ? R.drawable.ic_menu_expander_minimized_holo_light 151915ffc5ae1d10a7ed06d5541fe81da023fa3228fDaniel Lehmann : R.drawable.ic_menu_expander_maximized_holo_light); 15263ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan } else { 15312a93637e406ba7f3da4db8d53cd035c2c1d6e6eKatherine Kuan mExpansionViewContainer.setVisibility(View.GONE); 154392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 155392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 156392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 157392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann @Override 158392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann protected void requestFocusForFirstEditField() { 159392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann if (mFieldEditTexts != null && mFieldEditTexts.length != 0) { 160392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann EditText firstField = null; 161392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann boolean anyFieldHasFocus = false; 162392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann for (EditText editText : mFieldEditTexts) { 163392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann if (firstField == null && editText.getVisibility() == View.VISIBLE) { 164392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann firstField = editText; 165392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 166392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann if (editText.hasFocus()) { 167392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann anyFieldHasFocus = true; 168392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann break; 169392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 170392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 171392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann if (!anyFieldHasFocus && firstField != null) { 172392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann firstField.requestFocus(); 173392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 174392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 175392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 176392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 177392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann @Override 178851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu public void setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly, 179392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann ViewIdGenerator vig) { 180392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann super.setValues(kind, entry, state, readOnly, vig); 181392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann // Remove edit texts that we currently have 182392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann if (mFieldEditTexts != null) { 183392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann for (EditText fieldEditText : mFieldEditTexts) { 18463ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan mFields.removeView(fieldEditText); 185392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 186392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 187392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann boolean hidePossible = false; 188392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 189392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann int fieldCount = kind.fieldList.size(); 190392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann mFieldEditTexts = new EditText[fieldCount]; 191392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann for (int index = 0; index < fieldCount; index++) { 192392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann final EditField field = kind.fieldList.get(index); 193392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann final EditText fieldView = new EditText(mContext); 194392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann fieldView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 1951ebbd5bbe71814123b80df702b474bcd7196cbb7Katherine Kuan field.isMultiLine() ? LayoutParams.WRAP_CONTENT : mMinFieldHeight)); 1969db59fe67d9f2624120c718499a74eab7fea3c03Katherine Kuan // Set either a minimum line requirement or a minimum height (because {@link TextView} 1979db59fe67d9f2624120c718499a74eab7fea3c03Katherine Kuan // only takes one or the other at a single time). 1989db59fe67d9f2624120c718499a74eab7fea3c03Katherine Kuan if (field.minLines != 0) { 1999db59fe67d9f2624120c718499a74eab7fea3c03Katherine Kuan fieldView.setMinLines(field.minLines); 2009db59fe67d9f2624120c718499a74eab7fea3c03Katherine Kuan } else { 2019db59fe67d9f2624120c718499a74eab7fea3c03Katherine Kuan fieldView.setMinHeight(mMinFieldHeight); 2029db59fe67d9f2624120c718499a74eab7fea3c03Katherine Kuan } 20350a27b70581b0191995969c63edd1f6a3db3d1b7Makoto Onuki fieldView.setTextAppearance(getContext(), android.R.style.TextAppearance_Medium); 204392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann fieldView.setGravity(Gravity.TOP); 205392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann mFieldEditTexts[index] = fieldView; 206392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann fieldView.setId(vig.getId(state, kind, entry, index)); 207392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann if (field.titleRes > 0) { 208392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann fieldView.setHint(field.titleRes); 209392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 210392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann int inputType = field.inputType; 211392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann fieldView.setInputType(inputType); 212392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann if (inputType == InputType.TYPE_CLASS_PHONE) { 213e598332967106e3db63b73c701f21902d169efefMakoto Onuki PhoneNumberFormatter.setPhoneNumberFormattingTextWatcher(mContext, fieldView); 214392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 215392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 2160af02581eecabbe234e54364bdbbd65090518d7cKatherine Kuan // Show the "next" button in IME to navigate between text fields 2170af02581eecabbe234e54364bdbbd65090518d7cKatherine Kuan // TODO: Still need to properly navigate to/from sections without text fields, 2180af02581eecabbe234e54364bdbbd65090518d7cKatherine Kuan // See Bug: 5713510 2190af02581eecabbe234e54364bdbbd65090518d7cKatherine Kuan fieldView.setImeOptions(EditorInfo.IME_ACTION_NEXT); 2200af02581eecabbe234e54364bdbbd65090518d7cKatherine Kuan 221392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann // Read current value from state 222392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann final String column = field.column; 223392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann final String value = entry.getAsString(column); 224392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann fieldView.setText(value); 225392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 2265e330ed98f1c2711b4cd3b375a1c0f76bedaf3e1Katherine Kuan // Show the delete button if we have a non-null value 2275e330ed98f1c2711b4cd3b375a1c0f76bedaf3e1Katherine Kuan setDeleteButtonVisible(value != null); 2285e330ed98f1c2711b4cd3b375a1c0f76bedaf3e1Katherine Kuan 229392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann // Prepare listener for writing changes 230392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann fieldView.addTextChangedListener(new TextWatcher() { 231392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann @Override 232392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public void afterTextChanged(Editable s) { 233392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann // Trigger event for newly changed value 234392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann onFieldChanged(column, s.toString()); 235392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 236392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 237392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann @Override 238392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public void beforeTextChanged(CharSequence s, int start, int count, int after) { 239392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 240392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 241392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann @Override 242392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public void onTextChanged(CharSequence s, int start, int before, int count) { 243392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 244392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann }); 245392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 24602bb1252aa990813fa9e86ad75c458acc15e6801Dmitri Plotnikov fieldView.setEnabled(isEnabled() && !readOnly); 247392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 248392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann if (field.shortForm) { 249392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann hidePossible = true; 250392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann mHasShortAndLongForms = true; 251392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann fieldView.setVisibility(mHideOptional ? View.VISIBLE : View.GONE); 252392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } else if (field.longForm) { 253392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann hidePossible = true; 254392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann mHasShortAndLongForms = true; 255392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann fieldView.setVisibility(mHideOptional ? View.GONE : View.VISIBLE); 256392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } else { 257392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann // Hide field when empty and optional value 258392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann final boolean couldHide = (!ContactsUtils.isGraphic(value) && field.optional); 259392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann final boolean willHide = (mHideOptional && couldHide); 260392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann fieldView.setVisibility(willHide ? View.GONE : View.VISIBLE); 261392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann hidePossible = hidePossible || couldHide; 262392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 263392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 26463ffb90935d8aec295e4a8f62af6c77dafe1899fKatherine Kuan mFields.addView(fieldView); 265392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 266392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 267392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann // When hiding fields, place expandable 26812a93637e406ba7f3da4db8d53cd035c2c1d6e6eKatherine Kuan setupExpansionView(hidePossible, mHideOptional); 26912a93637e406ba7f3da4db8d53cd035c2c1d6e6eKatherine Kuan mExpansionView.setEnabled(!readOnly && isEnabled()); 270392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 271392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 2722591436971bad3050c85929c8e0c5a8d48111f88Katherine Kuan @Override 2732591436971bad3050c85929c8e0c5a8d48111f88Katherine Kuan public boolean isEmpty() { 2741b22073590c1801c3e6c7bde2f4636632a049f6cKatherine Kuan for (int i = 0; i < mFields.getChildCount(); i++) { 2751b22073590c1801c3e6c7bde2f4636632a049f6cKatherine Kuan EditText editText = (EditText) mFields.getChildAt(i); 2762591436971bad3050c85929c8e0c5a8d48111f88Katherine Kuan if (!TextUtils.isEmpty(editText.getText())) { 2772591436971bad3050c85929c8e0c5a8d48111f88Katherine Kuan return false; 2781b22073590c1801c3e6c7bde2f4636632a049f6cKatherine Kuan } 2791b22073590c1801c3e6c7bde2f4636632a049f6cKatherine Kuan } 2802591436971bad3050c85929c8e0c5a8d48111f88Katherine Kuan return true; 2811b22073590c1801c3e6c7bde2f4636632a049f6cKatherine Kuan } 2821b22073590c1801c3e6c7bde2f4636632a049f6cKatherine Kuan 283392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann /** 284392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * Returns true if the editor is currently configured to show optional fields. 285392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann */ 286392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public boolean areOptionalFieldsVisible() { 287392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann return !mHideOptional; 288392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 289392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 290392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public boolean hasShortAndLongForms() { 291392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann return mHasShortAndLongForms; 292392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 293392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 294392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann /** 295392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * Populates the bound rectangle with the bounds of the last editor field inside this view. 296392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann */ 297392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public void acquireEditorBounds(Rect bounds) { 298392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann if (mFieldEditTexts != null) { 299392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann for (int i = mFieldEditTexts.length; --i >= 0;) { 300392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann EditText editText = mFieldEditTexts[i]; 301392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann if (editText.getVisibility() == View.VISIBLE) { 302392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann bounds.set(editText.getLeft(), editText.getTop(), editText.getRight(), 303392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann editText.getBottom()); 304392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann return; 305392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 306392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 307392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 308392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 309392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 310392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann /** 311392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * Saves the visibility of the child EditTexts, and mHideOptional. 312392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann */ 313392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann @Override 314392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann protected Parcelable onSaveInstanceState() { 315392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann Parcelable superState = super.onSaveInstanceState(); 316392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann SavedState ss = new SavedState(superState); 317392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 318392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann ss.mHideOptional = mHideOptional; 319392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 3205ec40b04041818bc042c0671cb1a43b54f650e25Dmitri Plotnikov final int numChildren = mFieldEditTexts == null ? 0 : mFieldEditTexts.length; 321392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann ss.mVisibilities = new int[numChildren]; 322392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann for (int i = 0; i < numChildren; i++) { 323392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann ss.mVisibilities[i] = mFieldEditTexts[i].getVisibility(); 324392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 325392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 326392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann return ss; 327392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 328392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 329392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann /** 330392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann * Restores the visibility of the child EditTexts, and mHideOptional. 331392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann */ 332392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann @Override 333392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann protected void onRestoreInstanceState(Parcelable state) { 334392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann SavedState ss = (SavedState) state; 335392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann super.onRestoreInstanceState(ss.getSuperState()); 336392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 337392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann mHideOptional = ss.mHideOptional; 338392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 339392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann int numChildren = Math.min(mFieldEditTexts.length, ss.mVisibilities.length); 340392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann for (int i = 0; i < numChildren; i++) { 341392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann mFieldEditTexts[i].setVisibility(ss.mVisibilities[i]); 342392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 343392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 344392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 345392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann private static class SavedState extends BaseSavedState { 346392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public boolean mHideOptional; 347392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public int[] mVisibilities; 348392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 349392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann SavedState(Parcelable superState) { 350392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann super(superState); 351392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 352392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 353392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann private SavedState(Parcel in) { 354392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann super(in); 355392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann mVisibilities = new int[in.readInt()]; 356392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann in.readIntArray(mVisibilities); 357392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 358392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 359392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann @Override 360392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public void writeToParcel(Parcel out, int flags) { 361392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann super.writeToParcel(out, flags); 362392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann out.writeInt(mVisibilities.length); 363392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann out.writeIntArray(mVisibilities); 364392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 365392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 366cfb525b3f18d71d6cc5d4ef36d704b3ba9e9a6f3Daniel Lehmann @SuppressWarnings({"unused", "hiding" }) 367392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public static final Parcelable.Creator<SavedState> CREATOR 368392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann = new Parcelable.Creator<SavedState>() { 369392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann @Override 370392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public SavedState createFromParcel(Parcel in) { 371392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann return new SavedState(in); 372392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 373392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann 374392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann @Override 375392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann public SavedState[] newArray(int size) { 376392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann return new SavedState[size]; 377392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 378392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann }; 379392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann } 380da5bf1cf60beef3de5e651a569fa544293683926Dave Santoro 3812293e55d550fbc9974d1185960715e59acb14a85Katherine Kuan @Override 3822293e55d550fbc9974d1185960715e59acb14a85Katherine Kuan public void clearAllFields() { 3832293e55d550fbc9974d1185960715e59acb14a85Katherine Kuan if (mFieldEditTexts != null) { 3842293e55d550fbc9974d1185960715e59acb14a85Katherine Kuan for (EditText fieldEditText : mFieldEditTexts) { 3852293e55d550fbc9974d1185960715e59acb14a85Katherine Kuan // Update UI (which will trigger a state change through the {@link TextWatcher}) 3862293e55d550fbc9974d1185960715e59acb14a85Katherine Kuan fieldEditText.setText(""); 3872293e55d550fbc9974d1185960715e59acb14a85Katherine Kuan } 3882293e55d550fbc9974d1185960715e59acb14a85Katherine Kuan } 3892293e55d550fbc9974d1185960715e59acb14a85Katherine Kuan } 390392ccec3b56e8074a5a028af28106134b39f64bcDaniel Lehmann} 391