The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.server.am; |
| 18 | |
| 19 | import com.android.internal.os.BatteryStatsImpl; |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 20 | import com.android.server.NotificationManagerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 22 | import android.app.INotificationManager; |
| 23 | import android.app.Notification; |
| 24 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.ComponentName; |
| 26 | import android.content.Intent; |
| 27 | import android.content.pm.ApplicationInfo; |
| 28 | import android.content.pm.ServiceInfo; |
| 29 | import android.os.Binder; |
| 30 | import android.os.IBinder; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 31 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.os.SystemClock; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 33 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
| 35 | import java.io.PrintWriter; |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.HashMap; |
| 38 | import java.util.Iterator; |
| 39 | import java.util.List; |
| 40 | |
| 41 | /** |
| 42 | * A running application service. |
| 43 | */ |
| 44 | class ServiceRecord extends Binder { |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 45 | final ActivityManagerService ams; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | final BatteryStatsImpl.Uid.Pkg.Serv stats; |
| 47 | final ComponentName name; // service component. |
| 48 | final String shortName; // name.flattenToShortString(). |
| 49 | final Intent.FilterComparison intent; |
| 50 | // original intent used to find service. |
| 51 | final ServiceInfo serviceInfo; |
| 52 | // all information about the service. |
| 53 | final ApplicationInfo appInfo; |
| 54 | // information about service's app. |
| 55 | final String packageName; // the package implementing intent's component |
| 56 | final String processName; // process where this component wants to run |
| 57 | final String permission;// permission needed to access service |
| 58 | final String baseDir; // where activity source (resources etc) located |
| 59 | final String resDir; // where public activity source (public resources etc) located |
| 60 | final String dataDir; // where activity data should go |
| 61 | final boolean exported; // from ServiceInfo.exported |
| 62 | final Runnable restarter; // used to schedule retries of starting the service |
| 63 | final long createTime; // when this service was created |
| 64 | final HashMap<Intent.FilterComparison, IntentBindRecord> bindings |
| 65 | = new HashMap<Intent.FilterComparison, IntentBindRecord>(); |
| 66 | // All active bindings to the service. |
| 67 | final HashMap<IBinder, ConnectionRecord> connections |
| 68 | = new HashMap<IBinder, ConnectionRecord>(); |
| 69 | // IBinder -> ConnectionRecord of all bound clients |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 70 | |
| 71 | // Maximum number of delivery attempts before giving up. |
| 72 | static final int MAX_DELIVERY_COUNT = 3; |
| 73 | |
| 74 | // Maximum number of times it can fail during execution before giving up. |
| 75 | static final int MAX_DONE_EXECUTING_COUNT = 6; |
| 76 | |
| 77 | static class StartItem { |
| 78 | final int id; |
| 79 | final Intent intent; |
| 80 | long deliveredTime; |
| 81 | int deliveryCount; |
| 82 | int doneExecutingCount; |
| 83 | |
| 84 | StartItem(int _id, Intent _intent) { |
| 85 | id = _id; |
| 86 | intent = _intent; |
| 87 | } |
| 88 | } |
| 89 | final ArrayList<StartItem> deliveredStarts = new ArrayList<StartItem>(); |
| 90 | // start() arguments which been delivered. |
| 91 | final ArrayList<StartItem> pendingStarts = new ArrayList<StartItem>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | // start() arguments that haven't yet been delivered. |
| 93 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 94 | ProcessRecord app; // where this service is running or null. |
| 95 | boolean isForeground; // is service currently in foreground mode? |
| 96 | int foregroundId; // Notification ID of last foreground req. |
| 97 | Notification foregroundNoti; // Notification record of foreground state. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | long lastActivity; // last time there was some activity on the service. |
| 99 | boolean startRequested; // someone explicitly called start? |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 100 | boolean stopIfKilled; // last onStart() said to stop if service killed? |
| 101 | boolean callStart; // last onStart() has asked to alway be called on restart. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | int lastStartId; // identifier of most recent start request. |
| 103 | int executeNesting; // number of outstanding operations keeping foreground. |
| 104 | long executingStart; // start time of last execute request. |
| 105 | int crashCount; // number of times proc has crashed with service running |
| 106 | int totalRestartCount; // number of times we have had to restart. |
| 107 | int restartCount; // number of restarts performed in a row. |
| 108 | long restartDelay; // delay until next restart attempt. |
| 109 | long restartTime; // time of last restart. |
| 110 | long nextRestartTime; // time when restartDelay will expire. |
| 111 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 112 | String stringName; // caching of toString |
| 113 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 114 | void dumpStartList(PrintWriter pw, String prefix, List<StartItem> list, long now) { |
| 115 | final int N = list.size(); |
| 116 | for (int i=0; i<N; i++) { |
| 117 | StartItem si = list.get(i); |
| 118 | pw.print(prefix); pw.print("#"); pw.print(i); |
| 119 | pw.print(" id="); pw.print(si.id); |
| 120 | if (now != 0) pw.print(" dur="); pw.print(now-si.deliveredTime); |
| 121 | if (si.deliveryCount != 0) { |
| 122 | pw.print(" dc="); pw.print(si.deliveryCount); |
| 123 | } |
| 124 | if (si.doneExecutingCount != 0) { |
| 125 | pw.print(" dxc="); pw.print(si.doneExecutingCount); |
| 126 | } |
| 127 | pw.print(" "); |
| 128 | if (si.intent != null) pw.println(si.intent.toString()); |
| 129 | else pw.println("null"); |
| 130 | } |
| 131 | } |
| 132 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | void dump(PrintWriter pw, String prefix) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 134 | pw.print(prefix); pw.print("intent={"); |
| 135 | pw.print(intent.getIntent().toShortString(true, false)); |
| 136 | pw.println('}'); |
| 137 | pw.print(prefix); pw.print("packageName="); pw.println(packageName); |
| 138 | pw.print(prefix); pw.print("processName="); pw.println(processName); |
| 139 | if (permission != null) { |
| 140 | pw.print(prefix); pw.print("permission="); pw.println(permission); |
| 141 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 142 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 143 | pw.print(prefix); pw.print("baseDir="); pw.print(baseDir); |
| 144 | if (!resDir.equals(baseDir)) pw.print(" resDir="); pw.print(resDir); |
| 145 | pw.print(" dataDir="); pw.println(dataDir); |
| 146 | pw.print(prefix); pw.print("app="); pw.println(app); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 147 | if (isForeground || foregroundId != 0) { |
| 148 | pw.print(prefix); pw.print("isForeground="); pw.print(isForeground); |
| 149 | pw.print(" foregroundId="); pw.print(foregroundId); |
| 150 | pw.print(" foregroundNoti="); pw.println(foregroundNoti); |
| 151 | } |
Dianne Hackborn | 6ccd2af | 2009-08-27 12:26:44 -0700 | [diff] [blame] | 152 | pw.print(prefix); pw.print("lastActivity="); pw.print(lastActivity-now); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 153 | pw.print(" executingStart="); pw.print(executingStart-now); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 154 | pw.print(" restartTime="); pw.println(restartTime); |
| 155 | if (startRequested || lastStartId != 0) { |
| 156 | pw.print(prefix); pw.print("startRequested="); pw.print(startRequested); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 157 | pw.print(" stopIfKilled="); pw.print(stopIfKilled); |
| 158 | pw.print(" callStart="); pw.print(callStart); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 159 | pw.print(" lastStartId="); pw.println(lastStartId); |
| 160 | } |
| 161 | if (executeNesting != 0 || crashCount != 0 || restartCount != 0 |
| 162 | || restartDelay != 0 || nextRestartTime != 0) { |
| 163 | pw.print(prefix); pw.print("executeNesting="); pw.print(executeNesting); |
| 164 | pw.print(" restartCount="); pw.print(restartCount); |
Dianne Hackborn | 6ccd2af | 2009-08-27 12:26:44 -0700 | [diff] [blame] | 165 | pw.print(" restartDelay="); pw.print(restartDelay-now); |
| 166 | pw.print(" nextRestartTime="); pw.print(nextRestartTime-now); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 167 | pw.print(" crashCount="); pw.println(crashCount); |
| 168 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 169 | if (deliveredStarts.size() > 0) { |
| 170 | pw.print(prefix); pw.println("Delivered Starts:"); |
| 171 | dumpStartList(pw, prefix, deliveredStarts, SystemClock.uptimeMillis()); |
| 172 | } |
| 173 | if (pendingStarts.size() > 0) { |
| 174 | pw.print(prefix); pw.println("Pending Starts:"); |
| 175 | dumpStartList(pw, prefix, pendingStarts, 0); |
| 176 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | if (bindings.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | Iterator<IntentBindRecord> it = bindings.values().iterator(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 179 | pw.print(prefix); pw.println("Bindings:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | while (it.hasNext()) { |
| 181 | IntentBindRecord b = it.next(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 182 | pw.print(prefix); pw.print("* IntentBindRecord{"); |
| 183 | pw.print(Integer.toHexString(System.identityHashCode(b))); |
| 184 | pw.println("}:"); |
| 185 | b.dumpInService(pw, prefix + " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | if (connections.size() > 0) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 189 | pw.print(prefix); pw.println("All Connections:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | Iterator<ConnectionRecord> it = connections.values().iterator(); |
| 191 | while (it.hasNext()) { |
| 192 | ConnectionRecord c = it.next(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 193 | pw.print(prefix); pw.print(" "); pw.println(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 198 | ServiceRecord(ActivityManagerService ams, |
| 199 | BatteryStatsImpl.Uid.Pkg.Serv servStats, ComponentName name, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | Intent.FilterComparison intent, ServiceInfo sInfo, Runnable restarter) { |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 201 | this.ams = ams; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | this.stats = servStats; |
| 203 | this.name = name; |
| 204 | shortName = name.flattenToShortString(); |
| 205 | this.intent = intent; |
| 206 | serviceInfo = sInfo; |
| 207 | appInfo = sInfo.applicationInfo; |
| 208 | packageName = sInfo.applicationInfo.packageName; |
| 209 | processName = sInfo.processName; |
| 210 | permission = sInfo.permission; |
| 211 | baseDir = sInfo.applicationInfo.sourceDir; |
| 212 | resDir = sInfo.applicationInfo.publicSourceDir; |
| 213 | dataDir = sInfo.applicationInfo.dataDir; |
| 214 | exported = sInfo.exported; |
| 215 | this.restarter = restarter; |
| 216 | createTime = lastActivity = SystemClock.uptimeMillis(); |
| 217 | } |
| 218 | |
| 219 | public AppBindRecord retrieveAppBindingLocked(Intent intent, |
| 220 | ProcessRecord app) { |
| 221 | Intent.FilterComparison filter = new Intent.FilterComparison(intent); |
| 222 | IntentBindRecord i = bindings.get(filter); |
| 223 | if (i == null) { |
| 224 | i = new IntentBindRecord(this, filter); |
| 225 | bindings.put(filter, i); |
| 226 | } |
| 227 | AppBindRecord a = i.apps.get(app); |
| 228 | if (a != null) { |
| 229 | return a; |
| 230 | } |
| 231 | a = new AppBindRecord(this, i, app); |
| 232 | i.apps.put(app, a); |
| 233 | return a; |
| 234 | } |
| 235 | |
| 236 | public void resetRestartCounter() { |
| 237 | restartCount = 0; |
| 238 | restartDelay = 0; |
| 239 | restartTime = 0; |
| 240 | } |
| 241 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 242 | public StartItem findDeliveredStart(int id, boolean remove) { |
| 243 | final int N = deliveredStarts.size(); |
| 244 | for (int i=0; i<N; i++) { |
| 245 | StartItem si = deliveredStarts.get(i); |
| 246 | if (si.id == id) { |
| 247 | if (remove) deliveredStarts.remove(i); |
| 248 | return si; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return null; |
| 253 | } |
| 254 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 255 | public void postNotification() { |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 256 | final int appUid = appInfo.uid; |
| 257 | final int appPid = app.pid; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 258 | if (foregroundId != 0 && foregroundNoti != null) { |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 259 | // Do asynchronous communication with notification manager to |
| 260 | // avoid deadlocks. |
| 261 | final String localPackageName = packageName; |
| 262 | final int localForegroundId = foregroundId; |
| 263 | final Notification localForegroundNoti = foregroundNoti; |
| 264 | ams.mHandler.post(new Runnable() { |
| 265 | public void run() { |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 266 | NotificationManagerService nm = |
| 267 | (NotificationManagerService) NotificationManager.getService(); |
| 268 | if (nm == null) { |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | try { |
| 272 | int[] outId = new int[1]; |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 273 | nm.enqueueNotificationInternal(localPackageName, appUid, appPid, |
| 274 | null, localForegroundId, localForegroundNoti, outId); |
Joe Onorato | 34fcf97 | 2010-02-18 07:45:17 -0500 | [diff] [blame] | 275 | } catch (RuntimeException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 276 | Slog.w(ActivityManagerService.TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 277 | "Error showing notification for service", e); |
| 278 | // If it gave us a garbage notification, it doesn't |
| 279 | // get to be foreground. |
| 280 | ams.setServiceForeground(name, ServiceRecord.this, |
| 281 | localForegroundId, null, true); |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 282 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 283 | } |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 284 | }); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | public void cancelNotification() { |
| 289 | if (foregroundId != 0) { |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 290 | // Do asynchronous communication with notification manager to |
| 291 | // avoid deadlocks. |
| 292 | final String localPackageName = packageName; |
| 293 | final int localForegroundId = foregroundId; |
| 294 | ams.mHandler.post(new Runnable() { |
| 295 | public void run() { |
| 296 | INotificationManager inm = NotificationManager.getService(); |
| 297 | if (inm == null) { |
| 298 | return; |
| 299 | } |
| 300 | try { |
| 301 | inm.cancelNotification(localPackageName, localForegroundId); |
Joe Onorato | 34fcf97 | 2010-02-18 07:45:17 -0500 | [diff] [blame] | 302 | } catch (RuntimeException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 303 | Slog.w(ActivityManagerService.TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 304 | "Error canceling notification for service", e); |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 305 | } catch (RemoteException e) { |
| 306 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 307 | } |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 308 | }); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | public String toString() { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 313 | if (stringName != null) { |
| 314 | return stringName; |
| 315 | } |
| 316 | StringBuilder sb = new StringBuilder(128); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 317 | sb.append("ServiceRecord{") |
| 318 | .append(Integer.toHexString(System.identityHashCode(this))) |
| 319 | .append(' ').append(shortName).append('}'); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 320 | return stringName = sb.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | } |
| 322 | } |