Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [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 | |
Dmitri Plotnikov | 18ffaa2 | 2010-12-03 14:28:00 -0800 | [diff] [blame] | 17 | package com.android.contacts.editor; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 18 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 19 | import android.app.AlertDialog; |
| 20 | import android.app.Dialog; |
| 21 | import android.content.Context; |
| 22 | import android.content.DialogInterface; |
Daniel Lehmann | a04dc9c | 2012-04-14 16:09:48 -0700 | [diff] [blame] | 23 | import android.content.DialogInterface.OnShowListener; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 24 | import android.os.Bundle; |
Daniel Lehmann | 79c9c5a | 2010-11-11 16:05:27 -0800 | [diff] [blame] | 25 | import android.os.Handler; |
Daniel Lehmann | a04dc9c | 2012-04-14 16:09:48 -0700 | [diff] [blame] | 26 | import android.text.Editable; |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 27 | import android.text.TextUtils; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 28 | import android.text.TextWatcher; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Brian Attwell | 043fba6 | 2014-10-30 11:11:56 -0700 | [diff] [blame] | 30 | import android.util.TypedValue; |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 31 | import android.view.LayoutInflater; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 32 | import android.view.View; |
| 33 | import android.view.ViewGroup; |
Daniel Lehmann | a04dc9c | 2012-04-14 16:09:48 -0700 | [diff] [blame] | 34 | import android.view.WindowManager; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 35 | import android.view.inputmethod.EditorInfo; |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 36 | import android.widget.AdapterView; |
| 37 | import android.widget.AdapterView.OnItemSelectedListener; |
| 38 | import android.widget.ArrayAdapter; |
Daniel Lehmann | a04dc9c | 2012-04-14 16:09:48 -0700 | [diff] [blame] | 39 | import android.widget.Button; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 40 | import android.widget.EditText; |
Katherine Kuan | 12a9363 | 2011-05-05 13:38:01 -0700 | [diff] [blame] | 41 | import android.widget.ImageView; |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 42 | import android.widget.LinearLayout; |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 43 | import android.widget.Spinner; |
| 44 | import android.widget.TextView; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 45 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 46 | import com.android.contacts.R; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 47 | import com.android.contacts.common.model.RawContactDelta; |
| 48 | import com.android.contacts.common.ContactsUtils; |
Chiao Cheng | 738ff86 | 2012-11-30 12:06:03 -0800 | [diff] [blame] | 49 | import com.android.contacts.common.model.ValuesDelta; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 50 | import com.android.contacts.common.model.RawContactModifier; |
Chiao Cheng | 428f008 | 2012-11-13 18:38:56 -0800 | [diff] [blame] | 51 | import com.android.contacts.common.model.account.AccountType.EditType; |
| 52 | import com.android.contacts.common.model.dataitem.DataKind; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 53 | import com.android.contacts.util.DialogManager; |
| 54 | import com.android.contacts.util.DialogManager.DialogShowingView; |
| 55 | |
Dmitri Plotnikov | 14918c0 | 2010-12-07 17:20:27 -0800 | [diff] [blame] | 56 | import java.util.List; |
| 57 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 58 | /** |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 59 | * Base class for editors that handles labels and values. Uses |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 60 | * {@link ValuesDelta} to read any existing {@link RawContact} values, and to |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 61 | * correctly write any changes values. |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 62 | */ |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 63 | public abstract class LabeledEditorView extends LinearLayout implements Editor, DialogShowingView { |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 64 | protected static final String DIALOG_ID_KEY = "dialog_id"; |
| 65 | private static final int DIALOG_ID_CUSTOM = 1; |
| 66 | |
| 67 | private static final int INPUT_TYPE_CUSTOM = EditorInfo.TYPE_CLASS_TEXT |
| 68 | | EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS; |
| 69 | |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 70 | private Spinner mLabel; |
| 71 | private EditTypeAdapter mEditTypeAdapter; |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 72 | private View mDeleteContainer; |
Katherine Kuan | 12a9363 | 2011-05-05 13:38:01 -0700 | [diff] [blame] | 73 | private ImageView mDelete; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 74 | |
| 75 | private DataKind mKind; |
| 76 | private ValuesDelta mEntry; |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 77 | private RawContactDelta mState; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 78 | private boolean mReadOnly; |
Katherine Kuan | 2591436 | 2011-04-29 19:42:01 -0700 | [diff] [blame] | 79 | private boolean mWasEmpty = true; |
Katherine Kuan | 5e330ed | 2011-07-15 15:06:06 -0700 | [diff] [blame] | 80 | private boolean mIsDeletable = true; |
Katherine Kuan | 2293e55 | 2011-07-21 20:25:44 -0700 | [diff] [blame] | 81 | private boolean mIsAttachedToWindow; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 82 | |
| 83 | private EditType mType; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 84 | |
| 85 | private ViewIdGenerator mViewIdGenerator; |
| 86 | private DialogManager mDialogManager = null; |
| 87 | private EditorListener mListener; |
Dmitri Plotnikov | 91d8e89 | 2010-12-07 20:36:51 -0800 | [diff] [blame] | 88 | protected int mMinLineItemHeight; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 89 | |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 90 | /** |
| 91 | * A marker in the spinner adapter of the currently selected custom type. |
| 92 | */ |
| 93 | public static final EditType CUSTOM_SELECTION = new EditType(0, 0); |
| 94 | |
| 95 | private OnItemSelectedListener mSpinnerListener = new OnItemSelectedListener() { |
| 96 | |
| 97 | @Override |
| 98 | public void onItemSelected( |
| 99 | AdapterView<?> parent, View view, int position, long id) { |
| 100 | onTypeSelectionChange(position); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void onNothingSelected(AdapterView<?> parent) { |
| 105 | } |
| 106 | }; |
| 107 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 108 | public LabeledEditorView(Context context) { |
| 109 | super(context); |
Dmitri Plotnikov | 91d8e89 | 2010-12-07 20:36:51 -0800 | [diff] [blame] | 110 | init(context); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | public LabeledEditorView(Context context, AttributeSet attrs) { |
| 114 | super(context, attrs); |
Dmitri Plotnikov | 91d8e89 | 2010-12-07 20:36:51 -0800 | [diff] [blame] | 115 | init(context); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | public LabeledEditorView(Context context, AttributeSet attrs, int defStyle) { |
| 119 | super(context, attrs, defStyle); |
Dmitri Plotnikov | 91d8e89 | 2010-12-07 20:36:51 -0800 | [diff] [blame] | 120 | init(context); |
| 121 | } |
| 122 | |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame^] | 123 | public Long getRawContactId() { |
| 124 | return mState == null ? null : mState.getRawContactId(); |
| 125 | } |
| 126 | |
Dmitri Plotnikov | 91d8e89 | 2010-12-07 20:36:51 -0800 | [diff] [blame] | 127 | private void init(Context context) { |
| 128 | mMinLineItemHeight = context.getResources().getDimensionPixelSize( |
| 129 | R.dimen.editor_min_line_item_height); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 132 | /** {@inheritDoc} */ |
| 133 | @Override |
| 134 | protected void onFinishInflate() { |
| 135 | |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 136 | mLabel = (Spinner) findViewById(R.id.spinner); |
Daniel Lehmann | 67713b3 | 2012-02-21 21:57:09 -0800 | [diff] [blame] | 137 | // Turn off the Spinner's own state management. We do this ourselves on rotation |
| 138 | mLabel.setId(View.NO_ID); |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 139 | mLabel.setOnItemSelectedListener(mSpinnerListener); |
| 140 | |
Katherine Kuan | 12a9363 | 2011-05-05 13:38:01 -0700 | [diff] [blame] | 141 | mDelete = (ImageView) findViewById(R.id.delete_button); |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 142 | mDeleteContainer = findViewById(R.id.delete_button_container); |
Katherine Kuan | 12a9363 | 2011-05-05 13:38:01 -0700 | [diff] [blame] | 143 | mDeleteContainer.setOnClickListener(new OnClickListener() { |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 144 | @Override |
| 145 | public void onClick(View v) { |
| 146 | // defer removal of this button so that the pressed state is visible shortly |
| 147 | new Handler().post(new Runnable() { |
| 148 | @Override |
| 149 | public void run() { |
Katherine Kuan | 2293e55 | 2011-07-21 20:25:44 -0700 | [diff] [blame] | 150 | // Don't do anything if the view is no longer attached to the window |
| 151 | // (This check is needed because when this {@link Runnable} is executed, |
| 152 | // we can't guarantee the view is still valid. |
| 153 | if (!mIsAttachedToWindow) { |
| 154 | return; |
| 155 | } |
| 156 | // Send the delete request to the listener (which will in turn call |
| 157 | // deleteEditor() on this view if the deletion is valid - i.e. this is not |
| 158 | // the last {@link Editor} in the section). |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 159 | if (mListener != null) { |
Katherine Kuan | 2293e55 | 2011-07-21 20:25:44 -0700 | [diff] [blame] | 160 | mListener.onDeleteRequested(LabeledEditorView.this); |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | }); |
| 164 | } |
| 165 | }); |
Brian Attwell | 043fba6 | 2014-10-30 11:11:56 -0700 | [diff] [blame] | 166 | |
| 167 | setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), |
| 168 | (int) getResources().getDimension(R.dimen.editor_padding_between_editor_views)); |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Katherine Kuan | 2293e55 | 2011-07-21 20:25:44 -0700 | [diff] [blame] | 171 | @Override |
| 172 | protected void onAttachedToWindow() { |
| 173 | super.onAttachedToWindow(); |
| 174 | // Keep track of when the view is attached or detached from the window, so we know it's |
| 175 | // safe to remove views (in case the user requests to delete this editor). |
| 176 | mIsAttachedToWindow = true; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | protected void onDetachedFromWindow() { |
| 181 | super.onDetachedFromWindow(); |
| 182 | mIsAttachedToWindow = false; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void deleteEditor() { |
| 187 | // Keep around in model, but mark as deleted |
| 188 | mEntry.markDeleted(); |
| 189 | |
| 190 | // Remove the view |
Daniel Lehmann | ca87e9c | 2012-03-06 14:02:31 -0800 | [diff] [blame] | 191 | EditorAnimator.getInstance().removeEditorView(this); |
Katherine Kuan | 2293e55 | 2011-07-21 20:25:44 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Dmitri Plotnikov | 02bb125 | 2010-12-06 09:39:05 -0800 | [diff] [blame] | 194 | public boolean isReadOnly() { |
| 195 | return mReadOnly; |
| 196 | } |
| 197 | |
Dmitri Plotnikov | 91d8e89 | 2010-12-07 20:36:51 -0800 | [diff] [blame] | 198 | public int getBaseline(int row) { |
| 199 | if (row == 0 && mLabel != null) { |
| 200 | return mLabel.getBaseline(); |
| 201 | } |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | /** |
Katherine Kuan | a94859f | 2011-07-20 13:38:27 -0700 | [diff] [blame] | 206 | * Configures the visibility of the type label button and enables or disables it properly. |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 207 | */ |
| 208 | private void setupLabelButton(boolean shouldExist) { |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 209 | if (shouldExist) { |
Dmitri Plotnikov | 02bb125 | 2010-12-06 09:39:05 -0800 | [diff] [blame] | 210 | mLabel.setEnabled(!mReadOnly && isEnabled()); |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 211 | mLabel.setVisibility(View.VISIBLE); |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 212 | } else { |
| 213 | mLabel.setVisibility(View.GONE); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
Katherine Kuan | a94859f | 2011-07-20 13:38:27 -0700 | [diff] [blame] | 218 | * Configures the visibility of the "delete" button and enables or disables it properly. |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 219 | */ |
Katherine Kuan | 5e330ed | 2011-07-15 15:06:06 -0700 | [diff] [blame] | 220 | private void setupDeleteButton() { |
| 221 | if (mIsDeletable) { |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 222 | mDeleteContainer.setVisibility(View.VISIBLE); |
Dmitri Plotnikov | 02bb125 | 2010-12-06 09:39:05 -0800 | [diff] [blame] | 223 | mDelete.setEnabled(!mReadOnly && isEnabled()); |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 224 | } else { |
| 225 | mDeleteContainer.setVisibility(View.GONE); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Katherine Kuan | 5e330ed | 2011-07-15 15:06:06 -0700 | [diff] [blame] | 229 | public void setDeleteButtonVisible(boolean visible) { |
| 230 | if (mIsDeletable) { |
Katherine Kuan | a94859f | 2011-07-20 13:38:27 -0700 | [diff] [blame] | 231 | mDeleteContainer.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); |
Katherine Kuan | 5e330ed | 2011-07-15 15:06:06 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 235 | protected void onOptionalFieldVisibilityChange() { |
| 236 | if (mListener != null) { |
| 237 | mListener.onRequest(EditorListener.EDITOR_FORM_CHANGED); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void setEditorListener(EditorListener listener) { |
| 243 | mListener = listener; |
| 244 | } |
| 245 | |
Brian Attwell | d690dff | 2014-12-02 15:04:56 -0800 | [diff] [blame] | 246 | protected EditorListener getEditorListener(){ |
| 247 | return mListener; |
| 248 | } |
| 249 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 250 | @Override |
| 251 | public void setDeletable(boolean deletable) { |
Katherine Kuan | 5e330ed | 2011-07-15 15:06:06 -0700 | [diff] [blame] | 252 | mIsDeletable = deletable; |
| 253 | setupDeleteButton(); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public void setEnabled(boolean enabled) { |
Dmitri Plotnikov | 02bb125 | 2010-12-06 09:39:05 -0800 | [diff] [blame] | 258 | super.setEnabled(enabled); |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 259 | mLabel.setEnabled(!mReadOnly && enabled); |
| 260 | mDelete.setEnabled(!mReadOnly && enabled); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 263 | public Spinner getLabel() { |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 264 | return mLabel; |
| 265 | } |
| 266 | |
Katherine Kuan | 12a9363 | 2011-05-05 13:38:01 -0700 | [diff] [blame] | 267 | public ImageView getDelete() { |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 268 | return mDelete; |
| 269 | } |
| 270 | |
| 271 | protected DataKind getKind() { |
| 272 | return mKind; |
| 273 | } |
| 274 | |
| 275 | protected ValuesDelta getEntry() { |
| 276 | return mEntry; |
| 277 | } |
| 278 | |
| 279 | protected EditType getType() { |
| 280 | return mType; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Build the current label state based on selected {@link EditType} and |
| 285 | * possible custom label string. |
| 286 | */ |
Brian Attwell | d690dff | 2014-12-02 15:04:56 -0800 | [diff] [blame] | 287 | public void rebuildLabel() { |
Brian Attwell | f140227 | 2014-12-16 16:00:08 -0800 | [diff] [blame] | 288 | mEditTypeAdapter = new EditTypeAdapter(getContext()); |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 289 | mLabel.setAdapter(mEditTypeAdapter); |
| 290 | if (mEditTypeAdapter.hasCustomSelection()) { |
| 291 | mLabel.setSelection(mEditTypeAdapter.getPosition(CUSTOM_SELECTION)); |
| 292 | } else { |
| 293 | mLabel.setSelection(mEditTypeAdapter.getPosition(mType)); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 294 | } |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 297 | @Override |
| 298 | public void onFieldChanged(String column, String value) { |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 299 | if (!isFieldChanged(column, value)) { |
Dmitri Plotnikov | 7d4a8dd | 2010-12-07 20:49:25 -0800 | [diff] [blame] | 300 | return; |
| 301 | } |
| 302 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 303 | // Field changes are saved directly |
Katherine Kuan | e55faef | 2011-12-06 17:58:43 -0800 | [diff] [blame] | 304 | saveValue(column, value); |
| 305 | |
| 306 | // Notify listener if applicable |
| 307 | notifyEditorListener(); |
Brian Attwell | d690dff | 2014-12-02 15:04:56 -0800 | [diff] [blame] | 308 | |
| 309 | rebuildLabel(); |
Katherine Kuan | e55faef | 2011-12-06 17:58:43 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | protected void saveValue(String column, String value) { |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 313 | mEntry.put(column, value); |
Katherine Kuan | e55faef | 2011-12-06 17:58:43 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Brian Attwell | 8f9d84f | 2014-11-03 23:17:04 -0800 | [diff] [blame] | 316 | /** |
| 317 | * Sub classes should call this at the end of {@link #setValues} once they finish changing |
| 318 | * isEmpty(). This is needed to fix b/18194655. |
| 319 | */ |
| 320 | protected final void updateEmptiness() { |
| 321 | mWasEmpty = isEmpty(); |
| 322 | } |
| 323 | |
Katherine Kuan | e55faef | 2011-12-06 17:58:43 -0800 | [diff] [blame] | 324 | protected void notifyEditorListener() { |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 325 | if (mListener != null) { |
| 326 | mListener.onRequest(EditorListener.FIELD_CHANGED); |
| 327 | } |
Katherine Kuan | 2591436 | 2011-04-29 19:42:01 -0700 | [diff] [blame] | 328 | |
| 329 | boolean isEmpty = isEmpty(); |
| 330 | if (mWasEmpty != isEmpty) { |
| 331 | if (isEmpty) { |
Katherine Kuan | 37bddc2 | 2011-07-09 17:36:26 -0700 | [diff] [blame] | 332 | if (mListener != null) { |
| 333 | mListener.onRequest(EditorListener.FIELD_TURNED_EMPTY); |
| 334 | } |
Katherine Kuan | a94859f | 2011-07-20 13:38:27 -0700 | [diff] [blame] | 335 | if (mIsDeletable) mDeleteContainer.setVisibility(View.INVISIBLE); |
Katherine Kuan | 2591436 | 2011-04-29 19:42:01 -0700 | [diff] [blame] | 336 | } else { |
Katherine Kuan | 37bddc2 | 2011-07-09 17:36:26 -0700 | [diff] [blame] | 337 | if (mListener != null) { |
| 338 | mListener.onRequest(EditorListener.FIELD_TURNED_NON_EMPTY); |
| 339 | } |
Katherine Kuan | 5e330ed | 2011-07-15 15:06:06 -0700 | [diff] [blame] | 340 | if (mIsDeletable) mDeleteContainer.setVisibility(View.VISIBLE); |
Katherine Kuan | 2591436 | 2011-04-29 19:42:01 -0700 | [diff] [blame] | 341 | } |
| 342 | mWasEmpty = isEmpty; |
| 343 | } |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 346 | protected boolean isFieldChanged(String column, String value) { |
Daniel Lehmann | 60b239c | 2011-03-07 20:01:45 -0800 | [diff] [blame] | 347 | final String dbValue = mEntry.getAsString(column); |
| 348 | // nullable fields (e.g. Middle Name) are usually represented as empty columns, |
| 349 | // so lets treat null and empty space equivalently here |
| 350 | final String dbValueNoNull = dbValue == null ? "" : dbValue; |
| 351 | final String valueNoNull = value == null ? "" : value; |
| 352 | return !TextUtils.equals(dbValueNoNull, valueNoNull); |
Dmitri Plotnikov | 11bb73b | 2011-02-25 20:39:58 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 355 | protected void rebuildValues() { |
| 356 | setValues(mKind, mEntry, mState, mReadOnly, mViewIdGenerator); |
| 357 | } |
| 358 | |
| 359 | /** |
Brian Attwell | 8f9d84f | 2014-11-03 23:17:04 -0800 | [diff] [blame] | 360 | * Prepare this editor using the given {@link DataKind} for defining structure and |
| 361 | * {@link ValuesDelta} describing the content to edit. When overriding this, be careful |
| 362 | * to call {@link #updateEmptiness} at the end. |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 363 | */ |
| 364 | @Override |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 365 | public void setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly, |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 366 | ViewIdGenerator vig) { |
| 367 | mKind = kind; |
| 368 | mEntry = entry; |
| 369 | mState = state; |
| 370 | mReadOnly = readOnly; |
| 371 | mViewIdGenerator = vig; |
| 372 | setId(vig.getId(state, kind, entry, ViewIdGenerator.NO_VIEW_INDEX)); |
| 373 | |
| 374 | if (!entry.isVisible()) { |
| 375 | // Hide ourselves entirely if deleted |
| 376 | setVisibility(View.GONE); |
| 377 | return; |
| 378 | } |
| 379 | setVisibility(View.VISIBLE); |
| 380 | |
| 381 | // Display label selector if multiple types available |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 382 | final boolean hasTypes = RawContactModifier.hasEditTypes(kind); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 383 | setupLabelButton(hasTypes); |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 384 | mLabel.setEnabled(!readOnly && isEnabled()); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 385 | if (hasTypes) { |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 386 | mType = RawContactModifier.getCurrentType(entry, kind); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 387 | rebuildLabel(); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | public ValuesDelta getValues() { |
| 392 | return mEntry; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Prepare dialog for entering a custom label. The input value is trimmed: white spaces before |
| 397 | * and after the input text is removed. |
| 398 | * <p> |
| 399 | * If the final value is empty, this change request is ignored; |
| 400 | * no empty text is allowed in any custom label. |
| 401 | */ |
| 402 | private Dialog createCustomDialog() { |
Brian Attwell | f140227 | 2014-12-16 16:00:08 -0800 | [diff] [blame] | 403 | final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); |
Daniel Lehmann | a04dc9c | 2012-04-14 16:09:48 -0700 | [diff] [blame] | 404 | final LayoutInflater layoutInflater = LayoutInflater.from(builder.getContext()); |
Adam Powell | eb765ac | 2011-11-22 12:28:17 -0800 | [diff] [blame] | 405 | builder.setTitle(R.string.customLabelPickerTitle); |
| 406 | |
Daniel Lehmann | a04dc9c | 2012-04-14 16:09:48 -0700 | [diff] [blame] | 407 | final View view = layoutInflater.inflate(R.layout.contact_editor_label_name_dialog, null); |
| 408 | final EditText editText = (EditText) view.findViewById(R.id.custom_dialog_content); |
| 409 | editText.setInputType(INPUT_TYPE_CUSTOM); |
| 410 | editText.setSaveEnabled(true); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 411 | |
Daniel Lehmann | a04dc9c | 2012-04-14 16:09:48 -0700 | [diff] [blame] | 412 | builder.setView(view); |
| 413 | editText.requestFocus(); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 414 | |
| 415 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 416 | @Override |
| 417 | public void onClick(DialogInterface dialog, int which) { |
Daniel Lehmann | a04dc9c | 2012-04-14 16:09:48 -0700 | [diff] [blame] | 418 | final String customText = editText.getText().toString().trim(); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 419 | if (ContactsUtils.isGraphic(customText)) { |
Dmitri Plotnikov | 14918c0 | 2010-12-07 17:20:27 -0800 | [diff] [blame] | 420 | final List<EditType> allTypes = |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 421 | RawContactModifier.getValidTypes(mState, mKind, null); |
Dmitri Plotnikov | 14918c0 | 2010-12-07 17:20:27 -0800 | [diff] [blame] | 422 | mType = null; |
| 423 | for (EditType editType : allTypes) { |
| 424 | if (editType.customColumn != null) { |
| 425 | mType = editType; |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | if (mType == null) return; |
| 430 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 431 | mEntry.put(mKind.typeColumn, mType.rawValue); |
| 432 | mEntry.put(mType.customColumn, customText); |
| 433 | rebuildLabel(); |
| 434 | requestFocusForFirstEditField(); |
| 435 | onLabelRebuilt(); |
| 436 | } |
| 437 | } |
| 438 | }); |
| 439 | |
| 440 | builder.setNegativeButton(android.R.string.cancel, null); |
| 441 | |
Daniel Lehmann | a04dc9c | 2012-04-14 16:09:48 -0700 | [diff] [blame] | 442 | final AlertDialog dialog = builder.create(); |
| 443 | dialog.setOnShowListener(new OnShowListener() { |
| 444 | @Override |
| 445 | public void onShow(DialogInterface dialogInterface) { |
| 446 | updateCustomDialogOkButtonState(dialog, editText); |
| 447 | } |
| 448 | }); |
| 449 | editText.addTextChangedListener(new TextWatcher() { |
| 450 | @Override |
| 451 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 456 | } |
| 457 | |
| 458 | @Override |
| 459 | public void afterTextChanged(Editable s) { |
| 460 | updateCustomDialogOkButtonState(dialog, editText); |
| 461 | } |
| 462 | }); |
| 463 | dialog.getWindow().setSoftInputMode( |
| 464 | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); |
| 465 | |
| 466 | return dialog; |
| 467 | } |
| 468 | |
| 469 | /* package */ void updateCustomDialogOkButtonState(AlertDialog dialog, EditText editText) { |
| 470 | final Button okButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE); |
| 471 | okButton.setEnabled(!TextUtils.isEmpty(editText.getText().toString().trim())); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Called after the label has changed (either chosen from the list or entered in the Dialog) |
| 476 | */ |
| 477 | protected void onLabelRebuilt() { |
| 478 | } |
| 479 | |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 480 | protected void onTypeSelectionChange(int position) { |
| 481 | EditType selected = mEditTypeAdapter.getItem(position); |
| 482 | // See if the selection has in fact changed |
| 483 | if (mEditTypeAdapter.hasCustomSelection() && selected == CUSTOM_SELECTION) { |
| 484 | return; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 487 | if (mType == selected && mType.customColumn == null) { |
| 488 | return; |
| 489 | } |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 490 | |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 491 | if (selected.customColumn != null) { |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 492 | showDialog(DIALOG_ID_CUSTOM); |
| 493 | } else { |
| 494 | // User picked type, and we're sure it's ok to actually write the entry. |
| 495 | mType = selected; |
| 496 | mEntry.put(mKind.typeColumn, mType.rawValue); |
| 497 | rebuildLabel(); |
| 498 | requestFocusForFirstEditField(); |
| 499 | onLabelRebuilt(); |
| 500 | } |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* package */ |
| 504 | void showDialog(int bundleDialogId) { |
| 505 | Bundle bundle = new Bundle(); |
| 506 | bundle.putInt(DIALOG_ID_KEY, bundleDialogId); |
| 507 | getDialogManager().showDialogInView(this, bundle); |
| 508 | } |
| 509 | |
| 510 | private DialogManager getDialogManager() { |
| 511 | if (mDialogManager == null) { |
| 512 | Context context = getContext(); |
| 513 | if (!(context instanceof DialogManager.DialogShowingViewActivity)) { |
| 514 | throw new IllegalStateException( |
| 515 | "View must be hosted in an Activity that implements " + |
| 516 | "DialogManager.DialogShowingViewActivity"); |
| 517 | } |
| 518 | mDialogManager = ((DialogManager.DialogShowingViewActivity)context).getDialogManager(); |
| 519 | } |
| 520 | return mDialogManager; |
| 521 | } |
| 522 | |
| 523 | @Override |
| 524 | public Dialog createDialog(Bundle bundle) { |
| 525 | if (bundle == null) throw new IllegalArgumentException("bundle must not be null"); |
| 526 | int dialogId = bundle.getInt(DIALOG_ID_KEY); |
| 527 | switch (dialogId) { |
| 528 | case DIALOG_ID_CUSTOM: |
| 529 | return createCustomDialog(); |
| 530 | default: |
| 531 | throw new IllegalArgumentException("Invalid dialogId: " + dialogId); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | protected abstract void requestFocusForFirstEditField(); |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 536 | |
| 537 | private class EditTypeAdapter extends ArrayAdapter<EditType> { |
| 538 | private final LayoutInflater mInflater; |
| 539 | private boolean mHasCustomSelection; |
Brian Attwell | d690dff | 2014-12-02 15:04:56 -0800 | [diff] [blame] | 540 | private int mTextColorHintUnfocused; |
| 541 | private int mTextColorDark; |
| 542 | private int mTextColorSecondary; |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 543 | |
| 544 | public EditTypeAdapter(Context context) { |
| 545 | super(context, 0); |
| 546 | mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Brian Attwell | d690dff | 2014-12-02 15:04:56 -0800 | [diff] [blame] | 547 | mTextColorHintUnfocused = context.getResources().getColor( |
| 548 | R.color.editor_disabled_text_color); |
| 549 | mTextColorSecondary = context.getResources().getColor(R.color.secondary_text_color); |
| 550 | mTextColorDark = context.getResources().getColor(R.color.primary_text_color); |
| 551 | |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 552 | |
Dmitri Plotnikov | 4fa9cd0 | 2010-12-10 17:40:38 -0800 | [diff] [blame] | 553 | if (mType != null && mType.customColumn != null) { |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 554 | |
| 555 | // Use custom label string when present |
| 556 | final String customText = mEntry.getAsString(mType.customColumn); |
| 557 | if (customText != null) { |
| 558 | add(CUSTOM_SELECTION); |
| 559 | mHasCustomSelection = true; |
| 560 | } |
| 561 | } |
| 562 | |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 563 | addAll(RawContactModifier.getValidTypes(mState, mKind, mType)); |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | public boolean hasCustomSelection() { |
| 567 | return mHasCustomSelection; |
| 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public View getView(int position, View convertView, ViewGroup parent) { |
Brian Attwell | d690dff | 2014-12-02 15:04:56 -0800 | [diff] [blame] | 572 | final TextView view = createViewFromResource( |
Brian Attwell | 043fba6 | 2014-10-30 11:11:56 -0700 | [diff] [blame] | 573 | position, convertView, parent, R.layout.edit_simple_spinner_item); |
| 574 | // We don't want any background on this view. The background would obscure |
| 575 | // the spinner's background. |
| 576 | view.setBackground(null); |
Brian Attwell | d690dff | 2014-12-02 15:04:56 -0800 | [diff] [blame] | 577 | // The text color should be a very light hint color when unfocused and empty. When |
| 578 | // focused and empty, use a less light hint color. When non-empty, use a dark non-hint |
| 579 | // color. |
| 580 | if (!LabeledEditorView.this.isEmpty()) { |
| 581 | view.setTextColor(mTextColorDark); |
| 582 | } else if (LabeledEditorView.this.hasFocus()) { |
| 583 | view.setTextColor(mTextColorSecondary); |
| 584 | } else { |
| 585 | view.setTextColor(mTextColorHintUnfocused); |
| 586 | } |
Brian Attwell | 043fba6 | 2014-10-30 11:11:56 -0700 | [diff] [blame] | 587 | return view; |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | @Override |
| 591 | public View getDropDownView(int position, View convertView, ViewGroup parent) { |
| 592 | return createViewFromResource( |
| 593 | position, convertView, parent, android.R.layout.simple_spinner_dropdown_item); |
| 594 | } |
| 595 | |
Brian Attwell | d690dff | 2014-12-02 15:04:56 -0800 | [diff] [blame] | 596 | private TextView createViewFromResource(int position, View convertView, ViewGroup parent, |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 597 | int resource) { |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 598 | TextView textView; |
| 599 | |
| 600 | if (convertView == null) { |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 601 | textView = (TextView) mInflater.inflate(resource, parent, false); |
Brian Attwell | 043fba6 | 2014-10-30 11:11:56 -0700 | [diff] [blame] | 602 | textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension( |
| 603 | R.dimen.editor_form_text_size)); |
Brian Attwell | d690dff | 2014-12-02 15:04:56 -0800 | [diff] [blame] | 604 | textView.setTextColor(mTextColorDark); |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 605 | } else { |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 606 | textView = (TextView) convertView; |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 609 | EditType type = getItem(position); |
| 610 | String text; |
| 611 | if (type == CUSTOM_SELECTION) { |
| 612 | text = mEntry.getAsString(mType.customColumn); |
| 613 | } else { |
| 614 | text = getContext().getString(type.labelRes); |
| 615 | } |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 616 | textView.setText(text); |
| 617 | return textView; |
Dmitri Plotnikov | 2fcfa49 | 2010-12-07 16:10:33 -0800 | [diff] [blame] | 618 | } |
| 619 | } |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 620 | } |