Skip to content

Commit ac368b2

Browse files
jscott1989pfmaggi
authored andcommitted
No public description
PiperOrigin-RevId: 605570443
1 parent 366290d commit ac368b2

File tree

7 files changed

+47
-7
lines changed

7 files changed

+47
-7
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ You can find various kinds of provisioning methods [here](https://developers.goo
2323
3. Modify (if needed) and scan [this QR code] (http://down-box.appspot.com/qr/nQB0tw7b).
2424
4. Follow onscreen instructions
2525

26-
#### ADB command (Device Owner) ####
27-
28-
```shell
26+
#### adb command (Device Owner) ####
2927
adb shell dpm set-device-owner com.afwsamples.testdpc/.DeviceAdminReceiver
30-
```
3128

3229
#### Work profile ####
3330
The easiest way is to launch the "Set Up TestDPC" app in launcher and follow the onscreen instructions.

src/main/AndroidManifest.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
4141
<uses-permission android:name="com.google.android.setupwizard.SETUP_COMPAT_SERVICE" />
4242
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
43+
<uses-permission android:name="android.permission.MANAGE_DEVICE_POLICY_CERTIFICATES"/>
44+
<uses-permission android:name="android.permission.MANAGE_DEVICE_POLICY_SYSTEM_UPDATE_INFO"/>
4345

4446
<uses-feature android:name="android.hardware.wifi" android:required="false" />
4547
<uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
@@ -153,6 +155,15 @@
153155
</intent-filter>
154156
</activity>
155157

158+
<activity android:name="android.app.Activity" android:exported="true" android:permission="android.permission.LAUNCH_DEVICE_MANAGER_SETUP">
159+
<intent-filter>
160+
<action android:name="android.app.action.ROLE_HOLDER_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE" />
161+
<action android:name="android.app.action.ROLE_HOLDER_PROVISION_MANAGED_PROFILE" />
162+
<action android:name="android.app.action.ROLE_HOLDER_PROVISION_FINALIZATION" />
163+
<category android:name="android.intent.category.DEFAULT" />
164+
</intent-filter>
165+
</activity>
166+
156167
<receiver
157168
android:name=".DeviceAdminReceiver"
158169
android:exported="true"

src/main/java/com/afwsamples/testdpc/common/preference/DpcPreferenceHelper.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import android.app.admin.DevicePolicyManager;
2020
import android.content.Context;
21+
import android.content.pm.PackageManager;
2122
import android.content.res.TypedArray;
2223
import android.os.Build.VERSION_CODES;
2324
import android.text.TextUtils;
@@ -96,6 +97,7 @@ public class DpcPreferenceHelper {
9697
private @AdminKind int mAdminConstraint;
9798
private @UserKind int mUserConstraint;
9899
private String mDelegationConstraint;
100+
private String mPermissionConstraint;
99101

100102
public DpcPreferenceHelper(Context context, Preference preference, AttributeSet attrs) {
101103
mContext = context;
@@ -117,6 +119,7 @@ public DpcPreferenceHelper(Context context, Preference preference, AttributeSet
117119
// noinspection ResourceType
118120
mUserConstraint = a.getInt(R.styleable.DpcPreference_user, USER_DEFAULT);
119121
mDelegationConstraint = a.getString(R.styleable.DpcPreference_delegation);
122+
mPermissionConstraint = a.getString(R.styleable.DpcPreference_permission);
120123
a.recycle();
121124
}
122125

@@ -294,7 +297,7 @@ private int getCurrentUser() {
294297
}
295298

296299
private boolean isSufficientlyPrivileged(@AdminKind int admin, List<String> delegations) {
297-
return isEnabledForAdmin(admin) || hasDelegation(delegations);
300+
return isEnabledForAdmin(admin) || hasDelegation(delegations) || hasPermission();
298301
}
299302

300303
private boolean isEnabledForAdmin(@AdminKind int admin) {
@@ -305,6 +308,11 @@ private boolean hasDelegation(List<String> delegations) {
305308
return delegations.contains(mDelegationConstraint);
306309
}
307310

311+
private boolean hasPermission() {
312+
return mPermissionConstraint != null
313+
&& mContext.checkSelfPermission(mPermissionConstraint) == PackageManager.PERMISSION_GRANTED;
314+
}
315+
308316
private boolean isEnabledForUser(@UserKind int user) {
309317
return (mUserConstraint & user) == user;
310318
}

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import android.app.admin.PackagePolicy;
3939
import android.app.admin.SystemUpdateInfo;
4040
import android.app.admin.WifiSsidPolicy;
41+
import android.app.role.RoleManager;
4142
import android.content.ComponentName;
4243
import android.content.ContentResolver;
4344
import android.content.Context;
@@ -463,6 +464,11 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
463464

464465
private static final SparseIntArray PASSWORD_COMPLEXITY = new SparseIntArray(4);
465466

467+
// Copied over from RoleManager.ROLE_DEVICE_POLICY_MANAGEMENT, which can't be referenced directly
468+
// since it's a @SystemAPI.
469+
private static final String ROLE_DEVICE_POLICY_MANAGEMENT =
470+
"android.app.role.DEVICE_POLICY_MANAGEMENT";
471+
466472
static {
467473
if (Util.SDK_INT >= VERSION_CODES.Q) {
468474
final int[] complexityIds =
@@ -560,7 +566,7 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
560566
@Override
561567
public void onCreate(Bundle savedInstanceState) {
562568
Context context = getActivity();
563-
if (isDelegatedApp() || isCredentialManagerApp()) {
569+
if (isDelegatedApp() || isCredentialManagerApp() || isDeviceManagementRoleHolder()) {
564570
mAdminComponentName = null;
565571
} else {
566572
mAdminComponentName = DeviceAdminReceiver.getComponentName(context);
@@ -999,6 +1005,14 @@ private boolean isCredentialManagerApp() {
9991005
return !dpm.isDeviceOwnerApp(packageName) && !dpm.isProfileOwnerApp(packageName);
10001006
}
10011007

1008+
private boolean isDeviceManagementRoleHolder() {
1009+
if (Util.SDK_INT < VERSION_CODES.S) {
1010+
return false;
1011+
}
1012+
RoleManager rm = getActivity().getSystemService(RoleManager.class);
1013+
return rm.isRoleHeld(ROLE_DEVICE_POLICY_MANAGEMENT);
1014+
}
1015+
10021016
@Override
10031017
public boolean isAvailable(Context context) {
10041018
return true;
@@ -2580,6 +2594,8 @@ private void loadAppStatus() {
25802594
appStatusStringId = R.string.this_is_a_device_owner;
25812595
} else if (isDelegatedApp()) {
25822596
appStatusStringId = R.string.this_is_a_delegated_app;
2597+
} else if (isDeviceManagementRoleHolder()) {
2598+
appStatusStringId = R.string.this_is_a_role_holder;
25832599
} else {
25842600
appStatusStringId = R.string.this_is_not_an_admin;
25852601
}
@@ -2616,7 +2632,8 @@ private void loadEnrollmentSpecificId() {
26162632

26172633
String esid = mDevicePolicyManager.getEnrollmentSpecificId();
26182634

2619-
enrollmentSpecificIdPreference.setSummary(esid);
2635+
enrollmentSpecificIdPreference.setSummary(
2636+
TextUtils.isEmpty(esid) ? getString(R.string.enrollment_specific_id_empty) : esid);
26202637
}
26212638

26222639
@TargetApi(VERSION_CODES.P)

src/main/res/values/attrs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
<!-- Constrain a preference to a delegated scope. -->
5454
<attr name="delegation" format="string" />
5555

56+
<!-- Constrain a preference to a permission. -->
57+
<attr name="permission" format="string" />
58+
5659
<!-- Constrain a preference to certain users. -->
5760
<attr name="user">
5861
<flag name="primaryUser" value="0x1" />

src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
<string name="this_is_a_profile_owner">This app is a profile owner.</string>
123123
<string name="this_is_an_org_owned_profile_owner">This app is a profile owner of an organization-owned device.</string>
124124
<string name="this_is_a_delegated_app">This app has delegated permissions.</string>
125+
<string name="this_is_a_role_holder">This app is the Device Management Role Holder.</string>
125126
<string name="this_is_not_an_admin">This app is not an admin.</string>
126127

127128
<!-- Strings for provisioning disclaimers-->
@@ -1249,6 +1250,7 @@
12491250
<string name="set_organization_id">Set Organization Identifier</string>
12501251
<string name="organization_id_empty">Organization Identifier must not be empty.</string>
12511252
<string name="enrollment_specific_id">Enrollment-specific ID</string>
1253+
<string name="enrollment_specific_id_empty">Not Available</string>
12521254

12531255
<!-- Messsage used in a toast to inform that operations that could be unsafe to execute might be safe now (or vice versa) -->
12541256
<string name="safety_operations_change_message">Operations that affect <xliff:g id="reason">%1$s</xliff:g> are now <xliff:g id="state">%2$s</xliff:g></string>

src/main/res/xml/device_policy_header.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
android:title="@string/enrollment_specific_id"
3535
testdpc:admin="deviceOwner|profileOwner"
3636
testdpc:delegation="delegation-cert-install"
37+
testdpc:permission="android.permission.MANAGE_DEVICE_POLICY_CERTIFICATES"
3738
testdpc:minSdkVersion="S" />
3839

3940
<PreferenceCategory android:title="@string/accessibility_title">
@@ -615,6 +616,7 @@
615616
<com.afwsamples.testdpc.common.preference.DpcPreference
616617
android:key="system_update_pending"
617618
android:title="@string/system_update_pending"
619+
testdpc:permission="android.permission.MANAGE_DEVICE_POLICY_SYSTEM_UPDATE_INFO"
618620
testdpc:minSdkVersion="O" />
619621
<com.afwsamples.testdpc.common.preference.DpcPreference
620622
android:key="managed_system_updates"

0 commit comments

Comments
 (0)