• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.settingslib.wrapper;
17 
18 import android.content.ComponentName;
19 import android.content.Intent;
20 import android.content.IntentFilter;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.IPackageDeleteObserver;
23 import android.content.pm.PackageInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.PackageManager.NameNotFoundException;
26 import android.content.pm.ResolveInfo;
27 import android.graphics.drawable.Drawable;
28 import android.os.UserHandle;
29 import android.os.storage.VolumeInfo;
30 
31 import java.util.List;
32 
33 /**
34  * A thin wrapper class that simplifies testing by putting a mockable layer between the application
35  * and the PackageManager. This class only provides access to the minimum number of functions from
36  * the PackageManager needed for DeletionHelper to work.
37  */
38 public class PackageManagerWrapper {
39 
40     private final PackageManager mPm;
41 
PackageManagerWrapper(PackageManager pm)42     public PackageManagerWrapper(PackageManager pm) {
43         mPm = pm;
44     }
45 
46     /**
47      * Returns the real {@code PackageManager} object.
48      */
getPackageManager()49     public PackageManager getPackageManager() {
50         return mPm;
51     }
52 
53     /**
54      * Calls {@code PackageManager.getInstalledApplicationsAsUser()}.
55      *
56      * @see android.content.pm.PackageManager#getInstalledApplicationsAsUser
57      */
getInstalledApplicationsAsUser(int flags, int userId)58     public List<ApplicationInfo> getInstalledApplicationsAsUser(int flags, int userId) {
59         return mPm.getInstalledApplicationsAsUser(flags, userId);
60     }
61 
62     /**
63      * Calls {@code PackageManager.getInstalledPackagesAsUser}
64      */
getInstalledPackagesAsUser(int flags, int userId)65     public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
66         return mPm.getInstalledPackagesAsUser(flags, userId);
67     }
68 
69     /**
70      * Calls {@code PackageManager.hasSystemFeature()}.
71      *
72      * @see android.content.pm.PackageManager#hasSystemFeature
73      */
hasSystemFeature(String name)74     public boolean hasSystemFeature(String name) {
75         return mPm.hasSystemFeature(name);
76     }
77 
78     /**
79      * Calls {@code PackageManager.queryIntentActivitiesAsUser()}.
80      *
81      * @see android.content.pm.PackageManager#queryIntentActivitiesAsUser
82      */
queryIntentActivitiesAsUser(Intent intent, int flags, int userId)83     public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent, int flags, int userId) {
84         return mPm.queryIntentActivitiesAsUser(intent, flags, userId);
85     }
86 
87     /**
88      * Calls {@code PackageManager.getInstallReason()}.
89      *
90      * @see android.content.pm.PackageManager#getInstallReason
91      */
getInstallReason(String packageName, UserHandle user)92     public int getInstallReason(String packageName, UserHandle user) {
93         return mPm.getInstallReason(packageName, user);
94     }
95 
96     /**
97      * Calls {@code PackageManager.getApplicationInfoAsUser}
98      */
getApplicationInfoAsUser(String packageName, int i, int userId)99     public ApplicationInfo getApplicationInfoAsUser(String packageName, int i, int userId)
100             throws PackageManager.NameNotFoundException {
101         return mPm.getApplicationInfoAsUser(packageName, i, userId);
102     }
103 
104     /**
105      * Calls {@code PackageManager.setDefaultBrowserPackageNameAsUser}
106      */
setDefaultBrowserPackageNameAsUser(String packageName, int userId)107     public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
108         return mPm.setDefaultBrowserPackageNameAsUser(packageName, userId);
109     }
110 
111     /**
112      * Calls {@code PackageManager.getDefaultBrowserPackageNameAsUser}
113      */
getDefaultBrowserPackageNameAsUser(int userId)114     public String getDefaultBrowserPackageNameAsUser(int userId) {
115         return mPm.getDefaultBrowserPackageNameAsUser(userId);
116     }
117 
118     /**
119      * Calls {@code PackageManager.getHomeActivities}
120      */
getHomeActivities(List<ResolveInfo> homeActivities)121     public ComponentName getHomeActivities(List<ResolveInfo> homeActivities) {
122         return mPm.getHomeActivities(homeActivities);
123     }
124 
125     /**
126      * Calls {@code PackageManager.queryIntentServicesAsUser}
127      */
queryIntentServicesAsUser(Intent intent, int i, int user)128     public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int i, int user) {
129         return mPm.queryIntentServicesAsUser(intent, i, user);
130     }
131 
132     /**
133      * Calls {@code PackageManager.queryIntentServices}
134      */
queryIntentServices(Intent intent, int i)135     public List<ResolveInfo> queryIntentServices(Intent intent, int i) {
136         return mPm.queryIntentServices(intent, i);
137     }
138 
139     /**
140      * Calls {@code PackageManager.replacePreferredActivity}
141      */
replacePreferredActivity(IntentFilter homeFilter, int matchCategoryEmpty, ComponentName[] componentNames, ComponentName component)142     public void replacePreferredActivity(IntentFilter homeFilter, int matchCategoryEmpty,
143             ComponentName[] componentNames, ComponentName component) {
144         mPm.replacePreferredActivity(homeFilter, matchCategoryEmpty, componentNames, component);
145     }
146 
147     /**
148      * Gets information about a particular package from the package manager.
149      *
150      * @param packageName The name of the package we would like information about.
151      * @param i           additional options flags. see javadoc for
152      *                    {@link PackageManager#getPackageInfo(String, int)}
153      * @return The PackageInfo for the requested package
154      */
getPackageInfo(String packageName, int i)155     public PackageInfo getPackageInfo(String packageName, int i) throws NameNotFoundException {
156         return mPm.getPackageInfo(packageName, i);
157     }
158 
159     /**
160      * Retrieves the icon associated with this particular set of ApplicationInfo
161      *
162      * @param info The ApplicationInfo to retrieve the icon for
163      * @return The icon as a drawable.
164      */
getUserBadgedIcon(ApplicationInfo info)165     public Drawable getUserBadgedIcon(ApplicationInfo info) {
166         return mPm.getUserBadgedIcon(mPm.loadUnbadgedItemIcon(info, info),
167                 new UserHandle(UserHandle.getUserId(info.uid)));
168     }
169 
170     /**
171      * Retrieves the label associated with the particular set of ApplicationInfo
172      *
173      * @param app The ApplicationInfo to retrieve the label for
174      * @return the label as a CharSequence
175      */
loadLabel(ApplicationInfo app)176     public CharSequence loadLabel(ApplicationInfo app) {
177         return app.loadLabel(mPm);
178     }
179 
180     /**
181      * Retrieve all activities that can be performed for the given intent.
182      */
queryIntentActivities(Intent intent, int flags)183     public List<ResolveInfo> queryIntentActivities(Intent intent, int flags) {
184         return mPm.queryIntentActivities(intent, flags);
185     }
186 
187     /**
188      * Calls {@code PackageManager.getPrimaryStorageCurrentVolume}
189      */
getPrimaryStorageCurrentVolume()190     public VolumeInfo getPrimaryStorageCurrentVolume() {
191         return mPm.getPrimaryStorageCurrentVolume();
192     }
193 
194     /**
195      * Calls {@code PackageManager.deletePackageAsUser}
196      */
deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags, int userId)197     public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
198             int userId) {
199         mPm.deletePackageAsUser(packageName, observer, flags, userId);
200     }
201 
202     /**
203      * Calls {@code PackageManager.getPackageUidAsUser}
204      */
getPackageUidAsUser(String pkg, int userId)205     public int getPackageUidAsUser(String pkg, int userId)
206             throws PackageManager.NameNotFoundException {
207         return mPm.getPackageUidAsUser(pkg, userId);
208     }
209 
210     /**
211      * Calls {@code PackageManager.setApplicationEnabledSetting}
212      */
setApplicationEnabledSetting(String packageName, int newState, int flags)213     public void setApplicationEnabledSetting(String packageName, int newState, int flags) {
214         mPm.setApplicationEnabledSetting(packageName, newState, flags);
215     }
216 
217     /**
218      * Calls {@code PackageManager.getApplicationEnabledSetting}
219      */
getApplicationEnabledSetting(String packageName)220     public int getApplicationEnabledSetting(String packageName) {
221         return mPm.getApplicationEnabledSetting(packageName);
222     }
223 
224     /**
225      * Calls {@code PackageManager.setComponentEnabledSetting}
226      */
setComponentEnabledSetting(ComponentName componentName, int newState, int flags)227     public void setComponentEnabledSetting(ComponentName componentName, int newState, int flags) {
228         mPm.setComponentEnabledSetting(componentName, newState, flags);
229     }
230 
231     /**
232      * Calls {@code PackageManager.getApplicationInfo}
233      */
getApplicationInfo(String packageName, int flags)234     public ApplicationInfo getApplicationInfo(String packageName, int flags)
235             throws NameNotFoundException {
236         return mPm.getApplicationInfo(packageName, flags);
237     }
238 
239     /**
240      * Calls {@code PackageManager.getApplicationLabel}
241      */
getApplicationLabel(ApplicationInfo info)242     public CharSequence getApplicationLabel(ApplicationInfo info) {
243         return mPm.getApplicationLabel(info);
244     }
245 
246     /**
247      * Calls {@code PackageManager.queryBroadcastReceivers}
248      */
queryBroadcastReceivers(Intent intent, int flags)249     public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
250         return mPm.queryBroadcastReceivers(intent, flags);
251     }
252 }
253 
254