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