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