blob: f35a68e734d4c370cc1322b20d846f6dc2ed718f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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;
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040020import com.android.server.NotificationManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070022import android.app.INotificationManager;
23import android.app.Notification;
24import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.ComponentName;
26import android.content.Intent;
27import android.content.pm.ApplicationInfo;
28import android.content.pm.ServiceInfo;
29import android.os.Binder;
30import android.os.IBinder;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070031import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.os.SystemClock;
Joe Onorato8a9b2202010-02-26 18:56:32 -080033import android.util.Slog;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070034import android.util.TimeUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import java.io.PrintWriter;
37import java.util.ArrayList;
38import java.util.HashMap;
Dianne Hackborn39792d22010-08-19 18:01:52 -070039import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import java.util.Iterator;
41import java.util.List;
42
43/**
44 * A running application service.
45 */
46class ServiceRecord extends Binder {
Dianne Hackborn39792d22010-08-19 18:01:52 -070047 // Maximum number of delivery attempts before giving up.
48 static final int MAX_DELIVERY_COUNT = 3;
49
50 // Maximum number of times it can fail during execution before giving up.
51 static final int MAX_DONE_EXECUTING_COUNT = 6;
52
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -080053 final ActivityManagerService ams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 final BatteryStatsImpl.Uid.Pkg.Serv stats;
55 final ComponentName name; // service component.
56 final String shortName; // name.flattenToShortString().
57 final Intent.FilterComparison intent;
58 // original intent used to find service.
59 final ServiceInfo serviceInfo;
60 // all information about the service.
61 final ApplicationInfo appInfo;
62 // information about service's app.
63 final String packageName; // the package implementing intent's component
64 final String processName; // process where this component wants to run
65 final String permission;// permission needed to access service
66 final String baseDir; // where activity source (resources etc) located
67 final String resDir; // where public activity source (public resources etc) located
68 final String dataDir; // where activity data should go
69 final boolean exported; // from ServiceInfo.exported
70 final Runnable restarter; // used to schedule retries of starting the service
71 final long createTime; // when this service was created
72 final HashMap<Intent.FilterComparison, IntentBindRecord> bindings
73 = new HashMap<Intent.FilterComparison, IntentBindRecord>();
74 // All active bindings to the service.
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070075 final HashMap<IBinder, ArrayList<ConnectionRecord>> connections
76 = new HashMap<IBinder, ArrayList<ConnectionRecord>>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 // IBinder -> ConnectionRecord of all bound clients
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070079 ProcessRecord app; // where this service is running or null.
80 boolean isForeground; // is service currently in foreground mode?
81 int foregroundId; // Notification ID of last foreground req.
82 Notification foregroundNoti; // Notification record of foreground state.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 long lastActivity; // last time there was some activity on the service.
84 boolean startRequested; // someone explicitly called start?
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070085 boolean stopIfKilled; // last onStart() said to stop if service killed?
86 boolean callStart; // last onStart() has asked to alway be called on restart.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 int lastStartId; // identifier of most recent start request.
88 int executeNesting; // number of outstanding operations keeping foreground.
89 long executingStart; // start time of last execute request.
90 int crashCount; // number of times proc has crashed with service running
91 int totalRestartCount; // number of times we have had to restart.
92 int restartCount; // number of restarts performed in a row.
93 long restartDelay; // delay until next restart attempt.
94 long restartTime; // time of last restart.
95 long nextRestartTime; // time when restartDelay will expire.
96
Dianne Hackbornf210d6b2009-04-13 18:42:49 -070097 String stringName; // caching of toString
98
Dianne Hackborn7e269642010-08-25 19:50:20 -070099 static class StartItem {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700100 final ServiceRecord sr;
101 final int id;
102 final Intent intent;
103 final int targetPermissionUid;
104 long deliveredTime;
105 int deliveryCount;
106 int doneExecutingCount;
Dianne Hackborn7e269642010-08-25 19:50:20 -0700107 UriPermissionOwner uriPermissions;
Dianne Hackborn39792d22010-08-19 18:01:52 -0700108
109 String stringName; // caching of toString
110
Dianne Hackborn39792d22010-08-19 18:01:52 -0700111 StartItem(ServiceRecord _sr, int _id, Intent _intent, int _targetPermissionUid) {
112 sr = _sr;
113 id = _id;
114 intent = _intent;
115 targetPermissionUid = _targetPermissionUid;
116 }
117
Dianne Hackborn7e269642010-08-25 19:50:20 -0700118 UriPermissionOwner getUriPermissionsLocked() {
119 if (uriPermissions == null) {
120 uriPermissions = new UriPermissionOwner(sr.ams, this);
121 }
122 return uriPermissions;
123 }
124
Dianne Hackborn39792d22010-08-19 18:01:52 -0700125 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -0700126 if (uriPermissions != null) {
127 uriPermissions.removeUriPermissionsLocked();
128 uriPermissions = null;
Dianne Hackborn39792d22010-08-19 18:01:52 -0700129 }
130 }
131
132 public String toString() {
133 if (stringName != null) {
134 return stringName;
135 }
136 StringBuilder sb = new StringBuilder(128);
137 sb.append("ServiceRecord{")
138 .append(Integer.toHexString(System.identityHashCode(sr)))
139 .append(' ').append(sr.shortName)
140 .append(" StartItem ")
141 .append(Integer.toHexString(System.identityHashCode(this)))
142 .append(" id=").append(id).append('}');
143 return stringName = sb.toString();
144 }
145 }
146
147 final ArrayList<StartItem> deliveredStarts = new ArrayList<StartItem>();
148 // start() arguments which been delivered.
149 final ArrayList<StartItem> pendingStarts = new ArrayList<StartItem>();
150 // start() arguments that haven't yet been delivered.
151
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700152 void dumpStartList(PrintWriter pw, String prefix, List<StartItem> list, long now) {
153 final int N = list.size();
154 for (int i=0; i<N; i++) {
155 StartItem si = list.get(i);
156 pw.print(prefix); pw.print("#"); pw.print(i);
157 pw.print(" id="); pw.print(si.id);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700158 if (now != 0) {
159 pw.print(" dur=");
160 TimeUtils.formatDuration(si.deliveredTime, now, pw);
161 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700162 if (si.deliveryCount != 0) {
163 pw.print(" dc="); pw.print(si.deliveryCount);
164 }
165 if (si.doneExecutingCount != 0) {
166 pw.print(" dxc="); pw.print(si.doneExecutingCount);
167 }
Dianne Hackborn39792d22010-08-19 18:01:52 -0700168 pw.println("");
169 pw.print(prefix); pw.print(" intent=");
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700170 if (si.intent != null) pw.println(si.intent.toString());
171 else pw.println("null");
Dianne Hackborn39792d22010-08-19 18:01:52 -0700172 if (si.targetPermissionUid >= 0) {
173 pw.print(prefix); pw.print(" targetPermissionUid=");
174 pw.println(si.targetPermissionUid);
175 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700176 if (si.uriPermissions != null) {
177 if (si.uriPermissions.readUriPermissions != null) {
178 pw.print(prefix); pw.print(" readUriPermissions=");
179 pw.println(si.uriPermissions.readUriPermissions);
180 }
181 if (si.uriPermissions.writeUriPermissions != null) {
182 pw.print(prefix); pw.print(" writeUriPermissions=");
183 pw.println(si.uriPermissions.writeUriPermissions);
184 }
Dianne Hackborn39792d22010-08-19 18:01:52 -0700185 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700186 }
187 }
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700190 pw.print(prefix); pw.print("intent={");
191 pw.print(intent.getIntent().toShortString(true, false));
192 pw.println('}');
193 pw.print(prefix); pw.print("packageName="); pw.println(packageName);
194 pw.print(prefix); pw.print("processName="); pw.println(processName);
195 if (permission != null) {
196 pw.print(prefix); pw.print("permission="); pw.println(permission);
197 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700198 long now = SystemClock.uptimeMillis();
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700199 long nowReal = SystemClock.elapsedRealtime();
200 pw.print(prefix); pw.print("baseDir="); pw.println(baseDir);
201 if (!resDir.equals(baseDir)) pw.print(prefix); pw.print("resDir="); pw.println(resDir);
202 pw.print(prefix); pw.print("dataDir="); pw.println(dataDir);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700203 pw.print(prefix); pw.print("app="); pw.println(app);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700204 if (isForeground || foregroundId != 0) {
205 pw.print(prefix); pw.print("isForeground="); pw.print(isForeground);
206 pw.print(" foregroundId="); pw.print(foregroundId);
207 pw.print(" foregroundNoti="); pw.println(foregroundNoti);
208 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700209 pw.print(prefix); pw.print("createTime=");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700210 TimeUtils.formatDuration(createTime, nowReal, pw);
211 pw.print(" lastActivity=");
212 TimeUtils.formatDuration(lastActivity, now, pw);
213 pw.println("");
214 pw.print(prefix); pw.print(" executingStart=");
215 TimeUtils.formatDuration(executingStart, now, pw);
216 pw.print(" restartTime=");
217 TimeUtils.formatDuration(restartTime, now, pw);
218 pw.println("");
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700219 if (startRequested || lastStartId != 0) {
220 pw.print(prefix); pw.print("startRequested="); pw.print(startRequested);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700221 pw.print(" stopIfKilled="); pw.print(stopIfKilled);
222 pw.print(" callStart="); pw.print(callStart);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700223 pw.print(" lastStartId="); pw.println(lastStartId);
224 }
225 if (executeNesting != 0 || crashCount != 0 || restartCount != 0
226 || restartDelay != 0 || nextRestartTime != 0) {
227 pw.print(prefix); pw.print("executeNesting="); pw.print(executeNesting);
228 pw.print(" restartCount="); pw.print(restartCount);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700229 pw.print(" restartDelay=");
230 TimeUtils.formatDuration(restartDelay, now, pw);
231 pw.print(" nextRestartTime=");
232 TimeUtils.formatDuration(nextRestartTime, now, pw);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700233 pw.print(" crashCount="); pw.println(crashCount);
234 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700235 if (deliveredStarts.size() > 0) {
236 pw.print(prefix); pw.println("Delivered Starts:");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700237 dumpStartList(pw, prefix, deliveredStarts, now);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700238 }
239 if (pendingStarts.size() > 0) {
240 pw.print(prefix); pw.println("Pending Starts:");
241 dumpStartList(pw, prefix, pendingStarts, 0);
242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 if (bindings.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 Iterator<IntentBindRecord> it = bindings.values().iterator();
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700245 pw.print(prefix); pw.println("Bindings:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 while (it.hasNext()) {
247 IntentBindRecord b = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700248 pw.print(prefix); pw.print("* IntentBindRecord{");
249 pw.print(Integer.toHexString(System.identityHashCode(b)));
250 pw.println("}:");
251 b.dumpInService(pw, prefix + " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 }
253 }
254 if (connections.size() > 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700255 pw.print(prefix); pw.println("All Connections:");
Dianne Hackborn43d9ac82010-08-25 15:06:25 -0700256 Iterator<ArrayList<ConnectionRecord>> it = connections.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 while (it.hasNext()) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -0700258 ArrayList<ConnectionRecord> c = it.next();
259 for (int i=0; i<c.size(); i++) {
260 pw.print(prefix); pw.print(" "); pw.println(c.get(i));
261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 }
263 }
264 }
265
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800266 ServiceRecord(ActivityManagerService ams,
267 BatteryStatsImpl.Uid.Pkg.Serv servStats, ComponentName name,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 Intent.FilterComparison intent, ServiceInfo sInfo, Runnable restarter) {
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800269 this.ams = ams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 this.stats = servStats;
271 this.name = name;
272 shortName = name.flattenToShortString();
273 this.intent = intent;
274 serviceInfo = sInfo;
275 appInfo = sInfo.applicationInfo;
276 packageName = sInfo.applicationInfo.packageName;
277 processName = sInfo.processName;
278 permission = sInfo.permission;
279 baseDir = sInfo.applicationInfo.sourceDir;
280 resDir = sInfo.applicationInfo.publicSourceDir;
281 dataDir = sInfo.applicationInfo.dataDir;
282 exported = sInfo.exported;
283 this.restarter = restarter;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700284 createTime = SystemClock.elapsedRealtime();
285 lastActivity = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 }
287
288 public AppBindRecord retrieveAppBindingLocked(Intent intent,
289 ProcessRecord app) {
290 Intent.FilterComparison filter = new Intent.FilterComparison(intent);
291 IntentBindRecord i = bindings.get(filter);
292 if (i == null) {
293 i = new IntentBindRecord(this, filter);
294 bindings.put(filter, i);
295 }
296 AppBindRecord a = i.apps.get(app);
297 if (a != null) {
298 return a;
299 }
300 a = new AppBindRecord(this, i, app);
301 i.apps.put(app, a);
302 return a;
303 }
304
305 public void resetRestartCounter() {
306 restartCount = 0;
307 restartDelay = 0;
308 restartTime = 0;
309 }
310
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700311 public StartItem findDeliveredStart(int id, boolean remove) {
312 final int N = deliveredStarts.size();
313 for (int i=0; i<N; i++) {
314 StartItem si = deliveredStarts.get(i);
315 if (si.id == id) {
316 if (remove) deliveredStarts.remove(i);
317 return si;
318 }
319 }
320
321 return null;
322 }
323
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700324 public void postNotification() {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400325 final int appUid = appInfo.uid;
326 final int appPid = app.pid;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700327 if (foregroundId != 0 && foregroundNoti != null) {
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800328 // Do asynchronous communication with notification manager to
329 // avoid deadlocks.
330 final String localPackageName = packageName;
331 final int localForegroundId = foregroundId;
332 final Notification localForegroundNoti = foregroundNoti;
333 ams.mHandler.post(new Runnable() {
334 public void run() {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400335 NotificationManagerService nm =
336 (NotificationManagerService) NotificationManager.getService();
337 if (nm == null) {
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800338 return;
339 }
340 try {
341 int[] outId = new int[1];
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400342 nm.enqueueNotificationInternal(localPackageName, appUid, appPid,
343 null, localForegroundId, localForegroundNoti, outId);
Joe Onorato34fcf972010-02-18 07:45:17 -0500344 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800345 Slog.w(ActivityManagerService.TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800346 "Error showing notification for service", e);
347 // If it gave us a garbage notification, it doesn't
348 // get to be foreground.
349 ams.setServiceForeground(name, ServiceRecord.this,
350 localForegroundId, null, true);
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800351 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700352 }
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800353 });
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700354 }
355 }
356
357 public void cancelNotification() {
358 if (foregroundId != 0) {
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800359 // Do asynchronous communication with notification manager to
360 // avoid deadlocks.
361 final String localPackageName = packageName;
362 final int localForegroundId = foregroundId;
363 ams.mHandler.post(new Runnable() {
364 public void run() {
365 INotificationManager inm = NotificationManager.getService();
366 if (inm == null) {
367 return;
368 }
369 try {
370 inm.cancelNotification(localPackageName, localForegroundId);
Joe Onorato34fcf972010-02-18 07:45:17 -0500371 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800372 Slog.w(ActivityManagerService.TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800373 "Error canceling notification for service", e);
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800374 } catch (RemoteException e) {
375 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700376 }
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800377 });
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700378 }
379 }
380
Dianne Hackborn39792d22010-08-19 18:01:52 -0700381 public void clearDeliveredStartsLocked() {
382 for (int i=deliveredStarts.size()-1; i>=0; i--) {
383 deliveredStarts.get(i).removeUriPermissionsLocked();
384 }
385 deliveredStarts.clear();
386 }
387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700389 if (stringName != null) {
390 return stringName;
391 }
392 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700393 sb.append("ServiceRecord{")
394 .append(Integer.toHexString(System.identityHashCode(this)))
395 .append(' ').append(shortName).append('}');
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700396 return stringName = sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 }
398}