1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.settings.location;
15 
16 import android.content.Context;
17 import android.os.UserHandle;
18 import android.widget.Switch;
19 
20 import com.android.settings.widget.SettingsMainSwitchBar;
21 import com.android.settingslib.RestrictedLockUtils;
22 import com.android.settingslib.core.lifecycle.Lifecycle;
23 import com.android.settingslib.core.lifecycle.LifecycleObserver;
24 import com.android.settingslib.core.lifecycle.events.OnStart;
25 import com.android.settingslib.core.lifecycle.events.OnStop;
26 import com.android.settingslib.widget.OnMainSwitchChangeListener;
27 
28 /**
29  * The switch controller for the location.
30  */
31 public class LocationSwitchBarController implements OnMainSwitchChangeListener,
32         LocationEnabler.LocationModeChangeListener, LifecycleObserver, OnStart, OnStop {
33 
34     private final SettingsMainSwitchBar mSwitchBar;
35     private final LocationEnabler mLocationEnabler;
36     private boolean mValidListener;
37 
LocationSwitchBarController(Context context, SettingsMainSwitchBar switchBar, Lifecycle lifecycle)38     public LocationSwitchBarController(Context context, SettingsMainSwitchBar switchBar,
39             Lifecycle lifecycle) {
40         mSwitchBar = switchBar;
41         mLocationEnabler = new LocationEnabler(context, this /* listener */, lifecycle);
42         if (lifecycle != null) {
43             lifecycle.addObserver(this);
44         }
45     }
46 
47     @Override
onStart()48     public void onStart() {
49         if (!mValidListener) {
50             mSwitchBar.addOnSwitchChangeListener(this);
51             mValidListener = true;
52         }
53     }
54 
55     @Override
onStop()56     public void onStop() {
57         if (mValidListener) {
58             mSwitchBar.removeOnSwitchChangeListener(this);
59             mValidListener = false;
60         }
61     }
62 
63     @Override
onLocationModeChanged(int mode, boolean restricted)64     public void onLocationModeChanged(int mode, boolean restricted) {
65         // Restricted user can't change the location mode, so disable the primary switch. But in
66         // some corner cases, the location might still be enabled. In such case the primary switch
67         // should be disabled but checked.
68         final boolean enabled = mLocationEnabler.isEnabled(mode);
69         final int userId = UserHandle.myUserId();
70         final RestrictedLockUtils.EnforcedAdmin admin =
71                 mLocationEnabler.getShareLocationEnforcedAdmin(userId);
72         final boolean hasBaseUserRestriction =
73                 mLocationEnabler.hasShareLocationRestriction(userId);
74         // Disable the whole switch bar instead of the switch itself. If we disabled the switch
75         // only, it would be re-enabled again if the switch bar is not disabled.
76         if (!hasBaseUserRestriction && admin != null) {
77             mSwitchBar.setDisabledByAdmin(admin);
78         } else {
79             mSwitchBar.setEnabled(!restricted);
80         }
81 
82         if (enabled != mSwitchBar.isChecked()) {
83             // set listener to null so that that code below doesn't trigger onCheckedChanged()
84             if (mValidListener) {
85                 mSwitchBar.removeOnSwitchChangeListener(this);
86             }
87             mSwitchBar.setChecked(enabled);
88             if (mValidListener) {
89                 mSwitchBar.addOnSwitchChangeListener(this);
90             }
91         }
92     }
93 
94     /**
95      * Listens to the state change of the location primary switch.
96      */
97     @Override
onSwitchChanged(Switch switchView, boolean isChecked)98     public void onSwitchChanged(Switch switchView, boolean isChecked) {
99         mLocationEnabler.setLocationEnabled(isChecked);
100     }
101 }
102