ServiceRecord.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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.content.ComponentName;
20import android.content.Intent;
21import android.content.pm.ApplicationInfo;
22import android.content.pm.ServiceInfo;
23import android.os.Binder;
24import android.os.IBinder;
25import android.os.SystemClock;
26
27import java.io.PrintWriter;
28import java.util.ArrayList;
29import java.util.HashMap;
30import java.util.Iterator;
31import java.util.List;
32
33/**
34 * A running application service.
35 */
36class ServiceRecord extends Binder {
37    final BatteryStats.Uid.Pkg.Serv stats;
38    final ComponentName name; // service component.
39    final String shortName; // name.flattenToShortString().
40    final Intent.FilterComparison intent;
41                            // original intent used to find service.
42    final ServiceInfo serviceInfo;
43                            // all information about the service.
44    final ApplicationInfo appInfo;
45                            // information about service's app.
46    final String packageName; // the package implementing intent's component
47    final String processName; // process where this component wants to run
48    final String permission;// permission needed to access service
49    final String baseDir;   // where activity source (resources etc) located
50    final String resDir;   // where public activity source (public resources etc) located
51    final String dataDir;   // where activity data should go
52    final boolean exported; // from ServiceInfo.exported
53    final Runnable restarter; // used to schedule retries of starting the service
54    final long createTime;  // when this service was created
55    final HashMap<Intent.FilterComparison, IntentBindRecord> bindings
56            = new HashMap<Intent.FilterComparison, IntentBindRecord>();
57                            // All active bindings to the service.
58    final HashMap<IBinder, ConnectionRecord> connections
59            = new HashMap<IBinder, ConnectionRecord>();
60                            // IBinder -> ConnectionRecord of all bound clients
61    final List<Intent> startArgs = new ArrayList<Intent>();
62                            // start() arguments that haven't yet been delivered.
63
64    ProcessRecord app;  // where this service is running or null.
65    boolean isForeground;   // asked to run as a foreground service?
66    long lastActivity;      // last time there was some activity on the service.
67    boolean startRequested; // someone explicitly called start?
68    int lastStartId;        // identifier of most recent start request.
69    int executeNesting;     // number of outstanding operations keeping foreground.
70    long executingStart;    // start time of last execute request.
71    int crashCount;         // number of times proc has crashed with service running
72    int totalRestartCount;  // number of times we have had to restart.
73    int restartCount;       // number of restarts performed in a row.
74    long restartDelay;      // delay until next restart attempt.
75    long restartTime;       // time of last restart.
76    long nextRestartTime;   // time when restartDelay will expire.
77
78    void dump(PrintWriter pw, String prefix) {
79        pw.println(prefix + this);
80        pw.println(prefix + "intent=" + intent.getIntent());
81        pw.println(prefix + "packageName=" + packageName);
82        pw.println(prefix + "processName=" + processName);
83        pw.println(prefix + "permission=" + permission);
84        pw.println(prefix + "baseDir=" + baseDir+ " resDir=" + resDir + " dataDir=" + dataDir);
85        pw.println(prefix + "app=" + app);
86        pw.println(prefix + "isForeground=" + isForeground
87                + " lastActivity=" + lastActivity);
88        pw.println(prefix + "startRequested=" + startRequested
89              + " startId=" + lastStartId
90              + " executeNesting=" + executeNesting
91              + " executingStart=" + executingStart
92              + " crashCount=" + crashCount);
93        pw.println(prefix + "totalRestartCount=" + totalRestartCount
94                + " restartCount=" + restartCount
95                + " restartDelay=" + restartDelay
96                + " restartTime=" + restartTime
97                + " nextRestartTime=" + nextRestartTime);
98        if (bindings.size() > 0) {
99            pw.println(prefix + "Bindings:");
100            Iterator<IntentBindRecord> it = bindings.values().iterator();
101            while (it.hasNext()) {
102                IntentBindRecord b = it.next();
103                pw.println(prefix + "Binding " + b);
104                b.dump(pw, prefix + "  ");
105            }
106        }
107        if (connections.size() > 0) {
108            pw.println(prefix + "All Connections:");
109            Iterator<ConnectionRecord> it = connections.values().iterator();
110            while (it.hasNext()) {
111                ConnectionRecord c = it.next();
112                pw.println(prefix + "  " + c);
113            }
114        }
115    }
116
117    ServiceRecord(BatteryStats.Uid.Pkg.Serv servStats, ComponentName name,
118            Intent.FilterComparison intent, ServiceInfo sInfo, Runnable restarter) {
119        this.stats = servStats;
120        this.name = name;
121        shortName = name.flattenToShortString();
122        this.intent = intent;
123        serviceInfo = sInfo;
124        appInfo = sInfo.applicationInfo;
125        packageName = sInfo.applicationInfo.packageName;
126        processName = sInfo.processName;
127        permission = sInfo.permission;
128        baseDir = sInfo.applicationInfo.sourceDir;
129        resDir = sInfo.applicationInfo.publicSourceDir;
130        dataDir = sInfo.applicationInfo.dataDir;
131        exported = sInfo.exported;
132        this.restarter = restarter;
133        createTime = lastActivity = SystemClock.uptimeMillis();
134    }
135
136    public AppBindRecord retrieveAppBindingLocked(Intent intent,
137            ProcessRecord app) {
138        Intent.FilterComparison filter = new Intent.FilterComparison(intent);
139        IntentBindRecord i = bindings.get(filter);
140        if (i == null) {
141            i = new IntentBindRecord(this, filter);
142            bindings.put(filter, i);
143        }
144        AppBindRecord a = i.apps.get(app);
145        if (a != null) {
146            return a;
147        }
148        a = new AppBindRecord(this, i, app);
149        i.apps.put(app, a);
150        return a;
151    }
152
153    public void resetRestartCounter() {
154        restartCount = 0;
155        restartDelay = 0;
156        restartTime = 0;
157    }
158
159    public String toString() {
160        return "ServiceRecord{"
161            + Integer.toHexString(System.identityHashCode(this))
162            + " " + shortName + "}";
163    }
164}
165