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.packageinstaller.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.Bundle;
25 import android.os.UserHandle;
26 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 
30 import com.android.packageinstaller.role.utils.UserUtils;
31 
32 import java.util.List;
33 
34 /**
35  * Specifies a required {@code ContentProvider} for an application to qualify for a {@link Role}.
36  */
37 public class RequiredContentProvider extends RequiredComponent {
38 
RequiredContentProvider(@onNull IntentFilterData intentFilterData, @Nullable String permission, @NonNull List<RequiredMetaData> metaData)39     public RequiredContentProvider(@NonNull IntentFilterData intentFilterData,
40             @Nullable String permission, @NonNull List<RequiredMetaData> metaData) {
41         super(intentFilterData, permission, metaData);
42     }
43 
44     @NonNull
45     @Override
queryIntentComponentsAsUser(@onNull Intent intent, int flags, @NonNull UserHandle user, @NonNull Context context)46     protected List<ResolveInfo> queryIntentComponentsAsUser(@NonNull Intent intent, int flags,
47             @NonNull UserHandle user, @NonNull Context context) {
48         Context userContext = UserUtils.getUserContext(context, user);
49         PackageManager userPackageManager = userContext.getPackageManager();
50         return userPackageManager.queryIntentContentProviders(intent, flags);
51     }
52 
53     @NonNull
54     @Override
getComponentComponentName(@onNull ResolveInfo resolveInfo)55     protected ComponentName getComponentComponentName(@NonNull ResolveInfo resolveInfo) {
56         return new ComponentName(resolveInfo.providerInfo.packageName,
57                 resolveInfo.providerInfo.name);
58     }
59 
60     @Nullable
61     @Override
getComponentPermission(@onNull ResolveInfo resolveInfo)62     protected String getComponentPermission(@NonNull ResolveInfo resolveInfo) {
63         // TODO: Which permission? Or both?
64         //return resolveInfo.providerInfo.readPermission;
65         throw new UnsupportedOperationException();
66     }
67 
68     @Nullable
69     @Override
getComponentMetaData(@onNull ResolveInfo resolveInfo)70     protected Bundle getComponentMetaData(@NonNull ResolveInfo resolveInfo) {
71         return resolveInfo.providerInfo.metaData;
72     }
73 }
74