Skip to content

Commit a3c14c7

Browse files
author
Pavel Grafov
committed
Support for personal apps suspension.
Bug: 147414651 Test: manual Change-Id: I4623c6973c9efd88fed482bc156ae6b8fb9fd873
1 parent 1997881 commit a3c14c7

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,16 @@
4141
<activity
4242
android:name=".PolicyManagementActivity"
4343
android:label="@string/app_name"
44+
android:exported="true"
4445
android:windowSoftInputMode="adjustPan">
4546
<intent-filter>
4647
<action android:name="android.intent.action.MAIN"/>
4748
<category android:name="android.intent.category.LAUNCHER"/>
4849
</intent-filter>
50+
<intent-filter>
51+
<action android:name="android.app.action.CHECK_POLICY_COMPLIANCE"/>
52+
<category android:name="android.intent.category.DEFAULT"/>
53+
</intent-filter>
4954
</activity>
5055

5156
<activity

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

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
359359
private static final String SYSTEM_UPDATE_POLICY_KEY = "system_update_policy";
360360
private static final String SYSTEM_UPDATE_PENDING_KEY = "system_update_pending";
361361
private static final String TEST_KEY_USABILITY_KEY = "test_key_usability";
362-
363362
private static final String UNHIDE_APPS_KEY = "unhide_apps";
364363
private static final String UNSUSPEND_APPS_KEY = "unsuspend_apps";
365364
private static final String CLEAR_APP_DATA_KEY = "clear_app_data";
@@ -382,27 +381,20 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
382381
private static final String SET_PROFILE_PARENT_NEW_PASSWORD = "set_profile_parent_new_password";
383382
private static final String BIND_DEVICE_ADMIN_POLICIES = "bind_device_admin_policies";
384383
private static final String CROSS_PROFILE_APPS = "cross_profile_apps";
385-
386384
private static final String SET_SCREEN_BRIGHTNESS_KEY = "set_screen_brightness";
387385
private static final String AUTO_BRIGHTNESS_KEY = "auto_brightness";
388386
private static final String CROSS_PROFILE_CALENDAR_KEY = "cross_profile_calendar";
389387
private static final String SET_SCREEN_OFF_TIMEOUT_KEY = "set_screen_off_timeout";
390-
391388
private static final String SET_TIME_KEY = "set_time";
392389
private static final String SET_TIME_ZONE_KEY = "set_time_zone";
393-
394390
private static final String SET_PROFILE_NAME_KEY = "set_profile_name";
395-
396391
private static final String MANAGE_OVERRIDE_APN_KEY = "manage_override_apn";
397-
398392
private static final String MANAGED_SYSTEM_UPDATES_KEY = "managed_system_updates";
399-
400393
private static final String SET_PRIVATE_DNS_MODE_KEY = "set_private_dns_mode";
401-
402394
private static final String FACTORY_RESET_ORG_OWNED_DEVICE = "factory_reset_org_owned_device";
403-
404395
private static final String SET_LOCATION_ENABLED_KEY = "set_location_enabled";
405396
private static final String SET_LOCATION_MODE_KEY = "set_location_mode";
397+
private static final String SUSPEND_PERSONAL_APPS_KEY = "suspend_personal_apps";
406398

407399
private static final String BATTERY_PLUGGED_ANY = Integer.toString(
408400
BatteryManager.BATTERY_PLUGGED_AC |
@@ -485,6 +477,7 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
485477
private DpcSwitchPreference mSetLocationModePreference;
486478

487479
private DpcPreference mUserRestrictionsParentPreference;
480+
private DpcSwitchPreference mSuspendPersonalApps;
488481

489482
private GetAccessibilityServicesTask mGetAccessibilityServicesTask = null;
490483
private GetInputMethodsTask mGetInputMethodsTask = null;
@@ -736,6 +729,10 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
736729
mSetLocationModePreference = (DpcSwitchPreference) findPreference(SET_LOCATION_MODE_KEY);
737730
mSetLocationModePreference.setOnPreferenceChangeListener(this);
738731

732+
mSuspendPersonalApps = (DpcSwitchPreference) findPreference(SUSPEND_PERSONAL_APPS_KEY);
733+
mSuspendPersonalApps.setOnPreferenceChangeListener(this);
734+
mSuspendPersonalApps.setCustomConstraint(
735+
this::validateProfileOwnerOfOrganizationOwnedDevice);
739736

740737
onCreateSetNewPasswordWithComplexityPreference();
741738
constrainSpecialCasePreferences();
@@ -756,6 +753,40 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
756753
reloadSetAutoTimeZoneUi();
757754
reloadEnableLogoutUi();
758755
reloadAutoBrightnessUi();
756+
reloadPersonalAppsSuspendedUi();
757+
}
758+
759+
@TargetApi(Util.R_VERSION_CODE)
760+
private void reloadPersonalAppsSuspendedUi() {
761+
// TODO: nuke it when R sdk is available
762+
final int PERSONAL_APPS_NOT_SUSPENDED = 0;
763+
if (mSuspendPersonalApps.isEnabled()) {
764+
int suspendReasons = getPersonalAppsSuspensionReasons();
765+
mSuspendPersonalApps.setChecked(suspendReasons != 0);
766+
}
767+
}
768+
769+
// TODO: nuke it when R sdk is available.
770+
int getPersonalAppsSuspensionReasons() {
771+
try {
772+
return (Integer) ReflectionUtil.invoke(mDevicePolicyManager,
773+
"getPersonalAppsSuspendedReasons", new Class<?>[]{ComponentName.class},
774+
mAdminComponentName);
775+
} catch (ReflectionIsTemporaryException e) {
776+
Log.e(TAG, "Error invoking getPersonalAppsSuspendedReasons", e);
777+
return 0;
778+
}
779+
}
780+
781+
// TODO: nuke it when R sdk is available.
782+
void setPersonalAppsSuspended(boolean suspended) {
783+
try {
784+
ReflectionUtil.invoke(mDevicePolicyManager, "setPersonalAppsSuspended",
785+
new Class<?>[]{ComponentName.class, boolean.class},
786+
mAdminComponentName, suspended);
787+
} catch (ReflectionIsTemporaryException e) {
788+
Log.e(TAG, "Error invoking setPersonalAppsSuspended", e);
789+
}
759790
}
760791

761792
private void onCreateSetNewPasswordWithComplexityPreference() {
@@ -1453,6 +1484,10 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
14531484
reloadLocationEnabledUi();
14541485
reloadLocationModeUi();
14551486
return true;
1487+
case SUSPEND_PERSONAL_APPS_KEY:
1488+
setPersonalAppsSuspended((Boolean) newValue);
1489+
reloadPersonalAppsSuspendedUi();
1490+
return true;
14561491
}
14571492
return false;
14581493
}
@@ -3826,15 +3861,25 @@ private int validateDeviceOwnerBeforeP() {
38263861
return NO_CUSTOM_CONSTRIANT;
38273862
}
38283863

3864+
// TODO: nuke it when R sdk is available
3865+
private boolean isOrganizationOwnedDeviceWithManagedProfile() {
3866+
try {
3867+
return (Boolean) ReflectionUtil.invoke(
3868+
mDevicePolicyManager, "isOrganizationOwnedDeviceWithManagedProfile");
3869+
} catch (ReflectionIsTemporaryException e) {
3870+
Log.e(TAG, "Error invoking isOrganizationOwnedDeviceWithManagedProfile", e);
3871+
return false;
3872+
}
3873+
}
3874+
38293875
private int validateProfileOwnerOfOrganizationOwnedDevice() {
3830-
// TODO(b/144486887): Need to check if in COPE mode
3831-
if (!Util.isManagedProfileOwner(getActivity()) || Util.SDK_INT < Util.R_VERSION_CODE) {
3876+
if (Util.SDK_INT < Util.R_VERSION_CODE || !isOrganizationOwnedDeviceWithManagedProfile()) {
38323877
return R.string.requires_profile_owner_organization_owned_device;
38333878
}
38343879
return NO_CUSTOM_CONSTRIANT;
38353880
}
38363881

3837-
abstract class ManageLockTaskListCallback {
3882+
abstract static class ManageLockTaskListCallback {
38383883
public abstract void onPositiveButtonClicked(String[] lockTaskArray);
38393884
}
38403885
}

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,9 @@
613613
<!-- Backup and Restore for Device Owner -->
614614
<string name="enable_backup_service">Enable backup service</string>
615615

616+
<!-- Suspension of personal apps -->
617+
<string name="suspend_personal_apps">Suspend personal apps</string>
618+
616619
<!-- Enterprise security logging -->
617620
<string name="enable_security_logging">Enable security logging</string>
618621
<string name="request_security_logs">Request security logs</string>

app/src/main/res/xml/device_policy_header.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,11 @@
734734
android:title="@string/factory_reset_org_owned"
735735
testdpc:admin="profileOwner"
736736
testdpc:minSdkVersion="R" />
737+
<com.afwsamples.testdpc.common.preference.DpcSwitchPreference
738+
android:key="suspend_personal_apps"
739+
android:title="@string/suspend_personal_apps"
740+
testdpc:admin="profileOwner"
741+
testdpc:minSdkVersion="R" />
737742
</PreferenceCategory>
738743

739744
<PreferenceCategory android:title="@string/transfer_ownership">

0 commit comments

Comments
 (0)