blob: 1eea1fb06bdac729b978aac6d8bc77992973da0e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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.os.Handler;
20
21/**
22 * Provides callbacks indicating the steps in two digit pressing within a
23 * timeout.
24 *
25 * Package private: only relevant in helping {@link TimeSpinnerHelper}.
26 */
27class DoubleDigitManager {
28
29 private final long timeoutInMillis;
30 private final CallBack mCallBack;
31
32 private Integer intermediateDigit;
33
34 /**
35 * @param timeoutInMillis How long after the first digit is pressed does
36 * the user have to press the second digit?
37 * @param callBack The callback to indicate what's going on with the user.
38 */
39 public DoubleDigitManager(long timeoutInMillis, CallBack callBack) {
40 this.timeoutInMillis = timeoutInMillis;
41 mCallBack = callBack;
42 }
43
44 /**
45 * Report to this manager that a digit was pressed.
46 * @param digit
47 */
48 public void reportDigit(int digit) {
49 if (intermediateDigit == null) {
50 intermediateDigit = digit;
51
52 new Handler().postDelayed(new Runnable() {
53 public void run() {
54 if (intermediateDigit != null) {
55 mCallBack.singleDigitFinal(intermediateDigit);
56 intermediateDigit = null;
57 }
58 }
59 }, timeoutInMillis);
60
61 if (!mCallBack.singleDigitIntermediate(digit)) {
62
63 // this wasn't a good candidate for the intermediate digit,
64 // make it the final digit (since there is no opportunity to
65 // reject the final digit).
66 intermediateDigit = null;
67 mCallBack.singleDigitFinal(digit);
68 }
69 } else if (mCallBack.twoDigitsFinal(intermediateDigit, digit)) {
70 intermediateDigit = null;
71 }
72 }
73
74 /**
75 * The callback to indicate what is going on with the digits pressed.
76 */
77 static interface CallBack {
78
79 /**
80 * A digit was pressed, and there are no intermediate digits.
81 * @param digit The digit pressed.
82 * @return Whether the digit was accepted; how the user of this manager
83 * tells us that the intermediate digit is acceptable as an
84 * intermediate digit.
85 */
86 boolean singleDigitIntermediate(int digit);
87
88 /**
89 * A single digit was pressed, and it is 'the final answer'.
90 * - a single digit pressed, and the timeout expires.
91 * - a single digit pressed, and {@link #singleDigitIntermediate}
92 * returned false.
93 * @param digit The digit.
94 */
95 void singleDigitFinal(int digit);
96
97 /**
98 * The user pressed digit1, then digit2 within the timeout.
99 * @param digit1
100 * @param digit2
101 */
102 boolean twoDigitsFinal(int digit1, int digit2);
103 }
104
105}