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