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