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.os.UserManager.DISALLOW_CONFIG_LOCATION; 20 import static android.os.UserManager.DISALLOW_SHARE_LOCATION; 21 22 import static com.android.car.settings.enterprise.ActionDisabledByAdminDialogFragment.DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG; 23 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByDpm; 24 25 import android.car.drivingstate.CarUxRestrictions; 26 import android.content.Context; 27 import android.os.UserHandle; 28 import android.provider.Settings; 29 import android.widget.Toast; 30 31 import com.android.car.settings.R; 32 import com.android.car.settings.common.FragmentController; 33 import com.android.car.settings.common.Logger; 34 import com.android.car.settings.enterprise.EnterpriseUtils; 35 import com.android.car.ui.preference.CarUiSwitchPreference; 36 import com.android.settingslib.Utils; 37 38 /** 39 * Enables/disables location state via SwitchPreference. 40 */ 41 public class LocationStateSwitchPreferenceController extends 42 LocationStateListenerBasePreferenceController<CarUiSwitchPreference> { 43 private static final Logger LOG = new Logger( 44 LocationStateSwitchPreferenceController.class); 45 LocationStateSwitchPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)46 public LocationStateSwitchPreferenceController(Context context, 47 String preferenceKey, 48 FragmentController fragmentController, 49 CarUxRestrictions uxRestrictions) { 50 super(context, preferenceKey, fragmentController, uxRestrictions); 51 } 52 53 @Override getDefaultAvailabilityStatus()54 protected int getDefaultAvailabilityStatus() { 55 if (hasUserRestrictionByDpm(getContext(), DISALLOW_CONFIG_LOCATION) 56 || hasUserRestrictionByDpm(getContext(), DISALLOW_SHARE_LOCATION) 57 || !getIsPowerPolicyOn()) { 58 return AVAILABLE_FOR_VIEWING; 59 } 60 return AVAILABLE; 61 } 62 63 @Override getPreferenceType()64 protected Class<CarUiSwitchPreference> getPreferenceType() { 65 return CarUiSwitchPreference.class; 66 } 67 68 @Override updateState(CarUiSwitchPreference preference)69 protected void updateState(CarUiSwitchPreference preference) { 70 preference.setChecked(getLocationManager().isLocationEnabled() 71 && !hasUserRestrictionByDpm(getContext(), DISALLOW_SHARE_LOCATION)); 72 } 73 74 @Override handlePreferenceChanged(CarUiSwitchPreference preference, Object newValue)75 protected boolean handlePreferenceChanged(CarUiSwitchPreference preference, Object newValue) { 76 boolean isLocationEnabled = (Boolean) newValue; 77 Utils.updateLocationEnabled( 78 getContext(), 79 isLocationEnabled, 80 UserHandle.myUserId(), 81 Settings.Secure.LOCATION_CHANGER_SYSTEM_SETTINGS); 82 return true; 83 } 84 85 @Override onCreateInternal()86 protected void onCreateInternal() { 87 addDefaultMainLocationStateListener(); 88 addDefaultPowerPolicyListener(); 89 90 setClickableWhileDisabled(getPreference(), /* clickable= */ true, p -> { 91 // All the cases here should coincide with the ones in getAvailabilityStatus() 92 if (!getIsPowerPolicyOn()) { 93 Toast.makeText(getContext(), R.string.power_component_disabled, Toast.LENGTH_LONG) 94 .show(); 95 return; 96 } 97 if (hasUserRestrictionByDpm(getContext(), DISALLOW_SHARE_LOCATION)) { 98 showActionDisabledByAdminDialog(DISALLOW_SHARE_LOCATION); 99 return; 100 } 101 if (hasUserRestrictionByDpm(getContext(), DISALLOW_CONFIG_LOCATION)) { 102 showActionDisabledByAdminDialog(DISALLOW_CONFIG_LOCATION); 103 return; 104 } 105 }); 106 } 107 showActionDisabledByAdminDialog(String restrictionType)108 private void showActionDisabledByAdminDialog(String restrictionType) { 109 getFragmentController().showDialog( 110 EnterpriseUtils.getActionDisabledByAdminDialog(getContext(), 111 restrictionType), 112 DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG); 113 } 114 } 115