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.DatePickerDialog;
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.DatePicker;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.preference.Preference;
30 
31 import com.android.settings.core.PreferenceControllerMixin;
32 import com.android.settingslib.RestrictedPreference;
33 import com.android.settingslib.core.AbstractPreferenceController;
34 
35 import java.util.Calendar;
36 
37 public class DatePreferenceController extends AbstractPreferenceController
38         implements PreferenceControllerMixin, DatePickerDialog.OnDateSetListener {
39 
40     public interface DatePreferenceHost extends UpdateTimeAndDateCallback {
showDatePicker()41         void showDatePicker();
42     }
43 
44     public static final int DIALOG_DATEPICKER = 0;
45 
46     private static final String KEY_DATE = "date";
47 
48     private final DatePreferenceHost mHost;
49     private final AutoTimePreferenceController mAutoTimePreferenceController;
50 
DatePreferenceController(Context context, DatePreferenceHost host, AutoTimePreferenceController autoTimePreferenceController)51     public DatePreferenceController(Context context, DatePreferenceHost host,
52             AutoTimePreferenceController autoTimePreferenceController) {
53         super(context);
54         mHost = host;
55         mAutoTimePreferenceController = autoTimePreferenceController;
56     }
57 
58     @Override
isAvailable()59     public boolean isAvailable() {
60         return true;
61     }
62 
63     @Override
updateState(Preference preference)64     public void updateState(Preference preference) {
65         if (!(preference instanceof RestrictedPreference)) {
66             return;
67         }
68         final Calendar now = Calendar.getInstance();
69         preference.setSummary(DateFormat.getLongDateFormat(mContext).format(now.getTime()));
70         if (!((RestrictedPreference) preference).isDisabledByAdmin()) {
71             preference.setEnabled(!mAutoTimePreferenceController.isEnabled());
72         }
73     }
74 
75     @Override
handlePreferenceTreeClick(Preference preference)76     public boolean handlePreferenceTreeClick(Preference preference) {
77         if (!TextUtils.equals(preference.getKey(), KEY_DATE)) {
78             return false;
79         }
80         mHost.showDatePicker();
81         return true;
82     }
83 
84     @Override
getPreferenceKey()85     public String getPreferenceKey() {
86         return KEY_DATE;
87     }
88 
89     @Override
onDateSet(DatePicker view, int year, int month, int day)90     public void onDateSet(DatePicker view, int year, int month, int day) {
91         setDate(year, month, day);
92         mHost.updateTimeAndDateDisplay(mContext);
93     }
94 
buildDatePicker(Activity activity)95     public DatePickerDialog buildDatePicker(Activity activity) {
96         final Calendar calendar = Calendar.getInstance();
97         final DatePickerDialog d = new DatePickerDialog(
98                 activity,
99                 this,
100                 calendar.get(Calendar.YEAR),
101                 calendar.get(Calendar.MONTH),
102                 calendar.get(Calendar.DAY_OF_MONTH));
103         // The system clock can't represent dates outside this range.
104         calendar.clear();
105         calendar.set(2007, Calendar.JANUARY, 1);
106         d.getDatePicker().setMinDate(calendar.getTimeInMillis());
107         calendar.clear();
108         calendar.set(2037, Calendar.DECEMBER, 31);
109         d.getDatePicker().setMaxDate(calendar.getTimeInMillis());
110         return d;
111     }
112 
113     @VisibleForTesting
setDate(int year, int month, int day)114     void setDate(int year, int month, int day) {
115         Calendar c = Calendar.getInstance();
116 
117         c.set(Calendar.YEAR, year);
118         c.set(Calendar.MONTH, month);
119         c.set(Calendar.DAY_OF_MONTH, day);
120         long when = Math.max(c.getTimeInMillis(), DatePreferenceHost.MIN_DATE);
121 
122         if (when / 1000 < Integer.MAX_VALUE) {
123             TimeDetector timeDetector = mContext.getSystemService(TimeDetector.class);
124             ManualTimeSuggestion manualTimeSuggestion =
125                     TimeDetector.createManualTimeSuggestion(when, "Settings: Set date");
126             timeDetector.suggestManualTime(manualTimeSuggestion);
127         }
128     }
129 }
130