1/*
2 * Copyright (C) 2006 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
17package com.android.server.am;
18
19import android.app.IActivityManager.ContentProviderHolder;
20import android.content.ComponentName;
21import android.content.pm.ApplicationInfo;
22import android.content.pm.ProviderInfo;
23import android.os.Process;
24
25import java.io.PrintWriter;
26import java.util.HashSet;
27
28class ContentProviderRecord extends ContentProviderHolder {
29    // All attached clients
30    final HashSet<ProcessRecord> clients = new HashSet<ProcessRecord>();
31    final int uid;
32    final ApplicationInfo appInfo;
33    final ComponentName name;
34    int externals;     // number of non-framework processes supported by this provider
35    ProcessRecord app; // if non-null, hosting application
36    ProcessRecord launchingApp; // if non-null, waiting for this app to be launched.
37    String stringName;
38
39    public ContentProviderRecord(ProviderInfo _info, ApplicationInfo ai) {
40        super(_info);
41        uid = ai.uid;
42        appInfo = ai;
43        name = new ComponentName(_info.packageName, _info.name);
44        noReleaseNeeded = uid == 0 || uid == Process.SYSTEM_UID;
45    }
46
47    public ContentProviderRecord(ContentProviderRecord cpr) {
48        super(cpr.info);
49        uid = cpr.uid;
50        appInfo = cpr.appInfo;
51        name = cpr.name;
52        noReleaseNeeded = cpr.noReleaseNeeded;
53    }
54
55    public boolean canRunHere(ProcessRecord app) {
56        return (info.multiprocess || info.processName.equals(app.processName))
57                && (uid == Process.SYSTEM_UID || uid == app.info.uid);
58    }
59
60    void dump(PrintWriter pw, String prefix) {
61        pw.print(prefix); pw.print("package=");
62                pw.print(info.applicationInfo.packageName);
63                pw.print("process="); pw.println(info.processName);
64        pw.print(prefix); pw.print("app="); pw.println(app);
65        if (launchingApp != null) {
66            pw.print(prefix); pw.print("launchingApp="); pw.println(launchingApp);
67        }
68        pw.print(prefix); pw.print("uid="); pw.print(uid);
69                pw.print(" provider="); pw.println(provider);
70        pw.print(prefix); pw.print("name="); pw.println(info.authority);
71        if (info.isSyncable || info.multiprocess || info.initOrder != 0) {
72            pw.print(prefix); pw.print("isSyncable="); pw.print(info.isSyncable);
73                    pw.print("multiprocess="); pw.print(info.multiprocess);
74                    pw.print(" initOrder="); pw.println(info.initOrder);
75        }
76        if (clients.size() > 0) {
77            pw.print(prefix); pw.print("clients="); pw.println(clients);
78        }
79        if (externals != 0) {
80            pw.print(prefix); pw.print("externals="); pw.println(externals);
81        }
82    }
83
84    public String toString() {
85        if (stringName != null) {
86            return stringName;
87        }
88        StringBuilder sb = new StringBuilder(128);
89        sb.append("ContentProviderRecord{");
90        sb.append(Integer.toHexString(System.identityHashCode(this)));
91        sb.append(' ');
92        sb.append(info.name);
93        sb.append('}');
94        return stringName = sb.toString();
95    }
96}
97