1 /*
2  * Copyright (C) 2019 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.Dialog;
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.content.Intent;
24 
25 import com.android.settings.R;
26 import com.android.settings.dashboard.DashboardFragment;
27 import com.android.settings.search.BaseSearchIndexProvider;
28 import com.android.settingslib.core.AbstractPreferenceController;
29 import com.android.settingslib.search.SearchIndexable;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 @SearchIndexable
35 public class DateTimeSettings extends DashboardFragment implements
36         TimePreferenceController.TimePreferenceHost, DatePreferenceController.DatePreferenceHost {
37 
38     private static final String TAG = "DateTimeSettings";
39 
40     // have we been launched from the setup wizard?
41     protected static final String EXTRA_IS_FROM_SUW = "firstRun";
42 
43     @Override
getMetricsCategory()44     public int getMetricsCategory() {
45         return SettingsEnums.DATE_TIME;
46     }
47 
48     @Override
getLogTag()49     protected String getLogTag() {
50         return TAG;
51     }
52 
53     @Override
getPreferenceScreenResId()54     protected int getPreferenceScreenResId() {
55         return R.xml.date_time_prefs;
56     }
57 
58     @Override
onAttach(Context context)59     public void onAttach(Context context) {
60         super.onAttach(context);
61         getSettingsLifecycle().addObserver(new TimeChangeListenerMixin(context, this));
62     }
63 
64     @Override
createPreferenceControllers(Context context)65     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
66         final List<AbstractPreferenceController> controllers = new ArrayList<>();
67         final Activity activity = getActivity();
68         final Intent intent = activity.getIntent();
69         final boolean isFromSUW = intent.getBooleanExtra(EXTRA_IS_FROM_SUW, false);
70 
71         final AutoTimeZonePreferenceController autoTimeZonePreferenceController =
72                 new AutoTimeZonePreferenceController(
73                         activity, this /* UpdateTimeAndDateCallback */, isFromSUW);
74         final AutoTimePreferenceController autoTimePreferenceController =
75                 new AutoTimePreferenceController(
76                         activity, this /* UpdateTimeAndDateCallback */);
77         final AutoTimeFormatPreferenceController autoTimeFormatPreferenceController =
78                 new AutoTimeFormatPreferenceController(
79                         activity, this /* UpdateTimeAndDateCallback */);
80         controllers.add(autoTimeZonePreferenceController);
81         controllers.add(autoTimePreferenceController);
82         controllers.add(autoTimeFormatPreferenceController);
83 
84         controllers.add(new TimeFormatPreferenceController(
85                 activity, this /* UpdateTimeAndDateCallback */, isFromSUW));
86         controllers.add(new TimeZonePreferenceController(
87                 activity, autoTimeZonePreferenceController));
88         controllers.add(new TimePreferenceController(
89                 activity, this /* UpdateTimeAndDateCallback */, autoTimePreferenceController));
90         controllers.add(new DatePreferenceController(
91                 activity, this /* UpdateTimeAndDateCallback */, autoTimePreferenceController));
92         return controllers;
93     }
94 
95     @Override
updateTimeAndDateDisplay(Context context)96     public void updateTimeAndDateDisplay(Context context) {
97         updatePreferenceStates();
98     }
99 
100     @Override
onCreateDialog(int id)101     public Dialog onCreateDialog(int id) {
102         switch (id) {
103             case DatePreferenceController.DIALOG_DATEPICKER:
104                 return use(DatePreferenceController.class)
105                         .buildDatePicker(getActivity());
106             case TimePreferenceController.DIALOG_TIMEPICKER:
107                 return use(TimePreferenceController.class)
108                         .buildTimePicker(getActivity());
109             default:
110                 throw new IllegalArgumentException();
111         }
112     }
113 
114     @Override
getDialogMetricsCategory(int dialogId)115     public int getDialogMetricsCategory(int dialogId) {
116         switch (dialogId) {
117             case DatePreferenceController.DIALOG_DATEPICKER:
118                 return SettingsEnums.DIALOG_DATE_PICKER;
119             case TimePreferenceController.DIALOG_TIMEPICKER:
120                 return SettingsEnums.DIALOG_TIME_PICKER;
121             default:
122                 return 0;
123         }
124     }
125 
126     @Override
showTimePicker()127     public void showTimePicker() {
128         removeDialog(TimePreferenceController.DIALOG_TIMEPICKER);
129         showDialog(TimePreferenceController.DIALOG_TIMEPICKER);
130     }
131 
132     @Override
showDatePicker()133     public void showDatePicker() {
134         showDialog(DatePreferenceController.DIALOG_DATEPICKER);
135     }
136 
137     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
138             new BaseSearchIndexProvider(R.xml.date_time_prefs);
139 }
140