1 /* 2 * Copyright (C) 2017 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 package com.android.settings.location; 17 18 import android.content.Context; 19 import android.os.UserManager; 20 21 import androidx.preference.Preference; 22 import androidx.preference.PreferenceScreen; 23 24 import com.android.settings.R; 25 import com.android.settings.Utils; 26 import com.android.settingslib.RestrictedLockUtils; 27 import com.android.settingslib.RestrictedSwitchPreference; 28 import com.android.settingslib.core.lifecycle.Lifecycle; 29 30 public class LocationForWorkPreferenceController extends LocationBasePreferenceController { 31 32 /** 33 * Key for managed profile location switch preference. Shown only 34 * if there is a managed profile. 35 */ 36 private static final String KEY_MANAGED_PROFILE_SWITCH = "managed_profile_location_switch"; 37 38 private RestrictedSwitchPreference mPreference; 39 LocationForWorkPreferenceController(Context context, Lifecycle lifecycle)40 public LocationForWorkPreferenceController(Context context, Lifecycle lifecycle) { 41 super(context, lifecycle); 42 } 43 44 @Override handlePreferenceTreeClick(Preference preference)45 public boolean handlePreferenceTreeClick(Preference preference) { 46 if (KEY_MANAGED_PROFILE_SWITCH.equals(preference.getKey())) { 47 final boolean switchState = mPreference.isChecked(); 48 mUserManager.setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, !switchState, 49 Utils.getManagedProfile(mUserManager)); 50 mPreference.setSummary(switchState ? 51 R.string.switch_on_text : R.string.switch_off_text); 52 return true; 53 } 54 return false; 55 } 56 57 @Override displayPreference(PreferenceScreen screen)58 public void displayPreference(PreferenceScreen screen) { 59 super.displayPreference(screen); 60 mPreference = screen.findPreference(KEY_MANAGED_PROFILE_SWITCH); 61 } 62 63 @Override isAvailable()64 public boolean isAvailable() { 65 // Looking for a managed profile. If there are no managed profiles then we are removing the 66 // managed profile category. 67 return Utils.getManagedProfile(mUserManager) != null; 68 } 69 70 @Override getPreferenceKey()71 public String getPreferenceKey() { 72 return KEY_MANAGED_PROFILE_SWITCH; 73 } 74 75 @Override onLocationModeChanged(int mode, boolean restricted)76 public void onLocationModeChanged(int mode, boolean restricted) { 77 if (!mPreference.isVisible() || !isAvailable()) { 78 return; 79 } 80 final RestrictedLockUtils.EnforcedAdmin admin = 81 mLocationEnabler.getShareLocationEnforcedAdmin( 82 Utils.getManagedProfile(mUserManager).getIdentifier()); 83 final boolean isRestrictedByBase = mLocationEnabler.isManagedProfileRestrictedByBase(); 84 if (!isRestrictedByBase && admin != null) { 85 mPreference.setDisabledByAdmin(admin); 86 mPreference.setChecked(false); 87 } else { 88 final boolean enabled = mLocationEnabler.isEnabled(mode); 89 mPreference.setEnabled(enabled); 90 91 int summaryResId = R.string.switch_off_text; 92 if (!enabled) { 93 mPreference.setChecked(false); 94 } else { 95 mPreference.setChecked(!isRestrictedByBase); 96 summaryResId = (isRestrictedByBase ? 97 R.string.switch_off_text : R.string.switch_on_text); 98 } 99 mPreference.setSummary(summaryResId); 100 } 101 } 102 } 103 104