Expose Location toggling logic

Specifically refactor the check for secondary user and the
method to toggle location mode using the previously set
location mode.

Test: robotests
Bug: 62022517
Change-Id: I519fad6d802efd8af2e77c623f698e19d4cdc800
This commit is contained in:
Matthew Fritze
2017-07-31 17:16:27 -07:00
committed by Andrew Sapperstein
parent 688e5438dd
commit 6593453d59
2 changed files with 25 additions and 11 deletions

View File

@@ -85,13 +85,23 @@ public abstract class LocationSettingsBase extends SettingsPreferenceFragment {
/** Called when location mode has changed. */ /** Called when location mode has changed. */
public abstract void onModeChanged(int mode, boolean restricted); public abstract void onModeChanged(int mode, boolean restricted);
private boolean isRestricted() { public static boolean isRestricted(Context context) {
final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE); final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
return um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION); return um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION);
} }
public static boolean updateLocationMode(Context context, int oldMode, int newMode) {
Intent intent = new Intent(MODE_CHANGING_ACTION);
intent.putExtra(CURRENT_MODE_KEY, oldMode);
intent.putExtra(NEW_MODE_KEY, newMode);
context.sendBroadcast(intent, android.Manifest.permission.WRITE_SECURE_SETTINGS);
return Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
newMode);
}
public void setLocationMode(int mode) { public void setLocationMode(int mode) {
if (isRestricted()) { Context context = getActivity();
if (isRestricted(context)) {
// Location toggling disabled by user restriction. Read the current location mode to // Location toggling disabled by user restriction. Read the current location mode to
// update the location master switch. // update the location master switch.
if (Log.isLoggable(TAG, Log.INFO)) { if (Log.isLoggable(TAG, Log.INFO)) {
@@ -104,11 +114,8 @@ public abstract class LocationSettingsBase extends SettingsPreferenceFragment {
} }
return; return;
} }
Intent intent = new Intent(MODE_CHANGING_ACTION);
intent.putExtra(CURRENT_MODE_KEY, mCurrentMode); updateLocationMode(context, mCurrentMode, mode);
intent.putExtra(NEW_MODE_KEY, mode);
getActivity().sendBroadcast(intent, android.Manifest.permission.WRITE_SECURE_SETTINGS);
Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, mode);
refreshLocationMode(); refreshLocationMode();
} }
@@ -120,7 +127,7 @@ public abstract class LocationSettingsBase extends SettingsPreferenceFragment {
if (Log.isLoggable(TAG, Log.INFO)) { if (Log.isLoggable(TAG, Log.INFO)) {
Log.i(TAG, "Location mode has been changed"); Log.i(TAG, "Location mode has been changed");
} }
onModeChanged(mode, isRestricted()); onModeChanged(mode, isRestricted(getActivity()));
} }
} }
} }

View File

@@ -70,7 +70,8 @@ public class ResultPayload implements Parcelable {
Availability.DISABLED_DEPENDENT_APP, Availability.DISABLED_DEPENDENT_APP,
Availability.DISABLED_UNSUPPORTED, Availability.DISABLED_UNSUPPORTED,
Availability.RESOURCE_CONTENTION, Availability.RESOURCE_CONTENTION,
Availability.INTENT_ONLY,}) Availability.INTENT_ONLY,
Availability.DISABLED_FOR_USER,})
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface Availability { public @interface Availability {
/** /**
@@ -96,7 +97,7 @@ public class ResultPayload implements Parcelable {
int RESOURCE_CONTENTION = 3; int RESOURCE_CONTENTION = 3;
/** /**
* The setting is disabled because corresponding app is disabled * The setting is disabled because corresponding app is disabled.
*/ */
int DISABLED_DEPENDENT_APP = 4; int DISABLED_DEPENDENT_APP = 4;
@@ -104,6 +105,12 @@ public class ResultPayload implements Parcelable {
* This setting is supported on the device but cannot be changed inline. * This setting is supported on the device but cannot be changed inline.
*/ */
int INTENT_ONLY = 5; int INTENT_ONLY = 5;
/**
* The setting cannot be changed by the current user.
* ex: MobileNetworkTakeMeThereSetting should not be available to a secondary user.
*/
int DISABLED_FOR_USER = 6;
} }
@IntDef({SettingsSource.UNKNOWN, SettingsSource.SYSTEM, SettingsSource.SECURE, @IntDef({SettingsSource.UNKNOWN, SettingsSource.SYSTEM, SettingsSource.SECURE,