TimePickerClockDelegate.java revision 7334441d69917473ab52f4666459bbcc5a9813e4
1/* 2 * Copyright (C) 2013 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 17package android.widget; 18 19import android.annotation.IntDef; 20import android.annotation.Nullable; 21import android.annotation.TestApi; 22import android.content.Context; 23import android.content.res.ColorStateList; 24import android.content.res.Resources; 25import android.content.res.TypedArray; 26import android.icu.text.DecimalFormatSymbols; 27import android.os.Parcelable; 28import android.text.SpannableStringBuilder; 29import android.text.format.DateFormat; 30import android.text.format.DateUtils; 31import android.text.style.TtsSpan; 32import android.util.AttributeSet; 33import android.util.StateSet; 34import android.view.HapticFeedbackConstants; 35import android.view.LayoutInflater; 36import android.view.MotionEvent; 37import android.view.View; 38import android.view.View.AccessibilityDelegate; 39import android.view.View.MeasureSpec; 40import android.view.ViewGroup; 41import android.view.accessibility.AccessibilityEvent; 42import android.view.accessibility.AccessibilityNodeInfo; 43import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; 44import android.view.inputmethod.InputMethodManager; 45import android.widget.RadialTimePickerView.OnValueSelectedListener; 46import android.widget.TextInputTimePickerView.OnValueTypedListener; 47 48import com.android.internal.R; 49import com.android.internal.widget.NumericTextView; 50import com.android.internal.widget.NumericTextView.OnValueChangedListener; 51 52import java.lang.annotation.Retention; 53import java.lang.annotation.RetentionPolicy; 54import java.util.Calendar; 55 56/** 57 * A delegate implementing the radial clock-based TimePicker. 58 */ 59class TimePickerClockDelegate extends TimePicker.AbstractTimePickerDelegate { 60 /** 61 * Delay in milliseconds before valid but potentially incomplete, for 62 * example "1" but not "12", keyboard edits are propagated from the 63 * hour / minute fields to the radial picker. 64 */ 65 private static final long DELAY_COMMIT_MILLIS = 2000; 66 67 @IntDef({FROM_EXTERNAL_API, FROM_RADIAL_PICKER, FROM_INPUT_PICKER}) 68 @Retention(RetentionPolicy.SOURCE) 69 private @interface ChangeSource {} 70 private static final int FROM_EXTERNAL_API = 0; 71 private static final int FROM_RADIAL_PICKER = 1; 72 private static final int FROM_INPUT_PICKER = 2; 73 74 // Index used by RadialPickerLayout 75 private static final int HOUR_INDEX = RadialTimePickerView.HOURS; 76 private static final int MINUTE_INDEX = RadialTimePickerView.MINUTES; 77 78 private static final int[] ATTRS_TEXT_COLOR = new int[] {R.attr.textColor}; 79 private static final int[] ATTRS_DISABLED_ALPHA = new int[] {R.attr.disabledAlpha}; 80 81 private static final int AM = 0; 82 private static final int PM = 1; 83 84 private static final int HOURS_IN_HALF_DAY = 12; 85 86 private final NumericTextView mHourView; 87 private final NumericTextView mMinuteView; 88 private final View mAmPmLayout; 89 private final RadioButton mAmLabel; 90 private final RadioButton mPmLabel; 91 private final RadialTimePickerView mRadialTimePickerView; 92 private final TextView mSeparatorView; 93 94 private boolean mRadialPickerModeEnabled = true; 95 private final ImageButton mRadialTimePickerModeButton; 96 private final String mRadialTimePickerModeEnabledDescription; 97 private final String mTextInputPickerModeEnabledDescription; 98 private final View mRadialTimePickerHeader; 99 private final View mTextInputPickerHeader; 100 101 private final TextInputTimePickerView mTextInputPickerView; 102 103 private final Calendar mTempCalendar; 104 105 // Accessibility strings. 106 private final String mSelectHours; 107 private final String mSelectMinutes; 108 109 private boolean mIsEnabled = true; 110 private boolean mAllowAutoAdvance; 111 private int mCurrentHour; 112 private int mCurrentMinute; 113 private boolean mIs24Hour; 114 private boolean mIsAmPmAtStart; 115 116 // Localization data. 117 private boolean mHourFormatShowLeadingZero; 118 private boolean mHourFormatStartsAtZero; 119 120 // Most recent time announcement values for accessibility. 121 private CharSequence mLastAnnouncedText; 122 private boolean mLastAnnouncedIsHour; 123 124 public TimePickerClockDelegate(TimePicker delegator, Context context, AttributeSet attrs, 125 int defStyleAttr, int defStyleRes) { 126 super(delegator, context); 127 128 // process style attributes 129 final TypedArray a = mContext.obtainStyledAttributes(attrs, 130 R.styleable.TimePicker, defStyleAttr, defStyleRes); 131 final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( 132 Context.LAYOUT_INFLATER_SERVICE); 133 final Resources res = mContext.getResources(); 134 135 mSelectHours = res.getString(R.string.select_hours); 136 mSelectMinutes = res.getString(R.string.select_minutes); 137 138 final int layoutResourceId = a.getResourceId(R.styleable.TimePicker_internalLayout, 139 R.layout.time_picker_material); 140 final View mainView = inflater.inflate(layoutResourceId, delegator); 141 mainView.setSaveFromParentEnabled(false); 142 mRadialTimePickerHeader = mainView.findViewById(R.id.time_header); 143 mRadialTimePickerHeader.setOnTouchListener(new NearestTouchDelegate()); 144 145 // Set up hour/minute labels. 146 mHourView = (NumericTextView) mainView.findViewById(R.id.hours); 147 mHourView.setOnClickListener(mClickListener); 148 mHourView.setOnFocusChangeListener(mFocusListener); 149 mHourView.setOnDigitEnteredListener(mDigitEnteredListener); 150 mHourView.setAccessibilityDelegate( 151 new ClickActionDelegate(context, R.string.select_hours)); 152 mSeparatorView = (TextView) mainView.findViewById(R.id.separator); 153 mMinuteView = (NumericTextView) mainView.findViewById(R.id.minutes); 154 mMinuteView.setOnClickListener(mClickListener); 155 mMinuteView.setOnFocusChangeListener(mFocusListener); 156 mMinuteView.setOnDigitEnteredListener(mDigitEnteredListener); 157 mMinuteView.setAccessibilityDelegate( 158 new ClickActionDelegate(context, R.string.select_minutes)); 159 mMinuteView.setRange(0, 59); 160 161 // Set up AM/PM labels. 162 mAmPmLayout = mainView.findViewById(R.id.ampm_layout); 163 mAmPmLayout.setOnTouchListener(new NearestTouchDelegate()); 164 165 final String[] amPmStrings = TimePicker.getAmPmStrings(context); 166 mAmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.am_label); 167 mAmLabel.setText(obtainVerbatim(amPmStrings[0])); 168 mAmLabel.setOnClickListener(mClickListener); 169 ensureMinimumTextWidth(mAmLabel); 170 171 mPmLabel = (RadioButton) mAmPmLayout.findViewById(R.id.pm_label); 172 mPmLabel.setText(obtainVerbatim(amPmStrings[1])); 173 mPmLabel.setOnClickListener(mClickListener); 174 ensureMinimumTextWidth(mPmLabel); 175 176 // For the sake of backwards compatibility, attempt to extract the text 177 // color from the header time text appearance. If it's set, we'll let 178 // that override the "real" header text color. 179 ColorStateList headerTextColor = null; 180 181 @SuppressWarnings("deprecation") 182 final int timeHeaderTextAppearance = a.getResourceId( 183 R.styleable.TimePicker_headerTimeTextAppearance, 0); 184 if (timeHeaderTextAppearance != 0) { 185 final TypedArray textAppearance = mContext.obtainStyledAttributes(null, 186 ATTRS_TEXT_COLOR, 0, timeHeaderTextAppearance); 187 final ColorStateList legacyHeaderTextColor = textAppearance.getColorStateList(0); 188 headerTextColor = applyLegacyColorFixes(legacyHeaderTextColor); 189 textAppearance.recycle(); 190 } 191 192 if (headerTextColor == null) { 193 headerTextColor = a.getColorStateList(R.styleable.TimePicker_headerTextColor); 194 } 195 196 mTextInputPickerHeader = mainView.findViewById(R.id.input_header); 197 198 if (headerTextColor != null) { 199 mHourView.setTextColor(headerTextColor); 200 mSeparatorView.setTextColor(headerTextColor); 201 mMinuteView.setTextColor(headerTextColor); 202 mAmLabel.setTextColor(headerTextColor); 203 mPmLabel.setTextColor(headerTextColor); 204 } 205 206 // Set up header background, if available. 207 if (a.hasValueOrEmpty(R.styleable.TimePicker_headerBackground)) { 208 mRadialTimePickerHeader.setBackground(a.getDrawable( 209 R.styleable.TimePicker_headerBackground)); 210 mTextInputPickerHeader.setBackground(a.getDrawable( 211 R.styleable.TimePicker_headerBackground)); 212 } 213 214 a.recycle(); 215 216 mRadialTimePickerView = (RadialTimePickerView) mainView.findViewById(R.id.radial_picker); 217 mRadialTimePickerView.applyAttributes(attrs, defStyleAttr, defStyleRes); 218 mRadialTimePickerView.setOnValueSelectedListener(mOnValueSelectedListener); 219 220 mTextInputPickerView = (TextInputTimePickerView) mainView.findViewById(R.id.input_mode); 221 mTextInputPickerView.setListener(mOnValueTypedListener); 222 223 mRadialTimePickerModeButton = 224 (ImageButton) mainView.findViewById(R.id.toggle_mode); 225 mRadialTimePickerModeButton.setOnClickListener(new View.OnClickListener() { 226 @Override 227 public void onClick(View v) { 228 toggleRadialPickerMode(); 229 } 230 }); 231 mRadialTimePickerModeEnabledDescription = context.getResources().getString( 232 R.string.time_picker_radial_mode_description); 233 mTextInputPickerModeEnabledDescription = context.getResources().getString( 234 R.string.time_picker_text_input_mode_description); 235 236 mAllowAutoAdvance = true; 237 238 updateHourFormat(); 239 240 // Initialize with current time. 241 mTempCalendar = Calendar.getInstance(mLocale); 242 final int currentHour = mTempCalendar.get(Calendar.HOUR_OF_DAY); 243 final int currentMinute = mTempCalendar.get(Calendar.MINUTE); 244 initialize(currentHour, currentMinute, mIs24Hour, HOUR_INDEX); 245 } 246 247 private void toggleRadialPickerMode() { 248 if (mRadialPickerModeEnabled) { 249 mRadialTimePickerView.setVisibility(View.GONE); 250 mRadialTimePickerHeader.setVisibility(View.GONE); 251 mTextInputPickerHeader.setVisibility(View.VISIBLE); 252 mTextInputPickerView.setVisibility(View.VISIBLE); 253 mRadialTimePickerModeButton.setImageResource(R.drawable.btn_clock_material); 254 mRadialTimePickerModeButton.setContentDescription( 255 mRadialTimePickerModeEnabledDescription); 256 mRadialPickerModeEnabled = false; 257 } else { 258 mRadialTimePickerView.setVisibility(View.VISIBLE); 259 mRadialTimePickerHeader.setVisibility(View.VISIBLE); 260 mTextInputPickerHeader.setVisibility(View.GONE); 261 mTextInputPickerView.setVisibility(View.GONE); 262 mRadialTimePickerModeButton.setImageResource(R.drawable.btn_keyboard_key_material); 263 mRadialTimePickerModeButton.setContentDescription( 264 mTextInputPickerModeEnabledDescription); 265 updateTextInputPicker(); 266 InputMethodManager imm = InputMethodManager.peekInstance(); 267 if (imm != null) { 268 imm.hideSoftInputFromWindow(mDelegator.getWindowToken(), 0); 269 } 270 mRadialPickerModeEnabled = true; 271 } 272 } 273 274 @Override 275 public boolean validateInput() { 276 return mTextInputPickerView.validateInput(); 277 } 278 279 /** 280 * Ensures that a TextView is wide enough to contain its text without 281 * wrapping or clipping. Measures the specified view and sets the minimum 282 * width to the view's desired width. 283 * 284 * @param v the text view to measure 285 */ 286 private static void ensureMinimumTextWidth(TextView v) { 287 v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); 288 289 // Set both the TextView and the View version of minimum 290 // width because they are subtly different. 291 final int minWidth = v.getMeasuredWidth(); 292 v.setMinWidth(minWidth); 293 v.setMinimumWidth(minWidth); 294 } 295 296 /** 297 * Updates hour formatting based on the current locale and 24-hour mode. 298 * <p> 299 * Determines how the hour should be formatted, sets member variables for 300 * leading zero and starting hour, and sets the hour view's presentation. 301 */ 302 private void updateHourFormat() { 303 final String bestDateTimePattern = DateFormat.getBestDateTimePattern( 304 mLocale, mIs24Hour ? "Hm" : "hm"); 305 final int lengthPattern = bestDateTimePattern.length(); 306 boolean showLeadingZero = false; 307 char hourFormat = '\0'; 308 309 for (int i = 0; i < lengthPattern; i++) { 310 final char c = bestDateTimePattern.charAt(i); 311 if (c == 'H' || c == 'h' || c == 'K' || c == 'k') { 312 hourFormat = c; 313 if (i + 1 < lengthPattern && c == bestDateTimePattern.charAt(i + 1)) { 314 showLeadingZero = true; 315 } 316 break; 317 } 318 } 319 320 mHourFormatShowLeadingZero = showLeadingZero; 321 mHourFormatStartsAtZero = hourFormat == 'K' || hourFormat == 'H'; 322 323 // Update hour text field. 324 final int minHour = mHourFormatStartsAtZero ? 0 : 1; 325 final int maxHour = (mIs24Hour ? 23 : 11) + minHour; 326 mHourView.setRange(minHour, maxHour); 327 mHourView.setShowLeadingZeroes(mHourFormatShowLeadingZero); 328 329 final String[] digits = DecimalFormatSymbols.getInstance(mLocale).getDigitStrings(); 330 int maxCharLength = 0; 331 for (int i = 0; i < 10; i++) { 332 maxCharLength = Math.max(maxCharLength, digits[i].length()); 333 } 334 mTextInputPickerView.setHourFormat(maxCharLength * 2); 335 } 336 337 static final CharSequence obtainVerbatim(String text) { 338 return new SpannableStringBuilder().append(text, 339 new TtsSpan.VerbatimBuilder(text).build(), 0); 340 } 341 342 /** 343 * The legacy text color might have been poorly defined. Ensures that it 344 * has an appropriate activated state, using the selected state if one 345 * exists or modifying the default text color otherwise. 346 * 347 * @param color a legacy text color, or {@code null} 348 * @return a color state list with an appropriate activated state, or 349 * {@code null} if a valid activated state could not be generated 350 */ 351 @Nullable 352 private ColorStateList applyLegacyColorFixes(@Nullable ColorStateList color) { 353 if (color == null || color.hasState(R.attr.state_activated)) { 354 return color; 355 } 356 357 final int activatedColor; 358 final int defaultColor; 359 if (color.hasState(R.attr.state_selected)) { 360 activatedColor = color.getColorForState(StateSet.get( 361 StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_SELECTED), 0); 362 defaultColor = color.getColorForState(StateSet.get( 363 StateSet.VIEW_STATE_ENABLED), 0); 364 } else { 365 activatedColor = color.getDefaultColor(); 366 367 // Generate a non-activated color using the disabled alpha. 368 final TypedArray ta = mContext.obtainStyledAttributes(ATTRS_DISABLED_ALPHA); 369 final float disabledAlpha = ta.getFloat(0, 0.30f); 370 defaultColor = multiplyAlphaComponent(activatedColor, disabledAlpha); 371 } 372 373 if (activatedColor == 0 || defaultColor == 0) { 374 // We somehow failed to obtain the colors. 375 return null; 376 } 377 378 final int[][] stateSet = new int[][] {{ R.attr.state_activated }, {}}; 379 final int[] colors = new int[] { activatedColor, defaultColor }; 380 return new ColorStateList(stateSet, colors); 381 } 382 383 private int multiplyAlphaComponent(int color, float alphaMod) { 384 final int srcRgb = color & 0xFFFFFF; 385 final int srcAlpha = (color >> 24) & 0xFF; 386 final int dstAlpha = (int) (srcAlpha * alphaMod + 0.5f); 387 return srcRgb | (dstAlpha << 24); 388 } 389 390 private static class ClickActionDelegate extends AccessibilityDelegate { 391 private final AccessibilityAction mClickAction; 392 393 public ClickActionDelegate(Context context, int resId) { 394 mClickAction = new AccessibilityAction( 395 AccessibilityNodeInfo.ACTION_CLICK, context.getString(resId)); 396 } 397 398 @Override 399 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { 400 super.onInitializeAccessibilityNodeInfo(host, info); 401 402 info.addAction(mClickAction); 403 } 404 } 405 406 private void initialize(int hourOfDay, int minute, boolean is24HourView, int index) { 407 mCurrentHour = hourOfDay; 408 mCurrentMinute = minute; 409 mIs24Hour = is24HourView; 410 updateUI(index); 411 } 412 413 private void updateUI(int index) { 414 updateHeaderAmPm(); 415 updateHeaderHour(mCurrentHour, false); 416 updateHeaderSeparator(); 417 updateHeaderMinute(mCurrentMinute, false); 418 updateRadialPicker(index); 419 updateTextInputPicker(); 420 421 mDelegator.invalidate(); 422 } 423 424 private void updateTextInputPicker() { 425 mTextInputPickerView.updateTextInputValues(getLocalizedHour(mCurrentHour), mCurrentMinute, 426 mCurrentHour < 12 ? AM : PM, mIs24Hour, mHourFormatStartsAtZero); 427 } 428 429 private void updateRadialPicker(int index) { 430 mRadialTimePickerView.initialize(mCurrentHour, mCurrentMinute, mIs24Hour); 431 setCurrentItemShowing(index, false, true); 432 } 433 434 private void updateHeaderAmPm() { 435 if (mIs24Hour) { 436 mAmPmLayout.setVisibility(View.GONE); 437 } else { 438 // Ensure that AM/PM layout is in the correct position. 439 final String dateTimePattern = DateFormat.getBestDateTimePattern(mLocale, "hm"); 440 final boolean isAmPmAtStart = dateTimePattern.startsWith("a"); 441 setAmPmAtStart(isAmPmAtStart); 442 443 updateAmPmLabelStates(mCurrentHour < 12 ? AM : PM); 444 } 445 } 446 447 private void setAmPmAtStart(boolean isAmPmAtStart) { 448 if (mIsAmPmAtStart != isAmPmAtStart) { 449 mIsAmPmAtStart = isAmPmAtStart; 450 451 final RelativeLayout.LayoutParams params = 452 (RelativeLayout.LayoutParams) mAmPmLayout.getLayoutParams(); 453 if (params.getRule(RelativeLayout.RIGHT_OF) != 0 || 454 params.getRule(RelativeLayout.LEFT_OF) != 0) { 455 if (isAmPmAtStart) { 456 params.removeRule(RelativeLayout.RIGHT_OF); 457 params.addRule(RelativeLayout.LEFT_OF, mHourView.getId()); 458 } else { 459 params.removeRule(RelativeLayout.LEFT_OF); 460 params.addRule(RelativeLayout.RIGHT_OF, mMinuteView.getId()); 461 } 462 } 463 464 mAmPmLayout.setLayoutParams(params); 465 } 466 } 467 468 /** 469 * Set the current hour. 470 */ 471 @Override 472 public void setHour(int hour) { 473 setHourInternal(hour, FROM_EXTERNAL_API, true); 474 } 475 476 private void setHourInternal(int hour, @ChangeSource int source, boolean announce) { 477 if (mCurrentHour == hour) { 478 return; 479 } 480 481 mCurrentHour = hour; 482 updateHeaderHour(hour, announce); 483 updateHeaderAmPm(); 484 485 if (source != FROM_RADIAL_PICKER) { 486 mRadialTimePickerView.setCurrentHour(hour); 487 mRadialTimePickerView.setAmOrPm(hour < 12 ? AM : PM); 488 } 489 if (source != FROM_INPUT_PICKER) { 490 updateTextInputPicker(); 491 } 492 493 mDelegator.invalidate(); 494 onTimeChanged(); 495 } 496 497 /** 498 * @return the current hour in the range (0-23) 499 */ 500 @Override 501 public int getHour() { 502 final int currentHour = mRadialTimePickerView.getCurrentHour(); 503 if (mIs24Hour) { 504 return currentHour; 505 } 506 507 if (mRadialTimePickerView.getAmOrPm() == PM) { 508 return (currentHour % HOURS_IN_HALF_DAY) + HOURS_IN_HALF_DAY; 509 } else { 510 return currentHour % HOURS_IN_HALF_DAY; 511 } 512 } 513 514 /** 515 * Set the current minute (0-59). 516 */ 517 @Override 518 public void setMinute(int minute) { 519 setMinuteInternal(minute, FROM_EXTERNAL_API); 520 } 521 522 private void setMinuteInternal(int minute, @ChangeSource int source) { 523 if (mCurrentMinute == minute) { 524 return; 525 } 526 527 mCurrentMinute = minute; 528 updateHeaderMinute(minute, true); 529 530 if (source != FROM_RADIAL_PICKER) { 531 mRadialTimePickerView.setCurrentMinute(minute); 532 } 533 if (source != FROM_INPUT_PICKER) { 534 updateTextInputPicker(); 535 } 536 537 mDelegator.invalidate(); 538 onTimeChanged(); 539 } 540 541 /** 542 * @return The current minute. 543 */ 544 @Override 545 public int getMinute() { 546 return mRadialTimePickerView.getCurrentMinute(); 547 } 548 549 /** 550 * Sets whether time is displayed in 24-hour mode or 12-hour mode with 551 * AM/PM indicators. 552 * 553 * @param is24Hour {@code true} to display time in 24-hour mode or 554 * {@code false} for 12-hour mode with AM/PM 555 */ 556 public void setIs24Hour(boolean is24Hour) { 557 if (mIs24Hour != is24Hour) { 558 mIs24Hour = is24Hour; 559 mCurrentHour = getHour(); 560 561 updateHourFormat(); 562 updateUI(mRadialTimePickerView.getCurrentItemShowing()); 563 } 564 } 565 566 /** 567 * @return {@code true} if time is displayed in 24-hour mode, or 568 * {@code false} if time is displayed in 12-hour mode with AM/PM 569 * indicators 570 */ 571 @Override 572 public boolean is24Hour() { 573 return mIs24Hour; 574 } 575 576 @Override 577 public void setEnabled(boolean enabled) { 578 mHourView.setEnabled(enabled); 579 mMinuteView.setEnabled(enabled); 580 mAmLabel.setEnabled(enabled); 581 mPmLabel.setEnabled(enabled); 582 mRadialTimePickerView.setEnabled(enabled); 583 mIsEnabled = enabled; 584 } 585 586 @Override 587 public boolean isEnabled() { 588 return mIsEnabled; 589 } 590 591 @Override 592 public int getBaseline() { 593 // does not support baseline alignment 594 return -1; 595 } 596 597 @Override 598 public Parcelable onSaveInstanceState(Parcelable superState) { 599 return new SavedState(superState, getHour(), getMinute(), 600 is24Hour(), getCurrentItemShowing()); 601 } 602 603 @Override 604 public void onRestoreInstanceState(Parcelable state) { 605 if (state instanceof SavedState) { 606 final SavedState ss = (SavedState) state; 607 initialize(ss.getHour(), ss.getMinute(), ss.is24HourMode(), ss.getCurrentItemShowing()); 608 mRadialTimePickerView.invalidate(); 609 } 610 } 611 612 @Override 613 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { 614 onPopulateAccessibilityEvent(event); 615 return true; 616 } 617 618 @Override 619 public void onPopulateAccessibilityEvent(AccessibilityEvent event) { 620 int flags = DateUtils.FORMAT_SHOW_TIME; 621 if (mIs24Hour) { 622 flags |= DateUtils.FORMAT_24HOUR; 623 } else { 624 flags |= DateUtils.FORMAT_12HOUR; 625 } 626 627 mTempCalendar.set(Calendar.HOUR_OF_DAY, getHour()); 628 mTempCalendar.set(Calendar.MINUTE, getMinute()); 629 630 final String selectedTime = DateUtils.formatDateTime(mContext, 631 mTempCalendar.getTimeInMillis(), flags); 632 final String selectionMode = mRadialTimePickerView.getCurrentItemShowing() == HOUR_INDEX ? 633 mSelectHours : mSelectMinutes; 634 event.getText().add(selectedTime + " " + selectionMode); 635 } 636 637 /** @hide */ 638 @Override 639 @TestApi 640 public View getHourView() { 641 return mHourView; 642 } 643 644 /** @hide */ 645 @Override 646 @TestApi 647 public View getMinuteView() { 648 return mMinuteView; 649 } 650 651 /** @hide */ 652 @Override 653 @TestApi 654 public View getAmView() { 655 return mAmLabel; 656 } 657 658 /** @hide */ 659 @Override 660 @TestApi 661 public View getPmView() { 662 return mPmLabel; 663 } 664 665 /** 666 * @return the index of the current item showing 667 */ 668 private int getCurrentItemShowing() { 669 return mRadialTimePickerView.getCurrentItemShowing(); 670 } 671 672 /** 673 * Propagate the time change 674 */ 675 private void onTimeChanged() { 676 mDelegator.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); 677 if (mOnTimeChangedListener != null) { 678 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute()); 679 } 680 if (mAutoFillChangeListener != null) { 681 mAutoFillChangeListener.onTimeChanged(mDelegator, getHour(), getMinute()); 682 } 683 } 684 685 private void tryVibrate() { 686 mDelegator.performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK); 687 } 688 689 private void updateAmPmLabelStates(int amOrPm) { 690 final boolean isAm = amOrPm == AM; 691 mAmLabel.setActivated(isAm); 692 mAmLabel.setChecked(isAm); 693 694 final boolean isPm = amOrPm == PM; 695 mPmLabel.setActivated(isPm); 696 mPmLabel.setChecked(isPm); 697 } 698 699 /** 700 * Converts hour-of-day (0-23) time into a localized hour number. 701 * <p> 702 * The localized value may be in the range (0-23), (1-24), (0-11), or 703 * (1-12) depending on the locale. This method does not handle leading 704 * zeroes. 705 * 706 * @param hourOfDay the hour-of-day (0-23) 707 * @return a localized hour number 708 */ 709 private int getLocalizedHour(int hourOfDay) { 710 if (!mIs24Hour) { 711 // Convert to hour-of-am-pm. 712 hourOfDay %= 12; 713 } 714 715 if (!mHourFormatStartsAtZero && hourOfDay == 0) { 716 // Convert to clock-hour (either of-day or of-am-pm). 717 hourOfDay = mIs24Hour ? 24 : 12; 718 } 719 720 return hourOfDay; 721 } 722 723 private void updateHeaderHour(int hourOfDay, boolean announce) { 724 final int localizedHour = getLocalizedHour(hourOfDay); 725 mHourView.setValue(localizedHour); 726 727 if (announce) { 728 tryAnnounceForAccessibility(mHourView.getText(), true); 729 } 730 } 731 732 private void updateHeaderMinute(int minuteOfHour, boolean announce) { 733 mMinuteView.setValue(minuteOfHour); 734 735 if (announce) { 736 tryAnnounceForAccessibility(mMinuteView.getText(), false); 737 } 738 } 739 740 /** 741 * The time separator is defined in the Unicode CLDR and cannot be supposed to be ":". 742 * 743 * See http://unicode.org/cldr/trac/browser/trunk/common/main 744 * 745 * We pass the correct "skeleton" depending on 12 or 24 hours view and then extract the 746 * separator as the character which is just after the hour marker in the returned pattern. 747 */ 748 private void updateHeaderSeparator() { 749 final String bestDateTimePattern = DateFormat.getBestDateTimePattern(mLocale, 750 (mIs24Hour) ? "Hm" : "hm"); 751 final String separatorText; 752 // See http://www.unicode.org/reports/tr35/tr35-dates.html for hour formats 753 final char[] hourFormats = {'H', 'h', 'K', 'k'}; 754 int hIndex = lastIndexOfAny(bestDateTimePattern, hourFormats); 755 if (hIndex == -1) { 756 // Default case 757 separatorText = ":"; 758 } else { 759 separatorText = Character.toString(bestDateTimePattern.charAt(hIndex + 1)); 760 } 761 mSeparatorView.setText(separatorText); 762 mTextInputPickerView.updateSeparator(separatorText); 763 } 764 765 static private int lastIndexOfAny(String str, char[] any) { 766 final int lengthAny = any.length; 767 if (lengthAny > 0) { 768 for (int i = str.length() - 1; i >= 0; i--) { 769 char c = str.charAt(i); 770 for (int j = 0; j < lengthAny; j++) { 771 if (c == any[j]) { 772 return i; 773 } 774 } 775 } 776 } 777 return -1; 778 } 779 780 private void tryAnnounceForAccessibility(CharSequence text, boolean isHour) { 781 if (mLastAnnouncedIsHour != isHour || !text.equals(mLastAnnouncedText)) { 782 // TODO: Find a better solution, potentially live regions? 783 mDelegator.announceForAccessibility(text); 784 mLastAnnouncedText = text; 785 mLastAnnouncedIsHour = isHour; 786 } 787 } 788 789 /** 790 * Show either Hours or Minutes. 791 */ 792 private void setCurrentItemShowing(int index, boolean animateCircle, boolean announce) { 793 mRadialTimePickerView.setCurrentItemShowing(index, animateCircle); 794 795 if (index == HOUR_INDEX) { 796 if (announce) { 797 mDelegator.announceForAccessibility(mSelectHours); 798 } 799 } else { 800 if (announce) { 801 mDelegator.announceForAccessibility(mSelectMinutes); 802 } 803 } 804 805 mHourView.setActivated(index == HOUR_INDEX); 806 mMinuteView.setActivated(index == MINUTE_INDEX); 807 } 808 809 private void setAmOrPm(int amOrPm) { 810 updateAmPmLabelStates(amOrPm); 811 812 if (mRadialTimePickerView.setAmOrPm(amOrPm)) { 813 mCurrentHour = getHour(); 814 updateTextInputPicker(); 815 if (mOnTimeChangedListener != null) { 816 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute()); 817 } 818 } 819 } 820 821 /** Listener for RadialTimePickerView interaction. */ 822 private final OnValueSelectedListener mOnValueSelectedListener = new OnValueSelectedListener() { 823 @Override 824 public void onValueSelected(int pickerType, int newValue, boolean autoAdvance) { 825 boolean valueChanged = false; 826 switch (pickerType) { 827 case RadialTimePickerView.HOURS: 828 if (getHour() != newValue) { 829 valueChanged = true; 830 } 831 final boolean isTransition = mAllowAutoAdvance && autoAdvance; 832 setHourInternal(newValue, FROM_RADIAL_PICKER, !isTransition); 833 if (isTransition) { 834 setCurrentItemShowing(MINUTE_INDEX, true, false); 835 836 final int localizedHour = getLocalizedHour(newValue); 837 mDelegator.announceForAccessibility(localizedHour + ". " + mSelectMinutes); 838 } 839 break; 840 case RadialTimePickerView.MINUTES: 841 if (getMinute() != newValue) { 842 valueChanged = true; 843 } 844 setMinuteInternal(newValue, FROM_RADIAL_PICKER); 845 break; 846 } 847 848 if (mOnTimeChangedListener != null && valueChanged) { 849 mOnTimeChangedListener.onTimeChanged(mDelegator, getHour(), getMinute()); 850 } 851 } 852 }; 853 854 private final OnValueTypedListener mOnValueTypedListener = new OnValueTypedListener() { 855 @Override 856 public void onValueChanged(int pickerType, int newValue) { 857 switch (pickerType) { 858 case TextInputTimePickerView.HOURS: 859 setHourInternal(newValue, FROM_INPUT_PICKER, false); 860 break; 861 case TextInputTimePickerView.MINUTES: 862 setMinuteInternal(newValue, FROM_INPUT_PICKER); 863 break; 864 case TextInputTimePickerView.AMPM: 865 setAmOrPm(newValue); 866 break; 867 } 868 } 869 }; 870 871 /** Listener for keyboard interaction. */ 872 private final OnValueChangedListener mDigitEnteredListener = new OnValueChangedListener() { 873 @Override 874 public void onValueChanged(NumericTextView view, int value, 875 boolean isValid, boolean isFinished) { 876 final Runnable commitCallback; 877 final View nextFocusTarget; 878 if (view == mHourView) { 879 commitCallback = mCommitHour; 880 nextFocusTarget = view.isFocused() ? mMinuteView : null; 881 } else if (view == mMinuteView) { 882 commitCallback = mCommitMinute; 883 nextFocusTarget = null; 884 } else { 885 return; 886 } 887 888 view.removeCallbacks(commitCallback); 889 890 if (isValid) { 891 if (isFinished) { 892 // Done with hours entry, make visual updates 893 // immediately and move to next focus if needed. 894 commitCallback.run(); 895 896 if (nextFocusTarget != null) { 897 nextFocusTarget.requestFocus(); 898 } 899 } else { 900 // May still be making changes. Postpone visual 901 // updates to prevent distracting the user. 902 view.postDelayed(commitCallback, DELAY_COMMIT_MILLIS); 903 } 904 } 905 } 906 }; 907 908 private final Runnable mCommitHour = new Runnable() { 909 @Override 910 public void run() { 911 setHour(mHourView.getValue()); 912 } 913 }; 914 915 private final Runnable mCommitMinute = new Runnable() { 916 @Override 917 public void run() { 918 setMinute(mMinuteView.getValue()); 919 } 920 }; 921 922 private final View.OnFocusChangeListener mFocusListener = new View.OnFocusChangeListener() { 923 @Override 924 public void onFocusChange(View v, boolean focused) { 925 if (focused) { 926 switch (v.getId()) { 927 case R.id.am_label: 928 setAmOrPm(AM); 929 break; 930 case R.id.pm_label: 931 setAmOrPm(PM); 932 break; 933 case R.id.hours: 934 setCurrentItemShowing(HOUR_INDEX, true, true); 935 break; 936 case R.id.minutes: 937 setCurrentItemShowing(MINUTE_INDEX, true, true); 938 break; 939 default: 940 // Failed to handle this click, don't vibrate. 941 return; 942 } 943 944 tryVibrate(); 945 } 946 } 947 }; 948 949 private final View.OnClickListener mClickListener = new View.OnClickListener() { 950 @Override 951 public void onClick(View v) { 952 953 final int amOrPm; 954 switch (v.getId()) { 955 case R.id.am_label: 956 setAmOrPm(AM); 957 break; 958 case R.id.pm_label: 959 setAmOrPm(PM); 960 break; 961 case R.id.hours: 962 setCurrentItemShowing(HOUR_INDEX, true, true); 963 break; 964 case R.id.minutes: 965 setCurrentItemShowing(MINUTE_INDEX, true, true); 966 break; 967 default: 968 // Failed to handle this click, don't vibrate. 969 return; 970 } 971 972 tryVibrate(); 973 } 974 }; 975 976 /** 977 * Delegates unhandled touches in a view group to the nearest child view. 978 */ 979 private static class NearestTouchDelegate implements View.OnTouchListener { 980 private View mInitialTouchTarget; 981 982 @Override 983 public boolean onTouch(View view, MotionEvent motionEvent) { 984 final int actionMasked = motionEvent.getActionMasked(); 985 if (actionMasked == MotionEvent.ACTION_DOWN) { 986 if (view instanceof ViewGroup) { 987 mInitialTouchTarget = findNearestChild((ViewGroup) view, 988 (int) motionEvent.getX(), (int) motionEvent.getY()); 989 } else { 990 mInitialTouchTarget = null; 991 } 992 } 993 994 final View child = mInitialTouchTarget; 995 if (child == null) { 996 return false; 997 } 998 999 final float offsetX = view.getScrollX() - child.getLeft(); 1000 final float offsetY = view.getScrollY() - child.getTop(); 1001 motionEvent.offsetLocation(offsetX, offsetY); 1002 final boolean handled = child.dispatchTouchEvent(motionEvent); 1003 motionEvent.offsetLocation(-offsetX, -offsetY); 1004 1005 if (actionMasked == MotionEvent.ACTION_UP 1006 || actionMasked == MotionEvent.ACTION_CANCEL) { 1007 mInitialTouchTarget = null; 1008 } 1009 1010 return handled; 1011 } 1012 1013 private View findNearestChild(ViewGroup v, int x, int y) { 1014 View bestChild = null; 1015 int bestDist = Integer.MAX_VALUE; 1016 1017 for (int i = 0, count = v.getChildCount(); i < count; i++) { 1018 final View child = v.getChildAt(i); 1019 final int dX = x - (child.getLeft() + child.getWidth() / 2); 1020 final int dY = y - (child.getTop() + child.getHeight() / 2); 1021 final int dist = dX * dX + dY * dY; 1022 if (bestDist > dist) { 1023 bestChild = child; 1024 bestDist = dist; 1025 } 1026 } 1027 1028 return bestChild; 1029 } 1030 } 1031} 1032