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