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 | |
| 19 | import android.app.ActivityManager; |
| 20 | import android.app.AppOpsManager; |
| 21 | import android.app.StatusBarManager; |
| 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.location.LocationManager; |
| 28 | import android.os.Handler; |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 29 | import android.os.Looper; |
| 30 | import android.os.Message; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 31 | import android.os.UserHandle; |
| 32 | import android.os.UserManager; |
| 33 | import android.provider.Settings; |
| 34 | |
| 35 | import com.android.systemui.R; |
| 36 | |
| 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 { |
| 44 | // The name of the placeholder corresponding to the location request status icon. |
| 45 | // This string corresponds to config_statusBarIcons in core/res/res/values/config.xml. |
Jorim Jaggi | 5172dc2 | 2014-09-02 14:59:06 +0200 | [diff] [blame] | 46 | public static final int LOCATION_STATUS_ICON_ID = R.drawable.stat_sys_location; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 47 | |
| 48 | private static final int[] mHighPowerRequestAppOpArray |
| 49 | = new int[] {AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION}; |
| 50 | |
| 51 | private Context mContext; |
| 52 | |
| 53 | private AppOpsManager mAppOpsManager; |
| 54 | private StatusBarManager mStatusBarManager; |
| 55 | |
| 56 | private boolean mAreActiveLocationRequests; |
| 57 | |
| 58 | private ArrayList<LocationSettingsChangeCallback> mSettingsChangeCallbacks = |
| 59 | new ArrayList<LocationSettingsChangeCallback>(); |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 60 | private final H mHandler = new H(); |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 61 | public final String mSlotLocation; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 62 | |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 63 | public LocationControllerImpl(Context context, Looper bgLooper) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 64 | mContext = context; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 65 | mSlotLocation = mContext.getString(com.android.internal.R.string.status_bar_location); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 66 | |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 67 | // Register to listen for changes in location settings. |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 68 | IntentFilter filter = new IntentFilter(); |
| 69 | filter.addAction(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION); |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 70 | filter.addAction(LocationManager.MODE_CHANGED_ACTION); |
| 71 | context.registerReceiverAsUser(this, UserHandle.ALL, filter, null, new Handler(bgLooper)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 72 | |
| 73 | mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); |
| 74 | mStatusBarManager |
| 75 | = (StatusBarManager) context.getSystemService(Context.STATUS_BAR_SERVICE); |
| 76 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 77 | // Examine the current location state and initialize the status view. |
| 78 | updateActiveLocationRequests(); |
| 79 | refreshViews(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Add a callback to listen for changes in location settings. |
| 84 | */ |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 85 | public void addCallback(LocationSettingsChangeCallback cb) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 86 | mSettingsChangeCallbacks.add(cb); |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 87 | mHandler.sendEmptyMessage(H.MSG_LOCATION_SETTINGS_CHANGED); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 88 | } |
| 89 | |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 90 | public void removeCallback(LocationSettingsChangeCallback cb) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 91 | mSettingsChangeCallbacks.remove(cb); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Enable or disable location in settings. |
| 96 | * |
| 97 | * <p>This will attempt to enable/disable every type of location setting |
| 98 | * (e.g. high and balanced power). |
| 99 | * |
| 100 | * <p>If enabling, a user consent dialog will pop up prompting the user to accept. |
| 101 | * If the user doesn't accept, network location won't be enabled. |
| 102 | * |
| 103 | * @return true if attempt to change setting was successful. |
| 104 | */ |
| 105 | public boolean setLocationEnabled(boolean enabled) { |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 106 | int currentUserId = ActivityManager.getCurrentUser(); |
| 107 | if (isUserLocationRestricted(currentUserId)) { |
| 108 | return false; |
| 109 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 110 | final ContentResolver cr = mContext.getContentResolver(); |
| 111 | // When enabling location, a user consent dialog will pop up, and the |
| 112 | // setting won't be fully enabled until the user accepts the agreement. |
| 113 | int mode = enabled |
Lifu Tang | d1fa1d6 | 2015-11-25 21:42:44 -0800 | [diff] [blame] | 114 | ? Settings.Secure.LOCATION_MODE_PREVIOUS : Settings.Secure.LOCATION_MODE_OFF; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 115 | // QuickSettings always runs as the owner, so specifically set the settings |
| 116 | // for the current foreground user. |
| 117 | return Settings.Secure |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 118 | .putIntForUser(cr, Settings.Secure.LOCATION_MODE, mode, currentUserId); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns true if location isn't disabled in settings. |
| 123 | */ |
| 124 | public boolean isLocationEnabled() { |
| 125 | ContentResolver resolver = mContext.getContentResolver(); |
| 126 | // QuickSettings always runs as the owner, so specifically retrieve the settings |
| 127 | // for the current foreground user. |
| 128 | int mode = Settings.Secure.getIntForUser(resolver, Settings.Secure.LOCATION_MODE, |
| 129 | Settings.Secure.LOCATION_MODE_OFF, ActivityManager.getCurrentUser()); |
| 130 | return mode != Settings.Secure.LOCATION_MODE_OFF; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Returns true if the current user is restricted from using location. |
| 135 | */ |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 136 | private boolean isUserLocationRestricted(int userId) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 137 | final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Sudheer Shanka | b6fc931 | 2016-01-27 19:59:03 +0000 | [diff] [blame] | 138 | return um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 139 | UserHandle.of(userId)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns true if there currently exist active high power location requests. |
| 144 | */ |
| 145 | private boolean areActiveHighPowerLocationRequests() { |
| 146 | List<AppOpsManager.PackageOps> packages |
| 147 | = mAppOpsManager.getPackagesForOps(mHighPowerRequestAppOpArray); |
| 148 | // AppOpsManager can return null when there is no requested data. |
| 149 | if (packages != null) { |
| 150 | final int numPackages = packages.size(); |
| 151 | for (int packageInd = 0; packageInd < numPackages; packageInd++) { |
| 152 | AppOpsManager.PackageOps packageOp = packages.get(packageInd); |
| 153 | List<AppOpsManager.OpEntry> opEntries = packageOp.getOps(); |
| 154 | if (opEntries != null) { |
| 155 | final int numOps = opEntries.size(); |
| 156 | for (int opInd = 0; opInd < numOps; opInd++) { |
| 157 | AppOpsManager.OpEntry opEntry = opEntries.get(opInd); |
| 158 | // AppOpsManager should only return OP_MONITOR_HIGH_POWER_LOCATION because |
| 159 | // of the mHighPowerRequestAppOpArray filter, but checking defensively. |
| 160 | if (opEntry.getOp() == AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION) { |
| 161 | if (opEntry.isRunning()) { |
| 162 | return true; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | // Updates the status view based on the current state of location requests. |
| 174 | private void refreshViews() { |
| 175 | if (mAreActiveLocationRequests) { |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 176 | mStatusBarManager.setIcon(mSlotLocation, LOCATION_STATUS_ICON_ID, |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 177 | 0, mContext.getString(R.string.accessibility_location_active)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 178 | } else { |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 179 | mStatusBarManager.removeIcon(mSlotLocation); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | // Reads the active location requests and updates the status view if necessary. |
| 184 | private void updateActiveLocationRequests() { |
| 185 | boolean hadActiveLocationRequests = mAreActiveLocationRequests; |
| 186 | mAreActiveLocationRequests = areActiveHighPowerLocationRequests(); |
| 187 | if (mAreActiveLocationRequests != hadActiveLocationRequests) { |
| 188 | refreshViews(); |
| 189 | } |
| 190 | } |
| 191 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 192 | @Override |
| 193 | public void onReceive(Context context, Intent intent) { |
| 194 | final String action = intent.getAction(); |
| 195 | if (LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION.equals(action)) { |
| 196 | updateActiveLocationRequests(); |
Jason Monk | 05b86bc | 2015-05-19 14:21:28 -0400 | [diff] [blame] | 197 | } else if (LocationManager.MODE_CHANGED_ACTION.equals(action)) { |
| 198 | mHandler.sendEmptyMessage(H.MSG_LOCATION_SETTINGS_CHANGED); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | private final class H extends Handler { |
| 203 | private static final int MSG_LOCATION_SETTINGS_CHANGED = 1; |
| 204 | |
| 205 | @Override |
| 206 | public void handleMessage(Message msg) { |
| 207 | switch (msg.what) { |
| 208 | case MSG_LOCATION_SETTINGS_CHANGED: |
| 209 | locationSettingsChanged(); |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | private void locationSettingsChanged() { |
| 215 | boolean isEnabled = isLocationEnabled(); |
| 216 | for (LocationSettingsChangeCallback cb : mSettingsChangeCallbacks) { |
| 217 | cb.onLocationSettingsChanged(isEnabled); |
| 218 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | } |