blob: 004e963b27fe80b62bc41bfa343a4147a1437845 [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 executeNesting; // number of outstanding operations keeping foreground.
88 long executingStart; // start time of last execute request.
89 int crashCount; // number of times proc has crashed with service running
90 int totalRestartCount; // number of times we have had to restart.
91 int restartCount; // number of restarts performed in a row.
92 long restartDelay; // delay until next restart attempt.
93 long restartTime; // time of last restart.
94 long nextRestartTime; // time when restartDelay will expire.
95
Dianne Hackbornf210d6b2009-04-13 18:42:49 -070096 String stringName; // caching of toString
97
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070098 private int lastStartId; // identifier of most recent start request.
99
Dianne Hackborn7e269642010-08-25 19:50:20 -0700100 static class StartItem {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700101 final ServiceRecord sr;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700102 final boolean taskRemoved;
Dianne Hackborn39792d22010-08-19 18:01:52 -0700103 final int id;
104 final Intent intent;
105 final int targetPermissionUid;
106 long deliveredTime;
107 int deliveryCount;
108 int doneExecutingCount;
Dianne Hackborn7e269642010-08-25 19:50:20 -0700109 UriPermissionOwner uriPermissions;
Dianne Hackborn39792d22010-08-19 18:01:52 -0700110
111 String stringName; // caching of toString
112
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700113 StartItem(ServiceRecord _sr, boolean _taskRemoved, int _id, Intent _intent,
114 int _targetPermissionUid) {
Dianne Hackborn39792d22010-08-19 18:01:52 -0700115 sr = _sr;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700116 taskRemoved = _taskRemoved;
Dianne Hackborn39792d22010-08-19 18:01:52 -0700117 id = _id;
118 intent = _intent;
119 targetPermissionUid = _targetPermissionUid;
120 }
121
Dianne Hackborn7e269642010-08-25 19:50:20 -0700122 UriPermissionOwner getUriPermissionsLocked() {
123 if (uriPermissions == null) {
124 uriPermissions = new UriPermissionOwner(sr.ams, this);
125 }
126 return uriPermissions;
127 }
128
Dianne Hackborn39792d22010-08-19 18:01:52 -0700129 void removeUriPermissionsLocked() {
Dianne Hackborn7e269642010-08-25 19:50:20 -0700130 if (uriPermissions != null) {
131 uriPermissions.removeUriPermissionsLocked();
132 uriPermissions = null;
Dianne Hackborn39792d22010-08-19 18:01:52 -0700133 }
134 }
135
136 public String toString() {
137 if (stringName != null) {
138 return stringName;
139 }
140 StringBuilder sb = new StringBuilder(128);
141 sb.append("ServiceRecord{")
142 .append(Integer.toHexString(System.identityHashCode(sr)))
143 .append(' ').append(sr.shortName)
144 .append(" StartItem ")
145 .append(Integer.toHexString(System.identityHashCode(this)))
146 .append(" id=").append(id).append('}');
147 return stringName = sb.toString();
148 }
149 }
150
151 final ArrayList<StartItem> deliveredStarts = new ArrayList<StartItem>();
152 // start() arguments which been delivered.
153 final ArrayList<StartItem> pendingStarts = new ArrayList<StartItem>();
154 // start() arguments that haven't yet been delivered.
155
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700156 void dumpStartList(PrintWriter pw, String prefix, List<StartItem> list, long now) {
157 final int N = list.size();
158 for (int i=0; i<N; i++) {
159 StartItem si = list.get(i);
160 pw.print(prefix); pw.print("#"); pw.print(i);
161 pw.print(" id="); pw.print(si.id);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700162 if (now != 0) {
163 pw.print(" dur=");
164 TimeUtils.formatDuration(si.deliveredTime, now, pw);
165 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700166 if (si.deliveryCount != 0) {
167 pw.print(" dc="); pw.print(si.deliveryCount);
168 }
169 if (si.doneExecutingCount != 0) {
170 pw.print(" dxc="); pw.print(si.doneExecutingCount);
171 }
Dianne Hackborn39792d22010-08-19 18:01:52 -0700172 pw.println("");
173 pw.print(prefix); pw.print(" intent=");
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700174 if (si.intent != null) pw.println(si.intent.toString());
175 else pw.println("null");
Dianne Hackborn39792d22010-08-19 18:01:52 -0700176 if (si.targetPermissionUid >= 0) {
177 pw.print(prefix); pw.print(" targetPermissionUid=");
178 pw.println(si.targetPermissionUid);
179 }
Dianne Hackborn7e269642010-08-25 19:50:20 -0700180 if (si.uriPermissions != null) {
181 if (si.uriPermissions.readUriPermissions != null) {
182 pw.print(prefix); pw.print(" readUriPermissions=");
183 pw.println(si.uriPermissions.readUriPermissions);
184 }
185 if (si.uriPermissions.writeUriPermissions != null) {
186 pw.print(prefix); pw.print(" writeUriPermissions=");
187 pw.println(si.uriPermissions.writeUriPermissions);
188 }
Dianne Hackborn39792d22010-08-19 18:01:52 -0700189 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700190 }
191 }
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700194 pw.print(prefix); pw.print("intent={");
195 pw.print(intent.getIntent().toShortString(true, false));
196 pw.println('}');
197 pw.print(prefix); pw.print("packageName="); pw.println(packageName);
198 pw.print(prefix); pw.print("processName="); pw.println(processName);
199 if (permission != null) {
200 pw.print(prefix); pw.print("permission="); pw.println(permission);
201 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700202 long now = SystemClock.uptimeMillis();
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700203 long nowReal = SystemClock.elapsedRealtime();
204 pw.print(prefix); pw.print("baseDir="); pw.println(baseDir);
Marco Nelissena301fe62011-05-04 08:44:59 -0700205 if (!resDir.equals(baseDir)) {
206 pw.print(prefix); pw.print("resDir="); pw.println(resDir);
207 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700208 pw.print(prefix); pw.print("dataDir="); pw.println(dataDir);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700209 pw.print(prefix); pw.print("app="); pw.println(app);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700210 if (isForeground || foregroundId != 0) {
211 pw.print(prefix); pw.print("isForeground="); pw.print(isForeground);
212 pw.print(" foregroundId="); pw.print(foregroundId);
213 pw.print(" foregroundNoti="); pw.println(foregroundNoti);
214 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700215 pw.print(prefix); pw.print("createTime=");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700216 TimeUtils.formatDuration(createTime, nowReal, pw);
217 pw.print(" lastActivity=");
218 TimeUtils.formatDuration(lastActivity, now, pw);
219 pw.println("");
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700220 pw.print(prefix); pw.print("executingStart=");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700221 TimeUtils.formatDuration(executingStart, now, pw);
222 pw.print(" restartTime=");
223 TimeUtils.formatDuration(restartTime, now, pw);
224 pw.println("");
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700225 if (startRequested || lastStartId != 0) {
226 pw.print(prefix); pw.print("startRequested="); pw.print(startRequested);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700227 pw.print(" stopIfKilled="); pw.print(stopIfKilled);
228 pw.print(" callStart="); pw.print(callStart);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700229 pw.print(" lastStartId="); pw.println(lastStartId);
230 }
231 if (executeNesting != 0 || crashCount != 0 || restartCount != 0
232 || restartDelay != 0 || nextRestartTime != 0) {
233 pw.print(prefix); pw.print("executeNesting="); pw.print(executeNesting);
234 pw.print(" restartCount="); pw.print(restartCount);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700235 pw.print(" restartDelay=");
236 TimeUtils.formatDuration(restartDelay, now, pw);
237 pw.print(" nextRestartTime=");
238 TimeUtils.formatDuration(nextRestartTime, now, pw);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700239 pw.print(" crashCount="); pw.println(crashCount);
240 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700241 if (deliveredStarts.size() > 0) {
242 pw.print(prefix); pw.println("Delivered Starts:");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700243 dumpStartList(pw, prefix, deliveredStarts, now);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700244 }
245 if (pendingStarts.size() > 0) {
246 pw.print(prefix); pw.println("Pending Starts:");
247 dumpStartList(pw, prefix, pendingStarts, 0);
248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 if (bindings.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 Iterator<IntentBindRecord> it = bindings.values().iterator();
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700251 pw.print(prefix); pw.println("Bindings:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 while (it.hasNext()) {
253 IntentBindRecord b = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700254 pw.print(prefix); pw.print("* IntentBindRecord{");
255 pw.print(Integer.toHexString(System.identityHashCode(b)));
256 pw.println("}:");
257 b.dumpInService(pw, prefix + " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 }
259 }
260 if (connections.size() > 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700261 pw.print(prefix); pw.println("All Connections:");
Dianne Hackborn43d9ac82010-08-25 15:06:25 -0700262 Iterator<ArrayList<ConnectionRecord>> it = connections.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 while (it.hasNext()) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -0700264 ArrayList<ConnectionRecord> c = it.next();
265 for (int i=0; i<c.size(); i++) {
266 pw.print(prefix); pw.print(" "); pw.println(c.get(i));
267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 }
269 }
270 }
271
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800272 ServiceRecord(ActivityManagerService ams,
273 BatteryStatsImpl.Uid.Pkg.Serv servStats, ComponentName name,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 Intent.FilterComparison intent, ServiceInfo sInfo, Runnable restarter) {
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800275 this.ams = ams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 this.stats = servStats;
277 this.name = name;
278 shortName = name.flattenToShortString();
279 this.intent = intent;
280 serviceInfo = sInfo;
281 appInfo = sInfo.applicationInfo;
282 packageName = sInfo.applicationInfo.packageName;
283 processName = sInfo.processName;
284 permission = sInfo.permission;
285 baseDir = sInfo.applicationInfo.sourceDir;
286 resDir = sInfo.applicationInfo.publicSourceDir;
287 dataDir = sInfo.applicationInfo.dataDir;
288 exported = sInfo.exported;
289 this.restarter = restarter;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700290 createTime = SystemClock.elapsedRealtime();
291 lastActivity = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 }
293
294 public AppBindRecord retrieveAppBindingLocked(Intent intent,
295 ProcessRecord app) {
296 Intent.FilterComparison filter = new Intent.FilterComparison(intent);
297 IntentBindRecord i = bindings.get(filter);
298 if (i == null) {
299 i = new IntentBindRecord(this, filter);
300 bindings.put(filter, i);
301 }
302 AppBindRecord a = i.apps.get(app);
303 if (a != null) {
304 return a;
305 }
306 a = new AppBindRecord(this, i, app);
307 i.apps.put(app, a);
308 return a;
309 }
310
311 public void resetRestartCounter() {
312 restartCount = 0;
313 restartDelay = 0;
314 restartTime = 0;
315 }
316
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700317 public StartItem findDeliveredStart(int id, boolean remove) {
318 final int N = deliveredStarts.size();
319 for (int i=0; i<N; i++) {
320 StartItem si = deliveredStarts.get(i);
321 if (si.id == id) {
322 if (remove) deliveredStarts.remove(i);
323 return si;
324 }
325 }
326
327 return null;
328 }
329
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700330 public int getLastStartId() {
331 return lastStartId;
332 }
333
334 public int makeNextStartId() {
335 lastStartId++;
336 if (lastStartId < 1) {
337 lastStartId = 1;
338 }
339 return lastStartId;
340 }
341
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700342 public void postNotification() {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400343 final int appUid = appInfo.uid;
344 final int appPid = app.pid;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700345 if (foregroundId != 0 && foregroundNoti != null) {
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800346 // Do asynchronous communication with notification manager to
347 // avoid deadlocks.
348 final String localPackageName = packageName;
349 final int localForegroundId = foregroundId;
350 final Notification localForegroundNoti = foregroundNoti;
351 ams.mHandler.post(new Runnable() {
352 public void run() {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400353 NotificationManagerService nm =
354 (NotificationManagerService) NotificationManager.getService();
355 if (nm == null) {
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800356 return;
357 }
358 try {
359 int[] outId = new int[1];
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400360 nm.enqueueNotificationInternal(localPackageName, appUid, appPid,
361 null, localForegroundId, localForegroundNoti, outId);
Joe Onorato34fcf972010-02-18 07:45:17 -0500362 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800363 Slog.w(ActivityManagerService.TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800364 "Error showing notification for service", e);
365 // If it gave us a garbage notification, it doesn't
366 // get to be foreground.
367 ams.setServiceForeground(name, ServiceRecord.this,
Joe Onoratoeaa07182010-09-23 16:49:20 -0700368 0, null, true);
369 ams.crashApplication(appUid, appPid, localPackageName,
370 "Bad notification for startForeground: " + e);
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800371 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700372 }
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800373 });
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700374 }
375 }
376
377 public void cancelNotification() {
378 if (foregroundId != 0) {
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800379 // Do asynchronous communication with notification manager to
380 // avoid deadlocks.
381 final String localPackageName = packageName;
382 final int localForegroundId = foregroundId;
383 ams.mHandler.post(new Runnable() {
384 public void run() {
385 INotificationManager inm = NotificationManager.getService();
386 if (inm == null) {
387 return;
388 }
389 try {
390 inm.cancelNotification(localPackageName, localForegroundId);
Joe Onorato34fcf972010-02-18 07:45:17 -0500391 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800392 Slog.w(ActivityManagerService.TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800393 "Error canceling notification for service", e);
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800394 } catch (RemoteException e) {
395 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700396 }
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -0800397 });
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700398 }
399 }
400
Dianne Hackborn39792d22010-08-19 18:01:52 -0700401 public void clearDeliveredStartsLocked() {
402 for (int i=deliveredStarts.size()-1; i>=0; i--) {
403 deliveredStarts.get(i).removeUriPermissionsLocked();
404 }
405 deliveredStarts.clear();
406 }
407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700409 if (stringName != null) {
410 return stringName;
411 }
412 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700413 sb.append("ServiceRecord{")
414 .append(Integer.toHexString(System.identityHashCode(this)))
415 .append(' ').append(shortName).append('}');
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700416 return stringName = sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418}