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