ServiceRecord.java revision 9210bc85545f31973c957b5179e6a82d05f473c6
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.app.ProcessStats; 20import com.android.internal.os.BatteryStatsImpl; 21import com.android.server.NotificationManagerService; 22 23import android.app.INotificationManager; 24import android.app.Notification; 25import android.app.NotificationManager; 26import android.app.PendingIntent; 27import android.content.ComponentName; 28import android.content.Context; 29import android.content.Intent; 30import android.content.pm.ApplicationInfo; 31import android.content.pm.PackageManager; 32import android.content.pm.ServiceInfo; 33import android.net.Uri; 34import android.os.Binder; 35import android.os.IBinder; 36import android.os.RemoteException; 37import android.os.SystemClock; 38import android.os.UserHandle; 39import android.provider.Settings; 40import android.util.ArrayMap; 41import android.util.Slog; 42import android.util.TimeUtils; 43 44import java.io.PrintWriter; 45import java.util.ArrayList; 46import java.util.List; 47 48/** 49 * A running application service. 50 */ 51final class ServiceRecord extends Binder { 52 // Maximum number of delivery attempts before giving up. 53 static final int MAX_DELIVERY_COUNT = 3; 54 55 // Maximum number of times it can fail during execution before giving up. 56 static final int MAX_DONE_EXECUTING_COUNT = 6; 57 58 final ActivityManagerService ams; 59 final BatteryStatsImpl.Uid.Pkg.Serv stats; 60 final ComponentName name; // service component. 61 final String shortName; // name.flattenToShortString(). 62 final Intent.FilterComparison intent; 63 // original intent used to find service. 64 final ServiceInfo serviceInfo; 65 // all information about the service. 66 final ApplicationInfo appInfo; 67 // information about service's app. 68 final int userId; // user that this service is running as 69 final String packageName; // the package implementing intent's component 70 final String processName; // process where this component wants to run 71 final String permission;// permission needed to access service 72 final String baseDir; // where activity source (resources etc) located 73 final String resDir; // where public activity source (public resources etc) located 74 final String dataDir; // where activity data should go 75 final boolean exported; // from ServiceInfo.exported 76 final Runnable restarter; // used to schedule retries of starting the service 77 final long createTime; // when this service was created 78 final ArrayMap<Intent.FilterComparison, IntentBindRecord> bindings 79 = new ArrayMap<Intent.FilterComparison, IntentBindRecord>(); 80 // All active bindings to the service. 81 final ArrayMap<IBinder, ArrayList<ConnectionRecord>> connections 82 = new ArrayMap<IBinder, ArrayList<ConnectionRecord>>(); 83 // IBinder -> ConnectionRecord of all bound clients 84 85 ProcessRecord app; // where this service is running or null. 86 ProcessRecord isolatedProc; // keep track of isolated process, if requested 87 ProcessStats.ServiceState tracker; // tracking service execution, may be null 88 boolean delayed; // are we waiting to start this service in the background? 89 boolean isForeground; // is service currently in foreground mode? 90 int foregroundId; // Notification ID of last foreground req. 91 Notification foregroundNoti; // Notification record of foreground state. 92 long lastActivity; // last time there was some activity on the service. 93 long startingBgTimeout; // time at which we scheduled this for a delayed start. 94 boolean startRequested; // someone explicitly called start? 95 boolean delayedStop; // service has been stopped but is in a delayed start? 96 boolean stopIfKilled; // last onStart() said to stop if service killed? 97 boolean callStart; // last onStart() has asked to alway be called on restart. 98 int executeNesting; // number of outstanding operations keeping foreground. 99 boolean executeFg; // should we be executing in the foreground? 100 long executingStart; // start time of last execute request. 101 boolean createdFromFg; // was this service last created due to a foreground process call? 102 int crashCount; // number of times proc has crashed with service running 103 int totalRestartCount; // number of times we have had to restart. 104 int restartCount; // number of restarts performed in a row. 105 long restartDelay; // delay until next restart attempt. 106 long restartTime; // time of last restart. 107 long nextRestartTime; // time when restartDelay will expire. 108 109 String stringName; // caching of toString 110 111 private int lastStartId; // identifier of most recent start request. 112 113 static class StartItem { 114 final ServiceRecord sr; 115 final boolean taskRemoved; 116 final int id; 117 final Intent intent; 118 final ActivityManagerService.NeededUriGrants neededGrants; 119 long deliveredTime; 120 int deliveryCount; 121 int doneExecutingCount; 122 UriPermissionOwner uriPermissions; 123 124 String stringName; // caching of toString 125 126 StartItem(ServiceRecord _sr, boolean _taskRemoved, int _id, Intent _intent, 127 ActivityManagerService.NeededUriGrants _neededGrants) { 128 sr = _sr; 129 taskRemoved = _taskRemoved; 130 id = _id; 131 intent = _intent; 132 neededGrants = _neededGrants; 133 } 134 135 UriPermissionOwner getUriPermissionsLocked() { 136 if (uriPermissions == null) { 137 uriPermissions = new UriPermissionOwner(sr.ams, this); 138 } 139 return uriPermissions; 140 } 141 142 void removeUriPermissionsLocked() { 143 if (uriPermissions != null) { 144 uriPermissions.removeUriPermissionsLocked(); 145 uriPermissions = null; 146 } 147 } 148 149 public String toString() { 150 if (stringName != null) { 151 return stringName; 152 } 153 StringBuilder sb = new StringBuilder(128); 154 sb.append("ServiceRecord{") 155 .append(Integer.toHexString(System.identityHashCode(sr))) 156 .append(' ').append(sr.shortName) 157 .append(" StartItem ") 158 .append(Integer.toHexString(System.identityHashCode(this))) 159 .append(" id=").append(id).append('}'); 160 return stringName = sb.toString(); 161 } 162 } 163 164 final ArrayList<StartItem> deliveredStarts = new ArrayList<StartItem>(); 165 // start() arguments which been delivered. 166 final ArrayList<StartItem> pendingStarts = new ArrayList<StartItem>(); 167 // start() arguments that haven't yet been delivered. 168 169 void dumpStartList(PrintWriter pw, String prefix, List<StartItem> list, long now) { 170 final int N = list.size(); 171 for (int i=0; i<N; i++) { 172 StartItem si = list.get(i); 173 pw.print(prefix); pw.print("#"); pw.print(i); 174 pw.print(" id="); pw.print(si.id); 175 if (now != 0) { 176 pw.print(" dur="); 177 TimeUtils.formatDuration(si.deliveredTime, now, pw); 178 } 179 if (si.deliveryCount != 0) { 180 pw.print(" dc="); pw.print(si.deliveryCount); 181 } 182 if (si.doneExecutingCount != 0) { 183 pw.print(" dxc="); pw.print(si.doneExecutingCount); 184 } 185 pw.println(""); 186 pw.print(prefix); pw.print(" intent="); 187 if (si.intent != null) pw.println(si.intent.toString()); 188 else pw.println("null"); 189 if (si.neededGrants != null) { 190 pw.print(prefix); pw.print(" neededGrants="); 191 pw.println(si.neededGrants); 192 } 193 if (si.uriPermissions != null) { 194 if (si.uriPermissions.readUriPermissions != null) { 195 pw.print(prefix); pw.print(" readUriPermissions="); 196 pw.println(si.uriPermissions.readUriPermissions); 197 } 198 if (si.uriPermissions.writeUriPermissions != null) { 199 pw.print(prefix); pw.print(" writeUriPermissions="); 200 pw.println(si.uriPermissions.writeUriPermissions); 201 } 202 } 203 } 204 } 205 206 void dump(PrintWriter pw, String prefix) { 207 pw.print(prefix); pw.print("intent={"); 208 pw.print(intent.getIntent().toShortString(false, true, false, true)); 209 pw.println('}'); 210 pw.print(prefix); pw.print("packageName="); pw.println(packageName); 211 pw.print(prefix); pw.print("processName="); pw.println(processName); 212 if (permission != null) { 213 pw.print(prefix); pw.print("permission="); pw.println(permission); 214 } 215 long now = SystemClock.uptimeMillis(); 216 long nowReal = SystemClock.elapsedRealtime(); 217 pw.print(prefix); pw.print("baseDir="); pw.println(baseDir); 218 if (!resDir.equals(baseDir)) { 219 pw.print(prefix); pw.print("resDir="); pw.println(resDir); 220 } 221 pw.print(prefix); pw.print("dataDir="); pw.println(dataDir); 222 pw.print(prefix); pw.print("app="); pw.println(app); 223 if (isolatedProc != null) { 224 pw.print(prefix); pw.print("isolatedProc="); pw.println(isolatedProc); 225 } 226 if (delayed) { 227 pw.print(prefix); pw.print("delayed="); pw.println(delayed); 228 } 229 if (isForeground || foregroundId != 0) { 230 pw.print(prefix); pw.print("isForeground="); pw.print(isForeground); 231 pw.print(" foregroundId="); pw.print(foregroundId); 232 pw.print(" foregroundNoti="); pw.println(foregroundNoti); 233 } 234 pw.print(prefix); pw.print("createTime="); 235 TimeUtils.formatDuration(createTime, nowReal, pw); 236 pw.print(" startingBgTimeout="); 237 TimeUtils.formatDuration(startingBgTimeout, now, pw); 238 pw.println(); 239 pw.print(prefix); pw.print("lastActivity="); 240 TimeUtils.formatDuration(lastActivity, now, pw); 241 pw.print(" restartTime="); 242 TimeUtils.formatDuration(restartTime, now, pw); 243 pw.print(" createdFromFg="); pw.println(createdFromFg); 244 if (startRequested || delayedStop || lastStartId != 0) { 245 pw.print(prefix); pw.print("startRequested="); pw.print(startRequested); 246 pw.print(" delayedStop="); pw.print(delayedStop); 247 pw.print(" stopIfKilled="); pw.print(stopIfKilled); 248 pw.print(" callStart="); pw.print(callStart); 249 pw.print(" lastStartId="); pw.println(lastStartId); 250 } 251 if (executeNesting != 0) { 252 pw.print(prefix); pw.print("executeNesting="); pw.print(executeNesting); 253 pw.print(" executeFg="); pw.print(executeFg); 254 pw.print(" executingStart="); 255 TimeUtils.formatDuration(executingStart, now, pw); 256 } 257 if (crashCount != 0 || restartCount != 0 258 || restartDelay != 0 || nextRestartTime != 0) { 259 pw.print(prefix); pw.print("restartCount="); pw.print(restartCount); 260 pw.print(" restartDelay="); 261 TimeUtils.formatDuration(restartDelay, now, pw); 262 pw.print(" nextRestartTime="); 263 TimeUtils.formatDuration(nextRestartTime, now, pw); 264 pw.print(" crashCount="); pw.println(crashCount); 265 } 266 if (deliveredStarts.size() > 0) { 267 pw.print(prefix); pw.println("Delivered Starts:"); 268 dumpStartList(pw, prefix, deliveredStarts, now); 269 } 270 if (pendingStarts.size() > 0) { 271 pw.print(prefix); pw.println("Pending Starts:"); 272 dumpStartList(pw, prefix, pendingStarts, 0); 273 } 274 if (bindings.size() > 0) { 275 pw.print(prefix); pw.println("Bindings:"); 276 for (int i=0; i<bindings.size(); i++) { 277 IntentBindRecord b = bindings.valueAt(i); 278 pw.print(prefix); pw.print("* IntentBindRecord{"); 279 pw.print(Integer.toHexString(System.identityHashCode(b))); 280 if ((b.collectFlags()&Context.BIND_AUTO_CREATE) != 0) { 281 pw.append(" CREATE"); 282 } 283 pw.println("}:"); 284 b.dumpInService(pw, prefix + " "); 285 } 286 } 287 if (connections.size() > 0) { 288 pw.print(prefix); pw.println("All Connections:"); 289 for (int conni=0; conni<connections.size(); conni++) { 290 ArrayList<ConnectionRecord> c = connections.valueAt(conni); 291 for (int i=0; i<c.size(); i++) { 292 pw.print(prefix); pw.print(" "); pw.println(c.get(i)); 293 } 294 } 295 } 296 } 297 298 ServiceRecord(ActivityManagerService ams, 299 BatteryStatsImpl.Uid.Pkg.Serv servStats, ComponentName name, 300 Intent.FilterComparison intent, ServiceInfo sInfo, boolean callerIsFg, 301 Runnable restarter) { 302 this.ams = ams; 303 this.stats = servStats; 304 this.name = name; 305 shortName = name.flattenToShortString(); 306 this.intent = intent; 307 serviceInfo = sInfo; 308 appInfo = sInfo.applicationInfo; 309 packageName = sInfo.applicationInfo.packageName; 310 processName = sInfo.processName; 311 permission = sInfo.permission; 312 baseDir = sInfo.applicationInfo.sourceDir; 313 resDir = sInfo.applicationInfo.publicSourceDir; 314 dataDir = sInfo.applicationInfo.dataDir; 315 exported = sInfo.exported; 316 this.restarter = restarter; 317 createTime = SystemClock.elapsedRealtime(); 318 lastActivity = SystemClock.uptimeMillis(); 319 userId = UserHandle.getUserId(appInfo.uid); 320 createdFromFg = callerIsFg; 321 } 322 323 public ProcessStats.ServiceState getTracker() { 324 if (tracker != null) { 325 return tracker; 326 } 327 if ((serviceInfo.applicationInfo.flags&ApplicationInfo.FLAG_PERSISTENT) == 0) { 328 tracker = ams.mProcessStats.getServiceStateLocked(serviceInfo.packageName, 329 serviceInfo.applicationInfo.uid, serviceInfo.processName, serviceInfo.name); 330 tracker.makeActive(); 331 } 332 return tracker; 333 } 334 335 public AppBindRecord retrieveAppBindingLocked(Intent intent, 336 ProcessRecord app) { 337 Intent.FilterComparison filter = new Intent.FilterComparison(intent); 338 IntentBindRecord i = bindings.get(filter); 339 if (i == null) { 340 i = new IntentBindRecord(this, filter); 341 bindings.put(filter, i); 342 } 343 AppBindRecord a = i.apps.get(app); 344 if (a != null) { 345 return a; 346 } 347 a = new AppBindRecord(this, i, app); 348 i.apps.put(app, a); 349 return a; 350 } 351 352 public boolean hasAutoCreateConnections() { 353 // XXX should probably keep a count of the number of auto-create 354 // connections directly in the service. 355 for (int conni=connections.size()-1; conni>=0; conni--) { 356 ArrayList<ConnectionRecord> cr = connections.valueAt(conni); 357 for (int i=0; i<cr.size(); i++) { 358 if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) { 359 return true; 360 } 361 } 362 } 363 return false; 364 } 365 366 public void resetRestartCounter() { 367 restartCount = 0; 368 restartDelay = 0; 369 restartTime = 0; 370 } 371 372 public StartItem findDeliveredStart(int id, boolean remove) { 373 final int N = deliveredStarts.size(); 374 for (int i=0; i<N; i++) { 375 StartItem si = deliveredStarts.get(i); 376 if (si.id == id) { 377 if (remove) deliveredStarts.remove(i); 378 return si; 379 } 380 } 381 382 return null; 383 } 384 385 public int getLastStartId() { 386 return lastStartId; 387 } 388 389 public int makeNextStartId() { 390 lastStartId++; 391 if (lastStartId < 1) { 392 lastStartId = 1; 393 } 394 return lastStartId; 395 } 396 397 public void postNotification() { 398 final int appUid = appInfo.uid; 399 final int appPid = app.pid; 400 if (foregroundId != 0 && foregroundNoti != null) { 401 // Do asynchronous communication with notification manager to 402 // avoid deadlocks. 403 final String localPackageName = packageName; 404 final int localForegroundId = foregroundId; 405 final Notification localForegroundNoti = foregroundNoti; 406 ams.mHandler.post(new Runnable() { 407 public void run() { 408 NotificationManagerService nm = 409 (NotificationManagerService) NotificationManager.getService(); 410 if (nm == null) { 411 return; 412 } 413 try { 414 if (localForegroundNoti.icon == 0) { 415 // It is not correct for the caller to supply a notification 416 // icon, but this used to be able to slip through, so for 417 // those dirty apps give it the app's icon. 418 localForegroundNoti.icon = appInfo.icon; 419 420 // Do not allow apps to present a sneaky invisible content view either. 421 localForegroundNoti.contentView = null; 422 localForegroundNoti.bigContentView = null; 423 CharSequence appName = appInfo.loadLabel( 424 ams.mContext.getPackageManager()); 425 if (appName == null) { 426 appName = appInfo.packageName; 427 } 428 Context ctx = null; 429 try { 430 ctx = ams.mContext.createPackageContext( 431 appInfo.packageName, 0); 432 Intent runningIntent = new Intent( 433 Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 434 runningIntent.setData(Uri.fromParts("package", 435 appInfo.packageName, null)); 436 PendingIntent pi = PendingIntent.getActivity(ams.mContext, 0, 437 runningIntent, PendingIntent.FLAG_UPDATE_CURRENT); 438 localForegroundNoti.setLatestEventInfo(ctx, 439 ams.mContext.getString( 440 com.android.internal.R.string 441 .app_running_notification_title, 442 appName), 443 ams.mContext.getString( 444 com.android.internal.R.string 445 .app_running_notification_text, 446 appName), 447 pi); 448 } catch (PackageManager.NameNotFoundException e) { 449 localForegroundNoti.icon = 0; 450 } 451 } 452 if (localForegroundNoti.icon == 0) { 453 // Notifications whose icon is 0 are defined to not show 454 // a notification, silently ignoring it. We don't want to 455 // just ignore it, we want to prevent the service from 456 // being foreground. 457 throw new RuntimeException("icon must be non-zero"); 458 } 459 int[] outId = new int[1]; 460 nm.enqueueNotificationInternal(localPackageName, localPackageName, 461 appUid, appPid, null, localForegroundId, localForegroundNoti, 462 outId, userId); 463 } catch (RuntimeException e) { 464 Slog.w(ActivityManagerService.TAG, 465 "Error showing notification for service", e); 466 // If it gave us a garbage notification, it doesn't 467 // get to be foreground. 468 ams.setServiceForeground(name, ServiceRecord.this, 469 0, null, true); 470 ams.crashApplication(appUid, appPid, localPackageName, 471 "Bad notification for startForeground: " + e); 472 } 473 } 474 }); 475 } 476 } 477 478 public void cancelNotification() { 479 if (foregroundId != 0) { 480 // Do asynchronous communication with notification manager to 481 // avoid deadlocks. 482 final String localPackageName = packageName; 483 final int localForegroundId = foregroundId; 484 ams.mHandler.post(new Runnable() { 485 public void run() { 486 INotificationManager inm = NotificationManager.getService(); 487 if (inm == null) { 488 return; 489 } 490 try { 491 inm.cancelNotificationWithTag(localPackageName, null, 492 localForegroundId, userId); 493 } catch (RuntimeException e) { 494 Slog.w(ActivityManagerService.TAG, 495 "Error canceling notification for service", e); 496 } catch (RemoteException e) { 497 } 498 } 499 }); 500 } 501 } 502 503 public void clearDeliveredStartsLocked() { 504 for (int i=deliveredStarts.size()-1; i>=0; i--) { 505 deliveredStarts.get(i).removeUriPermissionsLocked(); 506 } 507 deliveredStarts.clear(); 508 } 509 510 public String toString() { 511 if (stringName != null) { 512 return stringName; 513 } 514 StringBuilder sb = new StringBuilder(128); 515 sb.append("ServiceRecord{") 516 .append(Integer.toHexString(System.identityHashCode(this))) 517 .append(" u").append(userId) 518 .append(' ').append(shortName).append('}'); 519 return stringName = sb.toString(); 520 } 521} 522