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