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.text.TextUtils; 21 22 import androidx.preference.Preference; 23 import androidx.preference.PreferenceScreen; 24 25 import com.android.settings.R; 26 import com.android.settings.Utils; 27 import com.android.settingslib.RestrictedLockUtils; 28 import com.android.settingslib.RestrictedSwitchPreference; 29 30 public class LocationForWorkPreferenceController extends LocationBasePreferenceController { 31 32 private RestrictedSwitchPreference mPreference; 33 LocationForWorkPreferenceController(Context context, String key)34 public LocationForWorkPreferenceController(Context context, String key) { 35 super(context, key); 36 } 37 38 @Override handlePreferenceTreeClick(Preference preference)39 public boolean handlePreferenceTreeClick(Preference preference) { 40 if (TextUtils.equals(preference.getKey(), getPreferenceKey())) { 41 final boolean switchState = mPreference.isChecked(); 42 mUserManager.setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, !switchState, 43 Utils.getManagedProfile(mUserManager)); 44 mPreference.setSummary(switchState ? 45 R.string.switch_on_text : R.string.switch_off_text); 46 return true; 47 } 48 return false; 49 } 50 51 @Override displayPreference(PreferenceScreen screen)52 public void displayPreference(PreferenceScreen screen) { 53 super.displayPreference(screen); 54 mPreference = screen.findPreference(getPreferenceKey()); 55 } 56 57 @Override getAvailabilityStatus()58 public int getAvailabilityStatus() { 59 return AVAILABLE; 60 } 61 62 @Override onLocationModeChanged(int mode, boolean restricted)63 public void onLocationModeChanged(int mode, boolean restricted) { 64 if (!mPreference.isVisible() || !isAvailable()) { 65 return; 66 } 67 final RestrictedLockUtils.EnforcedAdmin admin = 68 mLocationEnabler.getShareLocationEnforcedAdmin( 69 Utils.getManagedProfile(mUserManager).getIdentifier()); 70 if (admin != null) { 71 mPreference.setDisabledByAdmin(admin); 72 } else { 73 final boolean enabled = mLocationEnabler.isEnabled(mode); 74 mPreference.setEnabled(enabled); 75 int summaryResId; 76 77 final boolean isRestrictedByBase = 78 mLocationEnabler.isManagedProfileRestrictedByBase(); 79 if (isRestrictedByBase || !enabled) { 80 mPreference.setChecked(false); 81 summaryResId = enabled ? R.string.switch_off_text 82 : R.string.location_app_permission_summary_location_off; 83 } else { 84 mPreference.setChecked(true); 85 summaryResId = R.string.switch_on_text; 86 } 87 mPreference.setSummary(summaryResId); 88 } 89 } 90 } 91 92