blob: cc61605a5aa67917b385392b00d88fabf5c0e397 [file] [log] [blame]
John Spurlockaf8d6c42014-05-07 17:49:08 -04001/*
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;
Jason Monk05b86bc2015-05-19 14:21:28 -040029import android.os.Looper;
30import android.os.Message;
John Spurlockaf8d6c42014-05-07 17:49:08 -040031import android.os.UserHandle;
32import android.os.UserManager;
33import android.provider.Settings;
34
35import com.android.systemui.R;
36
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 // 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 Jaggi5172dc22014-09-02 14:59:06 +020046 public static final int LOCATION_STATUS_ICON_ID = R.drawable.stat_sys_location;
John Spurlockaf8d6c42014-05-07 17:49:08 -040047
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 Monk05b86bc2015-05-19 14:21:28 -040060 private final H mHandler = new H();
Jason Monk3e189872016-01-12 09:10:34 -050061 public final String mSlotLocation;
John Spurlockaf8d6c42014-05-07 17:49:08 -040062
Jason Monk05b86bc2015-05-19 14:21:28 -040063 public LocationControllerImpl(Context context, Looper bgLooper) {
John Spurlockaf8d6c42014-05-07 17:49:08 -040064 mContext = context;
Jason Monk3e189872016-01-12 09:10:34 -050065 mSlotLocation = mContext.getString(com.android.internal.R.string.status_bar_location);
John Spurlockaf8d6c42014-05-07 17:49:08 -040066
Jason Monk05b86bc2015-05-19 14:21:28 -040067 // Register to listen for changes in location settings.
John Spurlockaf8d6c42014-05-07 17:49:08 -040068 IntentFilter filter = new IntentFilter();
69 filter.addAction(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION);
Jason Monk05b86bc2015-05-19 14:21:28 -040070 filter.addAction(LocationManager.MODE_CHANGED_ACTION);
71 context.registerReceiverAsUser(this, UserHandle.ALL, filter, null, new Handler(bgLooper));
John Spurlockaf8d6c42014-05-07 17:49:08 -040072
73 mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
74 mStatusBarManager
75 = (StatusBarManager) context.getSystemService(Context.STATUS_BAR_SERVICE);
76
John Spurlockaf8d6c42014-05-07 17:49:08 -040077 // 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 Monk88529052016-11-04 13:29:58 -040085 public void addCallback(LocationSettingsChangeCallback cb) {
John Spurlockaf8d6c42014-05-07 17:49:08 -040086 mSettingsChangeCallbacks.add(cb);
Jason Monk05b86bc2015-05-19 14:21:28 -040087 mHandler.sendEmptyMessage(H.MSG_LOCATION_SETTINGS_CHANGED);
John Spurlockaf8d6c42014-05-07 17:49:08 -040088 }
89
Jason Monk88529052016-11-04 13:29:58 -040090 public void removeCallback(LocationSettingsChangeCallback cb) {
John Spurlockaf8d6c42014-05-07 17:49:08 -040091 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 Shankaa8fbbb32016-02-11 17:17:57 +0000106 int currentUserId = ActivityManager.getCurrentUser();
107 if (isUserLocationRestricted(currentUserId)) {
108 return false;
109 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400110 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 Tangd1fa1d62015-11-25 21:42:44 -0800114 ? Settings.Secure.LOCATION_MODE_PREVIOUS : Settings.Secure.LOCATION_MODE_OFF;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400115 // QuickSettings always runs as the owner, so specifically set the settings
116 // for the current foreground user.
117 return Settings.Secure
Sudheer Shankaa8fbbb32016-02-11 17:17:57 +0000118 .putIntForUser(cr, Settings.Secure.LOCATION_MODE, mode, currentUserId);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400119 }
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 Shankaa8fbbb32016-02-11 17:17:57 +0000136 private boolean isUserLocationRestricted(int userId) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400137 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Sudheer Shankab6fc9312016-01-27 19:59:03 +0000138 return um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION,
Sudheer Shankaa8fbbb32016-02-11 17:17:57 +0000139 UserHandle.of(userId));
John Spurlockaf8d6c42014-05-07 17:49:08 -0400140 }
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 Monk3e189872016-01-12 09:10:34 -0500176 mStatusBarManager.setIcon(mSlotLocation, LOCATION_STATUS_ICON_ID,
Jason Monk05b86bc2015-05-19 14:21:28 -0400177 0, mContext.getString(R.string.accessibility_location_active));
John Spurlockaf8d6c42014-05-07 17:49:08 -0400178 } else {
Jason Monk3e189872016-01-12 09:10:34 -0500179 mStatusBarManager.removeIcon(mSlotLocation);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400180 }
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 Spurlockaf8d6c42014-05-07 17:49:08 -0400192 @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 Monk05b86bc2015-05-19 14:21:28 -0400197 } 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 Spurlockaf8d6c42014-05-07 17:49:08 -0400219 }
220 }
221}