ServiceRecord.java revision d8a43f61680bacf0d4b52a03ff3c7a07307377fc
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.app.INotificationManager;
22import android.app.Notification;
23import android.app.NotificationManager;
24import android.content.ComponentName;
25import android.content.Intent;
26import android.content.pm.ApplicationInfo;
27import android.content.pm.ServiceInfo;
28import android.os.Binder;
29import android.os.IBinder;
30import android.os.RemoteException;
31import android.os.SystemClock;
32
33import java.io.PrintWriter;
34import java.util.ArrayList;
35import java.util.HashMap;
36import java.util.Iterator;
37import java.util.List;
38
39/**
40 * A running application service.
41 */
42class ServiceRecord extends Binder {
43    final BatteryStatsImpl.Uid.Pkg.Serv stats;
44    final ComponentName name; // service component.
45    final String shortName; // name.flattenToShortString().
46    final Intent.FilterComparison intent;
47                            // original intent used to find service.
48    final ServiceInfo serviceInfo;
49                            // all information about the service.
50    final ApplicationInfo appInfo;
51                            // information about service's app.
52    final String packageName; // the package implementing intent's component
53    final String processName; // process where this component wants to run
54    final String permission;// permission needed to access service
55    final String baseDir;   // where activity source (resources etc) located
56    final String resDir;   // where public activity source (public resources etc) located
57    final String dataDir;   // where activity data should go
58    final boolean exported; // from ServiceInfo.exported
59    final Runnable restarter; // used to schedule retries of starting the service
60    final long createTime;  // when this service was created
61    final HashMap<Intent.FilterComparison, IntentBindRecord> bindings
62            = new HashMap<Intent.FilterComparison, IntentBindRecord>();
63                            // All active bindings to the service.
64    final HashMap<IBinder, ConnectionRecord> connections
65            = new HashMap<IBinder, ConnectionRecord>();
66                            // IBinder -> ConnectionRecord of all bound clients
67    final List<Intent> startArgs = new ArrayList<Intent>();
68                            // start() arguments that haven't yet been delivered.
69
70    ProcessRecord app;      // where this service is running or null.
71    boolean isForeground;   // is service currently in foreground mode?
72    int foregroundId;       // Notification ID of last foreground req.
73    Notification foregroundNoti; // Notification record of foreground state.
74    long lastActivity;      // last time there was some activity on the service.
75    boolean startRequested; // someone explicitly called start?
76    int lastStartId;        // identifier of most recent start request.
77    int executeNesting;     // number of outstanding operations keeping foreground.
78    long executingStart;    // start time of last execute request.
79    int crashCount;         // number of times proc has crashed with service running
80    int totalRestartCount;  // number of times we have had to restart.
81    int restartCount;       // number of restarts performed in a row.
82    long restartDelay;      // delay until next restart attempt.
83    long restartTime;       // time of last restart.
84    long nextRestartTime;   // time when restartDelay will expire.
85
86    String stringName;      // caching of toString
87
88    void dump(PrintWriter pw, String prefix) {
89        pw.print(prefix); pw.print("intent={");
90                pw.print(intent.getIntent().toShortString(true, false));
91                pw.println('}');
92        pw.print(prefix); pw.print("packageName="); pw.println(packageName);
93        pw.print(prefix); pw.print("processName="); pw.println(processName);
94        if (permission != null) {
95            pw.print(prefix); pw.print("permission="); pw.println(permission);
96        }
97        pw.print(prefix); pw.print("baseDir="); pw.print(baseDir);
98                if (!resDir.equals(baseDir)) pw.print(" resDir="); pw.print(resDir);
99                pw.print(" dataDir="); pw.println(dataDir);
100        pw.print(prefix); pw.print("app="); pw.println(app);
101        if (isForeground || foregroundId != 0) {
102            pw.print(prefix); pw.print("isForeground="); pw.print(isForeground);
103                    pw.print(" foregroundId="); pw.print(foregroundId);
104                    pw.print(" foregroundNoti="); pw.println(foregroundNoti);
105        }
106        pw.print(prefix); pw.print("lastActivity="); pw.print(lastActivity);
107                pw.print(" executingStart="); pw.print(executingStart);
108                pw.print(" restartTime="); pw.println(restartTime);
109        if (startRequested || lastStartId != 0) {
110            pw.print(prefix); pw.print("startRequested="); pw.print(startRequested);
111                    pw.print(" lastStartId="); pw.println(lastStartId);
112        }
113        if (executeNesting != 0 || crashCount != 0 || restartCount != 0
114                || restartDelay != 0 || nextRestartTime != 0) {
115            pw.print(prefix); pw.print("executeNesting="); pw.print(executeNesting);
116                    pw.print(" restartCount="); pw.print(restartCount);
117                    pw.print(" restartDelay="); pw.print(restartDelay);
118                    pw.print(" nextRestartTime="); pw.print(nextRestartTime);
119                    pw.print(" crashCount="); pw.println(crashCount);
120        }
121        if (bindings.size() > 0) {
122            Iterator<IntentBindRecord> it = bindings.values().iterator();
123            while (it.hasNext()) {
124                IntentBindRecord b = it.next();
125                pw.print(prefix); pw.print("* IntentBindRecord{");
126                        pw.print(Integer.toHexString(System.identityHashCode(b)));
127                        pw.println("}:");
128                b.dumpInService(pw, prefix + "  ");
129            }
130        }
131        if (connections.size() > 0) {
132            pw.print(prefix); pw.println("All Connections:");
133            Iterator<ConnectionRecord> it = connections.values().iterator();
134            while (it.hasNext()) {
135                ConnectionRecord c = it.next();
136                pw.print(prefix); pw.print("  "); pw.println(c);
137            }
138        }
139    }
140
141    ServiceRecord(BatteryStatsImpl.Uid.Pkg.Serv servStats, ComponentName name,
142            Intent.FilterComparison intent, ServiceInfo sInfo, Runnable restarter) {
143        this.stats = servStats;
144        this.name = name;
145        shortName = name.flattenToShortString();
146        this.intent = intent;
147        serviceInfo = sInfo;
148        appInfo = sInfo.applicationInfo;
149        packageName = sInfo.applicationInfo.packageName;
150        processName = sInfo.processName;
151        permission = sInfo.permission;
152        baseDir = sInfo.applicationInfo.sourceDir;
153        resDir = sInfo.applicationInfo.publicSourceDir;
154        dataDir = sInfo.applicationInfo.dataDir;
155        exported = sInfo.exported;
156        this.restarter = restarter;
157        createTime = lastActivity = SystemClock.uptimeMillis();
158    }
159
160    public AppBindRecord retrieveAppBindingLocked(Intent intent,
161            ProcessRecord app) {
162        Intent.FilterComparison filter = new Intent.FilterComparison(intent);
163        IntentBindRecord i = bindings.get(filter);
164        if (i == null) {
165            i = new IntentBindRecord(this, filter);
166            bindings.put(filter, i);
167        }
168        AppBindRecord a = i.apps.get(app);
169        if (a != null) {
170            return a;
171        }
172        a = new AppBindRecord(this, i, app);
173        i.apps.put(app, a);
174        return a;
175    }
176
177    public void resetRestartCounter() {
178        restartCount = 0;
179        restartDelay = 0;
180        restartTime = 0;
181    }
182
183    public void postNotification() {
184        if (foregroundId != 0 && foregroundNoti != null) {
185            INotificationManager inm = NotificationManager.getService();
186            if (inm != null) {
187                try {
188                    int[] outId = new int[1];
189                    inm.enqueueNotification(packageName, foregroundId,
190                            foregroundNoti, outId);
191                } catch (RemoteException e) {
192                }
193            }
194        }
195    }
196
197    public void cancelNotification() {
198        if (foregroundId != 0) {
199            INotificationManager inm = NotificationManager.getService();
200            if (inm != null) {
201                try {
202                    inm.cancelNotification(packageName, foregroundId);
203                } catch (RemoteException e) {
204                }
205            }
206        }
207    }
208
209    public String toString() {
210        if (stringName != null) {
211            return stringName;
212        }
213        StringBuilder sb = new StringBuilder(128);
214        sb.append("ServiceRecord{")
215            .append(Integer.toHexString(System.identityHashCode(this)))
216            .append(' ').append(shortName).append('}');
217        return stringName = sb.toString();
218    }
219}
220