Home | History | Annotate | Download | only in pm
      1 /*
      2  * Copyright (C) 2009 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 package android.content.pm;
     18 
     19 import android.content.Context;
     20 import android.content.BroadcastReceiver;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.content.ComponentName;
     24 import android.content.pm.PackageManager.NameNotFoundException;
     25 import android.content.res.Resources;
     26 import android.content.res.XmlResourceParser;
     27 import android.os.Environment;
     28 import android.os.Handler;
     29 import android.util.Log;
     30 import android.util.AttributeSet;
     31 import android.util.Xml;
     32 
     33 import java.util.Map;
     34 import java.util.Collection;
     35 import java.util.Collections;
     36 import java.util.HashMap;
     37 import java.util.List;
     38 import java.util.ArrayList;
     39 import java.util.concurrent.atomic.AtomicReference;
     40 import java.io.File;
     41 import java.io.FileOutputStream;
     42 import java.io.FileDescriptor;
     43 import java.io.PrintWriter;
     44 import java.io.IOException;
     45 import java.io.FileInputStream;
     46 
     47 import com.android.internal.os.AtomicFile;
     48 import com.android.internal.util.FastXmlSerializer;
     49 
     50 import com.google.android.collect.Maps;
     51 import com.google.android.collect.Lists;
     52 
     53 import org.xmlpull.v1.XmlPullParserException;
     54 import org.xmlpull.v1.XmlPullParser;
     55 import org.xmlpull.v1.XmlSerializer;
     56 
     57 /**
     58  * A cache of registered services. This cache
     59  * is built by interrogating the {@link PackageManager} and is updated as packages are added,
     60  * removed and changed. The services are referred to by type V and
     61  * are made available via the {@link #getServiceInfo} method.
     62  * @hide
     63  */
     64 public abstract class RegisteredServicesCache<V> {
     65     private static final String TAG = "PackageManager";
     66 
     67     public final Context mContext;
     68     private final String mInterfaceName;
     69     private final String mMetaDataName;
     70     private final String mAttributesName;
     71     private final XmlSerializerAndParser<V> mSerializerAndParser;
     72     private final AtomicReference<BroadcastReceiver> mReceiver;
     73 
     74     private final Object mServicesLock = new Object();
     75     // synchronized on mServicesLock
     76     private HashMap<V, Integer> mPersistentServices;
     77     // synchronized on mServicesLock
     78     private Map<V, ServiceInfo<V>> mServices;
     79     // synchronized on mServicesLock
     80     private boolean mPersistentServicesFileDidNotExist;
     81 
     82     /**
     83      * This file contains the list of known services. We would like to maintain this forever
     84      * so we store it as an XML file.
     85      */
     86     private final AtomicFile mPersistentServicesFile;
     87 
     88     // the listener and handler are synchronized on "this" and must be updated together
     89     private RegisteredServicesCacheListener<V> mListener;
     90     private Handler mHandler;
     91 
     92     public RegisteredServicesCache(Context context, String interfaceName, String metaDataName,
     93             String attributeName, XmlSerializerAndParser<V> serializerAndParser) {
     94         mContext = context;
     95         mInterfaceName = interfaceName;
     96         mMetaDataName = metaDataName;
     97         mAttributesName = attributeName;
     98         mSerializerAndParser = serializerAndParser;
     99 
    100         File dataDir = Environment.getDataDirectory();
    101         File systemDir = new File(dataDir, "system");
    102         File syncDir = new File(systemDir, "registered_services");
    103         mPersistentServicesFile = new AtomicFile(new File(syncDir, interfaceName + ".xml"));
    104 
    105         generateServicesMap();
    106 
    107         final BroadcastReceiver receiver = new BroadcastReceiver() {
    108             @Override
    109             public void onReceive(Context context1, Intent intent) {
    110                 generateServicesMap();
    111             }
    112         };
    113         mReceiver = new AtomicReference<BroadcastReceiver>(receiver);
    114         IntentFilter intentFilter = new IntentFilter();
    115         intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
    116         intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    117         intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    118         intentFilter.addDataScheme("package");
    119         mContext.registerReceiver(receiver, intentFilter);
    120         // Register for events related to sdcard installation.
    121         IntentFilter sdFilter = new IntentFilter();
    122         sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
    123         sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
    124         mContext.registerReceiver(receiver, sdFilter);
    125     }
    126 
    127     public void dump(FileDescriptor fd, PrintWriter fout, String[] args) {
    128         Map<V, ServiceInfo<V>> services;
    129         synchronized (mServicesLock) {
    130             services = mServices;
    131         }
    132         fout.println("RegisteredServicesCache: " + services.size() + " services");
    133         for (ServiceInfo info : services.values()) {
    134             fout.println("  " + info);
    135         }
    136     }
    137 
    138     public RegisteredServicesCacheListener<V> getListener() {
    139         synchronized (this) {
    140             return mListener;
    141         }
    142     }
    143 
    144     public void setListener(RegisteredServicesCacheListener<V> listener, Handler handler) {
    145         if (handler == null) {
    146             handler = new Handler(mContext.getMainLooper());
    147         }
    148         synchronized (this) {
    149             mHandler = handler;
    150             mListener = listener;
    151         }
    152     }
    153 
    154     private void notifyListener(final V type, final boolean removed) {
    155         if (Log.isLoggable(TAG, Log.VERBOSE)) {
    156             Log.d(TAG, "notifyListener: " + type + " is " + (removed ? "removed" : "added"));
    157         }
    158         RegisteredServicesCacheListener<V> listener;
    159         Handler handler;
    160         synchronized (this) {
    161             listener = mListener;
    162             handler = mHandler;
    163         }
    164         if (listener == null) {
    165             return;
    166         }
    167 
    168         final RegisteredServicesCacheListener<V> listener2 = listener;
    169         handler.post(new Runnable() {
    170             public void run() {
    171                 listener2.onServiceChanged(type, removed);
    172             }
    173         });
    174     }
    175 
    176     /**
    177      * Value type that describes a Service. The information within can be used
    178      * to bind to the service.
    179      */
    180     public static class ServiceInfo<V> {
    181         public final V type;
    182         public final ComponentName componentName;
    183         public final int uid;
    184 
    185         /** @hide */
    186         public ServiceInfo(V type, ComponentName componentName, int uid) {
    187             this.type = type;
    188             this.componentName = componentName;
    189             this.uid = uid;
    190         }
    191 
    192         @Override
    193         public String toString() {
    194             return "ServiceInfo: " + type + ", " + componentName + ", uid " + uid;
    195         }
    196     }
    197 
    198     /**
    199      * Accessor for the registered authenticators.
    200      * @param type the account type of the authenticator
    201      * @return the AuthenticatorInfo that matches the account type or null if none is present
    202      */
    203     public ServiceInfo<V> getServiceInfo(V type) {
    204         synchronized (mServicesLock) {
    205             return mServices.get(type);
    206         }
    207     }
    208 
    209     /**
    210      * @return a collection of {@link RegisteredServicesCache.ServiceInfo} objects for all
    211      * registered authenticators.
    212      */
    213     public Collection<ServiceInfo<V>> getAllServices() {
    214         synchronized (mServicesLock) {
    215             return Collections.unmodifiableCollection(mServices.values());
    216         }
    217     }
    218 
    219     /**
    220      * Stops the monitoring of package additions, removals and changes.
    221      */
    222     public void close() {
    223         final BroadcastReceiver receiver = mReceiver.getAndSet(null);
    224         if (receiver != null) {
    225             mContext.unregisterReceiver(receiver);
    226         }
    227     }
    228 
    229     @Override
    230     protected void finalize() throws Throwable {
    231         if (mReceiver.get() != null) {
    232             Log.e(TAG, "RegisteredServicesCache finalized without being closed");
    233         }
    234         close();
    235         super.finalize();
    236     }
    237 
    238     private boolean inSystemImage(int callerUid) {
    239         String[] packages = mContext.getPackageManager().getPackagesForUid(callerUid);
    240         for (String name : packages) {
    241             try {
    242                 PackageInfo packageInfo =
    243                         mContext.getPackageManager().getPackageInfo(name, 0 /* flags */);
    244                 if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
    245                     return true;
    246                 }
    247             } catch (PackageManager.NameNotFoundException e) {
    248                 return false;
    249             }
    250         }
    251         return false;
    252     }
    253 
    254     void generateServicesMap() {
    255         PackageManager pm = mContext.getPackageManager();
    256         ArrayList<ServiceInfo<V>> serviceInfos = new ArrayList<ServiceInfo<V>>();
    257         List<ResolveInfo> resolveInfos = pm.queryIntentServices(new Intent(mInterfaceName),
    258                 PackageManager.GET_META_DATA);
    259         for (ResolveInfo resolveInfo : resolveInfos) {
    260             try {
    261                 ServiceInfo<V> info = parseServiceInfo(resolveInfo);
    262                 if (info == null) {
    263                     Log.w(TAG, "Unable to load service info " + resolveInfo.toString());
    264                     continue;
    265                 }
    266                 serviceInfos.add(info);
    267             } catch (XmlPullParserException e) {
    268                 Log.w(TAG, "Unable to load service info " + resolveInfo.toString(), e);
    269             } catch (IOException e) {
    270                 Log.w(TAG, "Unable to load service info " + resolveInfo.toString(), e);
    271             }
    272         }
    273 
    274         synchronized (mServicesLock) {
    275             if (mPersistentServices == null) {
    276                 readPersistentServicesLocked();
    277             }
    278             mServices = Maps.newHashMap();
    279             StringBuilder changes = new StringBuilder();
    280             for (ServiceInfo<V> info : serviceInfos) {
    281                 // four cases:
    282                 // - doesn't exist yet
    283                 //   - add, notify user that it was added
    284                 // - exists and the UID is the same
    285                 //   - replace, don't notify user
    286                 // - exists, the UID is different, and the new one is not a system package
    287                 //   - ignore
    288                 // - exists, the UID is different, and the new one is a system package
    289                 //   - add, notify user that it was added
    290                 Integer previousUid = mPersistentServices.get(info.type);
    291                 if (previousUid == null) {
    292                     changes.append("  New service added: ").append(info).append("\n");
    293                     mServices.put(info.type, info);
    294                     mPersistentServices.put(info.type, info.uid);
    295                     if (!mPersistentServicesFileDidNotExist) {
    296                         notifyListener(info.type, false /* removed */);
    297                     }
    298                 } else if (previousUid == info.uid) {
    299                     if (Log.isLoggable(TAG, Log.VERBOSE)) {
    300                         changes.append("  Existing service (nop): ").append(info).append("\n");
    301                     }
    302                     mServices.put(info.type, info);
    303                 } else if (inSystemImage(info.uid)
    304                         || !containsTypeAndUid(serviceInfos, info.type, previousUid)) {
    305                     if (inSystemImage(info.uid)) {
    306                         changes.append("  System service replacing existing: ").append(info)
    307                                 .append("\n");
    308                     } else {
    309                         changes.append("  Existing service replacing a removed service: ")
    310                                 .append(info).append("\n");
    311                     }
    312                     mServices.put(info.type, info);
    313                     mPersistentServices.put(info.type, info.uid);
    314                     notifyListener(info.type, false /* removed */);
    315                 } else {
    316                     // ignore
    317                     changes.append("  Existing service with new uid ignored: ").append(info)
    318                             .append("\n");
    319                 }
    320             }
    321 
    322             ArrayList<V> toBeRemoved = Lists.newArrayList();
    323             for (V v1 : mPersistentServices.keySet()) {
    324                 if (!containsType(serviceInfos, v1)) {
    325                     toBeRemoved.add(v1);
    326                 }
    327             }
    328             for (V v1 : toBeRemoved) {
    329                 mPersistentServices.remove(v1);
    330                 changes.append("  Service removed: ").append(v1).append("\n");
    331                 notifyListener(v1, true /* removed */);
    332             }
    333             if (changes.length() > 0) {
    334                 Log.d(TAG, "generateServicesMap(" + mInterfaceName + "): " +
    335                         serviceInfos.size() + " services:\n" + changes);
    336                 writePersistentServicesLocked();
    337             } else {
    338                 Log.d(TAG, "generateServicesMap(" + mInterfaceName + "): " +
    339                         serviceInfos.size() + " services unchanged");
    340             }
    341             mPersistentServicesFileDidNotExist = false;
    342         }
    343     }
    344 
    345     private boolean containsType(ArrayList<ServiceInfo<V>> serviceInfos, V type) {
    346         for (int i = 0, N = serviceInfos.size(); i < N; i++) {
    347             if (serviceInfos.get(i).type.equals(type)) {
    348                 return true;
    349             }
    350         }
    351 
    352         return false;
    353     }
    354 
    355     private boolean containsTypeAndUid(ArrayList<ServiceInfo<V>> serviceInfos, V type, int uid) {
    356         for (int i = 0, N = serviceInfos.size(); i < N; i++) {
    357             final ServiceInfo<V> serviceInfo = serviceInfos.get(i);
    358             if (serviceInfo.type.equals(type) && serviceInfo.uid == uid) {
    359                 return true;
    360             }
    361         }
    362 
    363         return false;
    364     }
    365 
    366     private ServiceInfo<V> parseServiceInfo(ResolveInfo service)
    367             throws XmlPullParserException, IOException {
    368         android.content.pm.ServiceInfo si = service.serviceInfo;
    369         ComponentName componentName = new ComponentName(si.packageName, si.name);
    370 
    371         PackageManager pm = mContext.getPackageManager();
    372 
    373         XmlResourceParser parser = null;
    374         try {
    375             parser = si.loadXmlMetaData(pm, mMetaDataName);
    376             if (parser == null) {
    377                 throw new XmlPullParserException("No " + mMetaDataName + " meta-data");
    378             }
    379 
    380             AttributeSet attrs = Xml.asAttributeSet(parser);
    381 
    382             int type;
    383             while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
    384                     && type != XmlPullParser.START_TAG) {
    385             }
    386 
    387             String nodeName = parser.getName();
    388             if (!mAttributesName.equals(nodeName)) {
    389                 throw new XmlPullParserException(
    390                         "Meta-data does not start with " + mAttributesName +  " tag");
    391             }
    392 
    393             V v = parseServiceAttributes(pm.getResourcesForApplication(si.applicationInfo),
    394                     si.packageName, attrs);
    395             if (v == null) {
    396                 return null;
    397             }
    398             final android.content.pm.ServiceInfo serviceInfo = service.serviceInfo;
    399             final ApplicationInfo applicationInfo = serviceInfo.applicationInfo;
    400             final int uid = applicationInfo.uid;
    401             return new ServiceInfo<V>(v, componentName, uid);
    402         } catch (NameNotFoundException e) {
    403             throw new XmlPullParserException(
    404                     "Unable to load resources for pacakge " + si.packageName);
    405         } finally {
    406             if (parser != null) parser.close();
    407         }
    408     }
    409 
    410     /**
    411      * Read all sync status back in to the initial engine state.
    412      */
    413     private void readPersistentServicesLocked() {
    414         mPersistentServices = Maps.newHashMap();
    415         if (mSerializerAndParser == null) {
    416             return;
    417         }
    418         FileInputStream fis = null;
    419         try {
    420             mPersistentServicesFileDidNotExist = !mPersistentServicesFile.getBaseFile().exists();
    421             if (mPersistentServicesFileDidNotExist) {
    422                 return;
    423             }
    424             fis = mPersistentServicesFile.openRead();
    425             XmlPullParser parser = Xml.newPullParser();
    426             parser.setInput(fis, null);
    427             int eventType = parser.getEventType();
    428             while (eventType != XmlPullParser.START_TAG) {
    429                 eventType = parser.next();
    430             }
    431             String tagName = parser.getName();
    432             if ("services".equals(tagName)) {
    433                 eventType = parser.next();
    434                 do {
    435                     if (eventType == XmlPullParser.START_TAG && parser.getDepth() == 2) {
    436                         tagName = parser.getName();
    437                         if ("service".equals(tagName)) {
    438                             V service = mSerializerAndParser.createFromXml(parser);
    439                             if (service == null) {
    440                                 break;
    441                             }
    442                             String uidString = parser.getAttributeValue(null, "uid");
    443                             int uid = Integer.parseInt(uidString);
    444                             mPersistentServices.put(service, uid);
    445                         }
    446                     }
    447                     eventType = parser.next();
    448                 } while (eventType != XmlPullParser.END_DOCUMENT);
    449             }
    450         } catch (Exception e) {
    451             Log.w(TAG, "Error reading persistent services, starting from scratch", e);
    452         } finally {
    453             if (fis != null) {
    454                 try {
    455                     fis.close();
    456                 } catch (java.io.IOException e1) {
    457                 }
    458             }
    459         }
    460     }
    461 
    462     /**
    463      * Write all sync status to the sync status file.
    464      */
    465     private void writePersistentServicesLocked() {
    466         if (mSerializerAndParser == null) {
    467             return;
    468         }
    469         FileOutputStream fos = null;
    470         try {
    471             fos = mPersistentServicesFile.startWrite();
    472             XmlSerializer out = new FastXmlSerializer();
    473             out.setOutput(fos, "utf-8");
    474             out.startDocument(null, true);
    475             out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
    476             out.startTag(null, "services");
    477             for (Map.Entry<V, Integer> service : mPersistentServices.entrySet()) {
    478                 out.startTag(null, "service");
    479                 out.attribute(null, "uid", Integer.toString(service.getValue()));
    480                 mSerializerAndParser.writeAsXml(service.getKey(), out);
    481                 out.endTag(null, "service");
    482             }
    483             out.endTag(null, "services");
    484             out.endDocument();
    485             mPersistentServicesFile.finishWrite(fos);
    486         } catch (java.io.IOException e1) {
    487             Log.w(TAG, "Error writing accounts", e1);
    488             if (fos != null) {
    489                 mPersistentServicesFile.failWrite(fos);
    490             }
    491         }
    492     }
    493 
    494     public abstract V parseServiceAttributes(Resources res,
    495             String packageName, AttributeSet attrs);
    496 }
    497