blob: c96548c621ec8769f877fe11b7699ee47fe1b59d [file] [log] [blame]
Dmitri Plotnikov5ec40b02011-03-01 18:36:16 -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 Plotnikov5ec40b02011-03-01 18:36:16 -080019import android.content.Context;
Gary Mai1c6298b2016-09-09 14:34:52 -070020import android.content.res.Resources;
Dmitri Plotnikov5ec40b02011-03-01 18:36:16 -080021import android.text.TextUtils;
22import android.util.AttributeSet;
23
Gary Mai1c6298b2016-09-09 14:34:52 -070024import com.android.contacts.R;
Gary Mai69c182a2016-12-05 13:07:03 -080025import com.android.contacts.model.RawContactDelta;
26import com.android.contacts.model.ValuesDelta;
27import com.android.contacts.model.dataitem.DataKind;
28import com.android.contacts.model.dataitem.StructuredNameDataItem;
29import com.android.contacts.util.NameConverter;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070030
Dmitri Plotnikov5ec40b02011-03-01 18:36:16 -080031/**
32 * A dedicated editor for phonetic name. It is similar to {@link StructuredNameEditorView}.
33 */
34public class PhoneticNameEditorView extends TextFieldsEditorView {
35
36 private static class PhoneticValuesDelta extends ValuesDelta {
37 private ValuesDelta mValues;
38 private String mPhoneticName;
39
40 public PhoneticValuesDelta(ValuesDelta values) {
41 mValues = values;
42 buildPhoneticName();
43 }
44
45 @Override
46 public void put(String key, String value) {
47 if (key.equals(DataKind.PSEUDO_COLUMN_PHONETIC_NAME)) {
48 mPhoneticName = value;
49 parsePhoneticName(value);
50 } else {
51 mValues.put(key, value);
52 buildPhoneticName();
53 }
54 }
55
56 @Override
57 public String getAsString(String key) {
58 if (key.equals(DataKind.PSEUDO_COLUMN_PHONETIC_NAME)) {
59 return mPhoneticName;
60 } else {
61 return mValues.getAsString(key);
62 }
63 }
64
65 private void parsePhoneticName(String value) {
Yorke Leecd321f62013-10-28 15:20:15 -070066 StructuredNameDataItem dataItem = NameConverter.parsePhoneticName(value, null);
Maurice Chu851222a2012-06-21 11:43:08 -070067 mValues.setPhoneticFamilyName(dataItem.getPhoneticFamilyName());
68 mValues.setPhoneticMiddleName(dataItem.getPhoneticMiddleName());
69 mValues.setPhoneticGivenName(dataItem.getPhoneticGivenName());
Dmitri Plotnikov5ec40b02011-03-01 18:36:16 -080070 }
71
72 private void buildPhoneticName() {
Maurice Chu851222a2012-06-21 11:43:08 -070073 String family = mValues.getPhoneticFamilyName();
74 String middle = mValues.getPhoneticMiddleName();
75 String given = mValues.getPhoneticGivenName();
Yorke Leecd321f62013-10-28 15:20:15 -070076 mPhoneticName = NameConverter.buildPhoneticName(family, middle, given);
Dmitri Plotnikov5ec40b02011-03-01 18:36:16 -080077 }
78
79 @Override
80 public Long getId() {
81 return mValues.getId();
82 }
83
84 @Override
85 public boolean isVisible() {
86 return mValues.isVisible();
87 }
88 }
89
Katherine Kuan1a7f4242011-11-15 17:49:22 -080090 public static boolean isUnstructuredPhoneticNameColumn(String column) {
91 return DataKind.PSEUDO_COLUMN_PHONETIC_NAME.equals(column);
92 }
93
Dmitri Plotnikov5ec40b02011-03-01 18:36:16 -080094 public PhoneticNameEditorView(Context context) {
95 super(context);
96 }
97
98 public PhoneticNameEditorView(Context context, AttributeSet attrs) {
99 super(context, attrs);
100 }
101
102 public PhoneticNameEditorView(Context context, AttributeSet attrs, int defStyle) {
103 super(context, attrs, defStyle);
104 }
105
106 @Override
Gary Mai1c6298b2016-09-09 14:34:52 -0700107 protected void onFinishInflate() {
108 super.onFinishInflate();
109 final Resources res = getResources();
110 mCollapseButtonDescription = res
111 .getString(R.string.collapse_phonetic_name_fields_description);
112 mExpandButtonDescription = res
113 .getString(R.string.expand_phonetic_name_fields_description);
114 }
115
116 @Override
Maurice Chu851222a2012-06-21 11:43:08 -0700117 public void setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly,
Dmitri Plotnikov5ec40b02011-03-01 18:36:16 -0800118 ViewIdGenerator vig) {
119 if (!(entry instanceof PhoneticValuesDelta)) {
120 entry = new PhoneticValuesDelta(entry);
121 }
122 super.setValues(kind, entry, state, readOnly, vig);
Brian Attwell8f9d84f2014-11-03 23:17:04 -0800123 updateEmptiness();
Dmitri Plotnikov5ec40b02011-03-01 18:36:16 -0800124 }
125
Katherine Kuan1a7f4242011-11-15 17:49:22 -0800126 @Override
127 public void onFieldChanged(String column, String value) {
128 if (!isFieldChanged(column, value)) {
129 return;
130 }
131
132 if (hasShortAndLongForms()) {
133 PhoneticValuesDelta entry = (PhoneticValuesDelta) getEntry();
134
135 // Determine whether the user is modifying the structured or unstructured phonetic
136 // name field. See a similar approach in {@link StructuredNameEditor#onFieldChanged}.
137 // This is because on device rotation, a hidden TextView's onRestoreInstanceState() will
138 // be called and incorrectly restore a null value for the hidden field, which ultimately
139 // modifies the underlying phonetic name. Hence, ignore onFieldChanged() update requests
140 // from fields that aren't visible.
141 boolean isEditingUnstructuredPhoneticName = !areOptionalFieldsVisible();
142
143 if (isEditingUnstructuredPhoneticName == isUnstructuredPhoneticNameColumn(column)) {
144 // Call into the superclass to update the field and rebuild the underlying
145 // phonetic name.
146 super.onFieldChanged(column, value);
147 }
Katherine Kuan223dea32011-12-05 17:56:08 -0800148 } else {
149 // All fields are always visible, so we don't have to worry about blocking updates
150 // from onRestoreInstanceState() from hidden fields. Always call into the superclass
151 // to update the field and rebuild the underlying phonetic name.
152 super.onFieldChanged(column, value);
Katherine Kuan1a7f4242011-11-15 17:49:22 -0800153 }
154 }
155
Dmitri Plotnikov5ec40b02011-03-01 18:36:16 -0800156 public boolean hasData() {
157 ValuesDelta entry = getEntry();
158
Maurice Chu851222a2012-06-21 11:43:08 -0700159 String family = entry.getPhoneticFamilyName();
160 String middle = entry.getPhoneticMiddleName();
161 String given = entry.getPhoneticGivenName();
Dmitri Plotnikov5ec40b02011-03-01 18:36:16 -0800162
163 return !TextUtils.isEmpty(family) || !TextUtils.isEmpty(middle)
164 || !TextUtils.isEmpty(given);
165 }
Dmitri Plotnikov5ec40b02011-03-01 18:36:16 -0800166}