package com.afwsamples.testdpc;

import android.annotation.TargetApi;
import android.app.Fragment;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.CrossProfileApps;
import android.graphics.drawable.Drawable;
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 Object 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(Context.CROSS_PROFILE_APPS_SERVICE);
    }

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

    private void refreshUi() {
        try {
            List<UserHandle> targetUserProfiles =
                    (List<UserHandle>) ReflectionUtil.invoke(
                            mCrossProfileApps,
                            "getTargetUserProfiles");
            if (targetUserProfiles.isEmpty()) {
                showNoTargetUserUi();
            } else {
                showHasTargetUserUi(targetUserProfiles.get(0));
            }
        } catch (ReflectionUtil.ReflectionIsTemporaryException ex) {
            Log.e(TAG, "Failed to call CrossProfileApps API: ", ex);
        }
    }

    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)
            throws ReflectionUtil.ReflectionIsTemporaryException {
        mSwitchProfileTextView.setText((String)
                ReflectionUtil.invoke(
                        mCrossProfileApps, "getProfileSwitchingLabel", userHandle));
        mSwitchProfileImageView.setImageDrawable((Drawable) ReflectionUtil.invoke(
                mCrossProfileApps, "getProfileSwitchingIconDrawable", userHandle));
        mDescriptionTextView.setText(R.string.cross_profile_apps_available);
        mSwitchProfileImageView.setOnClickListener(
                view -> {
                    try {
                        ReflectionUtil.invoke(
                                mCrossProfileApps, "startMainActivity",
                                new ComponentName(getActivity(), PolicyManagementActivity.class),
                                userHandle);
                    } catch (ReflectionUtil.ReflectionIsTemporaryException e) {
                        e.printStackTrace();
                    }
                });
    }
}

@@ -157,102 +157,27 @@
 ent.
-Context;%0Aimport android.content.pm.CrossProfileApps;%0Aimport android.graphics.drawable.Drawable
+pm.CrossProfileApps
 ;%0Aim
@@ -234,33 +234,8 @@
 le;%0A
-import android.util.Log;%0A
 impo
@@ -392,63 +392,8 @@
 ew;%0A
-%0Aimport com.afwsamples.testdpc.common.ReflectionUtil;%0A%0A
 impo
@@ -761,14 +761,24 @@
 ate 
-Object
+CrossProfileApps
  mCr
@@ -1521,41 +1521,29 @@
 ce(C
-ontext.CROSS_PROFILE_APPS_SERVICE
+rossProfileApps.class
 );%0A 
@@ -1676,34 +1676,16 @@
 hUi() %7B%0A
-        try %7B%0A    
         
@@ -1725,147 +1725,27 @@
 es =
-%0A                    (List%3CUserHandle%3E) ReflectionUtil.invoke(%0A                            mCrossProfileApps,%0A                            %22
+ mCrossProfileApps.
 getT
@@ -1761,24 +1761,20 @@
 Profiles
-%22
+(
 );%0A
-    
         
@@ -1817,28 +1817,24 @@
             
-    
 showNoTarget
@@ -1851,20 +1851,16 @@
         
-   
- 
 %7D else %7B
@@ -1860,20 +1860,16 @@
  else %7B%0A
-    
         
@@ -1924,160 +1924,8 @@
 ));%0A
-            %7D%0A        %7D catch (ReflectionUtil.ReflectionIsTemporaryException ex) %7B%0A            Log.e(TAG, %22Failed to call CrossProfileApps API: %22, ex);%0A
     
@@ -2286,187 +2286,50 @@
 dle)
-%0A            throws ReflectionUtil.ReflectionIsTemporaryException %7B%0A        mSwitchProfileTextView.setText((String)%0A                ReflectionUtil.invoke(%0A                        
+ %7B%0A        mSwitchProfileTextView.setText(
 mCro
@@ -2333,35 +2333,33 @@
 CrossProfileApps
-, %22
+.
 getProfileSwitch
@@ -2366,19 +2366,17 @@
 ingLabel
-%22, 
+(
 userHand
@@ -2434,44 +2434,9 @@
 ble(
-(Drawable) ReflectionUtil.invoke(%0A  
+%0A
     
@@ -2435,34 +2435,32 @@
 le(%0A            
-  
 mCrossProfileApp
@@ -2460,19 +2460,17 @@
 fileApps
-, %22
+.
 getProfi
@@ -2496,11 +2496,9 @@
 able
-%22, 
+(
 user
@@ -2652,111 +2652,16 @@
     
-    
 view -%3E
- %7B%0A                    try %7B%0A                        ReflectionUtil.invoke(%0A                
+%0A
     
@@ -2693,11 +2693,9 @@
 Apps
-, %22
+.
 star
@@ -2711,18 +2711,10 @@
 vity
-%22,%0A       
+(%0A
     
@@ -2729,21 +2729,16 @@
         
-     
 new Comp
@@ -2798,218 +2798,20 @@
 ss),
-%0A                                userHandle);%0A                    %7D catch (ReflectionUtil.ReflectionIsTemporaryException e) %7B%0A                        e.printStackTrace();%0A                    %7D%0A                %7D
+ userHandle)
 );%0A 

Use CrossProfileAppsAPI directly