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.app.Dialog; 20 import android.content.Context; 21 import android.content.res.Resources; 22 import android.os.Bundle; 23 import android.text.TextUtils; 24 import android.util.AttributeSet; 25 import android.view.View; 26 import android.widget.Button; 27 28 import com.android.contacts.R; 29 import com.android.contacts.datepicker.DatePicker; 30 import com.android.contacts.datepicker.DatePickerDialog; 31 import com.android.contacts.datepicker.DatePickerDialog.OnDateSetListener; 32 import com.android.contacts.common.model.RawContactDelta; 33 import com.android.contacts.common.model.ValuesDelta; 34 import com.android.contacts.common.model.account.AccountType.EditField; 35 import com.android.contacts.common.model.account.AccountType.EventEditType; 36 import com.android.contacts.common.model.dataitem.DataKind; 37 import com.android.contacts.common.util.CommonDateUtils; 38 import com.android.contacts.common.util.DateUtils; 39 40 import java.text.ParsePosition; 41 import java.util.Calendar; 42 import java.util.Date; 43 import java.util.Locale; 44 45 /** 46 * Editor that allows editing Events using a {@link DatePickerDialog} 47 */ 48 public class EventFieldEditorView extends LabeledEditorView { 49 50 /** 51 * Default string to show when there is no date selected yet. 52 */ 53 private String mNoDateString; 54 private int mPrimaryTextColor; 55 private int mHintTextColor; 56 57 private Button mDateView; 58 59 public EventFieldEditorView(Context context) { 60 super(context); 61 } 62 63 public EventFieldEditorView(Context context, AttributeSet attrs) { 64 super(context, attrs); 65 } 66 67 public EventFieldEditorView(Context context, AttributeSet attrs, int defStyle) { 68 super(context, attrs, defStyle); 69 } 70 71 /** {@inheritDoc} */ 72 @Override 73 protected void onFinishInflate() { 74 super.onFinishInflate(); 75 76 Resources resources = getContext().getResources(); 77 mPrimaryTextColor = resources.getColor(R.color.primary_text_color); 78 mHintTextColor = resources.getColor(R.color.editor_disabled_text_color); 79 mNoDateString = getContext().getString(R.string.event_edit_field_hint_text); 80 81 mDateView = (Button) findViewById(R.id.date_view); 82 mDateView.setOnClickListener(new OnClickListener() { 83 @Override 84 public void onClick(View v) { 85 if (!isTypeVisible()) { 86 showType(); 87 } 88 showDialog(R.id.dialog_event_date_picker); 89 } 90 }); 91 } 92 93 @Override 94 public void editNewlyAddedField() { 95 showDialog(R.id.dialog_event_date_picker); 96 } 97 98 @Override 99 protected void requestFocusForFirstEditField() { 100 mDateView.requestFocus(); 101 } 102 103 @Override 104 public void setEnabled(boolean enabled) { 105 super.setEnabled(enabled); 106 107 mDateView.setEnabled(!isReadOnly() && enabled); 108 } 109 110 @Override 111 public void setValues(DataKind kind, ValuesDelta entry, RawContactDelta state, boolean readOnly, 112 ViewIdGenerator vig) { 113 if (kind.fieldList.size() != 1) throw new IllegalStateException("kind must have 1 field"); 114 super.setValues(kind, entry, state, readOnly, vig); 115 116 mDateView.setEnabled(isEnabled() && !readOnly); 117 118 rebuildDateView(); 119 updateEmptiness(); 120 } 121 122 private void rebuildDateView() { 123 final EditField editField = getKind().fieldList.get(0); 124 final String column = editField.column; 125 String data = DateUtils.formatDate(getContext(), getEntry().getAsString(column), 126 false /*Use the short DateFormat to ensure that it fits inside the EditText*/); 127 if (TextUtils.isEmpty(data)) { 128 mDateView.setText(mNoDateString); 129 mDateView.setTextColor(mHintTextColor); 130 setDeleteButtonVisible(false); 131 } else { 132 mDateView.setText(data); 133 mDateView.setTextColor(mPrimaryTextColor); 134 setDeleteButtonVisible(true); 135 if (!isTypeVisible()) { 136 showType(); 137 } 138 } 139 } 140 141 @Override 142 public boolean isEmpty() { 143 final EditField editField = getKind().fieldList.get(0); 144 final String column = editField.column; 145 return TextUtils.isEmpty(getEntry().getAsString(column)); 146 } 147 148 @Override 149 public Dialog createDialog(Bundle bundle) { 150 if (bundle == null) throw new IllegalArgumentException("bundle must not be null"); 151 int dialogId = bundle.getInt(DIALOG_ID_KEY); 152 switch (dialogId) { 153 case R.id.dialog_event_date_picker: 154 return createDatePickerDialog(); 155 default: 156 return super.createDialog(bundle); 157 } 158 } 159 160 @Override 161 protected EventEditType getType() { 162 return (EventEditType) super.getType(); 163 } 164 165 @Override 166 protected void onLabelRebuilt() { 167 // if we changed to a type that requires a year, ensure that it is actually set 168 final String column = getKind().fieldList.get(0).column; 169 final String oldValue = getEntry().getAsString(column); 170 final DataKind kind = getKind(); 171 172 final Calendar calendar = Calendar.getInstance(DateUtils.UTC_TIMEZONE, Locale.US); 173 final int defaultYear = calendar.get(Calendar.YEAR); 174 175 // Check whether the year is optional 176 final boolean isYearOptional = getType() != null && getType().isYearOptional(); 177 178 if (!isYearOptional && !TextUtils.isEmpty(oldValue)) { 179 final ParsePosition position = new ParsePosition(0); 180 final Date date2 = kind.dateFormatWithoutYear.parse(oldValue, position); 181 182 // Don't understand the date, lets not change it 183 if (date2 == null) return; 184 185 // This value is missing the year. Add it now 186 calendar.setTime(date2); 187 calendar.set(defaultYear, calendar.get(Calendar.MONTH), 188 calendar.get(Calendar.DAY_OF_MONTH), CommonDateUtils.DEFAULT_HOUR, 0, 0); 189 190 onFieldChanged(column, kind.dateFormatWithYear.format(calendar.getTime())); 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 203 final Calendar calendar = Calendar.getInstance(DateUtils.UTC_TIMEZONE, Locale.US); 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; 210 211 if (TextUtils.isEmpty(oldValue)) { 212 // Default to the current date 213 oldYear = defaultYear; 214 oldMonth = calendar.get(Calendar.MONTH); 215 oldDay = calendar.get(Calendar.DAY_OF_MONTH); 216 } else { 217 // Try parsing with year 218 Calendar cal = DateUtils.parseDate(oldValue, false); 219 if (cal != null) { 220 if (DateUtils.isYearSet(cal)) { 221 oldYear = cal.get(Calendar.YEAR); 222 } else { 223 //cal.set(Calendar.YEAR, 0); 224 oldYear = isYearOptional ? DatePickerDialog.NO_YEAR : defaultYear; 225 } 226 oldMonth = cal.get(Calendar.MONTH); 227 oldDay = cal.get(Calendar.DAY_OF_MONTH); 228 } else { 229 return null; 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(); 236 final Calendar outCalendar = 237 Calendar.getInstance(DateUtils.UTC_TIMEZONE, Locale.US); 238 239 // If no year specified, set it to 2000 (we could pick any leap year here). 240 // The format string will ignore that year. 241 // For formats other than Exchange, the time of the day is ignored 242 outCalendar.clear(); 243 outCalendar.set(year == DatePickerDialog.NO_YEAR ? 2000 : year, monthOfYear, 244 dayOfMonth, CommonDateUtils.DEFAULT_HOUR, 0, 0); 245 246 final String resultString; 247 if (year == 0) { 248 resultString = kind.dateFormatWithoutYear.format(outCalendar.getTime()); 249 } else { 250 resultString = kind.dateFormatWithYear.format(outCalendar.getTime()); 251 } 252 onFieldChanged(column, resultString); 253 rebuildDateView(); 254 } 255 }; 256 final DatePickerDialog resultDialog = new DatePickerDialog(getContext(), callBack, 257 oldYear, oldMonth, oldDay, isYearOptional); 258 return resultDialog; 259 } 260 261 @Override 262 public void clearAllFields() { 263 // Update UI 264 mDateView.setText(mNoDateString); 265 mDateView.setTextColor(mHintTextColor); 266 267 // Update state 268 final String column = getKind().fieldList.get(0).column; 269 onFieldChanged(column, ""); 270 } 271 } 272