1 /*
2  * Copyright (C) 2018 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.permissioncontroller.role.model;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.os.UserHandle;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 
29 import com.android.permissioncontroller.role.utils.UserUtils;
30 
31 import java.util.List;
32 
33 /**
34  * Specifies a required {@code Activity} for an application to qualify for a {@link Role}.
35  */
36 public class RequiredActivity extends RequiredComponent {
37 
RequiredActivity(@onNull IntentFilterData intentFilterData, @Nullable String permission)38     public RequiredActivity(@NonNull IntentFilterData intentFilterData,
39             @Nullable String permission) {
40         super(intentFilterData, permission);
41     }
42 
43     @NonNull
44     @Override
queryIntentComponentsAsUser(@onNull Intent intent, int flags, @NonNull UserHandle user, @NonNull Context context)45     protected List<ResolveInfo> queryIntentComponentsAsUser(@NonNull Intent intent, int flags,
46             @NonNull UserHandle user, @NonNull Context context) {
47         Context userContext = UserUtils.getUserContext(context, user);
48         PackageManager userPackageManager = userContext.getPackageManager();
49         flags |= PackageManager.MATCH_DEFAULT_ONLY;
50         return userPackageManager.queryIntentActivities(intent, flags);
51     }
52 
53     @NonNull
54     @Override
getComponentComponentName(@onNull ResolveInfo resolveInfo)55     protected ComponentName getComponentComponentName(@NonNull ResolveInfo resolveInfo) {
56         return new ComponentName(resolveInfo.activityInfo.packageName,
57                 resolveInfo.activityInfo.name);
58     }
59 
60     @Nullable
61     @Override
getComponentPermission(@onNull ResolveInfo resolveInfo)62     protected String getComponentPermission(@NonNull ResolveInfo resolveInfo) {
63         return resolveInfo.activityInfo.permission;
64     }
65 }
66