Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -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 | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 19 | import android.content.ContentValues; |
| 20 | import android.content.Context; |
Gary Mai | 1c6298b | 2016-09-09 14:34:52 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 24 | import android.util.AttributeSet; |
| 25 | |
Gary Mai | 1c6298b | 2016-09-09 14:34:52 -0700 | [diff] [blame] | 26 | import com.android.contacts.R; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 27 | import com.android.contacts.common.model.RawContactDelta; |
Chiao Cheng | 738ff86 | 2012-11-30 12:06:03 -0800 | [diff] [blame] | 28 | import com.android.contacts.common.model.ValuesDelta; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 29 | import com.android.contacts.common.model.dataitem.DataItem; |
Chiao Cheng | 428f008 | 2012-11-13 18:38:56 -0800 | [diff] [blame] | 30 | import com.android.contacts.common.model.dataitem.DataKind; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 31 | import com.android.contacts.common.model.dataitem.StructuredNameDataItem; |
Gary Mai | 1c6298b | 2016-09-09 14:34:52 -0700 | [diff] [blame] | 32 | import com.android.contacts.common.util.NameConverter; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 33 | |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 34 | /** |
Gary Mai | 698cee7 | 2016-09-19 16:09:54 -0700 | [diff] [blame] | 35 | * A dedicated editor for structured name. |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 36 | */ |
| 37 | public class StructuredNameEditorView extends TextFieldsEditorView { |
| 38 | |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 39 | private StructuredNameDataItem mSnapshot; |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 40 | 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 Mai | 1c6298b | 2016-09-09 14:34:52 -0700 | [diff] [blame] | 55 | 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 Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 65 | public void setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly, |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 66 | ViewIdGenerator vig) { |
| 67 | super.setValues(kind, entry, state, readOnly, vig); |
| 68 | if (mSnapshot == null) { |
Chiao Cheng | 47b6f70 | 2012-09-07 17:28:17 -0700 | [diff] [blame] | 69 | mSnapshot = (StructuredNameDataItem) DataItem.createFrom( |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 70 | new ContentValues(getValues().getCompleteValues())); |
Dmitri Plotnikov | c77f489 | 2011-02-28 16:29:21 -0800 | [diff] [blame] | 71 | mChanged = entry.isInsert(); |
| 72 | } else { |
| 73 | mChanged = false; |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 74 | } |
Brian Attwell | 8f9d84f | 2014-11-03 23:17:04 -0800 | [diff] [blame] | 75 | updateEmptiness(); |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void onFieldChanged(String column, String value) { |
| 80 | if (!isFieldChanged(column, value)) { |
| 81 | return; |
| 82 | } |
| 83 | |
Katherine Kuan | e55faef | 2011-12-06 17:58:43 -0800 | [diff] [blame] | 84 | // First save the new value for the column. |
| 85 | saveValue(column, value); |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 86 | mChanged = true; |
| 87 | |
Gary Mai | 698cee7 | 2016-09-19 16:09:54 -0700 | [diff] [blame] | 88 | // Then notify the listener. |
Katherine Kuan | e55faef | 2011-12-06 17:58:43 -0800 | [diff] [blame] | 89 | notifyEditorListener(); |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Walter Jang | 4f5594a | 2015-10-06 18:40:31 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Returns the display name currently displayed in the editor. |
| 94 | */ |
| 95 | public String getDisplayName() { |
Gary Mai | 698cee7 | 2016-09-19 16:09:54 -0700 | [diff] [blame] | 96 | return NameConverter.structuredNameToDisplayName(getContext(), |
| 97 | getValues().getCompleteValues()); |
Walter Jang | 4f5594a | 2015-10-06 18:40:31 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Walter Jang | 25f9694 | 2015-06-15 13:55:15 -0700 | [diff] [blame] | 100 | @Override |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 101 | protected Parcelable onSaveInstanceState() { |
| 102 | SavedState state = new SavedState(super.onSaveInstanceState()); |
| 103 | state.mChanged = mChanged; |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 104 | state.mSnapshot = mSnapshot.getContentValues(); |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 105 | return state; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected void onRestoreInstanceState(Parcelable state) { |
| 110 | SavedState ss = (SavedState) state; |
Dmitri Plotnikov | 83cc117 | 2011-03-01 10:59:23 -0800 | [diff] [blame] | 111 | super.onRestoreInstanceState(ss.mSuperState); |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 112 | |
| 113 | mChanged = ss.mChanged; |
Chiao Cheng | 47b6f70 | 2012-09-07 17:28:17 -0700 | [diff] [blame] | 114 | mSnapshot = (StructuredNameDataItem) DataItem.createFrom(ss.mSnapshot); |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Dmitri Plotnikov | 83cc117 | 2011-03-01 10:59:23 -0800 | [diff] [blame] | 117 | private static class SavedState implements Parcelable { |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 118 | public boolean mChanged; |
| 119 | public ContentValues mSnapshot; |
Dmitri Plotnikov | 83cc117 | 2011-03-01 10:59:23 -0800 | [diff] [blame] | 120 | public Parcelable mSuperState; |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 121 | |
| 122 | SavedState(Parcelable superState) { |
Dmitri Plotnikov | 83cc117 | 2011-03-01 10:59:23 -0800 | [diff] [blame] | 123 | mSuperState = superState; |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | private SavedState(Parcel in) { |
Dmitri Plotnikov | 83cc117 | 2011-03-01 10:59:23 -0800 | [diff] [blame] | 127 | ClassLoader loader = getClass().getClassLoader(); |
| 128 | mSuperState = in.readParcelable(loader); |
| 129 | |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 130 | mChanged = in.readInt() != 0; |
Dmitri Plotnikov | 83cc117 | 2011-03-01 10:59:23 -0800 | [diff] [blame] | 131 | mSnapshot = in.readParcelable(loader); |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void writeToParcel(Parcel out, int flags) { |
Dmitri Plotnikov | 83cc117 | 2011-03-01 10:59:23 -0800 | [diff] [blame] | 136 | out.writeParcelable(mSuperState, 0); |
| 137 | |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 138 | out.writeInt(mChanged ? 1 : 0); |
| 139 | out.writeParcelable(mSnapshot, 0); |
| 140 | } |
| 141 | |
Dmitri Plotnikov | 83cc117 | 2011-03-01 10:59:23 -0800 | [diff] [blame] | 142 | @SuppressWarnings({"unused"}) |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 143 | 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 Plotnikov | 83cc117 | 2011-03-01 10:59:23 -0800 | [diff] [blame] | 155 | |
| 156 | @Override |
| 157 | public int describeContents() { |
| 158 | return 0; |
| 159 | } |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 160 | } |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 161 | } |