• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 // Copyright 2011 Google Inc. All Rights Reserved.
18 
19 package com.android.providers.applications;
20 
21 import android.content.ComponentName;
22 import android.content.Intent;
23 import android.content.pm.ActivityInfo;
24 import android.content.pm.ApplicationInfo;
25 import android.content.pm.PackageManager;
26 import android.content.pm.ResolveInfo;
27 
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 
33 public class MockPackageManager extends android.test.mock.MockPackageManager {
34 
35     private List<ResolveInfo> mPackages = new ArrayList<ResolveInfo>();
36 
37     private Map<String, Integer> mApplicationEnabledSettings = new HashMap<String, Integer>();
38     private Map<String, Integer> mComponentEnabledSettings = new HashMap<String, Integer>();
39 
40     /**
41      * Returns all packages registered with the mock package manager.
42      * ApplicationsProvider uses this method to query the list of applications.
43      */
44     @Override
queryIntentActivities(Intent intent, int flags)45     public List<ResolveInfo> queryIntentActivities(Intent intent, int flags) {
46         return mPackages;
47     }
48 
49     @Override
getApplicationEnabledSetting(String packageName)50     public int getApplicationEnabledSetting(String packageName) {
51         return mApplicationEnabledSettings.get(packageName);
52     }
53 
54     @Override
getComponentEnabledSetting(ComponentName componentName)55     public int getComponentEnabledSetting(ComponentName componentName) {
56         return mComponentEnabledSettings.get(componentName.flattenToString());
57     }
58 
59     /**
60      * Adds a new package to the mock package manager.
61      *
62      * Example:
63      * addPackage("Email", new ComponentName("com.android.email", "com.android.email.MainView"),
64      *            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
65      *            PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
66      *
67      * @param title the user-friendly title of the application (this is what
68      *         users will search for)
69      * @param componentName The full component name of the app.
70      * @param appEnabledSetting The setting which should be returned from
71      *        {@link #getApplicationEnabledSetting}, for this component's package.
72      * @param componentEnabledSetting The setting which should be returned from
73      *        {@link #getComponentEnabledSetting}, for this component.
74      */
addPackage(final String title, ComponentName componentName, int appEnabledSetting, int componentEnabledSetting)75     public void addPackage(final String title, ComponentName componentName, int appEnabledSetting,
76             int componentEnabledSetting) {
77         // Set the application's title.
78         ResolveInfo packageInfo = new ResolveInfo() {
79             @Override
80             public CharSequence loadLabel(PackageManager pm) {
81                 return title;
82             }
83         };
84 
85         // Set the application's ComponentName.
86         packageInfo.activityInfo = new ActivityInfo();
87         packageInfo.activityInfo.name = componentName.getClassName();
88         packageInfo.activityInfo.applicationInfo = new ApplicationInfo();
89         packageInfo.activityInfo.applicationInfo.packageName = componentName.getPackageName();
90 
91         mPackages.add(packageInfo);
92 
93         mApplicationEnabledSettings.put(componentName.getPackageName(), appEnabledSetting);
94         mComponentEnabledSettings.put(componentName.flattenToString(), componentEnabledSetting);
95     }
96 
addPackage(final String title, ComponentName componentName)97     public void addPackage(final String title, ComponentName componentName) {
98         addPackage(title, componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
99                 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
100     }
101 }
102