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 static android.app.time.Capabilities.CAPABILITY_POSSESSED;
20 
21 import android.app.DatePickerDialog;
22 import android.app.time.TimeCapabilities;
23 import android.app.time.TimeManager;
24 import android.app.timedetector.ManualTimeSuggestion;
25 import android.app.timedetector.TimeDetector;
26 import android.app.timedetector.TimeDetectorHelper;
27 import android.content.Context;
28 import android.text.TextUtils;
29 import android.text.format.DateFormat;
30 import android.util.Log;
31 import android.widget.DatePicker;
32 
33 import androidx.annotation.VisibleForTesting;
34 import androidx.preference.Preference;
35 
36 import com.android.settings.core.BasePreferenceController;
37 import com.android.settingslib.RestrictedPreference;
38 
39 import java.util.Calendar;
40 
41 public class DatePreferenceController extends BasePreferenceController
42         implements DatePickerDialog.OnDateSetListener {
43 
44     public interface DatePreferenceHost extends UpdateTimeAndDateCallback {
showDatePicker()45         void showDatePicker();
46     }
47 
48     public static final int DIALOG_DATEPICKER = 0;
49 
50     private static final String TAG = "DatePreferenceController";
51 
52     private DatePreferenceHost mHost;
53     private final TimeManager mTimeManager;
54 
DatePreferenceController(Context context, String preferenceKey)55     public DatePreferenceController(Context context, String preferenceKey) {
56         super(context, preferenceKey);
57         mTimeManager = context.getSystemService(TimeManager.class);
58     }
59 
setHost(DatePreferenceHost host)60     public void setHost(DatePreferenceHost host) {
61         mHost = host;
62     }
63 
64     @Override
getAvailabilityStatus()65     public int getAvailabilityStatus() {
66         return isEnabled() ? AVAILABLE : DISABLED_DEPENDENT_SETTING;
67     }
68 
69     @Override
updateState(Preference preference)70     public void updateState(Preference preference) {
71         super.updateState(preference);
72 
73         if (preference instanceof RestrictedPreference
74                 && ((RestrictedPreference) preference).isDisabledByAdmin()) {
75             return;
76         }
77         preference.setEnabled(isEnabled());
78     }
79 
80     @Override
getSummary()81     public CharSequence getSummary() {
82         Calendar now = Calendar.getInstance();
83         return DateFormat.getLongDateFormat(mContext).format(now.getTime());
84     }
85 
86     @Override
handlePreferenceTreeClick(Preference preference)87     public boolean handlePreferenceTreeClick(Preference preference) {
88         if (!TextUtils.equals(getPreferenceKey(), preference.getKey())) {
89             return false;
90         }
91         mHost.showDatePicker();
92         return true;
93     }
94 
95     @Override
onDateSet(DatePicker view, int year, int month, int day)96     public void onDateSet(DatePicker view, int year, int month, int day) {
97         setDate(year, month, day);
98         mHost.updateTimeAndDateDisplay(mContext);
99     }
100 
101     /**
102      * Builds a {@link DatePickerDialog} that can be used to request the current date from the user.
103      */
buildDatePicker( Context parentContext, TimeDetectorHelper timeDetectorHelper)104     public DatePickerDialog buildDatePicker(
105             Context parentContext, TimeDetectorHelper timeDetectorHelper) {
106         final Calendar calendar = Calendar.getInstance();
107         final DatePickerDialog dialog = new DatePickerDialog(
108                 parentContext,
109                 this,
110                 calendar.get(Calendar.YEAR),
111                 calendar.get(Calendar.MONTH),
112                 calendar.get(Calendar.DAY_OF_MONTH));
113 
114         // Limit the dates the user can pick to a sensible range.
115         DatePicker datePicker = dialog.getDatePicker();
116 
117         calendar.clear();
118         int minYear = timeDetectorHelper.getManualDateSelectionYearMin();
119         calendar.set(minYear, Calendar.JANUARY, 1);
120         datePicker.setMinDate(calendar.getTimeInMillis());
121 
122         int maxYear = timeDetectorHelper.getManualDateSelectionYearMax();
123         calendar.clear();
124         calendar.set(maxYear, Calendar.DECEMBER, 31);
125         datePicker.setMaxDate(calendar.getTimeInMillis());
126         return dialog;
127     }
128 
129     @VisibleForTesting
setDate(int year, int month, int day)130     void setDate(int year, int month, int day) {
131         Calendar c = Calendar.getInstance();
132 
133         c.set(Calendar.YEAR, year);
134         c.set(Calendar.MONTH, month);
135         c.set(Calendar.DAY_OF_MONTH, day);
136         long when = c.getTimeInMillis();
137 
138         TimeDetector timeDetector = mContext.getSystemService(TimeDetector.class);
139         ManualTimeSuggestion manualTimeSuggestion =
140                 TimeDetector.createManualTimeSuggestion(when, "Settings: Set date");
141         boolean success = timeDetector.suggestManualTime(manualTimeSuggestion);
142         if (!success) {
143             // This implies the system server is applying tighter bounds than the settings app or
144             // the date/time cannot be set for other reasons, e.g. perhaps "auto time" is turned on.
145             Log.w(TAG, "Unable to set date with suggestion=" + manualTimeSuggestion);
146         }
147     }
148 
149     /**
150      * Returns whether selecting the preference should prompt for the user to enter the date
151      * manually. Exposed as public so that the time controller can easily share the same logic as
152      * the rules are identical for time.
153      */
isEnabled()154     public boolean isEnabled() {
155         TimeCapabilities timeZoneCapabilities =
156                 mTimeManager.getTimeCapabilitiesAndConfig().getCapabilities();
157         int suggestManualTimeCapability = timeZoneCapabilities.getSetManualTimeCapability();
158         return suggestManualTimeCapability == CAPABILITY_POSSESSED;
159     }
160 }
161