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 17 package com.android.contacts.editor; 18 19 import android.content.ContentValues; 20 import android.content.Context; 21 import android.content.res.Resources; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 import android.provider.ContactsContract.CommonDataKinds.StructuredName; 25 import android.text.Editable; 26 import android.util.AttributeSet; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.EditText; 30 31 import com.android.contacts.R; 32 import com.android.contacts.model.RawContactDelta; 33 import com.android.contacts.model.ValuesDelta; 34 import com.android.contacts.model.dataitem.DataItem; 35 import com.android.contacts.model.dataitem.DataKind; 36 import com.android.contacts.model.dataitem.StructuredNameDataItem; 37 import com.android.contacts.util.NameConverter; 38 39 /** 40 * A dedicated editor for structured name. 41 */ 42 public class StructuredNameEditorView extends TextFieldsEditorView { 43 44 private StructuredNameDataItem mSnapshot; 45 private boolean mChanged; 46 47 private TextFieldsEditorView mPhoneticView; 48 StructuredNameEditorView(Context context)49 public StructuredNameEditorView(Context context) { 50 super(context); 51 } 52 StructuredNameEditorView(Context context, AttributeSet attrs)53 public StructuredNameEditorView(Context context, AttributeSet attrs) { 54 super(context, attrs); 55 } 56 StructuredNameEditorView(Context context, AttributeSet attrs, int defStyle)57 public StructuredNameEditorView(Context context, AttributeSet attrs, int defStyle) { 58 super(context, attrs, defStyle); 59 } 60 61 @Override onFinishInflate()62 protected void onFinishInflate() { 63 super.onFinishInflate(); 64 final Resources res = getResources(); 65 mCollapseButtonDescription = res 66 .getString(R.string.collapse_name_fields_description); 67 mExpandButtonDescription = res 68 .getString(R.string.expand_name_fields_description); 69 } 70 71 @Override setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly, ViewIdGenerator vig)72 public void setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly, 73 ViewIdGenerator vig) { 74 super.setValues(kind, entry, state, readOnly, vig); 75 if (mSnapshot == null) { 76 mSnapshot = (StructuredNameDataItem) DataItem.createFrom( 77 new ContentValues(getValues().getCompleteValues())); 78 mChanged = entry.isInsert(); 79 } else { 80 mChanged = false; 81 } 82 updateEmptiness(); 83 // Right alien with rest of the editors. As this view has an extra expand/collapse view on 84 // the right, we need to free the space from deleteContainer 85 mDeleteContainer.setVisibility(View.GONE); 86 } 87 88 @Override onFieldChanged(String column, String value)89 public void onFieldChanged(String column, String value) { 90 if (!isFieldChanged(column, value)) { 91 return; 92 } 93 94 // First save the new value for the column. 95 saveValue(column, value); 96 mChanged = true; 97 98 // Then notify the listener. 99 notifyEditorListener(); 100 } 101 updatePhonetic(String column, String value)102 public void updatePhonetic(String column, String value) { 103 EditText view = null; 104 105 if (mPhoneticView != null) { 106 ViewGroup fields = (ViewGroup) mPhoneticView.findViewById(R.id.editors); 107 108 if (StructuredName.FAMILY_NAME.equals(column)) { 109 view = (EditText) fields.getChildAt(0); 110 } else if (StructuredName.GIVEN_NAME.equals(column)) { 111 view = (EditText) fields.getChildAt(2); 112 } else if (StructuredName.MIDDLE_NAME.equals(column)) { 113 view = (EditText) fields.getChildAt(1); 114 } 115 116 if (view != null) { 117 view.setText(value); 118 } 119 } 120 } 121 122 @Override getPhonetic(String column)123 public String getPhonetic(String column) { 124 String input = ""; 125 EditText view = null; 126 127 if (mPhoneticView != null) { 128 ViewGroup fields = (ViewGroup) mPhoneticView.findViewById(R.id.editors); 129 130 if (StructuredName.FAMILY_NAME.equals(column)) { 131 view = (EditText) fields.getChildAt(0); 132 } else if (StructuredName.GIVEN_NAME.equals(column)) { 133 view = (EditText) fields.getChildAt(2); 134 } else if (StructuredName.MIDDLE_NAME.equals(column)) { 135 view = (EditText) fields.getChildAt(1); 136 } 137 138 if (view != null) { 139 input = view.getText().toString(); 140 } 141 } 142 return input; 143 } 144 setPhoneticView(TextFieldsEditorView phoneticNameEditor)145 public void setPhoneticView(TextFieldsEditorView phoneticNameEditor) { 146 mPhoneticView = phoneticNameEditor; 147 } 148 149 /** 150 * Returns the display name currently displayed in the editor. 151 */ getDisplayName()152 public String getDisplayName() { 153 return NameConverter.structuredNameToDisplayName(getContext(), 154 getValues().getCompleteValues()); 155 } 156 157 @Override onSaveInstanceState()158 protected Parcelable onSaveInstanceState() { 159 SavedState state = new SavedState(super.onSaveInstanceState()); 160 state.mChanged = mChanged; 161 state.mSnapshot = mSnapshot.getContentValues(); 162 return state; 163 } 164 165 @Override onRestoreInstanceState(Parcelable state)166 protected void onRestoreInstanceState(Parcelable state) { 167 SavedState ss = (SavedState) state; 168 super.onRestoreInstanceState(ss.mSuperState); 169 170 mChanged = ss.mChanged; 171 mSnapshot = (StructuredNameDataItem) DataItem.createFrom(ss.mSnapshot); 172 } 173 174 private static class SavedState implements Parcelable { 175 public boolean mChanged; 176 public ContentValues mSnapshot; 177 public Parcelable mSuperState; 178 SavedState(Parcelable superState)179 SavedState(Parcelable superState) { 180 mSuperState = superState; 181 } 182 SavedState(Parcel in)183 private SavedState(Parcel in) { 184 ClassLoader loader = getClass().getClassLoader(); 185 mSuperState = in.readParcelable(loader); 186 187 mChanged = in.readInt() != 0; 188 mSnapshot = in.readParcelable(loader); 189 } 190 191 @Override writeToParcel(Parcel out, int flags)192 public void writeToParcel(Parcel out, int flags) { 193 out.writeParcelable(mSuperState, 0); 194 195 out.writeInt(mChanged ? 1 : 0); 196 out.writeParcelable(mSnapshot, 0); 197 } 198 199 @SuppressWarnings({"unused"}) 200 public static final Parcelable.Creator<SavedState> CREATOR 201 = new Parcelable.Creator<SavedState>() { 202 @Override 203 public SavedState createFromParcel(Parcel in) { 204 return new SavedState(in); 205 } 206 207 @Override 208 public SavedState[] newArray(int size) { 209 return new SavedState[size]; 210 } 211 }; 212 213 @Override describeContents()214 public int describeContents() { 215 return 0; 216 } 217 } 218 } 219