1 /* 2 * Copyright (C) 2024 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.UserHandle; 20 import android.os.UserManager; 21 import android.text.TextUtils; 22 23 import androidx.annotation.NonNull; 24 import androidx.annotation.Nullable; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceScreen; 27 28 import com.android.settings.R; 29 import com.android.settings.Utils; 30 import com.android.settings.dashboard.profileselector.ProfileSelectFragment.ProfileType; 31 import com.android.settingslib.RestrictedLockUtils; 32 import com.android.settingslib.RestrictedSwitchPreference; 33 34 public class LocationForPrivateProfilePreferenceController 35 extends LocationBasePreferenceController { 36 @Nullable private RestrictedSwitchPreference mPreference; 37 @Nullable private final UserHandle mPrivateProfileHandle; LocationForPrivateProfilePreferenceController( @onNull Context context, @NonNull String key)38 public LocationForPrivateProfilePreferenceController( 39 @NonNull Context context, @NonNull String key) { 40 super(context, key); 41 mPrivateProfileHandle = Utils.getProfileOfType(mUserManager, ProfileType.PRIVATE); 42 } 43 44 @Override handlePreferenceTreeClick(@onNull Preference preference)45 public boolean handlePreferenceTreeClick(@NonNull Preference preference) { 46 if (TextUtils.equals(preference.getKey(), getPreferenceKey())) { 47 final boolean switchState = mPreference.isChecked(); 48 mUserManager.setUserRestriction( 49 UserManager.DISALLOW_SHARE_LOCATION, 50 !switchState, 51 mPrivateProfileHandle); 52 mPreference.setSummary(switchState 53 ? R.string.switch_on_text : R.string.switch_off_text); 54 return true; 55 } 56 return false; 57 } 58 59 @Override displayPreference(@onNull PreferenceScreen screen)60 public void displayPreference(@NonNull PreferenceScreen screen) { 61 super.displayPreference(screen); 62 mPreference = screen.findPreference(getPreferenceKey()); 63 if (mPreference != null) { 64 mPreference.setEnabled(isPrivateProfileAvailable()); 65 } 66 } 67 68 @Override getAvailabilityStatus()69 public int getAvailabilityStatus() { 70 if (!android.os.Flags.allowPrivateProfile() 71 || !android.multiuser.Flags.enablePrivateSpaceFeatures() 72 || !android.multiuser.Flags.handleInterleavedSettingsForPrivateSpace() 73 || !isPrivateProfileAvailable()) { 74 return CONDITIONALLY_UNAVAILABLE; 75 } 76 return AVAILABLE; 77 } 78 79 @Override onLocationModeChanged(int mode, boolean restricted)80 public void onLocationModeChanged(int mode, boolean restricted) { 81 if ((mPreference != null && !mPreference.isVisible()) 82 || !isAvailable() 83 || !isPrivateProfileAvailable()) { 84 return; 85 } 86 87 // The profile owner (which is the admin for the child profile) might have added a location 88 // sharing restriction. 89 final RestrictedLockUtils.EnforcedAdmin admin = 90 mLocationEnabler.getShareLocationEnforcedAdmin( 91 mPrivateProfileHandle.getIdentifier()); 92 if (admin != null) { 93 mPreference.setDisabledByAdmin(admin); 94 } else { 95 final boolean enabled = mLocationEnabler.isEnabled(mode); 96 mPreference.setEnabled(enabled); 97 int summaryResId; 98 99 final boolean isRestrictedByBase = 100 mLocationEnabler 101 .hasShareLocationRestriction(mPrivateProfileHandle.getIdentifier()); 102 if (isRestrictedByBase || !enabled) { 103 mPreference.setChecked(false); 104 summaryResId = enabled ? R.string.switch_off_text 105 : R.string.location_app_permission_summary_location_off; 106 } else { 107 mPreference.setChecked(true); 108 summaryResId = R.string.switch_on_text; 109 } 110 mPreference.setSummary(summaryResId); 111 } 112 } 113 isPrivateProfileAvailable()114 private boolean isPrivateProfileAvailable() { 115 return mPrivateProfileHandle != null 116 && !mUserManager.isQuietModeEnabled(mPrivateProfileHandle); 117 } 118 } 119