LocationControllerImpl.java revision aa080f96763a9d38f21fb33de93216cd207a5930
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 static com.android.settingslib.Utils.updateLocationEnabled; 20 21import android.app.ActivityManager; 22import android.app.AppOpsManager; 23import android.app.StatusBarManager; 24import android.content.BroadcastReceiver; 25import android.content.Context; 26import android.content.Intent; 27import android.content.IntentFilter; 28import android.location.LocationManager; 29import android.os.Handler; 30import android.os.Looper; 31import android.os.Message; 32import android.os.Process; 33import android.os.UserHandle; 34import android.os.UserManager; 35import android.support.annotation.VisibleForTesting; 36import com.android.systemui.util.Utils; 37import java.util.ArrayList; 38import java.util.List; 39 40/** 41 * A controller to manage changes of location related states and update the views accordingly. 42 */ 43public class LocationControllerImpl extends BroadcastReceiver implements LocationController { 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 55 private ArrayList<LocationChangeCallback> mSettingsChangeCallbacks = 56 new ArrayList<LocationChangeCallback>(); 57 private final H mHandler = new H(); 58 59 public LocationControllerImpl(Context context, Looper bgLooper) { 60 mContext = context; 61 62 // Register to listen for changes in location settings. 63 IntentFilter filter = new IntentFilter(); 64 filter.addAction(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION); 65 filter.addAction(LocationManager.MODE_CHANGED_ACTION); 66 context.registerReceiverAsUser(this, UserHandle.ALL, filter, null, new Handler(bgLooper)); 67 68 mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); 69 mStatusBarManager 70 = (StatusBarManager) context.getSystemService(Context.STATUS_BAR_SERVICE); 71 72 // Examine the current location state and initialize the status view. 73 updateActiveLocationRequests(); 74 } 75 76 /** 77 * Add a callback to listen for changes in location settings. 78 */ 79 public void addCallback(LocationChangeCallback cb) { 80 mSettingsChangeCallbacks.add(cb); 81 mHandler.sendEmptyMessage(H.MSG_LOCATION_SETTINGS_CHANGED); 82 } 83 84 public void removeCallback(LocationChangeCallback cb) { 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) { 100 // QuickSettings always runs as the owner, so specifically set the settings 101 // for the current foreground user. 102 int currentUserId = ActivityManager.getCurrentUser(); 103 if (isUserLocationRestricted(currentUserId)) { 104 return false; 105 } 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. 108 updateLocationEnabled(mContext, enabled, currentUserId); 109 return true; 110 } 111 112 /** 113 * Returns true if location is enabled in settings. 114 */ 115 public boolean isLocationEnabled() { 116 // QuickSettings always runs as the owner, so specifically retrieve the settings 117 // for the current foreground user. 118 LocationManager locationManager = 119 (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); 120 return locationManager.isLocationEnabledForUser(Process.myUserHandle()); 121 } 122 123 @Override 124 public boolean isLocationActive() { 125 return mAreActiveLocationRequests; 126 } 127 128 /** 129 * Returns true if the current user is restricted from using location. 130 */ 131 private boolean isUserLocationRestricted(int userId) { 132 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); 133 return um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, 134 UserHandle.of(userId)); 135 } 136 137 /** 138 * Returns true if there currently exist active high power location requests. 139 */ 140 @VisibleForTesting 141 protected boolean areActiveHighPowerLocationRequests() { 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 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) { 174 mHandler.sendEmptyMessage(H.MSG_LOCATION_ACTIVE_CHANGED); 175 } 176 } 177 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(); 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; 190 private static final int MSG_LOCATION_ACTIVE_CHANGED = 2; 191 192 @Override 193 public void handleMessage(Message msg) { 194 switch (msg.what) { 195 case MSG_LOCATION_SETTINGS_CHANGED: 196 locationSettingsChanged(); 197 break; 198 case MSG_LOCATION_ACTIVE_CHANGED: 199 locationActiveChanged(); 200 break; 201 } 202 } 203 204 private void locationActiveChanged() { 205 Utils.safeForeach(mSettingsChangeCallbacks, 206 cb -> cb.onLocationActiveChanged(mAreActiveLocationRequests)); 207 } 208 209 private void locationSettingsChanged() { 210 boolean isEnabled = isLocationEnabled(); 211 Utils.safeForeach(mSettingsChangeCallbacks, 212 cb -> cb.onLocationSettingsChanged(isEnabled)); 213 } 214 } 215} 216