Skip to content

Commit 773a0b6

Browse files
committed
Added commands to set / get user location.
Test: adb shell dumpsys activity service --user 0 com.afwsamples.testdpc is-location-enabled Test: adb shell dumpsys activity service --user 0 com.afwsamples.testdpc set-location-enabled false Bug: 171350084 Bug: 185523465 Change-Id: I98d770a3fdeb2f9ab0d75cd0f34e0dee090e4ae4
1 parent 7d94be9 commit 773a0b6

File tree

4 files changed

+71
-12
lines changed

4 files changed

+71
-12
lines changed

app/src/main/java/com/afwsamples/testdpc/DevicePolicyManagerGateway.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,17 @@ void setPermissionGrantState(String packageName, String permission, int grantSta
402402
*/
403403
int getPermissionGrantState(String packageName, String permission);
404404

405+
/**
406+
* See {@link android.app.admin.DevicePolicyManager#setLocationEnabled(ComponentName, boolean)}.
407+
*/
408+
void setLocationEnabled(boolean enabled, @NonNull Consumer<Void> onSuccess,
409+
@NonNull Consumer<Exception> onError);
410+
411+
/**
412+
* See {@link android.location.LocationManager#isLocationEnabled()}.
413+
*/
414+
boolean isLocationEnabled();
415+
405416
/**
406417
* Used on error callbacks to indicate a {@link android.app.admin.DevicePolicyManager} method
407418
* call failed.

app/src/main/java/com/afwsamples/testdpc/DevicePolicyManagerGatewayImpl.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.content.pm.PackageManager;
2525
import android.content.pm.PackageManager.NameNotFoundException;
2626
import android.graphics.Bitmap;
27+
import android.location.LocationManager;
2728
import android.os.Build;
2829
import android.os.Bundle;
2930
import android.os.PersistableBundle;
@@ -54,19 +55,22 @@ public final class DevicePolicyManagerGatewayImpl implements DevicePolicyManager
5455
private final UserManager mUserManager;
5556
private final ComponentName mAdminComponentName;
5657
private final PackageManager mPackageManager;
58+
private final LocationManager mLocationManager;
5759

5860
public DevicePolicyManagerGatewayImpl(@NonNull Context context) {
5961
this(context.getSystemService(DevicePolicyManager.class),
6062
context.getSystemService(UserManager.class),
6163
context.getPackageManager(),
64+
context.getSystemService(LocationManager.class),
6265
DeviceAdminReceiver.getComponentName(context));
6366
}
6467

6568
public DevicePolicyManagerGatewayImpl(@NonNull DevicePolicyManager dpm, @NonNull UserManager um,
66-
@NonNull PackageManager pm, @NonNull ComponentName admin) {
69+
@NonNull PackageManager pm, @NonNull LocationManager lm, @NonNull ComponentName admin) {
6770
mDevicePolicyManager = dpm;
6871
mUserManager = um;
6972
mPackageManager = pm;
73+
mLocationManager = lm;
7074
mAdminComponentName = admin;
7175

7276
Log.d(TAG, "constructor: admin=" + mAdminComponentName + ", dpm=" + dpm);
@@ -78,7 +82,8 @@ public static DevicePolicyManagerGatewayImpl forParentProfile(@NonNull Context c
7882
.getParentProfileInstance(admin);
7983
UserManager um = context.getSystemService(UserManager.class);
8084
PackageManager pm = context.getPackageManager();
81-
return new DevicePolicyManagerGatewayImpl(dpm, um, pm, admin);
85+
LocationManager lm = context.getSystemService(LocationManager.class);
86+
return new DevicePolicyManagerGatewayImpl(dpm, um, pm, lm, admin);
8287
}
8388

8489
@Override
@@ -792,6 +797,23 @@ public int getPermissionGrantState(String packageName, String permission) {
792797
return grantState;
793798
}
794799

800+
@Override
801+
public void setLocationEnabled(boolean enabled, Consumer<Void> onSuccess,
802+
Consumer<Exception> onError) {
803+
Log.d(TAG, "setLocationEnabled(" + enabled + ")");
804+
try {
805+
mDevicePolicyManager.setLocationEnabled(mAdminComponentName, enabled);
806+
onSuccess.accept(null);
807+
} catch (Exception e) {
808+
onError.accept(e);
809+
}
810+
}
811+
812+
@Override
813+
public boolean isLocationEnabled() {
814+
return mLocationManager.isLocationEnabled();
815+
}
816+
795817
@Override
796818
public String toString() {
797819
return "DevicePolicyManagerGatewayImpl[" + mAdminComponentName + "]";

app/src/main/java/com/afwsamples/testdpc/ShellCommand.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ final class ShellCommand {
9797
private static final String CMD_GET_APP_RESTRICTIONS = "get-app-restrictions";
9898
private static final String CMD_SET_PERMISSION_GRANT_STATE = "set-permission-grant-state";
9999
private static final String CMD_GET_PERMISSION_GRANT_STATE = "get-permission-grant-state";
100+
private static final String CMD_SET_LOCATION_ENABLED = "set-location-enabled";
101+
private static final String CMD_IS_LOCATION_ENABLED = "is-location-enabled";
100102

101103
// Commands for APIs added on Android S
102104
private static final String CMD_SET_PERMITTED_INPUT_METHODS_PARENT =
@@ -277,6 +279,12 @@ public void run() {
277279
case CMD_GET_PERMISSION_GRANT_STATE:
278280
execute(() -> getPermissionGrantState());
279281
break;
282+
case CMD_SET_LOCATION_ENABLED:
283+
execute(() -> setLocationEnabled());
284+
break;
285+
case CMD_IS_LOCATION_ENABLED:
286+
execute(() -> isLocationEnabled());
287+
break;
280288
default:
281289
mWriter.printf("Invalid command: %s\n\n", cmd);
282290
showUsage();
@@ -382,6 +390,10 @@ private void showUsage() {
382390
CMD_SET_PERMISSION_GRANT_STATE);
383391
mWriter.printf("\t%s <PKG> <PERMISSION> - get the grant state for the given permission / "
384392
+ "package\n", CMD_GET_PERMISSION_GRANT_STATE);
393+
mWriter.printf("\t%s <true|false> - set location enabled for the user\n",
394+
CMD_SET_LOCATION_ENABLED);
395+
mWriter.printf("\t%s - get whether location is enabled for the user\n",
396+
CMD_IS_LOCATION_ENABLED);
385397

386398
// Separator for S / pre-S commands - do NOT remove line to avoid cherry-pick conflicts
387399

@@ -891,6 +903,20 @@ private void getPermissionGrantState() {
891903
Util.grantStateToString(grantState));
892904
}
893905

906+
private void setLocationEnabled() {
907+
// TODO(b/171350084): check args
908+
boolean enabled = Boolean.parseBoolean(mArgs[1]);
909+
910+
mDevicePolicyManagerGateway.setLocationEnabled(enabled,
911+
(v) -> onSuccess("Set location enabled to %b", enabled),
912+
(e) -> onError(e, "Error setting location enabled to %b", enabled));
913+
}
914+
915+
private void isLocationEnabled() {
916+
boolean enabled = mDevicePolicyManagerGateway.isLocationEnabled();
917+
mWriter.printf("Location enabled: %b\n", enabled);
918+
}
919+
894920
private static String permittedToString(boolean permitted) {
895921
return permitted ? "PERMITTED" : "NOT PERMITTED";
896922
}

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -535,22 +535,22 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
535535

536536
@Override
537537
public void onCreate(Bundle savedInstanceState) {
538+
Context context = getActivity();
538539
if (isDelegatedApp() || isCredentialManagerApp()) {
539540
mAdminComponentName = null;
540541
} else {
541-
mAdminComponentName = DeviceAdminReceiver.getComponentName(getActivity());
542+
mAdminComponentName = DeviceAdminReceiver.getComponentName(context);
542543
}
543-
mDevicePolicyManager = (DevicePolicyManager) getActivity().getSystemService(
544-
Context.DEVICE_POLICY_SERVICE);
545-
mUserManager = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
546-
mPackageManager = getActivity().getPackageManager();
544+
mDevicePolicyManager = context.getSystemService(DevicePolicyManager.class);
545+
mUserManager = context.getSystemService(UserManager.class);
546+
mPackageManager = context.getPackageManager();
547547
mDevicePolicyManagerGateway = new DevicePolicyManagerGatewayImpl(mDevicePolicyManager,
548-
mUserManager, mPackageManager, mAdminComponentName);
548+
mUserManager, mPackageManager, context.getSystemService(LocationManager.class),
549+
mAdminComponentName);
549550
mIsProfileOwner = mDevicePolicyManagerGateway.isProfileOwnerApp();
550-
mTelephonyManager = (TelephonyManager) getActivity()
551-
.getSystemService(Context.TELEPHONY_SERVICE);
552-
mAccountManager = AccountManager.get(getActivity());
553-
mPackageName = getActivity().getPackageName();
551+
mTelephonyManager = context.getSystemService(TelephonyManager.class);
552+
mAccountManager = AccountManager.get(context);
553+
mPackageName = context.getPackageName();
554554

555555
mImageUri = getStorageUri("image.jpg");
556556
mVideoUri = getStorageUri("video.mp4");

0 commit comments

Comments
 (0)