John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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.systemui.statusbar.policy; |
| 18 | |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 19 | import static com.android.settingslib.Utils.updateLocationEnabled; |
| 20 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 21 | import android.app.ActivityManager; |
| 22 | import android.app.AppOpsManager; |
| 23 | import android.app.StatusBarManager; |
| 24 | import android.content.BroadcastReceiver; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
| 28 | import android.location.LocationManager; |
| 29 | import android.os.Handler; |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 30 | import android.os.Looper; |
| 31 | import android.os.Message; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 32 | import android.os.UserHandle; |
| 33 | import android.os.UserManager; |
Lifu Tang | 0cba58f | 2018-01-23 21:14:15 -0800 | [diff] [blame] | 34 | import android.provider.Settings; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 35 | |
Aurimas Liutikas | fd52c14 | 2018-04-17 09:50:46 -0700 | [diff] [blame] | 36 | import androidx.annotation.VisibleForTesting; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 37 | |
Dave Mankoff | f473681 | 2019-10-18 17:25:50 -0400 | [diff] [blame] | 38 | import com.android.systemui.dagger.qualifiers.BgLooper; |
Jason Monk | b46a3c9 | 2017-06-22 09:19:54 -0400 | [diff] [blame] | 39 | import com.android.systemui.util.Utils; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 40 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 41 | import java.util.ArrayList; |
| 42 | import java.util.List; |
| 43 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 44 | import javax.inject.Inject; |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 45 | import javax.inject.Singleton; |
| 46 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 47 | /** |
| 48 | * A controller to manage changes of location related states and update the views accordingly. |
| 49 | */ |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 50 | @Singleton |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 51 | public class LocationControllerImpl extends BroadcastReceiver implements LocationController { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 52 | |
| 53 | private static final int[] mHighPowerRequestAppOpArray |
| 54 | = new int[] {AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION}; |
| 55 | |
| 56 | private Context mContext; |
| 57 | |
| 58 | private AppOpsManager mAppOpsManager; |
| 59 | private StatusBarManager mStatusBarManager; |
| 60 | |
| 61 | private boolean mAreActiveLocationRequests; |
| 62 | |
Jason Monk | 359bb74 | 2017-04-13 10:40:40 -0400 | [diff] [blame] | 63 | private ArrayList<LocationChangeCallback> mSettingsChangeCallbacks = |
| 64 | new ArrayList<LocationChangeCallback>(); |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 65 | private final H mHandler = new H(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 66 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 67 | @Inject |
Dave Mankoff | f473681 | 2019-10-18 17:25:50 -0400 | [diff] [blame] | 68 | public LocationControllerImpl(Context context, @BgLooper Looper bgLooper) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 69 | mContext = context; |
| 70 | |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 71 | // Register to listen for changes in location settings. |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 72 | IntentFilter filter = new IntentFilter(); |
| 73 | filter.addAction(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION); |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 74 | filter.addAction(LocationManager.MODE_CHANGED_ACTION); |
| 75 | context.registerReceiverAsUser(this, UserHandle.ALL, filter, null, new Handler(bgLooper)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 76 | |
| 77 | mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); |
| 78 | mStatusBarManager |
| 79 | = (StatusBarManager) context.getSystemService(Context.STATUS_BAR_SERVICE); |
| 80 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 81 | // Examine the current location state and initialize the status view. |
| 82 | updateActiveLocationRequests(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Add a callback to listen for changes in location settings. |
| 87 | */ |
Jason Monk | 359bb74 | 2017-04-13 10:40:40 -0400 | [diff] [blame] | 88 | public void addCallback(LocationChangeCallback cb) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 89 | mSettingsChangeCallbacks.add(cb); |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 90 | mHandler.sendEmptyMessage(H.MSG_LOCATION_SETTINGS_CHANGED); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Jason Monk | 359bb74 | 2017-04-13 10:40:40 -0400 | [diff] [blame] | 93 | public void removeCallback(LocationChangeCallback cb) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 94 | mSettingsChangeCallbacks.remove(cb); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Enable or disable location in settings. |
| 99 | * |
| 100 | * <p>This will attempt to enable/disable every type of location setting |
| 101 | * (e.g. high and balanced power). |
| 102 | * |
| 103 | * <p>If enabling, a user consent dialog will pop up prompting the user to accept. |
| 104 | * If the user doesn't accept, network location won't be enabled. |
| 105 | * |
| 106 | * @return true if attempt to change setting was successful. |
| 107 | */ |
| 108 | public boolean setLocationEnabled(boolean enabled) { |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 109 | // QuickSettings always runs as the owner, so specifically set the settings |
| 110 | // for the current foreground user. |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 111 | int currentUserId = ActivityManager.getCurrentUser(); |
| 112 | if (isUserLocationRestricted(currentUserId)) { |
| 113 | return false; |
| 114 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 115 | // When enabling location, a user consent dialog will pop up, and the |
| 116 | // setting won't be fully enabled until the user accepts the agreement. |
Lifu Tang | 0cba58f | 2018-01-23 21:14:15 -0800 | [diff] [blame] | 117 | updateLocationEnabled(mContext, enabled, currentUserId, |
| 118 | Settings.Secure.LOCATION_CHANGER_QUICK_SETTINGS); |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 119 | return true; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 123 | * Returns true if location is enabled in settings. |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 124 | */ |
| 125 | public boolean isLocationEnabled() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 126 | // QuickSettings always runs as the owner, so specifically retrieve the settings |
| 127 | // for the current foreground user. |
Maggie | aa080f9 | 2018-01-04 15:35:11 -0800 | [diff] [blame] | 128 | LocationManager locationManager = |
| 129 | (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
Maggie | 91e630c | 2018-01-24 17:31:46 -0800 | [diff] [blame] | 130 | return locationManager.isLocationEnabledForUser( |
| 131 | UserHandle.of(ActivityManager.getCurrentUser())); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 132 | } |
| 133 | |
Jason Monk | 359bb74 | 2017-04-13 10:40:40 -0400 | [diff] [blame] | 134 | @Override |
| 135 | public boolean isLocationActive() { |
| 136 | return mAreActiveLocationRequests; |
| 137 | } |
| 138 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 139 | /** |
| 140 | * Returns true if the current user is restricted from using location. |
| 141 | */ |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 142 | private boolean isUserLocationRestricted(int userId) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 143 | final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 144 | return um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 145 | UserHandle.of(userId)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns true if there currently exist active high power location requests. |
| 150 | */ |
Jason Monk | b46a3c9 | 2017-06-22 09:19:54 -0400 | [diff] [blame] | 151 | @VisibleForTesting |
| 152 | protected boolean areActiveHighPowerLocationRequests() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 153 | List<AppOpsManager.PackageOps> packages |
| 154 | = mAppOpsManager.getPackagesForOps(mHighPowerRequestAppOpArray); |
| 155 | // AppOpsManager can return null when there is no requested data. |
| 156 | if (packages != null) { |
| 157 | final int numPackages = packages.size(); |
| 158 | for (int packageInd = 0; packageInd < numPackages; packageInd++) { |
| 159 | AppOpsManager.PackageOps packageOp = packages.get(packageInd); |
| 160 | List<AppOpsManager.OpEntry> opEntries = packageOp.getOps(); |
| 161 | if (opEntries != null) { |
| 162 | final int numOps = opEntries.size(); |
| 163 | for (int opInd = 0; opInd < numOps; opInd++) { |
| 164 | AppOpsManager.OpEntry opEntry = opEntries.get(opInd); |
| 165 | // AppOpsManager should only return OP_MONITOR_HIGH_POWER_LOCATION because |
| 166 | // of the mHighPowerRequestAppOpArray filter, but checking defensively. |
| 167 | if (opEntry.getOp() == AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION) { |
| 168 | if (opEntry.isRunning()) { |
| 169 | return true; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return false; |
| 178 | } |
| 179 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 180 | // Reads the active location requests and updates the status view if necessary. |
| 181 | private void updateActiveLocationRequests() { |
| 182 | boolean hadActiveLocationRequests = mAreActiveLocationRequests; |
| 183 | mAreActiveLocationRequests = areActiveHighPowerLocationRequests(); |
| 184 | if (mAreActiveLocationRequests != hadActiveLocationRequests) { |
Jason Monk | 359bb74 | 2017-04-13 10:40:40 -0400 | [diff] [blame] | 185 | mHandler.sendEmptyMessage(H.MSG_LOCATION_ACTIVE_CHANGED); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 189 | @Override |
| 190 | public void onReceive(Context context, Intent intent) { |
| 191 | final String action = intent.getAction(); |
| 192 | if (LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION.equals(action)) { |
| 193 | updateActiveLocationRequests(); |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 194 | } else if (LocationManager.MODE_CHANGED_ACTION.equals(action)) { |
| 195 | mHandler.sendEmptyMessage(H.MSG_LOCATION_SETTINGS_CHANGED); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | private final class H extends Handler { |
| 200 | private static final int MSG_LOCATION_SETTINGS_CHANGED = 1; |
Jason Monk | 359bb74 | 2017-04-13 10:40:40 -0400 | [diff] [blame] | 201 | private static final int MSG_LOCATION_ACTIVE_CHANGED = 2; |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 202 | |
| 203 | @Override |
| 204 | public void handleMessage(Message msg) { |
| 205 | switch (msg.what) { |
| 206 | case MSG_LOCATION_SETTINGS_CHANGED: |
| 207 | locationSettingsChanged(); |
| 208 | break; |
Jason Monk | 359bb74 | 2017-04-13 10:40:40 -0400 | [diff] [blame] | 209 | case MSG_LOCATION_ACTIVE_CHANGED: |
| 210 | locationActiveChanged(); |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | private void locationActiveChanged() { |
Jason Monk | b46a3c9 | 2017-06-22 09:19:54 -0400 | [diff] [blame] | 216 | Utils.safeForeach(mSettingsChangeCallbacks, |
| 217 | cb -> cb.onLocationActiveChanged(mAreActiveLocationRequests)); |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | private void locationSettingsChanged() { |
| 221 | boolean isEnabled = isLocationEnabled(); |
Jason Monk | b46a3c9 | 2017-06-22 09:19:54 -0400 | [diff] [blame] | 222 | Utils.safeForeach(mSettingsChangeCallbacks, |
| 223 | cb -> cb.onLocationSettingsChanged(isEnabled)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | } |