Dmitri Plotnikov | 5ec40b0 | 2011-03-01 18:36:16 -0800 | [diff] [blame] | 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 | |
Dmitri Plotnikov | 5ec40b0 | 2011-03-01 18:36:16 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Gary Mai | 1c6298b | 2016-09-09 14:34:52 -0700 | [diff] [blame] | 20 | import android.content.res.Resources; |
Dmitri Plotnikov | 5ec40b0 | 2011-03-01 18:36:16 -0800 | [diff] [blame] | 21 | import android.text.TextUtils; |
| 22 | import android.util.AttributeSet; |
| 23 | |
Gary Mai | 1c6298b | 2016-09-09 14:34:52 -0700 | [diff] [blame] | 24 | import com.android.contacts.R; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 25 | import com.android.contacts.model.RawContactDelta; |
| 26 | import com.android.contacts.model.ValuesDelta; |
| 27 | import com.android.contacts.model.dataitem.DataKind; |
| 28 | import com.android.contacts.model.dataitem.StructuredNameDataItem; |
| 29 | import com.android.contacts.util.NameConverter; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 30 | |
Dmitri Plotnikov | 5ec40b0 | 2011-03-01 18:36:16 -0800 | [diff] [blame] | 31 | /** |
| 32 | * A dedicated editor for phonetic name. It is similar to {@link StructuredNameEditorView}. |
| 33 | */ |
| 34 | public 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 Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 66 | StructuredNameDataItem dataItem = NameConverter.parsePhoneticName(value, null); |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 67 | mValues.setPhoneticFamilyName(dataItem.getPhoneticFamilyName()); |
| 68 | mValues.setPhoneticMiddleName(dataItem.getPhoneticMiddleName()); |
| 69 | mValues.setPhoneticGivenName(dataItem.getPhoneticGivenName()); |
Dmitri Plotnikov | 5ec40b0 | 2011-03-01 18:36:16 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | private void buildPhoneticName() { |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 73 | String family = mValues.getPhoneticFamilyName(); |
| 74 | String middle = mValues.getPhoneticMiddleName(); |
| 75 | String given = mValues.getPhoneticGivenName(); |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 76 | mPhoneticName = NameConverter.buildPhoneticName(family, middle, given); |
Dmitri Plotnikov | 5ec40b0 | 2011-03-01 18:36:16 -0800 | [diff] [blame] | 77 | } |
| 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 Kuan | 1a7f424 | 2011-11-15 17:49:22 -0800 | [diff] [blame] | 90 | public static boolean isUnstructuredPhoneticNameColumn(String column) { |
| 91 | return DataKind.PSEUDO_COLUMN_PHONETIC_NAME.equals(column); |
| 92 | } |
| 93 | |
Dmitri Plotnikov | 5ec40b0 | 2011-03-01 18:36:16 -0800 | [diff] [blame] | 94 | 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 Mai | 1c6298b | 2016-09-09 14:34:52 -0700 | [diff] [blame] | 107 | 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 Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 117 | public void setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly, |
Dmitri Plotnikov | 5ec40b0 | 2011-03-01 18:36:16 -0800 | [diff] [blame] | 118 | ViewIdGenerator vig) { |
| 119 | if (!(entry instanceof PhoneticValuesDelta)) { |
| 120 | entry = new PhoneticValuesDelta(entry); |
| 121 | } |
| 122 | super.setValues(kind, entry, state, readOnly, vig); |
Brian Attwell | 8f9d84f | 2014-11-03 23:17:04 -0800 | [diff] [blame] | 123 | updateEmptiness(); |
Dmitri Plotnikov | 5ec40b0 | 2011-03-01 18:36:16 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Katherine Kuan | 1a7f424 | 2011-11-15 17:49:22 -0800 | [diff] [blame] | 126 | @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 Kuan | 223dea3 | 2011-12-05 17:56:08 -0800 | [diff] [blame] | 148 | } 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 Kuan | 1a7f424 | 2011-11-15 17:49:22 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Dmitri Plotnikov | 5ec40b0 | 2011-03-01 18:36:16 -0800 | [diff] [blame] | 156 | public boolean hasData() { |
| 157 | ValuesDelta entry = getEntry(); |
| 158 | |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 159 | String family = entry.getPhoneticFamilyName(); |
| 160 | String middle = entry.getPhoneticMiddleName(); |
| 161 | String given = entry.getPhoneticGivenName(); |
Dmitri Plotnikov | 5ec40b0 | 2011-03-01 18:36:16 -0800 | [diff] [blame] | 162 | |
| 163 | return !TextUtils.isEmpty(family) || !TextUtils.isEmpty(middle) |
| 164 | || !TextUtils.isEmpty(given); |
| 165 | } |
Dmitri Plotnikov | 5ec40b0 | 2011-03-01 18:36:16 -0800 | [diff] [blame] | 166 | } |