| Fan Zhang | beddff8 | 2016-11-18 11:10:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | package com.android.settings.datetime; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.AlarmManager; |
| 21 | import android.app.TimePickerDialog; |
| 22 | import android.content.Context; |
| 23 | import android.support.v7.preference.Preference; |
| 24 | import android.text.TextUtils; |
| 25 | import android.text.format.DateFormat; |
| 26 | import android.widget.TimePicker; |
| 27 | |
| 28 | import com.android.settings.core.PreferenceController; |
| 29 | |
| 30 | import java.util.Calendar; |
| 31 | |
| 32 | public class TimePreferenceController extends PreferenceController implements |
| 33 | TimePickerDialog.OnTimeSetListener { |
| 34 | |
| 35 | public interface TimePreferenceHost extends UpdateTimeAndDateCallback { |
| 36 | void showTimePicker(); |
| 37 | } |
| 38 | |
| 39 | public static final int DIALOG_TIMEPICKER = 1; |
| 40 | |
| 41 | private static final String KEY_TIME = "time"; |
| 42 | |
| 43 | private final AutoTimePreferenceController mAutoTimePreferenceController; |
| 44 | private final TimePreferenceHost mHost; |
| 45 | |
| 46 | |
| 47 | public TimePreferenceController(Context context, |
| 48 | TimePreferenceHost callback, |
| 49 | AutoTimePreferenceController autoTimePreferenceController) { |
| 50 | super(context); |
| 51 | mHost = callback; |
| 52 | mAutoTimePreferenceController = autoTimePreferenceController; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public boolean isAvailable() { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void updateState(Preference preference) { |
| 62 | final Calendar now = Calendar.getInstance(); |
| 63 | preference.setSummary(DateFormat.getTimeFormat(mContext).format(now.getTime())); |
| 64 | preference.setEnabled(!mAutoTimePreferenceController.isEnabled()); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public boolean handlePreferenceTreeClick(Preference preference) { |
| 69 | if (!TextUtils.equals(KEY_TIME, preference.getKey())) { |
| 70 | return false; |
| 71 | } |
| 72 | mHost.showTimePicker(); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String getPreferenceKey() { |
| 78 | return KEY_TIME; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void onTimeSet(TimePicker view, int hourOfDay, int minute) { |
| 83 | if (mContext != null) { |
| 84 | setTime(hourOfDay, minute); |
| 85 | mHost.updateTimeAndDateDisplay(mContext); |
| 86 | } |
| 87 | // We don't need to call timeUpdated() here because the TIME_CHANGED |
| 88 | // broadcast is sent by the AlarmManager as a side effect of setting the |
| 89 | // SystemClock time. |
| 90 | } |
| 91 | |
| 92 | public TimePickerDialog buildTimePicker(Activity activity) { |
| 93 | final Calendar calendar = Calendar.getInstance(); |
| 94 | return new TimePickerDialog( |
| 95 | activity, |
| 96 | this, |
| 97 | calendar.get(Calendar.HOUR_OF_DAY), |
| 98 | calendar.get(Calendar.MINUTE), |
| 99 | DateFormat.is24HourFormat(activity)); |
| 100 | } |
| 101 | |
| 102 | void setTime(int hourOfDay, int minute) { |
| 103 | Calendar c = Calendar.getInstance(); |
| 104 | |
| 105 | c.set(Calendar.HOUR_OF_DAY, hourOfDay); |
| 106 | c.set(Calendar.MINUTE, minute); |
| 107 | c.set(Calendar.SECOND, 0); |
| 108 | c.set(Calendar.MILLISECOND, 0); |
| 109 | long when = Math.max(c.getTimeInMillis(), TimePreferenceHost.MIN_DATE); |
| 110 | |
| 111 | if (when / 1000 < Integer.MAX_VALUE) { |
| 112 | ((AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE)).setTime(when); |
| 113 | } |
| 114 | } |
| 115 | } |