1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.intentresolver.icons; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.pm.ResolveInfo; 22 import android.graphics.drawable.Drawable; 23 import android.os.Trace; 24 import android.os.UserHandle; 25 import android.util.Log; 26 27 import com.android.intentresolver.TargetPresentationGetter; 28 import com.android.intentresolver.chooser.DisplayResolveInfo; 29 30 import java.util.function.Consumer; 31 32 class LoadIconTask extends BaseLoadIconTask { 33 private static final String TAG = "IconTask"; 34 protected final DisplayResolveInfo mDisplayResolveInfo; 35 private final UserHandle mUserHandle; 36 private final ResolveInfo mResolveInfo; 37 LoadIconTask( Context context, DisplayResolveInfo dri, UserHandle userHandle, TargetPresentationGetter.Factory presentationFactory, Consumer<Drawable> callback)38 LoadIconTask( 39 Context context, DisplayResolveInfo dri, 40 UserHandle userHandle, 41 TargetPresentationGetter.Factory presentationFactory, 42 Consumer<Drawable> callback) { 43 super(context, presentationFactory, callback); 44 mUserHandle = userHandle; 45 mDisplayResolveInfo = dri; 46 mResolveInfo = dri.getResolveInfo(); 47 } 48 49 @Override doInBackground(Void... params)50 protected Drawable doInBackground(Void... params) { 51 Trace.beginSection("app-icon"); 52 try { 53 return loadIconForResolveInfo(mResolveInfo); 54 } catch (Exception e) { 55 ComponentName componentName = mDisplayResolveInfo.getResolvedComponentName(); 56 Log.e(TAG, "Failed to load app icon for " + componentName, e); 57 return loadIconPlaceholder(); 58 } finally { 59 Trace.endSection(); 60 } 61 } 62 loadIconForResolveInfo(ResolveInfo ri)63 protected final Drawable loadIconForResolveInfo(ResolveInfo ri) { 64 // Load icons based on userHandle from ResolveInfo. If in work profile/clone profile, icons 65 // should be badged. 66 return mPresentationFactory.makePresentationGetter(ri).getIcon(ri.userHandle); 67 } 68 69 } 70