1/*
2 * Copyright (C) 2014 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 com.android.tv.settings.widget.picker;
18
19import android.os.Bundle;
20
21import java.util.ArrayList;
22import java.util.Calendar;
23
24
25public class TimePicker extends Picker {
26
27    private static final String EXTRA_24H_FORMAT = "24h_format";
28    private static final String EXTRA_DEFAULT_TO_CURRENT = "delault_to_current";
29
30    private static final int COL_HOUR = 0;
31    private static final int COL_MINUTE = 1;
32    private static final int COL_AMPM = 2;
33
34    private static final int HOURS_IN_HALF_DAY = 12;
35
36    private boolean mIs24hFormat = false;
37    private boolean mPendingTime = false;
38    private int mInitHour;
39    private int mInitMinute;
40    private boolean mInitIsPm;
41
42    public static TimePicker newInstance() {
43        return newInstance(true, true);
44    }
45
46    public static TimePicker newInstance(boolean is24hFormat, boolean defaultToCurrentTime) {
47        TimePicker picker = new TimePicker();
48        Bundle args = new Bundle();
49        args.putBoolean(EXTRA_24H_FORMAT, is24hFormat);
50        args.putBoolean(EXTRA_DEFAULT_TO_CURRENT, defaultToCurrentTime);
51        picker.setArguments(args);
52        return picker;
53    }
54
55    @Override
56    public void onCreate(Bundle savedInstanceState) {
57        mIs24hFormat = getArguments().getBoolean(EXTRA_24H_FORMAT, false);
58        boolean useCurrent = getArguments().getBoolean(EXTRA_DEFAULT_TO_CURRENT, false);
59
60        super.onCreate(savedInstanceState);
61
62        if (useCurrent) {
63            mPendingTime = true;
64            Calendar cal = Calendar.getInstance();
65            mInitHour = cal.get(Calendar.HOUR_OF_DAY);
66
67            if (!mIs24hFormat) {
68                if (mInitHour >= HOURS_IN_HALF_DAY) {
69                    // PM case, valid hours: 12-23
70                    mInitIsPm = true;
71                    if (mInitHour > HOURS_IN_HALF_DAY) {
72                        mInitHour = mInitHour - HOURS_IN_HALF_DAY;
73                    }
74                } else {
75                    // AM case, valid hours: 0-11
76                    mInitIsPm = false;
77                    if (mInitHour == 0) {
78                        mInitHour = HOURS_IN_HALF_DAY;
79                    }
80                }
81            }
82
83            mInitMinute = cal.get(Calendar.MINUTE);
84        }
85    }
86
87    @Override
88    public void onResume() {
89        if (mPendingTime) {
90            mPendingTime = false;
91            setTime(mInitHour, mInitMinute, mInitIsPm);
92        }
93        super.onResume();
94    }
95
96    protected boolean setTime(int hour, int minute, boolean isPm) {
97        if (minute < 0 || minute > 59) {
98            return false;
99        }
100
101        if (mIs24hFormat) {
102            if (hour < 0 || hour > 23) {
103                return false;
104            }
105        } else {
106            if (hour < 1 || hour > 12) {
107                return false;
108            }
109        }
110
111        updateSelection(COL_HOUR, mIs24hFormat ? hour : (hour - 1));
112        updateSelection(COL_MINUTE, minute);
113        if (!mIs24hFormat) {
114            updateSelection(COL_AMPM, isPm ? 1 : 0);
115        }
116
117        return true;
118    }
119
120    @Override
121    protected ArrayList<PickerColumn> getColumns() {
122        ArrayList<PickerColumn> ret = new ArrayList<PickerColumn>();
123        PickerColumn hours = new PickerColumn(mIs24hFormat ? mConstant.hours24 : mConstant.hours12);
124        PickerColumn minutes = new PickerColumn(mConstant.minutes);
125        ret.add(hours);
126        ret.add(minutes);
127
128        if (!mIs24hFormat) {
129            PickerColumn ampm = new PickerColumn(mConstant.ampm);
130            ret.add(ampm);
131        }
132        return ret;
133    }
134
135    @Override
136    protected String getSeparator() {
137        return mConstant.timeSeparator;
138    }
139}
140