165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/* 265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project 365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License"); 565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License. 665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at 765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * http://www.apache.org/licenses/LICENSE-2.0 965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software 1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS, 1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and 1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License. 1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */ 1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.widget.picker; 1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Bundle; 2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.ArrayList; 2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.Calendar; 2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class TimePicker extends Picker { 2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private static final String EXTRA_24H_FORMAT = "24h_format"; 2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private static final String EXTRA_DEFAULT_TO_CURRENT = "delault_to_current"; 2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private static final int COL_HOUR = 0; 3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private static final int COL_MINUTE = 1; 3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private static final int COL_AMPM = 2; 3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private static final int HOURS_IN_HALF_DAY = 12; 3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private boolean mIs24hFormat = false; 3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private boolean mPendingTime = false; 3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private int mInitHour; 3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private int mInitMinute; 4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private boolean mInitIsPm; 4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public static TimePicker newInstance() { 4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return newInstance(true, true); 4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public static TimePicker newInstance(boolean is24hFormat, boolean defaultToCurrentTime) { 4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane TimePicker picker = new TimePicker(); 4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane Bundle args = new Bundle(); 4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane args.putBoolean(EXTRA_24H_FORMAT, is24hFormat); 5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane args.putBoolean(EXTRA_DEFAULT_TO_CURRENT, defaultToCurrentTime); 5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane picker.setArguments(args); 5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return picker; 5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane @Override 5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public void onCreate(Bundle savedInstanceState) { 5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mIs24hFormat = getArguments().getBoolean(EXTRA_24H_FORMAT, false); 5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane boolean useCurrent = getArguments().getBoolean(EXTRA_DEFAULT_TO_CURRENT, false); 5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane super.onCreate(savedInstanceState); 6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (useCurrent) { 6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mPendingTime = true; 6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane Calendar cal = Calendar.getInstance(); 6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mInitHour = cal.get(Calendar.HOUR_OF_DAY); 6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (!mIs24hFormat) { 6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (mInitHour >= HOURS_IN_HALF_DAY) { 6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane // PM case, valid hours: 12-23 7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mInitIsPm = true; 7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (mInitHour > HOURS_IN_HALF_DAY) { 7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mInitHour = mInitHour - HOURS_IN_HALF_DAY; 7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } else { 7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane // AM case, valid hours: 0-11 7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mInitIsPm = false; 7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (mInitHour == 0) { 7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mInitHour = HOURS_IN_HALF_DAY; 7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mInitMinute = cal.get(Calendar.MINUTE); 8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane @Override 8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public void onResume() { 8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (mPendingTime) { 9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mPendingTime = false; 9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane setTime(mInitHour, mInitMinute, mInitIsPm); 9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane super.onResume(); 9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 9565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 9665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane protected boolean setTime(int hour, int minute, boolean isPm) { 9765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (minute < 0 || minute > 59) { 9865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return false; 9965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 10065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 10165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (mIs24hFormat) { 10265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (hour < 0 || hour > 23) { 10365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return false; 10465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 10565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } else { 10665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (hour < 1 || hour > 12) { 10765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return false; 10865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 10965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 11065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 11165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane updateSelection(COL_HOUR, mIs24hFormat ? hour : (hour - 1)); 11265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane updateSelection(COL_MINUTE, minute); 11365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (!mIs24hFormat) { 11465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane updateSelection(COL_AMPM, isPm ? 1 : 0); 11565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 11665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 11765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return true; 11865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 11965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 12065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane @Override 12165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane protected ArrayList<PickerColumn> getColumns() { 12265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane ArrayList<PickerColumn> ret = new ArrayList<PickerColumn>(); 12365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane PickerColumn hours = new PickerColumn(mIs24hFormat ? mConstant.hours24 : mConstant.hours12); 12465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane PickerColumn minutes = new PickerColumn(mConstant.minutes); 12565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane ret.add(hours); 12665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane ret.add(minutes); 12765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 12865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (!mIs24hFormat) { 12965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane PickerColumn ampm = new PickerColumn(mConstant.ampm); 13065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane ret.add(ampm); 13165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 13265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return ret; 13365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 13465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 13565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane @Override 13665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane protected String getSeparator() { 13765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return mConstant.timeSeparator; 13865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 13965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane} 140