Skip to content

Commit 17b5cfc

Browse files
committed
Enable app restriction proxy code on L and M.
Bug: 26111675 Change-Id: Ib9d2987429401c53fd9151f8c8703bb30a9c5346
1 parent 48a96e9 commit 17b5cfc

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
294294
};
295295

296296
private static String[] NYC_PLUS_PREFERENCES = {
297-
APP_RESTRICTIONS_MANAGING_PACKAGE_KEY, REBOOT_KEY, REMOVE_KEY_CERTIFICATE_KEY,
298-
SET_ALWAYS_ON_VPN_KEY, SHOW_WIFI_MAC_ADDRESS_KEY, SUSPEND_APPS_KEY, UNSUSPEND_APPS_KEY,
297+
REBOOT_KEY, REMOVE_KEY_CERTIFICATE_KEY, SET_ALWAYS_ON_VPN_KEY,
298+
SHOW_WIFI_MAC_ADDRESS_KEY, SUSPEND_APPS_KEY, UNSUSPEND_APPS_KEY,
299299
SET_SHORT_SUPPORT_MESSAGE_KEY, SET_LONG_SUPPORT_MESSAGE_KEY, REQUEST_BUGREPORT_KEY,
300300
ENABLE_PROCESS_LOGGING, REQUEST_PROCESS_LOGS, CREATE_AND_MANAGE_USER_KEY
301301
};

app/src/main/java/com/afwsamples/testdpc/profilepolicy/apprestrictions/AppRestrictionsManagingPackageFragment.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,22 @@
3030

3131
/**
3232
* This fragment lets the user select an app that can manage application restrictions for the
33-
* current user. Related APIs:
34-
* 1) {@link DevicePolicyManager#setApplicationRestrictionsManagingPackage}
35-
* 2) {@link DevicePolicyManager#getApplicationRestrictionsManagingPackage}
36-
* 3) {@link DevicePolicyManager#isCallerApplicationRestrictionsManagingPackage}
33+
* current user.
34+
*
35+
* <p>On Android N and after, it allows the selected app to call the DevicePolicyManager APIs using:
36+
* {@link DevicePolicyManager#setApplicationRestrictionsManagingPackage}
37+
* {@link DevicePolicyManager#getApplicationRestrictionsManagingPackage}
38+
*
39+
* <p>On Android M and before, it allows the selected app to proxy API calls to DevicePolicyManager
40+
* via TestDPC, see {@link AppRestrictionsProxyHandler}.
3741
*/
38-
@TargetApi(Build.VERSION_CODES.N)
3942
public class AppRestrictionsManagingPackageFragment extends SelectAppFragment {
4043

4144
private DevicePolicyManager mDpm;
4245
@Override
4346
public void onCreate(Bundle savedInstanceState) {
4447
super.onCreate(savedInstanceState);
45-
mDpm = (DevicePolicyManager) getContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
48+
mDpm = (DevicePolicyManager) getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
4649
}
4750

4851
@Override
@@ -57,11 +60,10 @@ protected void setSelectedPackage(String pkgName) {
5760
if (TextUtils.isEmpty(pkgName)) {
5861
pkgName = null;
5962
}
60-
try {
61-
mDpm.setApplicationRestrictionsManagingPackage(
62-
DeviceAdminReceiver.getComponentName(getActivity()), pkgName);
63-
} catch (NameNotFoundException nnpe) {
64-
throw new IllegalArgumentException(nnpe);
63+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
64+
setApplicationRestrictionsManagingPackage(pkgName);
65+
} else {
66+
setApplicationRestrictionsManagingPackageWithProxy(pkgName);
6567
}
6668
}
6769

@@ -72,7 +74,35 @@ protected void clearSelectedPackage() {
7274

7375
@Override
7476
protected String getSelectedPackage() {
77+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
78+
return getApplicationRestrictionsManagingPackage();
79+
} else {
80+
return getApplicationRestrictionsManagingPackageWithProxy();
81+
}
82+
}
83+
84+
@TargetApi(Build.VERSION_CODES.N)
85+
private void setApplicationRestrictionsManagingPackage(String pkgName) {
86+
try {
87+
mDpm.setApplicationRestrictionsManagingPackage(
88+
DeviceAdminReceiver.getComponentName(getActivity()), pkgName);
89+
} catch (NameNotFoundException e) {
90+
throw new IllegalArgumentException(e);
91+
}
92+
}
93+
94+
private void setApplicationRestrictionsManagingPackageWithProxy(String pkgName) {
95+
AppRestrictionsProxyHandler.setApplicationRestrictionsManagingPackage(
96+
getActivity(), pkgName);
97+
}
98+
99+
@TargetApi(Build.VERSION_CODES.N)
100+
private String getApplicationRestrictionsManagingPackage() {
75101
return mDpm.getApplicationRestrictionsManagingPackage(
76-
DeviceAdminReceiver.getComponentName(getActivity()));
102+
DeviceAdminReceiver.getComponentName(getActivity()));
103+
}
104+
105+
private String getApplicationRestrictionsManagingPackageWithProxy() {
106+
return AppRestrictionsProxyHandler.getApplicationRestrictionsManagingPackage(getActivity());
77107
}
78108
}

0 commit comments

Comments
 (0)