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 */
16package com.android.uiautomator.common.helpers;
17
18import com.android.uiautomator.core.UiObject;
19import com.android.uiautomator.core.UiObjectNotFoundException;
20import com.android.uiautomator.core.UiSelector;
21
22import java.util.Calendar;
23import java.util.Locale;
24
25/**
26 * Use this helper anywhere there is a time picker to manage. This helper
27 * will set time specified in a Calendar object.
28 */
29public class TimePickerHelper {
30
31    public static final int HOUR = 0;
32    public static final int MINUTE = 1;
33    public static final int MERIDIEM = 2;
34
35    public static String getCurrentHour() throws UiObjectNotFoundException {
36        return getNumberPickerField(HOUR).getText();
37    }
38
39    public static String getCurrentMinute() throws UiObjectNotFoundException {
40        return getNumberPickerField(MINUTE).getText();
41    }
42
43    public static String getCurrentMeridiem() throws UiObjectNotFoundException {
44        return getNumberPickerField(MERIDIEM).getText();
45    }
46
47
48    public static void incrementHour() throws UiObjectNotFoundException {
49        incrementHour(1);
50    }
51
52    public static void incrementHour(int count) throws UiObjectNotFoundException {
53        for (int x = 0; x < count; x++)
54            getNumberPickerIncrementButton(HOUR).click();
55    }
56
57    public static void decrementHour() throws UiObjectNotFoundException {
58        decrementHour(1);
59    }
60
61    public static void decrementHour(int count) throws UiObjectNotFoundException {
62        for (int x = 0; x < count; x++)
63            getNumberPickerDecrementButton(HOUR).click();
64    }
65
66    public static void incrementMinute() throws UiObjectNotFoundException {
67        incrementMinute(1);
68    }
69
70    public static void incrementMinute(int count) throws UiObjectNotFoundException {
71        for (int x = 0; x < count; x++)
72            getNumberPickerIncrementButton(MINUTE).click();
73    }
74
75    public static void decrementMinute() throws UiObjectNotFoundException {
76        decrementMinute(1);
77    }
78
79    public static void decrementMinute(int count) throws UiObjectNotFoundException {
80        for (int x = 0; x < count; x++)
81            getNumberPickerDecrementButton(MINUTE).click();
82    }
83
84    public static void selectPM() throws UiObjectNotFoundException {
85        getNumberPicker(MERIDIEM).getChild(new UiSelector().text("PM")).click();
86    }
87
88    public static void selectAM() throws UiObjectNotFoundException {
89        getNumberPicker(MERIDIEM).getChild(new UiSelector().text("AM")).click();
90    }
91
92    public static UiObject getNumberPicker(int instance) {
93        return new UiObject(new UiSelector().className(
94                android.widget.NumberPicker.class.getName()).instance(instance));
95    }
96
97    public static UiObject getNumberPickerField(int instance)
98            throws UiObjectNotFoundException {
99        return getNumberPicker(instance).getChild(
100                new UiSelector().className(android.widget.EditText.class.getName()));
101    }
102
103    public static UiObject getNumberPickerDecrementButton(int instance)
104            throws UiObjectNotFoundException {
105        return getNumberPicker(instance).getChild(
106                new UiSelector().className(android.widget.Button.class.getName()).instance(0));
107    }
108
109    public static UiObject getNumberPickerIncrementButton(int instance)
110            throws UiObjectNotFoundException {
111        return getNumberPicker(instance).getChild(
112                new UiSelector().className(android.widget.Button.class.getName()).instance(1));
113    }
114
115    public static void clickDone() throws UiObjectNotFoundException {
116        new UiObject(new UiSelector().text("Done")).click();
117    }
118
119    public static void setTime(Calendar cal) throws UiObjectNotFoundException {
120        // Adjust minutes - increment or decrement using the shortest path
121        int tpMinute = Integer.parseInt(getCurrentMinute());
122        int calMinute = cal.get(Calendar.MINUTE);
123        if (calMinute > tpMinute) {
124            if (calMinute - tpMinute < 30)
125                incrementMinute(calMinute - tpMinute);
126            else
127                decrementMinute(tpMinute - calMinute + 60);
128        } else if (tpMinute > calMinute) {
129            if (tpMinute - calMinute < 30)
130                decrementMinute(tpMinute - calMinute);
131            else
132                incrementMinute(calMinute - tpMinute + 60);
133        }
134
135        // Adjust hour - increment or decrement using the shortest path
136        int tpHour = Integer.parseInt(getCurrentHour());
137        int calHour = cal.get(Calendar.HOUR);
138        if (calHour > tpHour) {
139            if (calHour - tpHour < 6)
140                incrementHour(calHour - tpHour);
141            else
142                decrementHour(tpHour - calHour + 12);
143        } else if (tpHour > calHour) {
144            if (tpHour - calHour < 6)
145                decrementHour(tpHour - calHour);
146            else
147                incrementHour(calHour - tpHour + 12);
148        }
149
150        // Adjust meridiem
151        String calMer = cal.getDisplayName(Calendar.AM_PM, Calendar.SHORT, Locale.US);
152        String tpMer = getCurrentMeridiem();
153        if (tpMer.equalsIgnoreCase(calMer))
154            return;
155
156        if (!calMer.equalsIgnoreCase("AM")) {
157            selectPM();
158        } else {
159            selectAM();
160        }
161    }
162}
163