1 /* 2 * Copyright (C) 2024 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.app.time.TimeConfiguration; 20 import android.app.time.TimeManager; 21 import android.app.time.TimeZoneConfiguration; 22 import android.car.drivingstate.CarUxRestrictions; 23 import android.content.BroadcastReceiver; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.IntentFilter; 27 import android.location.LocationManager; 28 29 import androidx.preference.SwitchPreference; 30 31 import com.android.car.settings.R; 32 import com.android.car.settings.common.ConfirmationDialogFragment; 33 import com.android.car.settings.common.FragmentController; 34 import com.android.car.settings.common.PreferenceController; 35 import com.android.car.settings.location.LocationAccessFragment; 36 37 /** 38 * Business logic which controls the auto local time toggle. 39 */ 40 public class AutoLocalTimeTogglePreferenceController extends 41 PreferenceController<SwitchPreference> { 42 private final TimeManager mTimeManager; 43 private final LocationManager mLocationManager; 44 private final BroadcastReceiver mLocationReceiver = new BroadcastReceiver() { 45 @Override 46 public void onReceive(Context context, Intent intent) { 47 refreshUi(); 48 } 49 }; 50 AutoLocalTimeTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)51 public AutoLocalTimeTogglePreferenceController(Context context, String preferenceKey, 52 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 53 super(context, preferenceKey, fragmentController, uxRestrictions); 54 mTimeManager = context.getSystemService(TimeManager.class); 55 mLocationManager = context.getSystemService(LocationManager.class); 56 } 57 58 @Override getPreferenceType()59 protected Class<SwitchPreference> getPreferenceType() { 60 return SwitchPreference.class; 61 } 62 63 @Override onCreateInternal()64 protected void onCreateInternal() { 65 setClickableWhileDisabled(getPreference(), /* clickable= */ true, p -> 66 DatetimeUtils.runClickableWhileDisabled(getContext(), getFragmentController())); 67 } 68 69 @Override onStartInternal()70 protected void onStartInternal() { 71 IntentFilter locationChangeFilter = new IntentFilter(); 72 locationChangeFilter.addAction(LocationManager.MODE_CHANGED_ACTION); 73 getContext().registerReceiver( 74 mLocationReceiver, locationChangeFilter, Context.RECEIVER_NOT_EXPORTED); 75 } 76 77 @Override onStopInternal()78 protected void onStopInternal() { 79 getContext().unregisterReceiver(mLocationReceiver); 80 } 81 82 @Override updateState(SwitchPreference preference)83 protected void updateState(SwitchPreference preference) { 84 boolean isEnabled = DatetimeUtils.isAutoLocalTimeDetectionEnabled(mTimeManager); 85 preference.setChecked(isEnabled); 86 if (isEnabled && !mLocationManager.isLocationEnabled()) { 87 preference.setSummary(R.string.auto_local_time_toggle_summary); 88 } else { 89 preference.setSummary(""); 90 } 91 } 92 93 @Override handlePreferenceChanged(SwitchPreference preference, Object newValue)94 protected boolean handlePreferenceChanged(SwitchPreference preference, Object newValue) { 95 if (!DatetimeUtils.isAutoTimeDetectionCapabilityPossessed(mTimeManager) 96 || !DatetimeUtils.isAutoTimeZoneDetectionCapabilityPossessed(mTimeManager)) { 97 return false; 98 } 99 100 boolean setAutoLocalTimeEnabled = (boolean) newValue; 101 updateTimeAndTimeZoneConfiguration(setAutoLocalTimeEnabled); 102 103 if (setAutoLocalTimeEnabled && !mLocationManager.isLocationEnabled()) { 104 preference.setSummary(R.string.auto_local_time_toggle_summary); 105 getFragmentController().showDialog(getConfirmationDialog(), 106 ConfirmationDialogFragment.TAG); 107 } else { 108 preference.setSummary(""); 109 } 110 return true; 111 } 112 113 @Override getDefaultAvailabilityStatus()114 public int getDefaultAvailabilityStatus() { 115 return DatetimeUtils.getAvailabilityStatus(getContext()); 116 } 117 updateTimeAndTimeZoneConfiguration(boolean setAutoDatetimeEnabled)118 private void updateTimeAndTimeZoneConfiguration(boolean setAutoDatetimeEnabled) { 119 mTimeManager.updateTimeConfiguration(new TimeConfiguration.Builder() 120 .setAutoDetectionEnabled(setAutoDatetimeEnabled).build()); 121 mTimeManager.updateTimeZoneConfiguration(new TimeZoneConfiguration.Builder() 122 .setAutoDetectionEnabled(setAutoDatetimeEnabled).build()); 123 getContext().sendBroadcast(new Intent(Intent.ACTION_TIME_CHANGED)); 124 } 125 getConfirmationDialog()126 private ConfirmationDialogFragment getConfirmationDialog() { 127 return new ConfirmationDialogFragment.Builder(getContext()) 128 .setMessage(R.string.auto_local_time_dialog_msg) 129 .setNegativeButton(R.string.auto_local_time_dialog_negative_button_text, 130 /* listener= */ null) 131 .setPositiveButton(R.string.auto_local_time_dialog_positive_button_text, 132 arguments -> { 133 getFragmentController().launchFragment(new LocationAccessFragment()); 134 }) 135 .build(); 136 } 137 } 138