Skip to content

Commit bd49f01

Browse files
Make TestDPC use reflection for updated COSU APIs
Test: startuserInBackground, stopUser, logoutUser and clearApplicationUserData can be invoked Bug: 73066020 Change-Id: Iec61f9355d0a2b054ed3098afbf61a7a5a72354b
1 parent 26ac167 commit bd49f01

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

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

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
import java.util.List;
150150
import java.util.Set;
151151
import java.util.TimeZone;
152+
import java.util.concurrent.Executor;
152153
import java.util.stream.Collectors;
153154

154155
import static android.os.UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
@@ -382,6 +383,9 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
382383
public static final String GMSCORE_BACKUP_TRANSPORT =
383384
"com.google.android.gms.backup.BackupTransportService";
384385

386+
private static final int USER_OPERATION_ERROR_UNKNOWN = 1;
387+
private static final int USER_OPERATION_SUCCESS = 0;
388+
385389
private DevicePolicyManager mDevicePolicyManager;
386390
private PackageManager mPackageManager;
387391
private String mPackageName;
@@ -1844,17 +1848,17 @@ private void showSwitchUserPrompt() {
18441848
@TargetApi(28)
18451849
private void showStartUserInBackgroundPrompt() {
18461850
showChooseUserPrompt(R.string.start_user_in_background, userHandle -> {
1847-
boolean success = false;
1851+
int status = USER_OPERATION_ERROR_UNKNOWN;
18481852
try {
1849-
success = (boolean) ReflectionUtil.invoke(
1853+
status = (int) ReflectionUtil.invoke(
18501854
mDevicePolicyManager,
18511855
"startUserInBackground",
18521856
mAdminComponentName,
18531857
userHandle);
18541858
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
18551859
Log.e(TAG,"Can't invoke startUserInBackground", e);
18561860
}
1857-
showToast(success
1861+
showToast(status == USER_OPERATION_SUCCESS
18581862
? R.string.user_started_in_background
18591863
: R.string.failed_to_start_user_in_background);
18601864
});
@@ -1867,9 +1871,18 @@ private void showStartUserInBackgroundPrompt() {
18671871
@TargetApi(28)
18681872
private void showStopUserPrompt() {
18691873
showChooseUserPrompt(R.string.stop_user, userHandle -> {
1870-
boolean success =
1871-
mDevicePolicyManager.stopUser(mAdminComponentName, userHandle);
1872-
showToast(success ? R.string.user_stopped : R.string.failed_to_stop_user);
1874+
int status = USER_OPERATION_ERROR_UNKNOWN;
1875+
try {
1876+
status = (int) ReflectionUtil.invoke(
1877+
mDevicePolicyManager,
1878+
"stopUser",
1879+
mAdminComponentName,
1880+
userHandle);
1881+
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
1882+
Log.e(TAG,"Can't invoke stopUser", e);
1883+
}
1884+
showToast(status == USER_OPERATION_SUCCESS ? R.string.user_stopped
1885+
: R.string.failed_to_stop_user);
18731886
});
18741887
}
18751888

@@ -1908,8 +1921,17 @@ private void showChooseUserPrompt(int titleResId, UserCallback callback) {
19081921
*/
19091922
@TargetApi(28)
19101923
private void logoutUser() {
1911-
boolean success = mDevicePolicyManager.logoutUser(mAdminComponentName);
1912-
showToast(success ? R.string.user_logouted : R.string.failed_to_logout_user);
1924+
int status = USER_OPERATION_ERROR_UNKNOWN;
1925+
try {
1926+
status = (int) ReflectionUtil.invoke(
1927+
mDevicePolicyManager,
1928+
"logoutUser",
1929+
mAdminComponentName);
1930+
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
1931+
Log.e(TAG,"Can't invoke logoutUser", e);
1932+
}
1933+
showToast(status == USER_OPERATION_SUCCESS ? R.string.user_logouted
1934+
: R.string.failed_to_logout_user);
19131935
}
19141936

19151937
/**
@@ -2826,14 +2848,21 @@ private void showClearAppDataPrompt() {
28262848

28272849
@TargetApi(28)
28282850
private void clearApplicationUserData(String packageName) {
2829-
if (!mDevicePolicyManager.clearApplicationUserData(
2830-
mAdminComponentName,
2831-
packageName,
2832-
new MainThreadExecutor(),
2833-
(__, succeed) -> showToast(
2834-
succeed ? R.string.clear_app_data_success : R.string.clear_app_data_failure,
2835-
packageName))) {
2836-
showToast(R.string.clear_app_data_failure, packageName);
2851+
try {
2852+
ReflectionUtil.invoke(
2853+
mDevicePolicyManager, "clearApplicationUserData",
2854+
new Class[]{ComponentName.class, String.class, Executor.class,
2855+
DevicePolicyManager.OnClearApplicationUserDataListener.class},
2856+
mAdminComponentName,
2857+
packageName,
2858+
new MainThreadExecutor(),
2859+
(DevicePolicyManager.OnClearApplicationUserDataListener)
2860+
(__, succeed) -> showToast(
2861+
succeed ? R.string.clear_app_data_success
2862+
: R.string.clear_app_data_failure,
2863+
packageName));
2864+
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
2865+
Log.e(LOG_TAG, "cannot invoke clearApplicationUserData", e);
28372866
}
28382867
}
28392868

0 commit comments

Comments
 (0)