blob: 5aca809a60aad5a6501f6aa61d45c1641c984621 [file] [log] [blame]
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -08001/*
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
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -080019import android.content.ContentValues;
20import android.content.Context;
Gary Mai1c6298b2016-09-09 14:34:52 -070021import android.content.res.Resources;
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -080022import android.os.Parcel;
23import android.os.Parcelable;
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -080024import android.util.AttributeSet;
25
Gary Mai1c6298b2016-09-09 14:34:52 -070026import com.android.contacts.R;
Yorke Leecd321f62013-10-28 15:20:15 -070027import com.android.contacts.common.model.RawContactDelta;
Chiao Cheng738ff862012-11-30 12:06:03 -080028import com.android.contacts.common.model.ValuesDelta;
Yorke Leecd321f62013-10-28 15:20:15 -070029import com.android.contacts.common.model.dataitem.DataItem;
Chiao Cheng428f0082012-11-13 18:38:56 -080030import com.android.contacts.common.model.dataitem.DataKind;
Yorke Leecd321f62013-10-28 15:20:15 -070031import com.android.contacts.common.model.dataitem.StructuredNameDataItem;
Gary Mai1c6298b2016-09-09 14:34:52 -070032import com.android.contacts.common.util.NameConverter;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070033
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -080034/**
Gary Mai698cee72016-09-19 16:09:54 -070035 * A dedicated editor for structured name.
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -080036 */
37public class StructuredNameEditorView extends TextFieldsEditorView {
38
Maurice Chu851222a2012-06-21 11:43:08 -070039 private StructuredNameDataItem mSnapshot;
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -080040 private boolean mChanged;
41
42 public StructuredNameEditorView(Context context) {
43 super(context);
44 }
45
46 public StructuredNameEditorView(Context context, AttributeSet attrs) {
47 super(context, attrs);
48 }
49
50 public StructuredNameEditorView(Context context, AttributeSet attrs, int defStyle) {
51 super(context, attrs, defStyle);
52 }
53
54 @Override
Gary Mai1c6298b2016-09-09 14:34:52 -070055 protected void onFinishInflate() {
56 super.onFinishInflate();
57 final Resources res = getResources();
58 mCollapseButtonDescription = res
59 .getString(R.string.collapse_name_fields_description);
60 mExpandButtonDescription = res
61 .getString(R.string.expand_name_fields_description);
62 }
63
64 @Override
Maurice Chu851222a2012-06-21 11:43:08 -070065 public void setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly,
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -080066 ViewIdGenerator vig) {
67 super.setValues(kind, entry, state, readOnly, vig);
68 if (mSnapshot == null) {
Chiao Cheng47b6f702012-09-07 17:28:17 -070069 mSnapshot = (StructuredNameDataItem) DataItem.createFrom(
Maurice Chu851222a2012-06-21 11:43:08 -070070 new ContentValues(getValues().getCompleteValues()));
Dmitri Plotnikovc77f4892011-02-28 16:29:21 -080071 mChanged = entry.isInsert();
72 } else {
73 mChanged = false;
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -080074 }
Brian Attwell8f9d84f2014-11-03 23:17:04 -080075 updateEmptiness();
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -080076 }
77
78 @Override
79 public void onFieldChanged(String column, String value) {
80 if (!isFieldChanged(column, value)) {
81 return;
82 }
83
Katherine Kuane55faef2011-12-06 17:58:43 -080084 // First save the new value for the column.
85 saveValue(column, value);
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -080086 mChanged = true;
87
Gary Mai698cee72016-09-19 16:09:54 -070088 // Then notify the listener.
Katherine Kuane55faef2011-12-06 17:58:43 -080089 notifyEditorListener();
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -080090 }
91
Walter Jang4f5594a2015-10-06 18:40:31 -070092 /**
93 * Returns the display name currently displayed in the editor.
94 */
95 public String getDisplayName() {
Gary Mai698cee72016-09-19 16:09:54 -070096 return NameConverter.structuredNameToDisplayName(getContext(),
97 getValues().getCompleteValues());
Walter Jang4f5594a2015-10-06 18:40:31 -070098 }
99
Walter Jang25f96942015-06-15 13:55:15 -0700100 @Override
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -0800101 protected Parcelable onSaveInstanceState() {
102 SavedState state = new SavedState(super.onSaveInstanceState());
103 state.mChanged = mChanged;
Maurice Chu851222a2012-06-21 11:43:08 -0700104 state.mSnapshot = mSnapshot.getContentValues();
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -0800105 return state;
106 }
107
108 @Override
109 protected void onRestoreInstanceState(Parcelable state) {
110 SavedState ss = (SavedState) state;
Dmitri Plotnikov83cc1172011-03-01 10:59:23 -0800111 super.onRestoreInstanceState(ss.mSuperState);
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -0800112
113 mChanged = ss.mChanged;
Chiao Cheng47b6f702012-09-07 17:28:17 -0700114 mSnapshot = (StructuredNameDataItem) DataItem.createFrom(ss.mSnapshot);
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -0800115 }
116
Dmitri Plotnikov83cc1172011-03-01 10:59:23 -0800117 private static class SavedState implements Parcelable {
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -0800118 public boolean mChanged;
119 public ContentValues mSnapshot;
Dmitri Plotnikov83cc1172011-03-01 10:59:23 -0800120 public Parcelable mSuperState;
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -0800121
122 SavedState(Parcelable superState) {
Dmitri Plotnikov83cc1172011-03-01 10:59:23 -0800123 mSuperState = superState;
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -0800124 }
125
126 private SavedState(Parcel in) {
Dmitri Plotnikov83cc1172011-03-01 10:59:23 -0800127 ClassLoader loader = getClass().getClassLoader();
128 mSuperState = in.readParcelable(loader);
129
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -0800130 mChanged = in.readInt() != 0;
Dmitri Plotnikov83cc1172011-03-01 10:59:23 -0800131 mSnapshot = in.readParcelable(loader);
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -0800132 }
133
134 @Override
135 public void writeToParcel(Parcel out, int flags) {
Dmitri Plotnikov83cc1172011-03-01 10:59:23 -0800136 out.writeParcelable(mSuperState, 0);
137
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -0800138 out.writeInt(mChanged ? 1 : 0);
139 out.writeParcelable(mSnapshot, 0);
140 }
141
Dmitri Plotnikov83cc1172011-03-01 10:59:23 -0800142 @SuppressWarnings({"unused"})
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -0800143 public static final Parcelable.Creator<SavedState> CREATOR
144 = new Parcelable.Creator<SavedState>() {
145 @Override
146 public SavedState createFromParcel(Parcel in) {
147 return new SavedState(in);
148 }
149
150 @Override
151 public SavedState[] newArray(int size) {
152 return new SavedState[size];
153 }
154 };
Dmitri Plotnikov83cc1172011-03-01 10:59:23 -0800155
156 @Override
157 public int describeContents() {
158 return 0;
159 }
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -0800160 }
Dmitri Plotnikov11bb73b2011-02-25 20:39:58 -0800161}