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 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 19 | import android.app.Dialog; |
| 20 | import android.content.Context; |
| 21 | import android.content.res.Resources; |
| 22 | import android.os.Bundle; |
Wenyi Wang | 2da5d2f | 2015-09-29 22:15:38 -0700 | [diff] [blame] | 23 | import android.provider.ContactsContract; |
| 24 | import android.provider.ContactsContract.CommonDataKinds.Event; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 25 | import android.text.TextUtils; |
| 26 | import android.util.AttributeSet; |
| 27 | import android.view.View; |
| 28 | import android.widget.Button; |
| 29 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 30 | import com.android.contacts.R; |
| 31 | import com.android.contacts.datepicker.DatePicker; |
| 32 | import com.android.contacts.datepicker.DatePickerDialog; |
| 33 | import com.android.contacts.datepicker.DatePickerDialog.OnDateSetListener; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 34 | import com.android.contacts.common.model.RawContactDelta; |
Chiao Cheng | 738ff86 | 2012-11-30 12:06:03 -0800 | [diff] [blame] | 35 | import com.android.contacts.common.model.ValuesDelta; |
Chiao Cheng | 428f008 | 2012-11-13 18:38:56 -0800 | [diff] [blame] | 36 | import com.android.contacts.common.model.account.AccountType.EditField; |
| 37 | import com.android.contacts.common.model.account.AccountType.EventEditType; |
| 38 | import com.android.contacts.common.model.dataitem.DataKind; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 39 | import com.android.contacts.common.util.CommonDateUtils; |
| 40 | import com.android.contacts.common.util.DateUtils; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 41 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 42 | import java.text.ParsePosition; |
| 43 | import java.util.Calendar; |
| 44 | import java.util.Date; |
Daniel Lehmann | 4b64848 | 2011-03-04 14:59:04 -0800 | [diff] [blame] | 45 | import java.util.Locale; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Editor that allows editing Events using a {@link DatePickerDialog} |
| 49 | */ |
| 50 | public class EventFieldEditorView extends LabeledEditorView { |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 51 | |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Default string to show when there is no date selected yet. |
| 54 | */ |
| 55 | private String mNoDateString; |
| 56 | private int mPrimaryTextColor; |
Brian Attwell | d690dff | 2014-12-02 15:04:56 -0800 | [diff] [blame] | 57 | private int mHintTextColor; |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 58 | |
Dmitri Plotnikov | 135b44c | 2011-01-19 11:05:04 -0800 | [diff] [blame] | 59 | private Button mDateView; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 60 | |
| 61 | public EventFieldEditorView(Context context) { |
| 62 | super(context); |
| 63 | } |
| 64 | |
| 65 | public EventFieldEditorView(Context context, AttributeSet attrs) { |
| 66 | super(context, attrs); |
| 67 | } |
| 68 | |
| 69 | public EventFieldEditorView(Context context, AttributeSet attrs, int defStyle) { |
| 70 | super(context, attrs, defStyle); |
| 71 | } |
| 72 | |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 73 | /** {@inheritDoc} */ |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 74 | @Override |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 75 | protected void onFinishInflate() { |
| 76 | super.onFinishInflate(); |
Dmitri Plotnikov | 91d8e89 | 2010-12-07 20:36:51 -0800 | [diff] [blame] | 77 | |
Brian Attwell | f140227 | 2014-12-16 16:00:08 -0800 | [diff] [blame] | 78 | Resources resources = getContext().getResources(); |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 79 | mPrimaryTextColor = resources.getColor(R.color.primary_text_color); |
Brian Attwell | d690dff | 2014-12-02 15:04:56 -0800 | [diff] [blame] | 80 | mHintTextColor = resources.getColor(R.color.editor_disabled_text_color); |
Brian Attwell | f140227 | 2014-12-16 16:00:08 -0800 | [diff] [blame] | 81 | mNoDateString = getContext().getString(R.string.event_edit_field_hint_text); |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 82 | |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 83 | mDateView = (Button) findViewById(R.id.date_view); |
| 84 | mDateView.setOnClickListener(new OnClickListener() { |
| 85 | @Override |
| 86 | public void onClick(View v) { |
| 87 | showDialog(R.id.dialog_event_date_picker); |
| 88 | } |
| 89 | }); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
Josh Gargus | 26918da | 2012-02-02 16:58:04 -0800 | [diff] [blame] | 93 | public void editNewlyAddedField() { |
| 94 | showDialog(R.id.dialog_event_date_picker); |
| 95 | } |
| 96 | |
| 97 | @Override |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 98 | protected void requestFocusForFirstEditField() { |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 99 | mDateView.requestFocus(); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void setEnabled(boolean enabled) { |
| 104 | super.setEnabled(enabled); |
| 105 | |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 106 | mDateView.setEnabled(!isReadOnly() && enabled); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 110 | public void setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly, |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 111 | ViewIdGenerator vig) { |
| 112 | if (kind.fieldList.size() != 1) throw new IllegalStateException("kind must have 1 field"); |
| 113 | super.setValues(kind, entry, state, readOnly, vig); |
| 114 | |
Katherine Kuan | 63ffb90 | 2011-04-26 19:53:40 -0700 | [diff] [blame] | 115 | mDateView.setEnabled(isEnabled() && !readOnly); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 116 | |
| 117 | rebuildDateView(); |
Brian Attwell | 8f9d84f | 2014-11-03 23:17:04 -0800 | [diff] [blame] | 118 | updateEmptiness(); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | private void rebuildDateView() { |
| 122 | final EditField editField = getKind().fieldList.get(0); |
| 123 | final String column = editField.column; |
Yorke Lee | 4b294d6 | 2012-12-19 12:04:38 -0800 | [diff] [blame] | 124 | String data = DateUtils.formatDate(getContext(), getEntry().getAsString(column), |
| 125 | false /*Use the short DateFormat to ensure that it fits inside the EditText*/); |
Dmitri Plotnikov | 135b44c | 2011-01-19 11:05:04 -0800 | [diff] [blame] | 126 | if (TextUtils.isEmpty(data)) { |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 127 | mDateView.setText(mNoDateString); |
Brian Attwell | d690dff | 2014-12-02 15:04:56 -0800 | [diff] [blame] | 128 | mDateView.setTextColor(mHintTextColor); |
Katherine Kuan | 5e330ed | 2011-07-15 15:06:06 -0700 | [diff] [blame] | 129 | setDeleteButtonVisible(false); |
| 130 | } else { |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 131 | mDateView.setText(data); |
| 132 | mDateView.setTextColor(mPrimaryTextColor); |
Katherine Kuan | 5e330ed | 2011-07-15 15:06:06 -0700 | [diff] [blame] | 133 | setDeleteButtonVisible(true); |
Dmitri Plotnikov | 135b44c | 2011-01-19 11:05:04 -0800 | [diff] [blame] | 134 | } |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Katherine Kuan | 2591436 | 2011-04-29 19:42:01 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public boolean isEmpty() { |
Josh Gargus | 296cca5 | 2011-12-01 17:57:48 -0800 | [diff] [blame] | 139 | final EditField editField = getKind().fieldList.get(0); |
| 140 | final String column = editField.column; |
| 141 | return TextUtils.isEmpty(getEntry().getAsString(column)); |
Katherine Kuan | 1b22073 | 2011-04-28 18:45:39 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 144 | @Override |
| 145 | public Dialog createDialog(Bundle bundle) { |
| 146 | if (bundle == null) throw new IllegalArgumentException("bundle must not be null"); |
| 147 | int dialogId = bundle.getInt(DIALOG_ID_KEY); |
| 148 | switch (dialogId) { |
| 149 | case R.id.dialog_event_date_picker: |
| 150 | return createDatePickerDialog(); |
| 151 | default: |
| 152 | return super.createDialog(bundle); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | protected EventEditType getType() { |
| 158 | return (EventEditType) super.getType(); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | protected void onLabelRebuilt() { |
| 163 | // if we changed to a type that requires a year, ensure that it is actually set |
| 164 | final String column = getKind().fieldList.get(0).column; |
| 165 | final String oldValue = getEntry().getAsString(column); |
| 166 | final DataKind kind = getKind(); |
| 167 | |
Daniel Lehmann | 4b64848 | 2011-03-04 14:59:04 -0800 | [diff] [blame] | 168 | final Calendar calendar = Calendar.getInstance(DateUtils.UTC_TIMEZONE, Locale.US); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 169 | final int defaultYear = calendar.get(Calendar.YEAR); |
| 170 | |
| 171 | // Check whether the year is optional |
Jay Shrauner | 7bcdf51 | 2015-05-26 15:45:36 -0700 | [diff] [blame] | 172 | final boolean isYearOptional = getType() != null && getType().isYearOptional(); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 173 | |
Dmitri Plotnikov | 17e6f9d | 2011-01-03 11:27:49 -0800 | [diff] [blame] | 174 | if (!isYearOptional && !TextUtils.isEmpty(oldValue)) { |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 175 | final ParsePosition position = new ParsePosition(0); |
Walter Jang | 5f8932d | 2016-10-12 17:45:44 -0700 | [diff] [blame] | 176 | final Date date2 = kind.dateFormatWithoutYear == null |
| 177 | ? null : kind.dateFormatWithoutYear.parse(oldValue, position); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 178 | |
| 179 | // Don't understand the date, lets not change it |
| 180 | if (date2 == null) return; |
| 181 | |
| 182 | // This value is missing the year. Add it now |
| 183 | calendar.setTime(date2); |
| 184 | calendar.set(defaultYear, calendar.get(Calendar.MONTH), |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 185 | calendar.get(Calendar.DAY_OF_MONTH), CommonDateUtils.DEFAULT_HOUR, 0, 0); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 186 | |
Walter Jang | 5f8932d | 2016-10-12 17:45:44 -0700 | [diff] [blame] | 187 | final String formattedDate = kind.dateFormatWithYear == null |
| 188 | ? null : kind.dateFormatWithYear.format(calendar.getTime()); |
| 189 | if (formattedDate == null) return; |
| 190 | |
| 191 | onFieldChanged(column, formattedDate); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 192 | rebuildDateView(); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Prepare dialog for entering a date |
| 198 | */ |
| 199 | private Dialog createDatePickerDialog() { |
| 200 | final String column = getKind().fieldList.get(0).column; |
| 201 | final String oldValue = getEntry().getAsString(column); |
| 202 | final DataKind kind = getKind(); |
| 203 | |
Daniel Lehmann | 4b64848 | 2011-03-04 14:59:04 -0800 | [diff] [blame] | 204 | final Calendar calendar = Calendar.getInstance(DateUtils.UTC_TIMEZONE, Locale.US); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 205 | final int defaultYear = calendar.get(Calendar.YEAR); |
| 206 | |
| 207 | // Check whether the year is optional |
| 208 | final boolean isYearOptional = getType().isYearOptional(); |
| 209 | |
| 210 | final int oldYear, oldMonth, oldDay; |
Yorke Lee | ec6bfa0 | 2012-12-19 15:45:37 -0800 | [diff] [blame] | 211 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 212 | if (TextUtils.isEmpty(oldValue)) { |
Yorke Lee | 78253a9 | 2012-12-04 12:22:12 -0800 | [diff] [blame] | 213 | // Default to the current date |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 214 | oldYear = defaultYear; |
Yorke Lee | 78253a9 | 2012-12-04 12:22:12 -0800 | [diff] [blame] | 215 | oldMonth = calendar.get(Calendar.MONTH); |
| 216 | oldDay = calendar.get(Calendar.DAY_OF_MONTH); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 217 | } else { |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 218 | // Try parsing with year |
Yorke Lee | ec6bfa0 | 2012-12-19 15:45:37 -0800 | [diff] [blame] | 219 | Calendar cal = DateUtils.parseDate(oldValue, false); |
| 220 | if (cal != null) { |
| 221 | if (DateUtils.isYearSet(cal)) { |
| 222 | oldYear = cal.get(Calendar.YEAR); |
Daniel Lehmann | 5a7a269 | 2012-08-03 21:39:01 -0700 | [diff] [blame] | 223 | } else { |
Yorke Lee | ec6bfa0 | 2012-12-19 15:45:37 -0800 | [diff] [blame] | 224 | //cal.set(Calendar.YEAR, 0); |
Daniel Lehmann | 5a7a269 | 2012-08-03 21:39:01 -0700 | [diff] [blame] | 225 | oldYear = isYearOptional ? DatePickerDialog.NO_YEAR : defaultYear; |
Daniel Lehmann | 5a7a269 | 2012-08-03 21:39:01 -0700 | [diff] [blame] | 226 | } |
Yorke Lee | ec6bfa0 | 2012-12-19 15:45:37 -0800 | [diff] [blame] | 227 | oldMonth = cal.get(Calendar.MONTH); |
| 228 | oldDay = cal.get(Calendar.DAY_OF_MONTH); |
| 229 | } else { |
| 230 | return null; |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | final OnDateSetListener callBack = new OnDateSetListener() { |
| 234 | @Override |
| 235 | public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { |
| 236 | if (year == 0 && !isYearOptional) throw new IllegalStateException(); |
Daniel Lehmann | 4b64848 | 2011-03-04 14:59:04 -0800 | [diff] [blame] | 237 | final Calendar outCalendar = |
| 238 | Calendar.getInstance(DateUtils.UTC_TIMEZONE, Locale.US); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 239 | |
Daniel Lehmann | 11812c5 | 2012-03-29 17:50:29 -0700 | [diff] [blame] | 240 | // If no year specified, set it to 2000 (we could pick any leap year here). |
| 241 | // The format string will ignore that year. |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 242 | // For formats other than Exchange, the time of the day is ignored |
| 243 | outCalendar.clear(); |
Daniel Lehmann | 5a7a269 | 2012-08-03 21:39:01 -0700 | [diff] [blame] | 244 | outCalendar.set(year == DatePickerDialog.NO_YEAR ? 2000 : year, monthOfYear, |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 245 | dayOfMonth, CommonDateUtils.DEFAULT_HOUR, 0, 0); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 246 | |
| 247 | final String resultString; |
| 248 | if (year == 0) { |
Walter Jang | 5f8932d | 2016-10-12 17:45:44 -0700 | [diff] [blame] | 249 | resultString = kind.dateFormatWithoutYear == null |
| 250 | ? null : kind.dateFormatWithoutYear.format(outCalendar.getTime()); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 251 | } else { |
Walter Jang | 5f8932d | 2016-10-12 17:45:44 -0700 | [diff] [blame] | 252 | resultString = kind.dateFormatWithYear == null |
| 253 | ? null : kind.dateFormatWithYear.format(outCalendar.getTime()); |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 254 | } |
Walter Jang | 5f8932d | 2016-10-12 17:45:44 -0700 | [diff] [blame] | 255 | if (resultString == null) return; |
| 256 | |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 257 | onFieldChanged(column, resultString); |
| 258 | rebuildDateView(); |
| 259 | } |
| 260 | }; |
| 261 | final DatePickerDialog resultDialog = new DatePickerDialog(getContext(), callBack, |
| 262 | oldYear, oldMonth, oldDay, isYearOptional); |
| 263 | return resultDialog; |
| 264 | } |
Daisuke Miyakawa | d37a891 | 2011-03-27 09:35:28 -0700 | [diff] [blame] | 265 | |
Katherine Kuan | 2293e55 | 2011-07-21 20:25:44 -0700 | [diff] [blame] | 266 | @Override |
| 267 | public void clearAllFields() { |
| 268 | // Update UI |
Katherine Kuan | fd70903 | 2011-08-02 14:18:16 -0700 | [diff] [blame] | 269 | mDateView.setText(mNoDateString); |
Brian Attwell | d690dff | 2014-12-02 15:04:56 -0800 | [diff] [blame] | 270 | mDateView.setTextColor(mHintTextColor); |
Katherine Kuan | 2293e55 | 2011-07-21 20:25:44 -0700 | [diff] [blame] | 271 | |
| 272 | // Update state |
| 273 | final String column = getKind().fieldList.get(0).column; |
| 274 | onFieldChanged(column, ""); |
| 275 | } |
Wenyi Wang | 2da5d2f | 2015-09-29 22:15:38 -0700 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * Sets the typeColumn of entry as TYPE_BIRTHDAY and calls rebuildValues() to refresh the view. |
| 279 | */ |
| 280 | public void restoreBirthday() { |
| 281 | saveValue(getKind().typeColumn, Integer.toString(Event.TYPE_BIRTHDAY)); |
| 282 | rebuildValues(); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * EventEditType Birthday: |
| 287 | * rawValue=3 labelRes=17039911 secondary=false specificMax=1 customColumn=null |
| 288 | * mYearOptional=true |
| 289 | */ |
| 290 | public boolean isBirthdayType(){ |
| 291 | final EventEditType eventType = getType(); |
| 292 | return eventType.rawValue == Event.TYPE_BIRTHDAY && !eventType.secondary |
| 293 | && eventType.specificMax == 1 && eventType.customColumn == null |
| 294 | && eventType.isYearOptional(); |
| 295 | } |
Daniel Lehmann | 392ccec | 2010-11-01 20:50:03 -0700 | [diff] [blame] | 296 | } |