1 /* 2 * Copyright (C) 2020 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.location; 18 19 import static android.car.hardware.power.PowerComponent.LOCATION; 20 21 import android.car.drivingstate.CarUxRestrictions; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.location.LocationManager; 27 import android.os.UserHandle; 28 import android.provider.Settings; 29 import android.widget.Toast; 30 31 import androidx.annotation.VisibleForTesting; 32 33 import com.android.car.settings.R; 34 import com.android.car.settings.common.ClickableWhileDisabledSwitchPreference; 35 import com.android.car.settings.common.FragmentController; 36 import com.android.car.settings.common.PowerPolicyListener; 37 import com.android.car.settings.common.PreferenceController; 38 import com.android.settingslib.Utils; 39 40 /** 41 * Enables/disables location state via SwitchPreference. 42 */ 43 public class LocationStateSwitchPreferenceController extends 44 PreferenceController<ClickableWhileDisabledSwitchPreference> { 45 46 private static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED = 47 new IntentFilter(LocationManager.MODE_CHANGED_ACTION); 48 49 private final Context mContext; 50 private final LocationManager mLocationManager; 51 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 52 @Override 53 public void onReceive(Context context, Intent intent) { 54 refreshUi(); 55 } 56 }; 57 58 @VisibleForTesting 59 final PowerPolicyListener mPowerPolicyListener; 60 LocationStateSwitchPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)61 public LocationStateSwitchPreferenceController(Context context, 62 String preferenceKey, 63 FragmentController fragmentController, 64 CarUxRestrictions uxRestrictions) { 65 super(context, preferenceKey, fragmentController, uxRestrictions); 66 mContext = context; 67 mLocationManager = context.getSystemService(LocationManager.class); 68 mPowerPolicyListener = new PowerPolicyListener(context, LOCATION, 69 isOn -> { 70 enableSwitchPreference(getPreference(), isOn); 71 }); 72 } 73 74 @Override getPreferenceType()75 protected Class<ClickableWhileDisabledSwitchPreference> getPreferenceType() { 76 return ClickableWhileDisabledSwitchPreference.class; 77 } 78 79 @Override updateState(ClickableWhileDisabledSwitchPreference preference)80 protected void updateState(ClickableWhileDisabledSwitchPreference preference) { 81 updateSwitchPreference(preference, mLocationManager.isLocationEnabled()); 82 } 83 84 @Override handlePreferenceChanged(ClickableWhileDisabledSwitchPreference preference, Object newValue)85 protected boolean handlePreferenceChanged(ClickableWhileDisabledSwitchPreference preference, 86 Object newValue) { 87 boolean locationEnabled = (Boolean) newValue; 88 Utils.updateLocationEnabled( 89 mContext, 90 locationEnabled, 91 UserHandle.myUserId(), 92 Settings.Secure.LOCATION_CHANGER_SYSTEM_SETTINGS); 93 return true; 94 } 95 96 @Override onCreateInternal()97 protected void onCreateInternal() { 98 getPreference().setContentDescription( 99 getContext().getString(R.string.location_state_switch_content_description)); 100 getPreference().setDisabledClickListener(p -> 101 Toast.makeText(getContext(), 102 getContext().getString(R.string.power_component_disabled), 103 Toast.LENGTH_LONG).show()); 104 } 105 106 @Override onStartInternal()107 protected void onStartInternal() { 108 mContext.registerReceiver(mReceiver, INTENT_FILTER_LOCATION_MODE_CHANGED); 109 } 110 111 @Override onResumeInternal()112 protected void onResumeInternal() { 113 mPowerPolicyListener.handleCurrentPolicy(); 114 } 115 116 @Override onStopInternal()117 protected void onStopInternal() { 118 mContext.unregisterReceiver(mReceiver); 119 } 120 121 @Override onDestroyInternal()122 protected void onDestroyInternal() { 123 mPowerPolicyListener.release(); 124 } 125 updateSwitchPreference(ClickableWhileDisabledSwitchPreference preference, boolean enabled)126 private void updateSwitchPreference(ClickableWhileDisabledSwitchPreference preference, 127 boolean enabled) { 128 preference.setTitle(enabled ? R.string.car_ui_preference_switch_on 129 : R.string.car_ui_preference_switch_off); 130 preference.setChecked(enabled); 131 } 132 enableSwitchPreference(ClickableWhileDisabledSwitchPreference preference, boolean enabled)133 private void enableSwitchPreference(ClickableWhileDisabledSwitchPreference preference, 134 boolean enabled) { 135 preference.setEnabled(enabled); 136 } 137 } 138