Home | History | Annotate | Download | only in editor
      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.Context;
     20 import android.text.TextUtils;
     21 import android.util.AttributeSet;
     22 
     23 import com.android.contacts.common.model.RawContactDelta;
     24 import com.android.contacts.common.model.ValuesDelta;
     25 import com.android.contacts.common.model.dataitem.DataKind;
     26 import com.android.contacts.common.model.dataitem.StructuredNameDataItem;
     27 import com.android.contacts.common.util.NameConverter;
     28 
     29 /**
     30  * A dedicated editor for phonetic name. It is similar to {@link StructuredNameEditorView}.
     31  */
     32 public 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         updateEmptiness();
    112     }
    113 
    114     @Override
    115     public void onFieldChanged(String column, String value) {
    116         if (!isFieldChanged(column, value)) {
    117             return;
    118         }
    119 
    120         if (hasShortAndLongForms()) {
    121             PhoneticValuesDelta entry = (PhoneticValuesDelta) getEntry();
    122 
    123             // Determine whether the user is modifying the structured or unstructured phonetic
    124             // name field. See a similar approach in {@link StructuredNameEditor#onFieldChanged}.
    125             // This is because on device rotation, a hidden TextView's onRestoreInstanceState() will
    126             // be called and incorrectly restore a null value for the hidden field, which ultimately
    127             // modifies the underlying phonetic name. Hence, ignore onFieldChanged() update requests
    128             // from fields that aren't visible.
    129             boolean isEditingUnstructuredPhoneticName = !areOptionalFieldsVisible();
    130 
    131             if (isEditingUnstructuredPhoneticName == isUnstructuredPhoneticNameColumn(column)) {
    132                 // Call into the superclass to update the field and rebuild the underlying
    133                 // phonetic name.
    134                 super.onFieldChanged(column, value);
    135             }
    136         } else {
    137             // All fields are always visible, so we don't have to worry about blocking updates
    138             // from onRestoreInstanceState() from hidden fields. Always call into the superclass
    139             // to update the field and rebuild the underlying phonetic name.
    140             super.onFieldChanged(column, value);
    141         }
    142     }
    143 
    144     public boolean hasData() {
    145         ValuesDelta entry = getEntry();
    146 
    147         String family = entry.getPhoneticFamilyName();
    148         String middle = entry.getPhoneticMiddleName();
    149         String given = entry.getPhoneticGivenName();
    150 
    151         return !TextUtils.isEmpty(family) || !TextUtils.isEmpty(middle)
    152                 || !TextUtils.isEmpty(given);
    153     }
    154 }
    155