• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 
17 package com.android.settings.privacy;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 
22 import androidx.preference.PreferenceScreen;
23 
24 import com.android.settings.core.TogglePreferenceController;
25 import com.android.settings.location.LocationEnabler;
26 import com.android.settingslib.RestrictedLockUtils;
27 import com.android.settingslib.RestrictedSwitchPreference;
28 import com.android.settingslib.core.lifecycle.Lifecycle;
29 
30 /**
31  * Controller for location toggle
32  */
33 public class LocationToggleController extends TogglePreferenceController
34         implements LocationEnabler.LocationModeChangeListener {
35 
36     private final LocationEnabler mLocationEnabler;
37     private RestrictedSwitchPreference mPreference;
38 
39     private boolean mIsLocationEnabled = true;
40 
LocationToggleController(Context context, String preferenceKey, Lifecycle lifecycle)41     public LocationToggleController(Context context, String preferenceKey, Lifecycle lifecycle) {
42         super(context, preferenceKey);
43         mLocationEnabler = new LocationEnabler(context, this, lifecycle);
44         mLocationEnabler.refreshLocationMode();
45     }
46     @Override
onLocationModeChanged(int mode, boolean restricted)47     public void onLocationModeChanged(int mode, boolean restricted) {
48         if (mPreference == null) {
49             return;
50         }
51 
52         mIsLocationEnabled = mLocationEnabler.isEnabled(mode);
53         final int userId = UserHandle.myUserId();
54         final RestrictedLockUtils.EnforcedAdmin admin =
55                 mLocationEnabler.getShareLocationEnforcedAdmin(userId);
56         final boolean hasBaseUserRestriction =
57                 mLocationEnabler.hasShareLocationRestriction(userId);
58         // Disable the whole switch bar instead of the switch itself. If we disabled the switch
59         // only, it would be re-enabled again if the switch bar is not disabled.
60         if (!hasBaseUserRestriction && admin != null) {
61             mPreference.setDisabledByAdmin(admin);
62         } else {
63             mPreference.setEnabled(!restricted);
64         }
65         updateState(mPreference);
66     }
67 
68     @Override
getAvailabilityStatus()69     public int getAvailabilityStatus() {
70         return AVAILABLE;
71     }
72 
73     @Override
isChecked()74     public boolean isChecked() {
75         return mIsLocationEnabled;
76     }
77 
78     @Override
setChecked(boolean isChecked)79     public boolean setChecked(boolean isChecked) {
80         mLocationEnabler.setLocationEnabled(isChecked);
81         return true;
82     }
83 
84     @Override
displayPreference(PreferenceScreen screen)85     public void displayPreference(PreferenceScreen screen) {
86         super.displayPreference(screen);
87         mPreference = screen.findPreference(getPreferenceKey());
88         mLocationEnabler.refreshLocationMode();
89     }
90 
91     @Override
getSliceHighlightMenuRes()92     public int getSliceHighlightMenuRes() {
93         return 0;
94     }
95 }
96