@@ -488,6 +488,7 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
488
488
}
489
489
490
490
private DevicePolicyManager mDevicePolicyManager ;
491
+ private DevicePolicyManager mParentDevicePolicyManager ;
491
492
private DevicePolicyManagerGateway mDevicePolicyManagerGateway ;
492
493
private PackageManager mPackageManager ;
493
494
private String mPackageName ;
@@ -556,12 +557,17 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
556
557
private Uri mImageUri ;
557
558
private Uri mVideoUri ;
558
559
private boolean mIsProfileOwner ;
560
+ private boolean mIsOrganizationOwnedProfileOwner ;
559
561
560
562
@ Override
561
563
public void onCreate (Bundle savedInstanceState ) {
562
564
Context context = getActivity ();
563
565
mAdminComponentName = DeviceAdminReceiver .getComponentName (context );
564
566
mDevicePolicyManager = context .getSystemService (DevicePolicyManager .class );
567
+ mParentDevicePolicyManager =
568
+ Util .SDK_INT >= VERSION_CODES .N && isManagedProfileOwner ()
569
+ ? mDevicePolicyManager .getParentProfileInstance (mAdminComponentName )
570
+ : null ;
565
571
mUserManager = context .getSystemService (UserManager .class );
566
572
mPackageManager = context .getPackageManager ();
567
573
mDevicePolicyManagerGateway =
@@ -572,6 +578,11 @@ public void onCreate(Bundle savedInstanceState) {
572
578
context .getSystemService (LocationManager .class ),
573
579
mAdminComponentName );
574
580
mIsProfileOwner = mDevicePolicyManagerGateway .isProfileOwnerApp ();
581
+ mIsOrganizationOwnedProfileOwner =
582
+ Util .SDK_INT >= VERSION_CODES .R
583
+ && mIsProfileOwner
584
+ && mDevicePolicyManagerGateway .isOrganizationOwnedDeviceWithManagedProfile ();
585
+
575
586
mTelephonyManager = context .getSystemService (TelephonyManager .class );
576
587
mAccountManager = AccountManager .get (context );
577
588
mPackageName = context .getPackageName ();
@@ -778,9 +789,14 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
778
789
findPreference (CROSS_PROFILE_APPS ).setOnPreferenceClickListener (this );
779
790
findPreference (CROSS_PROFILE_APPS_ALLOWLIST ).setOnPreferenceClickListener (this );
780
791
792
+ ((DpcPreference ) findPreference (SET_SCREEN_BRIGHTNESS_KEY ))
793
+ .setCustomConstraint (this ::validateBrightnessControlConstraint );
781
794
findPreference (SET_SCREEN_BRIGHTNESS_KEY ).setOnPreferenceClickListener (this );
782
795
mAutoBrightnessPreference = (DpcSwitchPreference ) findPreference (AUTO_BRIGHTNESS_KEY );
796
+ mAutoBrightnessPreference .setCustomConstraint (this ::validateBrightnessControlConstraint );
783
797
mAutoBrightnessPreference .setOnPreferenceChangeListener (this );
798
+ ((DpcPreference ) findPreference (SET_SCREEN_OFF_TIMEOUT_KEY ))
799
+ .setCustomConstraint (this ::validateBrightnessControlConstraint );
784
800
findPreference (SET_SCREEN_OFF_TIMEOUT_KEY ).setOnPreferenceClickListener (this );
785
801
786
802
findPreference (SET_TIME_KEY ).setOnPreferenceClickListener (this );
@@ -1676,10 +1692,11 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
1676
1692
reloadEnableLogoutUi ();
1677
1693
return true ;
1678
1694
case AUTO_BRIGHTNESS_KEY :
1679
- mDevicePolicyManager .setSystemSetting (
1680
- mAdminComponentName ,
1681
- Settings .System .SCREEN_BRIGHTNESS_MODE ,
1682
- newValue .equals (true ) ? "1" : "0" );
1695
+ (mIsOrganizationOwnedProfileOwner ? mParentDevicePolicyManager : mDevicePolicyManager )
1696
+ .setSystemSetting (
1697
+ mAdminComponentName ,
1698
+ Settings .System .SCREEN_BRIGHTNESS_MODE ,
1699
+ newValue .equals (true ) ? "1" : "0" );
1683
1700
reloadAutoBrightnessUi ();
1684
1701
return true ;
1685
1702
case SET_NEW_PASSWORD_WITH_COMPLEXITY :
@@ -1742,9 +1759,7 @@ private void setCameraDisabled(boolean disabled) {
1742
1759
1743
1760
@ TargetApi (VERSION_CODES .R )
1744
1761
private void setCameraDisabledOnParent (boolean disabled ) {
1745
- DevicePolicyManager parentDpm =
1746
- mDevicePolicyManager .getParentProfileInstance (mAdminComponentName );
1747
- parentDpm .setCameraDisabled (mAdminComponentName , disabled );
1762
+ mParentDevicePolicyManager .setCameraDisabled (mAdminComponentName , disabled );
1748
1763
}
1749
1764
1750
1765
@ TargetApi (VERSION_CODES .N )
@@ -1793,9 +1808,7 @@ private void setScreenCaptureDisabled(boolean disabled) {
1793
1808
1794
1809
@ TargetApi (VERSION_CODES .R )
1795
1810
private void setScreenCaptureDisabledOnParent (boolean disabled ) {
1796
- DevicePolicyManager parentDpm =
1797
- mDevicePolicyManager .getParentProfileInstance (mAdminComponentName );
1798
- parentDpm .setScreenCaptureDisabled (mAdminComponentName , disabled );
1811
+ mParentDevicePolicyManager .setScreenCaptureDisabled (mAdminComponentName , disabled );
1799
1812
}
1800
1813
1801
1814
private boolean isDeviceOwner () {
@@ -2544,11 +2557,8 @@ private void loadAppFeedbackNotifications() {
2544
2557
2545
2558
private void loadAppStatus () {
2546
2559
final @ StringRes List <Integer > appStatus = new ArrayList <>();
2547
- boolean isOrgOwned =
2548
- Util .SDK_INT >= VERSION_CODES .R
2549
- && mDevicePolicyManagerGateway .isOrganizationOwnedDeviceWithManagedProfile ();
2550
2560
if (mDevicePolicyManager .isProfileOwnerApp (mPackageName )) {
2551
- if (isOrgOwned ) {
2561
+ if (mIsOrganizationOwnedProfileOwner ) {
2552
2562
appStatus .add (R .string .this_is_an_org_owned_profile_owner );
2553
2563
} else {
2554
2564
appStatus .add (R .string .this_is_a_profile_owner );
@@ -2630,9 +2640,8 @@ private void loadPasswordComplexity() {
2630
2640
String summary ;
2631
2641
int complexity = PASSWORD_COMPLEXITY .get (mDevicePolicyManager .getPasswordComplexity ());
2632
2642
if (isManagedProfileOwner () && Util .SDK_INT >= VERSION_CODES .R ) {
2633
- DevicePolicyManager parentDpm =
2634
- mDevicePolicyManager .getParentProfileInstance (mAdminComponentName );
2635
- int parentComplexity = PASSWORD_COMPLEXITY .get (parentDpm .getPasswordComplexity ());
2643
+ int parentComplexity =
2644
+ PASSWORD_COMPLEXITY .get (mParentDevicePolicyManager .getPasswordComplexity ());
2636
2645
summary =
2637
2646
String .format (
2638
2647
getString (R .string .password_complexity_profile_summary ),
@@ -2659,9 +2668,8 @@ private void loadRequiredPasswordComplexity() {
2659
2668
String summary ;
2660
2669
int complexity = PASSWORD_COMPLEXITY .get (getRequiredComplexity (mDevicePolicyManager ));
2661
2670
if (isManagedProfileOwner () && Util .SDK_INT >= VERSION_CODES .S ) {
2662
- DevicePolicyManager parentDpm =
2663
- mDevicePolicyManager .getParentProfileInstance (mAdminComponentName );
2664
- int parentComplexity = PASSWORD_COMPLEXITY .get (getRequiredComplexity (parentDpm ));
2671
+ int parentComplexity =
2672
+ PASSWORD_COMPLEXITY .get (getRequiredComplexity (mParentDevicePolicyManager ));
2665
2673
summary =
2666
2674
String .format (
2667
2675
getString (R .string .password_complexity_profile_summary ),
@@ -2687,8 +2695,7 @@ private void setRequiredPasswordComplexity(int complexity) {
2687
2695
// running older releases and obviates the need for a target sdk check here.
2688
2696
@ TargetApi (VERSION_CODES .S )
2689
2697
private void setRequiredPasswordComplexityOnParent (int complexity ) {
2690
- setRequiredPasswordComplexity (
2691
- mDevicePolicyManager .getParentProfileInstance (mAdminComponentName ), complexity );
2698
+ setRequiredPasswordComplexity (mParentDevicePolicyManager , complexity );
2692
2699
}
2693
2700
2694
2701
// NOTE: The setRequiredPasswordComplexity call is gated by a check in device_policy_header.xml,
@@ -2712,15 +2719,14 @@ private void loadPasswordCompliant() {
2712
2719
String summary ;
2713
2720
boolean compliant = mDevicePolicyManager .isActivePasswordSufficient ();
2714
2721
if (isManagedProfileOwner ()) {
2715
- DevicePolicyManager parentDpm =
2716
- mDevicePolicyManager .getParentProfileInstance (mAdminComponentName );
2717
- boolean parentCompliant = parentDpm .isActivePasswordSufficient ();
2722
+ boolean parentCompliant = mParentDevicePolicyManager .isActivePasswordSufficient ();
2718
2723
final String deviceCompliant ;
2719
2724
if (Util .SDK_INT < VERSION_CODES .S ) {
2720
2725
deviceCompliant = "N/A" ;
2721
2726
} else {
2722
2727
deviceCompliant =
2723
- Boolean .toString (parentDpm .isActivePasswordSufficientForDeviceRequirement ());
2728
+ Boolean .toString (
2729
+ mParentDevicePolicyManager .isActivePasswordSufficientForDeviceRequirement ());
2724
2730
}
2725
2731
summary =
2726
2732
String .format (
@@ -2812,9 +2818,7 @@ private void reloadCameraDisableUi() {
2812
2818
2813
2819
@ TargetApi (VERSION_CODES .R )
2814
2820
private void reloadCameraDisableOnParentUi () {
2815
- DevicePolicyManager parentDpm =
2816
- mDevicePolicyManager .getParentProfileInstance (mAdminComponentName );
2817
- boolean isCameraDisabled = parentDpm .getCameraDisabled (mAdminComponentName );
2821
+ boolean isCameraDisabled = mParentDevicePolicyManager .getCameraDisabled (mAdminComponentName );
2818
2822
mDisableCameraOnParentSwitchPreference .setChecked (isCameraDisabled );
2819
2823
}
2820
2824
@@ -2871,9 +2875,8 @@ private void reloadScreenCaptureDisableUi() {
2871
2875
2872
2876
@ TargetApi (VERSION_CODES .R )
2873
2877
private void reloadScreenCaptureDisableOnParentUi () {
2874
- DevicePolicyManager parentDpm =
2875
- mDevicePolicyManager .getParentProfileInstance (mAdminComponentName );
2876
- boolean isScreenCaptureDisabled = parentDpm .getScreenCaptureDisabled (mAdminComponentName );
2878
+ boolean isScreenCaptureDisabled =
2879
+ mParentDevicePolicyManager .getScreenCaptureDisabled (mAdminComponentName );
2877
2880
mDisableScreenCaptureOnParentSwitchPreference .setChecked (isScreenCaptureDisabled );
2878
2881
}
2879
2882
@@ -3682,9 +3685,8 @@ private void showHideAppsOnParentPrompt(final boolean showHiddenApps) {
3682
3685
(dialog , which ) -> {
3683
3686
String packageName = input .getText ().toString ();
3684
3687
try {
3685
- if (mDevicePolicyManager
3686
- .getParentProfileInstance (mAdminComponentName )
3687
- .setApplicationHidden (mAdminComponentName , packageName , !showHiddenApps )) {
3688
+ if (mParentDevicePolicyManager .setApplicationHidden (
3689
+ mAdminComponentName , packageName , !showHiddenApps )) {
3688
3690
showToast (successResId , packageName );
3689
3691
} else {
3690
3692
showToast (getString (failureResId , packageName ), Toast .LENGTH_LONG );
@@ -4263,8 +4265,9 @@ private void showSetScreenBrightnessDialog() {
4263
4265
showToast (R .string .invalid_screen_brightness );
4264
4266
return ;
4265
4267
}
4266
- mDevicePolicyManager .setSystemSetting (
4267
- mAdminComponentName , Settings .System .SCREEN_BRIGHTNESS , brightness );
4268
+ (mIsOrganizationOwnedProfileOwner ? mParentDevicePolicyManager : mDevicePolicyManager )
4269
+ .setSystemSetting (
4270
+ mAdminComponentName , Settings .System .SCREEN_BRIGHTNESS , brightness );
4268
4271
})
4269
4272
.setNegativeButton (android .R .string .cancel , null )
4270
4273
.show ();
@@ -4341,10 +4344,11 @@ private void showSetScreenOffTimeoutDialog() {
4341
4344
showToast (R .string .invalid_screen_off_timeout );
4342
4345
return ;
4343
4346
}
4344
- mDevicePolicyManager .setSystemSetting (
4345
- mAdminComponentName ,
4346
- Settings .System .SCREEN_OFF_TIMEOUT ,
4347
- Integer .toString (screenTimeoutVaue * 1000 ));
4347
+ (mIsOrganizationOwnedProfileOwner ? mParentDevicePolicyManager : mDevicePolicyManager )
4348
+ .setSystemSetting (
4349
+ mAdminComponentName ,
4350
+ Settings .System .SCREEN_OFF_TIMEOUT ,
4351
+ Integer .toString (screenTimeoutVaue * 1000 ));
4348
4352
})
4349
4353
.setNegativeButton (android .R .string .cancel , null )
4350
4354
.show ();
@@ -4766,6 +4770,15 @@ && getActivity().getApplicationInfo().targetSdkVersion >= VERSION_CODES.O) {
4766
4770
return NO_CUSTOM_CONSTRAINT ;
4767
4771
}
4768
4772
4773
+ private int validateBrightnessControlConstraint () {
4774
+ // Brightness control is always available to DO, and is enabled for COPE starting from
4775
+ // Android V
4776
+ if (mIsOrganizationOwnedProfileOwner && Util .SDK_INT < VERSION_CODES .VANILLA_ICE_CREAM ) {
4777
+ return R .string .requires_android_v ;
4778
+ }
4779
+ return NO_CUSTOM_CONSTRAINT ;
4780
+ }
4781
+
4769
4782
interface ManageLockTaskListCallback {
4770
4783
void onPositiveButtonClicked (String [] lockTaskArray );
4771
4784
}
0 commit comments