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