149
149
import java .util .List ;
150
150
import java .util .Set ;
151
151
import java .util .TimeZone ;
152
+ import java .util .concurrent .Executor ;
152
153
import java .util .stream .Collectors ;
153
154
154
155
import static android .os .UserManager .DISALLOW_INSTALL_UNKNOWN_SOURCES ;
@@ -382,6 +383,9 @@ public class PolicyManagementFragment extends BaseSearchablePolicyPreferenceFrag
382
383
public static final String GMSCORE_BACKUP_TRANSPORT =
383
384
"com.google.android.gms.backup.BackupTransportService" ;
384
385
386
+ private static final int USER_OPERATION_ERROR_UNKNOWN = 1 ;
387
+ private static final int USER_OPERATION_SUCCESS = 0 ;
388
+
385
389
private DevicePolicyManager mDevicePolicyManager ;
386
390
private PackageManager mPackageManager ;
387
391
private String mPackageName ;
@@ -1844,17 +1848,17 @@ private void showSwitchUserPrompt() {
1844
1848
@ TargetApi (28 )
1845
1849
private void showStartUserInBackgroundPrompt () {
1846
1850
showChooseUserPrompt (R .string .start_user_in_background , userHandle -> {
1847
- boolean success = false ;
1851
+ int status = USER_OPERATION_ERROR_UNKNOWN ;
1848
1852
try {
1849
- success = (boolean ) ReflectionUtil .invoke (
1853
+ status = (int ) ReflectionUtil .invoke (
1850
1854
mDevicePolicyManager ,
1851
1855
"startUserInBackground" ,
1852
1856
mAdminComponentName ,
1853
1857
userHandle );
1854
1858
} catch (ReflectionUtil .ReflectionIsTemporaryException e ) {
1855
1859
Log .e (TAG ,"Can't invoke startUserInBackground" , e );
1856
1860
}
1857
- showToast (success
1861
+ showToast (status == USER_OPERATION_SUCCESS
1858
1862
? R .string .user_started_in_background
1859
1863
: R .string .failed_to_start_user_in_background );
1860
1864
});
@@ -1867,9 +1871,18 @@ private void showStartUserInBackgroundPrompt() {
1867
1871
@ TargetApi (28 )
1868
1872
private void showStopUserPrompt () {
1869
1873
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 );
1873
1886
});
1874
1887
}
1875
1888
@@ -1908,8 +1921,17 @@ private void showChooseUserPrompt(int titleResId, UserCallback callback) {
1908
1921
*/
1909
1922
@ TargetApi (28 )
1910
1923
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 );
1913
1935
}
1914
1936
1915
1937
/**
@@ -2826,14 +2848,21 @@ private void showClearAppDataPrompt() {
2826
2848
2827
2849
@ TargetApi (28 )
2828
2850
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 );
2837
2866
}
2838
2867
}
2839
2868
0 commit comments