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 import android.support.v7.preference.Preference; 21 import android.support.v7.preference.PreferenceScreen; 22 23 import com.android.settings.R; 24 import com.android.settings.Utils; 25 import com.android.settingslib.RestrictedLockUtils; 26 import com.android.settingslib.RestrictedSwitchPreference; 27 import com.android.settingslib.core.lifecycle.Lifecycle; 28 29 public class LocationForWorkPreferenceController extends LocationBasePreferenceController { 30 31 /** 32 * Key for managed profile location switch preference. Shown only 33 * if there is a managed profile. 34 */ 35 private static final String KEY_MANAGED_PROFILE_SWITCH = "managed_profile_location_switch"; 36 37 private RestrictedSwitchPreference mPreference; 38 LocationForWorkPreferenceController(Context context, Lifecycle lifecycle)39 public LocationForWorkPreferenceController(Context context, Lifecycle lifecycle) { 40 super(context, lifecycle); 41 } 42 43 @Override handlePreferenceTreeClick(Preference preference)44 public boolean handlePreferenceTreeClick(Preference preference) { 45 if (KEY_MANAGED_PROFILE_SWITCH.equals(preference.getKey())) { 46 final boolean switchState = mPreference.isChecked(); 47 mUserManager.setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, !switchState, 48 Utils.getManagedProfile(mUserManager)); 49 mPreference.setSummary(switchState ? 50 R.string.switch_on_text : R.string.switch_off_text); 51 return true; 52 } 53 return false; 54 } 55 56 @Override displayPreference(PreferenceScreen screen)57 public void displayPreference(PreferenceScreen screen) { 58 super.displayPreference(screen); 59 mPreference = 60 (RestrictedSwitchPreference) 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