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
17package com.android.contacts.editor;
18
19import android.content.Context;
20import android.text.TextUtils;
21import android.util.AttributeSet;
22
23import com.android.contacts.common.model.RawContactDelta;
24import com.android.contacts.common.model.ValuesDelta;
25import com.android.contacts.common.model.dataitem.DataKind;
26import com.android.contacts.common.model.dataitem.StructuredNameDataItem;
27import com.android.contacts.common.util.NameConverter;
28
29/**
30 * A dedicated editor for phonetic name. It is similar to {@link StructuredNameEditorView}.
31 */
32public class PhoneticNameEditorView extends TextFieldsEditorView {
33
34    private static class PhoneticValuesDelta extends ValuesDelta {
35        private ValuesDelta mValues;
36        private String mPhoneticName;
37
38        public PhoneticValuesDelta(ValuesDelta values) {
39            mValues = values;
40            buildPhoneticName();
41        }
42
43        @Override
44        public void put(String key, String value) {
45            if (key.equals(DataKind.PSEUDO_COLUMN_PHONETIC_NAME)) {
46                mPhoneticName = value;
47                parsePhoneticName(value);
48            } else {
49                mValues.put(key, value);
50                buildPhoneticName();
51            }
52        }
53
54        @Override
55        public String getAsString(String key) {
56            if (key.equals(DataKind.PSEUDO_COLUMN_PHONETIC_NAME)) {
57                return mPhoneticName;
58            } else {
59                return mValues.getAsString(key);
60            }
61        }
62
63        private void parsePhoneticName(String value) {
64            StructuredNameDataItem dataItem = NameConverter.parsePhoneticName(value, null);
65            mValues.setPhoneticFamilyName(dataItem.getPhoneticFamilyName());
66            mValues.setPhoneticMiddleName(dataItem.getPhoneticMiddleName());
67            mValues.setPhoneticGivenName(dataItem.getPhoneticGivenName());
68        }
69
70        private void buildPhoneticName() {
71            String family = mValues.getPhoneticFamilyName();
72            String middle = mValues.getPhoneticMiddleName();
73            String given = mValues.getPhoneticGivenName();
74            mPhoneticName = NameConverter.buildPhoneticName(family, middle, given);
75        }
76
77        @Override
78        public Long getId() {
79            return mValues.getId();
80        }
81
82        @Override
83        public boolean isVisible() {
84            return mValues.isVisible();
85        }
86    }
87
88    public static boolean isUnstructuredPhoneticNameColumn(String column) {
89        return DataKind.PSEUDO_COLUMN_PHONETIC_NAME.equals(column);
90    }
91
92    public PhoneticNameEditorView(Context context) {
93        super(context);
94    }
95
96    public PhoneticNameEditorView(Context context, AttributeSet attrs) {
97        super(context, attrs);
98    }
99
100    public PhoneticNameEditorView(Context context, AttributeSet attrs, int defStyle) {
101        super(context, attrs, defStyle);
102    }
103
104    @Override
105    public void setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly,
106            ViewIdGenerator vig) {
107        if (!(entry instanceof PhoneticValuesDelta)) {
108            entry = new PhoneticValuesDelta(entry);
109        }
110        super.setValues(kind, entry, state, readOnly, vig);
111    }
112
113    @Override
114    public void onFieldChanged(String column, String value) {
115        if (!isFieldChanged(column, value)) {
116            return;
117        }
118
119        if (hasShortAndLongForms()) {
120            PhoneticValuesDelta entry = (PhoneticValuesDelta) getEntry();
121
122            // Determine whether the user is modifying the structured or unstructured phonetic
123            // name field. See a similar approach in {@link StructuredNameEditor#onFieldChanged}.
124            // This is because on device rotation, a hidden TextView's onRestoreInstanceState() will
125            // be called and incorrectly restore a null value for the hidden field, which ultimately
126            // modifies the underlying phonetic name. Hence, ignore onFieldChanged() update requests
127            // from fields that aren't visible.
128            boolean isEditingUnstructuredPhoneticName = !areOptionalFieldsVisible();
129
130            if (isEditingUnstructuredPhoneticName == isUnstructuredPhoneticNameColumn(column)) {
131                // Call into the superclass to update the field and rebuild the underlying
132                // phonetic name.
133                super.onFieldChanged(column, value);
134            }
135        } else {
136            // All fields are always visible, so we don't have to worry about blocking updates
137            // from onRestoreInstanceState() from hidden fields. Always call into the superclass
138            // to update the field and rebuild the underlying phonetic name.
139            super.onFieldChanged(column, value);
140        }
141    }
142
143    public boolean hasData() {
144        ValuesDelta entry = getEntry();
145
146        String family = entry.getPhoneticFamilyName();
147        String middle = entry.getPhoneticMiddleName();
148        String given = entry.getPhoneticGivenName();
149
150        return !TextUtils.isEmpty(family) || !TextUtils.isEmpty(middle)
151                || !TextUtils.isEmpty(given);
152    }
153}
154