package com.afwsamples.testdpc;

import android.annotation.TargetApi;
import android.app.Fragment;
import android.content.ComponentName;
import android.content.pm.crossprofile.CrossProfileApps;
import android.os.Bundle;
import android.os.UserHandle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.afwsamples.testdpc.common.ReflectionUtil;

import java.util.List;

// TODO: Replace it with P.
@TargetApi(28)
public class CrossProfileAppsFragment extends Fragment {
    private static final String TAG = "CrossProfileAppsFragmen";

    private View mInflatedView;
    private TextView mSwitchProfileTextView;
    private TextView mDescriptionTextView;
    private ImageView mSwitchProfileImageView;
    private CrossProfileApps mCrossProfileApps;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mInflatedView = inflater.inflate(R.layout.cross_profile_apps, container, false);
        mSwitchProfileTextView = mInflatedView.findViewById(R.id.cross_profile_app_label);
        mSwitchProfileImageView = mInflatedView.findViewById(R.id.cross_profile_app_icon);
        mDescriptionTextView = mInflatedView.findViewById(R.id.cross_profile_app_description);
        return mInflatedView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mCrossProfileApps = getActivity().getSystemService(CrossProfileApps.class);
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshUi();
    }

    private void refreshUi() {
        List<UserHandle> targetUserProfiles = mCrossProfileApps.getTargetUserProfiles();
        if (targetUserProfiles.isEmpty()) {
            showNoTargetUserUi();
        } else {
            showHasTargetUserUi(targetUserProfiles.get(0));
        }
    }

    private void showNoTargetUserUi() {
        mDescriptionTextView.setText(R.string.cross_profile_apps_not_available);
        mSwitchProfileTextView.setText("");
        mSwitchProfileImageView.setImageDrawable(null);
        mSwitchProfileImageView.setOnClickListener(null);
    }

    private void showHasTargetUserUi(UserHandle userHandle) {
        mSwitchProfileTextView.setText(mCrossProfileApps.getProfileSwitchingLabel(userHandle));
        mSwitchProfileImageView.setImageDrawable(
                mCrossProfileApps.getProfileSwitchingIcon(userHandle));
        mDescriptionTextView.setText(R.string.cross_profile_apps_available);
        mSwitchProfileImageView.setOnClickListener(
                view -> mCrossProfileApps.startMainActivity(
                        new ComponentName(getActivity(),
                            PolicyManagementActivity.class),
                            userHandle));
    }
}

@@ -157,40 +157,115 @@
 ent.
-pm.crossprofile.CrossProfileApps
+Context;%0Aimport android.content.pm.crossprofile.CrossProfileApps;%0Aimport android.graphics.drawable.Drawable
 ;%0Aim
@@ -925,32 +925,22 @@
 private 
-CrossProfileApps
+Object
  mCrossP
@@ -1679,29 +1679,41 @@
 ce(C
-rossProfileApps.class
+ontext.CROSS_PROFILE_APPS_SERVICE
 );%0A 
@@ -1842,24 +1842,42 @@
 freshUi() %7B%0A
+        try %7B%0A    
         List
@@ -1909,35 +1909,155 @@
 ofiles =
- mCrossProfileApps.
+%0A                    (List%3CUserHandle%3E) ReflectionUtil.invoke(%0A                            mCrossProfileApps,%0A                            %22
 getTarge
@@ -2069,17 +2069,17 @@
 Profiles
-(
+%22
 );%0A     
@@ -2081,16 +2081,20 @@
         
+    
 if (targ
@@ -2129,24 +2129,28 @@
             
+   
+ 
 showNoTarget
@@ -2167,16 +2167,20 @@
         
+    
 %7D else %7B
@@ -2188,24 +2188,28 @@
             
+ 
+   
 showHasTarge
@@ -2240,24 +2240,176 @@
 es.get(0));%0A
+            %7D%0A        %7D catch (ReflectionUtil.ReflectionIsTemporaryException ex) %7B%0A            Log.e(TAG, %22Failed to call CrossProfileApps API: %22, ex);%0A
         %7D%0A  
@@ -2762,50 +2762,187 @@
 dle)
- %7B%0A        mSwitchProfileTextView.setText(
+%0A            throws ReflectionUtil.ReflectionIsTemporaryException %7B%0A        mSwitchProfileTextView.setText((String)%0A                ReflectionUtil.invoke(%0A                        
 mCro
@@ -2946,33 +2946,35 @@
 CrossProfileApps
-.
+, %22
 getProfileSwitch
@@ -2981,17 +2981,19 @@
 ingLabel
-(
+%22, 
 userHand
@@ -3047,16 +3047,49 @@
 rawable(
+(Drawable) ReflectionUtil.invoke(
 %0A       
@@ -3114,17 +3114,19 @@
 fileApps
-.
+, %22
 getProfi
@@ -3140,17 +3140,27 @@
 hingIcon
-(
+Drawable%22, 
 userHand
@@ -3317,16 +3317,123 @@
  view -%3E
+ %7B%0A                    try %7B%0A                        ReflectionUtil.invoke(%0A                               
  mCrossP
@@ -3442,17 +3442,19 @@
 fileApps
-.
+, %22
 startMai
@@ -3462,17 +3462,18 @@
 Activity
-(
+%22,
 %0A       
@@ -3489,16 +3489,24 @@
         
+        
 new Comp
@@ -3533,36 +3533,8 @@
 y(),
-%0A                           
  Pol
@@ -3591,16 +3591,20 @@
         
+    
 userHand
@@ -3602,23 +3602,189 @@
  userHandle)
+;%0A                    %7D catch (ReflectionUtil.ReflectionIsTemporaryException e) %7B%0A                        e.printStackTrace();%0A                    %7D%0A                %7D
 );%0A    %7D%0A%7D%0A

Change according to the API change