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.SwitchBar;
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 
27 public class LocationSwitchBarController implements SwitchBar.OnSwitchChangeListener,
28         LocationEnabler.LocationModeChangeListener, LifecycleObserver, OnStart, OnStop {
29 
30     private final SwitchBar mSwitchBar;
31     private final Switch mSwitch;
32     private final LocationEnabler mLocationEnabler;
33     private boolean mValidListener;
34 
LocationSwitchBarController(Context context, SwitchBar switchBar, Lifecycle lifecycle)35     public LocationSwitchBarController(Context context, SwitchBar switchBar,
36             Lifecycle lifecycle) {
37         mSwitchBar = switchBar;
38         mSwitch = mSwitchBar.getSwitch();
39         mLocationEnabler = new LocationEnabler(context, this /* listener */, lifecycle);
40         if (lifecycle != null) {
41             lifecycle.addObserver(this);
42         }
43     }
44 
45     @Override
onStart()46     public void onStart() {
47         if (!mValidListener) {
48             mSwitchBar.addOnSwitchChangeListener(this);
49             mValidListener = true;
50         }
51     }
52 
53     @Override
onStop()54     public void onStop() {
55         if (mValidListener) {
56             mSwitchBar.removeOnSwitchChangeListener(this);
57             mValidListener = false;
58         }
59     }
60 
61     @Override
onLocationModeChanged(int mode, boolean restricted)62     public void onLocationModeChanged(int mode, boolean restricted) {
63         // Restricted user can't change the location mode, so disable the master switch. But in some
64         // corner cases, the location might still be enabled. In such case the master switch should
65         // be disabled but checked.
66         final boolean enabled = mLocationEnabler.isEnabled(mode);
67         final int userId = UserHandle.myUserId();
68         final RestrictedLockUtils.EnforcedAdmin admin =
69                 mLocationEnabler.getShareLocationEnforcedAdmin(userId);
70         final boolean hasBaseUserRestriction =
71                 mLocationEnabler.hasShareLocationRestriction(userId);
72         // Disable the whole switch bar instead of the switch itself. If we disabled the switch
73         // only, it would be re-enabled again if the switch bar is not disabled.
74         if (!hasBaseUserRestriction && admin != null) {
75             mSwitchBar.setDisabledByAdmin(admin);
76         } else {
77             mSwitchBar.setEnabled(!restricted);
78         }
79 
80         if (enabled != mSwitch.isChecked()) {
81             // set listener to null so that that code below doesn't trigger onCheckedChanged()
82             if (mValidListener) {
83                 mSwitchBar.removeOnSwitchChangeListener(this);
84             }
85             mSwitch.setChecked(enabled);
86             if (mValidListener) {
87                 mSwitchBar.addOnSwitchChangeListener(this);
88             }
89         }
90     }
91 
92     /**
93      * Listens to the state change of the location master switch.
94      */
95     @Override
onSwitchChanged(Switch switchView, boolean isChecked)96     public void onSwitchChanged(Switch switchView, boolean isChecked) {
97         mLocationEnabler.setLocationEnabled(isChecked);
98     }
99 }
100