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