30
30
31
31
/**
32
32
* 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}.
37
41
*/
38
- @ TargetApi (Build .VERSION_CODES .N )
39
42
public class AppRestrictionsManagingPackageFragment extends SelectAppFragment {
40
43
41
44
private DevicePolicyManager mDpm ;
42
45
@ Override
43
46
public void onCreate (Bundle savedInstanceState ) {
44
47
super .onCreate (savedInstanceState );
45
- mDpm = (DevicePolicyManager ) getContext ().getSystemService (Context .DEVICE_POLICY_SERVICE );
48
+ mDpm = (DevicePolicyManager ) getActivity ().getSystemService (Context .DEVICE_POLICY_SERVICE );
46
49
}
47
50
48
51
@ Override
@@ -57,11 +60,10 @@ protected void setSelectedPackage(String pkgName) {
57
60
if (TextUtils .isEmpty (pkgName )) {
58
61
pkgName = null ;
59
62
}
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 );
65
67
}
66
68
}
67
69
@@ -72,7 +74,35 @@ protected void clearSelectedPackage() {
72
74
73
75
@ Override
74
76
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 () {
75
101
return mDpm .getApplicationRestrictionsManagingPackage (
76
- DeviceAdminReceiver .getComponentName (getActivity ()));
102
+ DeviceAdminReceiver .getComponentName (getActivity ()));
103
+ }
104
+
105
+ private String getApplicationRestrictionsManagingPackageWithProxy () {
106
+ return AppRestrictionsProxyHandler .getApplicationRestrictionsManagingPackage (getActivity ());
77
107
}
78
108
}
0 commit comments