• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Context;
20 import android.content.PermissionChecker;
21 import android.content.pm.ActivityInfo;
22 import android.os.AsyncTask;
23 import android.os.Trace;
24 
25 import com.android.intentresolver.R;
26 import com.android.intentresolver.TargetPresentationGetter;
27 import com.android.intentresolver.chooser.DisplayResolveInfo;
28 
29 import java.util.function.Consumer;
30 
31 class LoadLabelTask extends AsyncTask<Void, Void, CharSequence[]> {
32     private final Context mContext;
33     private final DisplayResolveInfo mDisplayResolveInfo;
34     private final boolean mIsAudioCaptureDevice;
35     protected final TargetPresentationGetter.Factory mPresentationFactory;
36     private final Consumer<CharSequence[]> mCallback;
37 
LoadLabelTask(Context context, DisplayResolveInfo dri, boolean isAudioCaptureDevice, TargetPresentationGetter.Factory presentationFactory, Consumer<CharSequence[]> callback)38     LoadLabelTask(Context context, DisplayResolveInfo dri,
39             boolean isAudioCaptureDevice, TargetPresentationGetter.Factory presentationFactory,
40             Consumer<CharSequence[]> callback) {
41         mContext = context;
42         mDisplayResolveInfo = dri;
43         mIsAudioCaptureDevice = isAudioCaptureDevice;
44         mPresentationFactory = presentationFactory;
45         mCallback = callback;
46     }
47 
48     @Override
doInBackground(Void... voids)49     protected CharSequence[] doInBackground(Void... voids) {
50         try {
51             Trace.beginSection("app-label");
52             return loadLabel();
53         } finally {
54             Trace.endSection();
55         }
56     }
57 
loadLabel()58     private CharSequence[] loadLabel() {
59         TargetPresentationGetter pg = mPresentationFactory.makePresentationGetter(
60                 mDisplayResolveInfo.getResolveInfo());
61 
62         if (mIsAudioCaptureDevice) {
63             // This is an audio capture device, so check record permissions
64             ActivityInfo activityInfo = mDisplayResolveInfo.getResolveInfo().activityInfo;
65             String packageName = activityInfo.packageName;
66 
67             int uid = activityInfo.applicationInfo.uid;
68             boolean hasRecordPermission =
69                     PermissionChecker.checkPermissionForPreflight(
70                             mContext,
71                             android.Manifest.permission.RECORD_AUDIO, -1, uid,
72                             packageName)
73                             == android.content.pm.PackageManager.PERMISSION_GRANTED;
74 
75             if (!hasRecordPermission) {
76                 // Doesn't have record permission, so warn the user
77                 return new CharSequence[]{
78                         pg.getLabel(),
79                         mContext.getString(R.string.usb_device_resolve_prompt_warn)
80                 };
81             }
82         }
83 
84         return new CharSequence[]{
85                 pg.getLabel(),
86                 pg.getSubLabel()
87         };
88     }
89 
90     @Override
onPostExecute(CharSequence[] result)91     protected void onPostExecute(CharSequence[] result) {
92         mCallback.accept(result);
93     }
94 }
95