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