3
3
import android .annotation .TargetApi ;
4
4
import android .app .Fragment ;
5
5
import android .content .ComponentName ;
6
- import android .content .Context ;
7
6
import android .content .pm .CrossProfileApps ;
8
- import android .graphics .drawable .Drawable ;
9
7
import android .os .Bundle ;
10
8
import android .os .UserHandle ;
11
- import android .util .Log ;
12
9
import android .view .LayoutInflater ;
13
10
import android .view .View ;
14
11
import android .view .ViewGroup ;
15
12
import android .widget .ImageView ;
16
13
import android .widget .TextView ;
17
-
18
- import com .afwsamples .testdpc .common .ReflectionUtil ;
19
-
20
14
import java .util .List ;
21
15
22
16
// TODO: Replace it with P.
@@ -28,7 +22,7 @@ public class CrossProfileAppsFragment extends Fragment {
28
22
private TextView mSwitchProfileTextView ;
29
23
private TextView mDescriptionTextView ;
30
24
private ImageView mSwitchProfileImageView ;
31
- private Object mCrossProfileApps ;
25
+ private CrossProfileApps mCrossProfileApps ;
32
26
33
27
@ Override
34
28
public View onCreateView (LayoutInflater inflater , ViewGroup container ,
@@ -43,7 +37,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
43
37
@ Override
44
38
public void onActivityCreated (Bundle savedInstanceState ) {
45
39
super .onActivityCreated (savedInstanceState );
46
- mCrossProfileApps = getActivity ().getSystemService (Context . CROSS_PROFILE_APPS_SERVICE );
40
+ mCrossProfileApps = getActivity ().getSystemService (CrossProfileApps . class );
47
41
}
48
42
49
43
@ Override
@@ -53,18 +47,11 @@ public void onResume() {
53
47
}
54
48
55
49
private void refreshUi () {
56
- try {
57
- List <UserHandle > targetUserProfiles =
58
- (List <UserHandle >) ReflectionUtil .invoke (
59
- mCrossProfileApps ,
60
- "getTargetUserProfiles" );
61
- if (targetUserProfiles .isEmpty ()) {
62
- showNoTargetUserUi ();
63
- } else {
64
- showHasTargetUserUi (targetUserProfiles .get (0 ));
65
- }
66
- } catch (ReflectionUtil .ReflectionIsTemporaryException ex ) {
67
- Log .e (TAG , "Failed to call CrossProfileApps API: " , ex );
50
+ List <UserHandle > targetUserProfiles = mCrossProfileApps .getTargetUserProfiles ();
51
+ if (targetUserProfiles .isEmpty ()) {
52
+ showNoTargetUserUi ();
53
+ } else {
54
+ showHasTargetUserUi (targetUserProfiles .get (0 ));
68
55
}
69
56
}
70
57
@@ -75,24 +62,14 @@ private void showNoTargetUserUi() {
75
62
mSwitchProfileImageView .setOnClickListener (null );
76
63
}
77
64
78
- private void showHasTargetUserUi (UserHandle userHandle )
79
- throws ReflectionUtil .ReflectionIsTemporaryException {
80
- mSwitchProfileTextView .setText ((String )
81
- ReflectionUtil .invoke (
82
- mCrossProfileApps , "getProfileSwitchingLabel" , userHandle ));
83
- mSwitchProfileImageView .setImageDrawable ((Drawable ) ReflectionUtil .invoke (
84
- mCrossProfileApps , "getProfileSwitchingIconDrawable" , userHandle ));
65
+ private void showHasTargetUserUi (UserHandle userHandle ) {
66
+ mSwitchProfileTextView .setText (mCrossProfileApps .getProfileSwitchingLabel (userHandle ));
67
+ mSwitchProfileImageView .setImageDrawable (
68
+ mCrossProfileApps .getProfileSwitchingIconDrawable (userHandle ));
85
69
mDescriptionTextView .setText (R .string .cross_profile_apps_available );
86
70
mSwitchProfileImageView .setOnClickListener (
87
- view -> {
88
- try {
89
- ReflectionUtil .invoke (
90
- mCrossProfileApps , "startMainActivity" ,
91
- new ComponentName (getActivity (), PolicyManagementActivity .class ),
92
- userHandle );
93
- } catch (ReflectionUtil .ReflectionIsTemporaryException e ) {
94
- e .printStackTrace ();
95
- }
96
- });
71
+ view ->
72
+ mCrossProfileApps .startMainActivity (
73
+ new ComponentName (getActivity (), PolicyManagementActivity .class ), userHandle ));
97
74
}
98
75
}
0 commit comments