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.applications; 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.PackageInfo; 23 import android.content.pm.PackageManager; 24 import android.content.pm.PackageManager.NameNotFoundException; 25 import android.content.pm.ResolveInfo; 26 import android.graphics.drawable.Drawable; 27 import android.os.UserHandle; 28 29 import java.util.List; 30 31 /** 32 * This interface replicates a subset of the android.content.pm.PackageManager (PM). The interface 33 * exists so that we can use a thin wrapper around the PM in production code and a mock in tests. 34 * We cannot directly mock or shadow the PM, because some of the methods we rely on are newer than 35 * the API version supported by Robolectric. 36 */ 37 public interface PackageManagerWrapper { 38 39 /** 40 * Returns the real {@code PackageManager} object. 41 */ 42 PackageManager getPackageManager(); 43 44 /** 45 * Calls {@code PackageManager.getInstalledApplicationsAsUser()}. 46 * 47 * @see android.content.pm.PackageManager#getInstalledApplicationsAsUser 48 */ 49 List<ApplicationInfo> getInstalledApplicationsAsUser(int flags, int userId); 50 51 /** 52 * Calls {@code PackageManager.hasSystemFeature()}. 53 * 54 * @see android.content.pm.PackageManager#hasSystemFeature 55 */ 56 boolean hasSystemFeature(String name); 57 58 /** 59 * Calls {@code PackageManager.queryIntentActivitiesAsUser()}. 60 * 61 * @see android.content.pm.PackageManager#queryIntentActivitiesAsUser 62 */ 63 List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent, int flags, int userId); 64 65 /** 66 * Calls {@code PackageManager.getInstallReason()}. 67 * 68 * @see android.content.pm.PackageManager#getInstallReason 69 */ 70 int getInstallReason(String packageName, UserHandle user); 71 72 /** 73 * Calls {@code PackageManager.getApplicationInfoAsUser} 74 */ 75 ApplicationInfo getApplicationInfoAsUser(String packageName, int i, int userId) 76 throws PackageManager.NameNotFoundException; 77 78 /** 79 * Calls {@code PackageManager.setDefaultBrowserPackageNameAsUser} 80 */ 81 boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId); 82 83 /** 84 * Calls {@code PackageManager.getDefaultBrowserPackageNameAsUser} 85 */ 86 String getDefaultBrowserPackageNameAsUser(int userId); 87 88 /** 89 * Calls {@code PackageManager.getHomeActivities} 90 */ 91 ComponentName getHomeActivities(List<ResolveInfo> homeActivities); 92 93 /** 94 * Calls {@code PackageManager.queryIntentServicesAsUser} 95 */ 96 List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int i, int user); 97 98 /** 99 * Calls {@code PackageManager.replacePreferredActivity} 100 */ 101 void replacePreferredActivity(IntentFilter homeFilter, int matchCategoryEmpty, 102 ComponentName[] componentNames, ComponentName component); 103 104 /** 105 * Gets information about a particular package from the package manager. 106 * @param packageName The name of the package we would like information about. 107 * @param i additional options flags. see javadoc for {@link PackageManager#getPackageInfo(String, int)} 108 * @return The PackageInfo for the requested package 109 * @throws NameNotFoundException 110 */ 111 PackageInfo getPackageInfo(String packageName, int i) throws NameNotFoundException; 112 113 /** 114 * Retrieves the icon associated with this particular set of ApplicationInfo 115 * @param info The ApplicationInfo to retrieve the icon for 116 * @return The icon as a drawable. 117 */ 118 Drawable getUserBadgedIcon(ApplicationInfo info); 119 120 /** 121 * Retrieves the label associated with the particular set of ApplicationInfo 122 * @param app The ApplicationInfo to retrieve the label for 123 * @return the label as a CharSequence 124 */ 125 CharSequence loadLabel(ApplicationInfo app); 126 127 /** 128 * Retrieve all activities that can be performed for the given intent. 129 */ 130 List<ResolveInfo> queryIntentActivities(Intent intent, int flags); 131 } 132