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.util.AttributeSet;
25 import android.view.View;
26 
27 import com.android.contacts.R;
28 import com.android.contacts.model.RawContactDelta;
29 import com.android.contacts.model.ValuesDelta;
30 import com.android.contacts.model.dataitem.DataItem;
31 import com.android.contacts.model.dataitem.DataKind;
32 import com.android.contacts.model.dataitem.StructuredNameDataItem;
33 import com.android.contacts.util.NameConverter;
34 
35 /**
36  * A dedicated editor for structured name.
37  */
38 public class StructuredNameEditorView extends TextFieldsEditorView {
39 
40     private StructuredNameDataItem mSnapshot;
41     private boolean mChanged;
42 
StructuredNameEditorView(Context context)43     public StructuredNameEditorView(Context context) {
44         super(context);
45     }
46 
StructuredNameEditorView(Context context, AttributeSet attrs)47     public StructuredNameEditorView(Context context, AttributeSet attrs) {
48         super(context, attrs);
49     }
50 
StructuredNameEditorView(Context context, AttributeSet attrs, int defStyle)51     public StructuredNameEditorView(Context context, AttributeSet attrs, int defStyle) {
52         super(context, attrs, defStyle);
53     }
54 
55     @Override
onFinishInflate()56     protected void onFinishInflate() {
57         super.onFinishInflate();
58         final Resources res = getResources();
59         mCollapseButtonDescription = res
60                 .getString(R.string.collapse_name_fields_description);
61         mExpandButtonDescription = res
62                 .getString(R.string.expand_name_fields_description);
63     }
64 
65     @Override
setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly, ViewIdGenerator vig)66     public void setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly,
67             ViewIdGenerator vig) {
68         super.setValues(kind, entry, state, readOnly, vig);
69         if (mSnapshot == null) {
70             mSnapshot = (StructuredNameDataItem) DataItem.createFrom(
71                     new ContentValues(getValues().getCompleteValues()));
72             mChanged = entry.isInsert();
73         } else {
74             mChanged = false;
75         }
76         updateEmptiness();
77         // Right alien with rest of the editors. As this view has an extra expand/collapse view on
78         // the right, we need to free the space from deleteContainer
79         mDeleteContainer.setVisibility(View.GONE);
80     }
81 
82     @Override
onFieldChanged(String column, String value)83     public void onFieldChanged(String column, String value) {
84         if (!isFieldChanged(column, value)) {
85             return;
86         }
87 
88         // First save the new value for the column.
89         saveValue(column, value);
90         mChanged = true;
91 
92         // Then notify the listener.
93         notifyEditorListener();
94     }
95 
96     /**
97      * Returns the display name currently displayed in the editor.
98      */
getDisplayName()99     public String getDisplayName() {
100         return NameConverter.structuredNameToDisplayName(getContext(),
101                 getValues().getCompleteValues());
102     }
103 
104     @Override
onSaveInstanceState()105     protected Parcelable onSaveInstanceState() {
106         SavedState state = new SavedState(super.onSaveInstanceState());
107         state.mChanged = mChanged;
108         state.mSnapshot = mSnapshot.getContentValues();
109         return state;
110     }
111 
112     @Override
onRestoreInstanceState(Parcelable state)113     protected void onRestoreInstanceState(Parcelable state) {
114         SavedState ss = (SavedState) state;
115         super.onRestoreInstanceState(ss.mSuperState);
116 
117         mChanged = ss.mChanged;
118         mSnapshot = (StructuredNameDataItem) DataItem.createFrom(ss.mSnapshot);
119     }
120 
121     private static class SavedState implements Parcelable {
122         public boolean mChanged;
123         public ContentValues mSnapshot;
124         public Parcelable mSuperState;
125 
SavedState(Parcelable superState)126         SavedState(Parcelable superState) {
127             mSuperState = superState;
128         }
129 
SavedState(Parcel in)130         private SavedState(Parcel in) {
131             ClassLoader loader = getClass().getClassLoader();
132             mSuperState = in.readParcelable(loader);
133 
134             mChanged = in.readInt() != 0;
135             mSnapshot = in.readParcelable(loader);
136         }
137 
138         @Override
writeToParcel(Parcel out, int flags)139         public void writeToParcel(Parcel out, int flags) {
140             out.writeParcelable(mSuperState, 0);
141 
142             out.writeInt(mChanged ? 1 : 0);
143             out.writeParcelable(mSnapshot, 0);
144         }
145 
146         @SuppressWarnings({"unused"})
147         public static final Parcelable.Creator<SavedState> CREATOR
148                 = new Parcelable.Creator<SavedState>() {
149             @Override
150             public SavedState createFromParcel(Parcel in) {
151                 return new SavedState(in);
152             }
153 
154             @Override
155             public SavedState[] newArray(int size) {
156                 return new SavedState[size];
157             }
158         };
159 
160         @Override
describeContents()161         public int describeContents() {
162             return 0;
163         }
164     }
165 }
166