am 61edc94a: Merge "Make location QuickSettings multi-user compatible (b/10563313)" into klp-dev

* commit '61edc94ab58354381ab381cd9b92faf0a337742b':
  Make location QuickSettings multi-user compatible (b/10563313)
This commit is contained in:
David Christie
2013-09-04 12:25:37 -07:00
committed by Android Git Automerger
3 changed files with 62 additions and 24 deletions

View File

@@ -357,7 +357,18 @@ public class UserManager {
* @param restrictionKey the string key representing the restriction
*/
public boolean hasUserRestriction(String restrictionKey) {
return getUserRestrictions().getBoolean(restrictionKey, false);
return hasUserRestriction(restrictionKey, Process.myUserHandle());
}
/**
* @hide
* Returns whether the given user has been disallowed from performing certain actions
* or setting certain settings.
* @param restrictionKey the string key representing the restriction
* @param userHandle the UserHandle of the user for whom to retrieve the restrictions.
*/
public boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
return getUserRestrictions(userHandle).getBoolean(restrictionKey, false);
}
/**

View File

@@ -640,10 +640,10 @@ class QuickSettings {
@Override
public boolean onLongClick(View v) {
boolean newLocationEnabledState = !mLocationController.isLocationEnabled();
mLocationController.setLocationEnabled(newLocationEnabledState);
if (newLocationEnabledState) {
// Close the notifications tray so that the network location provider
// consent dialog can be shown.
if (mLocationController.setLocationEnabled(newLocationEnabledState)
&& newLocationEnabledState) {
// If we've successfully switched from location off to on, close the
// notifications tray to show the network location provider consent dialog.
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
mContext.sendBroadcast(closeDialog);
}

View File

@@ -16,6 +16,7 @@
package com.android.systemui.statusbar.policy;
import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.app.StatusBarManager;
import android.content.BroadcastReceiver;
@@ -26,6 +27,7 @@ import android.content.IntentFilter;
import android.database.ContentObserver;
import android.location.LocationManager;
import android.os.Handler;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
@@ -81,18 +83,18 @@ public class LocationController extends BroadcastReceiver {
mStatusBarManager
= (StatusBarManager) context.getSystemService(Context.STATUS_BAR_SERVICE);
// Register to listen for changes to the location settings
context.getContentResolver().registerContentObserver(
Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true,
new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
boolean isEnabled = isLocationEnabled();
for (LocationSettingsChangeCallback cb : mSettingsChangeCallbacks) {
cb.onLocationSettingsChanged(isEnabled);
}
}
});
// Register to listen for changes in location settings.
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(LocationManager.MODE_CHANGED_ACTION);
context.registerReceiverAsUser(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (LocationManager.MODE_CHANGED_ACTION.equals(action)) {
locationSettingsChanged();
}
}
}, UserHandle.ALL, intentFilter, null, new Handler());
// Examine the current location state and initialize the status view.
updateActiveLocationRequests();
@@ -114,30 +116,48 @@ public class LocationController extends BroadcastReceiver {
*
* <p>If enabling, a user consent dialog will pop up prompting the user to accept.
* If the user doesn't accept, network location won't be enabled.
*
* @return true if attempt to change setting was successful.
*/
public void setLocationEnabled(boolean enabled) {
final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
if (um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
return;
public boolean setLocationEnabled(boolean enabled) {
int currentUserId = ActivityManager.getCurrentUser();
if (isUserLocationRestricted(currentUserId)) {
return false;
}
final ContentResolver cr = mContext.getContentResolver();
// When enabling location, a user consent dialog will pop up, and the
// setting won't be fully enabled until the user accepts the agreement.
int mode = enabled
? Settings.Secure.LOCATION_MODE_HIGH_ACCURACY : Settings.Secure.LOCATION_MODE_OFF;
Settings.Secure.putInt(cr, Settings.Secure.LOCATION_MODE, mode);
return Settings.Secure
.putIntForUser(cr, Settings.Secure.LOCATION_MODE, mode, currentUserId);
}
/**
* Returns true if location isn't disabled in settings.
*/
public boolean isLocationEnabled() {
int currentUserId = ActivityManager.getCurrentUser();
if (isUserLocationRestricted(currentUserId)) {
return false;
}
ContentResolver resolver = mContext.getContentResolver();
int mode = Settings.Secure.getInt(resolver, Settings.Secure.LOCATION_MODE,
Settings.Secure.LOCATION_MODE_OFF);
int mode = Settings.Secure.getIntForUser(resolver, Settings.Secure.LOCATION_MODE,
Settings.Secure.LOCATION_MODE_OFF, currentUserId);
return mode != Settings.Secure.LOCATION_MODE_OFF;
}
/**
* Returns true if the current user is restricted from using location.
*/
private boolean isUserLocationRestricted(int userId) {
final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
return um.hasUserRestriction(
UserManager.DISALLOW_SHARE_LOCATION,
new UserHandle(userId));
}
/**
* Returns true if there currently exist active high power location requests.
*/
@@ -188,6 +208,13 @@ public class LocationController extends BroadcastReceiver {
}
}
private void locationSettingsChanged() {
boolean isEnabled = isLocationEnabled();
for (LocationSettingsChangeCallback cb : mSettingsChangeCallbacks) {
cb.onLocationSettingsChanged(isEnabled);
}
}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();