Skip to content

Commit 58d7b4d

Browse files
TreeHugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "Use CrossProfileAppsAPI directly" into ub-testdpc-pic
2 parents 35994a4 + 851f4f8 commit 58d7b4d

File tree

1 file changed

+14
-37
lines changed

1 file changed

+14
-37
lines changed

app/src/main/java/com/afwsamples/testdpc/CrossProfileAppsFragment.java

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,14 @@
33
import android.annotation.TargetApi;
44
import android.app.Fragment;
55
import android.content.ComponentName;
6-
import android.content.Context;
76
import android.content.pm.CrossProfileApps;
8-
import android.graphics.drawable.Drawable;
97
import android.os.Bundle;
108
import android.os.UserHandle;
11-
import android.util.Log;
129
import android.view.LayoutInflater;
1310
import android.view.View;
1411
import android.view.ViewGroup;
1512
import android.widget.ImageView;
1613
import android.widget.TextView;
17-
18-
import com.afwsamples.testdpc.common.ReflectionUtil;
19-
2014
import java.util.List;
2115

2216
// TODO: Replace it with P.
@@ -28,7 +22,7 @@ public class CrossProfileAppsFragment extends Fragment {
2822
private TextView mSwitchProfileTextView;
2923
private TextView mDescriptionTextView;
3024
private ImageView mSwitchProfileImageView;
31-
private Object mCrossProfileApps;
25+
private CrossProfileApps mCrossProfileApps;
3226

3327
@Override
3428
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -43,7 +37,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
4337
@Override
4438
public void onActivityCreated(Bundle savedInstanceState) {
4539
super.onActivityCreated(savedInstanceState);
46-
mCrossProfileApps = getActivity().getSystemService(Context.CROSS_PROFILE_APPS_SERVICE);
40+
mCrossProfileApps = getActivity().getSystemService(CrossProfileApps.class);
4741
}
4842

4943
@Override
@@ -53,18 +47,11 @@ public void onResume() {
5347
}
5448

5549
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));
6855
}
6956
}
7057

@@ -75,24 +62,14 @@ private void showNoTargetUserUi() {
7562
mSwitchProfileImageView.setOnClickListener(null);
7663
}
7764

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));
8569
mDescriptionTextView.setText(R.string.cross_profile_apps_available);
8670
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));
9774
}
9875
}

0 commit comments

Comments
 (0)