• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.car.settings.testutils;
17 
18 import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
19 
20 import android.annotation.NonNull;
21 import android.annotation.UserIdInt;
22 import android.app.ApplicationPackageManager;
23 import android.content.ComponentName;
24 import android.content.IntentFilter;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.IPackageDataObserver;
27 import android.content.pm.ModuleInfo;
28 import android.content.pm.PackageManager;
29 import android.content.pm.ProviderInfo;
30 import android.content.pm.ResolveInfo;
31 import android.content.res.Resources;
32 import android.graphics.drawable.Drawable;
33 import android.os.UserHandle;
34 import android.util.Pair;
35 
36 import org.robolectric.annotation.Implementation;
37 import org.robolectric.annotation.Implements;
38 import org.robolectric.annotation.Resetter;
39 
40 import java.util.Collections;
41 import java.util.HashMap;
42 import java.util.LinkedHashMap;
43 import java.util.List;
44 import java.util.Map;
45 
46 /** Shadow of ApplicationPackageManager that allows the getting of content providers per user. */
47 @Implements(value = ApplicationPackageManager.class)
48 public class ShadowApplicationPackageManager extends
49         org.robolectric.shadows.ShadowApplicationPackageManager {
50 
51     private static Resources sResources = null;
52     private static PackageManager sPackageManager;
53 
54     private final Map<Integer, String> mUserIdToDefaultBrowserMap = new HashMap<>();
55     private final Map<String, ComponentName> mPkgToDefaultActivityMap = new HashMap<>();
56     private final Map<String, IntentFilter> mPkgToDefaultActivityIntentFilterMap = new HashMap<>();
57     private final Map<IntentFilter, ComponentName> mPreferredActivities = new LinkedHashMap<>();
58     private final Map<Pair<String, Integer>, Integer> mPkgAndUserIdToIntentVerificationStatusMap =
59             new HashMap<>();
60     private List<ResolveInfo> mHomeActivities = Collections.emptyList();
61     private ComponentName mDefaultHomeActivity;
62     private String mPermissionControllerPackageName;
63 
64     @Resetter
reset()65     public static void reset() {
66         sResources = null;
67         sPackageManager = null;
68     }
69 
70     @Implementation
71     @NonNull
getInstalledModules(@ackageManager.ModuleInfoFlags int flags)72     protected List<ModuleInfo> getInstalledModules(@PackageManager.ModuleInfoFlags int flags) {
73         return Collections.emptyList();
74     }
75 
76     @Implementation
getUserBadgedIcon(Drawable icon, UserHandle user)77     protected Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
78         return icon;
79     }
80 
81     @Override
82     @Implementation
resolveContentProviderAsUser(String name, int flags, @UserIdInt int userId)83     protected ProviderInfo resolveContentProviderAsUser(String name, int flags,
84             @UserIdInt int userId) {
85         return resolveContentProvider(name, flags);
86     }
87 
88     @Implementation
getPackageUidAsUser(String packageName, int flags, int userId)89     protected int getPackageUidAsUser(String packageName, int flags, int userId)
90             throws PackageManager.NameNotFoundException {
91         return 0;
92     }
93 
94     @Implementation
deleteApplicationCacheFiles(String packageName, IPackageDataObserver observer)95     protected void deleteApplicationCacheFiles(String packageName, IPackageDataObserver observer) {
96         sPackageManager.deleteApplicationCacheFiles(packageName, observer);
97     }
98 
99     @Implementation
getResourcesForApplication(String appPackageName)100     protected Resources getResourcesForApplication(String appPackageName)
101             throws PackageManager.NameNotFoundException {
102         return sResources;
103     }
104 
105     @Implementation
getInstalledApplicationsAsUser(int flags, int userId)106     protected List<ApplicationInfo> getInstalledApplicationsAsUser(int flags, int userId) {
107         return getInstalledApplications(flags);
108     }
109 
110     @Implementation
getApplicationInfoAsUser(String packageName, int flags, int userId)111     protected ApplicationInfo getApplicationInfoAsUser(String packageName, int flags, int userId)
112             throws PackageManager.NameNotFoundException {
113         return getApplicationInfo(packageName, flags);
114     }
115 
116     @Implementation
117     @Override
getHomeActivities(List<ResolveInfo> outActivities)118     protected ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
119         outActivities.addAll(mHomeActivities);
120         return mDefaultHomeActivity;
121     }
122 
123     @Implementation
124     @Override
clearPackagePreferredActivities(String packageName)125     protected void clearPackagePreferredActivities(String packageName) {
126         mPreferredActivities.clear();
127     }
128 
129     @Implementation
130     @Override
getPreferredActivities(List<IntentFilter> outFilters, List<ComponentName> outActivities, String packageName)131     public int getPreferredActivities(List<IntentFilter> outFilters,
132             List<ComponentName> outActivities, String packageName) {
133         for (IntentFilter filter : mPreferredActivities.keySet()) {
134             ComponentName name = mPreferredActivities.get(filter);
135             // If packageName is null, match everything, else filter by packageName.
136             if (packageName == null) {
137                 outFilters.add(filter);
138                 outActivities.add(name);
139             } else if (name.getPackageName().equals(packageName)) {
140                 outFilters.add(filter);
141                 outActivities.add(name);
142             }
143         }
144         return 0;
145     }
146 
147     @Implementation
148     @Override
addPreferredActivity(IntentFilter filter, int match, ComponentName[] set, ComponentName activity)149     public void addPreferredActivity(IntentFilter filter, int match, ComponentName[] set,
150             ComponentName activity) {
151         mPreferredActivities.put(filter, activity);
152     }
153 
154     @Implementation
155     @Override
getDefaultBrowserPackageNameAsUser(int userId)156     protected String getDefaultBrowserPackageNameAsUser(int userId) {
157         return mUserIdToDefaultBrowserMap.getOrDefault(userId, null);
158     }
159 
160     @Implementation
161     @Override
setDefaultBrowserPackageNameAsUser(String packageName, int userId)162     protected boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
163         mUserIdToDefaultBrowserMap.put(userId, packageName);
164         return true;
165     }
166 
167     @Implementation
168     @Override
getIntentVerificationStatusAsUser(String packageName, int userId)169     protected int getIntentVerificationStatusAsUser(String packageName, int userId) {
170         Pair<String, Integer> key = new Pair<>(packageName, userId);
171         return mPkgAndUserIdToIntentVerificationStatusMap.getOrDefault(key,
172                 INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED);
173     }
174 
175     @Implementation
176     @Override
updateIntentVerificationStatusAsUser(String packageName, int status, int userId)177     protected boolean updateIntentVerificationStatusAsUser(String packageName, int status,
178             int userId) {
179         Pair<String, Integer> key = new Pair<>(packageName, userId);
180         mPkgAndUserIdToIntentVerificationStatusMap.put(key, status);
181         return true;
182     }
183 
184     @Implementation
getPermissionControllerPackageName()185     protected String getPermissionControllerPackageName() {
186         return mPermissionControllerPackageName;
187     }
188 
setPermissionControllerPackageName(String packageName)189     public void setPermissionControllerPackageName(String packageName) {
190         mPermissionControllerPackageName = packageName;
191     }
192 
setHomeActivities(List<ResolveInfo> homeActivities)193     public void setHomeActivities(List<ResolveInfo> homeActivities) {
194         mHomeActivities = homeActivities;
195     }
196 
setDefaultHomeActivity(ComponentName defaultHomeActivity)197     public void setDefaultHomeActivity(ComponentName defaultHomeActivity) {
198         mDefaultHomeActivity = defaultHomeActivity;
199     }
200 
setResources(Resources resources)201     public static void setResources(Resources resources) {
202         sResources = resources;
203     }
204 
setPackageManager(PackageManager packageManager)205     public static void setPackageManager(PackageManager packageManager) {
206         sPackageManager = packageManager;
207     }
208 }
209