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.car.developeroptions; 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 import android.provider.SearchIndexableResource; 25 26 import com.android.car.developeroptions.dashboard.DashboardFragment; 27 import com.android.car.developeroptions.dashboard.SummaryLoader; 28 import com.android.car.developeroptions.datetime.AutoTimeFormatPreferenceController; 29 import com.android.car.developeroptions.datetime.AutoTimePreferenceController; 30 import com.android.car.developeroptions.datetime.AutoTimeZonePreferenceController; 31 import com.android.car.developeroptions.datetime.DatePreferenceController; 32 import com.android.car.developeroptions.datetime.TimeChangeListenerMixin; 33 import com.android.car.developeroptions.datetime.TimeFormatPreferenceController; 34 import com.android.car.developeroptions.datetime.TimePreferenceController; 35 import com.android.car.developeroptions.datetime.TimeZonePreferenceController; 36 import com.android.car.developeroptions.search.BaseSearchIndexProvider; 37 import com.android.settingslib.core.AbstractPreferenceController; 38 import com.android.settingslib.datetime.ZoneGetter; 39 import com.android.settingslib.search.Indexable; 40 import com.android.settingslib.search.SearchIndexable; 41 42 import java.util.ArrayList; 43 import java.util.Calendar; 44 import java.util.List; 45 46 @SearchIndexable 47 public class DateTimeSettings extends DashboardFragment implements 48 TimePreferenceController.TimePreferenceHost, DatePreferenceController.DatePreferenceHost { 49 50 private static final String TAG = "DateTimeSettings"; 51 52 // have we been launched from the setup wizard? 53 protected static final String EXTRA_IS_FROM_SUW = "firstRun"; 54 55 @Override getMetricsCategory()56 public int getMetricsCategory() { 57 return SettingsEnums.DATE_TIME; 58 } 59 60 @Override getLogTag()61 protected String getLogTag() { 62 return TAG; 63 } 64 65 @Override getPreferenceScreenResId()66 protected int getPreferenceScreenResId() { 67 return R.xml.date_time_prefs; 68 } 69 70 @Override onAttach(Context context)71 public void onAttach(Context context) { 72 super.onAttach(context); 73 getSettingsLifecycle().addObserver(new TimeChangeListenerMixin(context, this)); 74 } 75 76 @Override createPreferenceControllers(Context context)77 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 78 final List<AbstractPreferenceController> controllers = new ArrayList<>(); 79 final Activity activity = getActivity(); 80 final Intent intent = activity.getIntent(); 81 final boolean isFromSUW = intent.getBooleanExtra(EXTRA_IS_FROM_SUW, false); 82 83 final AutoTimeZonePreferenceController autoTimeZonePreferenceController = 84 new AutoTimeZonePreferenceController( 85 activity, this /* UpdateTimeAndDateCallback */, isFromSUW); 86 final AutoTimePreferenceController autoTimePreferenceController = 87 new AutoTimePreferenceController( 88 activity, this /* UpdateTimeAndDateCallback */); 89 final AutoTimeFormatPreferenceController autoTimeFormatPreferenceController = 90 new AutoTimeFormatPreferenceController( 91 activity, this /* UpdateTimeAndDateCallback */); 92 controllers.add(autoTimeZonePreferenceController); 93 controllers.add(autoTimePreferenceController); 94 controllers.add(autoTimeFormatPreferenceController); 95 96 controllers.add(new TimeFormatPreferenceController( 97 activity, this /* UpdateTimeAndDateCallback */, isFromSUW)); 98 controllers.add(new TimeZonePreferenceController( 99 activity, autoTimeZonePreferenceController)); 100 controllers.add(new TimePreferenceController( 101 activity, this /* UpdateTimeAndDateCallback */, autoTimePreferenceController)); 102 controllers.add(new DatePreferenceController( 103 activity, this /* UpdateTimeAndDateCallback */, autoTimePreferenceController)); 104 return controllers; 105 } 106 107 @Override updateTimeAndDateDisplay(Context context)108 public void updateTimeAndDateDisplay(Context context) { 109 updatePreferenceStates(); 110 } 111 112 @Override onCreateDialog(int id)113 public Dialog onCreateDialog(int id) { 114 switch (id) { 115 case DatePreferenceController.DIALOG_DATEPICKER: 116 return use(DatePreferenceController.class) 117 .buildDatePicker(getActivity()); 118 case TimePreferenceController.DIALOG_TIMEPICKER: 119 return use(TimePreferenceController.class) 120 .buildTimePicker(getActivity()); 121 default: 122 throw new IllegalArgumentException(); 123 } 124 } 125 126 @Override getDialogMetricsCategory(int dialogId)127 public int getDialogMetricsCategory(int dialogId) { 128 switch (dialogId) { 129 case DatePreferenceController.DIALOG_DATEPICKER: 130 return SettingsEnums.DIALOG_DATE_PICKER; 131 case TimePreferenceController.DIALOG_TIMEPICKER: 132 return SettingsEnums.DIALOG_TIME_PICKER; 133 default: 134 return 0; 135 } 136 } 137 138 @Override showTimePicker()139 public void showTimePicker() { 140 removeDialog(TimePreferenceController.DIALOG_TIMEPICKER); 141 showDialog(TimePreferenceController.DIALOG_TIMEPICKER); 142 } 143 144 @Override showDatePicker()145 public void showDatePicker() { 146 showDialog(DatePreferenceController.DIALOG_DATEPICKER); 147 } 148 149 private static class SummaryProvider implements SummaryLoader.SummaryProvider { 150 151 private final Context mContext; 152 private final SummaryLoader mSummaryLoader; 153 SummaryProvider(Context context, SummaryLoader summaryLoader)154 public SummaryProvider(Context context, SummaryLoader summaryLoader) { 155 mContext = context; 156 mSummaryLoader = summaryLoader; 157 } 158 159 @Override setListening(boolean listening)160 public void setListening(boolean listening) { 161 if (listening) { 162 final Calendar now = Calendar.getInstance(); 163 mSummaryLoader.setSummary(this, ZoneGetter.getTimeZoneOffsetAndName(mContext, 164 now.getTimeZone(), now.getTime())); 165 } 166 } 167 } 168 169 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY 170 = new SummaryLoader.SummaryProviderFactory() { 171 @Override 172 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity, 173 SummaryLoader summaryLoader) { 174 return new SummaryProvider(activity, summaryLoader); 175 } 176 }; 177 178 179 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 180 new DateTimeSearchIndexProvider(); 181 182 private static class DateTimeSearchIndexProvider extends BaseSearchIndexProvider { 183 184 @Override getXmlResourcesToIndex( Context context, boolean enabled)185 public List<SearchIndexableResource> getXmlResourcesToIndex( 186 Context context, boolean enabled) { 187 List<SearchIndexableResource> result = new ArrayList<>(); 188 189 SearchIndexableResource sir = new SearchIndexableResource(context); 190 sir.xmlResId = R.xml.date_time_prefs; 191 result.add(sir); 192 193 return result; 194 } 195 } 196 } 197