Skip to content

Commit 3e571ea

Browse files
Clean up reflections for COSU APIs
Bug: 73066020 Change-Id: I7454b226ed03b7f0501889bf099d544a3971e747
1 parent b99b21b commit 3e571ea

File tree

1 file changed

+12
-66
lines changed

1 file changed

+12
-66
lines changed

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

Lines changed: 12 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
import com.afwsamples.testdpc.common.BaseSearchablePolicyPreferenceFragment;
9191
import com.afwsamples.testdpc.common.CertificateUtil;
9292
import com.afwsamples.testdpc.common.MediaDisplayFragment;
93-
import com.afwsamples.testdpc.common.ReflectionUtil;
9493
import com.afwsamples.testdpc.common.UserArrayAdapter;
9594
import com.afwsamples.testdpc.common.Util;
9695
import com.afwsamples.testdpc.common.preference.DpcPreference;
@@ -149,7 +148,6 @@
149148
import java.util.List;
150149
import java.util.Set;
151150
import java.util.TimeZone;
152-
import java.util.concurrent.Executor;
153151
import java.util.stream.Collectors;
154152

155153
import static android.os.UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
@@ -1854,16 +1852,7 @@ private void showSwitchUserPrompt() {
18541852
@TargetApi(28)
18551853
private void showStartUserInBackgroundPrompt() {
18561854
showChooseUserPrompt(R.string.start_user_in_background, userHandle -> {
1857-
int status = USER_OPERATION_ERROR_UNKNOWN;
1858-
try {
1859-
status = (int) ReflectionUtil.invoke(
1860-
mDevicePolicyManager,
1861-
"startUserInBackground",
1862-
mAdminComponentName,
1863-
userHandle);
1864-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
1865-
Log.e(TAG,"Can't invoke startUserInBackground", e);
1866-
}
1855+
int status = mDevicePolicyManager.startUserInBackground(mAdminComponentName, userHandle);
18671856
showToast(status == USER_OPERATION_SUCCESS
18681857
? R.string.user_started_in_background
18691858
: R.string.failed_to_start_user_in_background);
@@ -1877,16 +1866,7 @@ private void showStartUserInBackgroundPrompt() {
18771866
@TargetApi(28)
18781867
private void showStopUserPrompt() {
18791868
showChooseUserPrompt(R.string.stop_user, userHandle -> {
1880-
int status = USER_OPERATION_ERROR_UNKNOWN;
1881-
try {
1882-
status = (int) ReflectionUtil.invoke(
1883-
mDevicePolicyManager,
1884-
"stopUser",
1885-
mAdminComponentName,
1886-
userHandle);
1887-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
1888-
Log.e(TAG,"Can't invoke stopUser", e);
1889-
}
1869+
int status = mDevicePolicyManager.stopUser(mAdminComponentName, userHandle);
18901870
showToast(status == USER_OPERATION_SUCCESS ? R.string.user_stopped
18911871
: R.string.failed_to_stop_user);
18921872
});
@@ -1927,15 +1907,7 @@ private void showChooseUserPrompt(int titleResId, UserCallback callback) {
19271907
*/
19281908
@TargetApi(28)
19291909
private void logoutUser() {
1930-
int status = USER_OPERATION_ERROR_UNKNOWN;
1931-
try {
1932-
status = (int) ReflectionUtil.invoke(
1933-
mDevicePolicyManager,
1934-
"logoutUser",
1935-
mAdminComponentName);
1936-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
1937-
Log.e(TAG,"Can't invoke logoutUser", e);
1938-
}
1910+
int status = mDevicePolicyManager.logoutUser(mAdminComponentName);
19391911
showToast(status == USER_OPERATION_SUCCESS ? R.string.user_logouted
19401912
: R.string.failed_to_logout_user);
19411913
}
@@ -2849,22 +2821,14 @@ private void showClearAppDataPrompt() {
28492821

28502822
@TargetApi(28)
28512823
private void clearApplicationUserData(String packageName) {
2852-
try {
2853-
ReflectionUtil.invoke(
2854-
mDevicePolicyManager, "clearApplicationUserData",
2855-
new Class[]{ComponentName.class, String.class, Executor.class,
2856-
DevicePolicyManager.OnClearApplicationUserDataListener.class},
2857-
mAdminComponentName,
2858-
packageName,
2859-
new MainThreadExecutor(),
2860-
(DevicePolicyManager.OnClearApplicationUserDataListener)
2861-
(__, succeed) -> showToast(
2862-
succeed ? R.string.clear_app_data_success
2863-
: R.string.clear_app_data_failure,
2864-
packageName));
2865-
} catch (ReflectionUtil.ReflectionIsTemporaryException e) {
2866-
Log.e(LOG_TAG, "cannot invoke clearApplicationUserData", e);
2867-
}
2824+
mDevicePolicyManager.clearApplicationUserData(
2825+
mAdminComponentName,
2826+
packageName,
2827+
new MainThreadExecutor(),
2828+
(__, succeed) -> showToast(succeed ?
2829+
R.string.clear_app_data_success
2830+
: R.string.clear_app_data_failure,
2831+
packageName));
28682832
}
28692833

28702834
@TargetApi(Build.VERSION_CODES.N)
@@ -3154,25 +3118,7 @@ private void relaunchInLockTaskMode() {
31543118
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
31553119

31563120
final ActivityOptions options = ActivityOptions.makeBasic();
3157-
try {
3158-
ReflectionUtil.invoke(
3159-
options,
3160-
"setLockTaskMode",
3161-
new Class[]{boolean.class},
3162-
true);
3163-
} catch (ReflectionUtil.ReflectionIsTemporaryException e1) {
3164-
// API name was recently changed, so we should be robust enough to support both.
3165-
// This workaround can be removed once the new API name is in the SDK prebuilt.
3166-
try {
3167-
ReflectionUtil.invoke(
3168-
options,
3169-
"setLockTaskEnabled",
3170-
new Class[]{boolean.class},
3171-
true);
3172-
} catch (ReflectionUtil.ReflectionIsTemporaryException e2) {
3173-
Log.e(TAG, "Unable to set lock task activity option.", e2);
3174-
}
3175-
}
3121+
options.setLockTaskMode(true);
31763122

31773123
try {
31783124
startActivity(intent, options.toBundle());

0 commit comments

Comments
 (0)