Home | History | Annotate | Download | only in contacts
      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 package com.android.providers.contacts;
     17 
     18 import android.content.Context;
     19 import android.content.Intent;
     20 import android.content.pm.ApplicationInfo;
     21 import android.content.pm.PackageInfo;
     22 import android.content.pm.PackageManager;
     23 import android.content.pm.ProviderInfo;
     24 import android.content.pm.ResolveInfo;
     25 import android.content.res.Resources;
     26 import android.os.Binder;
     27 import android.test.mock.MockPackageManager;
     28 
     29 import java.util.ArrayList;
     30 import java.util.HashMap;
     31 import java.util.List;
     32 
     33 /**
     34  * Mock {@link PackageManager} that knows about a specific set of packages
     35  * to help test security models. Because {@link Binder#getCallingUid()}
     36  * can't be mocked, you'll have to find your mock-UID manually using your
     37  * {@link Context#getPackageName()}.
     38  */
     39 public class ContactsMockPackageManager extends MockPackageManager {
     40 
     41     private final Context mRealContext;
     42 
     43     private final HashMap<Integer, String> mForward = new HashMap<Integer, String>();
     44     private final HashMap<String, Integer> mReverse = new HashMap<String, Integer>();
     45     private List<PackageInfo> mPackages;
     46 
     47     public ContactsMockPackageManager(Context realContext) {
     48         mRealContext = realContext;
     49     }
     50 
     51     /**
     52      * Add a UID-to-package mapping, which is then stored internally.
     53      */
     54     public void addPackage(int packageUid, String packageName) {
     55         mForward.put(packageUid, packageName);
     56         mReverse.put(packageName, packageUid);
     57     }
     58 
     59     public void removePackage(int packageUid) {
     60         final String packageName = mForward.remove(packageUid);
     61         if (packageName != null) {
     62             mReverse.remove(packageName);
     63         }
     64     }
     65 
     66     @Override
     67     public String getNameForUid(int uid) {
     68         return "name-for-uid";
     69     }
     70 
     71     @Override
     72     public String[] getPackagesForUid(int uid) {
     73         final String packageName = mForward.get(uid);
     74         if (packageName != null) {
     75             return new String[] {packageName};
     76         } else if (mPackages != null) {
     77             return new String[] { mPackages.get(0).packageName };
     78         } else {
     79             return new String[] {};
     80         }
     81     }
     82 
     83     @Override
     84     public ApplicationInfo getApplicationInfo(String packageName, int flags)
     85             throws NameNotFoundException {
     86         ApplicationInfo info = new ApplicationInfo();
     87         Integer uid = mReverse.get(packageName);
     88         if (uid == null) {
     89             throw new NameNotFoundException();
     90         }
     91         info.uid = (uid != null) ? uid : -1;
     92         info.flags = ApplicationInfo.FLAG_INSTALLED;
     93         return info;
     94     }
     95 
     96     public void setInstalledPackages(List<PackageInfo> packages) {
     97         this.mPackages = packages;
     98     }
     99 
    100     @Override
    101     public List<PackageInfo> getInstalledPackages(int flags) {
    102         return mPackages;
    103     }
    104 
    105     @Override
    106     public PackageInfo getPackageInfo(String packageName, int flags) throws NameNotFoundException {
    107         for (PackageInfo info : mPackages) {
    108             if (info.packageName.equals(packageName)) {
    109                 return info;
    110             }
    111         }
    112         throw new NameNotFoundException();
    113     }
    114 
    115     @Override
    116     public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
    117         return new ArrayList<ResolveInfo>();
    118     }
    119 
    120     @Override
    121     public Resources getResourcesForApplication(String appPackageName) {
    122         if (mRealContext.getPackageName().equals(appPackageName)) {
    123             return mRealContext.getResources();
    124         }
    125         return new ContactsMockResources();
    126     }
    127 
    128     @Override
    129     public List<ProviderInfo> queryContentProviders(String processName, int uid, int flags,
    130             String metaDataKey) {
    131         final List<ProviderInfo> ret = new ArrayList<>();
    132         final List<PackageInfo> packages = getInstalledPackages(flags);
    133         if (packages == null) {
    134             return ret;
    135         }
    136         for (PackageInfo pkg : packages) {
    137             if (pkg.providers == null) {
    138                 continue;
    139             }
    140             for (ProviderInfo proi : pkg.providers) {
    141                 if (metaDataKey == null) {
    142                     ret.add(proi);
    143                 } else {
    144                     if (proi.metaData != null && proi.metaData.containsKey(metaDataKey)) {
    145                         ret.add(proi);
    146                     }
    147                 }
    148             }
    149         }
    150         return ret;
    151     }
    152 }
    153