1 /* 2 * Copyright (C) 2018 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.settings.datetime; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.provider.Settings; 25 26 import androidx.preference.Preference; 27 28 import com.android.car.settings.common.FragmentController; 29 import com.android.car.settings.common.PreferenceController; 30 import com.android.settingslib.datetime.ZoneGetter; 31 32 import java.util.Calendar; 33 34 /** 35 * Business logic for the preference that allows for changing the timezone. 36 */ 37 public class TimeZonePickerPreferenceController extends PreferenceController<Preference> { 38 39 private final IntentFilter mIntentFilter; 40 private final BroadcastReceiver mTimeChangeReceiver = new BroadcastReceiver() { 41 @Override 42 public void onReceive(Context context, Intent intent) { 43 refreshUi(); 44 } 45 }; 46 TimeZonePickerPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)47 public TimeZonePickerPreferenceController(Context context, String preferenceKey, 48 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 49 super(context, preferenceKey, fragmentController, uxRestrictions); 50 51 // Listen to ACTION_TIME_CHANGED because it could be sent by the autoTimeZone toggle. 52 // Listen to ACTION_TIMEZONE_CHANGED because the change in timezone should update the 53 // description of the preference. 54 mIntentFilter = new IntentFilter(); 55 mIntentFilter.addAction(Intent.ACTION_TIME_CHANGED); 56 mIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 57 } 58 59 @Override getPreferenceType()60 protected Class<Preference> getPreferenceType() { 61 return Preference.class; 62 } 63 64 /** Starts the broadcast receiver which listens for time changes */ 65 @Override onStartInternal()66 protected void onStartInternal() { 67 getContext().registerReceiver(mTimeChangeReceiver, mIntentFilter); 68 } 69 70 /** Stops the broadcast receiver which listens for time changes */ 71 @Override onStopInternal()72 protected void onStopInternal() { 73 getContext().unregisterReceiver(mTimeChangeReceiver); 74 } 75 76 @Override updateState(Preference preference)77 protected void updateState(Preference preference) { 78 Calendar now = Calendar.getInstance(); 79 preference.setSummary(ZoneGetter.getTimeZoneOffsetAndName(getContext(), now.getTimeZone(), 80 now.getTime())); 81 preference.setEnabled(!autoTimezoneIsEnabled()); 82 } 83 autoTimezoneIsEnabled()84 private boolean autoTimezoneIsEnabled() { 85 return Settings.Global.getInt(getContext().getContentResolver(), 86 Settings.Global.AUTO_TIME_ZONE, 0) > 0; 87 } 88 } 89