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.system; 18 19import android.app.AlarmManager; 20import android.app.Fragment; 21import android.content.Context; 22import android.content.Intent; 23import android.os.Bundle; 24import android.support.annotation.Nullable; 25import android.text.format.DateFormat; 26 27import com.android.tv.settings.R; 28import com.android.tv.settings.dialog.old.ContentFragment; 29import com.android.tv.settings.dialog.old.DialogActivity; 30import com.android.tv.settings.widget.picker.DatePicker; 31import com.android.tv.settings.widget.picker.Picker; 32import com.android.tv.settings.widget.picker.PickerConstants; 33import com.android.tv.settings.widget.picker.TimePicker; 34 35import java.util.Calendar; 36import java.util.List; 37 38public class SetDateTimeActivity extends DialogActivity { 39 40 private static final String EXTRA_PICKER_TYPE = "SetDateTimeActivity.pickerType"; 41 private static final String TYPE_DATE = "date"; 42 private static final String TYPE_TIME = "time"; 43 44 private static final int HOURS_IN_HALF_DAY = 12; 45 46 private class DatePickerListener implements Picker.ResultListener { 47 @Override 48 public void onCommitResult(List<String> result) { 49 String formatOrder = new String( 50 DateFormat.getDateFormatOrder(SetDateTimeActivity.this)).toUpperCase(); 51 int yIndex = formatOrder.indexOf('Y'); 52 int mIndex = formatOrder.indexOf('M'); 53 int dIndex = formatOrder.indexOf('D'); 54 if (yIndex < 0 || mIndex < 0 || dIndex < 0 || 55 yIndex > 2 || mIndex > 2 || dIndex > 2) { 56 // Badly formatted input. Use default order. 57 mIndex = 0; 58 dIndex = 1; 59 yIndex = 2; 60 } 61 String month = result.get(mIndex); 62 int day = Integer.parseInt(result.get(dIndex)); 63 int year = Integer.parseInt(result.get(yIndex)); 64 int monthInt = 0; 65 String[] months = PickerConstants.getDateInstance(getResources()).months; 66 int totalMonths = months.length; 67 for (int i = 0; i < totalMonths; i++) { 68 if (months[i].equals(month)) { 69 monthInt = i; 70 } 71 } 72 73 setDate(SetDateTimeActivity.this, year, monthInt, day); 74 finish(); 75 } 76 } 77 78 private class TimePickerListener implements Picker.ResultListener { 79 @Override 80 public void onCommitResult(List<String> result) { 81 boolean is24hFormat = isTimeFormat24h(SetDateTimeActivity.this); 82 final TimePicker.ColumnOrder columnOrder = new TimePicker.ColumnOrder(is24hFormat); 83 int hour = Integer.parseInt(result.get(columnOrder.hours)); 84 int minute = Integer.parseInt(result.get(columnOrder.minutes)); 85 if (!is24hFormat) { 86 String ampm = result.get(columnOrder.amPm); 87 if (ampm.equals(getResources().getStringArray(R.array.ampm)[1])) { 88 // PM case, valid hours: 12-23 89 hour = (hour % HOURS_IN_HALF_DAY) + HOURS_IN_HALF_DAY; 90 } else { 91 // AM case, valid hours: 0-11 92 hour = hour % HOURS_IN_HALF_DAY; 93 } 94 } 95 96 setTime(SetDateTimeActivity.this, hour, minute); 97 finish(); 98 } 99 } 100 101 public static Intent getSetDateIntent(Context context) { 102 final Intent intent = new Intent(context, SetDateTimeActivity.class); 103 intent.putExtra(EXTRA_PICKER_TYPE, TYPE_DATE); 104 return intent; 105 } 106 107 public static Intent getSetTimeIntent(Context context) { 108 final Intent intent = new Intent(context, SetDateTimeActivity.class); 109 intent.putExtra(EXTRA_PICKER_TYPE, TYPE_TIME); 110 return intent; 111 } 112 113 @Override 114 protected void onCreate(@Nullable Bundle savedInstanceState) { 115 super.onCreate(savedInstanceState); 116 117 final String pickerType = getIntent().getStringExtra(EXTRA_PICKER_TYPE); 118 final Picker.ResultListener resultListener; 119 if (TYPE_DATE.equals(pickerType)) { 120 resultListener = new DatePickerListener(); 121 } else if (TYPE_TIME.equals(pickerType)) { 122 resultListener = new TimePickerListener(); 123 } else { 124 throw new IllegalArgumentException("Must specify " + EXTRA_PICKER_TYPE + " in intent"); 125 } 126 127 if (savedInstanceState == null) { 128 final Fragment contentFragment; 129 final Fragment actionFragment; 130 131 if (TYPE_DATE.equals(pickerType)) { 132 contentFragment = ContentFragment.newInstance(getString(R.string.system_set_date), 133 getString(R.string.system_date), null, R.drawable.ic_access_time_132dp, 134 getColor(R.color.icon_background)); 135 actionFragment = DatePicker 136 .newInstance(new String(DateFormat.getDateFormatOrder(this))); 137 } else { 138 contentFragment = ContentFragment.newInstance(getString(R.string.system_set_time), 139 getString(R.string.system_time), null, R.drawable.ic_access_time_132dp, 140 getColor(R.color.icon_background)); 141 actionFragment = TimePicker.newInstance(isTimeFormat24h(this), true); 142 } 143 144 setContentAndActionFragments(contentFragment, actionFragment); 145 } 146 147 getFragmentManager().executePendingTransactions(); 148 149 final Picker picker = (Picker) getActionFragment(); 150 picker.setResultListener(resultListener); 151 } 152 153 public static void setDate(Context context, int year, int month, int day) { 154 Calendar c = Calendar.getInstance(); 155 156 c.set(Calendar.YEAR, year); 157 c.set(Calendar.MONTH, month); 158 c.set(Calendar.DAY_OF_MONTH, day); 159 long when = c.getTimeInMillis(); 160 161 ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when); 162 } 163 164 public static void setTime(Context context, int hourOfDay, int minute) { 165 Calendar c = Calendar.getInstance(); 166 167 c.set(Calendar.HOUR_OF_DAY, hourOfDay); 168 c.set(Calendar.MINUTE, minute); 169 c.set(Calendar.SECOND, 0); 170 c.set(Calendar.MILLISECOND, 0); 171 long when = c.getTimeInMillis(); 172 173 ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when); 174 } 175 176 private static boolean isTimeFormat24h(Context context) { 177 return DateFormat.is24HourFormat(context); 178 } 179} 180