1 /* 2 * Copyright (C) 2021 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.privacy; 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.location.LocationManager; 25 import android.os.UserHandle; 26 import android.provider.Settings; 27 28 import com.android.car.settings.common.FragmentController; 29 import com.android.car.settings.common.PreferenceController; 30 import com.android.car.ui.preference.CarUiTwoActionSwitchPreference; 31 import com.android.internal.annotations.VisibleForTesting; 32 import com.android.settingslib.Utils; 33 34 /** Handles a {@link CarUiTwoActionSwitchPreference} which shows the current status of Location. */ 35 public class LocationTogglePreferenceController 36 extends PreferenceController<CarUiTwoActionSwitchPreference> { 37 38 @VisibleForTesting 39 static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED = 40 new IntentFilter(LocationManager.MODE_CHANGED_ACTION); 41 42 private final Context mContext; 43 private final LocationManager mLocationManager; 44 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 45 @Override 46 public void onReceive(Context context, Intent intent) { 47 refreshUi(); 48 } 49 }; 50 LocationTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)51 public LocationTogglePreferenceController(Context context, String preferenceKey, 52 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 53 super(context, preferenceKey, fragmentController, uxRestrictions); 54 mContext = context; 55 mLocationManager = context.getSystemService(LocationManager.class); 56 } 57 58 @Override getPreferenceType()59 protected Class<CarUiTwoActionSwitchPreference> getPreferenceType() { 60 return CarUiTwoActionSwitchPreference.class; 61 } 62 63 @Override onCreateInternal()64 protected void onCreateInternal() { 65 getPreference().setOnSecondaryActionClickListener(locationEnabled -> { 66 Utils.updateLocationEnabled( 67 mContext, 68 locationEnabled, 69 UserHandle.myUserId(), 70 Settings.Secure.LOCATION_CHANGER_SYSTEM_SETTINGS); 71 }); 72 } 73 74 @Override onStartInternal()75 protected void onStartInternal() { 76 mContext.registerReceiver(mReceiver, INTENT_FILTER_LOCATION_MODE_CHANGED); 77 } 78 79 @Override onStopInternal()80 protected void onStopInternal() { 81 mContext.unregisterReceiver(mReceiver); 82 } 83 84 @Override updateState(CarUiTwoActionSwitchPreference preference)85 protected void updateState(CarUiTwoActionSwitchPreference preference) { 86 preference.setSecondaryActionChecked(mLocationManager.isLocationEnabled()); 87 } 88 } 89