The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2008 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 | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import com.android.internal.os.BatteryStatsImpl; |
Dianne Hackborn | 45ce864 | 2011-07-14 16:10:16 -0700 | [diff] [blame] | 21 | import com.android.internal.os.ProcessStats; |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 22 | import com.android.server.AttributeCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import com.android.server.IntentResolver; |
| 24 | import com.android.server.ProcessMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import com.android.server.SystemServer; |
| 26 | import com.android.server.Watchdog; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 27 | import com.android.server.am.ActivityStack.ActivityState; |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 28 | import com.android.server.wm.WindowManagerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 30 | import dalvik.system.Zygote; |
| 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.app.Activity; |
| 33 | import android.app.ActivityManager; |
| 34 | import android.app.ActivityManagerNative; |
| 35 | import android.app.ActivityThread; |
| 36 | import android.app.AlertDialog; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 37 | import android.app.AppGlobals; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 38 | import android.app.ApplicationErrorReport; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.app.Dialog; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 40 | import android.app.IActivityController; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.app.IActivityWatcher; |
| 42 | import android.app.IApplicationThread; |
| 43 | import android.app.IInstrumentationWatcher; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 44 | import android.app.INotificationManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 45 | import android.app.IProcessObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.app.IServiceConnection; |
| 47 | import android.app.IThumbnailReceiver; |
| 48 | import android.app.Instrumentation; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 49 | import android.app.Notification; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 50 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.app.PendingIntent; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 52 | import android.app.Service; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 53 | import android.app.backup.IBackupManager; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 54 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 55 | import android.content.BroadcastReceiver; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 56 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.content.ComponentName; |
| 58 | import android.content.ContentResolver; |
| 59 | import android.content.Context; |
Christian Mehlmauer | 7664e20 | 2010-07-20 08:46:17 +0200 | [diff] [blame] | 60 | import android.content.DialogInterface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.content.Intent; |
| 62 | import android.content.IntentFilter; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 63 | import android.content.IIntentReceiver; |
| 64 | import android.content.IIntentSender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 65 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.content.pm.ActivityInfo; |
| 67 | import android.content.pm.ApplicationInfo; |
| 68 | import android.content.pm.ConfigurationInfo; |
| 69 | import android.content.pm.IPackageDataObserver; |
| 70 | import android.content.pm.IPackageManager; |
| 71 | import android.content.pm.InstrumentationInfo; |
Dan Egnor | 66c40e7 | 2010-01-26 16:23:11 -0800 | [diff] [blame] | 72 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.content.pm.PackageManager; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 74 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import android.content.pm.ProviderInfo; |
| 76 | import android.content.pm.ResolveInfo; |
| 77 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 78 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 79 | import android.content.res.CompatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.content.res.Configuration; |
| 81 | import android.graphics.Bitmap; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 82 | import android.net.Proxy; |
| 83 | import android.net.ProxyProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.net.Uri; |
| 85 | import android.os.Binder; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 86 | import android.os.Build; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 87 | import android.os.Bundle; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 88 | import android.os.Debug; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 89 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | import android.os.Environment; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 91 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.os.FileUtils; |
| 93 | import android.os.Handler; |
| 94 | import android.os.IBinder; |
| 95 | import android.os.IPermissionController; |
| 96 | import android.os.Looper; |
| 97 | import android.os.Message; |
| 98 | import android.os.Parcel; |
| 99 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import android.os.Process; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 101 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | import android.os.RemoteException; |
| 103 | import android.os.ServiceManager; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 104 | import android.os.StrictMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | import android.os.SystemClock; |
| 106 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.util.EventLog; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 109 | import android.util.Pair; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 110 | import android.util.Slog; |
Joe Onorato | 5d3bea6 | 2010-03-01 13:44:29 -0800 | [diff] [blame] | 111 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | import android.util.PrintWriterPrinter; |
| 113 | import android.util.SparseArray; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 114 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import android.view.Gravity; |
| 116 | import android.view.LayoutInflater; |
| 117 | import android.view.View; |
| 118 | import android.view.WindowManager; |
| 119 | import android.view.WindowManagerPolicy; |
| 120 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 121 | import java.io.BufferedInputStream; |
| 122 | import java.io.BufferedOutputStream; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 123 | import java.io.BufferedReader; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 124 | import java.io.DataInputStream; |
| 125 | import java.io.DataOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | import java.io.File; |
| 127 | import java.io.FileDescriptor; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 128 | import java.io.FileInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | import java.io.FileNotFoundException; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 130 | import java.io.FileOutputStream; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 131 | import java.io.IOException; |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 132 | import java.io.InputStreamReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | import java.io.PrintWriter; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 134 | import java.io.StringWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | import java.lang.IllegalStateException; |
| 136 | import java.lang.ref.WeakReference; |
| 137 | import java.util.ArrayList; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 138 | import java.util.Collections; |
| 139 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | import java.util.HashMap; |
| 141 | import java.util.HashSet; |
| 142 | import java.util.Iterator; |
| 143 | import java.util.List; |
| 144 | import java.util.Locale; |
| 145 | import java.util.Map; |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 146 | import java.util.Set; |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 147 | import java.util.concurrent.atomic.AtomicBoolean; |
| 148 | import java.util.concurrent.atomic.AtomicLong; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 150 | public final class ActivityManagerService extends ActivityManagerNative |
| 151 | implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | static final String TAG = "ActivityManager"; |
| 153 | static final boolean DEBUG = false; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 154 | static final boolean localLOGV = DEBUG; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | static final boolean DEBUG_SWITCH = localLOGV || false; |
| 156 | static final boolean DEBUG_TASKS = localLOGV || false; |
| 157 | static final boolean DEBUG_PAUSE = localLOGV || false; |
| 158 | static final boolean DEBUG_OOM_ADJ = localLOGV || false; |
| 159 | static final boolean DEBUG_TRANSITION = localLOGV || false; |
| 160 | static final boolean DEBUG_BROADCAST = localLOGV || false; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 161 | static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | static final boolean DEBUG_SERVICE = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 163 | static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | static final boolean DEBUG_VISBILITY = localLOGV || false; |
| 165 | static final boolean DEBUG_PROCESSES = localLOGV || false; |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 166 | static final boolean DEBUG_PROVIDER = localLOGV || false; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 167 | static final boolean DEBUG_URI_PERMISSION = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | static final boolean DEBUG_USER_LEAVING = localLOGV || false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 169 | static final boolean DEBUG_RESULTS = localLOGV || false; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 170 | static final boolean DEBUG_BACKUP = localLOGV || false; |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 171 | static final boolean DEBUG_CONFIGURATION = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 172 | static final boolean DEBUG_POWER = localLOGV || false; |
| 173 | static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | static final boolean VALIDATE_TOKENS = false; |
| 175 | static final boolean SHOW_ACTIVITY_START_TIME = true; |
| 176 | |
| 177 | // Control over CPU and battery monitoring. |
| 178 | static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes. |
| 179 | static final boolean MONITOR_CPU_USAGE = true; |
| 180 | static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds. |
| 181 | static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample. |
| 182 | static final boolean MONITOR_THREAD_CPU_USAGE = false; |
| 183 | |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 184 | // The flags that are set for all calls we make to the package manager. |
Dianne Hackborn | 11b822d | 2009-07-21 20:03:02 -0700 | [diff] [blame] | 185 | static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 186 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 187 | private static final String SYSTEM_DEBUGGABLE = "ro.debuggable"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | // Maximum number of recent tasks that we can remember. |
| 190 | static final int MAX_RECENT_TASKS = 20; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 191 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 192 | // Amount of time after a call to stopAppSwitches() during which we will |
| 193 | // prevent further untrusted switches from happening. |
| 194 | static final long APP_SWITCH_DELAY_TIME = 5*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | |
| 196 | // How long we wait for a launched process to attach to the activity manager |
| 197 | // before we decide it's never going to come up for real. |
| 198 | static final int PROC_START_TIMEOUT = 10*1000; |
| 199 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 200 | // How long we wait for a launched process to attach to the activity manager |
| 201 | // before we decide it's never going to come up for real, when the process was |
| 202 | // started with a wrapper for instrumentation (such as Valgrind) because it |
| 203 | // could take much longer than usual. |
| 204 | static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000; |
| 205 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | // How long to wait after going idle before forcing apps to GC. |
| 207 | static final int GC_TIMEOUT = 5*1000; |
| 208 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 209 | // The minimum amount of time between successive GC requests for a process. |
| 210 | static final int GC_MIN_INTERVAL = 60*1000; |
| 211 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 212 | // The rate at which we check for apps using excessive power -- 15 mins. |
| 213 | static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000; |
| 214 | |
| 215 | // The minimum sample duration we will allow before deciding we have |
| 216 | // enough data on wake locks to start killing things. |
| 217 | static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
| 218 | |
| 219 | // The minimum sample duration we will allow before deciding we have |
| 220 | // enough data on CPU usage to start killing things. |
| 221 | static final int CPU_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | // How long we allow a receiver to run before giving up on it. |
| 224 | static final int BROADCAST_TIMEOUT = 10*1000; |
| 225 | |
| 226 | // How long we wait for a service to finish executing. |
| 227 | static final int SERVICE_TIMEOUT = 20*1000; |
| 228 | |
| 229 | // How long a service needs to be running until restarting its process |
| 230 | // is no longer considered to be a relaunch of the service. |
| 231 | static final int SERVICE_RESTART_DURATION = 5*1000; |
| 232 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 233 | // How long a service needs to be running until it will start back at |
| 234 | // SERVICE_RESTART_DURATION after being killed. |
| 235 | static final int SERVICE_RESET_RUN_DURATION = 60*1000; |
| 236 | |
| 237 | // Multiplying factor to increase restart duration time by, for each time |
| 238 | // a service is killed before it has run for SERVICE_RESET_RUN_DURATION. |
| 239 | static final int SERVICE_RESTART_DURATION_FACTOR = 4; |
| 240 | |
| 241 | // The minimum amount of time between restarting services that we allow. |
| 242 | // That is, when multiple services are restarting, we won't allow each |
| 243 | // to restart less than this amount of time from the last one. |
| 244 | static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000; |
| 245 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | // Maximum amount of time for there to be no activity on a service before |
| 247 | // we consider it non-essential and allow its process to go on the |
| 248 | // LRU background list. |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 249 | static final int MAX_SERVICE_INACTIVITY = 30*60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | |
| 251 | // How long we wait until we timeout on key dispatching. |
| 252 | static final int KEY_DISPATCHING_TIMEOUT = 5*1000; |
| 253 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | // How long we wait until we timeout on key dispatching during instrumentation. |
| 255 | static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000; |
| 256 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 257 | static final int MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | |
| 259 | static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 260 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 261 | public ActivityStack mMainStack; |
| 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 264 | * Description of a request to start a new activity, which has been held |
| 265 | * due to app switches being disabled. |
| 266 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 267 | static class PendingActivityLaunch { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 268 | ActivityRecord r; |
| 269 | ActivityRecord sourceRecord; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 270 | Uri[] grantedUriPermissions; |
| 271 | int grantedMode; |
| 272 | boolean onlyIfNeeded; |
| 273 | } |
| 274 | |
| 275 | final ArrayList<PendingActivityLaunch> mPendingActivityLaunches |
| 276 | = new ArrayList<PendingActivityLaunch>(); |
| 277 | |
| 278 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | * List of all active broadcasts that are to be executed immediately |
| 280 | * (without waiting for another broadcast to finish). Currently this only |
| 281 | * contains broadcasts to registered receivers, to avoid spinning up |
| 282 | * a bunch of processes to execute IntentReceiver components. |
| 283 | */ |
| 284 | final ArrayList<BroadcastRecord> mParallelBroadcasts |
| 285 | = new ArrayList<BroadcastRecord>(); |
| 286 | |
| 287 | /** |
| 288 | * List of all active broadcasts that are to be executed one at a time. |
| 289 | * The object at the top of the list is the currently activity broadcasts; |
| 290 | * those after it are waiting for the top to finish.. |
| 291 | */ |
| 292 | final ArrayList<BroadcastRecord> mOrderedBroadcasts |
| 293 | = new ArrayList<BroadcastRecord>(); |
| 294 | |
| 295 | /** |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 296 | * Historical data of past broadcasts, for debugging. |
| 297 | */ |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 298 | static final int MAX_BROADCAST_HISTORY = 25; |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 299 | final BroadcastRecord[] mBroadcastHistory |
| 300 | = new BroadcastRecord[MAX_BROADCAST_HISTORY]; |
| 301 | |
| 302 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | * Set when we current have a BROADCAST_INTENT_MSG in flight. |
| 304 | */ |
| 305 | boolean mBroadcastsScheduled = false; |
| 306 | |
| 307 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | * Activity we have told the window manager to have key focus. |
| 309 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 310 | ActivityRecord mFocusedActivity = null; |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 311 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | * List of intents that were used to start the most recent tasks. |
| 313 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 314 | final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | |
| 316 | /** |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 317 | * Process management. |
| 318 | */ |
| 319 | final ProcessList mProcessList = new ProcessList(); |
| 320 | |
| 321 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | * All of the applications we currently have running organized by name. |
| 323 | * The keys are strings of the application package name (as |
| 324 | * returned by the package manager), and the keys are ApplicationRecord |
| 325 | * objects. |
| 326 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 327 | final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | |
| 329 | /** |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 330 | * The currently running heavy-weight process, if any. |
| 331 | */ |
| 332 | ProcessRecord mHeavyWeightProcess = null; |
| 333 | |
| 334 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | * The last time that various processes have crashed. |
| 336 | */ |
| 337 | final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>(); |
| 338 | |
| 339 | /** |
| 340 | * Set of applications that we consider to be bad, and will reject |
| 341 | * incoming broadcasts from (which the user has no control over). |
| 342 | * Processes are added to this set when they have crashed twice within |
| 343 | * a minimum amount of time; they are removed from it when they are |
| 344 | * later restarted (hopefully due to some user action). The value is the |
| 345 | * time it was added to the list. |
| 346 | */ |
| 347 | final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>(); |
| 348 | |
| 349 | /** |
| 350 | * All of the processes we currently have running organized by pid. |
| 351 | * The keys are the pid running the application. |
| 352 | * |
| 353 | * <p>NOTE: This object is protected by its own lock, NOT the global |
| 354 | * activity manager lock! |
| 355 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 356 | final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | |
| 358 | /** |
| 359 | * All of the processes that have been forced to be foreground. The key |
| 360 | * is the pid of the caller who requested it (we hold a death |
| 361 | * link on it). |
| 362 | */ |
| 363 | abstract class ForegroundToken implements IBinder.DeathRecipient { |
| 364 | int pid; |
| 365 | IBinder token; |
| 366 | } |
| 367 | final SparseArray<ForegroundToken> mForegroundProcesses |
| 368 | = new SparseArray<ForegroundToken>(); |
| 369 | |
| 370 | /** |
| 371 | * List of records for processes that someone had tried to start before the |
| 372 | * system was ready. We don't start them at that point, but ensure they |
| 373 | * are started by the time booting is complete. |
| 374 | */ |
| 375 | final ArrayList<ProcessRecord> mProcessesOnHold |
| 376 | = new ArrayList<ProcessRecord>(); |
| 377 | |
| 378 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | * List of persistent applications that are in the process |
| 380 | * of being started. |
| 381 | */ |
| 382 | final ArrayList<ProcessRecord> mPersistentStartingProcesses |
| 383 | = new ArrayList<ProcessRecord>(); |
| 384 | |
| 385 | /** |
| 386 | * Processes that are being forcibly torn down. |
| 387 | */ |
| 388 | final ArrayList<ProcessRecord> mRemovedProcesses |
| 389 | = new ArrayList<ProcessRecord>(); |
| 390 | |
| 391 | /** |
| 392 | * List of running applications, sorted by recent usage. |
| 393 | * The first entry in the list is the least recently used. |
| 394 | * It contains ApplicationRecord objects. This list does NOT include |
| 395 | * any persistent application records (since we never want to exit them). |
| 396 | */ |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 397 | final ArrayList<ProcessRecord> mLruProcesses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | = new ArrayList<ProcessRecord>(); |
| 399 | |
| 400 | /** |
| 401 | * List of processes that should gc as soon as things are idle. |
| 402 | */ |
| 403 | final ArrayList<ProcessRecord> mProcessesToGc |
| 404 | = new ArrayList<ProcessRecord>(); |
| 405 | |
| 406 | /** |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 407 | * This is the process holding what we currently consider to be |
| 408 | * the "home" activity. |
| 409 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 410 | ProcessRecord mHomeProcess; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 411 | |
| 412 | /** |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 413 | * This is the process holding the activity the user last visited that |
| 414 | * is in a different process from the one they are currently in. |
| 415 | */ |
| 416 | ProcessRecord mPreviousProcess; |
| 417 | |
| 418 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 419 | * Packages that the user has asked to have run in screen size |
| 420 | * compatibility mode instead of filling the screen. |
| 421 | */ |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 422 | final CompatModePackages mCompatModePackages; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 423 | |
| 424 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | * Set of PendingResultRecord objects that are currently active. |
| 426 | */ |
| 427 | final HashSet mPendingResultRecords = new HashSet(); |
| 428 | |
| 429 | /** |
| 430 | * Set of IntentSenderRecord objects that are currently active. |
| 431 | */ |
| 432 | final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords |
| 433 | = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>(); |
| 434 | |
| 435 | /** |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 436 | * Fingerprints (hashCode()) of stack traces that we've |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 437 | * already logged DropBox entries for. Guarded by itself. If |
| 438 | * something (rogue user app) forces this over |
| 439 | * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared. |
| 440 | */ |
| 441 | private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>(); |
| 442 | private static final int MAX_DUP_SUPPRESSED_STACKS = 5000; |
| 443 | |
| 444 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 445 | * Strict Mode background batched logging state. |
| 446 | * |
| 447 | * The string buffer is guarded by itself, and its lock is also |
| 448 | * used to determine if another batched write is already |
| 449 | * in-flight. |
| 450 | */ |
| 451 | private final StringBuilder mStrictModeBuffer = new StringBuilder(); |
| 452 | |
| 453 | /** |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 454 | * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler. |
| 455 | */ |
| 456 | private boolean mPendingBroadcastTimeoutMessage; |
| 457 | |
| 458 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | * Intent broadcast that we have tried to start, but are |
| 460 | * waiting for its application's process to be created. We only |
| 461 | * need one (instead of a list) because we always process broadcasts |
| 462 | * one at a time, so no others can be started while waiting for this |
| 463 | * one. |
| 464 | */ |
| 465 | BroadcastRecord mPendingBroadcast = null; |
| 466 | |
| 467 | /** |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 468 | * The receiver index that is pending, to restart the broadcast if needed. |
| 469 | */ |
| 470 | int mPendingBroadcastRecvIndex; |
| 471 | |
| 472 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | * Keeps track of all IIntentReceivers that have been registered for |
| 474 | * broadcasts. Hash keys are the receiver IBinder, hash value is |
| 475 | * a ReceiverList. |
| 476 | */ |
| 477 | final HashMap mRegisteredReceivers = new HashMap(); |
| 478 | |
| 479 | /** |
| 480 | * Resolver for broadcast intents to registered receivers. |
| 481 | * Holds BroadcastFilter (subclass of IntentFilter). |
| 482 | */ |
| 483 | final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver |
| 484 | = new IntentResolver<BroadcastFilter, BroadcastFilter>() { |
| 485 | @Override |
| 486 | protected boolean allowFilterResult( |
| 487 | BroadcastFilter filter, List<BroadcastFilter> dest) { |
| 488 | IBinder target = filter.receiverList.receiver.asBinder(); |
| 489 | for (int i=dest.size()-1; i>=0; i--) { |
| 490 | if (dest.get(i).receiverList.receiver.asBinder() == target) { |
| 491 | return false; |
| 492 | } |
| 493 | } |
| 494 | return true; |
| 495 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 496 | |
| 497 | @Override |
| 498 | protected String packageForFilter(BroadcastFilter filter) { |
| 499 | return filter.packageName; |
| 500 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | }; |
| 502 | |
| 503 | /** |
| 504 | * State of all active sticky broadcasts. Keys are the action of the |
| 505 | * sticky Intent, values are an ArrayList of all broadcasted intents with |
| 506 | * that action (which should usually be one). |
| 507 | */ |
| 508 | final HashMap<String, ArrayList<Intent>> mStickyBroadcasts = |
| 509 | new HashMap<String, ArrayList<Intent>>(); |
| 510 | |
| 511 | /** |
| 512 | * All currently running services. |
| 513 | */ |
| 514 | final HashMap<ComponentName, ServiceRecord> mServices = |
| 515 | new HashMap<ComponentName, ServiceRecord>(); |
| 516 | |
| 517 | /** |
| 518 | * All currently running services indexed by the Intent used to start them. |
| 519 | */ |
| 520 | final HashMap<Intent.FilterComparison, ServiceRecord> mServicesByIntent = |
| 521 | new HashMap<Intent.FilterComparison, ServiceRecord>(); |
| 522 | |
| 523 | /** |
| 524 | * All currently bound service connections. Keys are the IBinder of |
| 525 | * the client's IServiceConnection. |
| 526 | */ |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 527 | final HashMap<IBinder, ArrayList<ConnectionRecord>> mServiceConnections |
| 528 | = new HashMap<IBinder, ArrayList<ConnectionRecord>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | |
| 530 | /** |
| 531 | * List of services that we have been asked to start, |
| 532 | * but haven't yet been able to. It is used to hold start requests |
| 533 | * while waiting for their corresponding application thread to get |
| 534 | * going. |
| 535 | */ |
| 536 | final ArrayList<ServiceRecord> mPendingServices |
| 537 | = new ArrayList<ServiceRecord>(); |
| 538 | |
| 539 | /** |
| 540 | * List of services that are scheduled to restart following a crash. |
| 541 | */ |
| 542 | final ArrayList<ServiceRecord> mRestartingServices |
| 543 | = new ArrayList<ServiceRecord>(); |
| 544 | |
| 545 | /** |
| 546 | * List of services that are in the process of being stopped. |
| 547 | */ |
| 548 | final ArrayList<ServiceRecord> mStoppingServices |
| 549 | = new ArrayList<ServiceRecord>(); |
| 550 | |
| 551 | /** |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 552 | * Backup/restore process management |
| 553 | */ |
| 554 | String mBackupAppName = null; |
| 555 | BackupRecord mBackupTarget = null; |
| 556 | |
| 557 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | * List of PendingThumbnailsRecord objects of clients who are still |
| 559 | * waiting to receive all of the thumbnails for a task. |
| 560 | */ |
| 561 | final ArrayList mPendingThumbnails = new ArrayList(); |
| 562 | |
| 563 | /** |
| 564 | * List of HistoryRecord objects that have been finished and must |
| 565 | * still report back to a pending thumbnail receiver. |
| 566 | */ |
| 567 | final ArrayList mCancelledThumbnails = new ArrayList(); |
| 568 | |
| 569 | /** |
| 570 | * All of the currently running global content providers. Keys are a |
| 571 | * string containing the provider name and values are a |
| 572 | * ContentProviderRecord object containing the data about it. Note |
| 573 | * that a single provider may be published under multiple names, so |
| 574 | * there may be multiple entries here for a single one in mProvidersByClass. |
| 575 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 576 | final HashMap<String, ContentProviderRecord> mProvidersByName |
| 577 | = new HashMap<String, ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | |
| 579 | /** |
| 580 | * All of the currently running global content providers. Keys are a |
| 581 | * string containing the provider's implementation class and values are a |
| 582 | * ContentProviderRecord object containing the data about it. |
| 583 | */ |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 584 | final HashMap<ComponentName, ContentProviderRecord> mProvidersByClass |
| 585 | = new HashMap<ComponentName, ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | |
| 587 | /** |
| 588 | * List of content providers who have clients waiting for them. The |
| 589 | * application is currently being launched and the provider will be |
| 590 | * removed from this list once it is published. |
| 591 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 592 | final ArrayList<ContentProviderRecord> mLaunchingProviders |
| 593 | = new ArrayList<ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | |
| 595 | /** |
| 596 | * Global set of specific Uri permissions that have been granted. |
| 597 | */ |
| 598 | final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions |
| 599 | = new SparseArray<HashMap<Uri, UriPermission>>(); |
| 600 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 601 | CoreSettingsObserver mCoreSettingsObserver; |
| 602 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | /** |
| 604 | * Thread-local storage used to carry caller permissions over through |
| 605 | * indirect content-provider access. |
| 606 | * @see #ActivityManagerService.openContentUri() |
| 607 | */ |
| 608 | private class Identity { |
| 609 | public int pid; |
| 610 | public int uid; |
| 611 | |
| 612 | Identity(int _pid, int _uid) { |
| 613 | pid = _pid; |
| 614 | uid = _uid; |
| 615 | } |
| 616 | } |
| 617 | private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>(); |
| 618 | |
| 619 | /** |
| 620 | * All information we have collected about the runtime performance of |
| 621 | * any user id that can impact battery performance. |
| 622 | */ |
| 623 | final BatteryStatsService mBatteryStatsService; |
| 624 | |
| 625 | /** |
| 626 | * information about component usage |
| 627 | */ |
| 628 | final UsageStatsService mUsageStatsService; |
| 629 | |
| 630 | /** |
| 631 | * Current configuration information. HistoryRecord objects are given |
| 632 | * a reference to this object to indicate which configuration they are |
| 633 | * currently running in, so this object must be kept immutable. |
| 634 | */ |
| 635 | Configuration mConfiguration = new Configuration(); |
| 636 | |
| 637 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 638 | * Current sequencing integer of the configuration, for skipping old |
| 639 | * configurations. |
| 640 | */ |
| 641 | int mConfigurationSeq = 0; |
| 642 | |
| 643 | /** |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 644 | * Hardware-reported OpenGLES version. |
| 645 | */ |
| 646 | final int GL_ES_VERSION; |
| 647 | |
| 648 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | * List of initialization arguments to pass to all processes when binding applications to them. |
| 650 | * For example, references to the commonly used services. |
| 651 | */ |
| 652 | HashMap<String, IBinder> mAppBindArgs; |
| 653 | |
| 654 | /** |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 655 | * Temporary to avoid allocations. Protected by main lock. |
| 656 | */ |
| 657 | final StringBuilder mStringBuilder = new StringBuilder(256); |
| 658 | |
| 659 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | * Used to control how we initialize the service. |
| 661 | */ |
| 662 | boolean mStartRunning = false; |
| 663 | ComponentName mTopComponent; |
| 664 | String mTopAction; |
| 665 | String mTopData; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 666 | boolean mProcessesReady = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | boolean mSystemReady = false; |
| 668 | boolean mBooting = false; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 669 | boolean mWaitingUpdate = false; |
| 670 | boolean mDidUpdate = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 671 | boolean mOnBattery = false; |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 672 | boolean mLaunchWarningShown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | |
| 674 | Context mContext; |
| 675 | |
| 676 | int mFactoryTest; |
| 677 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 678 | boolean mCheckedForSetup; |
| 679 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 681 | * The time at which we will allow normal application switches again, |
| 682 | * after a call to {@link #stopAppSwitches()}. |
| 683 | */ |
| 684 | long mAppSwitchesAllowedTime; |
| 685 | |
| 686 | /** |
| 687 | * This is set to true after the first switch after mAppSwitchesAllowedTime |
| 688 | * is set; any switches after that will clear the time. |
| 689 | */ |
| 690 | boolean mDidAppSwitch; |
| 691 | |
| 692 | /** |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 693 | * Last time (in realtime) at which we checked for power usage. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 694 | */ |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 695 | long mLastPowerCheckRealtime; |
| 696 | |
| 697 | /** |
| 698 | * Last time (in uptime) at which we checked for power usage. |
| 699 | */ |
| 700 | long mLastPowerCheckUptime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 701 | |
| 702 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | * Set while we are wanting to sleep, to prevent any |
| 704 | * activities from being started/resumed. |
| 705 | */ |
| 706 | boolean mSleeping = false; |
| 707 | |
| 708 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 709 | * Set if we are shutting down the system, similar to sleeping. |
| 710 | */ |
| 711 | boolean mShuttingDown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | |
| 713 | /** |
| 714 | * Task identifier that activities are currently being started |
| 715 | * in. Incremented each time a new task is created. |
| 716 | * todo: Replace this with a TokenSpace class that generates non-repeating |
| 717 | * integers that won't wrap. |
| 718 | */ |
| 719 | int mCurTask = 1; |
| 720 | |
| 721 | /** |
| 722 | * Current sequence id for oom_adj computation traversal. |
| 723 | */ |
| 724 | int mAdjSeq = 0; |
| 725 | |
| 726 | /** |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 727 | * Current sequence id for process LRU updating. |
| 728 | */ |
| 729 | int mLruSeq = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | |
| 731 | /** |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 732 | * Keep track of the number of service processes we last found, to |
| 733 | * determine on the next iteration which should be B services. |
| 734 | */ |
| 735 | int mNumServiceProcs = 0; |
| 736 | int mNewNumServiceProcs = 0; |
| 737 | |
| 738 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | * System monitoring: number of processes that died since the last |
| 740 | * N procs were started. |
| 741 | */ |
| 742 | int[] mProcDeaths = new int[20]; |
| 743 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 744 | /** |
| 745 | * This is set if we had to do a delayed dexopt of an app before launching |
| 746 | * it, to increasing the ANR timeouts in that case. |
| 747 | */ |
| 748 | boolean mDidDexOpt; |
| 749 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | String mDebugApp = null; |
| 751 | boolean mWaitForDebugger = false; |
| 752 | boolean mDebugTransient = false; |
| 753 | String mOrigDebugApp = null; |
| 754 | boolean mOrigWaitForDebugger = false; |
| 755 | boolean mAlwaysFinishActivities = false; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 756 | IActivityController mController = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 757 | String mProfileApp = null; |
| 758 | ProcessRecord mProfileProc = null; |
| 759 | String mProfileFile; |
| 760 | ParcelFileDescriptor mProfileFd; |
| 761 | int mProfileType = 0; |
| 762 | boolean mAutoStopProfiler = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 764 | final RemoteCallbackList<IActivityWatcher> mWatchers |
| 765 | = new RemoteCallbackList<IActivityWatcher>(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 766 | |
| 767 | final RemoteCallbackList<IProcessObserver> mProcessObservers |
| 768 | = new RemoteCallbackList<IProcessObserver>(); |
| 769 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | /** |
| 771 | * Callback of last caller to {@link #requestPss}. |
| 772 | */ |
| 773 | Runnable mRequestPssCallback; |
| 774 | |
| 775 | /** |
| 776 | * Remaining processes for which we are waiting results from the last |
| 777 | * call to {@link #requestPss}. |
| 778 | */ |
| 779 | final ArrayList<ProcessRecord> mRequestPssList |
| 780 | = new ArrayList<ProcessRecord>(); |
| 781 | |
| 782 | /** |
| 783 | * Runtime statistics collection thread. This object's lock is used to |
| 784 | * protect all related state. |
| 785 | */ |
| 786 | final Thread mProcessStatsThread; |
| 787 | |
| 788 | /** |
| 789 | * Used to collect process stats when showing not responding dialog. |
| 790 | * Protected by mProcessStatsThread. |
| 791 | */ |
| 792 | final ProcessStats mProcessStats = new ProcessStats( |
| 793 | MONITOR_THREAD_CPU_USAGE); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 794 | final AtomicLong mLastCpuTime = new AtomicLong(0); |
| 795 | final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true); |
| 796 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | long mLastWriteTime = 0; |
| 798 | |
| 799 | /** |
| 800 | * Set to true after the system has finished booting. |
| 801 | */ |
| 802 | boolean mBooted = false; |
| 803 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 804 | int mProcessLimit = ProcessList.MAX_HIDDEN_APPS; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 805 | int mProcessLimitOverride = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | |
| 807 | WindowManagerService mWindowManager; |
| 808 | |
| 809 | static ActivityManagerService mSelf; |
| 810 | static ActivityThread mSystemThread; |
| 811 | |
| 812 | private final class AppDeathRecipient implements IBinder.DeathRecipient { |
| 813 | final ProcessRecord mApp; |
| 814 | final int mPid; |
| 815 | final IApplicationThread mAppThread; |
| 816 | |
| 817 | AppDeathRecipient(ProcessRecord app, int pid, |
| 818 | IApplicationThread thread) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 819 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | TAG, "New death recipient " + this |
| 821 | + " for thread " + thread.asBinder()); |
| 822 | mApp = app; |
| 823 | mPid = pid; |
| 824 | mAppThread = thread; |
| 825 | } |
| 826 | |
| 827 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 828 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | TAG, "Death received in " + this |
| 830 | + " for thread " + mAppThread.asBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | synchronized(ActivityManagerService.this) { |
| 832 | appDiedLocked(mApp, mPid, mAppThread); |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | static final int SHOW_ERROR_MSG = 1; |
| 838 | static final int SHOW_NOT_RESPONDING_MSG = 2; |
| 839 | static final int SHOW_FACTORY_ERROR_MSG = 3; |
| 840 | static final int UPDATE_CONFIGURATION_MSG = 4; |
| 841 | static final int GC_BACKGROUND_PROCESSES_MSG = 5; |
| 842 | static final int WAIT_FOR_DEBUGGER_MSG = 6; |
| 843 | static final int BROADCAST_INTENT_MSG = 7; |
| 844 | static final int BROADCAST_TIMEOUT_MSG = 8; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 845 | static final int SERVICE_TIMEOUT_MSG = 12; |
| 846 | static final int UPDATE_TIME_ZONE = 13; |
| 847 | static final int SHOW_UID_ERROR_MSG = 14; |
| 848 | static final int IM_FEELING_LUCKY_MSG = 15; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | static final int PROC_START_TIMEOUT_MSG = 20; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 850 | static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 851 | static final int KILL_APPLICATION_MSG = 22; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 852 | static final int FINALIZE_PENDING_INTENT_MSG = 23; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 853 | static final int POST_HEAVY_NOTIFICATION_MSG = 24; |
| 854 | static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 855 | static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 856 | static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 857 | static final int CLEAR_DNS_CACHE = 28; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 858 | static final int UPDATE_HTTP_PROXY = 29; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 859 | static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30; |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 860 | static final int DISPATCH_FOREGROUND_ACTIVITIES_CHANGED = 31; |
| 861 | static final int DISPATCH_PROCESS_DIED = 32; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 862 | static final int REPORT_MEM_USAGE = 33; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | |
| 864 | AlertDialog mUidAlert; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 865 | CompatModeDialog mCompatModeDialog; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 866 | long mLastMemUsageReportTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | |
| 868 | final Handler mHandler = new Handler() { |
| 869 | //public Handler() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 870 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | //} |
| 872 | |
| 873 | public void handleMessage(Message msg) { |
| 874 | switch (msg.what) { |
| 875 | case SHOW_ERROR_MSG: { |
| 876 | HashMap data = (HashMap) msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | synchronized (ActivityManagerService.this) { |
| 878 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 879 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 880 | Slog.e(TAG, "App already has crash dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | return; |
| 882 | } |
| 883 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 884 | if (!mSleeping && !mShuttingDown) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 885 | Dialog d = new AppErrorDialog(mContext, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | d.show(); |
| 887 | proc.crashDialog = d; |
| 888 | } else { |
| 889 | // The device is asleep, so just pretend that the user |
| 890 | // saw a crash dialog and hit "force quit". |
| 891 | res.set(0); |
| 892 | } |
| 893 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 894 | |
| 895 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 896 | } break; |
| 897 | case SHOW_NOT_RESPONDING_MSG: { |
| 898 | synchronized (ActivityManagerService.this) { |
| 899 | HashMap data = (HashMap) msg.obj; |
| 900 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 901 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 902 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | return; |
| 904 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 905 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 906 | Intent intent = new Intent("android.intent.action.ANR"); |
| 907 | if (!mProcessesReady) { |
| 908 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 909 | } |
| 910 | broadcastIntentLocked(null, null, intent, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 911 | null, null, 0, null, null, null, |
| 912 | false, false, MY_PID, Process.SYSTEM_UID); |
| 913 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 915 | mContext, proc, (ActivityRecord)data.get("activity")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | d.show(); |
| 917 | proc.anrDialog = d; |
| 918 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 919 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 920 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 922 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 923 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 924 | synchronized (ActivityManagerService.this) { |
| 925 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 926 | if (proc == null) { |
| 927 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 928 | break; |
| 929 | } |
| 930 | if (proc.crashDialog != null) { |
| 931 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 932 | return; |
| 933 | } |
| 934 | AppErrorResult res = (AppErrorResult) data.get("result"); |
| 935 | if (!mSleeping && !mShuttingDown) { |
| 936 | Dialog d = new StrictModeViolationDialog(mContext, res, proc); |
| 937 | d.show(); |
| 938 | proc.crashDialog = d; |
| 939 | } else { |
| 940 | // The device is asleep, so just pretend that the user |
| 941 | // saw a crash dialog and hit "force quit". |
| 942 | res.set(0); |
| 943 | } |
| 944 | } |
| 945 | ensureBootCompleted(); |
| 946 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | case SHOW_FACTORY_ERROR_MSG: { |
| 948 | Dialog d = new FactoryErrorDialog( |
| 949 | mContext, msg.getData().getCharSequence("msg")); |
| 950 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 951 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | } break; |
| 953 | case UPDATE_CONFIGURATION_MSG: { |
| 954 | final ContentResolver resolver = mContext.getContentResolver(); |
| 955 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 956 | } break; |
| 957 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 958 | synchronized (ActivityManagerService.this) { |
| 959 | performAppGcsIfAppropriateLocked(); |
| 960 | } |
| 961 | } break; |
| 962 | case WAIT_FOR_DEBUGGER_MSG: { |
| 963 | synchronized (ActivityManagerService.this) { |
| 964 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 965 | if (msg.arg1 != 0) { |
| 966 | if (!app.waitedForDebugger) { |
| 967 | Dialog d = new AppWaitingForDebuggerDialog( |
| 968 | ActivityManagerService.this, |
| 969 | mContext, app); |
| 970 | app.waitDialog = d; |
| 971 | app.waitedForDebugger = true; |
| 972 | d.show(); |
| 973 | } |
| 974 | } else { |
| 975 | if (app.waitDialog != null) { |
| 976 | app.waitDialog.dismiss(); |
| 977 | app.waitDialog = null; |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | } break; |
| 982 | case BROADCAST_INTENT_MSG: { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 983 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 984 | TAG, "Received BROADCAST_INTENT_MSG"); |
| 985 | processNextBroadcast(true); |
| 986 | } break; |
| 987 | case BROADCAST_TIMEOUT_MSG: { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 988 | synchronized (ActivityManagerService.this) { |
| 989 | broadcastTimeoutLocked(true); |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 990 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 991 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 993 | if (mDidDexOpt) { |
| 994 | mDidDexOpt = false; |
| 995 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 996 | nmsg.obj = msg.obj; |
| 997 | mHandler.sendMessageDelayed(nmsg, SERVICE_TIMEOUT); |
| 998 | return; |
| 999 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | serviceTimeout((ProcessRecord)msg.obj); |
| 1001 | } break; |
| 1002 | case UPDATE_TIME_ZONE: { |
| 1003 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1004 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1005 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | if (r.thread != null) { |
| 1007 | try { |
| 1008 | r.thread.updateTimeZone(); |
| 1009 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1010 | Slog.w(TAG, "Failed to update time zone for: " + r.info.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1015 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1016 | case CLEAR_DNS_CACHE: { |
| 1017 | synchronized (ActivityManagerService.this) { |
| 1018 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1019 | ProcessRecord r = mLruProcesses.get(i); |
| 1020 | if (r.thread != null) { |
| 1021 | try { |
| 1022 | r.thread.clearDnsCache(); |
| 1023 | } catch (RemoteException ex) { |
| 1024 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1030 | case UPDATE_HTTP_PROXY: { |
| 1031 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 1032 | String host = ""; |
| 1033 | String port = ""; |
| 1034 | String exclList = ""; |
| 1035 | if (proxy != null) { |
| 1036 | host = proxy.getHost(); |
| 1037 | port = Integer.toString(proxy.getPort()); |
| 1038 | exclList = proxy.getExclusionList(); |
| 1039 | } |
| 1040 | synchronized (ActivityManagerService.this) { |
| 1041 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1042 | ProcessRecord r = mLruProcesses.get(i); |
| 1043 | if (r.thread != null) { |
| 1044 | try { |
| 1045 | r.thread.setHttpProxy(host, port, exclList); |
| 1046 | } catch (RemoteException ex) { |
| 1047 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1048 | r.info.processName); |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | } |
| 1053 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | case SHOW_UID_ERROR_MSG: { |
| 1055 | // XXX This is a temporary dialog, no need to localize. |
| 1056 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1057 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1058 | d.setCancelable(false); |
| 1059 | d.setTitle("System UIDs Inconsistent"); |
| 1060 | d.setMessage("UIDs on the system are inconsistent, you need to wipe your data partition or your device will be unstable."); |
Christian Mehlmauer | 7664e20 | 2010-07-20 08:46:17 +0200 | [diff] [blame] | 1061 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1062 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1063 | mUidAlert = d; |
| 1064 | d.show(); |
| 1065 | } break; |
| 1066 | case IM_FEELING_LUCKY_MSG: { |
| 1067 | if (mUidAlert != null) { |
| 1068 | mUidAlert.dismiss(); |
| 1069 | mUidAlert = null; |
| 1070 | } |
| 1071 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1073 | if (mDidDexOpt) { |
| 1074 | mDidDexOpt = false; |
| 1075 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1076 | nmsg.obj = msg.obj; |
| 1077 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1078 | return; |
| 1079 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1081 | synchronized (ActivityManagerService.this) { |
| 1082 | processStartTimedOutLocked(app); |
| 1083 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1084 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1085 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1086 | synchronized (ActivityManagerService.this) { |
| 1087 | doPendingActivityLaunchesLocked(true); |
| 1088 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1089 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1090 | case KILL_APPLICATION_MSG: { |
| 1091 | synchronized (ActivityManagerService.this) { |
| 1092 | int uid = msg.arg1; |
| 1093 | boolean restart = (msg.arg2 == 1); |
| 1094 | String pkg = (String) msg.obj; |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 1095 | forceStopPackageLocked(pkg, uid, restart, false, true, false); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1096 | } |
| 1097 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1098 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1099 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1100 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1101 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1102 | INotificationManager inm = NotificationManager.getService(); |
| 1103 | if (inm == null) { |
| 1104 | return; |
| 1105 | } |
| 1106 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1107 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1108 | ProcessRecord process = root.app; |
| 1109 | if (process == null) { |
| 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | try { |
| 1114 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1115 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1116 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1117 | Notification notification = new Notification(); |
| 1118 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1119 | notification.when = 0; |
| 1120 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1121 | notification.tickerText = text; |
| 1122 | notification.defaults = 0; // please be quiet |
| 1123 | notification.sound = null; |
| 1124 | notification.vibrate = null; |
| 1125 | notification.setLatestEventInfo(context, text, |
| 1126 | mContext.getText(R.string.heavy_weight_notification_detail), |
| 1127 | PendingIntent.getActivity(mContext, 0, root.intent, |
| 1128 | PendingIntent.FLAG_CANCEL_CURRENT)); |
| 1129 | |
| 1130 | try { |
| 1131 | int[] outId = new int[1]; |
| 1132 | inm.enqueueNotification("android", R.string.heavy_weight_notification, |
| 1133 | notification, outId); |
| 1134 | } catch (RuntimeException e) { |
| 1135 | Slog.w(ActivityManagerService.TAG, |
| 1136 | "Error showing notification for heavy-weight app", e); |
| 1137 | } catch (RemoteException e) { |
| 1138 | } |
| 1139 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1140 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1141 | } |
| 1142 | } break; |
| 1143 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1144 | INotificationManager inm = NotificationManager.getService(); |
| 1145 | if (inm == null) { |
| 1146 | return; |
| 1147 | } |
| 1148 | try { |
| 1149 | inm.cancelNotification("android", |
| 1150 | R.string.heavy_weight_notification); |
| 1151 | } catch (RuntimeException e) { |
| 1152 | Slog.w(ActivityManagerService.TAG, |
| 1153 | "Error canceling notification for service", e); |
| 1154 | } catch (RemoteException e) { |
| 1155 | } |
| 1156 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1157 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1158 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1159 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1160 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1161 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1162 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1163 | } |
| 1164 | } break; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1165 | case SHOW_COMPAT_MODE_DIALOG_MSG: { |
| 1166 | synchronized (ActivityManagerService.this) { |
| 1167 | ActivityRecord ar = (ActivityRecord)msg.obj; |
| 1168 | if (mCompatModeDialog != null) { |
| 1169 | if (mCompatModeDialog.mAppInfo.packageName.equals( |
| 1170 | ar.info.applicationInfo.packageName)) { |
| 1171 | return; |
| 1172 | } |
| 1173 | mCompatModeDialog.dismiss(); |
| 1174 | mCompatModeDialog = null; |
| 1175 | } |
Dianne Hackborn | 2947826 | 2011-06-07 15:44:22 -0700 | [diff] [blame] | 1176 | if (ar != null && false) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1177 | if (mCompatModePackages.getPackageAskCompatModeLocked( |
| 1178 | ar.packageName)) { |
| 1179 | int mode = mCompatModePackages.computeCompatModeLocked( |
| 1180 | ar.info.applicationInfo); |
| 1181 | if (mode == ActivityManager.COMPAT_MODE_DISABLED |
| 1182 | || mode == ActivityManager.COMPAT_MODE_ENABLED) { |
| 1183 | mCompatModeDialog = new CompatModeDialog( |
| 1184 | ActivityManagerService.this, mContext, |
| 1185 | ar.info.applicationInfo); |
| 1186 | mCompatModeDialog.show(); |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | } |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 1191 | break; |
| 1192 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1193 | case DISPATCH_FOREGROUND_ACTIVITIES_CHANGED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1194 | final int pid = msg.arg1; |
| 1195 | final int uid = msg.arg2; |
| 1196 | final boolean foregroundActivities = (Boolean) msg.obj; |
| 1197 | dispatchForegroundActivitiesChanged(pid, uid, foregroundActivities); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1198 | break; |
| 1199 | } |
| 1200 | case DISPATCH_PROCESS_DIED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1201 | final int pid = msg.arg1; |
| 1202 | final int uid = msg.arg2; |
| 1203 | dispatchProcessDied(pid, uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1204 | break; |
| 1205 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1206 | case REPORT_MEM_USAGE: { |
| 1207 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 1208 | if (!isDebuggable) { |
| 1209 | return; |
| 1210 | } |
| 1211 | synchronized (ActivityManagerService.this) { |
| 1212 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1213 | if (now < (mLastMemUsageReportTime+5*60*1000)) { |
| 1214 | // Don't report more than every 5 minutes to somewhat |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1215 | // avoid spamming. |
| 1216 | return; |
| 1217 | } |
| 1218 | mLastMemUsageReportTime = now; |
| 1219 | } |
| 1220 | Thread thread = new Thread() { |
| 1221 | @Override public void run() { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1222 | StringBuilder dropBuilder = new StringBuilder(1024); |
| 1223 | StringBuilder logBuilder = new StringBuilder(1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame^] | 1224 | StringWriter oomSw = new StringWriter(); |
| 1225 | PrintWriter oomPw = new PrintWriter(oomSw); |
| 1226 | StringWriter catSw = new StringWriter(); |
| 1227 | PrintWriter catPw = new PrintWriter(catSw); |
| 1228 | String[] emptyArgs = new String[] { }; |
| 1229 | StringBuilder tag = new StringBuilder(128); |
| 1230 | StringBuilder stack = new StringBuilder(128); |
| 1231 | tag.append("Low on memory -- "); |
| 1232 | dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw, |
| 1233 | tag, stack); |
| 1234 | dropBuilder.append(stack); |
| 1235 | dropBuilder.append('\n'); |
| 1236 | dropBuilder.append('\n'); |
| 1237 | String oomString = oomSw.toString(); |
| 1238 | dropBuilder.append(oomString); |
| 1239 | dropBuilder.append('\n'); |
| 1240 | logBuilder.append(oomString); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1241 | try { |
| 1242 | java.lang.Process proc = Runtime.getRuntime().exec(new String[] { |
| 1243 | "procrank", }); |
| 1244 | final InputStreamReader converter = new InputStreamReader( |
| 1245 | proc.getInputStream()); |
| 1246 | BufferedReader in = new BufferedReader(converter); |
| 1247 | String line; |
| 1248 | while (true) { |
| 1249 | line = in.readLine(); |
| 1250 | if (line == null) { |
| 1251 | break; |
| 1252 | } |
| 1253 | if (line.length() > 0) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1254 | logBuilder.append(line); |
| 1255 | logBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1256 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1257 | dropBuilder.append(line); |
| 1258 | dropBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1259 | } |
| 1260 | converter.close(); |
| 1261 | } catch (IOException e) { |
| 1262 | } |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1263 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame^] | 1264 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1265 | dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1266 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1267 | dumpServicesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1268 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1269 | dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1270 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1271 | dropBuilder.append(catSw.toString()); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1272 | addErrorToDropBox("lowmem", null, "system_server", null, |
| 1273 | null, tag.toString(), dropBuilder.toString(), null, null); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1274 | Slog.i(TAG, logBuilder.toString()); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1275 | synchronized (ActivityManagerService.this) { |
| 1276 | long now = SystemClock.uptimeMillis(); |
| 1277 | if (mLastMemUsageReportTime < now) { |
| 1278 | mLastMemUsageReportTime = now; |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | }; |
| 1283 | thread.start(); |
| 1284 | break; |
| 1285 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | }; |
| 1289 | |
| 1290 | public static void setSystemProcess() { |
| 1291 | try { |
| 1292 | ActivityManagerService m = mSelf; |
| 1293 | |
| 1294 | ServiceManager.addService("activity", m); |
| 1295 | ServiceManager.addService("meminfo", new MemBinder(m)); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1296 | ServiceManager.addService("gfxinfo", new GraphicsBinder(m)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1297 | if (MONITOR_CPU_USAGE) { |
| 1298 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1299 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1300 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1301 | |
| 1302 | ApplicationInfo info = |
| 1303 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 1304 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1305 | mSystemThread.installSystemApplicationInfo(info); |
| 1306 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1307 | synchronized (mSelf) { |
| 1308 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1309 | mSystemThread.getApplicationThread(), info, |
| 1310 | info.processName); |
| 1311 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1312 | app.pid = MY_PID; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1313 | app.maxAdj = ProcessList.SYSTEM_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | mSelf.mProcessNames.put(app.processName, app.info.uid, app); |
| 1315 | synchronized (mSelf.mPidsSelfLocked) { |
| 1316 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1317 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1318 | mSelf.updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1319 | } |
| 1320 | } catch (PackageManager.NameNotFoundException e) { |
| 1321 | throw new RuntimeException( |
| 1322 | "Unable to find android system package", e); |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | public void setWindowManager(WindowManagerService wm) { |
| 1327 | mWindowManager = wm; |
| 1328 | } |
| 1329 | |
| 1330 | public static final Context main(int factoryTest) { |
| 1331 | AThread thr = new AThread(); |
| 1332 | thr.start(); |
| 1333 | |
| 1334 | synchronized (thr) { |
| 1335 | while (thr.mService == null) { |
| 1336 | try { |
| 1337 | thr.wait(); |
| 1338 | } catch (InterruptedException e) { |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | ActivityManagerService m = thr.mService; |
| 1344 | mSelf = m; |
| 1345 | ActivityThread at = ActivityThread.systemMain(); |
| 1346 | mSystemThread = at; |
| 1347 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1348 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1349 | m.mContext = context; |
| 1350 | m.mFactoryTest = factoryTest; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1351 | m.mMainStack = new ActivityStack(m, context, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1352 | |
| 1353 | m.mBatteryStatsService.publish(context); |
| 1354 | m.mUsageStatsService.publish(context); |
| 1355 | |
| 1356 | synchronized (thr) { |
| 1357 | thr.mReady = true; |
| 1358 | thr.notifyAll(); |
| 1359 | } |
| 1360 | |
| 1361 | m.startRunning(null, null, null, null); |
| 1362 | |
| 1363 | return context; |
| 1364 | } |
| 1365 | |
| 1366 | public static ActivityManagerService self() { |
| 1367 | return mSelf; |
| 1368 | } |
| 1369 | |
| 1370 | static class AThread extends Thread { |
| 1371 | ActivityManagerService mService; |
| 1372 | boolean mReady = false; |
| 1373 | |
| 1374 | public AThread() { |
| 1375 | super("ActivityManager"); |
| 1376 | } |
| 1377 | |
| 1378 | public void run() { |
| 1379 | Looper.prepare(); |
| 1380 | |
| 1381 | android.os.Process.setThreadPriority( |
| 1382 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1383 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1384 | |
| 1385 | ActivityManagerService m = new ActivityManagerService(); |
| 1386 | |
| 1387 | synchronized (this) { |
| 1388 | mService = m; |
| 1389 | notifyAll(); |
| 1390 | } |
| 1391 | |
| 1392 | synchronized (this) { |
| 1393 | while (!mReady) { |
| 1394 | try { |
| 1395 | wait(); |
| 1396 | } catch (InterruptedException e) { |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1401 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1402 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1403 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1404 | } |
| 1405 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | Looper.loop(); |
| 1407 | } |
| 1408 | } |
| 1409 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1410 | static class MemBinder extends Binder { |
| 1411 | ActivityManagerService mActivityManagerService; |
| 1412 | MemBinder(ActivityManagerService activityManagerService) { |
| 1413 | mActivityManagerService = activityManagerService; |
| 1414 | } |
| 1415 | |
| 1416 | @Override |
| 1417 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1418 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1419 | != PackageManager.PERMISSION_GRANTED) { |
| 1420 | pw.println("Permission Denial: can't dump meminfo from from pid=" |
| 1421 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1422 | + " without permission " + android.Manifest.permission.DUMP); |
| 1423 | return; |
| 1424 | } |
| 1425 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1426 | mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame^] | 1427 | false, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1431 | static class GraphicsBinder extends Binder { |
| 1432 | ActivityManagerService mActivityManagerService; |
| 1433 | GraphicsBinder(ActivityManagerService activityManagerService) { |
| 1434 | mActivityManagerService = activityManagerService; |
| 1435 | } |
| 1436 | |
| 1437 | @Override |
| 1438 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1439 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1440 | != PackageManager.PERMISSION_GRANTED) { |
| 1441 | pw.println("Permission Denial: can't dump gfxinfo from from pid=" |
| 1442 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1443 | + " without permission " + android.Manifest.permission.DUMP); |
| 1444 | return; |
| 1445 | } |
| 1446 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1447 | mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1451 | static class CpuBinder extends Binder { |
| 1452 | ActivityManagerService mActivityManagerService; |
| 1453 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1454 | mActivityManagerService = activityManagerService; |
| 1455 | } |
| 1456 | |
| 1457 | @Override |
| 1458 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1459 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1460 | != PackageManager.PERMISSION_GRANTED) { |
| 1461 | pw.println("Permission Denial: can't dump cpuinfo from from pid=" |
| 1462 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1463 | + " without permission " + android.Manifest.permission.DUMP); |
| 1464 | return; |
| 1465 | } |
| 1466 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1467 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1468 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1469 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1470 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | private ActivityManagerService() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1476 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | File dataDir = Environment.getDataDirectory(); |
| 1479 | File systemDir = new File(dataDir, "system"); |
| 1480 | systemDir.mkdirs(); |
| 1481 | mBatteryStatsService = new BatteryStatsService(new File( |
| 1482 | systemDir, "batterystats.bin").toString()); |
| 1483 | mBatteryStatsService.getActiveStatistics().readLocked(); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1484 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1485 | mOnBattery = DEBUG_POWER ? true |
| 1486 | : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1487 | mBatteryStatsService.getActiveStatistics().setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1488 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1489 | mUsageStatsService = new UsageStatsService(new File( |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1490 | systemDir, "usagestats").toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1492 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1493 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1494 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1495 | mConfiguration.setToDefaults(); |
| 1496 | mConfiguration.locale = Locale.getDefault(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 1497 | mConfigurationSeq = mConfiguration.seq = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1498 | mProcessStats.init(); |
| 1499 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 1500 | mCompatModePackages = new CompatModePackages(this, systemDir); |
| 1501 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | // Add ourself to the Watchdog monitors. |
| 1503 | Watchdog.getInstance().addMonitor(this); |
| 1504 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1505 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1506 | public void run() { |
| 1507 | while (true) { |
| 1508 | try { |
| 1509 | try { |
| 1510 | synchronized(this) { |
| 1511 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1512 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1513 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1514 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1515 | // + ", write delay=" + nextWriteDelay); |
| 1516 | if (nextWriteDelay < nextCpuDelay) { |
| 1517 | nextCpuDelay = nextWriteDelay; |
| 1518 | } |
| 1519 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1520 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1521 | this.wait(nextCpuDelay); |
| 1522 | } |
| 1523 | } |
| 1524 | } catch (InterruptedException e) { |
| 1525 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1526 | updateCpuStatsNow(); |
| 1527 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1528 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1529 | } |
| 1530 | } |
| 1531 | } |
| 1532 | }; |
| 1533 | mProcessStatsThread.start(); |
| 1534 | } |
| 1535 | |
| 1536 | @Override |
| 1537 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1538 | throws RemoteException { |
| 1539 | try { |
| 1540 | return super.onTransact(code, data, reply, flags); |
| 1541 | } catch (RuntimeException e) { |
| 1542 | // The activity manager only throws security exceptions, so let's |
| 1543 | // log all others. |
| 1544 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1545 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1546 | } |
| 1547 | throw e; |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1552 | final long now = SystemClock.uptimeMillis(); |
| 1553 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1554 | return; |
| 1555 | } |
| 1556 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1557 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1558 | mProcessStatsThread.notify(); |
| 1559 | } |
| 1560 | } |
| 1561 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1562 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1563 | void updateCpuStatsNow() { |
| 1564 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1565 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1566 | final long now = SystemClock.uptimeMillis(); |
| 1567 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1568 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1569 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1570 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1571 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1572 | haveNewCpuStats = true; |
| 1573 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1574 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1575 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1576 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1577 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1578 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1580 | int user = mProcessStats.getLastUserTime(); |
| 1581 | int system = mProcessStats.getLastSystemTime(); |
| 1582 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1583 | int irq = mProcessStats.getLastIrqTime(); |
| 1584 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1585 | int idle = mProcessStats.getLastIdleTime(); |
| 1586 | |
| 1587 | int total = user + system + iowait + irq + softIrq + idle; |
| 1588 | if (total == 0) total = 1; |
| 1589 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1590 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1591 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1592 | (user * 100) / total, |
| 1593 | (system * 100) / total, |
| 1594 | (iowait * 100) / total, |
| 1595 | (irq * 100) / total, |
| 1596 | (softIrq * 100) / total); |
| 1597 | } |
| 1598 | } |
| 1599 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1600 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1601 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1602 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1603 | synchronized(mPidsSelfLocked) { |
| 1604 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1605 | if (mOnBattery) { |
| 1606 | int perc = bstats.startAddingCpuLocked(); |
| 1607 | int totalUTime = 0; |
| 1608 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1609 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1611 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1612 | if (!st.working) { |
| 1613 | continue; |
| 1614 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1616 | int otherUTime = (st.rel_utime*perc)/100; |
| 1617 | int otherSTime = (st.rel_stime*perc)/100; |
| 1618 | totalUTime += otherUTime; |
| 1619 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | if (pr != null) { |
| 1621 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1622 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1623 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1624 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1625 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1626 | } else { |
| 1627 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1628 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1629 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1630 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1631 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1632 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1633 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1634 | } |
| 1635 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1636 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1637 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1638 | } |
| 1639 | } |
| 1640 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1641 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1642 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1643 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1644 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1645 | } |
| 1646 | } |
| 1647 | } |
| 1648 | } |
| 1649 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1650 | @Override |
| 1651 | public void batteryNeedsCpuUpdate() { |
| 1652 | updateCpuStatsNow(); |
| 1653 | } |
| 1654 | |
| 1655 | @Override |
| 1656 | public void batteryPowerChanged(boolean onBattery) { |
| 1657 | // When plugging in, update the CPU stats first before changing |
| 1658 | // the plug state. |
| 1659 | updateCpuStatsNow(); |
| 1660 | synchronized (this) { |
| 1661 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1662 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1667 | /** |
| 1668 | * Initialize the application bind args. These are passed to each |
| 1669 | * process when the bindApplication() IPC is sent to the process. They're |
| 1670 | * lazily setup to make sure the services are running when they're asked for. |
| 1671 | */ |
| 1672 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1673 | if (mAppBindArgs == null) { |
| 1674 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1675 | |
| 1676 | // Setup the application init args |
| 1677 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1678 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1679 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1680 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1681 | } |
| 1682 | return mAppBindArgs; |
| 1683 | } |
| 1684 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1685 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1686 | if (mFocusedActivity != r) { |
| 1687 | mFocusedActivity = r; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1688 | if (r != null) { |
| 1689 | mWindowManager.setFocusedApp(r.appToken, true); |
| 1690 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1691 | } |
| 1692 | } |
| 1693 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1694 | private final void updateLruProcessInternalLocked(ProcessRecord app, |
| 1695 | boolean oomAdj, boolean updateActivityTime, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1696 | // put it on the LRU to keep track of when it should be exited. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1697 | int lrui = mLruProcesses.indexOf(app); |
| 1698 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1699 | |
| 1700 | int i = mLruProcesses.size()-1; |
| 1701 | int skipTop = 0; |
| 1702 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1703 | app.lruSeq = mLruSeq; |
| 1704 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1705 | // compute the new weight for this process. |
| 1706 | if (updateActivityTime) { |
| 1707 | app.lastActivityTime = SystemClock.uptimeMillis(); |
| 1708 | } |
| 1709 | if (app.activities.size() > 0) { |
| 1710 | // If this process has activities, we more strongly want to keep |
| 1711 | // it around. |
| 1712 | app.lruWeight = app.lastActivityTime; |
| 1713 | } else if (app.pubProviders.size() > 0) { |
| 1714 | // If this process contains content providers, we want to keep |
| 1715 | // it a little more strongly. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1716 | app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1717 | // Also don't let it kick out the first few "real" hidden processes. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1718 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1719 | } else { |
| 1720 | // If this process doesn't have activities, we less strongly |
| 1721 | // want to keep it around, and generally want to avoid getting |
| 1722 | // in front of any very recently used activities. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1723 | app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1724 | // Also don't let it kick out the first few "real" hidden processes. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1725 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1726 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1727 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1728 | while (i >= 0) { |
| 1729 | ProcessRecord p = mLruProcesses.get(i); |
| 1730 | // If this app shouldn't be in front of the first N background |
| 1731 | // apps, then skip over that many that are currently hidden. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1732 | if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1733 | skipTop--; |
| 1734 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1735 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1736 | mLruProcesses.add(i+1, app); |
| 1737 | break; |
| 1738 | } |
| 1739 | i--; |
| 1740 | } |
| 1741 | if (i < 0) { |
| 1742 | mLruProcesses.add(0, app); |
| 1743 | } |
| 1744 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1745 | // If the app is currently using a content provider or service, |
| 1746 | // bump those processes as well. |
| 1747 | if (app.connections.size() > 0) { |
| 1748 | for (ConnectionRecord cr : app.connections) { |
| 1749 | if (cr.binding != null && cr.binding.service != null |
| 1750 | && cr.binding.service.app != null |
| 1751 | && cr.binding.service.app.lruSeq != mLruSeq) { |
| 1752 | updateLruProcessInternalLocked(cr.binding.service.app, oomAdj, |
| 1753 | updateActivityTime, i+1); |
| 1754 | } |
| 1755 | } |
| 1756 | } |
| 1757 | if (app.conProviders.size() > 0) { |
| 1758 | for (ContentProviderRecord cpr : app.conProviders.keySet()) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 1759 | if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) { |
| 1760 | updateLruProcessInternalLocked(cpr.proc, oomAdj, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1761 | updateActivityTime, i+1); |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1766 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1767 | if (oomAdj) { |
| 1768 | updateOomAdjLocked(); |
| 1769 | } |
| 1770 | } |
| 1771 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1772 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1773 | boolean oomAdj, boolean updateActivityTime) { |
| 1774 | mLruSeq++; |
| 1775 | updateLruProcessInternalLocked(app, oomAdj, updateActivityTime, 0); |
| 1776 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1777 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1778 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1779 | String processName, int uid) { |
| 1780 | if (uid == Process.SYSTEM_UID) { |
| 1781 | // The system gets to run in any process. If there are multiple |
| 1782 | // processes with the same uid, just pick the first (this |
| 1783 | // should never happen). |
| 1784 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1785 | processName); |
| 1786 | return procs != null ? procs.valueAt(0) : null; |
| 1787 | } |
| 1788 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1789 | return proc; |
| 1790 | } |
| 1791 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1792 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1793 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1794 | try { |
| 1795 | if (pm.performDexOpt(packageName)) { |
| 1796 | mDidDexOpt = true; |
| 1797 | } |
| 1798 | } catch (RemoteException e) { |
| 1799 | } |
| 1800 | } |
| 1801 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1802 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1803 | int transit = mWindowManager.getPendingAppTransition(); |
| 1804 | return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN |
| 1805 | || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN |
| 1806 | || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT; |
| 1807 | } |
| 1808 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1809 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1810 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1811 | String hostingType, ComponentName hostingName, boolean allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1812 | ProcessRecord app = getProcessRecordLocked(processName, info.uid); |
| 1813 | // We don't have to do anything more if: |
| 1814 | // (1) There is an existing application record; and |
| 1815 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 1816 | // object attached to it so we know it couldn't have crashed; and |
| 1817 | // (3) There is a pid assigned to it, so it is either starting or |
| 1818 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1819 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1820 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 1821 | + " thread=" + (app != null ? app.thread : null) |
| 1822 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1823 | if (app != null && app.pid > 0) { |
| 1824 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1825 | // We already have the app running, or are waiting for it to |
| 1826 | // come up (we have a pid but not yet its thread), so keep it. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1827 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1828 | // If this is a new package in the process, add the package to the list |
| 1829 | app.addPackage(info.packageName); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1830 | return app; |
| 1831 | } else { |
| 1832 | // An application record is attached to a previous process, |
| 1833 | // clean it up now. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1834 | if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 1835 | handleAppDiedLocked(app, true, true); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1836 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1837 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1838 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1839 | String hostingNameStr = hostingName != null |
| 1840 | ? hostingName.flattenToShortString() : null; |
| 1841 | |
| 1842 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 1843 | // If we are in the background, then check to see if this process |
| 1844 | // is bad. If so, we will just silently fail. |
| 1845 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1846 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 1847 | + "/" + info.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1848 | return null; |
| 1849 | } |
| 1850 | } else { |
| 1851 | // When the user is explicitly starting a process, then clear its |
| 1852 | // crash count so that we won't make it bad until they see at |
| 1853 | // least one crash dialog again, and make the process good again |
| 1854 | // if it had been bad. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1855 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
| 1856 | + "/" + info.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1857 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 1858 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1859 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1860 | info.processName); |
| 1861 | mBadProcesses.remove(info.processName, info.uid); |
| 1862 | if (app != null) { |
| 1863 | app.bad = false; |
| 1864 | } |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | if (app == null) { |
| 1869 | app = newProcessRecordLocked(null, info, processName); |
| 1870 | mProcessNames.put(processName, info.uid, app); |
| 1871 | } else { |
| 1872 | // If this is a new package in the process, add the package to the list |
| 1873 | app.addPackage(info.packageName); |
| 1874 | } |
| 1875 | |
| 1876 | // If the system is not ready yet, then hold off on starting this |
| 1877 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1878 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1879 | && !isAllowedWhileBooting(info) |
| 1880 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1881 | if (!mProcessesOnHold.contains(app)) { |
| 1882 | mProcessesOnHold.add(app); |
| 1883 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1884 | if (DEBUG_PROCESSES) Slog.v(TAG, "System not ready, putting on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1885 | return app; |
| 1886 | } |
| 1887 | |
| 1888 | startProcessLocked(app, hostingType, hostingNameStr); |
| 1889 | return (app.pid != 0) ? app : null; |
| 1890 | } |
| 1891 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1892 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 1893 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 1894 | } |
| 1895 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1896 | private final void startProcessLocked(ProcessRecord app, |
| 1897 | String hostingType, String hostingNameStr) { |
| 1898 | if (app.pid > 0 && app.pid != MY_PID) { |
| 1899 | synchronized (mPidsSelfLocked) { |
| 1900 | mPidsSelfLocked.remove(app.pid); |
| 1901 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 1902 | } |
| 1903 | app.pid = 0; |
| 1904 | } |
| 1905 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1906 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 1907 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1908 | mProcessesOnHold.remove(app); |
| 1909 | |
| 1910 | updateCpuStats(); |
| 1911 | |
| 1912 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 1913 | mProcDeaths[0] = 0; |
| 1914 | |
| 1915 | try { |
| 1916 | int uid = app.info.uid; |
| 1917 | int[] gids = null; |
| 1918 | try { |
| 1919 | gids = mContext.getPackageManager().getPackageGids( |
| 1920 | app.info.packageName); |
| 1921 | } catch (PackageManager.NameNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1922 | Slog.w(TAG, "Unable to retrieve gids", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1923 | } |
| 1924 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 1925 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 1926 | && mTopComponent != null |
| 1927 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 1928 | uid = 0; |
| 1929 | } |
| 1930 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 1931 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 1932 | uid = 0; |
| 1933 | } |
| 1934 | } |
| 1935 | int debugFlags = 0; |
| 1936 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 1937 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
Elliott Hughes | fa36aee | 2011-06-17 14:39:41 -0700 | [diff] [blame] | 1938 | // Also turn on CheckJNI for debuggable apps. It's quite |
| 1939 | // awkward to turn on otherwise. |
| 1940 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1941 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 1942 | // Run the app in safe mode if its manifest requests so or the |
| 1943 | // system is booted in safe mode. |
| 1944 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 1945 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 1946 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 1947 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1948 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 1949 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 1950 | } |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 1951 | if ("1".equals(SystemProperties.get("debug.jni.logging"))) { |
| 1952 | debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING; |
| 1953 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1954 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 1955 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 1956 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1957 | |
| 1958 | // Start the process. It will either succeed and return a result containing |
| 1959 | // the PID of the new process, or else throw a RuntimeException. |
| 1960 | Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread", |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 1961 | app.processName, uid, uid, gids, debugFlags, |
| 1962 | app.info.targetSdkVersion, null); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1963 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1964 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 1965 | synchronized (bs) { |
| 1966 | if (bs.isOnBattery()) { |
| 1967 | app.batteryStats.incStartsLocked(); |
| 1968 | } |
| 1969 | } |
| 1970 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1971 | EventLog.writeEvent(EventLogTags.AM_PROC_START, startResult.pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1972 | app.processName, hostingType, |
| 1973 | hostingNameStr != null ? hostingNameStr : ""); |
| 1974 | |
| 1975 | if (app.persistent) { |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1976 | Watchdog.getInstance().processStarted(app.processName, startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1977 | } |
| 1978 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 1979 | StringBuilder buf = mStringBuilder; |
| 1980 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1981 | buf.append("Start proc "); |
| 1982 | buf.append(app.processName); |
| 1983 | buf.append(" for "); |
| 1984 | buf.append(hostingType); |
| 1985 | if (hostingNameStr != null) { |
| 1986 | buf.append(" "); |
| 1987 | buf.append(hostingNameStr); |
| 1988 | } |
| 1989 | buf.append(": pid="); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1990 | buf.append(startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1991 | buf.append(" uid="); |
| 1992 | buf.append(uid); |
| 1993 | buf.append(" gids={"); |
| 1994 | if (gids != null) { |
| 1995 | for (int gi=0; gi<gids.length; gi++) { |
| 1996 | if (gi != 0) buf.append(", "); |
| 1997 | buf.append(gids[gi]); |
| 1998 | |
| 1999 | } |
| 2000 | } |
| 2001 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2002 | Slog.i(TAG, buf.toString()); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2003 | app.pid = startResult.pid; |
| 2004 | app.usingWrapper = startResult.usingWrapper; |
| 2005 | app.removed = false; |
| 2006 | synchronized (mPidsSelfLocked) { |
| 2007 | this.mPidsSelfLocked.put(startResult.pid, app); |
| 2008 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 2009 | msg.obj = app; |
| 2010 | mHandler.sendMessageDelayed(msg, startResult.usingWrapper |
| 2011 | ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2012 | } |
| 2013 | } catch (RuntimeException e) { |
| 2014 | // XXX do better error recovery. |
| 2015 | app.pid = 0; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2016 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2017 | } |
| 2018 | } |
| 2019 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2020 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2021 | if (resumed) { |
| 2022 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 2023 | } else { |
| 2024 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 2025 | } |
| 2026 | } |
| 2027 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2028 | boolean startHomeActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2029 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2030 | && mTopAction == null) { |
| 2031 | // We are running in factory test mode, but unable to find |
| 2032 | // the factory test app, so just sit around displaying the |
| 2033 | // error message and don't try to start anything. |
| 2034 | return false; |
| 2035 | } |
| 2036 | Intent intent = new Intent( |
| 2037 | mTopAction, |
| 2038 | mTopData != null ? Uri.parse(mTopData) : null); |
| 2039 | intent.setComponent(mTopComponent); |
| 2040 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 2041 | intent.addCategory(Intent.CATEGORY_HOME); |
| 2042 | } |
| 2043 | ActivityInfo aInfo = |
| 2044 | intent.resolveActivityInfo(mContext.getPackageManager(), |
| 2045 | STOCK_PM_FLAGS); |
| 2046 | if (aInfo != null) { |
| 2047 | intent.setComponent(new ComponentName( |
| 2048 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2049 | // Don't do this if the home app is currently being |
| 2050 | // instrumented. |
| 2051 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 2052 | aInfo.applicationInfo.uid); |
| 2053 | if (app == null || app.instrumentationClass == null) { |
| 2054 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2055 | mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2056 | null, null, 0, 0, 0, false, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | |
| 2061 | return true; |
| 2062 | } |
| 2063 | |
| 2064 | /** |
| 2065 | * Starts the "new version setup screen" if appropriate. |
| 2066 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2067 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2068 | // Only do this once per boot. |
| 2069 | if (mCheckedForSetup) { |
| 2070 | return; |
| 2071 | } |
| 2072 | |
| 2073 | // We will show this screen if the current one is a different |
| 2074 | // version than the last one shown, and we are not running in |
| 2075 | // low-level factory test mode. |
| 2076 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2077 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
| 2078 | Settings.Secure.getInt(resolver, |
| 2079 | Settings.Secure.DEVICE_PROVISIONED, 0) != 0) { |
| 2080 | mCheckedForSetup = true; |
| 2081 | |
| 2082 | // See if we should be showing the platform update setup UI. |
| 2083 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 2084 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 2085 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 2086 | |
| 2087 | // We don't allow third party apps to replace this. |
| 2088 | ResolveInfo ri = null; |
| 2089 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 2090 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 2091 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2092 | ri = ris.get(i); |
| 2093 | break; |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | if (ri != null) { |
| 2098 | String vers = ri.activityInfo.metaData != null |
| 2099 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 2100 | : null; |
| 2101 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2102 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2103 | Intent.METADATA_SETUP_VERSION); |
| 2104 | } |
| 2105 | String lastVers = Settings.Secure.getString( |
| 2106 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2107 | if (vers != null && !vers.equals(lastVers)) { |
| 2108 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2109 | intent.setComponent(new ComponentName( |
| 2110 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2111 | mMainStack.startActivityLocked(null, intent, null, null, 0, ri.activityInfo, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2112 | null, null, 0, 0, 0, false, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2113 | } |
| 2114 | } |
| 2115 | } |
| 2116 | } |
| 2117 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2118 | CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2119 | return mCompatModePackages.compatibilityInfoForPackageLocked(ai); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2120 | } |
| 2121 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2122 | public int getFrontActivityScreenCompatMode() { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2123 | synchronized (this) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2124 | return mCompatModePackages.getFrontActivityScreenCompatModeLocked(); |
| 2125 | } |
| 2126 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2127 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2128 | public void setFrontActivityScreenCompatMode(int mode) { |
| 2129 | synchronized (this) { |
| 2130 | mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode); |
| 2131 | } |
| 2132 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2133 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2134 | public int getPackageScreenCompatMode(String packageName) { |
| 2135 | synchronized (this) { |
| 2136 | return mCompatModePackages.getPackageScreenCompatModeLocked(packageName); |
| 2137 | } |
| 2138 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2139 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2140 | public void setPackageScreenCompatMode(String packageName, int mode) { |
| 2141 | synchronized (this) { |
| 2142 | mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2143 | } |
| 2144 | } |
| 2145 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2146 | public boolean getPackageAskScreenCompat(String packageName) { |
| 2147 | synchronized (this) { |
| 2148 | return mCompatModePackages.getPackageAskCompatModeLocked(packageName); |
| 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | public void setPackageAskScreenCompat(String packageName, boolean ask) { |
| 2153 | synchronized (this) { |
| 2154 | mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask); |
| 2155 | } |
| 2156 | } |
| 2157 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2158 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2159 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2160 | |
| 2161 | final int identHash = System.identityHashCode(r); |
| 2162 | updateUsageStats(r, true); |
| 2163 | |
| 2164 | int i = mWatchers.beginBroadcast(); |
| 2165 | while (i > 0) { |
| 2166 | i--; |
| 2167 | IActivityWatcher w = mWatchers.getBroadcastItem(i); |
| 2168 | if (w != null) { |
| 2169 | try { |
| 2170 | w.activityResuming(identHash); |
| 2171 | } catch (RemoteException e) { |
| 2172 | } |
| 2173 | } |
| 2174 | } |
| 2175 | mWatchers.finishBroadcast(); |
| 2176 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2177 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2178 | private void dispatchForegroundActivitiesChanged(int pid, int uid, boolean foregroundActivities) { |
| 2179 | int i = mProcessObservers.beginBroadcast(); |
| 2180 | while (i > 0) { |
| 2181 | i--; |
| 2182 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2183 | if (observer != null) { |
| 2184 | try { |
| 2185 | observer.onForegroundActivitiesChanged(pid, uid, foregroundActivities); |
| 2186 | } catch (RemoteException e) { |
| 2187 | } |
| 2188 | } |
| 2189 | } |
| 2190 | mProcessObservers.finishBroadcast(); |
| 2191 | } |
| 2192 | |
| 2193 | private void dispatchProcessDied(int pid, int uid) { |
| 2194 | int i = mProcessObservers.beginBroadcast(); |
| 2195 | while (i > 0) { |
| 2196 | i--; |
| 2197 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2198 | if (observer != null) { |
| 2199 | try { |
| 2200 | observer.onProcessDied(pid, uid); |
| 2201 | } catch (RemoteException e) { |
| 2202 | } |
| 2203 | } |
| 2204 | } |
| 2205 | mProcessObservers.finishBroadcast(); |
| 2206 | } |
| 2207 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2208 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2209 | final int N = mPendingActivityLaunches.size(); |
| 2210 | if (N <= 0) { |
| 2211 | return; |
| 2212 | } |
| 2213 | for (int i=0; i<N; i++) { |
| 2214 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2215 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2216 | pal.grantedUriPermissions, pal.grantedMode, pal.onlyIfNeeded, |
| 2217 | doResume && i == (N-1)); |
| 2218 | } |
| 2219 | mPendingActivityLaunches.clear(); |
| 2220 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2221 | |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2222 | public final int startActivity(IApplicationThread caller, |
| 2223 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2224 | int grantedMode, IBinder resultTo, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2225 | String resultWho, int requestCode, boolean onlyIfNeeded, boolean debug, |
| 2226 | String profileFile, ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2227 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2228 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2229 | requestCode, onlyIfNeeded, debug, profileFile, profileFd, autoStopProfiler, |
| 2230 | null, null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | public final WaitResult startActivityAndWait(IApplicationThread caller, |
| 2234 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2235 | int grantedMode, IBinder resultTo, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2236 | String resultWho, int requestCode, boolean onlyIfNeeded, boolean debug, |
| 2237 | String profileFile, ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2238 | WaitResult res = new WaitResult(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2239 | mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2240 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2241 | requestCode, onlyIfNeeded, debug, profileFile, profileFd, autoStopProfiler, |
| 2242 | res, null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2243 | return res; |
| 2244 | } |
| 2245 | |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2246 | public final int startActivityWithConfig(IApplicationThread caller, |
| 2247 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2248 | int grantedMode, IBinder resultTo, |
| 2249 | String resultWho, int requestCode, boolean onlyIfNeeded, |
| 2250 | boolean debug, Configuration config) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2251 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2252 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2253 | requestCode, onlyIfNeeded, debug, null, null, false, null, config); |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2254 | } |
| 2255 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2256 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2257 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2258 | IBinder resultTo, String resultWho, int requestCode, |
| 2259 | int flagsMask, int flagsValues) { |
| 2260 | // Refuse possible leaked file descriptors |
| 2261 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2262 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2263 | } |
| 2264 | |
| 2265 | IIntentSender sender = intent.getTarget(); |
| 2266 | if (!(sender instanceof PendingIntentRecord)) { |
| 2267 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2268 | } |
| 2269 | |
| 2270 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2271 | |
| 2272 | synchronized (this) { |
| 2273 | // If this is coming from the currently resumed activity, it is |
| 2274 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2275 | if (mMainStack.mResumedActivity != null |
| 2276 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2277 | Binder.getCallingUid()) { |
| 2278 | mAppSwitchesAllowedTime = 0; |
| 2279 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 2282 | return pir.sendInner(0, fillInIntent, resolvedType, null, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2283 | null, resultTo, resultWho, requestCode, flagsMask, flagsValues); |
| 2284 | } |
| 2285 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2286 | public boolean startNextMatchingActivity(IBinder callingActivity, |
| 2287 | Intent intent) { |
| 2288 | // Refuse possible leaked file descriptors |
| 2289 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2290 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2291 | } |
| 2292 | |
| 2293 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2294 | ActivityRecord r = mMainStack.isInStackLocked(callingActivity); |
| 2295 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2296 | return false; |
| 2297 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2298 | if (r.app == null || r.app.thread == null) { |
| 2299 | // The caller is not running... d'oh! |
| 2300 | return false; |
| 2301 | } |
| 2302 | intent = new Intent(intent); |
| 2303 | // The caller is not allowed to change the data. |
| 2304 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2305 | // And we are resetting to find the next component... |
| 2306 | intent.setComponent(null); |
| 2307 | |
| 2308 | ActivityInfo aInfo = null; |
| 2309 | try { |
| 2310 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2311 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2312 | intent, r.resolvedType, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 2313 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2314 | |
| 2315 | // Look for the original activity in the list... |
| 2316 | final int N = resolves != null ? resolves.size() : 0; |
| 2317 | for (int i=0; i<N; i++) { |
| 2318 | ResolveInfo rInfo = resolves.get(i); |
| 2319 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2320 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2321 | // We found the current one... the next matching is |
| 2322 | // after it. |
| 2323 | i++; |
| 2324 | if (i<N) { |
| 2325 | aInfo = resolves.get(i).activityInfo; |
| 2326 | } |
| 2327 | break; |
| 2328 | } |
| 2329 | } |
| 2330 | } catch (RemoteException e) { |
| 2331 | } |
| 2332 | |
| 2333 | if (aInfo == null) { |
| 2334 | // Nobody who is next! |
| 2335 | return false; |
| 2336 | } |
| 2337 | |
| 2338 | intent.setComponent(new ComponentName( |
| 2339 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2340 | intent.setFlags(intent.getFlags()&~( |
| 2341 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2342 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2343 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2344 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2345 | |
| 2346 | // Okay now we need to start the new activity, replacing the |
| 2347 | // currently running activity. This is a little tricky because |
| 2348 | // we want to start the new one as if the current one is finished, |
| 2349 | // but not finish the current one first so that there is no flicker. |
| 2350 | // And thus... |
| 2351 | final boolean wasFinishing = r.finishing; |
| 2352 | r.finishing = true; |
| 2353 | |
| 2354 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2355 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2356 | final String resultWho = r.resultWho; |
| 2357 | final int requestCode = r.requestCode; |
| 2358 | r.resultTo = null; |
| 2359 | if (resultTo != null) { |
| 2360 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2361 | } |
| 2362 | |
| 2363 | final long origId = Binder.clearCallingIdentity(); |
| 2364 | // XXX we are not dealing with propagating grantedUriPermissions... |
| 2365 | // those are not yet exposed to user code, so there is no need. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2366 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2367 | r.resolvedType, null, 0, aInfo, |
| 2368 | resultTo != null ? resultTo.appToken : null, resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2369 | requestCode, -1, r.launchedFromUid, false, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2370 | Binder.restoreCallingIdentity(origId); |
| 2371 | |
| 2372 | r.finishing = wasFinishing; |
| 2373 | if (res != START_SUCCESS) { |
| 2374 | return false; |
| 2375 | } |
| 2376 | return true; |
| 2377 | } |
| 2378 | } |
| 2379 | |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2380 | public final int startActivityInPackage(int uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2381 | Intent intent, String resolvedType, IBinder resultTo, |
| 2382 | String resultWho, int requestCode, boolean onlyIfNeeded) { |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2383 | |
| 2384 | // This is so super not safe, that only the system (or okay root) |
| 2385 | // can do it. |
| 2386 | final int callingUid = Binder.getCallingUid(); |
| 2387 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2388 | throw new SecurityException( |
| 2389 | "startActivityInPackage only available to the system"); |
| 2390 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2391 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2392 | return mMainStack.startActivityMayWait(null, uid, intent, resolvedType, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2393 | null, 0, resultTo, resultWho, requestCode, onlyIfNeeded, false, |
| 2394 | null, null, false, null, null); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | public final int startActivities(IApplicationThread caller, |
| 2398 | Intent[] intents, String[] resolvedTypes, IBinder resultTo) { |
| 2399 | return mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo); |
| 2400 | } |
| 2401 | |
| 2402 | public final int startActivitiesInPackage(int uid, |
| 2403 | Intent[] intents, String[] resolvedTypes, IBinder resultTo) { |
| 2404 | |
| 2405 | // This is so super not safe, that only the system (or okay root) |
| 2406 | // can do it. |
| 2407 | final int callingUid = Binder.getCallingUid(); |
| 2408 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2409 | throw new SecurityException( |
| 2410 | "startActivityInPackage only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2411 | } |
| 2412 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2413 | return mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2414 | } |
| 2415 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2416 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2417 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2418 | // Quick case: check if the top-most recent task is the same. |
| 2419 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2420 | return; |
| 2421 | } |
| 2422 | // Remove any existing entries that are the same kind of task. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2423 | for (int i=0; i<N; i++) { |
| 2424 | TaskRecord tr = mRecentTasks.get(i); |
| 2425 | if ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2426 | || (task.intent != null && task.intent.filterEquals(tr.intent))) { |
| 2427 | mRecentTasks.remove(i); |
| 2428 | i--; |
| 2429 | N--; |
| 2430 | if (task.intent == null) { |
| 2431 | // If the new recent task we are adding is not fully |
| 2432 | // specified, then replace it with the existing recent task. |
| 2433 | task = tr; |
| 2434 | } |
| 2435 | } |
| 2436 | } |
| 2437 | if (N >= MAX_RECENT_TASKS) { |
| 2438 | mRecentTasks.remove(N-1); |
| 2439 | } |
| 2440 | mRecentTasks.add(0, task); |
| 2441 | } |
| 2442 | |
| 2443 | public void setRequestedOrientation(IBinder token, |
| 2444 | int requestedOrientation) { |
| 2445 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2446 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2447 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2448 | return; |
| 2449 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2450 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2451 | mWindowManager.setAppOrientation(r.appToken, requestedOrientation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2452 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2453 | mConfiguration, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2454 | r.mayFreezeScreenLocked(r.app) ? r.appToken : null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2455 | if (config != null) { |
| 2456 | r.frozenBeforeDestroy = true; |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 2457 | if (!updateConfigurationLocked(config, r, false, false)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2458 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2459 | } |
| 2460 | } |
| 2461 | Binder.restoreCallingIdentity(origId); |
| 2462 | } |
| 2463 | } |
| 2464 | |
| 2465 | public int getRequestedOrientation(IBinder token) { |
| 2466 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2467 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2468 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2469 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2470 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2471 | return mWindowManager.getAppOrientation(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2472 | } |
| 2473 | } |
| 2474 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2475 | /** |
| 2476 | * This is the internal entry point for handling Activity.finish(). |
| 2477 | * |
| 2478 | * @param token The Binder token referencing the Activity we want to finish. |
| 2479 | * @param resultCode Result code, if any, from this Activity. |
| 2480 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2481 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2482 | * @return Returns true if the activity successfully finished, or false if it is still running. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2483 | */ |
| 2484 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2485 | // Refuse possible leaked file descriptors |
| 2486 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2487 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2488 | } |
| 2489 | |
| 2490 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2491 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2492 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2493 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2494 | if (next != null) { |
| 2495 | // ask watcher if this is allowed |
| 2496 | boolean resumeOK = true; |
| 2497 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2498 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2499 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2500 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | if (!resumeOK) { |
| 2504 | return false; |
| 2505 | } |
| 2506 | } |
| 2507 | } |
| 2508 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2509 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2510 | resultData, "app-request"); |
| 2511 | Binder.restoreCallingIdentity(origId); |
| 2512 | return res; |
| 2513 | } |
| 2514 | } |
| 2515 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2516 | public final void finishHeavyWeightApp() { |
| 2517 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2518 | != PackageManager.PERMISSION_GRANTED) { |
| 2519 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2520 | + Binder.getCallingPid() |
| 2521 | + ", uid=" + Binder.getCallingUid() |
| 2522 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2523 | Slog.w(TAG, msg); |
| 2524 | throw new SecurityException(msg); |
| 2525 | } |
| 2526 | |
| 2527 | synchronized(this) { |
| 2528 | if (mHeavyWeightProcess == null) { |
| 2529 | return; |
| 2530 | } |
| 2531 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2532 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2533 | mHeavyWeightProcess.activities); |
| 2534 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2535 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2536 | if (!r.finishing) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2537 | int index = mMainStack.indexOfTokenLocked(r.appToken); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2538 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2539 | mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED, |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2540 | null, "finish-heavy"); |
| 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | mHeavyWeightProcess = null; |
| 2546 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 2547 | } |
| 2548 | } |
| 2549 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2550 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2551 | String message) { |
| 2552 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2553 | != PackageManager.PERMISSION_GRANTED) { |
| 2554 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2555 | + Binder.getCallingPid() |
| 2556 | + ", uid=" + Binder.getCallingUid() |
| 2557 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2558 | Slog.w(TAG, msg); |
| 2559 | throw new SecurityException(msg); |
| 2560 | } |
| 2561 | |
| 2562 | synchronized(this) { |
| 2563 | ProcessRecord proc = null; |
| 2564 | |
| 2565 | // Figure out which process to kill. We don't trust that initialPid |
| 2566 | // still has any relation to current pids, so must scan through the |
| 2567 | // list. |
| 2568 | synchronized (mPidsSelfLocked) { |
| 2569 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2570 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
| 2571 | if (p.info.uid != uid) { |
| 2572 | continue; |
| 2573 | } |
| 2574 | if (p.pid == initialPid) { |
| 2575 | proc = p; |
| 2576 | break; |
| 2577 | } |
| 2578 | for (String str : p.pkgList) { |
| 2579 | if (str.equals(packageName)) { |
| 2580 | proc = p; |
| 2581 | } |
| 2582 | } |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2587 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2588 | + " initialPid=" + initialPid |
| 2589 | + " packageName=" + packageName); |
| 2590 | return; |
| 2591 | } |
| 2592 | |
| 2593 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2594 | if (proc.pid == Process.myPid()) { |
| 2595 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2596 | return; |
| 2597 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2598 | long ident = Binder.clearCallingIdentity(); |
| 2599 | try { |
| 2600 | proc.thread.scheduleCrash(message); |
| 2601 | } catch (RemoteException e) { |
| 2602 | } |
| 2603 | Binder.restoreCallingIdentity(ident); |
| 2604 | } |
| 2605 | } |
| 2606 | } |
| 2607 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2608 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2609 | int requestCode) { |
| 2610 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2611 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2612 | if (self == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2613 | return; |
| 2614 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2615 | |
| 2616 | final long origId = Binder.clearCallingIdentity(); |
| 2617 | |
| 2618 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2619 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2620 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2621 | if (r.resultTo == self && r.requestCode == requestCode) { |
| 2622 | if ((r.resultWho == null && resultWho == null) || |
| 2623 | (r.resultWho != null && r.resultWho.equals(resultWho))) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2624 | mMainStack.finishActivityLocked(r, i, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2625 | Activity.RESULT_CANCELED, null, "request-sub"); |
| 2626 | } |
| 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | Binder.restoreCallingIdentity(origId); |
| 2631 | } |
| 2632 | } |
| 2633 | |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2634 | public boolean willActivityBeVisible(IBinder token) { |
| 2635 | synchronized(this) { |
| 2636 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2637 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2638 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2639 | if (r.appToken == token) { |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2640 | return true; |
| 2641 | } |
| 2642 | if (r.fullscreen && !r.finishing) { |
| 2643 | return false; |
| 2644 | } |
| 2645 | } |
| 2646 | return true; |
| 2647 | } |
| 2648 | } |
| 2649 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2650 | public void overridePendingTransition(IBinder token, String packageName, |
| 2651 | int enterAnim, int exitAnim) { |
| 2652 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2653 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2654 | if (self == null) { |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2655 | return; |
| 2656 | } |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2657 | |
| 2658 | final long origId = Binder.clearCallingIdentity(); |
| 2659 | |
| 2660 | if (self.state == ActivityState.RESUMED |
| 2661 | || self.state == ActivityState.PAUSING) { |
| 2662 | mWindowManager.overridePendingAppTransition(packageName, |
| 2663 | enterAnim, exitAnim); |
| 2664 | } |
| 2665 | |
| 2666 | Binder.restoreCallingIdentity(origId); |
| 2667 | } |
| 2668 | } |
| 2669 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2670 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2671 | * Main function for removing an existing process from the activity manager |
| 2672 | * as a result of that process going away. Clears out all connections |
| 2673 | * to the process. |
| 2674 | */ |
| 2675 | private final void handleAppDiedLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2676 | boolean restarting, boolean allowRestart) { |
| 2677 | cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2678 | if (!restarting) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2679 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2680 | } |
| 2681 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2682 | if (mProfileProc == app) { |
| 2683 | clearProfilerLocked(); |
| 2684 | } |
| 2685 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2686 | // Just in case... |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2687 | if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) { |
| 2688 | if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity); |
| 2689 | mMainStack.mPausingActivity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2690 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2691 | if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) { |
| 2692 | mMainStack.mLastPausedActivity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | // Remove this application's activities from active lists. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2696 | mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2697 | |
| 2698 | boolean atTop = true; |
| 2699 | boolean hasVisibleActivities = false; |
| 2700 | |
| 2701 | // Clean out the history list. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2702 | int i = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2703 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2704 | TAG, "Removing app " + app + " from history with " + i + " entries"); |
| 2705 | while (i > 0) { |
| 2706 | i--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2707 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2708 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2709 | TAG, "Record #" + i + " " + r + ": app=" + r.app); |
| 2710 | if (r.app == app) { |
| 2711 | if ((!r.haveState && !r.stateNotNeeded) || r.finishing) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2712 | if (ActivityStack.DEBUG_ADD_REMOVE) { |
| 2713 | RuntimeException here = new RuntimeException("here"); |
| 2714 | here.fillInStackTrace(); |
| 2715 | Slog.i(TAG, "Removing activity " + r + " from stack at " + i |
| 2716 | + ": haveState=" + r.haveState |
| 2717 | + " stateNotNeeded=" + r.stateNotNeeded |
| 2718 | + " finishing=" + r.finishing |
| 2719 | + " state=" + r.state, here); |
| 2720 | } |
| 2721 | if (!r.finishing) { |
| 2722 | Slog.w(TAG, "Force removing " + r + ": app died, no saved state"); |
| 2723 | } |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 2724 | r.makeFinishing(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2725 | mMainStack.mHistory.remove(i); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 2726 | r.takeFromHistory(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2727 | mWindowManager.removeAppToken(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2728 | if (VALIDATE_TOKENS) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2729 | mMainStack.validateAppTokensLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2730 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2731 | r.removeUriPermissionsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2732 | |
| 2733 | } else { |
| 2734 | // We have the current state for this activity, so |
| 2735 | // it can be restarted later when needed. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2736 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2737 | TAG, "Keeping entry, setting app to null"); |
| 2738 | if (r.visible) { |
| 2739 | hasVisibleActivities = true; |
| 2740 | } |
| 2741 | r.app = null; |
| 2742 | r.nowVisible = false; |
| 2743 | if (!r.haveState) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2744 | if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG, |
| 2745 | "App died, clearing saved state of " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2746 | r.icicle = null; |
| 2747 | } |
| 2748 | } |
| 2749 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2750 | r.stack.cleanUpActivityLocked(r, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2751 | } |
| 2752 | atTop = false; |
| 2753 | } |
| 2754 | |
| 2755 | app.activities.clear(); |
| 2756 | |
| 2757 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2758 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2759 | + " running instrumentation " + app.instrumentationClass); |
| 2760 | Bundle info = new Bundle(); |
| 2761 | info.putString("shortMsg", "Process crashed."); |
| 2762 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 2763 | } |
| 2764 | |
| 2765 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2766 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2767 | // If there was nothing to resume, and we are not already |
| 2768 | // restarting this process, but there is a visible activity that |
| 2769 | // is hosted by the process... then make sure all visible |
| 2770 | // activities are running, taking care of restarting this |
| 2771 | // process. |
| 2772 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2773 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2774 | } |
| 2775 | } |
| 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 2780 | IBinder threadBinder = thread.asBinder(); |
| 2781 | |
| 2782 | // Find the application record. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2783 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2784 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2785 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 2786 | return i; |
| 2787 | } |
| 2788 | } |
| 2789 | return -1; |
| 2790 | } |
| 2791 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2792 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2793 | IApplicationThread thread) { |
| 2794 | if (thread == null) { |
| 2795 | return null; |
| 2796 | } |
| 2797 | |
| 2798 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2799 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2800 | } |
| 2801 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2802 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2803 | IApplicationThread thread) { |
| 2804 | |
| 2805 | mProcDeaths[0]++; |
| 2806 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2807 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 2808 | synchronized (stats) { |
| 2809 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 2810 | } |
| 2811 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2812 | // Clean up already done if the process has been re-started. |
| 2813 | if (app.pid == pid && app.thread != null && |
| 2814 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 2815 | if (!app.killedBackground) { |
| 2816 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 2817 | + ") has died."); |
| 2818 | } |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 2819 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2820 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2821 | TAG, "Dying app: " + app + ", pid: " + pid |
| 2822 | + ", thread: " + thread.asBinder()); |
| 2823 | boolean doLowMem = app.instrumentationClass == null; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2824 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2825 | |
| 2826 | if (doLowMem) { |
| 2827 | // If there are no longer any background processes running, |
| 2828 | // and the app that died was not running instrumentation, |
| 2829 | // then tell everyone we are now low on memory. |
| 2830 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2831 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2832 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2833 | if (rec.thread != null && rec.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2834 | haveBg = true; |
| 2835 | break; |
| 2836 | } |
| 2837 | } |
| 2838 | |
| 2839 | if (!haveBg) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2840 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2841 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2842 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2843 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 2844 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2845 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 2846 | // The low memory report is overriding any current |
| 2847 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2848 | // heavy/important/visible/foreground processes first. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2849 | if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2850 | rec.lastRequestedGc = 0; |
| 2851 | } else { |
| 2852 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2853 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2854 | rec.reportLowMemory = true; |
| 2855 | rec.lastLowMemory = now; |
| 2856 | mProcessesToGc.remove(rec); |
| 2857 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2858 | } |
| 2859 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 2860 | mHandler.sendEmptyMessage(REPORT_MEM_USAGE); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2861 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2862 | } |
| 2863 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2864 | } else if (app.pid != pid) { |
| 2865 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2866 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2867 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | 28a8c2b | 2010-03-01 11:30:02 -0800 | [diff] [blame] | 2868 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 2869 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2870 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2871 | + thread.asBinder()); |
| 2872 | } |
| 2873 | } |
| 2874 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2875 | /** |
| 2876 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2877 | * @param clearTraces causes the dump file to be erased prior to the new |
| 2878 | * traces being written, if true; when false, the new traces will be |
| 2879 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2880 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 2881 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2882 | * @return file containing stack traces, or null if no dump file is configured |
| 2883 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2884 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
| 2885 | ProcessStats processStats, SparseArray<Boolean> lastPids) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2886 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 2887 | if (tracesPath == null || tracesPath.length() == 0) { |
| 2888 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2889 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2890 | |
| 2891 | File tracesFile = new File(tracesPath); |
| 2892 | try { |
| 2893 | File tracesDir = tracesFile.getParentFile(); |
| 2894 | if (!tracesDir.exists()) tracesFile.mkdirs(); |
| 2895 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 2896 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2897 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2898 | tracesFile.createNewFile(); |
| 2899 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 2900 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2901 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2902 | return null; |
| 2903 | } |
| 2904 | |
| 2905 | // Use a FileObserver to detect when traces finish writing. |
| 2906 | // The order of traces is considered important to maintain for legibility. |
| 2907 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 2908 | public synchronized void onEvent(int event, String path) { notify(); } |
| 2909 | }; |
| 2910 | |
| 2911 | try { |
| 2912 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2913 | |
| 2914 | // First collect all of the stacks of the most important pids. |
| 2915 | try { |
| 2916 | int num = firstPids.size(); |
| 2917 | for (int i = 0; i < num; i++) { |
| 2918 | synchronized (observer) { |
| 2919 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 2920 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 2921 | } |
| 2922 | } |
| 2923 | } catch (InterruptedException e) { |
| 2924 | Log.wtf(TAG, e); |
| 2925 | } |
| 2926 | |
| 2927 | // Next measure CPU usage. |
| 2928 | if (processStats != null) { |
| 2929 | processStats.init(); |
| 2930 | System.gc(); |
| 2931 | processStats.update(); |
| 2932 | try { |
| 2933 | synchronized (processStats) { |
| 2934 | processStats.wait(500); // measure over 1/2 second. |
| 2935 | } |
| 2936 | } catch (InterruptedException e) { |
| 2937 | } |
| 2938 | processStats.update(); |
| 2939 | |
| 2940 | // We'll take the stack crawls of just the top apps using CPU. |
| 2941 | final int N = processStats.countWorkingStats(); |
| 2942 | int numProcs = 0; |
| 2943 | for (int i=0; i<N && numProcs<5; i++) { |
| 2944 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 2945 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 2946 | numProcs++; |
| 2947 | try { |
| 2948 | synchronized (observer) { |
| 2949 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 2950 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 2951 | } |
| 2952 | } catch (InterruptedException e) { |
| 2953 | Log.wtf(TAG, e); |
| 2954 | } |
| 2955 | |
| 2956 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2957 | } |
| 2958 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2959 | |
| 2960 | return tracesFile; |
| 2961 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2962 | } finally { |
| 2963 | observer.stopWatching(); |
| 2964 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2965 | } |
| 2966 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 2967 | private final class AppNotResponding implements Runnable { |
| 2968 | private final ProcessRecord mApp; |
| 2969 | private final String mAnnotation; |
| 2970 | |
| 2971 | public AppNotResponding(ProcessRecord app, String annotation) { |
| 2972 | mApp = app; |
| 2973 | mAnnotation = annotation; |
| 2974 | } |
| 2975 | |
| 2976 | @Override |
| 2977 | public void run() { |
| 2978 | appNotResponding(mApp, null, null, mAnnotation); |
| 2979 | } |
| 2980 | } |
| 2981 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2982 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
| 2983 | ActivityRecord parent, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2984 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 2985 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 2986 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 2987 | if (mController != null) { |
| 2988 | try { |
| 2989 | // 0 == continue, -1 = kill process immediately |
| 2990 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 2991 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 2992 | } catch (RemoteException e) { |
| 2993 | mController = null; |
| 2994 | } |
| 2995 | } |
| 2996 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2997 | long anrTime = SystemClock.uptimeMillis(); |
| 2998 | if (MONITOR_CPU_USAGE) { |
| 2999 | updateCpuStatsNow(); |
| 3000 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3001 | |
| 3002 | synchronized (this) { |
| 3003 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 3004 | if (mShuttingDown) { |
| 3005 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 3006 | return; |
| 3007 | } else if (app.notResponding) { |
| 3008 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 3009 | return; |
| 3010 | } else if (app.crashing) { |
| 3011 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 3012 | return; |
| 3013 | } |
| 3014 | |
| 3015 | // In case we come through here for the same app before completing |
| 3016 | // this one, mark as anring now so we will bail out. |
| 3017 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3018 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3019 | // Log the ANR to the event log. |
| 3020 | EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags, |
| 3021 | annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3022 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3023 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3024 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3025 | |
| 3026 | int parentPid = app.pid; |
| 3027 | if (parent != null && parent.app != null && parent.app.pid > 0) parentPid = parent.app.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3028 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3029 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3030 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3031 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3032 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 3033 | ProcessRecord r = mLruProcesses.get(i); |
| 3034 | if (r != null && r.thread != null) { |
| 3035 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3036 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 3037 | if (r.persistent) { |
| 3038 | firstPids.add(pid); |
| 3039 | } else { |
| 3040 | lastPids.put(pid, Boolean.TRUE); |
| 3041 | } |
| 3042 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3043 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3044 | } |
| 3045 | } |
| 3046 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3047 | // Log the ANR to the main log. |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3048 | StringBuilder info = mStringBuilder; |
| 3049 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3050 | info.append("ANR in ").append(app.processName); |
| 3051 | if (activity != null && activity.shortComponentName != null) { |
| 3052 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 3053 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3054 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3055 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3056 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3057 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3058 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3059 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3060 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3061 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3062 | final ProcessStats processStats = new ProcessStats(true); |
| 3063 | |
| 3064 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids); |
| 3065 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3066 | String cpuInfo = null; |
| 3067 | if (MONITOR_CPU_USAGE) { |
| 3068 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3069 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3070 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3071 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3072 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3073 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3074 | } |
| 3075 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3076 | info.append(processStats.printCurrentState(anrTime)); |
| 3077 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3078 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3079 | if (tracesFile == null) { |
| 3080 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 3081 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 3082 | } |
| 3083 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 3084 | addErrorToDropBox("anr", app, app.processName, activity, parent, annotation, |
| 3085 | cpuInfo, tracesFile, null); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3086 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3087 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3088 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3089 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 3090 | int res = mController.appNotResponding(app.processName, app.pid, info.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3091 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3092 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3093 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3094 | } |
| 3095 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3096 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3097 | } |
| 3098 | } |
| 3099 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3100 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 3101 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 3102 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3103 | |
| 3104 | synchronized (this) { |
| 3105 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3106 | Slog.w(TAG, "Killing " + app + ": background ANR"); |
| 3107 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 3108 | app.processName, app.setAdj, "background ANR"); |
| 3109 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3110 | return; |
| 3111 | } |
| 3112 | |
| 3113 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 3114 | makeAppNotRespondingLocked(app, |
| 3115 | activity != null ? activity.shortComponentName : null, |
| 3116 | annotation != null ? "ANR " + annotation : "ANR", |
| 3117 | info.toString()); |
| 3118 | |
| 3119 | // Bring up the infamous App Not Responding dialog |
| 3120 | Message msg = Message.obtain(); |
| 3121 | HashMap map = new HashMap(); |
| 3122 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3123 | msg.obj = map; |
| 3124 | map.put("app", app); |
| 3125 | if (activity != null) { |
| 3126 | map.put("activity", activity); |
| 3127 | } |
| 3128 | |
| 3129 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3130 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3131 | } |
| 3132 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3133 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3134 | if (!mLaunchWarningShown) { |
| 3135 | mLaunchWarningShown = true; |
| 3136 | mHandler.post(new Runnable() { |
| 3137 | @Override |
| 3138 | public void run() { |
| 3139 | synchronized (ActivityManagerService.this) { |
| 3140 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3141 | d.show(); |
| 3142 | mHandler.postDelayed(new Runnable() { |
| 3143 | @Override |
| 3144 | public void run() { |
| 3145 | synchronized (ActivityManagerService.this) { |
| 3146 | d.dismiss(); |
| 3147 | mLaunchWarningShown = false; |
| 3148 | } |
| 3149 | } |
| 3150 | }, 4000); |
| 3151 | } |
| 3152 | } |
| 3153 | }); |
| 3154 | } |
| 3155 | } |
| 3156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3157 | public boolean clearApplicationUserData(final String packageName, |
| 3158 | final IPackageDataObserver observer) { |
| 3159 | int uid = Binder.getCallingUid(); |
| 3160 | int pid = Binder.getCallingPid(); |
| 3161 | long callingId = Binder.clearCallingIdentity(); |
| 3162 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3163 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3164 | int pkgUid = -1; |
| 3165 | synchronized(this) { |
| 3166 | try { |
| 3167 | pkgUid = pm.getPackageUid(packageName); |
| 3168 | } catch (RemoteException e) { |
| 3169 | } |
| 3170 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3171 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3172 | return false; |
| 3173 | } |
| 3174 | if (uid == pkgUid || checkComponentPermission( |
| 3175 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3176 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3177 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3178 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3179 | } else { |
| 3180 | throw new SecurityException(pid+" does not have permission:"+ |
| 3181 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3182 | "for process:"+packageName); |
| 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | try { |
| 3187 | //clear application user data |
| 3188 | pm.clearApplicationUserData(packageName, observer); |
| 3189 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3190 | Uri.fromParts("package", packageName, null)); |
| 3191 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3192 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
| 3193 | null, null, 0, null, null, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3194 | } catch (RemoteException e) { |
| 3195 | } |
| 3196 | } finally { |
| 3197 | Binder.restoreCallingIdentity(callingId); |
| 3198 | } |
| 3199 | return true; |
| 3200 | } |
| 3201 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3202 | public void killBackgroundProcesses(final String packageName) { |
| 3203 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3204 | != PackageManager.PERMISSION_GRANTED && |
| 3205 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3206 | != PackageManager.PERMISSION_GRANTED) { |
| 3207 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3208 | + Binder.getCallingPid() |
| 3209 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3210 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3211 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3212 | throw new SecurityException(msg); |
| 3213 | } |
| 3214 | |
| 3215 | long callingId = Binder.clearCallingIdentity(); |
| 3216 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3217 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3218 | int pkgUid = -1; |
| 3219 | synchronized(this) { |
| 3220 | try { |
| 3221 | pkgUid = pm.getPackageUid(packageName); |
| 3222 | } catch (RemoteException e) { |
| 3223 | } |
| 3224 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3225 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3226 | return; |
| 3227 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3228 | killPackageProcessesLocked(packageName, pkgUid, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3229 | ProcessList.SERVICE_ADJ, false, true, true, false, "kill background"); |
| 3230 | } |
| 3231 | } finally { |
| 3232 | Binder.restoreCallingIdentity(callingId); |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | public void killAllBackgroundProcesses() { |
| 3237 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3238 | != PackageManager.PERMISSION_GRANTED) { |
| 3239 | String msg = "Permission Denial: killAllBackgroundProcesses() from pid=" |
| 3240 | + Binder.getCallingPid() |
| 3241 | + ", uid=" + Binder.getCallingUid() |
| 3242 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
| 3243 | Slog.w(TAG, msg); |
| 3244 | throw new SecurityException(msg); |
| 3245 | } |
| 3246 | |
| 3247 | long callingId = Binder.clearCallingIdentity(); |
| 3248 | try { |
| 3249 | synchronized(this) { |
| 3250 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 3251 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3252 | final int NA = apps.size(); |
| 3253 | for (int ia=0; ia<NA; ia++) { |
| 3254 | ProcessRecord app = apps.valueAt(ia); |
| 3255 | if (app.persistent) { |
| 3256 | // we don't kill persistent processes |
| 3257 | continue; |
| 3258 | } |
| 3259 | if (app.removed) { |
| 3260 | procs.add(app); |
| 3261 | } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 3262 | app.removed = true; |
| 3263 | procs.add(app); |
| 3264 | } |
| 3265 | } |
| 3266 | } |
| 3267 | |
| 3268 | int N = procs.size(); |
| 3269 | for (int i=0; i<N; i++) { |
| 3270 | removeProcessLocked(procs.get(i), false, true, "kill all background"); |
| 3271 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3272 | } |
| 3273 | } finally { |
| 3274 | Binder.restoreCallingIdentity(callingId); |
| 3275 | } |
| 3276 | } |
| 3277 | |
| 3278 | public void forceStopPackage(final String packageName) { |
| 3279 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3280 | != PackageManager.PERMISSION_GRANTED) { |
| 3281 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3282 | + Binder.getCallingPid() |
| 3283 | + ", uid=" + Binder.getCallingUid() |
| 3284 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3285 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3286 | throw new SecurityException(msg); |
| 3287 | } |
| 3288 | |
| 3289 | long callingId = Binder.clearCallingIdentity(); |
| 3290 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3291 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3292 | int pkgUid = -1; |
| 3293 | synchronized(this) { |
| 3294 | try { |
| 3295 | pkgUid = pm.getPackageUid(packageName); |
| 3296 | } catch (RemoteException e) { |
| 3297 | } |
| 3298 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3299 | Slog.w(TAG, "Invalid packageName: " + packageName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3300 | return; |
| 3301 | } |
| 3302 | forceStopPackageLocked(packageName, pkgUid); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3303 | try { |
| 3304 | pm.setPackageStoppedState(packageName, true); |
| 3305 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 3306 | } catch (IllegalArgumentException e) { |
| 3307 | Slog.w(TAG, "Failed trying to unstop package " |
| 3308 | + packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3309 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3310 | } |
| 3311 | } finally { |
| 3312 | Binder.restoreCallingIdentity(callingId); |
| 3313 | } |
| 3314 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3315 | |
| 3316 | /* |
| 3317 | * The pkg name and uid have to be specified. |
| 3318 | * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int) |
| 3319 | */ |
| 3320 | public void killApplicationWithUid(String pkg, int uid) { |
| 3321 | if (pkg == null) { |
| 3322 | return; |
| 3323 | } |
| 3324 | // Make sure the uid is valid. |
| 3325 | if (uid < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3326 | Slog.w(TAG, "Invalid uid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3327 | return; |
| 3328 | } |
| 3329 | int callerUid = Binder.getCallingUid(); |
| 3330 | // Only the system server can kill an application |
| 3331 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3332 | // Post an aysnc message to kill the application |
| 3333 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
| 3334 | msg.arg1 = uid; |
| 3335 | msg.arg2 = 0; |
| 3336 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3337 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3338 | } else { |
| 3339 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3340 | pkg); |
| 3341 | } |
| 3342 | } |
| 3343 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3344 | public void closeSystemDialogs(String reason) { |
| 3345 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3346 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3347 | if (reason != null) { |
| 3348 | intent.putExtra("reason", reason); |
| 3349 | } |
| 3350 | |
| 3351 | final int uid = Binder.getCallingUid(); |
| 3352 | final long origId = Binder.clearCallingIdentity(); |
| 3353 | synchronized (this) { |
| 3354 | int i = mWatchers.beginBroadcast(); |
| 3355 | while (i > 0) { |
| 3356 | i--; |
| 3357 | IActivityWatcher w = mWatchers.getBroadcastItem(i); |
| 3358 | if (w != null) { |
| 3359 | try { |
| 3360 | w.closingSystemDialogs(reason); |
| 3361 | } catch (RemoteException e) { |
| 3362 | } |
| 3363 | } |
| 3364 | } |
| 3365 | mWatchers.finishBroadcast(); |
| 3366 | |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3367 | mWindowManager.closeSystemDialogs(reason); |
| 3368 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3369 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 3370 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3371 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3372 | r.stack.finishActivityLocked(r, i, |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3373 | Activity.RESULT_CANCELED, null, "close-sys"); |
| 3374 | } |
| 3375 | } |
| 3376 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3377 | broadcastIntentLocked(null, null, intent, null, |
| 3378 | null, 0, null, null, null, false, false, -1, uid); |
| 3379 | } |
| 3380 | Binder.restoreCallingIdentity(origId); |
| 3381 | } |
| 3382 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3383 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3384 | throws RemoteException { |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3385 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3386 | for (int i=pids.length-1; i>=0; i--) { |
| 3387 | infos[i] = new Debug.MemoryInfo(); |
| 3388 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3389 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3390 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3391 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3392 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3393 | public long[] getProcessPss(int[] pids) throws RemoteException { |
| 3394 | long[] pss = new long[pids.length]; |
| 3395 | for (int i=pids.length-1; i>=0; i--) { |
| 3396 | pss[i] = Debug.getPss(pids[i]); |
| 3397 | } |
| 3398 | return pss; |
| 3399 | } |
| 3400 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3401 | public void killApplicationProcess(String processName, int uid) { |
| 3402 | if (processName == null) { |
| 3403 | return; |
| 3404 | } |
| 3405 | |
| 3406 | int callerUid = Binder.getCallingUid(); |
| 3407 | // Only the system server can kill an application |
| 3408 | if (callerUid == Process.SYSTEM_UID) { |
| 3409 | synchronized (this) { |
| 3410 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3411 | if (app != null && app.thread != null) { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3412 | try { |
| 3413 | app.thread.scheduleSuicide(); |
| 3414 | } catch (RemoteException e) { |
| 3415 | // If the other end already died, then our work here is done. |
| 3416 | } |
| 3417 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3418 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3419 | + processName + " / " + uid); |
| 3420 | } |
| 3421 | } |
| 3422 | } else { |
| 3423 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3424 | processName); |
| 3425 | } |
| 3426 | } |
| 3427 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3428 | private void forceStopPackageLocked(final String packageName, int uid) { |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3429 | forceStopPackageLocked(packageName, uid, false, false, true, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3430 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3431 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3432 | if (!mProcessesReady) { |
| 3433 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3434 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3435 | intent.putExtra(Intent.EXTRA_UID, uid); |
| 3436 | broadcastIntentLocked(null, null, intent, |
| 3437 | null, null, 0, null, null, null, |
| 3438 | false, false, MY_PID, Process.SYSTEM_UID); |
| 3439 | } |
| 3440 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3441 | private final boolean killPackageProcessesLocked(String packageName, int uid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3442 | int minOomAdj, boolean callerWillRestart, boolean allowRestart, boolean doit, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3443 | boolean evenPersistent, String reason) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3444 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3445 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3446 | // Remove all processes this package may have touched: all with the |
| 3447 | // same UID (except for the system or root user), and all whose name |
| 3448 | // matches the package name. |
| 3449 | final String procNamePrefix = packageName + ":"; |
| 3450 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3451 | final int NA = apps.size(); |
| 3452 | for (int ia=0; ia<NA; ia++) { |
| 3453 | ProcessRecord app = apps.valueAt(ia); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3454 | if (app.persistent && !evenPersistent) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3455 | // we don't kill persistent processes |
| 3456 | continue; |
| 3457 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3458 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3459 | if (doit) { |
| 3460 | procs.add(app); |
| 3461 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3462 | } else if ((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid) |
| 3463 | || app.processName.equals(packageName) |
| 3464 | || app.processName.startsWith(procNamePrefix)) { |
| 3465 | if (app.setAdj >= minOomAdj) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3466 | if (!doit) { |
| 3467 | return true; |
| 3468 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3469 | app.removed = true; |
| 3470 | procs.add(app); |
| 3471 | } |
| 3472 | } |
| 3473 | } |
| 3474 | } |
| 3475 | |
| 3476 | int N = procs.size(); |
| 3477 | for (int i=0; i<N; i++) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3478 | removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3479 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3480 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3481 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3482 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3483 | private final boolean forceStopPackageLocked(String name, int uid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3484 | boolean callerWillRestart, boolean purgeCache, boolean doit, |
| 3485 | boolean evenPersistent) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3486 | int i; |
| 3487 | int N; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3488 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3489 | if (uid < 0) { |
| 3490 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3491 | uid = AppGlobals.getPackageManager().getPackageUid(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3492 | } catch (RemoteException e) { |
| 3493 | } |
| 3494 | } |
| 3495 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3496 | if (doit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3497 | Slog.i(TAG, "Force stopping package " + name + " uid=" + uid); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3498 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3499 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3500 | while (badApps.hasNext()) { |
| 3501 | SparseArray<Long> ba = badApps.next(); |
| 3502 | if (ba.get(uid) != null) { |
| 3503 | badApps.remove(); |
| 3504 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3505 | } |
| 3506 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3507 | |
| 3508 | boolean didSomething = killPackageProcessesLocked(name, uid, -100, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3509 | callerWillRestart, false, doit, evenPersistent, "force stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3510 | |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3511 | TaskRecord lastTask = null; |
| 3512 | for (i=0; i<mMainStack.mHistory.size(); i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3513 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3514 | final boolean samePackage = r.packageName.equals(name); |
| 3515 | if ((samePackage || r.task == lastTask) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3516 | && (r.app == null || evenPersistent || !r.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3517 | if (!doit) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 3518 | if (r.finishing) { |
| 3519 | // If this activity is just finishing, then it is not |
| 3520 | // interesting as far as something to stop. |
| 3521 | continue; |
| 3522 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3523 | return true; |
| 3524 | } |
| 3525 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3526 | Slog.i(TAG, " Force finishing activity " + r); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3527 | if (samePackage) { |
| 3528 | if (r.app != null) { |
| 3529 | r.app.removed = true; |
| 3530 | } |
| 3531 | r.app = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3532 | } |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3533 | lastTask = r.task; |
| 3534 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
| 3535 | null, "force-stop")) { |
| 3536 | i--; |
| 3537 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3538 | } |
| 3539 | } |
| 3540 | |
| 3541 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
| 3542 | for (ServiceRecord service : mServices.values()) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3543 | if (service.packageName.equals(name) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3544 | && (service.app == null || evenPersistent || !service.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3545 | if (!doit) { |
| 3546 | return true; |
| 3547 | } |
| 3548 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3549 | Slog.i(TAG, " Force stopping service " + service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3550 | if (service.app != null) { |
| 3551 | service.app.removed = true; |
| 3552 | } |
| 3553 | service.app = null; |
| 3554 | services.add(service); |
| 3555 | } |
| 3556 | } |
| 3557 | |
| 3558 | N = services.size(); |
| 3559 | for (i=0; i<N; i++) { |
| 3560 | bringDownServiceLocked(services.get(i), true); |
| 3561 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3562 | |
| 3563 | ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); |
| 3564 | for (ContentProviderRecord provider : mProvidersByClass.values()) { |
| 3565 | if (provider.info.packageName.equals(name) |
| 3566 | && (provider.proc == null || evenPersistent || !provider.proc.persistent)) { |
| 3567 | if (!doit) { |
| 3568 | return true; |
| 3569 | } |
| 3570 | didSomething = true; |
| 3571 | providers.add(provider); |
| 3572 | } |
| 3573 | } |
| 3574 | |
| 3575 | N = providers.size(); |
| 3576 | for (i=0; i<N; i++) { |
| 3577 | removeDyingProviderLocked(null, providers.get(i)); |
| 3578 | } |
| 3579 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3580 | if (doit) { |
| 3581 | if (purgeCache) { |
| 3582 | AttributeCache ac = AttributeCache.instance(); |
| 3583 | if (ac != null) { |
| 3584 | ac.removePackage(name); |
| 3585 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3586 | } |
Dianne Hackborn | 38cc896 | 2011-10-13 11:33:55 -0700 | [diff] [blame] | 3587 | if (mBooted) { |
| 3588 | mMainStack.resumeTopActivityLocked(null); |
| 3589 | mMainStack.scheduleIdleLocked(); |
| 3590 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3591 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3592 | |
| 3593 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3594 | } |
| 3595 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3596 | private final boolean removeProcessLocked(ProcessRecord app, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3597 | boolean callerWillRestart, boolean allowRestart, String reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3598 | final String name = app.processName; |
| 3599 | final int uid = app.info.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3600 | if (DEBUG_PROCESSES) Slog.d( |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3601 | TAG, "Force removing proc " + app.toShortString() + " (" + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3602 | + "/" + uid + ")"); |
| 3603 | |
| 3604 | mProcessNames.remove(name, uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3605 | if (mHeavyWeightProcess == app) { |
| 3606 | mHeavyWeightProcess = null; |
| 3607 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3608 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3609 | boolean needRestart = false; |
| 3610 | if (app.pid > 0 && app.pid != MY_PID) { |
| 3611 | int pid = app.pid; |
| 3612 | synchronized (mPidsSelfLocked) { |
| 3613 | mPidsSelfLocked.remove(pid); |
| 3614 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3615 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3616 | Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3617 | handleAppDiedLocked(app, true, allowRestart); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3618 | mLruProcesses.remove(app); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3619 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3620 | |
| 3621 | if (app.persistent) { |
| 3622 | if (!callerWillRestart) { |
| 3623 | addAppLocked(app.info); |
| 3624 | } else { |
| 3625 | needRestart = true; |
| 3626 | } |
| 3627 | } |
| 3628 | } else { |
| 3629 | mRemovedProcesses.add(app); |
| 3630 | } |
| 3631 | |
| 3632 | return needRestart; |
| 3633 | } |
| 3634 | |
| 3635 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 3636 | final int pid = app.pid; |
| 3637 | boolean gone = false; |
| 3638 | synchronized (mPidsSelfLocked) { |
| 3639 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 3640 | if (knownApp != null && knownApp.thread == null) { |
| 3641 | mPidsSelfLocked.remove(pid); |
| 3642 | gone = true; |
| 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3647 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3648 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.info.uid, |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3649 | app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3650 | mProcessNames.remove(app.processName, app.info.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3651 | if (mHeavyWeightProcess == app) { |
| 3652 | mHeavyWeightProcess = null; |
| 3653 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3654 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3655 | // Take care of any launching providers waiting for this process. |
| 3656 | checkAppInLaunchingProvidersLocked(app, true); |
| 3657 | // Take care of any services that are waiting for the process. |
| 3658 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3659 | ServiceRecord sr = mPendingServices.get(i); |
| 3660 | if (app.info.uid == sr.appInfo.uid |
| 3661 | && app.processName.equals(sr.processName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3662 | Slog.w(TAG, "Forcing bringing down service: " + sr); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3663 | mPendingServices.remove(i); |
| 3664 | i--; |
| 3665 | bringDownServiceLocked(sr, true); |
| 3666 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3667 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3668 | EventLog.writeEvent(EventLogTags.AM_KILL, pid, |
| 3669 | app.processName, app.setAdj, "start timeout"); |
| 3670 | Process.killProcessQuiet(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3671 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3672 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3673 | try { |
| 3674 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 3675 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 3676 | bm.agentDisconnected(app.info.packageName); |
| 3677 | } catch (RemoteException e) { |
| 3678 | // Can't happen; the backup manager is local |
| 3679 | } |
| 3680 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3681 | if (mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3682 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3683 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 3684 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3685 | mPendingBroadcast = null; |
| 3686 | scheduleBroadcastsLocked(); |
| 3687 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3688 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3689 | Slog.w(TAG, "Spurious process start timeout - pid not known for " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3690 | } |
| 3691 | } |
| 3692 | |
| 3693 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 3694 | int pid) { |
| 3695 | |
| 3696 | // Find the application record that is being attached... either via |
| 3697 | // the pid if we are running in multiple processes, or just pull the |
| 3698 | // next app record if we are emulating process with anonymous threads. |
| 3699 | ProcessRecord app; |
| 3700 | if (pid != MY_PID && pid >= 0) { |
| 3701 | synchronized (mPidsSelfLocked) { |
| 3702 | app = mPidsSelfLocked.get(pid); |
| 3703 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3704 | } else { |
| 3705 | app = null; |
| 3706 | } |
| 3707 | |
| 3708 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3709 | Slog.w(TAG, "No pending application record for pid " + pid |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3710 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3711 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3712 | if (pid > 0 && pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3713 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3714 | } else { |
| 3715 | try { |
| 3716 | thread.scheduleExit(); |
| 3717 | } catch (Exception e) { |
| 3718 | // Ignore exceptions. |
| 3719 | } |
| 3720 | } |
| 3721 | return false; |
| 3722 | } |
| 3723 | |
| 3724 | // If this application record is still attached to a previous |
| 3725 | // process, clean it up now. |
| 3726 | if (app.thread != null) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3727 | handleAppDiedLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | // Tell the process all about itself. |
| 3731 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3732 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3733 | TAG, "Binding process pid " + pid + " to record " + app); |
| 3734 | |
| 3735 | String processName = app.processName; |
| 3736 | try { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 3737 | AppDeathRecipient adr = new AppDeathRecipient( |
| 3738 | app, pid, thread); |
| 3739 | thread.asBinder().linkToDeath(adr, 0); |
| 3740 | app.deathRecipient = adr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3741 | } catch (RemoteException e) { |
| 3742 | app.resetPackageList(); |
| 3743 | startProcessLocked(app, "link fail", processName); |
| 3744 | return false; |
| 3745 | } |
| 3746 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3747 | EventLog.writeEvent(EventLogTags.AM_PROC_BOUND, app.pid, app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3748 | |
| 3749 | app.thread = thread; |
| 3750 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 3751 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 3752 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3753 | app.forcingToForeground = null; |
| 3754 | app.foregroundServices = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 3755 | app.hasShownUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3756 | app.debugging = false; |
| 3757 | |
| 3758 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3759 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3760 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3761 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3762 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3763 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3764 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3765 | } |
| 3766 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3767 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3768 | TAG, "New app record " + app |
| 3769 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 3770 | try { |
| 3771 | int testMode = IApplicationThread.DEBUG_OFF; |
| 3772 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 3773 | testMode = mWaitForDebugger |
| 3774 | ? IApplicationThread.DEBUG_WAIT |
| 3775 | : IApplicationThread.DEBUG_ON; |
| 3776 | app.debugging = true; |
| 3777 | if (mDebugTransient) { |
| 3778 | mDebugApp = mOrigDebugApp; |
| 3779 | mWaitForDebugger = mOrigWaitForDebugger; |
| 3780 | } |
| 3781 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3782 | String profileFile = app.instrumentationProfileFile; |
| 3783 | ParcelFileDescriptor profileFd = null; |
| 3784 | boolean profileAutoStop = false; |
| 3785 | if (mProfileApp != null && mProfileApp.equals(processName)) { |
| 3786 | mProfileProc = app; |
| 3787 | profileFile = mProfileFile; |
| 3788 | profileFd = mProfileFd; |
| 3789 | profileAutoStop = mAutoStopProfiler; |
| 3790 | } |
| 3791 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3792 | // If the app is being launched for restore or full backup, set it up specially |
| 3793 | boolean isRestrictedBackupMode = false; |
| 3794 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 3795 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3796 | || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3797 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 3798 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3799 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 3800 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 3801 | ? app.instrumentationInfo.packageName |
| 3802 | : app.info.packageName); |
| 3803 | if (app.instrumentationClass != null) { |
| 3804 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3805 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3806 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 3807 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3808 | ApplicationInfo appInfo = app.instrumentationInfo != null |
| 3809 | ? app.instrumentationInfo : app.info; |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 3810 | app.compat = compatibilityInfoForPackageLocked(appInfo); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3811 | if (profileFd != null) { |
| 3812 | profileFd = profileFd.dup(); |
| 3813 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3814 | thread.bindApplication(processName, appInfo, providers, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3815 | app.instrumentationClass, profileFile, profileFd, profileAutoStop, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3816 | app.instrumentationArguments, app.instrumentationWatcher, testMode, |
Dianne Hackborn | 5d927c2 | 2011-09-02 12:22:18 -0700 | [diff] [blame] | 3817 | isRestrictedBackupMode || !normalMode, app.persistent, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 3818 | new Configuration(mConfiguration), app.compat, getCommonServicesLocked(), |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 3819 | mCoreSettingsObserver.getCoreSettingsLocked()); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3820 | updateLruProcessLocked(app, false, true); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3821 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3822 | } catch (Exception e) { |
| 3823 | // todo: Yikes! What should we do? For now we will try to |
| 3824 | // start another process, but that could easily get us in |
| 3825 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3826 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3827 | |
| 3828 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 3829 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3830 | startProcessLocked(app, "bind fail", processName); |
| 3831 | return false; |
| 3832 | } |
| 3833 | |
| 3834 | // Remove this record from the list of starting applications. |
| 3835 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3836 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 3837 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3838 | mProcessesOnHold.remove(app); |
| 3839 | |
| 3840 | boolean badApp = false; |
| 3841 | boolean didSomething = false; |
| 3842 | |
| 3843 | // See if the top visible activity is waiting to run in this process... |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3844 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 3845 | if (hr != null && normalMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3846 | if (hr.app == null && app.info.uid == hr.info.applicationInfo.uid |
| 3847 | && processName.equals(hr.processName)) { |
| 3848 | try { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3849 | if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3850 | didSomething = true; |
| 3851 | } |
| 3852 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3853 | Slog.w(TAG, "Exception in new application when starting activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3854 | + hr.intent.getComponent().flattenToShortString(), e); |
| 3855 | badApp = true; |
| 3856 | } |
| 3857 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3858 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3859 | } |
| 3860 | } |
| 3861 | |
| 3862 | // Find any services that should be running in this process... |
| 3863 | if (!badApp && mPendingServices.size() > 0) { |
| 3864 | ServiceRecord sr = null; |
| 3865 | try { |
| 3866 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3867 | sr = mPendingServices.get(i); |
| 3868 | if (app.info.uid != sr.appInfo.uid |
| 3869 | || !processName.equals(sr.processName)) { |
| 3870 | continue; |
| 3871 | } |
| 3872 | |
| 3873 | mPendingServices.remove(i); |
| 3874 | i--; |
| 3875 | realStartServiceLocked(sr, app); |
| 3876 | didSomething = true; |
| 3877 | } |
| 3878 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3879 | Slog.w(TAG, "Exception in new application when starting service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3880 | + sr.shortName, e); |
| 3881 | badApp = true; |
| 3882 | } |
| 3883 | } |
| 3884 | |
| 3885 | // Check if the next broadcast receiver is in this process... |
| 3886 | BroadcastRecord br = mPendingBroadcast; |
| 3887 | if (!badApp && br != null && br.curApp == app) { |
| 3888 | try { |
| 3889 | mPendingBroadcast = null; |
| 3890 | processCurBroadcastLocked(br, app); |
| 3891 | didSomething = true; |
| 3892 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3893 | Slog.w(TAG, "Exception in new application when starting receiver " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3894 | + br.curComponent.flattenToShortString(), e); |
| 3895 | badApp = true; |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 3896 | logBroadcastReceiverDiscardLocked(br); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3897 | finishReceiverLocked(br.receiver, br.resultCode, br.resultData, |
| 3898 | br.resultExtras, br.resultAbort, true); |
| 3899 | scheduleBroadcastsLocked(); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3900 | // We need to reset the state if we fails to start the receiver. |
| 3901 | br.state = BroadcastRecord.IDLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3902 | } |
| 3903 | } |
| 3904 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3905 | // Check whether the next backup agent is in this process... |
| 3906 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.info.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3907 | if (DEBUG_BACKUP) Slog.v(TAG, "New app is backup target, launching agent for " + app); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3908 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3909 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3910 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, |
| 3911 | compatibilityInfoForPackageLocked(mBackupTarget.appInfo), |
| 3912 | mBackupTarget.backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3913 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3914 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3915 | e.printStackTrace(); |
| 3916 | } |
| 3917 | } |
| 3918 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3919 | if (badApp) { |
| 3920 | // todo: Also need to kill application to deal with all |
| 3921 | // kinds of exceptions. |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3922 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3923 | return false; |
| 3924 | } |
| 3925 | |
| 3926 | if (!didSomething) { |
| 3927 | updateOomAdjLocked(); |
| 3928 | } |
| 3929 | |
| 3930 | return true; |
| 3931 | } |
| 3932 | |
| 3933 | public final void attachApplication(IApplicationThread thread) { |
| 3934 | synchronized (this) { |
| 3935 | int callingPid = Binder.getCallingPid(); |
| 3936 | final long origId = Binder.clearCallingIdentity(); |
| 3937 | attachApplicationLocked(thread, callingPid); |
| 3938 | Binder.restoreCallingIdentity(origId); |
| 3939 | } |
| 3940 | } |
| 3941 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3942 | public final void activityIdle(IBinder token, Configuration config, boolean stopProfiling) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3943 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3944 | ActivityRecord r = mMainStack.activityIdleInternal(token, false, config); |
| 3945 | if (stopProfiling) { |
| 3946 | synchronized (this) { |
| 3947 | if (mProfileProc == r.app) { |
| 3948 | if (mProfileFd != null) { |
| 3949 | try { |
| 3950 | mProfileFd.close(); |
| 3951 | } catch (IOException e) { |
| 3952 | } |
| 3953 | clearProfilerLocked(); |
| 3954 | } |
| 3955 | } |
| 3956 | } |
| 3957 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3958 | Binder.restoreCallingIdentity(origId); |
| 3959 | } |
| 3960 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3961 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3962 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3963 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3964 | mWindowManager.enableScreenAfterBoot(); |
| 3965 | } |
| 3966 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3967 | public void showBootMessage(final CharSequence msg, final boolean always) { |
| 3968 | mWindowManager.showBootMessage(msg, always); |
| 3969 | } |
| 3970 | |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 3971 | public void dismissKeyguardOnNextActivity() { |
| 3972 | synchronized (this) { |
| 3973 | mMainStack.dismissKeyguardOnNextActivityLocked(); |
| 3974 | } |
| 3975 | } |
| 3976 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3977 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3978 | IntentFilter pkgFilter = new IntentFilter(); |
| 3979 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 3980 | pkgFilter.addDataScheme("package"); |
| 3981 | mContext.registerReceiver(new BroadcastReceiver() { |
| 3982 | @Override |
| 3983 | public void onReceive(Context context, Intent intent) { |
| 3984 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 3985 | if (pkgs != null) { |
| 3986 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 3987 | synchronized (ActivityManagerService.this) { |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3988 | if (forceStopPackageLocked(pkg, -1, false, false, false, false)) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 3989 | setResultCode(Activity.RESULT_OK); |
| 3990 | return; |
| 3991 | } |
| 3992 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3993 | } |
| 3994 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3995 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3996 | }, pkgFilter); |
| 3997 | |
| 3998 | synchronized (this) { |
| 3999 | // Ensure that any processes we had put on hold are now started |
| 4000 | // up. |
| 4001 | final int NP = mProcessesOnHold.size(); |
| 4002 | if (NP > 0) { |
| 4003 | ArrayList<ProcessRecord> procs = |
| 4004 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 4005 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4006 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 4007 | + procs.get(ip)); |
| 4008 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4009 | } |
| 4010 | } |
| 4011 | |
| 4012 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4013 | // Start looking for apps that are abusing wake locks. |
| 4014 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 4015 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4016 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 4017 | SystemProperties.set("sys.boot_completed", "1"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4018 | broadcastIntentLocked(null, null, |
| 4019 | new Intent(Intent.ACTION_BOOT_COMPLETED, null), |
| 4020 | null, null, 0, null, null, |
| 4021 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
| 4022 | false, false, MY_PID, Process.SYSTEM_UID); |
| 4023 | } |
| 4024 | } |
| 4025 | } |
| 4026 | |
| 4027 | final void ensureBootCompleted() { |
| 4028 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4029 | boolean enableScreen; |
| 4030 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4031 | booting = mBooting; |
| 4032 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4033 | enableScreen = !mBooted; |
| 4034 | mBooted = true; |
| 4035 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4036 | |
| 4037 | if (booting) { |
| 4038 | finishBooting(); |
| 4039 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4040 | |
| 4041 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4042 | enableScreenAfterBoot(); |
| 4043 | } |
| 4044 | } |
| 4045 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4046 | public final void activityPaused(IBinder token) { |
| 4047 | final long origId = Binder.clearCallingIdentity(); |
| 4048 | mMainStack.activityPaused(token, false); |
| 4049 | Binder.restoreCallingIdentity(origId); |
| 4050 | } |
| 4051 | |
| 4052 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 4053 | CharSequence description) { |
| 4054 | if (localLOGV) Slog.v( |
| 4055 | TAG, "Activity stopped: token=" + token); |
| 4056 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4057 | // Refuse possible leaked file descriptors |
| 4058 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 4059 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 4060 | } |
| 4061 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4062 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4063 | |
| 4064 | final long origId = Binder.clearCallingIdentity(); |
| 4065 | |
| 4066 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4067 | r = mMainStack.isInStackLocked(token); |
| 4068 | if (r != null) { |
| 4069 | r.stack.activityStoppedLocked(r, icicle, thumbnail, description); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4070 | } |
| 4071 | } |
| 4072 | |
| 4073 | if (r != null) { |
| 4074 | sendPendingThumbnail(r, null, null, null, false); |
| 4075 | } |
| 4076 | |
| 4077 | trimApplications(); |
| 4078 | |
| 4079 | Binder.restoreCallingIdentity(origId); |
| 4080 | } |
| 4081 | |
| 4082 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4083 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4084 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4085 | } |
| 4086 | |
| 4087 | public String getCallingPackage(IBinder token) { |
| 4088 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4089 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 4090 | return r != null && r.app != null ? r.info.packageName : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4091 | } |
| 4092 | } |
| 4093 | |
| 4094 | public ComponentName getCallingActivity(IBinder token) { |
| 4095 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4096 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4097 | return r != null ? r.intent.getComponent() : null; |
| 4098 | } |
| 4099 | } |
| 4100 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4101 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4102 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4103 | if (r == null) { |
| 4104 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4105 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4106 | return r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | public ComponentName getActivityClassForToken(IBinder token) { |
| 4110 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4111 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4112 | if (r == null) { |
| 4113 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4114 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4115 | return r.intent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4116 | } |
| 4117 | } |
| 4118 | |
| 4119 | public String getPackageForToken(IBinder token) { |
| 4120 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4121 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4122 | if (r == null) { |
| 4123 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4124 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4125 | return r.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4126 | } |
| 4127 | } |
| 4128 | |
| 4129 | public IIntentSender getIntentSender(int type, |
| 4130 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4131 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4132 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4133 | if (intents != null) { |
| 4134 | if (intents.length < 1) { |
| 4135 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 4136 | } |
| 4137 | for (int i=0; i<intents.length; i++) { |
| 4138 | Intent intent = intents[i]; |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4139 | if (intent != null) { |
| 4140 | if (intent.hasFileDescriptors()) { |
| 4141 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 4142 | } |
| 4143 | if (type == INTENT_SENDER_BROADCAST && |
| 4144 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 4145 | throw new IllegalArgumentException( |
| 4146 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 4147 | } |
| 4148 | intents[i] = new Intent(intent); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4149 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4150 | } |
| 4151 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4152 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4153 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4154 | } |
| 4155 | } |
| 4156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4157 | synchronized(this) { |
| 4158 | int callingUid = Binder.getCallingUid(); |
| 4159 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4160 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4161 | int uid = AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4162 | .getPackageUid(packageName); |
| 4163 | if (uid != Binder.getCallingUid()) { |
| 4164 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 4165 | + Binder.getCallingPid() |
| 4166 | + ", uid=" + Binder.getCallingUid() |
| 4167 | + ", (need uid=" + uid + ")" |
| 4168 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4169 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4170 | throw new SecurityException(msg); |
| 4171 | } |
| 4172 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4173 | |
| 4174 | return getIntentSenderLocked(type, packageName, callingUid, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4175 | token, resultWho, requestCode, intents, resolvedTypes, flags); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4176 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4177 | } catch (RemoteException e) { |
| 4178 | throw new SecurityException(e); |
| 4179 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4180 | } |
| 4181 | } |
| 4182 | |
| 4183 | IIntentSender getIntentSenderLocked(int type, |
| 4184 | String packageName, int callingUid, IBinder token, String resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4185 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4186 | ActivityRecord activity = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4187 | if (type == INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4188 | activity = mMainStack.isInStackLocked(token); |
| 4189 | if (activity == null) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4190 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4191 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4192 | if (activity.finishing) { |
| 4193 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4194 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4195 | } |
| 4196 | |
| 4197 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 4198 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 4199 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 4200 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 4201 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 4202 | |
| 4203 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 4204 | type, packageName, activity, resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4205 | requestCode, intents, resolvedTypes, flags); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4206 | WeakReference<PendingIntentRecord> ref; |
| 4207 | ref = mIntentSenderRecords.get(key); |
| 4208 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 4209 | if (rec != null) { |
| 4210 | if (!cancelCurrent) { |
| 4211 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4212 | if (rec.key.requestIntent != null) { |
| 4213 | rec.key.requestIntent.replaceExtras(intents != null ? intents[0] : null); |
| 4214 | } |
| 4215 | if (intents != null) { |
| 4216 | intents[intents.length-1] = rec.key.requestIntent; |
| 4217 | rec.key.allIntents = intents; |
| 4218 | rec.key.allResolvedTypes = resolvedTypes; |
| 4219 | } else { |
| 4220 | rec.key.allIntents = null; |
| 4221 | rec.key.allResolvedTypes = null; |
| 4222 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4223 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4224 | return rec; |
| 4225 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4226 | rec.canceled = true; |
| 4227 | mIntentSenderRecords.remove(key); |
| 4228 | } |
| 4229 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4230 | return rec; |
| 4231 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4232 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4233 | mIntentSenderRecords.put(key, rec.ref); |
| 4234 | if (type == INTENT_SENDER_ACTIVITY_RESULT) { |
| 4235 | if (activity.pendingResults == null) { |
| 4236 | activity.pendingResults |
| 4237 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4238 | } |
| 4239 | activity.pendingResults.add(rec.ref); |
| 4240 | } |
| 4241 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | public void cancelIntentSender(IIntentSender sender) { |
| 4245 | if (!(sender instanceof PendingIntentRecord)) { |
| 4246 | return; |
| 4247 | } |
| 4248 | synchronized(this) { |
| 4249 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4250 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4251 | int uid = AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4252 | .getPackageUid(rec.key.packageName); |
| 4253 | if (uid != Binder.getCallingUid()) { |
| 4254 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4255 | + Binder.getCallingPid() |
| 4256 | + ", uid=" + Binder.getCallingUid() |
| 4257 | + " is not allowed to cancel packges " |
| 4258 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4259 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4260 | throw new SecurityException(msg); |
| 4261 | } |
| 4262 | } catch (RemoteException e) { |
| 4263 | throw new SecurityException(e); |
| 4264 | } |
| 4265 | cancelIntentSenderLocked(rec, true); |
| 4266 | } |
| 4267 | } |
| 4268 | |
| 4269 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4270 | rec.canceled = true; |
| 4271 | mIntentSenderRecords.remove(rec.key); |
| 4272 | if (cleanActivity && rec.key.activity != null) { |
| 4273 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4274 | } |
| 4275 | } |
| 4276 | |
| 4277 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4278 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4279 | return null; |
| 4280 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4281 | try { |
| 4282 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4283 | return res.key.packageName; |
| 4284 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4285 | } |
| 4286 | return null; |
| 4287 | } |
| 4288 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 4289 | public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) { |
| 4290 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4291 | return false; |
| 4292 | } |
| 4293 | try { |
| 4294 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4295 | if (res.key.allIntents == null) { |
| 4296 | return false; |
| 4297 | } |
| 4298 | for (int i=0; i<res.key.allIntents.length; i++) { |
| 4299 | Intent intent = res.key.allIntents[i]; |
| 4300 | if (intent.getPackage() != null && intent.getComponent() != null) { |
| 4301 | return false; |
| 4302 | } |
| 4303 | } |
| 4304 | return true; |
| 4305 | } catch (ClassCastException e) { |
| 4306 | } |
| 4307 | return false; |
| 4308 | } |
| 4309 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4310 | public void setProcessLimit(int max) { |
| 4311 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4312 | "setProcessLimit()"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4313 | synchronized (this) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4314 | mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4315 | mProcessLimitOverride = max; |
| 4316 | } |
| 4317 | trimApplications(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | public int getProcessLimit() { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4321 | synchronized (this) { |
| 4322 | return mProcessLimitOverride; |
| 4323 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4324 | } |
| 4325 | |
| 4326 | void foregroundTokenDied(ForegroundToken token) { |
| 4327 | synchronized (ActivityManagerService.this) { |
| 4328 | synchronized (mPidsSelfLocked) { |
| 4329 | ForegroundToken cur |
| 4330 | = mForegroundProcesses.get(token.pid); |
| 4331 | if (cur != token) { |
| 4332 | return; |
| 4333 | } |
| 4334 | mForegroundProcesses.remove(token.pid); |
| 4335 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4336 | if (pr == null) { |
| 4337 | return; |
| 4338 | } |
| 4339 | pr.forcingToForeground = null; |
| 4340 | pr.foregroundServices = false; |
| 4341 | } |
| 4342 | updateOomAdjLocked(); |
| 4343 | } |
| 4344 | } |
| 4345 | |
| 4346 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4347 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4348 | "setProcessForeground()"); |
| 4349 | synchronized(this) { |
| 4350 | boolean changed = false; |
| 4351 | |
| 4352 | synchronized (mPidsSelfLocked) { |
| 4353 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
| 4354 | if (pr == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4355 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4356 | return; |
| 4357 | } |
| 4358 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4359 | if (oldToken != null) { |
| 4360 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4361 | mForegroundProcesses.remove(pid); |
| 4362 | pr.forcingToForeground = null; |
| 4363 | changed = true; |
| 4364 | } |
| 4365 | if (isForeground && token != null) { |
| 4366 | ForegroundToken newToken = new ForegroundToken() { |
| 4367 | public void binderDied() { |
| 4368 | foregroundTokenDied(this); |
| 4369 | } |
| 4370 | }; |
| 4371 | newToken.pid = pid; |
| 4372 | newToken.token = token; |
| 4373 | try { |
| 4374 | token.linkToDeath(newToken, 0); |
| 4375 | mForegroundProcesses.put(pid, newToken); |
| 4376 | pr.forcingToForeground = token; |
| 4377 | changed = true; |
| 4378 | } catch (RemoteException e) { |
| 4379 | // If the process died while doing this, we will later |
| 4380 | // do the cleanup with the process death link. |
| 4381 | } |
| 4382 | } |
| 4383 | } |
| 4384 | |
| 4385 | if (changed) { |
| 4386 | updateOomAdjLocked(); |
| 4387 | } |
| 4388 | } |
| 4389 | } |
| 4390 | |
| 4391 | // ========================================================= |
| 4392 | // PERMISSIONS |
| 4393 | // ========================================================= |
| 4394 | |
| 4395 | static class PermissionController extends IPermissionController.Stub { |
| 4396 | ActivityManagerService mActivityManagerService; |
| 4397 | PermissionController(ActivityManagerService activityManagerService) { |
| 4398 | mActivityManagerService = activityManagerService; |
| 4399 | } |
| 4400 | |
| 4401 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4402 | return mActivityManagerService.checkPermission(permission, pid, |
| 4403 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4404 | } |
| 4405 | } |
| 4406 | |
| 4407 | /** |
| 4408 | * This can be called with or without the global lock held. |
| 4409 | */ |
| 4410 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4411 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4412 | // We might be performing an operation on behalf of an indirect binder |
| 4413 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4414 | // client identity accordingly before proceeding. |
| 4415 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4416 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4417 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4418 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4419 | uid = tlsIdentity.uid; |
| 4420 | pid = tlsIdentity.pid; |
| 4421 | } |
| 4422 | |
| 4423 | // Root, system server and our own process get to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4424 | if (uid == 0 || uid == Process.SYSTEM_UID || pid == MY_PID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4425 | return PackageManager.PERMISSION_GRANTED; |
| 4426 | } |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4427 | // If there is a uid that owns whatever is being accessed, it has |
| 4428 | // blanket access to it regardless of the permissions it requires. |
| 4429 | if (owningUid >= 0 && uid == owningUid) { |
| 4430 | return PackageManager.PERMISSION_GRANTED; |
| 4431 | } |
| 4432 | // If the target is not exported, then nobody else can get to it. |
| 4433 | if (!exported) { |
| 4434 | Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4435 | return PackageManager.PERMISSION_DENIED; |
| 4436 | } |
| 4437 | if (permission == null) { |
| 4438 | return PackageManager.PERMISSION_GRANTED; |
| 4439 | } |
| 4440 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4441 | return AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4442 | .checkUidPermission(permission, uid); |
| 4443 | } catch (RemoteException e) { |
| 4444 | // Should never happen, but if it does... deny! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4445 | Slog.e(TAG, "PackageManager is dead?!?", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4446 | } |
| 4447 | return PackageManager.PERMISSION_DENIED; |
| 4448 | } |
| 4449 | |
| 4450 | /** |
| 4451 | * As the only public entry point for permissions checking, this method |
| 4452 | * can enforce the semantic that requesting a check on a null global |
| 4453 | * permission is automatically denied. (Internally a null permission |
| 4454 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4455 | * when only uid-based security is needed.) |
| 4456 | * |
| 4457 | * This can be called with or without the global lock held. |
| 4458 | */ |
| 4459 | public int checkPermission(String permission, int pid, int uid) { |
| 4460 | if (permission == null) { |
| 4461 | return PackageManager.PERMISSION_DENIED; |
| 4462 | } |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4463 | return checkComponentPermission(permission, pid, uid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | /** |
| 4467 | * Binder IPC calls go through the public entry point. |
| 4468 | * This can be called with or without the global lock held. |
| 4469 | */ |
| 4470 | int checkCallingPermission(String permission) { |
| 4471 | return checkPermission(permission, |
| 4472 | Binder.getCallingPid(), |
| 4473 | Binder.getCallingUid()); |
| 4474 | } |
| 4475 | |
| 4476 | /** |
| 4477 | * This can be called with or without the global lock held. |
| 4478 | */ |
| 4479 | void enforceCallingPermission(String permission, String func) { |
| 4480 | if (checkCallingPermission(permission) |
| 4481 | == PackageManager.PERMISSION_GRANTED) { |
| 4482 | return; |
| 4483 | } |
| 4484 | |
| 4485 | String msg = "Permission Denial: " + func + " from pid=" |
| 4486 | + Binder.getCallingPid() |
| 4487 | + ", uid=" + Binder.getCallingUid() |
| 4488 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4489 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4490 | throw new SecurityException(msg); |
| 4491 | } |
| 4492 | |
| 4493 | private final boolean checkHoldingPermissionsLocked(IPackageManager pm, |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4494 | ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
| 4495 | boolean readPerm = (modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 4496 | boolean writePerm = (modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0; |
| 4497 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4498 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4499 | try { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4500 | // Is the component private from the target uid? |
| 4501 | final boolean prv = !pi.exported && pi.applicationInfo.uid != uid; |
| 4502 | |
| 4503 | // Acceptable if the there is no read permission needed from the |
| 4504 | // target or the target is holding the read permission. |
| 4505 | if (!readPerm) { |
| 4506 | if ((!prv && pi.readPermission == null) || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4507 | (pm.checkUidPermission(pi.readPermission, uid) |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4508 | == PackageManager.PERMISSION_GRANTED)) { |
| 4509 | readPerm = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4510 | } |
| 4511 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4512 | |
| 4513 | // Acceptable if the there is no write permission needed from the |
| 4514 | // target or the target is holding the read permission. |
| 4515 | if (!writePerm) { |
| 4516 | if (!prv && (pi.writePermission == null) || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4517 | (pm.checkUidPermission(pi.writePermission, uid) |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4518 | == PackageManager.PERMISSION_GRANTED)) { |
| 4519 | writePerm = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4520 | } |
| 4521 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4522 | |
| 4523 | // Acceptable if there is a path permission matching the URI that |
| 4524 | // the target holds the permission on. |
| 4525 | PathPermission[] pps = pi.pathPermissions; |
| 4526 | if (pps != null && (!readPerm || !writePerm)) { |
| 4527 | final String path = uri.getPath(); |
| 4528 | int i = pps.length; |
| 4529 | while (i > 0 && (!readPerm || !writePerm)) { |
| 4530 | i--; |
| 4531 | PathPermission pp = pps[i]; |
| 4532 | if (!readPerm) { |
| 4533 | final String pprperm = pp.getReadPermission(); |
| 4534 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 4535 | + pprperm + " for " + pp.getPath() |
| 4536 | + ": match=" + pp.match(path) |
| 4537 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 4538 | if (pprperm != null && pp.match(path) && |
| 4539 | (pm.checkUidPermission(pprperm, uid) |
| 4540 | == PackageManager.PERMISSION_GRANTED)) { |
| 4541 | readPerm = true; |
| 4542 | } |
| 4543 | } |
| 4544 | if (!writePerm) { |
| 4545 | final String ppwperm = pp.getWritePermission(); |
| 4546 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 4547 | + ppwperm + " for " + pp.getPath() |
| 4548 | + ": match=" + pp.match(path) |
| 4549 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 4550 | if (ppwperm != null && pp.match(path) && |
| 4551 | (pm.checkUidPermission(ppwperm, uid) |
| 4552 | == PackageManager.PERMISSION_GRANTED)) { |
| 4553 | writePerm = true; |
| 4554 | } |
| 4555 | } |
| 4556 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4557 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4558 | } catch (RemoteException e) { |
| 4559 | return false; |
| 4560 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4561 | |
| 4562 | return readPerm && writePerm; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 4566 | int modeFlags) { |
| 4567 | // Root gets to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4568 | if (uid == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4569 | return true; |
| 4570 | } |
| 4571 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 4572 | if (perms == null) return false; |
| 4573 | UriPermission perm = perms.get(uri); |
| 4574 | if (perm == null) return false; |
| 4575 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 4576 | } |
| 4577 | |
| 4578 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
| 4579 | // Another redirected-binder-call permissions check as in |
| 4580 | // {@link checkComponentPermission}. |
| 4581 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4582 | if (tlsIdentity != null) { |
| 4583 | uid = tlsIdentity.uid; |
| 4584 | pid = tlsIdentity.pid; |
| 4585 | } |
| 4586 | |
| 4587 | // Our own process gets to do everything. |
| 4588 | if (pid == MY_PID) { |
| 4589 | return PackageManager.PERMISSION_GRANTED; |
| 4590 | } |
| 4591 | synchronized(this) { |
| 4592 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 4593 | ? PackageManager.PERMISSION_GRANTED |
| 4594 | : PackageManager.PERMISSION_DENIED; |
| 4595 | } |
| 4596 | } |
| 4597 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4598 | /** |
| 4599 | * Check if the targetPkg can be granted permission to access uri by |
| 4600 | * the callingUid using the given modeFlags. Throws a security exception |
| 4601 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 4602 | * if the URI permission grant should be performed; returns -1 if it is not |
| 4603 | * needed (for example targetPkg already has permission to access the URI). |
| 4604 | */ |
| 4605 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
| 4606 | Uri uri, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4607 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4608 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4609 | if (modeFlags == 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4610 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4611 | } |
| 4612 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4613 | if (targetPkg != null) { |
| 4614 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4615 | "Checking grant " + targetPkg + " permission to " + uri); |
| 4616 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4617 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4618 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4619 | |
| 4620 | // If this is not a content: uri, we can't do anything with it. |
| 4621 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4622 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4623 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4624 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4625 | } |
| 4626 | |
| 4627 | String name = uri.getAuthority(); |
| 4628 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4629 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4630 | if (cpr != null) { |
| 4631 | pi = cpr.info; |
| 4632 | } else { |
| 4633 | try { |
| 4634 | pi = pm.resolveContentProvider(name, |
| 4635 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4636 | } catch (RemoteException ex) { |
| 4637 | } |
| 4638 | } |
| 4639 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 4640 | Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4641 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | int targetUid; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4645 | if (targetPkg != null) { |
| 4646 | try { |
| 4647 | targetUid = pm.getPackageUid(targetPkg); |
| 4648 | if (targetUid < 0) { |
| 4649 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4650 | "Can't grant URI permission no uid for: " + targetPkg); |
| 4651 | return -1; |
| 4652 | } |
| 4653 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4654 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4655 | } |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4656 | } else { |
| 4657 | targetUid = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4658 | } |
| 4659 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4660 | if (targetUid >= 0) { |
| 4661 | // First... does the target actually need this permission? |
| 4662 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 4663 | // No need to grant the target this permission. |
| 4664 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4665 | "Target " + targetPkg + " already has full permission to " + uri); |
| 4666 | return -1; |
| 4667 | } |
| 4668 | } else { |
| 4669 | // First... there is no target package, so can anyone access it? |
| 4670 | boolean allowed = pi.exported; |
| 4671 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4672 | if (pi.readPermission != null) { |
| 4673 | allowed = false; |
| 4674 | } |
| 4675 | } |
| 4676 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4677 | if (pi.writePermission != null) { |
| 4678 | allowed = false; |
| 4679 | } |
| 4680 | } |
| 4681 | if (allowed) { |
| 4682 | return -1; |
| 4683 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4684 | } |
| 4685 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4686 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4687 | if (!pi.grantUriPermissions) { |
| 4688 | throw new SecurityException("Provider " + pi.packageName |
| 4689 | + "/" + pi.name |
| 4690 | + " does not allow granting of Uri permissions (uri " |
| 4691 | + uri + ")"); |
| 4692 | } |
| 4693 | if (pi.uriPermissionPatterns != null) { |
| 4694 | final int N = pi.uriPermissionPatterns.length; |
| 4695 | boolean allowed = false; |
| 4696 | for (int i=0; i<N; i++) { |
| 4697 | if (pi.uriPermissionPatterns[i] != null |
| 4698 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 4699 | allowed = true; |
| 4700 | break; |
| 4701 | } |
| 4702 | } |
| 4703 | if (!allowed) { |
| 4704 | throw new SecurityException("Provider " + pi.packageName |
| 4705 | + "/" + pi.name |
| 4706 | + " does not allow granting of permission to path of Uri " |
| 4707 | + uri); |
| 4708 | } |
| 4709 | } |
| 4710 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4711 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4712 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4713 | if (callingUid != Process.myUid()) { |
| 4714 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 4715 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4716 | throw new SecurityException("Uid " + callingUid |
| 4717 | + " does not have permission to uri " + uri); |
| 4718 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4719 | } |
| 4720 | } |
| 4721 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4722 | return targetUid; |
| 4723 | } |
| 4724 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4725 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 4726 | Uri uri, int modeFlags) { |
| 4727 | synchronized(this) { |
| 4728 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags); |
| 4729 | } |
| 4730 | } |
| 4731 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4732 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 4733 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 4734 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4735 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4736 | if (modeFlags == 0) { |
| 4737 | return; |
| 4738 | } |
| 4739 | |
| 4740 | // So here we are: the caller has the assumed permission |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4741 | // to the uri, and the target doesn't. Let's now give this to |
| 4742 | // the target. |
| 4743 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4744 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4745 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4746 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4747 | HashMap<Uri, UriPermission> targetUris |
| 4748 | = mGrantedUriPermissions.get(targetUid); |
| 4749 | if (targetUris == null) { |
| 4750 | targetUris = new HashMap<Uri, UriPermission>(); |
| 4751 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 4752 | } |
| 4753 | |
| 4754 | UriPermission perm = targetUris.get(uri); |
| 4755 | if (perm == null) { |
| 4756 | perm = new UriPermission(targetUid, uri); |
| 4757 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4758 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4759 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4760 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4761 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4762 | perm.globalModeFlags |= modeFlags; |
Vairavan Srinivasan | 91c12c2 | 2011-01-21 18:26:06 -0800 | [diff] [blame] | 4763 | } else { |
| 4764 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4765 | perm.readOwners.add(owner); |
| 4766 | owner.addReadPermission(perm); |
| 4767 | } |
| 4768 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4769 | perm.writeOwners.add(owner); |
| 4770 | owner.addWritePermission(perm); |
| 4771 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4772 | } |
| 4773 | } |
| 4774 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4775 | void grantUriPermissionLocked(int callingUid, |
| 4776 | String targetPkg, Uri uri, int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4777 | if (targetPkg == null) { |
| 4778 | throw new NullPointerException("targetPkg"); |
| 4779 | } |
| 4780 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4781 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags); |
| 4782 | if (targetUid < 0) { |
| 4783 | return; |
| 4784 | } |
| 4785 | |
| 4786 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 4787 | } |
| 4788 | |
| 4789 | /** |
| 4790 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 4791 | */ |
| 4792 | int checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 4793 | String targetPkg, Intent intent) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4794 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4795 | "Checking URI perm to " + (intent != null ? intent.getData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4796 | + " from " + intent + "; flags=0x" |
| 4797 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 4798 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4799 | if (targetPkg == null) { |
| 4800 | throw new NullPointerException("targetPkg"); |
| 4801 | } |
| 4802 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4803 | if (intent == null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4804 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4805 | } |
| 4806 | Uri data = intent.getData(); |
| 4807 | if (data == null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4808 | return -1; |
| 4809 | } |
| 4810 | return checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 4811 | intent.getFlags()); |
| 4812 | } |
| 4813 | |
| 4814 | /** |
| 4815 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 4816 | */ |
| 4817 | void grantUriPermissionUncheckedFromIntentLocked(int targetUid, |
| 4818 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
| 4819 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, intent.getData(), |
| 4820 | intent.getFlags(), owner); |
| 4821 | } |
| 4822 | |
| 4823 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 4824 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
| 4825 | int targetUid = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, intent); |
| 4826 | if (targetUid < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4827 | return; |
| 4828 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4829 | |
| 4830 | grantUriPermissionUncheckedFromIntentLocked(targetUid, targetPkg, intent, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4831 | } |
| 4832 | |
| 4833 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 4834 | Uri uri, int modeFlags) { |
| 4835 | synchronized(this) { |
| 4836 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 4837 | if (r == null) { |
| 4838 | throw new SecurityException("Unable to find app for caller " |
| 4839 | + caller |
| 4840 | + " when granting permission to uri " + uri); |
| 4841 | } |
| 4842 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4843 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4844 | } |
| 4845 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4846 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | grantUriPermissionLocked(r.info.uid, targetPkg, uri, modeFlags, |
| 4850 | null); |
| 4851 | } |
| 4852 | } |
| 4853 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4854 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4855 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4856 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 4857 | HashMap<Uri, UriPermission> perms |
| 4858 | = mGrantedUriPermissions.get(perm.uid); |
| 4859 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4860 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4861 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4862 | perms.remove(perm.uri); |
| 4863 | if (perms.size() == 0) { |
| 4864 | mGrantedUriPermissions.remove(perm.uid); |
| 4865 | } |
| 4866 | } |
| 4867 | } |
| 4868 | } |
| 4869 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4870 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 4871 | int modeFlags) { |
| 4872 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4873 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4874 | if (modeFlags == 0) { |
| 4875 | return; |
| 4876 | } |
| 4877 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4878 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4879 | "Revoking all granted permissions to " + uri); |
| 4880 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4881 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4882 | |
| 4883 | final String authority = uri.getAuthority(); |
| 4884 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4885 | ContentProviderRecord cpr = mProvidersByName.get(authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4886 | if (cpr != null) { |
| 4887 | pi = cpr.info; |
| 4888 | } else { |
| 4889 | try { |
| 4890 | pi = pm.resolveContentProvider(authority, |
| 4891 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4892 | } catch (RemoteException ex) { |
| 4893 | } |
| 4894 | } |
| 4895 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 4896 | Slog.w(TAG, "No content provider found for permission revoke: " + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4897 | return; |
| 4898 | } |
| 4899 | |
| 4900 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4901 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4902 | // Right now, if you are not the original owner of the permission, |
| 4903 | // you are not allowed to revoke it. |
| 4904 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4905 | throw new SecurityException("Uid " + callingUid |
| 4906 | + " does not have permission to uri " + uri); |
| 4907 | //} |
| 4908 | } |
| 4909 | |
| 4910 | // Go through all of the permissions and remove any that match. |
| 4911 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 4912 | if (SEGMENTS != null) { |
| 4913 | final int NS = SEGMENTS.size(); |
| 4914 | int N = mGrantedUriPermissions.size(); |
| 4915 | for (int i=0; i<N; i++) { |
| 4916 | HashMap<Uri, UriPermission> perms |
| 4917 | = mGrantedUriPermissions.valueAt(i); |
| 4918 | Iterator<UriPermission> it = perms.values().iterator(); |
| 4919 | toploop: |
| 4920 | while (it.hasNext()) { |
| 4921 | UriPermission perm = it.next(); |
| 4922 | Uri targetUri = perm.uri; |
| 4923 | if (!authority.equals(targetUri.getAuthority())) { |
| 4924 | continue; |
| 4925 | } |
| 4926 | List<String> targetSegments = targetUri.getPathSegments(); |
| 4927 | if (targetSegments == null) { |
| 4928 | continue; |
| 4929 | } |
| 4930 | if (targetSegments.size() < NS) { |
| 4931 | continue; |
| 4932 | } |
| 4933 | for (int j=0; j<NS; j++) { |
| 4934 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 4935 | continue toploop; |
| 4936 | } |
| 4937 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4938 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4939 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4940 | perm.clearModes(modeFlags); |
| 4941 | if (perm.modeFlags == 0) { |
| 4942 | it.remove(); |
| 4943 | } |
| 4944 | } |
| 4945 | if (perms.size() == 0) { |
| 4946 | mGrantedUriPermissions.remove( |
| 4947 | mGrantedUriPermissions.keyAt(i)); |
| 4948 | N--; |
| 4949 | i--; |
| 4950 | } |
| 4951 | } |
| 4952 | } |
| 4953 | } |
| 4954 | |
| 4955 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 4956 | int modeFlags) { |
| 4957 | synchronized(this) { |
| 4958 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 4959 | if (r == null) { |
| 4960 | throw new SecurityException("Unable to find app for caller " |
| 4961 | + caller |
| 4962 | + " when revoking permission to uri " + uri); |
| 4963 | } |
| 4964 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4965 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4966 | return; |
| 4967 | } |
| 4968 | |
| 4969 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4970 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4971 | if (modeFlags == 0) { |
| 4972 | return; |
| 4973 | } |
| 4974 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4975 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4976 | |
| 4977 | final String authority = uri.getAuthority(); |
| 4978 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4979 | ContentProviderRecord cpr = mProvidersByName.get(authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4980 | if (cpr != null) { |
| 4981 | pi = cpr.info; |
| 4982 | } else { |
| 4983 | try { |
| 4984 | pi = pm.resolveContentProvider(authority, |
| 4985 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4986 | } catch (RemoteException ex) { |
| 4987 | } |
| 4988 | } |
| 4989 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 4990 | Slog.w(TAG, "No content provider found for permission revoke: " |
| 4991 | + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4992 | return; |
| 4993 | } |
| 4994 | |
| 4995 | revokeUriPermissionLocked(r.info.uid, uri, modeFlags); |
| 4996 | } |
| 4997 | } |
| 4998 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4999 | @Override |
| 5000 | public IBinder newUriPermissionOwner(String name) { |
| 5001 | synchronized(this) { |
| 5002 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 5003 | return owner.getExternalTokenLocked(); |
| 5004 | } |
| 5005 | } |
| 5006 | |
| 5007 | @Override |
| 5008 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 5009 | Uri uri, int modeFlags) { |
| 5010 | synchronized(this) { |
| 5011 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5012 | if (owner == null) { |
| 5013 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5014 | } |
| 5015 | if (fromUid != Binder.getCallingUid()) { |
| 5016 | if (Binder.getCallingUid() != Process.myUid()) { |
| 5017 | // Only system code can grant URI permissions on behalf |
| 5018 | // of other users. |
| 5019 | throw new SecurityException("nice try"); |
| 5020 | } |
| 5021 | } |
| 5022 | if (targetPkg == null) { |
| 5023 | throw new IllegalArgumentException("null target"); |
| 5024 | } |
| 5025 | if (uri == null) { |
| 5026 | throw new IllegalArgumentException("null uri"); |
| 5027 | } |
| 5028 | |
| 5029 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 5030 | } |
| 5031 | } |
| 5032 | |
| 5033 | @Override |
| 5034 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 5035 | synchronized(this) { |
| 5036 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5037 | if (owner == null) { |
| 5038 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5039 | } |
| 5040 | |
| 5041 | if (uri == null) { |
| 5042 | owner.removeUriPermissionsLocked(mode); |
| 5043 | } else { |
| 5044 | owner.removeUriPermissionLocked(uri, mode); |
| 5045 | } |
| 5046 | } |
| 5047 | } |
| 5048 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5049 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 5050 | synchronized (this) { |
| 5051 | ProcessRecord app = |
| 5052 | who != null ? getRecordForAppLocked(who) : null; |
| 5053 | if (app == null) return; |
| 5054 | |
| 5055 | Message msg = Message.obtain(); |
| 5056 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 5057 | msg.obj = app; |
| 5058 | msg.arg1 = waiting ? 1 : 0; |
| 5059 | mHandler.sendMessage(msg); |
| 5060 | } |
| 5061 | } |
| 5062 | |
| 5063 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5064 | final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ); |
| 5065 | final long hiddenAppMem = mProcessList.getMemLevel(ProcessList.HIDDEN_APP_MIN_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5066 | outInfo.availMem = Process.getFreeMemory(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5067 | outInfo.threshold = homeAppMem; |
| 5068 | outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2)); |
| 5069 | outInfo.hiddenAppThreshold = hiddenAppMem; |
| 5070 | outInfo.secondaryServerThreshold = mProcessList.getMemLevel( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 5071 | ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5072 | outInfo.visibleAppThreshold = mProcessList.getMemLevel( |
| 5073 | ProcessList.VISIBLE_APP_ADJ); |
| 5074 | outInfo.foregroundAppThreshold = mProcessList.getMemLevel( |
| 5075 | ProcessList.FOREGROUND_APP_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | // ========================================================= |
| 5079 | // TASK MANAGEMENT |
| 5080 | // ========================================================= |
| 5081 | |
| 5082 | public List getTasks(int maxNum, int flags, |
| 5083 | IThumbnailReceiver receiver) { |
| 5084 | ArrayList list = new ArrayList(); |
| 5085 | |
| 5086 | PendingThumbnailsRecord pending = null; |
| 5087 | IApplicationThread topThumbnail = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5088 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5089 | |
| 5090 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5091 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5092 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 5093 | + ", receiver=" + receiver); |
| 5094 | |
| 5095 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 5096 | != PackageManager.PERMISSION_GRANTED) { |
| 5097 | if (receiver != null) { |
| 5098 | // If the caller wants to wait for pending thumbnails, |
| 5099 | // it ain't gonna get them. |
| 5100 | try { |
| 5101 | receiver.finished(); |
| 5102 | } catch (RemoteException ex) { |
| 5103 | } |
| 5104 | } |
| 5105 | String msg = "Permission Denial: getTasks() from pid=" |
| 5106 | + Binder.getCallingPid() |
| 5107 | + ", uid=" + Binder.getCallingUid() |
| 5108 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5109 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5110 | throw new SecurityException(msg); |
| 5111 | } |
| 5112 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5113 | int pos = mMainStack.mHistory.size()-1; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5114 | ActivityRecord next = |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5115 | pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5116 | ActivityRecord top = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5117 | TaskRecord curTask = null; |
| 5118 | int numActivities = 0; |
| 5119 | int numRunning = 0; |
| 5120 | while (pos >= 0 && maxNum > 0) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5121 | final ActivityRecord r = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5122 | pos--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5123 | next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5124 | |
| 5125 | // Initialize state for next task if needed. |
| 5126 | if (top == null || |
| 5127 | (top.state == ActivityState.INITIALIZING |
| 5128 | && top.task == r.task)) { |
| 5129 | top = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5130 | curTask = r.task; |
| 5131 | numActivities = numRunning = 0; |
| 5132 | } |
| 5133 | |
| 5134 | // Add 'r' into the current task. |
| 5135 | numActivities++; |
| 5136 | if (r.app != null && r.app.thread != null) { |
| 5137 | numRunning++; |
| 5138 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5139 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5140 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5141 | TAG, r.intent.getComponent().flattenToShortString() |
| 5142 | + ": task=" + r.task); |
| 5143 | |
| 5144 | // If the next one is a different task, generate a new |
| 5145 | // TaskInfo entry for what we have. |
| 5146 | if (next == null || next.task != curTask) { |
| 5147 | ActivityManager.RunningTaskInfo ci |
| 5148 | = new ActivityManager.RunningTaskInfo(); |
| 5149 | ci.id = curTask.taskId; |
| 5150 | ci.baseActivity = r.intent.getComponent(); |
| 5151 | ci.topActivity = top.intent.getComponent(); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5152 | if (top.thumbHolder != null) { |
| 5153 | ci.description = top.thumbHolder.lastDescription; |
| 5154 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5155 | ci.numActivities = numActivities; |
| 5156 | ci.numRunning = numRunning; |
| 5157 | //System.out.println( |
| 5158 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 5159 | if (ci.thumbnail == null && receiver != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5160 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5161 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 5162 | + " app=" + top.app |
| 5163 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 5164 | if (top.state == ActivityState.RESUMED |
| 5165 | || top.state == ActivityState.PAUSING) { |
| 5166 | if (top.idle && top.app != null |
| 5167 | && top.app.thread != null) { |
| 5168 | topRecord = top; |
| 5169 | topThumbnail = top.app.thread; |
| 5170 | } else { |
| 5171 | top.thumbnailNeeded = true; |
| 5172 | } |
| 5173 | } |
| 5174 | if (pending == null) { |
| 5175 | pending = new PendingThumbnailsRecord(receiver); |
| 5176 | } |
| 5177 | pending.pendingRecords.add(top); |
| 5178 | } |
| 5179 | list.add(ci); |
| 5180 | maxNum--; |
| 5181 | top = null; |
| 5182 | } |
| 5183 | } |
| 5184 | |
| 5185 | if (pending != null) { |
| 5186 | mPendingThumbnails.add(pending); |
| 5187 | } |
| 5188 | } |
| 5189 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5190 | if (localLOGV) Slog.v(TAG, "We have pending thumbnails: " + pending); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5191 | |
| 5192 | if (topThumbnail != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5193 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5194 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5195 | topThumbnail.requestThumbnail(topRecord.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5196 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5197 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5198 | sendPendingThumbnail(null, topRecord.appToken, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5199 | } |
| 5200 | } |
| 5201 | |
| 5202 | if (pending == null && receiver != null) { |
| 5203 | // In this case all thumbnails were available and the client |
| 5204 | // is being asked to be told when the remaining ones come in... |
| 5205 | // which is unusually, since the top-most currently running |
| 5206 | // activity should never have a canned thumbnail! Oh well. |
| 5207 | try { |
| 5208 | receiver.finished(); |
| 5209 | } catch (RemoteException ex) { |
| 5210 | } |
| 5211 | } |
| 5212 | |
| 5213 | return list; |
| 5214 | } |
| 5215 | |
| 5216 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
| 5217 | int flags) { |
| 5218 | synchronized (this) { |
| 5219 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 5220 | "getRecentTasks()"); |
| 5221 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5222 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5223 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5224 | final int N = mRecentTasks.size(); |
| 5225 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 5226 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 5227 | maxNum < N ? maxNum : N); |
| 5228 | for (int i=0; i<N && maxNum > 0; i++) { |
| 5229 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5230 | // Return the entry if desired by the caller. We always return |
| 5231 | // the first entry, because callers always expect this to be the |
| 5232 | // forground app. We may filter others if the caller has |
| 5233 | // not supplied RECENT_WITH_EXCLUDED and there is some reason |
| 5234 | // we should exclude the entry. |
| 5235 | if (i == 0 |
| 5236 | || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5237 | || (tr.intent == null) |
| 5238 | || ((tr.intent.getFlags() |
| 5239 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 5240 | ActivityManager.RecentTaskInfo rti |
| 5241 | = new ActivityManager.RecentTaskInfo(); |
| 5242 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5243 | rti.persistentId = tr.taskId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5244 | rti.baseIntent = new Intent( |
| 5245 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5246 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 5247 | rti.description = tr.lastDescription; |
| 5248 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5249 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5250 | // Check whether this activity is currently available. |
| 5251 | try { |
| 5252 | if (rti.origActivity != null) { |
| 5253 | if (pm.getActivityInfo(rti.origActivity, 0) == null) { |
| 5254 | continue; |
| 5255 | } |
| 5256 | } else if (rti.baseIntent != null) { |
| 5257 | if (pm.queryIntentActivities(rti.baseIntent, |
| 5258 | null, 0) == null) { |
| 5259 | continue; |
| 5260 | } |
| 5261 | } |
| 5262 | } catch (RemoteException e) { |
| 5263 | // Will never happen. |
| 5264 | } |
| 5265 | } |
| 5266 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5267 | res.add(rti); |
| 5268 | maxNum--; |
| 5269 | } |
| 5270 | } |
| 5271 | return res; |
| 5272 | } |
| 5273 | } |
| 5274 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5275 | private TaskRecord taskForIdLocked(int id) { |
| 5276 | final int N = mRecentTasks.size(); |
| 5277 | for (int i=0; i<N; i++) { |
| 5278 | TaskRecord tr = mRecentTasks.get(i); |
| 5279 | if (tr.taskId == id) { |
| 5280 | return tr; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5281 | } |
| 5282 | } |
| 5283 | return null; |
| 5284 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5285 | |
| 5286 | public ActivityManager.TaskThumbnails getTaskThumbnails(int id) { |
| 5287 | synchronized (this) { |
| 5288 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5289 | "getTaskThumbnails()"); |
| 5290 | TaskRecord tr = taskForIdLocked(id); |
| 5291 | if (tr != null) { |
| 5292 | return mMainStack.getTaskThumbnailsLocked(tr); |
| 5293 | } |
| 5294 | } |
| 5295 | return null; |
| 5296 | } |
| 5297 | |
| 5298 | public boolean removeSubTask(int taskId, int subTaskIndex) { |
| 5299 | synchronized (this) { |
| 5300 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5301 | "removeSubTask()"); |
| 5302 | long ident = Binder.clearCallingIdentity(); |
| 5303 | try { |
| 5304 | return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex) != null; |
| 5305 | } finally { |
| 5306 | Binder.restoreCallingIdentity(ident); |
| 5307 | } |
| 5308 | } |
| 5309 | } |
| 5310 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5311 | private void cleanUpRemovedTaskLocked(ActivityRecord root, boolean killProcesses) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5312 | TaskRecord tr = root.task; |
| 5313 | Intent baseIntent = new Intent( |
| 5314 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5315 | ComponentName component = baseIntent.getComponent(); |
| 5316 | if (component == null) { |
| 5317 | Slog.w(TAG, "Now component for base intent of task: " + tr); |
| 5318 | return; |
| 5319 | } |
| 5320 | |
| 5321 | // Find any running services associated with this app. |
| 5322 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
| 5323 | for (ServiceRecord sr : mServices.values()) { |
| 5324 | if (sr.packageName.equals(component.getPackageName())) { |
| 5325 | services.add(sr); |
| 5326 | } |
| 5327 | } |
| 5328 | |
| 5329 | // Take care of any running services associated with the app. |
| 5330 | for (int i=0; i<services.size(); i++) { |
| 5331 | ServiceRecord sr = services.get(i); |
| 5332 | if (sr.startRequested) { |
| 5333 | if ((sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0) { |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5334 | Slog.i(TAG, "Stopping service " + sr.shortName + ": remove task"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5335 | stopServiceLocked(sr); |
| 5336 | } else { |
| 5337 | sr.pendingStarts.add(new ServiceRecord.StartItem(sr, true, |
| 5338 | sr.makeNextStartId(), baseIntent, -1)); |
| 5339 | if (sr.app != null && sr.app.thread != null) { |
| 5340 | sendServiceArgsLocked(sr, false); |
| 5341 | } |
| 5342 | } |
| 5343 | } |
| 5344 | } |
| 5345 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5346 | if (killProcesses) { |
| 5347 | // Find any running processes associated with this app. |
| 5348 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 5349 | SparseArray<ProcessRecord> appProcs |
| 5350 | = mProcessNames.getMap().get(component.getPackageName()); |
| 5351 | if (appProcs != null) { |
| 5352 | for (int i=0; i<appProcs.size(); i++) { |
| 5353 | procs.add(appProcs.valueAt(i)); |
| 5354 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5355 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5356 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5357 | // Kill the running processes. |
| 5358 | for (int i=0; i<procs.size(); i++) { |
| 5359 | ProcessRecord pr = procs.get(i); |
| 5360 | if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 5361 | Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task"); |
| 5362 | EventLog.writeEvent(EventLogTags.AM_KILL, pr.pid, |
| 5363 | pr.processName, pr.setAdj, "remove task"); |
| 5364 | Process.killProcessQuiet(pr.pid); |
| 5365 | } else { |
| 5366 | pr.waitingToKill = "remove task"; |
| 5367 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5368 | } |
| 5369 | } |
| 5370 | } |
| 5371 | |
| 5372 | public boolean removeTask(int taskId, int flags) { |
| 5373 | synchronized (this) { |
| 5374 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5375 | "removeTask()"); |
| 5376 | long ident = Binder.clearCallingIdentity(); |
| 5377 | try { |
| 5378 | ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1); |
| 5379 | if (r != null) { |
| 5380 | mRecentTasks.remove(r.task); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5381 | cleanUpRemovedTaskLocked(r, |
| 5382 | (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5383 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5384 | } else { |
| 5385 | TaskRecord tr = null; |
| 5386 | int i=0; |
| 5387 | while (i < mRecentTasks.size()) { |
| 5388 | TaskRecord t = mRecentTasks.get(i); |
| 5389 | if (t.taskId == taskId) { |
| 5390 | tr = t; |
| 5391 | break; |
| 5392 | } |
| 5393 | i++; |
| 5394 | } |
| 5395 | if (tr != null) { |
| 5396 | if (tr.numActivities <= 0) { |
| 5397 | // Caller is just removing a recent task that is |
| 5398 | // not actively running. That is easy! |
| 5399 | mRecentTasks.remove(i); |
| 5400 | } else { |
| 5401 | Slog.w(TAG, "removeTask: task " + taskId |
| 5402 | + " does not have activities to remove, " |
| 5403 | + " but numActivities=" + tr.numActivities |
| 5404 | + ": " + tr); |
| 5405 | } |
| 5406 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5407 | } |
| 5408 | } finally { |
| 5409 | Binder.restoreCallingIdentity(ident); |
| 5410 | } |
| 5411 | } |
| 5412 | return false; |
| 5413 | } |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5414 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5415 | private final int findAffinityTaskTopLocked(int startIndex, String affinity) { |
| 5416 | int j; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5417 | TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5418 | TaskRecord jt = startTask; |
| 5419 | |
| 5420 | // First look backwards |
| 5421 | for (j=startIndex-1; j>=0; j--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5422 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5423 | if (r.task != jt) { |
| 5424 | jt = r.task; |
| 5425 | if (affinity.equals(jt.affinity)) { |
| 5426 | return j; |
| 5427 | } |
| 5428 | } |
| 5429 | } |
| 5430 | |
| 5431 | // Now look forwards |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5432 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5433 | jt = startTask; |
| 5434 | for (j=startIndex+1; j<N; j++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5435 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5436 | if (r.task != jt) { |
| 5437 | if (affinity.equals(jt.affinity)) { |
| 5438 | return j; |
| 5439 | } |
| 5440 | jt = r.task; |
| 5441 | } |
| 5442 | } |
| 5443 | |
| 5444 | // Might it be at the top? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5445 | if (affinity.equals(((ActivityRecord)mMainStack.mHistory.get(N-1)).task.affinity)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5446 | return N-1; |
| 5447 | } |
| 5448 | |
| 5449 | return -1; |
| 5450 | } |
| 5451 | |
| 5452 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5453 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5454 | */ |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5455 | public void moveTaskToFront(int task, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5456 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5457 | "moveTaskToFront()"); |
| 5458 | |
| 5459 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5460 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5461 | Binder.getCallingUid(), "Task to front")) { |
| 5462 | return; |
| 5463 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5464 | final long origId = Binder.clearCallingIdentity(); |
| 5465 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5466 | TaskRecord tr = taskForIdLocked(task); |
| 5467 | if (tr != null) { |
| 5468 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5469 | mMainStack.mUserLeaving = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5470 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5471 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5472 | // Caller wants the home activity moved with it. To accomplish this, |
| 5473 | // we'll just move the home task to the top first. |
| 5474 | mMainStack.moveHomeToFrontLocked(); |
| 5475 | } |
| 5476 | mMainStack.moveTaskToFrontLocked(tr, null); |
| 5477 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5478 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5479 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 5480 | ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5481 | if (hr.task.taskId == task) { |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5482 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5483 | mMainStack.mUserLeaving = true; |
| 5484 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5485 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5486 | // Caller wants the home activity moved with it. To accomplish this, |
| 5487 | // we'll just move the home task to the top first. |
| 5488 | mMainStack.moveHomeToFrontLocked(); |
| 5489 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5490 | mMainStack.moveTaskToFrontLocked(hr.task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5491 | return; |
| 5492 | } |
| 5493 | } |
| 5494 | } finally { |
| 5495 | Binder.restoreCallingIdentity(origId); |
| 5496 | } |
| 5497 | } |
| 5498 | } |
| 5499 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5500 | public void moveTaskToBack(int task) { |
| 5501 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5502 | "moveTaskToBack()"); |
| 5503 | |
| 5504 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5505 | if (mMainStack.mResumedActivity != null |
| 5506 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5507 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5508 | Binder.getCallingUid(), "Task to back")) { |
| 5509 | return; |
| 5510 | } |
| 5511 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5512 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5513 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5514 | Binder.restoreCallingIdentity(origId); |
| 5515 | } |
| 5516 | } |
| 5517 | |
| 5518 | /** |
| 5519 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 5520 | * of the history stack. The activity's order within the task is unchanged. |
| 5521 | * |
| 5522 | * @param token A reference to the activity we wish to move |
| 5523 | * @param nonRoot If false then this only works if the activity is the root |
| 5524 | * of a task; if true it will work for any activity in a task. |
| 5525 | * @return Returns true if the move completed, false if not. |
| 5526 | */ |
| 5527 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
| 5528 | synchronized(this) { |
| 5529 | final long origId = Binder.clearCallingIdentity(); |
| 5530 | int taskId = getTaskForActivityLocked(token, !nonRoot); |
| 5531 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5532 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5533 | } |
| 5534 | Binder.restoreCallingIdentity(origId); |
| 5535 | } |
| 5536 | return false; |
| 5537 | } |
| 5538 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5539 | public void moveTaskBackwards(int task) { |
| 5540 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5541 | "moveTaskBackwards()"); |
| 5542 | |
| 5543 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5544 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5545 | Binder.getCallingUid(), "Task backwards")) { |
| 5546 | return; |
| 5547 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5548 | final long origId = Binder.clearCallingIdentity(); |
| 5549 | moveTaskBackwardsLocked(task); |
| 5550 | Binder.restoreCallingIdentity(origId); |
| 5551 | } |
| 5552 | } |
| 5553 | |
| 5554 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5555 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5556 | } |
| 5557 | |
| 5558 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 5559 | synchronized(this) { |
| 5560 | return getTaskForActivityLocked(token, onlyRoot); |
| 5561 | } |
| 5562 | } |
| 5563 | |
| 5564 | int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5565 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5566 | TaskRecord lastTask = null; |
| 5567 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5568 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5569 | if (r.appToken == token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5570 | if (!onlyRoot || lastTask != r.task) { |
| 5571 | return r.task.taskId; |
| 5572 | } |
| 5573 | return -1; |
| 5574 | } |
| 5575 | lastTask = r.task; |
| 5576 | } |
| 5577 | |
| 5578 | return -1; |
| 5579 | } |
| 5580 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5581 | public void finishOtherInstances(IBinder token, ComponentName className) { |
| 5582 | synchronized(this) { |
| 5583 | final long origId = Binder.clearCallingIdentity(); |
| 5584 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5585 | int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5586 | TaskRecord lastTask = null; |
| 5587 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5588 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5589 | if (r.realActivity.equals(className) |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5590 | && r.appToken != token && lastTask != r.task) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5591 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5592 | null, "others")) { |
| 5593 | i--; |
| 5594 | N--; |
| 5595 | } |
| 5596 | } |
| 5597 | lastTask = r.task; |
| 5598 | } |
| 5599 | |
| 5600 | Binder.restoreCallingIdentity(origId); |
| 5601 | } |
| 5602 | } |
| 5603 | |
| 5604 | // ========================================================= |
| 5605 | // THUMBNAILS |
| 5606 | // ========================================================= |
| 5607 | |
| 5608 | public void reportThumbnail(IBinder token, |
| 5609 | Bitmap thumbnail, CharSequence description) { |
| 5610 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 5611 | final long origId = Binder.clearCallingIdentity(); |
| 5612 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 5613 | Binder.restoreCallingIdentity(origId); |
| 5614 | } |
| 5615 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5616 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5617 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 5618 | TaskRecord task = null; |
| 5619 | ArrayList receivers = null; |
| 5620 | |
| 5621 | //System.out.println("Send pending thumbnail: " + r); |
| 5622 | |
| 5623 | synchronized(this) { |
| 5624 | if (r == null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 5625 | r = mMainStack.isInStackLocked(token); |
| 5626 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5627 | return; |
| 5628 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5629 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5630 | if (thumbnail == null && r.thumbHolder != null) { |
| 5631 | thumbnail = r.thumbHolder.lastThumbnail; |
| 5632 | description = r.thumbHolder.lastDescription; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5633 | } |
| 5634 | if (thumbnail == null && !always) { |
| 5635 | // If there is no thumbnail, and this entry is not actually |
| 5636 | // going away, then abort for now and pick up the next |
| 5637 | // thumbnail we get. |
| 5638 | return; |
| 5639 | } |
| 5640 | task = r.task; |
| 5641 | |
| 5642 | int N = mPendingThumbnails.size(); |
| 5643 | int i=0; |
| 5644 | while (i<N) { |
| 5645 | PendingThumbnailsRecord pr = |
| 5646 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 5647 | //System.out.println("Looking in " + pr.pendingRecords); |
| 5648 | if (pr.pendingRecords.remove(r)) { |
| 5649 | if (receivers == null) { |
| 5650 | receivers = new ArrayList(); |
| 5651 | } |
| 5652 | receivers.add(pr); |
| 5653 | if (pr.pendingRecords.size() == 0) { |
| 5654 | pr.finished = true; |
| 5655 | mPendingThumbnails.remove(i); |
| 5656 | N--; |
| 5657 | continue; |
| 5658 | } |
| 5659 | } |
| 5660 | i++; |
| 5661 | } |
| 5662 | } |
| 5663 | |
| 5664 | if (receivers != null) { |
| 5665 | final int N = receivers.size(); |
| 5666 | for (int i=0; i<N; i++) { |
| 5667 | try { |
| 5668 | PendingThumbnailsRecord pr = |
| 5669 | (PendingThumbnailsRecord)receivers.get(i); |
| 5670 | pr.receiver.newThumbnail( |
| 5671 | task != null ? task.taskId : -1, thumbnail, description); |
| 5672 | if (pr.finished) { |
| 5673 | pr.receiver.finished(); |
| 5674 | } |
| 5675 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5676 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5677 | } |
| 5678 | } |
| 5679 | } |
| 5680 | } |
| 5681 | |
| 5682 | // ========================================================= |
| 5683 | // CONTENT PROVIDERS |
| 5684 | // ========================================================= |
| 5685 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 5686 | private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) { |
| 5687 | List<ProviderInfo> providers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5688 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5689 | providers = AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5690 | queryContentProviders(app.processName, app.info.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5691 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5692 | } catch (RemoteException ex) { |
| 5693 | } |
| 5694 | if (providers != null) { |
| 5695 | final int N = providers.size(); |
| 5696 | for (int i=0; i<N; i++) { |
| 5697 | ProviderInfo cpi = |
| 5698 | (ProviderInfo)providers.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5699 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
| 5700 | ContentProviderRecord cpr = mProvidersByClass.get(comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5701 | if (cpr == null) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5702 | cpr = new ContentProviderRecord(cpi, app.info, comp); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5703 | mProvidersByClass.put(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5704 | } |
| 5705 | app.pubProviders.put(cpi.name, cpr); |
| 5706 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 5707 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5708 | } |
| 5709 | } |
| 5710 | return providers; |
| 5711 | } |
| 5712 | |
| 5713 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5714 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5715 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
| 5716 | final int callingUid = (r != null) ? r.info.uid : Binder.getCallingUid(); |
| 5717 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5718 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5719 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5720 | return null; |
| 5721 | } |
| 5722 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5723 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5724 | == PackageManager.PERMISSION_GRANTED) { |
| 5725 | return null; |
| 5726 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5727 | |
| 5728 | PathPermission[] pps = cpi.pathPermissions; |
| 5729 | if (pps != null) { |
| 5730 | int i = pps.length; |
| 5731 | while (i > 0) { |
| 5732 | i--; |
| 5733 | PathPermission pp = pps[i]; |
| 5734 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5735 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5736 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5737 | return null; |
| 5738 | } |
| 5739 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5740 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5741 | == PackageManager.PERMISSION_GRANTED) { |
| 5742 | return null; |
| 5743 | } |
| 5744 | } |
| 5745 | } |
| 5746 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5747 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 5748 | if (perms != null) { |
| 5749 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 5750 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 5751 | return null; |
| 5752 | } |
| 5753 | } |
| 5754 | } |
| 5755 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5756 | String msg; |
| 5757 | if (!cpi.exported) { |
| 5758 | msg = "Permission Denial: opening provider " + cpi.name |
| 5759 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 5760 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 5761 | + cpi.applicationInfo.uid; |
| 5762 | } else { |
| 5763 | msg = "Permission Denial: opening provider " + cpi.name |
| 5764 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 5765 | + ", uid=" + callingUid + ") requires " |
| 5766 | + cpi.readPermission + " or " + cpi.writePermission; |
| 5767 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5768 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5769 | return msg; |
| 5770 | } |
| 5771 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5772 | boolean incProviderCount(ProcessRecord r, ContentProviderRecord cpr) { |
| 5773 | if (r != null) { |
| 5774 | Integer cnt = r.conProviders.get(cpr); |
| 5775 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 5776 | "Adding provider requested by " |
| 5777 | + r.processName + " from process " |
| 5778 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 5779 | + " cnt=" + (cnt == null ? 1 : cnt)); |
| 5780 | if (cnt == null) { |
| 5781 | cpr.clients.add(r); |
| 5782 | r.conProviders.put(cpr, new Integer(1)); |
| 5783 | return true; |
| 5784 | } else { |
| 5785 | r.conProviders.put(cpr, new Integer(cnt.intValue()+1)); |
| 5786 | } |
| 5787 | } else { |
| 5788 | cpr.externals++; |
| 5789 | } |
| 5790 | return false; |
| 5791 | } |
| 5792 | |
| 5793 | boolean decProviderCount(ProcessRecord r, ContentProviderRecord cpr) { |
| 5794 | if (r != null) { |
| 5795 | Integer cnt = r.conProviders.get(cpr); |
| 5796 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 5797 | "Removing provider requested by " |
| 5798 | + r.processName + " from process " |
| 5799 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 5800 | + " cnt=" + cnt); |
| 5801 | if (cnt == null || cnt.intValue() <= 1) { |
| 5802 | cpr.clients.remove(r); |
| 5803 | r.conProviders.remove(cpr); |
| 5804 | return true; |
| 5805 | } else { |
| 5806 | r.conProviders.put(cpr, new Integer(cnt.intValue()-1)); |
| 5807 | } |
| 5808 | } else { |
| 5809 | cpr.externals++; |
| 5810 | } |
| 5811 | return false; |
| 5812 | } |
| 5813 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5814 | private final ContentProviderHolder getContentProviderImpl( |
| 5815 | IApplicationThread caller, String name) { |
| 5816 | ContentProviderRecord cpr; |
| 5817 | ProviderInfo cpi = null; |
| 5818 | |
| 5819 | synchronized(this) { |
| 5820 | ProcessRecord r = null; |
| 5821 | if (caller != null) { |
| 5822 | r = getRecordForAppLocked(caller); |
| 5823 | if (r == null) { |
| 5824 | throw new SecurityException( |
| 5825 | "Unable to find app for caller " + caller |
| 5826 | + " (pid=" + Binder.getCallingPid() |
| 5827 | + ") when getting content provider " + name); |
| 5828 | } |
| 5829 | } |
| 5830 | |
| 5831 | // First check if this content provider has been published... |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5832 | cpr = mProvidersByName.get(name); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5833 | boolean providerRunning = cpr != null; |
| 5834 | if (providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5835 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5836 | String msg; |
| 5837 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 5838 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5839 | } |
| 5840 | |
| 5841 | if (r != null && cpr.canRunHere(r)) { |
| 5842 | // This provider has been published or is in the process |
| 5843 | // of being published... but it is also allowed to run |
| 5844 | // in the caller's process, so don't make a connection |
| 5845 | // and just let the caller instantiate its own instance. |
| 5846 | if (cpr.provider != null) { |
| 5847 | // don't give caller the provider object, it needs |
| 5848 | // to make its own. |
| 5849 | cpr = new ContentProviderRecord(cpr); |
| 5850 | } |
| 5851 | return cpr; |
| 5852 | } |
| 5853 | |
| 5854 | final long origId = Binder.clearCallingIdentity(); |
| 5855 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5856 | // In this case the provider instance already exists, so we can |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5857 | // return it right away. |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5858 | final boolean countChanged = incProviderCount(r, cpr); |
| 5859 | if (countChanged) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5860 | if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 5861 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 5862 | // make sure to count it as being accessed and thus |
| 5863 | // back up on the LRU list. This is good because |
| 5864 | // content providers are often expensive to start. |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5865 | updateLruProcessLocked(cpr.proc, false, true); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 5866 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 5867 | } |
| 5868 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5869 | if (cpr.proc != null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5870 | if (false) { |
| 5871 | if (cpr.name.flattenToShortString().equals( |
| 5872 | "com.android.providers.calendar/.CalendarProvider2")) { |
| 5873 | Slog.v(TAG, "****************** KILLING " |
| 5874 | + cpr.name.flattenToShortString()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5875 | Process.killProcess(cpr.proc.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5876 | } |
| 5877 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5878 | boolean success = updateOomAdjLocked(cpr.proc); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5879 | if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success); |
| 5880 | // NOTE: there is still a race here where a signal could be |
| 5881 | // pending on the process even though we managed to update its |
| 5882 | // adj level. Not sure what to do about this, but at least |
| 5883 | // the race is now smaller. |
| 5884 | if (!success) { |
| 5885 | // Uh oh... it looks like the provider's process |
| 5886 | // has been killed on us. We need to wait for a new |
| 5887 | // process to be started, and make sure its death |
| 5888 | // doesn't kill our process. |
| 5889 | Slog.i(TAG, |
| 5890 | "Existing provider " + cpr.name.flattenToShortString() |
| 5891 | + " is crashing; detaching " + r); |
| 5892 | boolean lastRef = decProviderCount(r, cpr); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5893 | appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5894 | if (!lastRef) { |
| 5895 | // This wasn't the last ref our process had on |
| 5896 | // the provider... we have now been killed, bail. |
| 5897 | return null; |
| 5898 | } |
| 5899 | providerRunning = false; |
| 5900 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5901 | } |
| 5902 | |
| 5903 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5904 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5905 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5906 | if (!providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5907 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5908 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5909 | resolveContentProvider(name, |
| 5910 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5911 | } catch (RemoteException ex) { |
| 5912 | } |
| 5913 | if (cpi == null) { |
| 5914 | return null; |
| 5915 | } |
| 5916 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5917 | String msg; |
| 5918 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 5919 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5920 | } |
| 5921 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 5922 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 5923 | && !cpi.processName.equals("system")) { |
| 5924 | // If this content provider does not run in the system |
| 5925 | // process, and the system is not yet ready to run other |
| 5926 | // processes, then fail fast instead of hanging. |
| 5927 | throw new IllegalArgumentException( |
| 5928 | "Attempt to launch content provider before system ready"); |
| 5929 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5930 | |
| 5931 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
| 5932 | cpr = mProvidersByClass.get(comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5933 | final boolean firstClass = cpr == null; |
| 5934 | if (firstClass) { |
| 5935 | try { |
| 5936 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5937 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5938 | getApplicationInfo( |
| 5939 | cpi.applicationInfo.packageName, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5940 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5941 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5942 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5943 | + cpi.name); |
| 5944 | return null; |
| 5945 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5946 | cpr = new ContentProviderRecord(cpi, ai, comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5947 | } catch (RemoteException ex) { |
| 5948 | // pm is in same process, this will never happen. |
| 5949 | } |
| 5950 | } |
| 5951 | |
| 5952 | if (r != null && cpr.canRunHere(r)) { |
| 5953 | // If this is a multiprocess provider, then just return its |
| 5954 | // info and allow the caller to instantiate it. Only do |
| 5955 | // this if the provider is the same user as the caller's |
| 5956 | // process, or can run as root (so can be in any process). |
| 5957 | return cpr; |
| 5958 | } |
| 5959 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5960 | if (DEBUG_PROVIDER) { |
| 5961 | RuntimeException e = new RuntimeException("here"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5962 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.info.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5963 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5964 | } |
| 5965 | |
| 5966 | // This is single process, and our app is now connecting to it. |
| 5967 | // See if we are already in the process of launching this |
| 5968 | // provider. |
| 5969 | final int N = mLaunchingProviders.size(); |
| 5970 | int i; |
| 5971 | for (i=0; i<N; i++) { |
| 5972 | if (mLaunchingProviders.get(i) == cpr) { |
| 5973 | break; |
| 5974 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5975 | } |
| 5976 | |
| 5977 | // If the provider is not already being launched, then get it |
| 5978 | // started. |
| 5979 | if (i >= N) { |
| 5980 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 5981 | |
| 5982 | try { |
| 5983 | // Content provider is now in use, its package can't be stopped. |
| 5984 | try { |
| 5985 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 5986 | cpr.appInfo.packageName, false); |
| 5987 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 5988 | } catch (IllegalArgumentException e) { |
| 5989 | Slog.w(TAG, "Failed trying to unstop package " |
| 5990 | + cpr.appInfo.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 5991 | } |
| 5992 | |
| 5993 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 5994 | cpr.appInfo, false, 0, "content provider", |
| 5995 | new ComponentName(cpi.applicationInfo.packageName, |
| 5996 | cpi.name), false); |
| 5997 | if (proc == null) { |
| 5998 | Slog.w(TAG, "Unable to launch app " |
| 5999 | + cpi.applicationInfo.packageName + "/" |
| 6000 | + cpi.applicationInfo.uid + " for provider " |
| 6001 | + name + ": process is bad"); |
| 6002 | return null; |
| 6003 | } |
| 6004 | cpr.launchingApp = proc; |
| 6005 | mLaunchingProviders.add(cpr); |
| 6006 | } finally { |
| 6007 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6008 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6009 | } |
| 6010 | |
| 6011 | // Make sure the provider is published (the same provider class |
| 6012 | // may be published under multiple names). |
| 6013 | if (firstClass) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6014 | mProvidersByClass.put(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6015 | } |
| 6016 | mProvidersByName.put(name, cpr); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6017 | incProviderCount(r, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6018 | } |
| 6019 | } |
| 6020 | |
| 6021 | // Wait for the provider to be published... |
| 6022 | synchronized (cpr) { |
| 6023 | while (cpr.provider == null) { |
| 6024 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6025 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6026 | + cpi.applicationInfo.packageName + "/" |
| 6027 | + cpi.applicationInfo.uid + " for provider " |
| 6028 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6029 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6030 | cpi.applicationInfo.packageName, |
| 6031 | cpi.applicationInfo.uid, name); |
| 6032 | return null; |
| 6033 | } |
| 6034 | try { |
| 6035 | cpr.wait(); |
| 6036 | } catch (InterruptedException ex) { |
| 6037 | } |
| 6038 | } |
| 6039 | } |
| 6040 | return cpr; |
| 6041 | } |
| 6042 | |
| 6043 | public final ContentProviderHolder getContentProvider( |
| 6044 | IApplicationThread caller, String name) { |
| 6045 | if (caller == null) { |
| 6046 | String msg = "null IApplicationThread when getting content provider " |
| 6047 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6048 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6049 | throw new SecurityException(msg); |
| 6050 | } |
| 6051 | |
| 6052 | return getContentProviderImpl(caller, name); |
| 6053 | } |
| 6054 | |
| 6055 | private ContentProviderHolder getContentProviderExternal(String name) { |
| 6056 | return getContentProviderImpl(null, name); |
| 6057 | } |
| 6058 | |
| 6059 | /** |
| 6060 | * Drop a content provider from a ProcessRecord's bookkeeping |
| 6061 | * @param cpr |
| 6062 | */ |
| 6063 | public void removeContentProvider(IApplicationThread caller, String name) { |
| 6064 | synchronized (this) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6065 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6066 | if(cpr == null) { |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6067 | // remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6068 | if (DEBUG_PROVIDER) Slog.v(TAG, name + |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6069 | " provider not found in providers list"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6070 | return; |
| 6071 | } |
| 6072 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 6073 | if (r == null) { |
| 6074 | throw new SecurityException( |
| 6075 | "Unable to find app for caller " + caller + |
| 6076 | " when removing content provider " + name); |
| 6077 | } |
| 6078 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6079 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
| 6080 | ContentProviderRecord localCpr = mProvidersByClass.get(comp); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6081 | if (localCpr.proc == r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6082 | //should not happen. taken care of as a local provider |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6083 | Slog.w(TAG, "removeContentProvider called on local provider: " |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6084 | + cpr.info.name + " in process " + r.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6085 | return; |
| 6086 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6087 | if (decProviderCount(r, localCpr)) { |
| 6088 | updateOomAdjLocked(); |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 6089 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6090 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6091 | } |
| 6092 | } |
| 6093 | |
| 6094 | private void removeContentProviderExternal(String name) { |
| 6095 | synchronized (this) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6096 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6097 | if(cpr == null) { |
| 6098 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6099 | if(localLOGV) Slog.v(TAG, name+" content provider not found in providers list"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6100 | return; |
| 6101 | } |
| 6102 | |
| 6103 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6104 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
| 6105 | ContentProviderRecord localCpr = mProvidersByClass.get(comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6106 | localCpr.externals--; |
| 6107 | if (localCpr.externals < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6108 | Slog.e(TAG, "Externals < 0 for content provider " + localCpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6109 | } |
| 6110 | updateOomAdjLocked(); |
| 6111 | } |
| 6112 | } |
| 6113 | |
| 6114 | public final void publishContentProviders(IApplicationThread caller, |
| 6115 | List<ContentProviderHolder> providers) { |
| 6116 | if (providers == null) { |
| 6117 | return; |
| 6118 | } |
| 6119 | |
| 6120 | synchronized(this) { |
| 6121 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 6122 | if (r == null) { |
| 6123 | throw new SecurityException( |
| 6124 | "Unable to find app for caller " + caller |
| 6125 | + " (pid=" + Binder.getCallingPid() |
| 6126 | + ") when publishing content providers"); |
| 6127 | } |
| 6128 | |
| 6129 | final long origId = Binder.clearCallingIdentity(); |
| 6130 | |
| 6131 | final int N = providers.size(); |
| 6132 | for (int i=0; i<N; i++) { |
| 6133 | ContentProviderHolder src = providers.get(i); |
| 6134 | if (src == null || src.info == null || src.provider == null) { |
| 6135 | continue; |
| 6136 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6137 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6138 | if (dst != null) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6139 | ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name); |
| 6140 | mProvidersByClass.put(comp, dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6141 | String names[] = dst.info.authority.split(";"); |
| 6142 | for (int j = 0; j < names.length; j++) { |
| 6143 | mProvidersByName.put(names[j], dst); |
| 6144 | } |
| 6145 | |
| 6146 | int NL = mLaunchingProviders.size(); |
| 6147 | int j; |
| 6148 | for (j=0; j<NL; j++) { |
| 6149 | if (mLaunchingProviders.get(j) == dst) { |
| 6150 | mLaunchingProviders.remove(j); |
| 6151 | j--; |
| 6152 | NL--; |
| 6153 | } |
| 6154 | } |
| 6155 | synchronized (dst) { |
| 6156 | dst.provider = src.provider; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6157 | dst.proc = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6158 | dst.notifyAll(); |
| 6159 | } |
| 6160 | updateOomAdjLocked(r); |
| 6161 | } |
| 6162 | } |
| 6163 | |
| 6164 | Binder.restoreCallingIdentity(origId); |
| 6165 | } |
| 6166 | } |
| 6167 | |
| 6168 | public static final void installSystemProviders() { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6169 | List<ProviderInfo> providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6170 | synchronized (mSelf) { |
| 6171 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 6172 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 6173 | if (providers != null) { |
| 6174 | for (int i=providers.size()-1; i>=0; i--) { |
| 6175 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 6176 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6177 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 6178 | + ": not system .apk"); |
| 6179 | providers.remove(i); |
| 6180 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6181 | } |
| 6182 | } |
| 6183 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6184 | if (providers != null) { |
| 6185 | mSystemThread.installSystemProviders(providers); |
| 6186 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 6187 | |
| 6188 | mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 6189 | |
| 6190 | mSelf.mUsageStatsService.monitorPackages(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6191 | } |
| 6192 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6193 | /** |
| 6194 | * Allows app to retrieve the MIME type of a URI without having permission |
| 6195 | * to access its content provider. |
| 6196 | * |
| 6197 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 6198 | * |
| 6199 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 6200 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 6201 | */ |
| 6202 | public String getProviderMimeType(Uri uri) { |
| 6203 | final String name = uri.getAuthority(); |
| 6204 | final long ident = Binder.clearCallingIdentity(); |
| 6205 | ContentProviderHolder holder = null; |
| 6206 | |
| 6207 | try { |
| 6208 | holder = getContentProviderExternal(name); |
| 6209 | if (holder != null) { |
| 6210 | return holder.provider.getType(uri); |
| 6211 | } |
| 6212 | } catch (RemoteException e) { |
| 6213 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 6214 | return null; |
| 6215 | } finally { |
| 6216 | if (holder != null) { |
| 6217 | removeContentProviderExternal(name); |
| 6218 | } |
| 6219 | Binder.restoreCallingIdentity(ident); |
| 6220 | } |
| 6221 | |
| 6222 | return null; |
| 6223 | } |
| 6224 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6225 | // ========================================================= |
| 6226 | // GLOBAL MANAGEMENT |
| 6227 | // ========================================================= |
| 6228 | |
| 6229 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
| 6230 | ApplicationInfo info, String customProcess) { |
| 6231 | String proc = customProcess != null ? customProcess : info.processName; |
| 6232 | BatteryStatsImpl.Uid.Proc ps = null; |
| 6233 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 6234 | synchronized (stats) { |
| 6235 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 6236 | } |
| 6237 | return new ProcessRecord(ps, thread, info, proc); |
| 6238 | } |
| 6239 | |
| 6240 | final ProcessRecord addAppLocked(ApplicationInfo info) { |
| 6241 | ProcessRecord app = getProcessRecordLocked(info.processName, info.uid); |
| 6242 | |
| 6243 | if (app == null) { |
| 6244 | app = newProcessRecordLocked(null, info, null); |
| 6245 | mProcessNames.put(info.processName, info.uid, app); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 6246 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6247 | } |
| 6248 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6249 | // This package really, really can not be stopped. |
| 6250 | try { |
| 6251 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 6252 | info.packageName, false); |
| 6253 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6254 | } catch (IllegalArgumentException e) { |
| 6255 | Slog.w(TAG, "Failed trying to unstop package " |
| 6256 | + info.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6257 | } |
| 6258 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6259 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 6260 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 6261 | app.persistent = true; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 6262 | app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6263 | } |
| 6264 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 6265 | mPersistentStartingProcesses.add(app); |
| 6266 | startProcessLocked(app, "added application", app.processName); |
| 6267 | } |
| 6268 | |
| 6269 | return app; |
| 6270 | } |
| 6271 | |
| 6272 | public void unhandledBack() { |
| 6273 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 6274 | "unhandledBack()"); |
| 6275 | |
| 6276 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6277 | int count = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6278 | if (DEBUG_SWITCH) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6279 | TAG, "Performing unhandledBack(): stack size = " + count); |
| 6280 | if (count > 1) { |
| 6281 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6282 | mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6283 | count-1, Activity.RESULT_CANCELED, null, "unhandled-back"); |
| 6284 | Binder.restoreCallingIdentity(origId); |
| 6285 | } |
| 6286 | } |
| 6287 | } |
| 6288 | |
| 6289 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
| 6290 | String name = uri.getAuthority(); |
| 6291 | ContentProviderHolder cph = getContentProviderExternal(name); |
| 6292 | ParcelFileDescriptor pfd = null; |
| 6293 | if (cph != null) { |
| 6294 | // We record the binder invoker's uid in thread-local storage before |
| 6295 | // going to the content provider to open the file. Later, in the code |
| 6296 | // that handles all permissions checks, we look for this uid and use |
| 6297 | // that rather than the Activity Manager's own uid. The effect is that |
| 6298 | // we do the check against the caller's permissions even though it looks |
| 6299 | // to the content provider like the Activity Manager itself is making |
| 6300 | // the request. |
| 6301 | sCallerIdentity.set(new Identity( |
| 6302 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 6303 | try { |
| 6304 | pfd = cph.provider.openFile(uri, "r"); |
| 6305 | } catch (FileNotFoundException e) { |
| 6306 | // do nothing; pfd will be returned null |
| 6307 | } finally { |
| 6308 | // Ensure that whatever happens, we clean up the identity state |
| 6309 | sCallerIdentity.remove(); |
| 6310 | } |
| 6311 | |
| 6312 | // We've got the fd now, so we're done with the provider. |
| 6313 | removeContentProviderExternal(name); |
| 6314 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6315 | Slog.d(TAG, "Failed to get provider for authority '" + name + "'"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6316 | } |
| 6317 | return pfd; |
| 6318 | } |
| 6319 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6320 | // Actually is sleeping or shutting down or whatever else in the future |
| 6321 | // is an inactive state. |
| 6322 | public boolean isSleeping() { |
| 6323 | return mSleeping || mShuttingDown; |
| 6324 | } |
| 6325 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6326 | public void goingToSleep() { |
| 6327 | synchronized(this) { |
| 6328 | mSleeping = true; |
| 6329 | mWindowManager.setEventDispatching(false); |
| 6330 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6331 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6332 | |
| 6333 | // Initialize the wake times of all processes. |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 6334 | checkExcessivePowerUsageLocked(false); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6335 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 6336 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 6337 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6338 | } |
| 6339 | } |
| 6340 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6341 | public boolean shutdown(int timeout) { |
| 6342 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 6343 | != PackageManager.PERMISSION_GRANTED) { |
| 6344 | throw new SecurityException("Requires permission " |
| 6345 | + android.Manifest.permission.SHUTDOWN); |
| 6346 | } |
| 6347 | |
| 6348 | boolean timedout = false; |
| 6349 | |
| 6350 | synchronized(this) { |
| 6351 | mShuttingDown = true; |
| 6352 | mWindowManager.setEventDispatching(false); |
| 6353 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6354 | if (mMainStack.mResumedActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6355 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6356 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6357 | while (mMainStack.mResumedActivity != null |
| 6358 | || mMainStack.mPausingActivity != null) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6359 | long delay = endTime - System.currentTimeMillis(); |
| 6360 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6361 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6362 | timedout = true; |
| 6363 | break; |
| 6364 | } |
| 6365 | try { |
| 6366 | this.wait(); |
| 6367 | } catch (InterruptedException e) { |
| 6368 | } |
| 6369 | } |
| 6370 | } |
| 6371 | } |
| 6372 | |
| 6373 | mUsageStatsService.shutdown(); |
| 6374 | mBatteryStatsService.shutdown(); |
| 6375 | |
| 6376 | return timedout; |
| 6377 | } |
| 6378 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6379 | public final void activitySlept(IBinder token) { |
| 6380 | if (localLOGV) Slog.v( |
| 6381 | TAG, "Activity slept: token=" + token); |
| 6382 | |
| 6383 | ActivityRecord r = null; |
| 6384 | |
| 6385 | final long origId = Binder.clearCallingIdentity(); |
| 6386 | |
| 6387 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6388 | r = mMainStack.isInStackLocked(token); |
| 6389 | if (r != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6390 | mMainStack.activitySleptLocked(r); |
| 6391 | } |
| 6392 | } |
| 6393 | |
| 6394 | Binder.restoreCallingIdentity(origId); |
| 6395 | } |
| 6396 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6397 | public void wakingUp() { |
| 6398 | synchronized(this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6399 | mWindowManager.setEventDispatching(true); |
| 6400 | mSleeping = false; |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6401 | mMainStack.awakeFromSleepingLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6402 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6403 | } |
| 6404 | } |
| 6405 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6406 | public void stopAppSwitches() { |
| 6407 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6408 | != PackageManager.PERMISSION_GRANTED) { |
| 6409 | throw new SecurityException("Requires permission " |
| 6410 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6411 | } |
| 6412 | |
| 6413 | synchronized(this) { |
| 6414 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 6415 | + APP_SWITCH_DELAY_TIME; |
| 6416 | mDidAppSwitch = false; |
| 6417 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6418 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6419 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 6420 | } |
| 6421 | } |
| 6422 | |
| 6423 | public void resumeAppSwitches() { |
| 6424 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6425 | != PackageManager.PERMISSION_GRANTED) { |
| 6426 | throw new SecurityException("Requires permission " |
| 6427 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6428 | } |
| 6429 | |
| 6430 | synchronized(this) { |
| 6431 | // Note that we don't execute any pending app switches... we will |
| 6432 | // let those wait until either the timeout, or the next start |
| 6433 | // activity request. |
| 6434 | mAppSwitchesAllowedTime = 0; |
| 6435 | } |
| 6436 | } |
| 6437 | |
| 6438 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 6439 | String name) { |
| 6440 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 6441 | return true; |
| 6442 | } |
| 6443 | |
| 6444 | final int perm = checkComponentPermission( |
| 6445 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6446 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6447 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 6448 | return true; |
| 6449 | } |
| 6450 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6451 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6452 | return false; |
| 6453 | } |
| 6454 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6455 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 6456 | boolean persistent) { |
| 6457 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 6458 | "setDebugApp()"); |
| 6459 | |
| 6460 | // Note that this is not really thread safe if there are multiple |
| 6461 | // callers into it at the same time, but that's not a situation we |
| 6462 | // care about. |
| 6463 | if (persistent) { |
| 6464 | final ContentResolver resolver = mContext.getContentResolver(); |
| 6465 | Settings.System.putString( |
| 6466 | resolver, Settings.System.DEBUG_APP, |
| 6467 | packageName); |
| 6468 | Settings.System.putInt( |
| 6469 | resolver, Settings.System.WAIT_FOR_DEBUGGER, |
| 6470 | waitForDebugger ? 1 : 0); |
| 6471 | } |
| 6472 | |
| 6473 | synchronized (this) { |
| 6474 | if (!persistent) { |
| 6475 | mOrigDebugApp = mDebugApp; |
| 6476 | mOrigWaitForDebugger = mWaitForDebugger; |
| 6477 | } |
| 6478 | mDebugApp = packageName; |
| 6479 | mWaitForDebugger = waitForDebugger; |
| 6480 | mDebugTransient = !persistent; |
| 6481 | if (packageName != null) { |
| 6482 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 6483 | forceStopPackageLocked(packageName, -1, false, false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6484 | Binder.restoreCallingIdentity(origId); |
| 6485 | } |
| 6486 | } |
| 6487 | } |
| 6488 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 6489 | void setProfileApp(ApplicationInfo app, String processName, String profileFile, |
| 6490 | ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
| 6491 | synchronized (this) { |
| 6492 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 6493 | if (!isDebuggable) { |
| 6494 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 6495 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 6496 | } |
| 6497 | } |
| 6498 | mProfileApp = processName; |
| 6499 | mProfileFile = profileFile; |
| 6500 | if (mProfileFd != null) { |
| 6501 | try { |
| 6502 | mProfileFd.close(); |
| 6503 | } catch (IOException e) { |
| 6504 | } |
| 6505 | mProfileFd = null; |
| 6506 | } |
| 6507 | mProfileFd = profileFd; |
| 6508 | mProfileType = 0; |
| 6509 | mAutoStopProfiler = autoStopProfiler; |
| 6510 | } |
| 6511 | } |
| 6512 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6513 | public void setAlwaysFinish(boolean enabled) { |
| 6514 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 6515 | "setAlwaysFinish()"); |
| 6516 | |
| 6517 | Settings.System.putInt( |
| 6518 | mContext.getContentResolver(), |
| 6519 | Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
| 6520 | |
| 6521 | synchronized (this) { |
| 6522 | mAlwaysFinishActivities = enabled; |
| 6523 | } |
| 6524 | } |
| 6525 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6526 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6527 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6528 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6529 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6530 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6531 | } |
| 6532 | } |
| 6533 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 6534 | public boolean isUserAMonkey() { |
| 6535 | // For now the fact that there is a controller implies |
| 6536 | // we have a monkey. |
| 6537 | synchronized (this) { |
| 6538 | return mController != null; |
| 6539 | } |
| 6540 | } |
| 6541 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6542 | public void registerActivityWatcher(IActivityWatcher watcher) { |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6543 | synchronized (this) { |
| 6544 | mWatchers.register(watcher); |
| 6545 | } |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6546 | } |
| 6547 | |
| 6548 | public void unregisterActivityWatcher(IActivityWatcher watcher) { |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6549 | synchronized (this) { |
| 6550 | mWatchers.unregister(watcher); |
| 6551 | } |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6552 | } |
| 6553 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 6554 | public void registerProcessObserver(IProcessObserver observer) { |
| 6555 | mProcessObservers.register(observer); |
| 6556 | } |
| 6557 | |
| 6558 | public void unregisterProcessObserver(IProcessObserver observer) { |
| 6559 | mProcessObservers.unregister(observer); |
| 6560 | } |
| 6561 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6562 | public void setImmersive(IBinder token, boolean immersive) { |
| 6563 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6564 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 6565 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6566 | throw new IllegalArgumentException(); |
| 6567 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6568 | r.immersive = immersive; |
| 6569 | } |
| 6570 | } |
| 6571 | |
| 6572 | public boolean isImmersive(IBinder token) { |
| 6573 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6574 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 6575 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6576 | throw new IllegalArgumentException(); |
| 6577 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6578 | return r.immersive; |
| 6579 | } |
| 6580 | } |
| 6581 | |
| 6582 | public boolean isTopActivityImmersive() { |
| 6583 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6584 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6585 | return (r != null) ? r.immersive : false; |
| 6586 | } |
| 6587 | } |
| 6588 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6589 | public final void enterSafeMode() { |
| 6590 | synchronized(this) { |
| 6591 | // It only makes sense to do this before the system is ready |
| 6592 | // and started launching other packages. |
| 6593 | if (!mSystemReady) { |
| 6594 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6595 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6596 | } catch (RemoteException e) { |
| 6597 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6598 | } |
| 6599 | } |
| 6600 | } |
| 6601 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 6602 | public final void showSafeModeOverlay() { |
| 6603 | View v = LayoutInflater.from(mContext).inflate( |
| 6604 | com.android.internal.R.layout.safe_mode, null); |
| 6605 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 6606 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 6607 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6608 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6609 | lp.gravity = Gravity.BOTTOM | Gravity.LEFT; |
| 6610 | lp.format = v.getBackground().getOpacity(); |
| 6611 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 6612 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 6613 | ((WindowManager)mContext.getSystemService( |
| 6614 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 6615 | } |
| 6616 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6617 | public void noteWakeupAlarm(IIntentSender sender) { |
| 6618 | if (!(sender instanceof PendingIntentRecord)) { |
| 6619 | return; |
| 6620 | } |
| 6621 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 6622 | synchronized (stats) { |
| 6623 | if (mBatteryStatsService.isOnBattery()) { |
| 6624 | mBatteryStatsService.enforceCallingPermission(); |
| 6625 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 6626 | int MY_UID = Binder.getCallingUid(); |
| 6627 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 6628 | BatteryStatsImpl.Uid.Pkg pkg = |
| 6629 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 6630 | pkg.incWakeupsLocked(); |
| 6631 | } |
| 6632 | } |
| 6633 | } |
| 6634 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6635 | public boolean killPids(int[] pids, String pReason, boolean secure) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6636 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6637 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6638 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6639 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6640 | // XXX Note: don't acquire main activity lock here, because the window |
| 6641 | // manager calls in with its locks held. |
| 6642 | |
| 6643 | boolean killed = false; |
| 6644 | synchronized (mPidsSelfLocked) { |
| 6645 | int[] types = new int[pids.length]; |
| 6646 | int worstType = 0; |
| 6647 | for (int i=0; i<pids.length; i++) { |
| 6648 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 6649 | if (proc != null) { |
| 6650 | int type = proc.setAdj; |
| 6651 | types[i] = type; |
| 6652 | if (type > worstType) { |
| 6653 | worstType = type; |
| 6654 | } |
| 6655 | } |
| 6656 | } |
| 6657 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6658 | // If the worst oom_adj is somewhere in the hidden proc LRU range, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6659 | // then constrain it so we will kill all hidden procs. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 6660 | if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ |
| 6661 | && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 6662 | worstType = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6663 | } |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6664 | |
| 6665 | // If this is not a secure call, don't let it kill processes that |
| 6666 | // are important. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 6667 | if (!secure && worstType < ProcessList.SERVICE_ADJ) { |
| 6668 | worstType = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6669 | } |
| 6670 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6671 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6672 | for (int i=0; i<pids.length; i++) { |
| 6673 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 6674 | if (proc == null) { |
| 6675 | continue; |
| 6676 | } |
| 6677 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6678 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 6679 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6680 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid, |
| 6681 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6682 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6683 | proc.killedBackground = true; |
| 6684 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6685 | } |
| 6686 | } |
| 6687 | } |
| 6688 | return killed; |
| 6689 | } |
| 6690 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6691 | public final void startRunning(String pkg, String cls, String action, |
| 6692 | String data) { |
| 6693 | synchronized(this) { |
| 6694 | if (mStartRunning) { |
| 6695 | return; |
| 6696 | } |
| 6697 | mStartRunning = true; |
| 6698 | mTopComponent = pkg != null && cls != null |
| 6699 | ? new ComponentName(pkg, cls) : null; |
| 6700 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 6701 | mTopData = data; |
| 6702 | if (!mSystemReady) { |
| 6703 | return; |
| 6704 | } |
| 6705 | } |
| 6706 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6707 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6708 | } |
| 6709 | |
| 6710 | private void retrieveSettings() { |
| 6711 | final ContentResolver resolver = mContext.getContentResolver(); |
| 6712 | String debugApp = Settings.System.getString( |
| 6713 | resolver, Settings.System.DEBUG_APP); |
| 6714 | boolean waitForDebugger = Settings.System.getInt( |
| 6715 | resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0; |
| 6716 | boolean alwaysFinishActivities = Settings.System.getInt( |
| 6717 | resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
| 6718 | |
| 6719 | Configuration configuration = new Configuration(); |
| 6720 | Settings.System.getConfiguration(resolver, configuration); |
| 6721 | |
| 6722 | synchronized (this) { |
| 6723 | mDebugApp = mOrigDebugApp = debugApp; |
| 6724 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 6725 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 6726 | // This happens before any activities are started, so we can |
| 6727 | // change mConfiguration in-place. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 6728 | updateConfigurationLocked(configuration, null, false, true); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6729 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6730 | } |
| 6731 | } |
| 6732 | |
| 6733 | public boolean testIsSystemReady() { |
| 6734 | // no need to synchronize(this) just to read & return the value |
| 6735 | return mSystemReady; |
| 6736 | } |
| 6737 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6738 | private static File getCalledPreBootReceiversFile() { |
| 6739 | File dataDir = Environment.getDataDirectory(); |
| 6740 | File systemDir = new File(dataDir, "system"); |
| 6741 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 6742 | return fname; |
| 6743 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6744 | |
| 6745 | static final int LAST_DONE_VERSION = 10000; |
| 6746 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6747 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 6748 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 6749 | File file = getCalledPreBootReceiversFile(); |
| 6750 | FileInputStream fis = null; |
| 6751 | try { |
| 6752 | fis = new FileInputStream(file); |
| 6753 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6754 | int fvers = dis.readInt(); |
| 6755 | if (fvers == LAST_DONE_VERSION) { |
| 6756 | String vers = dis.readUTF(); |
| 6757 | String codename = dis.readUTF(); |
| 6758 | String build = dis.readUTF(); |
| 6759 | if (android.os.Build.VERSION.RELEASE.equals(vers) |
| 6760 | && android.os.Build.VERSION.CODENAME.equals(codename) |
| 6761 | && android.os.Build.VERSION.INCREMENTAL.equals(build)) { |
| 6762 | int num = dis.readInt(); |
| 6763 | while (num > 0) { |
| 6764 | num--; |
| 6765 | String pkg = dis.readUTF(); |
| 6766 | String cls = dis.readUTF(); |
| 6767 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 6768 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6769 | } |
| 6770 | } |
| 6771 | } catch (FileNotFoundException e) { |
| 6772 | } catch (IOException e) { |
| 6773 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 6774 | } finally { |
| 6775 | if (fis != null) { |
| 6776 | try { |
| 6777 | fis.close(); |
| 6778 | } catch (IOException e) { |
| 6779 | } |
| 6780 | } |
| 6781 | } |
| 6782 | return lastDoneReceivers; |
| 6783 | } |
| 6784 | |
| 6785 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 6786 | File file = getCalledPreBootReceiversFile(); |
| 6787 | FileOutputStream fos = null; |
| 6788 | DataOutputStream dos = null; |
| 6789 | try { |
| 6790 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 6791 | fos = new FileOutputStream(file); |
| 6792 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6793 | dos.writeInt(LAST_DONE_VERSION); |
| 6794 | dos.writeUTF(android.os.Build.VERSION.RELEASE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6795 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6796 | dos.writeUTF(android.os.Build.VERSION.INCREMENTAL); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6797 | dos.writeInt(list.size()); |
| 6798 | for (int i=0; i<list.size(); i++) { |
| 6799 | dos.writeUTF(list.get(i).getPackageName()); |
| 6800 | dos.writeUTF(list.get(i).getClassName()); |
| 6801 | } |
| 6802 | } catch (IOException e) { |
| 6803 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 6804 | file.delete(); |
| 6805 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 6806 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6807 | if (dos != null) { |
| 6808 | try { |
| 6809 | dos.close(); |
| 6810 | } catch (IOException e) { |
| 6811 | // TODO Auto-generated catch block |
| 6812 | e.printStackTrace(); |
| 6813 | } |
| 6814 | } |
| 6815 | } |
| 6816 | } |
| 6817 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6818 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6819 | synchronized(this) { |
| 6820 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6821 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6822 | return; |
| 6823 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6824 | |
| 6825 | // Check to see if there are any update receivers to run. |
| 6826 | if (!mDidUpdate) { |
| 6827 | if (mWaitingUpdate) { |
| 6828 | return; |
| 6829 | } |
| 6830 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 6831 | List<ResolveInfo> ris = null; |
| 6832 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6833 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6834 | intent, null, 0); |
| 6835 | } catch (RemoteException e) { |
| 6836 | } |
| 6837 | if (ris != null) { |
| 6838 | for (int i=ris.size()-1; i>=0; i--) { |
| 6839 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 6840 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6841 | ris.remove(i); |
| 6842 | } |
| 6843 | } |
| 6844 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6845 | |
| 6846 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
| 6847 | |
| 6848 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6849 | for (int i=0; i<ris.size(); i++) { |
| 6850 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6851 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 6852 | if (lastDoneReceivers.contains(comp)) { |
| 6853 | ris.remove(i); |
| 6854 | i--; |
| 6855 | } |
| 6856 | } |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 6857 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6858 | for (int i=0; i<ris.size(); i++) { |
| 6859 | ActivityInfo ai = ris.get(i).activityInfo; |
| 6860 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 6861 | doneReceivers.add(comp); |
| 6862 | intent.setComponent(comp); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6863 | IIntentReceiver finisher = null; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 6864 | if (i == ris.size()-1) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6865 | finisher = new IIntentReceiver.Stub() { |
| 6866 | public void performReceive(Intent intent, int resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 6867 | String data, Bundle extras, boolean ordered, |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 6868 | boolean sticky) { |
| 6869 | // The raw IIntentReceiver interface is called |
| 6870 | // with the AM lock held, so redispatch to |
| 6871 | // execute our code without the lock. |
| 6872 | mHandler.post(new Runnable() { |
| 6873 | public void run() { |
| 6874 | synchronized (ActivityManagerService.this) { |
| 6875 | mDidUpdate = true; |
| 6876 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6877 | writeLastDonePreBootReceivers(doneReceivers); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6878 | showBootMessage(mContext.getText( |
| 6879 | R.string.android_upgrading_complete), |
| 6880 | false); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 6881 | systemReady(goingCallback); |
| 6882 | } |
| 6883 | }); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6884 | } |
| 6885 | }; |
| 6886 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6887 | Slog.i(TAG, "Sending system update to: " + intent.getComponent()); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6888 | broadcastIntentLocked(null, null, intent, null, finisher, |
| 6889 | 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 6890 | if (finisher != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6891 | mWaitingUpdate = true; |
| 6892 | } |
| 6893 | } |
| 6894 | } |
| 6895 | if (mWaitingUpdate) { |
| 6896 | return; |
| 6897 | } |
| 6898 | mDidUpdate = true; |
| 6899 | } |
| 6900 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6901 | mSystemReady = true; |
| 6902 | if (!mStartRunning) { |
| 6903 | return; |
| 6904 | } |
| 6905 | } |
| 6906 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6907 | ArrayList<ProcessRecord> procsToKill = null; |
| 6908 | synchronized(mPidsSelfLocked) { |
| 6909 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 6910 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 6911 | if (!isAllowedWhileBooting(proc.info)){ |
| 6912 | if (procsToKill == null) { |
| 6913 | procsToKill = new ArrayList<ProcessRecord>(); |
| 6914 | } |
| 6915 | procsToKill.add(proc); |
| 6916 | } |
| 6917 | } |
| 6918 | } |
| 6919 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6920 | synchronized(this) { |
| 6921 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6922 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 6923 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6924 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 6925 | removeProcessLocked(proc, true, false, "system update done"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6926 | } |
| 6927 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6928 | |
| 6929 | // Now that we have cleaned up any update processes, we |
| 6930 | // are ready to start launching real processes and know that |
| 6931 | // we won't trample on them any more. |
| 6932 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6933 | } |
| 6934 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6935 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6936 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6937 | SystemClock.uptimeMillis()); |
| 6938 | |
| 6939 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6940 | // Make sure we have no pre-ready processes sitting around. |
| 6941 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6942 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 6943 | ResolveInfo ri = mContext.getPackageManager() |
| 6944 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6945 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6946 | CharSequence errorMsg = null; |
| 6947 | if (ri != null) { |
| 6948 | ActivityInfo ai = ri.activityInfo; |
| 6949 | ApplicationInfo app = ai.applicationInfo; |
| 6950 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 6951 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 6952 | mTopData = null; |
| 6953 | mTopComponent = new ComponentName(app.packageName, |
| 6954 | ai.name); |
| 6955 | } else { |
| 6956 | errorMsg = mContext.getResources().getText( |
| 6957 | com.android.internal.R.string.factorytest_not_system); |
| 6958 | } |
| 6959 | } else { |
| 6960 | errorMsg = mContext.getResources().getText( |
| 6961 | com.android.internal.R.string.factorytest_no_action); |
| 6962 | } |
| 6963 | if (errorMsg != null) { |
| 6964 | mTopAction = null; |
| 6965 | mTopData = null; |
| 6966 | mTopComponent = null; |
| 6967 | Message msg = Message.obtain(); |
| 6968 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 6969 | msg.getData().putCharSequence("msg", errorMsg); |
| 6970 | mHandler.sendMessage(msg); |
| 6971 | } |
| 6972 | } |
| 6973 | } |
| 6974 | |
| 6975 | retrieveSettings(); |
| 6976 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6977 | if (goingCallback != null) goingCallback.run(); |
| 6978 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6979 | synchronized (this) { |
| 6980 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 6981 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6982 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6983 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6984 | if (apps != null) { |
| 6985 | int N = apps.size(); |
| 6986 | int i; |
| 6987 | for (i=0; i<N; i++) { |
| 6988 | ApplicationInfo info |
| 6989 | = (ApplicationInfo)apps.get(i); |
| 6990 | if (info != null && |
| 6991 | !info.packageName.equals("android")) { |
| 6992 | addAppLocked(info); |
| 6993 | } |
| 6994 | } |
| 6995 | } |
| 6996 | } catch (RemoteException ex) { |
| 6997 | // pm is in same process, this will never happen. |
| 6998 | } |
| 6999 | } |
| 7000 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7001 | // Start up initial activity. |
| 7002 | mBooting = true; |
| 7003 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7004 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7005 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7006 | Message msg = Message.obtain(); |
| 7007 | msg.what = SHOW_UID_ERROR_MSG; |
| 7008 | mHandler.sendMessage(msg); |
| 7009 | } |
| 7010 | } catch (RemoteException e) { |
| 7011 | } |
| 7012 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7013 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7014 | } |
| 7015 | } |
| 7016 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7017 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7018 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7019 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7020 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7021 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7022 | startAppProblemLocked(app); |
| 7023 | app.stopFreezingAllLocked(); |
| 7024 | return handleAppCrashLocked(app); |
| 7025 | } |
| 7026 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7027 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7028 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7029 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7030 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7031 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 7032 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7033 | startAppProblemLocked(app); |
| 7034 | app.stopFreezingAllLocked(); |
| 7035 | } |
| 7036 | |
| 7037 | /** |
| 7038 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 7039 | * |
| 7040 | * @param app The ProcessRecord in which the error occurred. |
| 7041 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 7042 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7043 | * @param activity The activity associated with the crash, if known. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7044 | * @param shortMsg Short message describing the crash. |
| 7045 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7046 | * @param stackTrace Full crash stack trace, may be null. |
| 7047 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7048 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 7049 | */ |
| 7050 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7051 | int condition, String activity, String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7052 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7053 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7054 | report.condition = condition; |
| 7055 | report.processName = app.processName; |
| 7056 | report.pid = app.pid; |
| 7057 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7058 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7059 | report.shortMsg = shortMsg; |
| 7060 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7061 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7062 | |
| 7063 | return report; |
| 7064 | } |
| 7065 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7066 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7067 | synchronized (this) { |
| 7068 | app.crashing = false; |
| 7069 | app.crashingReport = null; |
| 7070 | app.notResponding = false; |
| 7071 | app.notRespondingReport = null; |
| 7072 | if (app.anrDialog == fromDialog) { |
| 7073 | app.anrDialog = null; |
| 7074 | } |
| 7075 | if (app.waitDialog == fromDialog) { |
| 7076 | app.waitDialog = null; |
| 7077 | } |
| 7078 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7079 | handleAppCrashLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7080 | Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request"); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7081 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 7082 | app.processName, app.setAdj, "user's request after error"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7083 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7084 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7085 | } |
| 7086 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7087 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7088 | private boolean handleAppCrashLocked(ProcessRecord app) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7089 | long now = SystemClock.uptimeMillis(); |
| 7090 | |
| 7091 | Long crashTime = mProcessCrashTimes.get(app.info.processName, |
| 7092 | app.info.uid); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7093 | if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7094 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7095 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7096 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7097 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7098 | app.info.processName, app.info.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7099 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 7100 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7101 | if (r.app == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7102 | Slog.w(TAG, " Force finishing activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7103 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7104 | r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, null, "crashed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7105 | } |
| 7106 | } |
| 7107 | if (!app.persistent) { |
| 7108 | // We don't want to start this process again until the user |
| 7109 | // explicitly does so... but for persistent process, we really |
| 7110 | // need to keep it running. If a persistent process is actually |
| 7111 | // repeatedly crashing, then badness for everyone. |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7112 | EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.info.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7113 | app.info.processName); |
| 7114 | mBadProcesses.put(app.info.processName, app.info.uid, now); |
| 7115 | app.bad = true; |
| 7116 | mProcessCrashTimes.remove(app.info.processName, app.info.uid); |
| 7117 | app.removed = true; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 7118 | // Don't let services in this process be restarted and potentially |
| 7119 | // annoy the user repeatedly. Unless it is persistent, since those |
| 7120 | // processes run critical code. |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7121 | removeProcessLocked(app, false, false, "crash"); |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7122 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7123 | return false; |
| 7124 | } |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7125 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7126 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7127 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7128 | if (r.app == app) { |
| 7129 | // If the top running activity is from this crashing |
| 7130 | // process, then terminate it to avoid getting in a loop. |
| 7131 | Slog.w(TAG, " Force finishing activity " |
| 7132 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 7133 | int index = mMainStack.indexOfActivityLocked(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7134 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7135 | Activity.RESULT_CANCELED, null, "crashed"); |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 7136 | // Also terminate any activities below it that aren't yet |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7137 | // stopped, to avoid a situation where one will get |
| 7138 | // re-start our crashing activity once it gets resumed again. |
| 7139 | index--; |
| 7140 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7141 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7142 | if (r.state == ActivityState.RESUMED |
| 7143 | || r.state == ActivityState.PAUSING |
| 7144 | || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 7145 | if (!r.isHomeActivity || mHomeProcess != r.app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7146 | Slog.w(TAG, " Force finishing activity " |
| 7147 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7148 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7149 | Activity.RESULT_CANCELED, null, "crashed"); |
| 7150 | } |
| 7151 | } |
| 7152 | } |
| 7153 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7154 | } |
| 7155 | |
| 7156 | // Bump up the crash count of any services currently running in the proc. |
| 7157 | if (app.services.size() != 0) { |
| 7158 | // Any services running in the application need to be placed |
| 7159 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7160 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7161 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7162 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7163 | sr.crashCount++; |
| 7164 | } |
| 7165 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7166 | |
| 7167 | // If the crashing process is what we consider to be the "home process" and it has been |
| 7168 | // replaced by a third-party app, clear the package preferred activities from packages |
| 7169 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 7170 | // from blocking the user to manually clear the list. |
| 7171 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 7172 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7173 | Iterator it = mHomeProcess.activities.iterator(); |
| 7174 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 7175 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7176 | if (r.isHomeActivity) { |
| 7177 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 7178 | try { |
| 7179 | ActivityThread.getPackageManager() |
| 7180 | .clearPackagePreferredActivities(r.packageName); |
| 7181 | } catch (RemoteException c) { |
| 7182 | // pm is in same process, this will never happen. |
| 7183 | } |
| 7184 | } |
| 7185 | } |
| 7186 | } |
| 7187 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7188 | mProcessCrashTimes.put(app.info.processName, app.info.uid, now); |
| 7189 | return true; |
| 7190 | } |
| 7191 | |
| 7192 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 7193 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 7194 | mContext, app.info.packageName, app.info.flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7195 | skipCurrentReceiverLocked(app); |
| 7196 | } |
| 7197 | |
| 7198 | void skipCurrentReceiverLocked(ProcessRecord app) { |
| 7199 | boolean reschedule = false; |
| 7200 | BroadcastRecord r = app.curReceiver; |
| 7201 | if (r != null) { |
| 7202 | // The current broadcast is waiting for this app's receiver |
| 7203 | // to be finished. Looks like that's not going to happen, so |
| 7204 | // let the broadcast continue. |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 7205 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7206 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 7207 | r.resultExtras, r.resultAbort, true); |
| 7208 | reschedule = true; |
| 7209 | } |
| 7210 | r = mPendingBroadcast; |
| 7211 | if (r != null && r.curApp == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7212 | if (DEBUG_BROADCAST) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7213 | "skip & discard pending app " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 7214 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7215 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 7216 | r.resultExtras, r.resultAbort, true); |
| 7217 | reschedule = true; |
| 7218 | } |
| 7219 | if (reschedule) { |
| 7220 | scheduleBroadcastsLocked(); |
| 7221 | } |
| 7222 | } |
| 7223 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7224 | /** |
| 7225 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 7226 | * The application process will exit immediately after this call returns. |
| 7227 | * @param app object of the crashing app, null for the system server |
| 7228 | * @param crashInfo describing the exception |
| 7229 | */ |
| 7230 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7231 | ProcessRecord r = findAppProcess(app, "Crash"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7232 | final String processName = app == null ? "system_server" |
| 7233 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7234 | |
| 7235 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7236 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7237 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7238 | crashInfo.exceptionClassName, |
| 7239 | crashInfo.exceptionMessage, |
| 7240 | crashInfo.throwFileName, |
| 7241 | crashInfo.throwLineNumber); |
| 7242 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7243 | addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7244 | |
| 7245 | crashApplication(r, crashInfo); |
| 7246 | } |
| 7247 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7248 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7249 | IBinder app, |
| 7250 | int violationMask, |
| 7251 | StrictMode.ViolationInfo info) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7252 | ProcessRecord r = findAppProcess(app, "StrictMode"); |
| 7253 | if (r == null) { |
| 7254 | return; |
| 7255 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7256 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7257 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 7258 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7259 | boolean logIt = true; |
| 7260 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 7261 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 7262 | logIt = false; |
| 7263 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7264 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7265 | // the relative pain numbers, without logging all |
| 7266 | // the stack traces repeatedly. We'd want to do |
| 7267 | // likewise in the client code, which also does |
| 7268 | // dup suppression, before the Binder call. |
| 7269 | } else { |
| 7270 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 7271 | mAlreadyLoggedViolatedStacks.clear(); |
| 7272 | } |
| 7273 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 7274 | } |
| 7275 | } |
| 7276 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7277 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7278 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7279 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7280 | |
| 7281 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 7282 | AppErrorResult result = new AppErrorResult(); |
| 7283 | synchronized (this) { |
| 7284 | final long origId = Binder.clearCallingIdentity(); |
| 7285 | |
| 7286 | Message msg = Message.obtain(); |
| 7287 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 7288 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 7289 | data.put("result", result); |
| 7290 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7291 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7292 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7293 | msg.obj = data; |
| 7294 | mHandler.sendMessage(msg); |
| 7295 | |
| 7296 | Binder.restoreCallingIdentity(origId); |
| 7297 | } |
| 7298 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 7299 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7300 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7301 | } |
| 7302 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7303 | // Depending on the policy in effect, there could be a bunch of |
| 7304 | // these in quick succession so we try to batch these together to |
| 7305 | // minimize disk writes, number of dropbox entries, and maximize |
| 7306 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7307 | private void logStrictModeViolationToDropBox( |
| 7308 | ProcessRecord process, |
| 7309 | StrictMode.ViolationInfo info) { |
| 7310 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7311 | return; |
| 7312 | } |
| 7313 | final boolean isSystemApp = process == null || |
| 7314 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 7315 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7316 | final String processName = process == null ? "unknown" : process.processName; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7317 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 7318 | final DropBoxManager dbox = (DropBoxManager) |
| 7319 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7320 | |
| 7321 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7322 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7323 | |
| 7324 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7325 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7326 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 7327 | synchronized (sb) { |
| 7328 | bufferWasEmpty = sb.length() == 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7329 | appendDropBoxProcessHeaders(process, processName, sb); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7330 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 7331 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7332 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 7333 | if (info.violationNumThisLoop != 0) { |
| 7334 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 7335 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 7336 | if (info.numAnimationsRunning != 0) { |
| 7337 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 7338 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 7339 | if (info.broadcastIntentAction != null) { |
| 7340 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 7341 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7342 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7343 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7344 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 7345 | if (info.numInstances != -1) { |
| 7346 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 7347 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7348 | if (info.tags != null) { |
| 7349 | for (String tag : info.tags) { |
| 7350 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 7351 | } |
| 7352 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7353 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7354 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 7355 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7356 | } |
| 7357 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7358 | |
| 7359 | // Only buffer up to ~64k. Various logging bits truncate |
| 7360 | // things at 128k. |
| 7361 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7362 | } |
| 7363 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7364 | // Flush immediately if the buffer's grown too large, or this |
| 7365 | // is a non-system app. Non-system apps are isolated with a |
| 7366 | // different tag & policy and not batched. |
| 7367 | // |
| 7368 | // Batching is useful during internal testing with |
| 7369 | // StrictMode settings turned up high. Without batching, |
| 7370 | // thousands of separate files could be created on boot. |
| 7371 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7372 | new Thread("Error dump: " + dropboxTag) { |
| 7373 | @Override |
| 7374 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7375 | String report; |
| 7376 | synchronized (sb) { |
| 7377 | report = sb.toString(); |
| 7378 | sb.delete(0, sb.length()); |
| 7379 | sb.trimToSize(); |
| 7380 | } |
| 7381 | if (report.length() != 0) { |
| 7382 | dbox.addText(dropboxTag, report); |
| 7383 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7384 | } |
| 7385 | }.start(); |
| 7386 | return; |
| 7387 | } |
| 7388 | |
| 7389 | // System app batching: |
| 7390 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7391 | // An existing dropbox-writing thread is outstanding, so |
| 7392 | // we don't need to start it up. The existing thread will |
| 7393 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7394 | return; |
| 7395 | } |
| 7396 | |
| 7397 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 7398 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7399 | new Thread("Error dump: " + dropboxTag) { |
| 7400 | @Override |
| 7401 | public void run() { |
| 7402 | // 5 second sleep to let stacks arrive and be batched together |
| 7403 | try { |
| 7404 | Thread.sleep(5000); // 5 seconds |
| 7405 | } catch (InterruptedException e) {} |
| 7406 | |
| 7407 | String errorReport; |
| 7408 | synchronized (mStrictModeBuffer) { |
| 7409 | errorReport = mStrictModeBuffer.toString(); |
| 7410 | if (errorReport.length() == 0) { |
| 7411 | return; |
| 7412 | } |
| 7413 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 7414 | mStrictModeBuffer.trimToSize(); |
| 7415 | } |
| 7416 | dbox.addText(dropboxTag, errorReport); |
| 7417 | } |
| 7418 | }.start(); |
| 7419 | } |
| 7420 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7421 | /** |
| 7422 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 7423 | * @param app object of the crashing app, null for the system server |
| 7424 | * @param tag reported by the caller |
| 7425 | * @param crashInfo describing the context of the error |
| 7426 | * @return true if the process should exit immediately (WTF is fatal) |
| 7427 | */ |
| 7428 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7429 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7430 | ProcessRecord r = findAppProcess(app, "WTF"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7431 | final String processName = app == null ? "system_server" |
| 7432 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7433 | |
| 7434 | EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7435 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7436 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7437 | tag, crashInfo.exceptionMessage); |
| 7438 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7439 | addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7440 | |
Dianne Hackborn | 1ab4377 | 2011-03-15 14:38:02 -0700 | [diff] [blame] | 7441 | if (r != null && r.pid != Process.myPid() && |
| 7442 | Settings.Secure.getInt(mContext.getContentResolver(), |
| 7443 | Settings.Secure.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7444 | crashApplication(r, crashInfo); |
| 7445 | return true; |
| 7446 | } else { |
| 7447 | return false; |
| 7448 | } |
| 7449 | } |
| 7450 | |
| 7451 | /** |
| 7452 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 7453 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 7454 | */ |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7455 | private ProcessRecord findAppProcess(IBinder app, String reason) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7456 | if (app == null) { |
| 7457 | return null; |
| 7458 | } |
| 7459 | |
| 7460 | synchronized (this) { |
| 7461 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 7462 | final int NA = apps.size(); |
| 7463 | for (int ia=0; ia<NA; ia++) { |
| 7464 | ProcessRecord p = apps.valueAt(ia); |
| 7465 | if (p.thread != null && p.thread.asBinder() == app) { |
| 7466 | return p; |
| 7467 | } |
| 7468 | } |
| 7469 | } |
| 7470 | |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7471 | Slog.w(TAG, "Can't find mystery application for " + reason |
| 7472 | + " from pid=" + Binder.getCallingPid() |
| 7473 | + " uid=" + Binder.getCallingUid() + ": " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7474 | return null; |
| 7475 | } |
| 7476 | } |
| 7477 | |
| 7478 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7479 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 7480 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7481 | */ |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7482 | private void appendDropBoxProcessHeaders(ProcessRecord process, String processName, |
| 7483 | StringBuilder sb) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 7484 | // Watchdog thread ends up invoking this function (with |
| 7485 | // a null ProcessRecord) to add the stack file to dropbox. |
| 7486 | // Do not acquire a lock on this (am) in such cases, as it |
| 7487 | // could cause a potential deadlock, if and when watchdog |
| 7488 | // is invoked due to unavailability of lock on am and it |
| 7489 | // would prevent watchdog from killing system_server. |
| 7490 | if (process == null) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7491 | sb.append("Process: ").append(processName).append("\n"); |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 7492 | return; |
| 7493 | } |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 7494 | // Note: ProcessRecord 'process' is guarded by the service |
| 7495 | // instance. (notably process.pkgList, which could otherwise change |
| 7496 | // concurrently during execution of this method) |
| 7497 | synchronized (this) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7498 | sb.append("Process: ").append(processName).append("\n"); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7499 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7500 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7501 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 7502 | for (String pkg : process.pkgList) { |
| 7503 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7504 | try { |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7505 | PackageInfo pi = pm.getPackageInfo(pkg, 0); |
| 7506 | if (pi != null) { |
| 7507 | sb.append(" v").append(pi.versionCode); |
| 7508 | if (pi.versionName != null) { |
| 7509 | sb.append(" (").append(pi.versionName).append(")"); |
| 7510 | } |
| 7511 | } |
| 7512 | } catch (RemoteException e) { |
| 7513 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7514 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7515 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7516 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7517 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7518 | } |
| 7519 | |
| 7520 | private static String processClass(ProcessRecord process) { |
| 7521 | if (process == null || process.pid == MY_PID) { |
| 7522 | return "system_server"; |
| 7523 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7524 | return "system_app"; |
| 7525 | } else { |
| 7526 | return "data_app"; |
| 7527 | } |
| 7528 | } |
| 7529 | |
| 7530 | /** |
| 7531 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 7532 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 7533 | * @param process which caused the error, null means the system server |
| 7534 | * @param activity which triggered the error, null if unknown |
| 7535 | * @param parent activity related to the error, null if unknown |
| 7536 | * @param subject line related to the error, null if absent |
| 7537 | * @param report in long form describing the error, null if absent |
| 7538 | * @param logFile to include in the report, null if none |
| 7539 | * @param crashInfo giving an application stack trace, null if absent |
| 7540 | */ |
| 7541 | public void addErrorToDropBox(String eventType, |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7542 | ProcessRecord process, String processName, ActivityRecord activity, |
| 7543 | ActivityRecord parent, String subject, |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7544 | final String report, final File logFile, |
| 7545 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 7546 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 7547 | // otherwise the watchdog may be prevented from resetting the system. |
| 7548 | |
| 7549 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 7550 | final DropBoxManager dbox = (DropBoxManager) |
| 7551 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7552 | |
| 7553 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7554 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7555 | |
| 7556 | final StringBuilder sb = new StringBuilder(1024); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7557 | appendDropBoxProcessHeaders(process, processName, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7558 | if (activity != null) { |
| 7559 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 7560 | } |
| 7561 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 7562 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 7563 | } |
| 7564 | if (parent != null && parent != activity) { |
| 7565 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 7566 | } |
| 7567 | if (subject != null) { |
| 7568 | sb.append("Subject: ").append(subject).append("\n"); |
| 7569 | } |
| 7570 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 7571 | if (Debug.isDebuggerConnected()) { |
| 7572 | sb.append("Debugger: Connected\n"); |
| 7573 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7574 | sb.append("\n"); |
| 7575 | |
| 7576 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 7577 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7578 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 7579 | @Override |
| 7580 | public void run() { |
| 7581 | if (report != null) { |
| 7582 | sb.append(report); |
| 7583 | } |
| 7584 | if (logFile != null) { |
| 7585 | try { |
| 7586 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 7587 | } catch (IOException e) { |
| 7588 | Slog.e(TAG, "Error reading " + logFile, e); |
| 7589 | } |
| 7590 | } |
| 7591 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 7592 | sb.append(crashInfo.stackTrace); |
| 7593 | } |
| 7594 | |
| 7595 | String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 7596 | int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0); |
| 7597 | if (lines > 0) { |
| 7598 | sb.append("\n"); |
| 7599 | |
| 7600 | // Merge several logcat streams, and take the last N lines |
| 7601 | InputStreamReader input = null; |
| 7602 | try { |
| 7603 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 7604 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 7605 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 7606 | |
| 7607 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 7608 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 7609 | input = new InputStreamReader(logcat.getInputStream()); |
| 7610 | |
| 7611 | int num; |
| 7612 | char[] buf = new char[8192]; |
| 7613 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 7614 | } catch (IOException e) { |
| 7615 | Slog.e(TAG, "Error running logcat", e); |
| 7616 | } finally { |
| 7617 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 7618 | } |
| 7619 | } |
| 7620 | |
| 7621 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7622 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7623 | }; |
| 7624 | |
| 7625 | if (process == null || process.pid == MY_PID) { |
| 7626 | worker.run(); // We may be about to die -- need to run this synchronously |
| 7627 | } else { |
| 7628 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7629 | } |
| 7630 | } |
| 7631 | |
| 7632 | /** |
| 7633 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 7634 | * Deal with edge cases (intercepts from instrumented applications, |
| 7635 | * ActivityController, error intent receivers, that sort of thing). |
| 7636 | * @param r the application crashing |
| 7637 | * @param crashInfo describing the failure |
| 7638 | */ |
| 7639 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7640 | long timeMillis = System.currentTimeMillis(); |
| 7641 | String shortMsg = crashInfo.exceptionClassName; |
| 7642 | String longMsg = crashInfo.exceptionMessage; |
| 7643 | String stackTrace = crashInfo.stackTrace; |
| 7644 | if (shortMsg != null && longMsg != null) { |
| 7645 | longMsg = shortMsg + ": " + longMsg; |
| 7646 | } else if (shortMsg != null) { |
| 7647 | longMsg = shortMsg; |
| 7648 | } |
| 7649 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7650 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7651 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7652 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7653 | try { |
| 7654 | String name = r != null ? r.processName : null; |
| 7655 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7656 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7657 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7658 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7659 | + " at watcher's request"); |
| 7660 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7661 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7662 | } |
| 7663 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7664 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7665 | } |
| 7666 | } |
| 7667 | |
| 7668 | final long origId = Binder.clearCallingIdentity(); |
| 7669 | |
| 7670 | // If this process is running instrumentation, finish it. |
| 7671 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7672 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7673 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7674 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 7675 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7676 | Bundle info = new Bundle(); |
| 7677 | info.putString("shortMsg", shortMsg); |
| 7678 | info.putString("longMsg", longMsg); |
| 7679 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 7680 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7681 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7682 | } |
| 7683 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7684 | // If we can't identify the process or it's already exceeded its crash quota, |
| 7685 | // quit right away without showing a crash dialog. |
| 7686 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7687 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7688 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7689 | } |
| 7690 | |
| 7691 | Message msg = Message.obtain(); |
| 7692 | msg.what = SHOW_ERROR_MSG; |
| 7693 | HashMap data = new HashMap(); |
| 7694 | data.put("result", result); |
| 7695 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7696 | msg.obj = data; |
| 7697 | mHandler.sendMessage(msg); |
| 7698 | |
| 7699 | Binder.restoreCallingIdentity(origId); |
| 7700 | } |
| 7701 | |
| 7702 | int res = result.get(); |
| 7703 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7704 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7705 | synchronized (this) { |
| 7706 | if (r != null) { |
| 7707 | mProcessCrashTimes.put(r.info.processName, r.info.uid, |
| 7708 | SystemClock.uptimeMillis()); |
| 7709 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7710 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7711 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7712 | } |
| 7713 | } |
| 7714 | |
| 7715 | if (appErrorIntent != null) { |
| 7716 | try { |
| 7717 | mContext.startActivity(appErrorIntent); |
| 7718 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7719 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7720 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7721 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7722 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7723 | |
| 7724 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 7725 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 7726 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7727 | if (report == null) { |
| 7728 | return null; |
| 7729 | } |
| 7730 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 7731 | result.setComponent(r.errorReportReceiver); |
| 7732 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 7733 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 7734 | return result; |
| 7735 | } |
| 7736 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7737 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 7738 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7739 | if (r.errorReportReceiver == null) { |
| 7740 | return null; |
| 7741 | } |
| 7742 | |
| 7743 | if (!r.crashing && !r.notResponding) { |
| 7744 | return null; |
| 7745 | } |
| 7746 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7747 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 7748 | report.packageName = r.info.packageName; |
| 7749 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 7750 | report.processName = r.processName; |
| 7751 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 7752 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7753 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7754 | if (r.crashing) { |
| 7755 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 7756 | report.crashInfo = crashInfo; |
| 7757 | } else if (r.notResponding) { |
| 7758 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 7759 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7760 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7761 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 7762 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 7763 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7764 | } |
| 7765 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7766 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7767 | } |
| 7768 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7769 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
| 7770 | // assume our apps are happy - lazy create the list |
| 7771 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 7772 | |
| 7773 | synchronized (this) { |
| 7774 | |
| 7775 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7776 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 7777 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7778 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 7779 | // This one's in trouble, so we'll generate a report for it |
| 7780 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 7781 | ActivityManager.ProcessErrorStateInfo report = null; |
| 7782 | if (app.crashing) { |
| 7783 | report = app.crashingReport; |
| 7784 | } else if (app.notResponding) { |
| 7785 | report = app.notRespondingReport; |
| 7786 | } |
| 7787 | |
| 7788 | if (report != null) { |
| 7789 | if (errList == null) { |
| 7790 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 7791 | } |
| 7792 | errList.add(report); |
| 7793 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7794 | Slog.w(TAG, "Missing app error report, app = " + app.processName + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7795 | " crashing = " + app.crashing + |
| 7796 | " notResponding = " + app.notResponding); |
| 7797 | } |
| 7798 | } |
| 7799 | } |
| 7800 | } |
| 7801 | |
| 7802 | return errList; |
| 7803 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7804 | |
| 7805 | static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) { |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7806 | if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7807 | if (currApp != null) { |
| 7808 | currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
| 7809 | } |
| 7810 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7811 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 7812 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7813 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 7814 | if (currApp != null) { |
| 7815 | currApp.lru = 0; |
| 7816 | } |
| 7817 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7818 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7819 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 7820 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 7821 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 7822 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 7823 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 7824 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 7825 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 7826 | } else { |
| 7827 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 7828 | } |
| 7829 | } |
| 7830 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7831 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
| 7832 | // Lazy instantiation of list |
| 7833 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
| 7834 | synchronized (this) { |
| 7835 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7836 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 7837 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7838 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 7839 | // Generate process state info for running application |
| 7840 | ActivityManager.RunningAppProcessInfo currApp = |
| 7841 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 7842 | app.pid, app.getPackageList()); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 7843 | currApp.uid = app.info.uid; |
Dianne Hackborn | baf42c6 | 2010-06-24 11:23:39 -0700 | [diff] [blame] | 7844 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 482566e | 2010-09-03 12:51:28 -0700 | [diff] [blame] | 7845 | currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
Dianne Hackborn | baf42c6 | 2010-06-24 11:23:39 -0700 | [diff] [blame] | 7846 | } |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 7847 | if (app.persistent) { |
| 7848 | currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 7849 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7850 | int adj = app.curAdj; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7851 | currApp.importance = oomAdjToImportance(adj, currApp); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 7852 | currApp.importanceReasonCode = app.adjTypeCode; |
| 7853 | if (app.adjSource instanceof ProcessRecord) { |
| 7854 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7855 | currApp.importanceReasonImportance = oomAdjToImportance( |
| 7856 | app.adjSourceOom, null); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7857 | } else if (app.adjSource instanceof ActivityRecord) { |
| 7858 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 7859 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 7860 | } |
| 7861 | if (app.adjTarget instanceof ComponentName) { |
| 7862 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 7863 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7864 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7865 | // + " lru=" + currApp.lru); |
| 7866 | if (runList == null) { |
| 7867 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 7868 | } |
| 7869 | runList.add(currApp); |
| 7870 | } |
| 7871 | } |
| 7872 | } |
| 7873 | return runList; |
| 7874 | } |
| 7875 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7876 | public List<ApplicationInfo> getRunningExternalApplications() { |
| 7877 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 7878 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 7879 | if (runningApps != null && runningApps.size() > 0) { |
| 7880 | Set<String> extList = new HashSet<String>(); |
| 7881 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 7882 | if (app.pkgList != null) { |
| 7883 | for (String pkg : app.pkgList) { |
| 7884 | extList.add(pkg); |
| 7885 | } |
| 7886 | } |
| 7887 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7888 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7889 | for (String pkg : extList) { |
| 7890 | try { |
| 7891 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0); |
| 7892 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 7893 | retList.add(info); |
| 7894 | } |
| 7895 | } catch (RemoteException e) { |
| 7896 | } |
| 7897 | } |
| 7898 | } |
| 7899 | return retList; |
| 7900 | } |
| 7901 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7902 | @Override |
| 7903 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7904 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 7905 | != PackageManager.PERMISSION_GRANTED) { |
| 7906 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 7907 | + Binder.getCallingPid() |
| 7908 | + ", uid=" + Binder.getCallingUid() |
| 7909 | + " without permission " |
| 7910 | + android.Manifest.permission.DUMP); |
| 7911 | return; |
| 7912 | } |
| 7913 | |
| 7914 | boolean dumpAll = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7915 | boolean dumpClient = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 7916 | String dumpPackage = null; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7917 | |
| 7918 | int opti = 0; |
| 7919 | while (opti < args.length) { |
| 7920 | String opt = args[opti]; |
| 7921 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 7922 | break; |
| 7923 | } |
| 7924 | opti++; |
| 7925 | if ("-a".equals(opt)) { |
| 7926 | dumpAll = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7927 | } else if ("-c".equals(opt)) { |
| 7928 | dumpClient = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7929 | } else if ("-h".equals(opt)) { |
| 7930 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7931 | pw.println(" [-a] [-c] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7932 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7933 | pw.println(" a[ctivities]: activity stack state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 7934 | pw.println(" b[roadcasts] [PACKAGE_NAME]: broadcast state"); |
| 7935 | pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state"); |
| 7936 | pw.println(" p[rocesses] [PACKAGE_NAME]: process state"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7937 | pw.println(" o[om]: out of memory management"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 7938 | pw.println(" prov[iders] [COMP_SPEC ...]: content provider state"); |
| 7939 | pw.println(" s[ervices] [COMP_SPEC ...]: service state"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7940 | pw.println(" service [COMP_SPEC]: service client-side state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 7941 | pw.println(" package [PACKAGE_NAME]: all state related to given package"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 7942 | pw.println(" all: dump all activities"); |
| 7943 | pw.println(" top: dump the top activity"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7944 | pw.println(" cmd may also be a COMP_SPEC to dump activities."); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 7945 | pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),"); |
| 7946 | pw.println(" a partial substring in a component name, a"); |
| 7947 | pw.println(" hex object identifier."); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7948 | pw.println(" -a: include all available server state."); |
| 7949 | pw.println(" -c: include client state."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7950 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7951 | } else { |
| 7952 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7953 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7954 | } |
| 7955 | |
| 7956 | // Is the caller requesting to dump a particular piece of data? |
| 7957 | if (opti < args.length) { |
| 7958 | String cmd = args[opti]; |
| 7959 | opti++; |
| 7960 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 7961 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 7962 | dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7963 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7964 | return; |
| 7965 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 7966 | String[] newArgs; |
| 7967 | String name; |
| 7968 | if (opti >= args.length) { |
| 7969 | name = null; |
| 7970 | newArgs = EMPTY_STRING_ARRAY; |
| 7971 | } else { |
| 7972 | name = args[opti]; |
| 7973 | opti++; |
| 7974 | newArgs = new String[args.length - opti]; |
| 7975 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 7976 | args.length - opti); |
| 7977 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7978 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 7979 | dumpBroadcastsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7980 | } |
| 7981 | return; |
| 7982 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 7983 | String[] newArgs; |
| 7984 | String name; |
| 7985 | if (opti >= args.length) { |
| 7986 | name = null; |
| 7987 | newArgs = EMPTY_STRING_ARRAY; |
| 7988 | } else { |
| 7989 | name = args[opti]; |
| 7990 | opti++; |
| 7991 | newArgs = new String[args.length - opti]; |
| 7992 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 7993 | args.length - opti); |
| 7994 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7995 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 7996 | dumpPendingIntentsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7997 | } |
| 7998 | return; |
| 7999 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8000 | String[] newArgs; |
| 8001 | String name; |
| 8002 | if (opti >= args.length) { |
| 8003 | name = null; |
| 8004 | newArgs = EMPTY_STRING_ARRAY; |
| 8005 | } else { |
| 8006 | name = args[opti]; |
| 8007 | opti++; |
| 8008 | newArgs = new String[args.length - opti]; |
| 8009 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8010 | args.length - opti); |
| 8011 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8012 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8013 | dumpProcessesLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8014 | } |
| 8015 | return; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8016 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 8017 | synchronized (this) { |
| 8018 | dumpOomLocked(fd, pw, args, opti, true); |
| 8019 | } |
| 8020 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8021 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 8022 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8023 | dumpProvidersLocked(fd, pw, args, opti, true, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8024 | } |
| 8025 | return; |
| 8026 | } else if ("service".equals(cmd)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8027 | String[] newArgs; |
| 8028 | String name; |
| 8029 | if (opti >= args.length) { |
| 8030 | name = null; |
| 8031 | newArgs = EMPTY_STRING_ARRAY; |
| 8032 | } else { |
| 8033 | name = args[opti]; |
| 8034 | opti++; |
| 8035 | newArgs = new String[args.length - opti]; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8036 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8037 | args.length - opti); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8038 | } |
| 8039 | if (!dumpService(fd, pw, name, newArgs, 0, dumpAll)) { |
| 8040 | pw.println("No services match: " + name); |
| 8041 | pw.println("Use -h for help."); |
| 8042 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8043 | return; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8044 | } else if ("package".equals(cmd)) { |
| 8045 | String[] newArgs; |
| 8046 | if (opti >= args.length) { |
| 8047 | pw.println("package: no package name specified"); |
| 8048 | pw.println("Use -h for help."); |
| 8049 | return; |
| 8050 | } else { |
| 8051 | dumpPackage = args[opti]; |
| 8052 | opti++; |
| 8053 | newArgs = new String[args.length - opti]; |
| 8054 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8055 | args.length - opti); |
| 8056 | args = newArgs; |
| 8057 | opti = 0; |
| 8058 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8059 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 8060 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8061 | dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8062 | } |
| 8063 | return; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8064 | } else { |
| 8065 | // Dumping a single activity? |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8066 | if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 8067 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 8068 | pw.println("Use -h for help."); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8069 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8070 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8071 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8072 | } |
| 8073 | |
| 8074 | // No piece of data specified, dump everything. |
| 8075 | synchronized (this) { |
| 8076 | boolean needSep; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8077 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8078 | if (needSep) { |
| 8079 | pw.println(" "); |
| 8080 | } |
| 8081 | if (dumpAll) { |
| 8082 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8083 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8084 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8085 | if (needSep) { |
| 8086 | pw.println(" "); |
| 8087 | } |
| 8088 | if (dumpAll) { |
| 8089 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8090 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8091 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8092 | if (needSep) { |
| 8093 | pw.println(" "); |
| 8094 | } |
| 8095 | if (dumpAll) { |
| 8096 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8097 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8098 | needSep = dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8099 | if (needSep) { |
| 8100 | pw.println(" "); |
| 8101 | } |
| 8102 | if (dumpAll) { |
| 8103 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8104 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8105 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8106 | if (needSep) { |
| 8107 | pw.println(" "); |
| 8108 | } |
| 8109 | if (dumpAll) { |
| 8110 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8111 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8112 | dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8113 | } |
| 8114 | } |
| 8115 | |
| 8116 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8117 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8118 | pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)"); |
| 8119 | pw.println(" Main stack:"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8120 | dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient, |
| 8121 | dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8122 | pw.println(" "); |
| 8123 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8124 | dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false, |
| 8125 | dumpPackage); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8126 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8127 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8128 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8129 | dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8130 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8131 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8132 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8133 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8134 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8135 | dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8136 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8137 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 8138 | if (mMainStack.mGoingToSleepActivities.size() > 0) { |
| 8139 | pw.println(" "); |
| 8140 | pw.println(" Activities waiting to sleep:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8141 | dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8142 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 8143 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8144 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8145 | pw.println(" "); |
| 8146 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8147 | dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8148 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8149 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8150 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8151 | pw.println(" "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8152 | if (mMainStack.mPausingActivity != null) { |
| 8153 | pw.println(" mPausingActivity: " + mMainStack.mPausingActivity); |
| 8154 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8155 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8156 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8157 | if (dumpAll) { |
| 8158 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
| 8159 | pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 8160 | pw.println(" mDismissKeyguardOnNextActivity: " |
| 8161 | + mMainStack.mDismissKeyguardOnNextActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8162 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8163 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8164 | if (mRecentTasks.size() > 0) { |
| 8165 | pw.println(); |
| 8166 | pw.println(" Recent tasks:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8167 | |
| 8168 | final int N = mRecentTasks.size(); |
| 8169 | for (int i=0; i<N; i++) { |
| 8170 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8171 | if (dumpPackage != null) { |
| 8172 | if (tr.realActivity == null || |
| 8173 | !dumpPackage.equals(tr.realActivity)) { |
| 8174 | continue; |
| 8175 | } |
| 8176 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8177 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 8178 | pw.println(tr); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8179 | if (dumpAll) { |
| 8180 | mRecentTasks.get(i).dump(pw, " "); |
| 8181 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8182 | } |
| 8183 | } |
| 8184 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8185 | if (dumpAll) { |
| 8186 | pw.println(" "); |
| 8187 | pw.println(" mCurTask: " + mCurTask); |
| 8188 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8189 | |
| 8190 | return true; |
| 8191 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8192 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8193 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8194 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8195 | boolean needSep = false; |
| 8196 | int numPers = 0; |
| 8197 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8198 | pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)"); |
| 8199 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8200 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8201 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 8202 | final int NA = procs.size(); |
| 8203 | for (int ia=0; ia<NA; ia++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8204 | ProcessRecord r = procs.valueAt(ia); |
| 8205 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8206 | continue; |
| 8207 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8208 | if (!needSep) { |
| 8209 | pw.println(" All known processes:"); |
| 8210 | needSep = true; |
| 8211 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8212 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 8213 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 8214 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8215 | r.dump(pw, " "); |
| 8216 | if (r.persistent) { |
| 8217 | numPers++; |
| 8218 | } |
| 8219 | } |
| 8220 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8221 | } |
| 8222 | |
| 8223 | if (mLruProcesses.size() > 0) { |
| 8224 | if (needSep) pw.println(" "); |
| 8225 | needSep = true; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8226 | pw.println(" Process LRU list (sorted by oom_adj):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8227 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8228 | "Proc", "PERS", false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8229 | needSep = true; |
| 8230 | } |
| 8231 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8232 | if (dumpAll) { |
| 8233 | synchronized (mPidsSelfLocked) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8234 | boolean printed = false; |
| 8235 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 8236 | ProcessRecord r = mPidsSelfLocked.valueAt(i); |
| 8237 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8238 | continue; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8239 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8240 | if (!printed) { |
| 8241 | if (needSep) pw.println(" "); |
| 8242 | needSep = true; |
| 8243 | pw.println(" PID mappings:"); |
| 8244 | printed = true; |
| 8245 | } |
| 8246 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 8247 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8248 | } |
| 8249 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8250 | } |
| 8251 | |
| 8252 | if (mForegroundProcesses.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8253 | synchronized (mPidsSelfLocked) { |
| 8254 | boolean printed = false; |
| 8255 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 8256 | ProcessRecord r = mPidsSelfLocked.get( |
| 8257 | mForegroundProcesses.valueAt(i).pid); |
| 8258 | if (dumpPackage != null && (r == null |
| 8259 | || !dumpPackage.equals(r.info.packageName))) { |
| 8260 | continue; |
| 8261 | } |
| 8262 | if (!printed) { |
| 8263 | if (needSep) pw.println(" "); |
| 8264 | needSep = true; |
| 8265 | pw.println(" Foreground Processes:"); |
| 8266 | printed = true; |
| 8267 | } |
| 8268 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 8269 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
| 8270 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8271 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8272 | } |
| 8273 | |
| 8274 | if (mPersistentStartingProcesses.size() > 0) { |
| 8275 | if (needSep) pw.println(" "); |
| 8276 | needSep = true; |
| 8277 | pw.println(" Persisent processes that are starting:"); |
| 8278 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8279 | "Starting Norm", "Restarting PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8280 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8281 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8282 | if (mRemovedProcesses.size() > 0) { |
| 8283 | if (needSep) pw.println(" "); |
| 8284 | needSep = true; |
| 8285 | pw.println(" Processes that are being removed:"); |
| 8286 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8287 | "Removed Norm", "Removed PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8288 | } |
| 8289 | |
| 8290 | if (mProcessesOnHold.size() > 0) { |
| 8291 | if (needSep) pw.println(" "); |
| 8292 | needSep = true; |
| 8293 | pw.println(" Processes that are on old until the system is ready:"); |
| 8294 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8295 | "OnHold Norm", "OnHold PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8296 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8297 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8298 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8299 | |
| 8300 | if (mProcessCrashTimes.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8301 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8302 | long now = SystemClock.uptimeMillis(); |
| 8303 | for (Map.Entry<String, SparseArray<Long>> procs |
| 8304 | : mProcessCrashTimes.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8305 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8306 | SparseArray<Long> uids = procs.getValue(); |
| 8307 | final int N = uids.size(); |
| 8308 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8309 | int puid = uids.keyAt(i); |
| 8310 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 8311 | if (dumpPackage != null && (r == null |
| 8312 | || !dumpPackage.equals(r.info.packageName))) { |
| 8313 | continue; |
| 8314 | } |
| 8315 | if (!printed) { |
| 8316 | if (needSep) pw.println(" "); |
| 8317 | needSep = true; |
| 8318 | pw.println(" Time since processes crashed:"); |
| 8319 | printed = true; |
| 8320 | } |
| 8321 | pw.print(" Process "); pw.print(pname); |
| 8322 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8323 | pw.print(": last crashed "); |
| 8324 | pw.print((now-uids.valueAt(i))); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 8325 | pw.println(" ms ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 8326 | } |
| 8327 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8328 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8329 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8330 | if (mBadProcesses.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8331 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8332 | for (Map.Entry<String, SparseArray<Long>> procs |
| 8333 | : mBadProcesses.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8334 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8335 | SparseArray<Long> uids = procs.getValue(); |
| 8336 | final int N = uids.size(); |
| 8337 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8338 | int puid = uids.keyAt(i); |
| 8339 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 8340 | if (dumpPackage != null && (r == null |
| 8341 | || !dumpPackage.equals(r.info.packageName))) { |
| 8342 | continue; |
| 8343 | } |
| 8344 | if (!printed) { |
| 8345 | if (needSep) pw.println(" "); |
| 8346 | needSep = true; |
| 8347 | pw.println(" Bad processes:"); |
| 8348 | } |
| 8349 | pw.print(" Bad process "); pw.print(pname); |
| 8350 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8351 | pw.print(": crashed at time "); |
| 8352 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8353 | } |
| 8354 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8355 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8356 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8357 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8358 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 8359 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8360 | if (mHeavyWeightProcess != null) { |
| 8361 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 8362 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8363 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8364 | if (dumpAll) { |
| 8365 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | 3d0724d | 2011-05-12 15:39:41 -0700 | [diff] [blame] | 8366 | if (mCompatModePackages.getPackages().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8367 | boolean printed = false; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 8368 | for (Map.Entry<String, Integer> entry |
| 8369 | : mCompatModePackages.getPackages().entrySet()) { |
| 8370 | String pkg = entry.getKey(); |
| 8371 | int mode = entry.getValue(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8372 | if (dumpPackage != null && !dumpPackage.equals(pkg)) { |
| 8373 | continue; |
| 8374 | } |
| 8375 | if (!printed) { |
| 8376 | pw.println(" mScreenCompatPackages:"); |
| 8377 | printed = true; |
| 8378 | } |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 8379 | pw.print(" "); pw.print(pkg); pw.print(": "); |
| 8380 | pw.print(mode); pw.println(); |
| 8381 | } |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 8382 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8383 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8384 | pw.println(" mSleeping=" + mSleeping + " mShuttingDown=" + mShuttingDown); |
| 8385 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 8386 | || mOrigWaitForDebugger) { |
| 8387 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 8388 | + " mDebugTransient=" + mDebugTransient |
| 8389 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 8390 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 8391 | if (mProfileApp != null || mProfileProc != null || mProfileFile != null |
| 8392 | || mProfileFd != null) { |
| 8393 | pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc); |
| 8394 | pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd); |
| 8395 | pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler=" |
| 8396 | + mAutoStopProfiler); |
| 8397 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8398 | if (mAlwaysFinishActivities || mController != null) { |
| 8399 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 8400 | + " mController=" + mController); |
| 8401 | } |
| 8402 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8403 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8404 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 8405 | + " mProcessesReady=" + mProcessesReady |
| 8406 | + " mSystemReady=" + mSystemReady); |
| 8407 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8408 | + " mBooted=" + mBooted |
| 8409 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8410 | pw.print(" mLastPowerCheckRealtime="); |
| 8411 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 8412 | pw.println(""); |
| 8413 | pw.print(" mLastPowerCheckUptime="); |
| 8414 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 8415 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8416 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 8417 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 8418 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8419 | pw.println(" mNumServiceProcs=" + mNumServiceProcs |
| 8420 | + " mNewNumServiceProcs=" + mNewNumServiceProcs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8421 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8422 | |
| 8423 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8424 | } |
| 8425 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8426 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8427 | int opti, boolean needSep, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8428 | if (mProcessesToGc.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8429 | boolean printed = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8430 | long now = SystemClock.uptimeMillis(); |
| 8431 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 8432 | ProcessRecord proc = mProcessesToGc.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8433 | if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) { |
| 8434 | continue; |
| 8435 | } |
| 8436 | if (!printed) { |
| 8437 | if (needSep) pw.println(" "); |
| 8438 | needSep = true; |
| 8439 | pw.println(" Processes that are waiting to GC:"); |
| 8440 | printed = true; |
| 8441 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8442 | pw.print(" Process "); pw.println(proc); |
| 8443 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 8444 | pw.print(", last gced="); |
| 8445 | pw.print(now-proc.lastRequestedGc); |
| 8446 | pw.print(" ms ago, last lowMem="); |
| 8447 | pw.print(now-proc.lastLowMemory); |
| 8448 | pw.println(" ms ago"); |
| 8449 | |
| 8450 | } |
| 8451 | } |
| 8452 | return needSep; |
| 8453 | } |
| 8454 | |
| 8455 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8456 | int opti, boolean dumpAll) { |
| 8457 | boolean needSep = false; |
| 8458 | |
| 8459 | if (mLruProcesses.size() > 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8460 | if (needSep) pw.println(" "); |
| 8461 | needSep = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 8462 | pw.println(" OOM levels:"); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8463 | pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8464 | pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8465 | pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ); |
| 8466 | pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ); |
| 8467 | pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ); |
| 8468 | pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 8469 | pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8470 | pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8471 | pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 8472 | pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8473 | pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8474 | pw.print(" HIDDEN_APP_MIN_ADJ: "); pw.println(ProcessList.HIDDEN_APP_MIN_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8475 | pw.print(" HIDDEN_APP_MAX_ADJ: "); pw.println(ProcessList.HIDDEN_APP_MAX_ADJ); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 8476 | |
| 8477 | if (needSep) pw.println(" "); |
| 8478 | needSep = true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8479 | pw.println(" Process OOM control:"); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8480 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8481 | "Proc", "PERS", true, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8482 | needSep = true; |
| 8483 | } |
| 8484 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8485 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8486 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8487 | pw.println(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8488 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 8489 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8490 | if (mHeavyWeightProcess != null) { |
| 8491 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 8492 | } |
| 8493 | |
| 8494 | return true; |
| 8495 | } |
| 8496 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8497 | /** |
| 8498 | * There are three ways to call this: |
| 8499 | * - no service specified: dump all the services |
| 8500 | * - a flattened component name that matched an existing service was specified as the |
| 8501 | * first arg: dump that one service |
| 8502 | * - the first arg isn't the flattened component name of an existing service: |
| 8503 | * dump all services whose component contains the first arg as a substring |
| 8504 | */ |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8505 | protected boolean dumpService(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 8506 | int opti, boolean dumpAll) { |
| 8507 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8508 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8509 | if ("all".equals(name)) { |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 8510 | synchronized (this) { |
| 8511 | for (ServiceRecord r1 : mServices.values()) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8512 | services.add(r1); |
| 8513 | } |
| 8514 | } |
| 8515 | } else { |
| 8516 | ComponentName componentName = name != null |
| 8517 | ? ComponentName.unflattenFromString(name) : null; |
| 8518 | int objectId = 0; |
| 8519 | if (componentName == null) { |
| 8520 | // Not a '/' separated full component name; maybe an object ID? |
| 8521 | try { |
| 8522 | objectId = Integer.parseInt(name, 16); |
| 8523 | name = null; |
| 8524 | componentName = null; |
| 8525 | } catch (RuntimeException e) { |
| 8526 | } |
| 8527 | } |
| 8528 | |
| 8529 | synchronized (this) { |
| 8530 | for (ServiceRecord r1 : mServices.values()) { |
| 8531 | if (componentName != null) { |
| 8532 | if (r1.name.equals(componentName)) { |
| 8533 | services.add(r1); |
| 8534 | } |
| 8535 | } else if (name != null) { |
| 8536 | if (r1.name.flattenToString().contains(name)) { |
| 8537 | services.add(r1); |
| 8538 | } |
| 8539 | } else if (System.identityHashCode(r1) == objectId) { |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 8540 | services.add(r1); |
| 8541 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8542 | } |
| 8543 | } |
| 8544 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8545 | |
| 8546 | if (services.size() <= 0) { |
| 8547 | return false; |
| 8548 | } |
| 8549 | |
| 8550 | boolean needSep = false; |
| 8551 | for (int i=0; i<services.size(); i++) { |
| 8552 | if (needSep) { |
| 8553 | pw.println(); |
| 8554 | } |
| 8555 | needSep = true; |
| 8556 | dumpService("", fd, pw, services.get(i), args, dumpAll); |
| 8557 | } |
| 8558 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8559 | } |
| 8560 | |
| 8561 | /** |
| 8562 | * Invokes IApplicationThread.dumpService() on the thread of the specified service if |
| 8563 | * there is a thread associated with the service. |
| 8564 | */ |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8565 | private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw, |
| 8566 | final ServiceRecord r, String[] args, boolean dumpAll) { |
| 8567 | String innerPrefix = prefix + " "; |
| 8568 | synchronized (this) { |
| 8569 | pw.print(prefix); pw.print("SERVICE "); |
| 8570 | pw.print(r.shortName); pw.print(" "); |
| 8571 | pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 8572 | pw.print(" pid="); |
| 8573 | if (r.app != null) pw.println(r.app.pid); |
| 8574 | else pw.println("(not running)"); |
| 8575 | if (dumpAll) { |
| 8576 | r.dump(pw, innerPrefix); |
| 8577 | } |
| 8578 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8579 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8580 | pw.print(prefix); pw.println(" Client:"); |
| 8581 | pw.flush(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8582 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8583 | TransferPipe tp = new TransferPipe(); |
| 8584 | try { |
| 8585 | r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), r, args); |
| 8586 | tp.setBufferPrefix(prefix + " "); |
| 8587 | tp.go(fd); |
| 8588 | } finally { |
| 8589 | tp.kill(); |
| 8590 | } |
| 8591 | } catch (IOException e) { |
| 8592 | pw.println(prefix + " Failure while dumping the service: " + e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8593 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8594 | pw.println(prefix + " Got a RemoteException while dumping the service"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8595 | } |
| 8596 | } |
| 8597 | } |
| 8598 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8599 | static class ItemMatcher { |
| 8600 | ArrayList<ComponentName> components; |
| 8601 | ArrayList<String> strings; |
| 8602 | ArrayList<Integer> objects; |
| 8603 | boolean all; |
| 8604 | |
| 8605 | ItemMatcher() { |
| 8606 | all = true; |
| 8607 | } |
| 8608 | |
| 8609 | void build(String name) { |
| 8610 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 8611 | if (componentName != null) { |
| 8612 | if (components == null) { |
| 8613 | components = new ArrayList<ComponentName>(); |
| 8614 | } |
| 8615 | components.add(componentName); |
| 8616 | all = false; |
| 8617 | } else { |
| 8618 | int objectId = 0; |
| 8619 | // Not a '/' separated full component name; maybe an object ID? |
| 8620 | try { |
| 8621 | objectId = Integer.parseInt(name, 16); |
| 8622 | if (objects == null) { |
| 8623 | objects = new ArrayList<Integer>(); |
| 8624 | } |
| 8625 | objects.add(objectId); |
| 8626 | all = false; |
| 8627 | } catch (RuntimeException e) { |
| 8628 | // Not an integer; just do string match. |
| 8629 | if (strings == null) { |
| 8630 | strings = new ArrayList<String>(); |
| 8631 | } |
| 8632 | strings.add(name); |
| 8633 | all = false; |
| 8634 | } |
| 8635 | } |
| 8636 | } |
| 8637 | |
| 8638 | int build(String[] args, int opti) { |
| 8639 | for (; opti<args.length; opti++) { |
| 8640 | String name = args[opti]; |
| 8641 | if ("--".equals(name)) { |
| 8642 | return opti+1; |
| 8643 | } |
| 8644 | build(name); |
| 8645 | } |
| 8646 | return opti; |
| 8647 | } |
| 8648 | |
| 8649 | boolean match(Object object, ComponentName comp) { |
| 8650 | if (all) { |
| 8651 | return true; |
| 8652 | } |
| 8653 | if (components != null) { |
| 8654 | for (int i=0; i<components.size(); i++) { |
| 8655 | if (components.get(i).equals(comp)) { |
| 8656 | return true; |
| 8657 | } |
| 8658 | } |
| 8659 | } |
| 8660 | if (objects != null) { |
| 8661 | for (int i=0; i<objects.size(); i++) { |
| 8662 | if (System.identityHashCode(object) == objects.get(i)) { |
| 8663 | return true; |
| 8664 | } |
| 8665 | } |
| 8666 | } |
| 8667 | if (strings != null) { |
| 8668 | String flat = comp.flattenToString(); |
| 8669 | for (int i=0; i<strings.size(); i++) { |
| 8670 | if (flat.contains(strings.get(i))) { |
| 8671 | return true; |
| 8672 | } |
| 8673 | } |
| 8674 | } |
| 8675 | return false; |
| 8676 | } |
| 8677 | } |
| 8678 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8679 | /** |
| 8680 | * There are three things that cmd can be: |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8681 | * - a flattened component name that matches an existing activity |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8682 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 8683 | * dump all activity whose component contains the cmd as a substring |
| 8684 | * - A hex number of the ActivityRecord object instance. |
| 8685 | */ |
| 8686 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 8687 | int opti, boolean dumpAll) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8688 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8689 | |
| 8690 | if ("all".equals(name)) { |
| 8691 | synchronized (this) { |
| 8692 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8693 | activities.add(r1); |
| 8694 | } |
| 8695 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 8696 | } else if ("top".equals(name)) { |
| 8697 | synchronized (this) { |
| 8698 | final int N = mMainStack.mHistory.size(); |
| 8699 | if (N > 0) { |
| 8700 | activities.add((ActivityRecord)mMainStack.mHistory.get(N-1)); |
| 8701 | } |
| 8702 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8703 | } else { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8704 | ItemMatcher matcher = new ItemMatcher(); |
| 8705 | matcher.build(name); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8706 | |
| 8707 | synchronized (this) { |
| 8708 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8709 | if (matcher.match(r1, r1.intent.getComponent())) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8710 | activities.add(r1); |
| 8711 | } |
| 8712 | } |
| 8713 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8714 | } |
| 8715 | |
| 8716 | if (activities.size() <= 0) { |
| 8717 | return false; |
| 8718 | } |
| 8719 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8720 | String[] newArgs = new String[args.length - opti]; |
| 8721 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 8722 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8723 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8724 | boolean needSep = false; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8725 | for (int i=activities.size()-1; i>=0; i--) { |
| 8726 | ActivityRecord r = (ActivityRecord)activities.get(i); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8727 | if (needSep) { |
| 8728 | pw.println(); |
| 8729 | } |
| 8730 | needSep = true; |
| 8731 | synchronized (this) { |
| 8732 | if (lastTask != r.task) { |
| 8733 | lastTask = r.task; |
| 8734 | pw.print("TASK "); pw.print(lastTask.affinity); |
| 8735 | pw.print(" id="); pw.println(lastTask.taskId); |
| 8736 | if (dumpAll) { |
| 8737 | lastTask.dump(pw, " "); |
| 8738 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8739 | } |
| 8740 | } |
| 8741 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8742 | } |
| 8743 | return true; |
| 8744 | } |
| 8745 | |
| 8746 | /** |
| 8747 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 8748 | * there is a thread associated with the activity. |
| 8749 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8750 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8751 | final ActivityRecord r, String[] args, boolean dumpAll) { |
| 8752 | String innerPrefix = prefix + " "; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8753 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8754 | pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName); |
| 8755 | pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 8756 | pw.print(" pid="); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8757 | if (r.app != null) pw.println(r.app.pid); |
| 8758 | else pw.println("(not running)"); |
| 8759 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8760 | r.dump(pw, innerPrefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8761 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8762 | } |
| 8763 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8764 | // flush anything that is already in the PrintWriter since the thread is going |
| 8765 | // to write to the file descriptor directly |
| 8766 | pw.flush(); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8767 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8768 | TransferPipe tp = new TransferPipe(); |
| 8769 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 8770 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 8771 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8772 | tp.go(fd); |
| 8773 | } finally { |
| 8774 | tp.kill(); |
| 8775 | } |
| 8776 | } catch (IOException e) { |
| 8777 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8778 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8779 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8780 | } |
| 8781 | } |
| 8782 | } |
| 8783 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8784 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8785 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8786 | boolean needSep = false; |
| 8787 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8788 | pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8789 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8790 | if (mRegisteredReceivers.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8791 | boolean printed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8792 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 8793 | while (it.hasNext()) { |
| 8794 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8795 | if (dumpPackage != null && (r.app == null || |
| 8796 | !dumpPackage.equals(r.app.info.packageName))) { |
| 8797 | continue; |
| 8798 | } |
| 8799 | if (!printed) { |
| 8800 | pw.println(" Registered Receivers:"); |
| 8801 | needSep = true; |
| 8802 | printed = true; |
| 8803 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8804 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8805 | r.dump(pw, " "); |
| 8806 | } |
| 8807 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8808 | |
| 8809 | if (mReceiverResolver.dump(pw, needSep ? |
| 8810 | "\n Receiver Resolver Table:" : " Receiver Resolver Table:", |
| 8811 | " ", dumpPackage, false)) { |
| 8812 | needSep = true; |
| 8813 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8814 | } |
| 8815 | |
| 8816 | if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0 |
| 8817 | || mPendingBroadcast != null) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8818 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8819 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8820 | BroadcastRecord br = mParallelBroadcasts.get(i); |
| 8821 | if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { |
| 8822 | continue; |
| 8823 | } |
| 8824 | if (!printed) { |
| 8825 | if (needSep) { |
| 8826 | pw.println(); |
| 8827 | } |
| 8828 | needSep = true; |
| 8829 | pw.println(" Active broadcasts:"); |
| 8830 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8831 | pw.println(" Broadcast #" + i + ":"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8832 | br.dump(pw, " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8833 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8834 | printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8835 | for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8836 | BroadcastRecord br = mOrderedBroadcasts.get(i); |
| 8837 | if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { |
| 8838 | continue; |
| 8839 | } |
| 8840 | if (!printed) { |
| 8841 | if (needSep) { |
| 8842 | pw.println(); |
| 8843 | } |
| 8844 | needSep = true; |
| 8845 | pw.println(" Active ordered broadcasts:"); |
| 8846 | } |
| 8847 | pw.println(" Ordered Broadcast #" + i + ":"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8848 | mOrderedBroadcasts.get(i).dump(pw, " "); |
| 8849 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8850 | if (dumpPackage == null || (mPendingBroadcast != null |
| 8851 | && dumpPackage.equals(mPendingBroadcast.callerPackage))) { |
| 8852 | if (needSep) { |
| 8853 | pw.println(); |
| 8854 | } |
| 8855 | pw.println(" Pending broadcast:"); |
| 8856 | if (mPendingBroadcast != null) { |
| 8857 | mPendingBroadcast.dump(pw, " "); |
| 8858 | } else { |
| 8859 | pw.println(" (null)"); |
| 8860 | } |
| 8861 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8862 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8863 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8864 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8865 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8866 | for (int i=0; i<MAX_BROADCAST_HISTORY; i++) { |
| 8867 | BroadcastRecord r = mBroadcastHistory[i]; |
| 8868 | if (r == null) { |
| 8869 | break; |
| 8870 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8871 | if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) { |
| 8872 | continue; |
| 8873 | } |
| 8874 | if (!printed) { |
| 8875 | if (needSep) { |
| 8876 | pw.println(); |
| 8877 | } |
| 8878 | needSep = true; |
| 8879 | pw.println(" Historical broadcasts:"); |
| 8880 | printed = true; |
| 8881 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8882 | if (dumpAll) { |
| 8883 | pw.print(" Historical Broadcast #"); pw.print(i); pw.println(":"); |
| 8884 | r.dump(pw, " "); |
| 8885 | } else { |
| 8886 | if (i >= 50) { |
| 8887 | pw.println(" ..."); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 8888 | break; |
| 8889 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8890 | pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 8891 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8892 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8893 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8894 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8895 | if (mStickyBroadcasts != null && dumpPackage == null) { |
| 8896 | if (needSep) { |
| 8897 | pw.println(); |
| 8898 | } |
| 8899 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8900 | pw.println(" Sticky broadcasts:"); |
| 8901 | StringBuilder sb = new StringBuilder(128); |
| 8902 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 8903 | : mStickyBroadcasts.entrySet()) { |
| 8904 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8905 | if (dumpAll) { |
| 8906 | pw.println(":"); |
| 8907 | ArrayList<Intent> intents = ent.getValue(); |
| 8908 | final int N = intents.size(); |
| 8909 | for (int i=0; i<N; i++) { |
| 8910 | sb.setLength(0); |
| 8911 | sb.append(" Intent: "); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 8912 | intents.get(i).toShortString(sb, false, true, false); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8913 | pw.println(sb.toString()); |
| 8914 | Bundle bundle = intents.get(i).getExtras(); |
| 8915 | if (bundle != null) { |
| 8916 | pw.print(" "); |
| 8917 | pw.println(bundle.toString()); |
| 8918 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8919 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8920 | } else { |
| 8921 | pw.println(""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8922 | } |
| 8923 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8924 | needSep = true; |
| 8925 | } |
| 8926 | |
| 8927 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8928 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8929 | pw.println(" mBroadcastsScheduled=" + mBroadcastsScheduled); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8930 | pw.println(" mHandler:"); |
| 8931 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8932 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8933 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8934 | |
| 8935 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8936 | } |
| 8937 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8938 | boolean dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8939 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8940 | boolean needSep = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8941 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8942 | ItemMatcher matcher = new ItemMatcher(); |
| 8943 | matcher.build(args, opti); |
| 8944 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8945 | pw.println("ACTIVITY MANAGER SERVICES (dumpsys activity services)"); |
| 8946 | if (mServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8947 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8948 | long nowReal = SystemClock.elapsedRealtime(); |
| 8949 | Iterator<ServiceRecord> it = mServices.values().iterator(); |
| 8950 | needSep = false; |
| 8951 | while (it.hasNext()) { |
| 8952 | ServiceRecord r = it.next(); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8953 | if (!matcher.match(r, r.name)) { |
| 8954 | continue; |
| 8955 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8956 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 8957 | continue; |
| 8958 | } |
| 8959 | if (!printed) { |
| 8960 | pw.println(" Active services:"); |
| 8961 | printed = true; |
| 8962 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8963 | if (needSep) { |
| 8964 | pw.println(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8965 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8966 | pw.print(" * "); pw.println(r); |
| 8967 | if (dumpAll) { |
| 8968 | r.dump(pw, " "); |
| 8969 | needSep = true; |
| 8970 | } else { |
| 8971 | pw.print(" app="); pw.println(r.app); |
| 8972 | pw.print(" created="); |
| 8973 | TimeUtils.formatDuration(r.createTime, nowReal, pw); |
| 8974 | pw.print(" started="); pw.print(r.startRequested); |
| 8975 | pw.print(" connections="); pw.println(r.connections.size()); |
Dianne Hackborn | 8ec8d41 | 2011-11-14 18:27:24 -0800 | [diff] [blame] | 8976 | if (r.connections.size() > 0) { |
| 8977 | pw.println(" Connections:"); |
| 8978 | for (ArrayList<ConnectionRecord> clist : r.connections.values()) { |
| 8979 | for (int i=0; i<clist.size(); i++) { |
| 8980 | ConnectionRecord conn = clist.get(i); |
| 8981 | pw.print(" "); |
| 8982 | pw.print(conn.binding.intent.intent.getIntent().toShortString( |
| 8983 | false, false, false)); |
| 8984 | pw.print(" -> "); |
| 8985 | ProcessRecord proc = conn.binding.client; |
| 8986 | pw.println(proc != null ? proc.toShortString() : "null"); |
| 8987 | } |
| 8988 | } |
| 8989 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8990 | } |
| 8991 | if (dumpClient && r.app != null && r.app.thread != null) { |
| 8992 | pw.println(" Client:"); |
| 8993 | pw.flush(); |
| 8994 | try { |
| 8995 | TransferPipe tp = new TransferPipe(); |
| 8996 | try { |
| 8997 | r.app.thread.dumpService( |
| 8998 | tp.getWriteFd().getFileDescriptor(), r, args); |
| 8999 | tp.setBufferPrefix(" "); |
| 9000 | // Short timeout, since blocking here can |
| 9001 | // deadlock with the application. |
| 9002 | tp.go(fd, 2000); |
| 9003 | } finally { |
| 9004 | tp.kill(); |
| 9005 | } |
| 9006 | } catch (IOException e) { |
| 9007 | pw.println(" Failure while dumping the service: " + e); |
| 9008 | } catch (RemoteException e) { |
| 9009 | pw.println(" Got a RemoteException while dumping the service"); |
| 9010 | } |
| 9011 | needSep = true; |
| 9012 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9013 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9014 | needSep = printed; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9015 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9016 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9017 | if (mPendingServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9018 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9019 | for (int i=0; i<mPendingServices.size(); i++) { |
| 9020 | ServiceRecord r = mPendingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9021 | if (!matcher.match(r, r.name)) { |
| 9022 | continue; |
| 9023 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9024 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9025 | continue; |
| 9026 | } |
| 9027 | if (!printed) { |
| 9028 | if (needSep) pw.println(" "); |
| 9029 | needSep = true; |
| 9030 | pw.println(" Pending services:"); |
| 9031 | printed = true; |
| 9032 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9033 | pw.print(" * Pending "); pw.println(r); |
| 9034 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9035 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9036 | needSep = true; |
| 9037 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9038 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9039 | if (mRestartingServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9040 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9041 | for (int i=0; i<mRestartingServices.size(); i++) { |
| 9042 | ServiceRecord r = mRestartingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9043 | if (!matcher.match(r, r.name)) { |
| 9044 | continue; |
| 9045 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9046 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9047 | continue; |
| 9048 | } |
| 9049 | if (!printed) { |
| 9050 | if (needSep) pw.println(" "); |
| 9051 | needSep = true; |
| 9052 | pw.println(" Restarting services:"); |
| 9053 | printed = true; |
| 9054 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9055 | pw.print(" * Restarting "); pw.println(r); |
| 9056 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9057 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9058 | needSep = true; |
| 9059 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9060 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9061 | if (mStoppingServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9062 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9063 | for (int i=0; i<mStoppingServices.size(); i++) { |
| 9064 | ServiceRecord r = mStoppingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9065 | if (!matcher.match(r, r.name)) { |
| 9066 | continue; |
| 9067 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9068 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9069 | continue; |
| 9070 | } |
| 9071 | if (!printed) { |
| 9072 | if (needSep) pw.println(" "); |
| 9073 | needSep = true; |
| 9074 | pw.println(" Stopping services:"); |
| 9075 | printed = true; |
| 9076 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9077 | pw.print(" * Stopping "); pw.println(r); |
| 9078 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9079 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9080 | needSep = true; |
| 9081 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9082 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9083 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9084 | if (mServiceConnections.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9085 | boolean printed = false; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9086 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9087 | = mServiceConnections.values().iterator(); |
| 9088 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9089 | ArrayList<ConnectionRecord> r = it.next(); |
| 9090 | for (int i=0; i<r.size(); i++) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9091 | ConnectionRecord cr = r.get(i); |
| 9092 | if (!matcher.match(cr.binding.service, cr.binding.service.name)) { |
| 9093 | continue; |
| 9094 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9095 | if (dumpPackage != null && (cr.binding.client == null |
| 9096 | || !dumpPackage.equals(cr.binding.client.info.packageName))) { |
| 9097 | continue; |
| 9098 | } |
| 9099 | if (!printed) { |
| 9100 | if (needSep) pw.println(" "); |
| 9101 | needSep = true; |
| 9102 | pw.println(" Connection bindings to services:"); |
| 9103 | printed = true; |
| 9104 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9105 | pw.print(" * "); pw.println(cr); |
| 9106 | cr.dump(pw, " "); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9107 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9108 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9109 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9110 | } |
| 9111 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9112 | |
| 9113 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9114 | } |
| 9115 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9116 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9117 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9118 | boolean needSep = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9119 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9120 | ItemMatcher matcher = new ItemMatcher(); |
| 9121 | matcher.build(args, opti); |
| 9122 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9123 | pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)"); |
| 9124 | if (mProvidersByClass.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9125 | boolean printed = false; |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9126 | Iterator<Map.Entry<ComponentName, ContentProviderRecord>> it |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9127 | = mProvidersByClass.entrySet().iterator(); |
| 9128 | while (it.hasNext()) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9129 | Map.Entry<ComponentName, ContentProviderRecord> e = it.next(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9130 | ContentProviderRecord r = e.getValue(); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9131 | ComponentName comp = e.getKey(); |
| 9132 | String cls = comp.getClassName(); |
| 9133 | int end = cls.lastIndexOf('.'); |
| 9134 | if (end > 0 && end < (cls.length()-2)) { |
| 9135 | cls = cls.substring(end+1); |
| 9136 | } |
| 9137 | if (!matcher.match(r, comp)) { |
| 9138 | continue; |
| 9139 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9140 | if (dumpPackage != null && !dumpPackage.equals(comp.getPackageName())) { |
| 9141 | continue; |
| 9142 | } |
| 9143 | if (!printed) { |
| 9144 | if (needSep) pw.println(" "); |
| 9145 | needSep = true; |
| 9146 | pw.println(" Published content providers (by class):"); |
| 9147 | printed = true; |
| 9148 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9149 | pw.print(" * "); pw.print(cls); pw.print(" ("); |
Dianne Hackborn | 8ec8d41 | 2011-11-14 18:27:24 -0800 | [diff] [blame] | 9150 | pw.print(comp.flattenToShortString()); pw.println(")"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9151 | if (dumpAll) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9152 | r.dump(pw, " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9153 | } else { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 9154 | if (r.proc != null) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 9155 | pw.print(" "); pw.println(r.proc); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9156 | } else { |
| 9157 | pw.println(); |
| 9158 | } |
Dianne Hackborn | 8ec8d41 | 2011-11-14 18:27:24 -0800 | [diff] [blame] | 9159 | if (r.clients.size() > 0) { |
| 9160 | pw.println(" Clients:"); |
| 9161 | for (ProcessRecord cproc : r.clients) { |
| 9162 | pw.print(" - "); pw.println(cproc); |
| 9163 | } |
| 9164 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9165 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9166 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9167 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9168 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9169 | if (dumpAll) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9170 | if (mProvidersByName.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9171 | boolean printed = false; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9172 | Iterator<Map.Entry<String, ContentProviderRecord>> it |
| 9173 | = mProvidersByName.entrySet().iterator(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9174 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9175 | Map.Entry<String, ContentProviderRecord> e = it.next(); |
| 9176 | ContentProviderRecord r = e.getValue(); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9177 | if (!matcher.match(r, r.name)) { |
| 9178 | continue; |
| 9179 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9180 | if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) { |
| 9181 | continue; |
| 9182 | } |
| 9183 | if (!printed) { |
| 9184 | if (needSep) pw.println(" "); |
| 9185 | needSep = true; |
| 9186 | pw.println(" Authority to provider mappings:"); |
| 9187 | printed = true; |
| 9188 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9189 | pw.print(" "); pw.print(e.getKey()); pw.println(":"); |
| 9190 | pw.print(" "); pw.println(r); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9191 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9192 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9193 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9194 | |
| 9195 | if (mLaunchingProviders.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9196 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9197 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9198 | ContentProviderRecord r = mLaunchingProviders.get(i); |
| 9199 | if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) { |
| 9200 | continue; |
| 9201 | } |
| 9202 | if (!printed) { |
| 9203 | if (needSep) pw.println(" "); |
| 9204 | needSep = true; |
| 9205 | pw.println(" Launching content providers:"); |
| 9206 | printed = true; |
| 9207 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9208 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9209 | pw.println(r); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9210 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9211 | } |
| 9212 | |
| 9213 | if (mGrantedUriPermissions.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9214 | if (needSep) pw.println(); |
| 9215 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9216 | pw.println("Granted Uri Permissions:"); |
| 9217 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 9218 | int uid = mGrantedUriPermissions.keyAt(i); |
| 9219 | HashMap<Uri, UriPermission> perms |
| 9220 | = mGrantedUriPermissions.valueAt(i); |
| 9221 | pw.print(" * UID "); pw.print(uid); |
| 9222 | pw.println(" holds:"); |
| 9223 | for (UriPermission perm : perms.values()) { |
| 9224 | pw.print(" "); pw.println(perm); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9225 | if (dumpAll) { |
| 9226 | perm.dump(pw, " "); |
| 9227 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9228 | } |
| 9229 | } |
| 9230 | needSep = true; |
| 9231 | } |
| 9232 | |
| 9233 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9234 | } |
| 9235 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9236 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9237 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9238 | boolean needSep = false; |
| 9239 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9240 | if (mIntentSenderRecords.size() > 0) { |
| 9241 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9242 | Iterator<WeakReference<PendingIntentRecord>> it |
| 9243 | = mIntentSenderRecords.values().iterator(); |
| 9244 | while (it.hasNext()) { |
| 9245 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 9246 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9247 | if (dumpPackage != null && (rec == null |
| 9248 | || !dumpPackage.equals(rec.key.packageName))) { |
| 9249 | continue; |
| 9250 | } |
| 9251 | if (!printed) { |
| 9252 | pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)"); |
| 9253 | printed = true; |
| 9254 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9255 | needSep = true; |
| 9256 | if (rec != null) { |
| 9257 | pw.print(" * "); pw.println(rec); |
| 9258 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9259 | rec.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9260 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9261 | } else { |
| 9262 | pw.print(" * "); pw.println(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9263 | } |
| 9264 | } |
| 9265 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9266 | |
| 9267 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9268 | } |
| 9269 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9270 | private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9271 | String prefix, String label, boolean complete, boolean brief, boolean client, |
| 9272 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9273 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9274 | boolean needNL = false; |
| 9275 | final String innerPrefix = prefix + " "; |
| 9276 | final String[] args = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9277 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9278 | final ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9279 | if (dumpPackage != null && !dumpPackage.equals(r.packageName)) { |
| 9280 | continue; |
| 9281 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 9282 | final boolean full = !brief && (complete || !r.isInHistory()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9283 | if (needNL) { |
| 9284 | pw.println(" "); |
| 9285 | needNL = false; |
| 9286 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9287 | if (lastTask != r.task) { |
| 9288 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9289 | pw.print(prefix); |
| 9290 | pw.print(full ? "* " : " "); |
| 9291 | pw.println(lastTask); |
| 9292 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9293 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9294 | } else if (complete) { |
| 9295 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 9296 | if (lastTask.intent != null) { |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9297 | pw.print(prefix); pw.print(" "); |
| 9298 | pw.println(lastTask.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9299 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9300 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9301 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9302 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 9303 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 9304 | pw.println(r); |
| 9305 | if (full) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9306 | r.dump(pw, innerPrefix); |
| 9307 | } else if (complete) { |
| 9308 | // Complete + brief == give a summary. Isn't that obvious?!? |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9309 | pw.print(innerPrefix); pw.println(r.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9310 | if (r.app != null) { |
| 9311 | pw.print(innerPrefix); pw.println(r.app); |
| 9312 | } |
| 9313 | } |
| 9314 | if (client && r.app != null && r.app.thread != null) { |
| 9315 | // flush anything that is already in the PrintWriter since the thread is going |
| 9316 | // to write to the file descriptor directly |
| 9317 | pw.flush(); |
| 9318 | try { |
| 9319 | TransferPipe tp = new TransferPipe(); |
| 9320 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9321 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9322 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9323 | // Short timeout, since blocking here can |
| 9324 | // deadlock with the application. |
| 9325 | tp.go(fd, 2000); |
| 9326 | } finally { |
| 9327 | tp.kill(); |
| 9328 | } |
| 9329 | } catch (IOException e) { |
| 9330 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
| 9331 | } catch (RemoteException e) { |
| 9332 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
| 9333 | } |
| 9334 | needNL = true; |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9335 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9336 | } |
| 9337 | } |
| 9338 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 9339 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 9340 | if (val == base) { |
| 9341 | if (space == null) return prefix; |
| 9342 | return prefix + " "; |
| 9343 | } |
| 9344 | return prefix + "+" + Integer.toString(val-base); |
| 9345 | } |
| 9346 | |
| 9347 | private static final int dumpProcessList(PrintWriter pw, |
| 9348 | ActivityManagerService service, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9349 | String prefix, String normalLabel, String persistentLabel, |
| 9350 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9351 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 9352 | final int N = list.size()-1; |
| 9353 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9354 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9355 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9356 | continue; |
| 9357 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9358 | pw.println(String.format("%s%s #%2d: %s", |
| 9359 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 9360 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9361 | if (r.persistent) { |
| 9362 | numPers++; |
| 9363 | } |
| 9364 | } |
| 9365 | return numPers; |
| 9366 | } |
| 9367 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9368 | private static final boolean dumpProcessOomList(PrintWriter pw, |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9369 | ActivityManagerService service, List<ProcessRecord> origList, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9370 | String prefix, String normalLabel, String persistentLabel, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9371 | boolean inclDetails, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9372 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9373 | ArrayList<Pair<ProcessRecord, Integer>> list |
| 9374 | = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size()); |
| 9375 | for (int i=0; i<origList.size(); i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9376 | ProcessRecord r = origList.get(i); |
| 9377 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9378 | continue; |
| 9379 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9380 | list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i)); |
| 9381 | } |
| 9382 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9383 | if (list.size() <= 0) { |
| 9384 | return false; |
| 9385 | } |
| 9386 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9387 | Comparator<Pair<ProcessRecord, Integer>> comparator |
| 9388 | = new Comparator<Pair<ProcessRecord, Integer>>() { |
| 9389 | @Override |
| 9390 | public int compare(Pair<ProcessRecord, Integer> object1, |
| 9391 | Pair<ProcessRecord, Integer> object2) { |
| 9392 | if (object1.first.setAdj != object2.first.setAdj) { |
| 9393 | return object1.first.setAdj > object2.first.setAdj ? -1 : 1; |
| 9394 | } |
| 9395 | if (object1.second.intValue() != object2.second.intValue()) { |
| 9396 | return object1.second.intValue() > object2.second.intValue() ? -1 : 1; |
| 9397 | } |
| 9398 | return 0; |
| 9399 | } |
| 9400 | }; |
| 9401 | |
| 9402 | Collections.sort(list, comparator); |
| 9403 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9404 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 9405 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 9406 | final long curUptime = SystemClock.uptimeMillis(); |
| 9407 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 9408 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9409 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9410 | ProcessRecord r = list.get(i).first; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9411 | String oomAdj; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9412 | if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9413 | oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9414 | } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) { |
| 9415 | oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9416 | } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) { |
| 9417 | oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9418 | } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) { |
| 9419 | oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9420 | } else if (r.setAdj >= ProcessList.SERVICE_ADJ) { |
| 9421 | oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9422 | } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame^] | 9423 | oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9424 | } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 9425 | oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9426 | } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 9427 | oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9428 | } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 9429 | oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ); |
| 9430 | } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 9431 | oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9432 | } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) { |
| 9433 | oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9434 | } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) { |
| 9435 | oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9436 | } else { |
| 9437 | oomAdj = Integer.toString(r.setAdj); |
| 9438 | } |
| 9439 | String schedGroup; |
| 9440 | switch (r.setSchedGroup) { |
| 9441 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 9442 | schedGroup = "B"; |
| 9443 | break; |
| 9444 | case Process.THREAD_GROUP_DEFAULT: |
| 9445 | schedGroup = "F"; |
| 9446 | break; |
| 9447 | default: |
| 9448 | schedGroup = Integer.toString(r.setSchedGroup); |
| 9449 | break; |
| 9450 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 9451 | String foreground; |
| 9452 | if (r.foregroundActivities) { |
| 9453 | foreground = "A"; |
| 9454 | } else if (r.foregroundServices) { |
| 9455 | foreground = "S"; |
| 9456 | } else { |
| 9457 | foreground = " "; |
| 9458 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 9459 | pw.println(String.format("%s%s #%2d: adj=%s/%s%s trm=%2d %s (%s)", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9460 | prefix, (r.persistent ? persistentLabel : normalLabel), |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9461 | (origList.size()-1)-list.get(i).second, oomAdj, schedGroup, |
| 9462 | foreground, r.trimMemoryLevel, r.toShortString(), r.adjType)); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9463 | if (r.adjSource != null || r.adjTarget != null) { |
| 9464 | pw.print(prefix); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9465 | pw.print(" "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9466 | if (r.adjTarget instanceof ComponentName) { |
| 9467 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 9468 | } else if (r.adjTarget != null) { |
| 9469 | pw.print(r.adjTarget.toString()); |
| 9470 | } else { |
| 9471 | pw.print("{null}"); |
| 9472 | } |
| 9473 | pw.print("<="); |
| 9474 | if (r.adjSource instanceof ProcessRecord) { |
| 9475 | pw.print("Proc{"); |
| 9476 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 9477 | pw.println("}"); |
| 9478 | } else if (r.adjSource != null) { |
| 9479 | pw.println(r.adjSource.toString()); |
| 9480 | } else { |
| 9481 | pw.println("{null}"); |
| 9482 | } |
| 9483 | } |
| 9484 | if (inclDetails) { |
| 9485 | pw.print(prefix); |
| 9486 | pw.print(" "); |
| 9487 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 9488 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
| 9489 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 9490 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 9491 | pw.print(" cur="); pw.print(r.curAdj); |
| 9492 | pw.print(" set="); pw.println(r.setAdj); |
| 9493 | pw.print(prefix); |
| 9494 | pw.print(" "); |
| 9495 | pw.print("keeping="); pw.print(r.keeping); |
| 9496 | pw.print(" hidden="); pw.print(r.hidden); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9497 | pw.print(" empty="); pw.print(r.empty); |
| 9498 | pw.print(" hasAboveClient="); pw.println(r.hasAboveClient); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9499 | |
| 9500 | if (!r.keeping) { |
| 9501 | if (r.lastWakeTime != 0) { |
| 9502 | long wtime; |
| 9503 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 9504 | synchronized (stats) { |
| 9505 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 9506 | r.pid, curRealtime); |
| 9507 | } |
| 9508 | long timeUsed = wtime - r.lastWakeTime; |
| 9509 | pw.print(prefix); |
| 9510 | pw.print(" "); |
| 9511 | pw.print("keep awake over "); |
| 9512 | TimeUtils.formatDuration(realtimeSince, pw); |
| 9513 | pw.print(" used "); |
| 9514 | TimeUtils.formatDuration(timeUsed, pw); |
| 9515 | pw.print(" ("); |
| 9516 | pw.print((timeUsed*100)/realtimeSince); |
| 9517 | pw.println("%)"); |
| 9518 | } |
| 9519 | if (r.lastCpuTime != 0) { |
| 9520 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 9521 | pw.print(prefix); |
| 9522 | pw.print(" "); |
| 9523 | pw.print("run cpu over "); |
| 9524 | TimeUtils.formatDuration(uptimeSince, pw); |
| 9525 | pw.print(" used "); |
| 9526 | TimeUtils.formatDuration(timeUsed, pw); |
| 9527 | pw.print(" ("); |
| 9528 | pw.print((timeUsed*100)/uptimeSince); |
| 9529 | pw.println("%)"); |
| 9530 | } |
| 9531 | } |
| 9532 | } |
| 9533 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9534 | return true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9535 | } |
| 9536 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9537 | ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9538 | ArrayList<ProcessRecord> procs; |
| 9539 | synchronized (this) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9540 | if (args != null && args.length > start |
| 9541 | && args[start].charAt(0) != '-') { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9542 | procs = new ArrayList<ProcessRecord>(); |
| 9543 | int pid = -1; |
| 9544 | try { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9545 | pid = Integer.parseInt(args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9546 | } catch (NumberFormatException e) { |
| 9547 | |
| 9548 | } |
| 9549 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 9550 | ProcessRecord proc = mLruProcesses.get(i); |
| 9551 | if (proc.pid == pid) { |
| 9552 | procs.add(proc); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9553 | } else if (proc.processName.equals(args[start])) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9554 | procs.add(proc); |
| 9555 | } |
| 9556 | } |
| 9557 | if (procs.size() <= 0) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9558 | pw.println("No process found for: " + args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9559 | return null; |
| 9560 | } |
| 9561 | } else { |
| 9562 | procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 9563 | } |
| 9564 | } |
| 9565 | return procs; |
| 9566 | } |
| 9567 | |
| 9568 | final void dumpGraphicsHardwareUsage(FileDescriptor fd, |
| 9569 | PrintWriter pw, String[] args) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9570 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9571 | if (procs == null) { |
| 9572 | return; |
| 9573 | } |
| 9574 | |
| 9575 | long uptime = SystemClock.uptimeMillis(); |
| 9576 | long realtime = SystemClock.elapsedRealtime(); |
| 9577 | pw.println("Applications Graphics Acceleration Info:"); |
| 9578 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 9579 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9580 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9581 | ProcessRecord r = procs.get(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9582 | if (r.thread != null) { |
| 9583 | pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **"); |
| 9584 | pw.flush(); |
| 9585 | try { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9586 | TransferPipe tp = new TransferPipe(); |
| 9587 | try { |
| 9588 | r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 9589 | tp.go(fd); |
| 9590 | } finally { |
| 9591 | tp.kill(); |
| 9592 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9593 | } catch (IOException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9594 | pw.println("Failure while dumping the app: " + r); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9595 | pw.flush(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9596 | } catch (RemoteException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9597 | pw.println("Got a RemoteException while dumping the app " + r); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9598 | pw.flush(); |
| 9599 | } |
| 9600 | } |
| 9601 | } |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9602 | } |
| 9603 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9604 | final static class MemItem { |
| 9605 | final String label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9606 | final String shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9607 | final long pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9608 | final int id; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9609 | ArrayList<MemItem> subitems; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9610 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9611 | public MemItem(String _label, String _shortLabel, long _pss, int _id) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9612 | label = _label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9613 | shortLabel = _shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9614 | pss = _pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9615 | id = _id; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9616 | } |
| 9617 | } |
| 9618 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9619 | static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9620 | boolean sort) { |
| 9621 | if (sort) { |
| 9622 | Collections.sort(items, new Comparator<MemItem>() { |
| 9623 | @Override |
| 9624 | public int compare(MemItem lhs, MemItem rhs) { |
| 9625 | if (lhs.pss < rhs.pss) { |
| 9626 | return 1; |
| 9627 | } else if (lhs.pss > rhs.pss) { |
| 9628 | return -1; |
| 9629 | } |
| 9630 | return 0; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9631 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9632 | }); |
| 9633 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9634 | |
| 9635 | for (int i=0; i<items.size(); i++) { |
| 9636 | MemItem mi = items.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9637 | pw.print(prefix); pw.printf("%7d kB: ", mi.pss); pw.println(mi.label); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9638 | if (mi.subitems != null) { |
| 9639 | dumpMemItems(pw, prefix + " ", mi.subitems, true); |
| 9640 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9641 | } |
| 9642 | } |
| 9643 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9644 | // These are in KB. |
| 9645 | static final long[] DUMP_MEM_BUCKETS = new long[] { |
| 9646 | 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024, |
| 9647 | 120*1024, 160*1024, 200*1024, |
| 9648 | 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024, |
| 9649 | 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024 |
| 9650 | }; |
| 9651 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame^] | 9652 | static final void appendMemBucket(StringBuilder out, long memKB, String label, |
| 9653 | boolean stackLike) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9654 | int start = label.lastIndexOf('.'); |
| 9655 | if (start >= 0) start++; |
| 9656 | else start = 0; |
| 9657 | int end = label.length(); |
| 9658 | for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) { |
| 9659 | if (DUMP_MEM_BUCKETS[i] >= memKB) { |
| 9660 | long bucket = DUMP_MEM_BUCKETS[i]/1024; |
| 9661 | out.append(bucket); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame^] | 9662 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9663 | out.append(label, start, end); |
| 9664 | return; |
| 9665 | } |
| 9666 | } |
| 9667 | out.append(memKB/1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame^] | 9668 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9669 | out.append(label, start, end); |
| 9670 | } |
| 9671 | |
| 9672 | static final int[] DUMP_MEM_OOM_ADJ = new int[] { |
| 9673 | ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ, |
| 9674 | ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ, |
| 9675 | ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ, |
| 9676 | ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ |
| 9677 | }; |
| 9678 | static final String[] DUMP_MEM_OOM_LABEL = new String[] { |
| 9679 | "System", "Persistent", "Foreground", |
| 9680 | "Visible", "Perceptible", "Heavy Weight", |
| 9681 | "Backup", "A Services", "Home", "Previous", |
| 9682 | "B Services", "Background" |
| 9683 | }; |
| 9684 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9685 | final void dumpApplicationMemoryUsage(FileDescriptor fd, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9686 | PrintWriter pw, String prefix, String[] args, boolean brief, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame^] | 9687 | PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9688 | boolean dumpAll = false; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9689 | boolean oomOnly = false; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9690 | |
| 9691 | int opti = 0; |
| 9692 | while (opti < args.length) { |
| 9693 | String opt = args[opti]; |
| 9694 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 9695 | break; |
| 9696 | } |
| 9697 | opti++; |
| 9698 | if ("-a".equals(opt)) { |
| 9699 | dumpAll = true; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9700 | } else if ("--oom".equals(opt)) { |
| 9701 | oomOnly = true; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9702 | } else if ("-h".equals(opt)) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9703 | pw.println("meminfo dump options: [-a] [--oom] [process]"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9704 | pw.println(" -a: include all available information for each process."); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9705 | pw.println(" --oom: only show processes organized by oom adj."); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9706 | pw.println("If [process] is specified it can be the name or "); |
| 9707 | pw.println("pid of a specific process to dump."); |
| 9708 | return; |
| 9709 | } else { |
| 9710 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 9711 | } |
| 9712 | } |
| 9713 | |
| 9714 | ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9715 | if (procs == null) { |
| 9716 | return; |
| 9717 | } |
| 9718 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 9719 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9720 | long uptime = SystemClock.uptimeMillis(); |
| 9721 | long realtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9722 | |
| 9723 | if (procs.size() == 1 || isCheckinRequest) { |
| 9724 | dumpAll = true; |
| 9725 | } |
| 9726 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9727 | if (isCheckinRequest) { |
| 9728 | // short checkin version |
| 9729 | pw.println(uptime + "," + realtime); |
| 9730 | pw.flush(); |
| 9731 | } else { |
| 9732 | pw.println("Applications Memory Usage (kB):"); |
| 9733 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 9734 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9735 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9736 | String[] innerArgs = new String[args.length-opti]; |
| 9737 | System.arraycopy(args, opti, innerArgs, 0, args.length-opti); |
| 9738 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9739 | ArrayList<MemItem> procMems = new ArrayList<MemItem>(); |
| 9740 | long nativePss=0, dalvikPss=0, otherPss=0; |
| 9741 | long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS]; |
| 9742 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9743 | long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length]; |
| 9744 | ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[]) |
| 9745 | new ArrayList[DUMP_MEM_OOM_LABEL.length]; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9746 | |
| 9747 | long totalPss = 0; |
| 9748 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9749 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9750 | ProcessRecord r = procs.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9751 | if (r.thread != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9752 | if (!isCheckinRequest && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9753 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 9754 | pw.flush(); |
| 9755 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9756 | Debug.MemoryInfo mi = null; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9757 | if (dumpAll) { |
| 9758 | try { |
| 9759 | mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs); |
| 9760 | } catch (RemoteException e) { |
| 9761 | if (!isCheckinRequest) { |
| 9762 | pw.println("Got RemoteException!"); |
| 9763 | pw.flush(); |
| 9764 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9765 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9766 | } else { |
| 9767 | mi = new Debug.MemoryInfo(); |
| 9768 | Debug.getMemoryInfo(r.pid, mi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9769 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9770 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9771 | if (!isCheckinRequest && mi != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9772 | long myTotalPss = mi.getTotalPss(); |
| 9773 | totalPss += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9774 | MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")", |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9775 | r.processName, myTotalPss, 0); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9776 | procMems.add(pssItem); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9777 | |
| 9778 | nativePss += mi.nativePss; |
| 9779 | dalvikPss += mi.dalvikPss; |
| 9780 | otherPss += mi.otherPss; |
| 9781 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
| 9782 | long mem = mi.getOtherPss(j); |
| 9783 | miscPss[j] += mem; |
| 9784 | otherPss -= mem; |
| 9785 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9786 | |
| 9787 | for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9788 | if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex] |
| 9789 | || oomIndex == (oomPss.length-1)) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9790 | oomPss[oomIndex] += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9791 | if (oomProcs[oomIndex] == null) { |
| 9792 | oomProcs[oomIndex] = new ArrayList<MemItem>(); |
| 9793 | } |
| 9794 | oomProcs[oomIndex].add(pssItem); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9795 | break; |
| 9796 | } |
| 9797 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9798 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9799 | } |
| 9800 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9801 | |
| 9802 | if (!isCheckinRequest && procs.size() > 1) { |
| 9803 | ArrayList<MemItem> catMems = new ArrayList<MemItem>(); |
| 9804 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9805 | catMems.add(new MemItem("Native", "Native", nativePss, -1)); |
| 9806 | catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2)); |
| 9807 | catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9808 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9809 | String label = Debug.MemoryInfo.getOtherLabel(j); |
| 9810 | catMems.add(new MemItem(label, label, miscPss[j], j)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9811 | } |
| 9812 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9813 | ArrayList<MemItem> oomMems = new ArrayList<MemItem>(); |
| 9814 | for (int j=0; j<oomPss.length; j++) { |
| 9815 | if (oomPss[j] != 0) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9816 | String label = DUMP_MEM_OOM_LABEL[j]; |
| 9817 | MemItem item = new MemItem(label, label, oomPss[j], |
| 9818 | DUMP_MEM_OOM_ADJ[j]); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9819 | item.subitems = oomProcs[j]; |
| 9820 | oomMems.add(item); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9821 | } |
| 9822 | } |
| 9823 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame^] | 9824 | if (outTag != null || outStack != null) { |
| 9825 | if (outTag != null) { |
| 9826 | appendMemBucket(outTag, totalPss, "total", false); |
| 9827 | } |
| 9828 | if (outStack != null) { |
| 9829 | appendMemBucket(outStack, totalPss, "total", true); |
| 9830 | } |
| 9831 | boolean firstLine = true; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9832 | for (int i=0; i<oomMems.size(); i++) { |
| 9833 | MemItem miCat = oomMems.get(i); |
| 9834 | if (miCat.subitems == null || miCat.subitems.size() < 1) { |
| 9835 | continue; |
| 9836 | } |
| 9837 | if (miCat.id < ProcessList.SERVICE_ADJ |
| 9838 | || miCat.id == ProcessList.HOME_APP_ADJ |
| 9839 | || miCat.id == ProcessList.PREVIOUS_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame^] | 9840 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 9841 | outTag.append(" / "); |
| 9842 | } |
| 9843 | if (outStack != null) { |
| 9844 | if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 9845 | if (firstLine) { |
| 9846 | outStack.append(":"); |
| 9847 | firstLine = false; |
| 9848 | } |
| 9849 | outStack.append("\n\t at "); |
| 9850 | } else { |
| 9851 | outStack.append("$"); |
| 9852 | } |
| 9853 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9854 | for (int j=0; j<miCat.subitems.size(); j++) { |
| 9855 | MemItem mi = miCat.subitems.get(j); |
| 9856 | if (j > 0) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame^] | 9857 | if (outTag != null) { |
| 9858 | outTag.append(" "); |
| 9859 | } |
| 9860 | if (outStack != null) { |
| 9861 | outStack.append("$"); |
| 9862 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9863 | } |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame^] | 9864 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 9865 | appendMemBucket(outTag, mi.pss, mi.shortLabel, false); |
| 9866 | } |
| 9867 | if (outStack != null) { |
| 9868 | appendMemBucket(outStack, mi.pss, mi.shortLabel, true); |
| 9869 | } |
| 9870 | } |
| 9871 | if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 9872 | outStack.append("("); |
| 9873 | for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) { |
| 9874 | if (DUMP_MEM_OOM_ADJ[k] == miCat.id) { |
| 9875 | outStack.append(DUMP_MEM_OOM_LABEL[k]); |
| 9876 | outStack.append(":"); |
| 9877 | outStack.append(DUMP_MEM_OOM_ADJ[k]); |
| 9878 | } |
| 9879 | } |
| 9880 | outStack.append(")"); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9881 | } |
| 9882 | } |
| 9883 | } |
| 9884 | } |
| 9885 | |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9886 | if (!brief && !oomOnly) { |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 9887 | pw.println(); |
| 9888 | pw.println("Total PSS by process:"); |
| 9889 | dumpMemItems(pw, " ", procMems, true); |
| 9890 | pw.println(); |
| 9891 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9892 | pw.println("Total PSS by OOM adjustment:"); |
| 9893 | dumpMemItems(pw, " ", oomMems, false); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9894 | if (!oomOnly) { |
| 9895 | PrintWriter out = categoryPw != null ? categoryPw : pw; |
| 9896 | out.println(); |
| 9897 | out.println("Total PSS by category:"); |
| 9898 | dumpMemItems(out, " ", catMems, true); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 9899 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9900 | pw.println(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9901 | pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9902 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9903 | } |
| 9904 | |
| 9905 | /** |
| 9906 | * Searches array of arguments for the specified string |
| 9907 | * @param args array of argument strings |
| 9908 | * @param value value to search for |
| 9909 | * @return true if the value is contained in the array |
| 9910 | */ |
| 9911 | private static boolean scanArgs(String[] args, String value) { |
| 9912 | if (args != null) { |
| 9913 | for (String arg : args) { |
| 9914 | if (value.equals(arg)) { |
| 9915 | return true; |
| 9916 | } |
| 9917 | } |
| 9918 | } |
| 9919 | return false; |
| 9920 | } |
| 9921 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9922 | private final void killServicesLocked(ProcessRecord app, |
| 9923 | boolean allowRestart) { |
| 9924 | // Report disconnected services. |
| 9925 | if (false) { |
| 9926 | // XXX we are letting the client link to the service for |
| 9927 | // death notifications. |
| 9928 | if (app.services.size() > 0) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9929 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9930 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9931 | ServiceRecord r = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9932 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9933 | Iterator<ArrayList<ConnectionRecord>> jt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9934 | = r.connections.values().iterator(); |
| 9935 | while (jt.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9936 | ArrayList<ConnectionRecord> cl = jt.next(); |
| 9937 | for (int i=0; i<cl.size(); i++) { |
| 9938 | ConnectionRecord c = cl.get(i); |
| 9939 | if (c.binding.client != app) { |
| 9940 | try { |
| 9941 | //c.conn.connected(r.className, null); |
| 9942 | } catch (Exception e) { |
| 9943 | // todo: this should be asynchronous! |
| 9944 | Slog.w(TAG, "Exception thrown disconnected servce " |
| 9945 | + r.shortName |
| 9946 | + " from app " + app.processName, e); |
| 9947 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9948 | } |
| 9949 | } |
| 9950 | } |
| 9951 | } |
| 9952 | } |
| 9953 | } |
| 9954 | } |
| 9955 | |
| 9956 | // Clean up any connections this application has to other services. |
| 9957 | if (app.connections.size() > 0) { |
| 9958 | Iterator<ConnectionRecord> it = app.connections.iterator(); |
| 9959 | while (it.hasNext()) { |
| 9960 | ConnectionRecord r = it.next(); |
| 9961 | removeConnectionLocked(r, app, null); |
| 9962 | } |
| 9963 | } |
| 9964 | app.connections.clear(); |
| 9965 | |
| 9966 | if (app.services.size() != 0) { |
| 9967 | // Any services running in the application need to be placed |
| 9968 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9969 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9970 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9971 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9972 | synchronized (sr.stats.getBatteryStats()) { |
| 9973 | sr.stats.stopLaunchedLocked(); |
| 9974 | } |
| 9975 | sr.app = null; |
| 9976 | sr.executeNesting = 0; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9977 | if (mStoppingServices.remove(sr)) { |
| 9978 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
| 9979 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9980 | |
| 9981 | boolean hasClients = sr.bindings.size() > 0; |
| 9982 | if (hasClients) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9983 | Iterator<IntentBindRecord> bindings |
| 9984 | = sr.bindings.values().iterator(); |
| 9985 | while (bindings.hasNext()) { |
| 9986 | IntentBindRecord b = bindings.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9987 | if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9988 | + ": shouldUnbind=" + b.hasBound); |
| 9989 | b.binder = null; |
| 9990 | b.requested = b.received = b.hasBound = false; |
| 9991 | } |
| 9992 | } |
| 9993 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 9994 | if (sr.crashCount >= 2 && (sr.serviceInfo.applicationInfo.flags |
| 9995 | &ApplicationInfo.FLAG_PERSISTENT) == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9996 | Slog.w(TAG, "Service crashed " + sr.crashCount |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9997 | + " times, stopping: " + sr); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 9998 | EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9999 | sr.crashCount, sr.shortName, app.pid); |
| 10000 | bringDownServiceLocked(sr, true); |
| 10001 | } else if (!allowRestart) { |
| 10002 | bringDownServiceLocked(sr, true); |
| 10003 | } else { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10004 | boolean canceled = scheduleServiceRestartLocked(sr, true); |
| 10005 | |
| 10006 | // Should the service remain running? Note that in the |
| 10007 | // extreme case of so many attempts to deliver a command |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10008 | // that it failed we also will stop it here. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10009 | if (sr.startRequested && (sr.stopIfKilled || canceled)) { |
| 10010 | if (sr.pendingStarts.size() == 0) { |
| 10011 | sr.startRequested = false; |
| 10012 | if (!hasClients) { |
| 10013 | // Whoops, no reason to restart! |
| 10014 | bringDownServiceLocked(sr, true); |
| 10015 | } |
| 10016 | } |
| 10017 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10018 | } |
| 10019 | } |
| 10020 | |
| 10021 | if (!allowRestart) { |
| 10022 | app.services.clear(); |
| 10023 | } |
| 10024 | } |
| 10025 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10026 | // Make sure we have no more records on the stopping list. |
| 10027 | int i = mStoppingServices.size(); |
| 10028 | while (i > 0) { |
| 10029 | i--; |
| 10030 | ServiceRecord sr = mStoppingServices.get(i); |
| 10031 | if (sr.app == app) { |
| 10032 | mStoppingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10033 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10034 | } |
| 10035 | } |
| 10036 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10037 | app.executingServices.clear(); |
| 10038 | } |
| 10039 | |
| 10040 | private final void removeDyingProviderLocked(ProcessRecord proc, |
| 10041 | ContentProviderRecord cpr) { |
| 10042 | synchronized (cpr) { |
| 10043 | cpr.launchingApp = null; |
| 10044 | cpr.notifyAll(); |
| 10045 | } |
| 10046 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10047 | mProvidersByClass.remove(cpr.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10048 | String names[] = cpr.info.authority.split(";"); |
| 10049 | for (int j = 0; j < names.length; j++) { |
| 10050 | mProvidersByName.remove(names[j]); |
| 10051 | } |
| 10052 | |
| 10053 | Iterator<ProcessRecord> cit = cpr.clients.iterator(); |
| 10054 | while (cit.hasNext()) { |
| 10055 | ProcessRecord capp = cit.next(); |
| 10056 | if (!capp.persistent && capp.thread != null |
| 10057 | && capp.pid != 0 |
| 10058 | && capp.pid != MY_PID) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 10059 | Slog.i(TAG, "Kill " + capp.processName |
| 10060 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10061 | + " in dying process " + (proc != null ? proc.processName : "??")); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 10062 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid, |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10063 | capp.processName, capp.setAdj, "dying provider " |
| 10064 | + cpr.name.toShortString()); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10065 | Process.killProcessQuiet(capp.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10066 | } |
| 10067 | } |
| 10068 | |
| 10069 | mLaunchingProviders.remove(cpr); |
| 10070 | } |
| 10071 | |
| 10072 | /** |
| 10073 | * Main code for cleaning up a process when it has gone away. This is |
| 10074 | * called both as a result of the process dying, or directly when stopping |
| 10075 | * a process when running in single process mode. |
| 10076 | */ |
| 10077 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10078 | boolean restarting, boolean allowRestart, int index) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10079 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10080 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10081 | } |
| 10082 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10083 | mProcessesToGc.remove(app); |
| 10084 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10085 | // Dismiss any open dialogs. |
| 10086 | if (app.crashDialog != null) { |
| 10087 | app.crashDialog.dismiss(); |
| 10088 | app.crashDialog = null; |
| 10089 | } |
| 10090 | if (app.anrDialog != null) { |
| 10091 | app.anrDialog.dismiss(); |
| 10092 | app.anrDialog = null; |
| 10093 | } |
| 10094 | if (app.waitDialog != null) { |
| 10095 | app.waitDialog.dismiss(); |
| 10096 | app.waitDialog = null; |
| 10097 | } |
| 10098 | |
| 10099 | app.crashing = false; |
| 10100 | app.notResponding = false; |
| 10101 | |
| 10102 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 10103 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10104 | app.thread = null; |
| 10105 | app.forcingToForeground = null; |
| 10106 | app.foregroundServices = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10107 | app.foregroundActivities = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 10108 | app.hasShownUi = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10109 | app.hasAboveClient = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10110 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10111 | killServicesLocked(app, allowRestart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10112 | |
| 10113 | boolean restart = false; |
| 10114 | |
| 10115 | int NL = mLaunchingProviders.size(); |
| 10116 | |
| 10117 | // Remove published content providers. |
| 10118 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10119 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10120 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10121 | ContentProviderRecord cpr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10122 | cpr.provider = null; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10123 | cpr.proc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10124 | |
| 10125 | // See if someone is waiting for this provider... in which |
| 10126 | // case we don't remove it, but just let it restart. |
| 10127 | int i = 0; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10128 | if (!app.bad && allowRestart) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10129 | for (; i<NL; i++) { |
| 10130 | if (mLaunchingProviders.get(i) == cpr) { |
| 10131 | restart = true; |
| 10132 | break; |
| 10133 | } |
| 10134 | } |
| 10135 | } else { |
| 10136 | i = NL; |
| 10137 | } |
| 10138 | |
| 10139 | if (i >= NL) { |
| 10140 | removeDyingProviderLocked(app, cpr); |
| 10141 | NL = mLaunchingProviders.size(); |
| 10142 | } |
| 10143 | } |
| 10144 | app.pubProviders.clear(); |
| 10145 | } |
| 10146 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10147 | // Take care of any launching providers waiting for this process. |
| 10148 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 10149 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10150 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10152 | // Unregister from connected content providers. |
| 10153 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 10154 | Iterator it = app.conProviders.keySet().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10155 | while (it.hasNext()) { |
| 10156 | ContentProviderRecord cpr = (ContentProviderRecord)it.next(); |
| 10157 | cpr.clients.remove(app); |
| 10158 | } |
| 10159 | app.conProviders.clear(); |
| 10160 | } |
| 10161 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10162 | // At this point there may be remaining entries in mLaunchingProviders |
| 10163 | // where we were the only one waiting, so they are no longer of use. |
| 10164 | // Look for these and clean up if found. |
| 10165 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 10166 | // the actual situation to identify what is actually going on. |
| 10167 | if (false) { |
| 10168 | for (int i=0; i<NL; i++) { |
| 10169 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 10170 | mLaunchingProviders.get(i); |
| 10171 | if (cpr.clients.size() <= 0 && cpr.externals <= 0) { |
| 10172 | synchronized (cpr) { |
| 10173 | cpr.launchingApp = null; |
| 10174 | cpr.notifyAll(); |
| 10175 | } |
| 10176 | } |
| 10177 | } |
| 10178 | } |
| 10179 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10180 | skipCurrentReceiverLocked(app); |
| 10181 | |
| 10182 | // Unregister any receivers. |
| 10183 | if (app.receivers.size() > 0) { |
| 10184 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 10185 | while (it.hasNext()) { |
| 10186 | removeReceiverLocked(it.next()); |
| 10187 | } |
| 10188 | app.receivers.clear(); |
| 10189 | } |
| 10190 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10191 | // If the app is undergoing backup, tell the backup manager about it |
| 10192 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10193 | if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10194 | try { |
| 10195 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10196 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10197 | bm.agentDisconnected(app.info.packageName); |
| 10198 | } catch (RemoteException e) { |
| 10199 | // can't happen; backup manager is local |
| 10200 | } |
| 10201 | } |
| 10202 | |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 10203 | mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10204 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10205 | // If the caller is restarting this app, then leave it in its |
| 10206 | // current lists and let the caller take care of it. |
| 10207 | if (restarting) { |
| 10208 | return; |
| 10209 | } |
| 10210 | |
| 10211 | if (!app.persistent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10212 | if (DEBUG_PROCESSES) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10213 | "Removing non-persistent process during cleanup: " + app); |
| 10214 | mProcessNames.remove(app.processName, app.info.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10215 | if (mHeavyWeightProcess == app) { |
| 10216 | mHeavyWeightProcess = null; |
| 10217 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 10218 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10219 | } else if (!app.removed) { |
| 10220 | // This app is persistent, so we need to keep its record around. |
| 10221 | // If it is not already on the pending app list, add it there |
| 10222 | // and start a new process for it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10223 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 10224 | mPersistentStartingProcesses.add(app); |
| 10225 | restart = true; |
| 10226 | } |
| 10227 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10228 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 10229 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10230 | mProcessesOnHold.remove(app); |
| 10231 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 10232 | if (app == mHomeProcess) { |
| 10233 | mHomeProcess = null; |
| 10234 | } |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10235 | if (app == mPreviousProcess) { |
| 10236 | mPreviousProcess = null; |
| 10237 | } |
| 10238 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10239 | if (restart) { |
| 10240 | // We have components that still need to be running in the |
| 10241 | // process, so re-launch it. |
| 10242 | mProcessNames.put(app.processName, app.info.uid, app); |
| 10243 | startProcessLocked(app, "restart", app.processName); |
| 10244 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 10245 | // Goodbye! |
| 10246 | synchronized (mPidsSelfLocked) { |
| 10247 | mPidsSelfLocked.remove(app.pid); |
| 10248 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 10249 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10250 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10251 | } |
| 10252 | } |
| 10253 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10254 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 10255 | // Look through the content providers we are waiting to have launched, |
| 10256 | // and if any run in this process then either schedule a restart of |
| 10257 | // the process or kill the client waiting for it if this process has |
| 10258 | // gone bad. |
| 10259 | int NL = mLaunchingProviders.size(); |
| 10260 | boolean restart = false; |
| 10261 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10262 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10263 | if (cpr.launchingApp == app) { |
| 10264 | if (!alwaysBad && !app.bad) { |
| 10265 | restart = true; |
| 10266 | } else { |
| 10267 | removeDyingProviderLocked(app, cpr); |
| 10268 | NL = mLaunchingProviders.size(); |
| 10269 | } |
| 10270 | } |
| 10271 | } |
| 10272 | return restart; |
| 10273 | } |
| 10274 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10275 | // ========================================================= |
| 10276 | // SERVICES |
| 10277 | // ========================================================= |
| 10278 | |
| 10279 | ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) { |
| 10280 | ActivityManager.RunningServiceInfo info = |
| 10281 | new ActivityManager.RunningServiceInfo(); |
| 10282 | info.service = r.name; |
| 10283 | if (r.app != null) { |
| 10284 | info.pid = r.app.pid; |
| 10285 | } |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 10286 | info.uid = r.appInfo.uid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10287 | info.process = r.processName; |
| 10288 | info.foreground = r.isForeground; |
| 10289 | info.activeSince = r.createTime; |
| 10290 | info.started = r.startRequested; |
| 10291 | info.clientCount = r.connections.size(); |
| 10292 | info.crashCount = r.crashCount; |
| 10293 | info.lastActivityTime = r.lastActivity; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 10294 | if (r.isForeground) { |
| 10295 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND; |
| 10296 | } |
| 10297 | if (r.startRequested) { |
| 10298 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED; |
| 10299 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 10300 | if (r.app != null && r.app.pid == MY_PID) { |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 10301 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS; |
| 10302 | } |
| 10303 | if (r.app != null && r.app.persistent) { |
| 10304 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS; |
| 10305 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10306 | |
| 10307 | for (ArrayList<ConnectionRecord> connl : r.connections.values()) { |
| 10308 | for (int i=0; i<connl.size(); i++) { |
| 10309 | ConnectionRecord conn = connl.get(i); |
| 10310 | if (conn.clientLabel != 0) { |
| 10311 | info.clientPackage = conn.binding.client.info.packageName; |
| 10312 | info.clientLabel = conn.clientLabel; |
| 10313 | return info; |
| 10314 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10315 | } |
| 10316 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10317 | return info; |
| 10318 | } |
| 10319 | |
| 10320 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 10321 | int flags) { |
| 10322 | synchronized (this) { |
| 10323 | ArrayList<ActivityManager.RunningServiceInfo> res |
| 10324 | = new ArrayList<ActivityManager.RunningServiceInfo>(); |
| 10325 | |
| 10326 | if (mServices.size() > 0) { |
| 10327 | Iterator<ServiceRecord> it = mServices.values().iterator(); |
| 10328 | while (it.hasNext() && res.size() < maxNum) { |
| 10329 | res.add(makeRunningServiceInfoLocked(it.next())); |
| 10330 | } |
| 10331 | } |
| 10332 | |
| 10333 | for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { |
| 10334 | ServiceRecord r = mRestartingServices.get(i); |
| 10335 | ActivityManager.RunningServiceInfo info = |
| 10336 | makeRunningServiceInfoLocked(r); |
| 10337 | info.restarting = r.nextRestartTime; |
| 10338 | res.add(info); |
| 10339 | } |
| 10340 | |
| 10341 | return res; |
| 10342 | } |
| 10343 | } |
| 10344 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10345 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
| 10346 | synchronized (this) { |
| 10347 | ServiceRecord r = mServices.get(name); |
| 10348 | if (r != null) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10349 | for (ArrayList<ConnectionRecord> conn : r.connections.values()) { |
| 10350 | for (int i=0; i<conn.size(); i++) { |
| 10351 | if (conn.get(i).clientIntent != null) { |
| 10352 | return conn.get(i).clientIntent; |
| 10353 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10354 | } |
| 10355 | } |
| 10356 | } |
| 10357 | } |
| 10358 | return null; |
| 10359 | } |
| 10360 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10361 | private final ServiceRecord findServiceLocked(ComponentName name, |
| 10362 | IBinder token) { |
| 10363 | ServiceRecord r = mServices.get(name); |
| 10364 | return r == token ? r : null; |
| 10365 | } |
| 10366 | |
| 10367 | private final class ServiceLookupResult { |
| 10368 | final ServiceRecord record; |
| 10369 | final String permission; |
| 10370 | |
| 10371 | ServiceLookupResult(ServiceRecord _record, String _permission) { |
| 10372 | record = _record; |
| 10373 | permission = _permission; |
| 10374 | } |
| 10375 | }; |
| 10376 | |
| 10377 | private ServiceLookupResult findServiceLocked(Intent service, |
| 10378 | String resolvedType) { |
| 10379 | ServiceRecord r = null; |
| 10380 | if (service.getComponent() != null) { |
| 10381 | r = mServices.get(service.getComponent()); |
| 10382 | } |
| 10383 | if (r == null) { |
| 10384 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
| 10385 | r = mServicesByIntent.get(filter); |
| 10386 | } |
| 10387 | |
| 10388 | if (r == null) { |
| 10389 | try { |
| 10390 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10391 | AppGlobals.getPackageManager().resolveService( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10392 | service, resolvedType, 0); |
| 10393 | ServiceInfo sInfo = |
| 10394 | rInfo != null ? rInfo.serviceInfo : null; |
| 10395 | if (sInfo == null) { |
| 10396 | return null; |
| 10397 | } |
| 10398 | |
| 10399 | ComponentName name = new ComponentName( |
| 10400 | sInfo.applicationInfo.packageName, sInfo.name); |
| 10401 | r = mServices.get(name); |
| 10402 | } catch (RemoteException ex) { |
| 10403 | // pm is in same process, this will never happen. |
| 10404 | } |
| 10405 | } |
| 10406 | if (r != null) { |
| 10407 | int callingPid = Binder.getCallingPid(); |
| 10408 | int callingUid = Binder.getCallingUid(); |
| 10409 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10410 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10411 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10412 | if (!r.exported) { |
| 10413 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 10414 | + " from pid=" + callingPid |
| 10415 | + ", uid=" + callingUid |
| 10416 | + " that is not exported from uid " + r.appInfo.uid); |
| 10417 | return new ServiceLookupResult(null, "not exported from uid " |
| 10418 | + r.appInfo.uid); |
| 10419 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10420 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10421 | + " from pid=" + callingPid |
| 10422 | + ", uid=" + callingUid |
| 10423 | + " requires " + r.permission); |
| 10424 | return new ServiceLookupResult(null, r.permission); |
| 10425 | } |
| 10426 | return new ServiceLookupResult(r, null); |
| 10427 | } |
| 10428 | return null; |
| 10429 | } |
| 10430 | |
| 10431 | private class ServiceRestarter implements Runnable { |
| 10432 | private ServiceRecord mService; |
| 10433 | |
| 10434 | void setService(ServiceRecord service) { |
| 10435 | mService = service; |
| 10436 | } |
| 10437 | |
| 10438 | public void run() { |
| 10439 | synchronized(ActivityManagerService.this) { |
| 10440 | performServiceRestartLocked(mService); |
| 10441 | } |
| 10442 | } |
| 10443 | } |
| 10444 | |
| 10445 | private ServiceLookupResult retrieveServiceLocked(Intent service, |
| 10446 | String resolvedType, int callingPid, int callingUid) { |
| 10447 | ServiceRecord r = null; |
| 10448 | if (service.getComponent() != null) { |
| 10449 | r = mServices.get(service.getComponent()); |
| 10450 | } |
| 10451 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
| 10452 | r = mServicesByIntent.get(filter); |
| 10453 | if (r == null) { |
| 10454 | try { |
| 10455 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10456 | AppGlobals.getPackageManager().resolveService( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 10457 | service, resolvedType, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10458 | ServiceInfo sInfo = |
| 10459 | rInfo != null ? rInfo.serviceInfo : null; |
| 10460 | if (sInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10461 | Slog.w(TAG, "Unable to start service " + service + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10462 | ": not found"); |
| 10463 | return null; |
| 10464 | } |
| 10465 | |
| 10466 | ComponentName name = new ComponentName( |
| 10467 | sInfo.applicationInfo.packageName, sInfo.name); |
| 10468 | r = mServices.get(name); |
| 10469 | if (r == null) { |
| 10470 | filter = new Intent.FilterComparison(service.cloneFilter()); |
| 10471 | ServiceRestarter res = new ServiceRestarter(); |
| 10472 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 10473 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 10474 | synchronized (stats) { |
| 10475 | ss = stats.getServiceStatsLocked( |
| 10476 | sInfo.applicationInfo.uid, sInfo.packageName, |
| 10477 | sInfo.name); |
| 10478 | } |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 10479 | r = new ServiceRecord(this, ss, name, filter, sInfo, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10480 | res.setService(r); |
| 10481 | mServices.put(name, r); |
| 10482 | mServicesByIntent.put(filter, r); |
| 10483 | |
| 10484 | // Make sure this component isn't in the pending list. |
| 10485 | int N = mPendingServices.size(); |
| 10486 | for (int i=0; i<N; i++) { |
| 10487 | ServiceRecord pr = mPendingServices.get(i); |
| 10488 | if (pr.name.equals(name)) { |
| 10489 | mPendingServices.remove(i); |
| 10490 | i--; |
| 10491 | N--; |
| 10492 | } |
| 10493 | } |
| 10494 | } |
| 10495 | } catch (RemoteException ex) { |
| 10496 | // pm is in same process, this will never happen. |
| 10497 | } |
| 10498 | } |
| 10499 | if (r != null) { |
| 10500 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10501 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10502 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10503 | if (!r.exported) { |
| 10504 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 10505 | + " from pid=" + callingPid |
| 10506 | + ", uid=" + callingUid |
| 10507 | + " that is not exported from uid " + r.appInfo.uid); |
| 10508 | return new ServiceLookupResult(null, "not exported from uid " |
| 10509 | + r.appInfo.uid); |
| 10510 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10511 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10512 | + " from pid=" + callingPid |
| 10513 | + ", uid=" + callingUid |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10514 | + " requires " + r.permission); |
| 10515 | return new ServiceLookupResult(null, r.permission); |
| 10516 | } |
| 10517 | return new ServiceLookupResult(r, null); |
| 10518 | } |
| 10519 | return null; |
| 10520 | } |
| 10521 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10522 | private final void bumpServiceExecutingLocked(ServiceRecord r, String why) { |
| 10523 | if (DEBUG_SERVICE) Log.v(TAG, ">>> EXECUTING " |
| 10524 | + why + " of " + r + " in app " + r.app); |
| 10525 | else if (DEBUG_SERVICE_EXECUTING) Log.v(TAG, ">>> EXECUTING " |
| 10526 | + why + " of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10527 | long now = SystemClock.uptimeMillis(); |
| 10528 | if (r.executeNesting == 0 && r.app != null) { |
| 10529 | if (r.app.executingServices.size() == 0) { |
| 10530 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 10531 | msg.obj = r.app; |
| 10532 | mHandler.sendMessageAtTime(msg, now+SERVICE_TIMEOUT); |
| 10533 | } |
| 10534 | r.app.executingServices.add(r); |
| 10535 | } |
| 10536 | r.executeNesting++; |
| 10537 | r.executingStart = now; |
| 10538 | } |
| 10539 | |
| 10540 | private final void sendServiceArgsLocked(ServiceRecord r, |
| 10541 | boolean oomAdjusted) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10542 | final int N = r.pendingStarts.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10543 | if (N == 0) { |
| 10544 | return; |
| 10545 | } |
| 10546 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10547 | while (r.pendingStarts.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10548 | try { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10549 | ServiceRecord.StartItem si = r.pendingStarts.remove(0); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10550 | if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to: " |
| 10551 | + r + " " + r.intent + " args=" + si.intent); |
Dianne Hackborn | 3a28f22 | 2011-03-01 12:25:54 -0800 | [diff] [blame] | 10552 | if (si.intent == null && N > 1) { |
| 10553 | // If somehow we got a dummy null intent in the middle, |
| 10554 | // then skip it. DO NOT skip a null intent when it is |
| 10555 | // the only one in the list -- this is to support the |
| 10556 | // onStartCommand(null) case. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10557 | continue; |
| 10558 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10559 | si.deliveredTime = SystemClock.uptimeMillis(); |
| 10560 | r.deliveredStarts.add(si); |
| 10561 | si.deliveryCount++; |
Dianne Hackborn | 3a28f22 | 2011-03-01 12:25:54 -0800 | [diff] [blame] | 10562 | if (si.targetPermissionUid >= 0 && si.intent != null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10563 | grantUriPermissionUncheckedFromIntentLocked(si.targetPermissionUid, |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 10564 | r.packageName, si.intent, si.getUriPermissionsLocked()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10565 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10566 | bumpServiceExecutingLocked(r, "start"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10567 | if (!oomAdjusted) { |
| 10568 | oomAdjusted = true; |
| 10569 | updateOomAdjLocked(r.app); |
| 10570 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10571 | int flags = 0; |
| 10572 | if (si.deliveryCount > 0) { |
| 10573 | flags |= Service.START_FLAG_RETRY; |
| 10574 | } |
| 10575 | if (si.doneExecutingCount > 0) { |
| 10576 | flags |= Service.START_FLAG_REDELIVERY; |
| 10577 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10578 | r.app.thread.scheduleServiceArgs(r, si.taskRemoved, si.id, flags, si.intent); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10579 | } catch (RemoteException e) { |
| 10580 | // Remote process gone... we'll let the normal cleanup take |
| 10581 | // care of this. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10582 | if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while scheduling start: " + r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10583 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10584 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10585 | Slog.w(TAG, "Unexpected exception", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10586 | break; |
| 10587 | } |
| 10588 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10589 | } |
| 10590 | |
| 10591 | private final boolean requestServiceBindingLocked(ServiceRecord r, |
| 10592 | IntentBindRecord i, boolean rebind) { |
| 10593 | if (r.app == null || r.app.thread == null) { |
| 10594 | // If service is not currently running, can't yet bind. |
| 10595 | return false; |
| 10596 | } |
| 10597 | if ((!i.requested || rebind) && i.apps.size() > 0) { |
| 10598 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10599 | bumpServiceExecutingLocked(r, "bind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10600 | r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind); |
| 10601 | if (!rebind) { |
| 10602 | i.requested = true; |
| 10603 | } |
| 10604 | i.hasBound = true; |
| 10605 | i.doRebind = false; |
| 10606 | } catch (RemoteException e) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10607 | if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while binding " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10608 | return false; |
| 10609 | } |
| 10610 | } |
| 10611 | return true; |
| 10612 | } |
| 10613 | |
| 10614 | private final void requestServiceBindingsLocked(ServiceRecord r) { |
| 10615 | Iterator<IntentBindRecord> bindings = r.bindings.values().iterator(); |
| 10616 | while (bindings.hasNext()) { |
| 10617 | IntentBindRecord i = bindings.next(); |
| 10618 | if (!requestServiceBindingLocked(r, i, false)) { |
| 10619 | break; |
| 10620 | } |
| 10621 | } |
| 10622 | } |
| 10623 | |
| 10624 | private final void realStartServiceLocked(ServiceRecord r, |
| 10625 | ProcessRecord app) throws RemoteException { |
| 10626 | if (app.thread == null) { |
| 10627 | throw new RemoteException(); |
| 10628 | } |
| 10629 | |
| 10630 | r.app = app; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 10631 | r.restartTime = r.lastActivity = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10632 | |
| 10633 | app.services.add(r); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10634 | bumpServiceExecutingLocked(r, "create"); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10635 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10636 | |
| 10637 | boolean created = false; |
| 10638 | try { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 10639 | mStringBuilder.setLength(0); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 10640 | r.intent.getIntent().toShortString(mStringBuilder, true, false, true); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10641 | EventLog.writeEvent(EventLogTags.AM_CREATE_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10642 | System.identityHashCode(r), r.shortName, |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 10643 | mStringBuilder.toString(), r.app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10644 | synchronized (r.stats.getBatteryStats()) { |
| 10645 | r.stats.startLaunchedLocked(); |
| 10646 | } |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 10647 | ensurePackageDexOpt(r.serviceInfo.packageName); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 10648 | app.thread.scheduleCreateService(r, r.serviceInfo, |
| 10649 | compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo)); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10650 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10651 | created = true; |
| 10652 | } finally { |
| 10653 | if (!created) { |
| 10654 | app.services.remove(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10655 | scheduleServiceRestartLocked(r, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10656 | } |
| 10657 | } |
| 10658 | |
| 10659 | requestServiceBindingsLocked(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10660 | |
| 10661 | // If the service is in the started state, and there are no |
| 10662 | // pending arguments, then fake up one so its onStartCommand() will |
| 10663 | // be called. |
| 10664 | if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10665 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
| 10666 | null, -1)); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10667 | } |
| 10668 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10669 | sendServiceArgsLocked(r, true); |
| 10670 | } |
| 10671 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10672 | private final boolean scheduleServiceRestartLocked(ServiceRecord r, |
| 10673 | boolean allowCancel) { |
| 10674 | boolean canceled = false; |
| 10675 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10676 | final long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10677 | long minDuration = SERVICE_RESTART_DURATION; |
Dianne Hackborn | 6ccd2af | 2009-08-27 12:26:44 -0700 | [diff] [blame] | 10678 | long resetTime = SERVICE_RESET_RUN_DURATION; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10679 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 10680 | if ((r.serviceInfo.applicationInfo.flags |
| 10681 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 10682 | minDuration /= 4; |
| 10683 | } |
| 10684 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10685 | // Any delivered but not yet finished starts should be put back |
| 10686 | // on the pending list. |
| 10687 | final int N = r.deliveredStarts.size(); |
| 10688 | if (N > 0) { |
| 10689 | for (int i=N-1; i>=0; i--) { |
| 10690 | ServiceRecord.StartItem si = r.deliveredStarts.get(i); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10691 | si.removeUriPermissionsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10692 | if (si.intent == null) { |
| 10693 | // We'll generate this again if needed. |
| 10694 | } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT |
| 10695 | && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) { |
| 10696 | r.pendingStarts.add(0, si); |
| 10697 | long dur = SystemClock.uptimeMillis() - si.deliveredTime; |
| 10698 | dur *= 2; |
| 10699 | if (minDuration < dur) minDuration = dur; |
| 10700 | if (resetTime < dur) resetTime = dur; |
| 10701 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10702 | Slog.w(TAG, "Canceling start item " + si.intent + " in service " |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10703 | + r.name); |
| 10704 | canceled = true; |
| 10705 | } |
| 10706 | } |
| 10707 | r.deliveredStarts.clear(); |
| 10708 | } |
| 10709 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10710 | r.totalRestartCount++; |
| 10711 | if (r.restartDelay == 0) { |
| 10712 | r.restartCount++; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10713 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10714 | } else { |
| 10715 | // If it has been a "reasonably long time" since the service |
| 10716 | // was started, then reset our restart duration back to |
| 10717 | // the beginning, so we don't infinitely increase the duration |
| 10718 | // on a service that just occasionally gets killed (which is |
| 10719 | // a normal case, due to process being killed to reclaim memory). |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10720 | if (now > (r.restartTime+resetTime)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10721 | r.restartCount = 1; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10722 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10723 | } else { |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 10724 | if ((r.serviceInfo.applicationInfo.flags |
| 10725 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 10726 | // Services in peristent processes will restart much more |
| 10727 | // quickly, since they are pretty important. (Think SystemUI). |
| 10728 | r.restartDelay += minDuration/2; |
| 10729 | } else { |
| 10730 | r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR; |
| 10731 | if (r.restartDelay < minDuration) { |
| 10732 | r.restartDelay = minDuration; |
| 10733 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10734 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10735 | } |
| 10736 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10737 | |
| 10738 | r.nextRestartTime = now + r.restartDelay; |
| 10739 | |
| 10740 | // Make sure that we don't end up restarting a bunch of services |
| 10741 | // all at the same time. |
| 10742 | boolean repeat; |
| 10743 | do { |
| 10744 | repeat = false; |
| 10745 | for (int i=mRestartingServices.size()-1; i>=0; i--) { |
| 10746 | ServiceRecord r2 = mRestartingServices.get(i); |
| 10747 | if (r2 != r && r.nextRestartTime |
| 10748 | >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN) |
| 10749 | && r.nextRestartTime |
| 10750 | < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) { |
| 10751 | r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN; |
| 10752 | r.restartDelay = r.nextRestartTime - now; |
| 10753 | repeat = true; |
| 10754 | break; |
| 10755 | } |
| 10756 | } |
| 10757 | } while (repeat); |
| 10758 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10759 | if (!mRestartingServices.contains(r)) { |
| 10760 | mRestartingServices.add(r); |
| 10761 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10762 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10763 | r.cancelNotification(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10764 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10765 | mHandler.removeCallbacks(r.restarter); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10766 | mHandler.postAtTime(r.restarter, r.nextRestartTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10767 | r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10768 | Slog.w(TAG, "Scheduling restart of crashed service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10769 | + r.shortName + " in " + r.restartDelay + "ms"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10770 | EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10771 | r.shortName, r.restartDelay); |
| 10772 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10773 | return canceled; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10774 | } |
| 10775 | |
| 10776 | final void performServiceRestartLocked(ServiceRecord r) { |
| 10777 | if (!mRestartingServices.contains(r)) { |
| 10778 | return; |
| 10779 | } |
| 10780 | bringUpServiceLocked(r, r.intent.getIntent().getFlags(), true); |
| 10781 | } |
| 10782 | |
| 10783 | private final boolean unscheduleServiceRestartLocked(ServiceRecord r) { |
| 10784 | if (r.restartDelay == 0) { |
| 10785 | return false; |
| 10786 | } |
| 10787 | r.resetRestartCounter(); |
| 10788 | mRestartingServices.remove(r); |
| 10789 | mHandler.removeCallbacks(r.restarter); |
| 10790 | return true; |
| 10791 | } |
| 10792 | |
| 10793 | private final boolean bringUpServiceLocked(ServiceRecord r, |
| 10794 | int intentFlags, boolean whileRestarting) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10795 | //Slog.i(TAG, "Bring up service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10796 | //r.dump(" "); |
| 10797 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10798 | if (r.app != null && r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10799 | sendServiceArgsLocked(r, false); |
| 10800 | return true; |
| 10801 | } |
| 10802 | |
| 10803 | if (!whileRestarting && r.restartDelay > 0) { |
| 10804 | // If waiting for a restart, then do nothing. |
| 10805 | return true; |
| 10806 | } |
| 10807 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10808 | if (DEBUG_SERVICE) Slog.v(TAG, "Bringing up " + r + " " + r.intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10809 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10810 | // We are now bringing the service up, so no longer in the |
| 10811 | // restarting state. |
| 10812 | mRestartingServices.remove(r); |
| 10813 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10814 | // Service is now being launched, its package can't be stopped. |
| 10815 | try { |
| 10816 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 10817 | r.packageName, false); |
| 10818 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 10819 | } catch (IllegalArgumentException e) { |
| 10820 | Slog.w(TAG, "Failed trying to unstop package " |
| 10821 | + r.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10822 | } |
| 10823 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10824 | final String appName = r.processName; |
| 10825 | ProcessRecord app = getProcessRecordLocked(appName, r.appInfo.uid); |
| 10826 | if (app != null && app.thread != null) { |
| 10827 | try { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 10828 | app.addPackage(r.appInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10829 | realStartServiceLocked(r, app); |
| 10830 | return true; |
| 10831 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10832 | Slog.w(TAG, "Exception when starting service " + r.shortName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10833 | } |
| 10834 | |
| 10835 | // If a dead object exception was thrown -- fall through to |
| 10836 | // restart the application. |
| 10837 | } |
| 10838 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10839 | // Not running -- get it started, and enqueue this service record |
| 10840 | // to be executed when the app comes up. |
| 10841 | if (startProcessLocked(appName, r.appInfo, true, intentFlags, |
| 10842 | "service", r.name, false) == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10843 | Slog.w(TAG, "Unable to launch app " |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10844 | + r.appInfo.packageName + "/" |
| 10845 | + r.appInfo.uid + " for service " |
| 10846 | + r.intent.getIntent() + ": process is bad"); |
| 10847 | bringDownServiceLocked(r, true); |
| 10848 | return false; |
| 10849 | } |
| 10850 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10851 | if (!mPendingServices.contains(r)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10852 | mPendingServices.add(r); |
| 10853 | } |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10854 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10855 | return true; |
| 10856 | } |
| 10857 | |
| 10858 | private final void bringDownServiceLocked(ServiceRecord r, boolean force) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10859 | //Slog.i(TAG, "Bring down service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10860 | //r.dump(" "); |
| 10861 | |
| 10862 | // Does it still need to run? |
| 10863 | if (!force && r.startRequested) { |
| 10864 | return; |
| 10865 | } |
| 10866 | if (r.connections.size() > 0) { |
| 10867 | if (!force) { |
| 10868 | // XXX should probably keep a count of the number of auto-create |
| 10869 | // connections directly in the service. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10870 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10871 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10872 | ArrayList<ConnectionRecord> cr = it.next(); |
| 10873 | for (int i=0; i<cr.size(); i++) { |
| 10874 | if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) { |
| 10875 | return; |
| 10876 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10877 | } |
| 10878 | } |
| 10879 | } |
| 10880 | |
| 10881 | // Report to all of the connections that the service is no longer |
| 10882 | // available. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10883 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10884 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10885 | ArrayList<ConnectionRecord> c = it.next(); |
| 10886 | for (int i=0; i<c.size(); i++) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10887 | ConnectionRecord cr = c.get(i); |
| 10888 | // There is still a connection to the service that is |
| 10889 | // being brought down. Mark it as dead. |
| 10890 | cr.serviceDead = true; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10891 | try { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10892 | cr.conn.connected(r.name, null); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10893 | } catch (Exception e) { |
| 10894 | Slog.w(TAG, "Failure disconnecting service " + r.name + |
| 10895 | " to connection " + c.get(i).conn.asBinder() + |
| 10896 | " (in " + c.get(i).binding.client.processName + ")", e); |
| 10897 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10898 | } |
| 10899 | } |
| 10900 | } |
| 10901 | |
| 10902 | // Tell the service that it has been unbound. |
| 10903 | if (r.bindings.size() > 0 && r.app != null && r.app.thread != null) { |
| 10904 | Iterator<IntentBindRecord> it = r.bindings.values().iterator(); |
| 10905 | while (it.hasNext()) { |
| 10906 | IntentBindRecord ibr = it.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10907 | if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down binding " + ibr |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10908 | + ": hasBound=" + ibr.hasBound); |
| 10909 | if (r.app != null && r.app.thread != null && ibr.hasBound) { |
| 10910 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10911 | bumpServiceExecutingLocked(r, "bring down unbind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10912 | updateOomAdjLocked(r.app); |
| 10913 | ibr.hasBound = false; |
| 10914 | r.app.thread.scheduleUnbindService(r, |
| 10915 | ibr.intent.getIntent()); |
| 10916 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10917 | Slog.w(TAG, "Exception when unbinding service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10918 | + r.shortName, e); |
| 10919 | serviceDoneExecutingLocked(r, true); |
| 10920 | } |
| 10921 | } |
| 10922 | } |
| 10923 | } |
| 10924 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10925 | if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down " + r + " " + r.intent); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10926 | EventLog.writeEvent(EventLogTags.AM_DESTROY_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10927 | System.identityHashCode(r), r.shortName, |
| 10928 | (r.app != null) ? r.app.pid : -1); |
| 10929 | |
| 10930 | mServices.remove(r.name); |
| 10931 | mServicesByIntent.remove(r.intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10932 | r.totalRestartCount = 0; |
| 10933 | unscheduleServiceRestartLocked(r); |
| 10934 | |
| 10935 | // Also make sure it is not on the pending list. |
| 10936 | int N = mPendingServices.size(); |
| 10937 | for (int i=0; i<N; i++) { |
| 10938 | if (mPendingServices.get(i) == r) { |
| 10939 | mPendingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10940 | if (DEBUG_SERVICE) Slog.v(TAG, "Removed pending: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10941 | i--; |
| 10942 | N--; |
| 10943 | } |
| 10944 | } |
| 10945 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10946 | r.cancelNotification(); |
| 10947 | r.isForeground = false; |
| 10948 | r.foregroundId = 0; |
| 10949 | r.foregroundNoti = null; |
| 10950 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10951 | // Clear start entries. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10952 | r.clearDeliveredStartsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10953 | r.pendingStarts.clear(); |
| 10954 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10955 | if (r.app != null) { |
| 10956 | synchronized (r.stats.getBatteryStats()) { |
| 10957 | r.stats.stopLaunchedLocked(); |
| 10958 | } |
| 10959 | r.app.services.remove(r); |
| 10960 | if (r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10961 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10962 | bumpServiceExecutingLocked(r, "stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10963 | mStoppingServices.add(r); |
| 10964 | updateOomAdjLocked(r.app); |
| 10965 | r.app.thread.scheduleStopService(r); |
| 10966 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10967 | Slog.w(TAG, "Exception when stopping service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10968 | + r.shortName, e); |
| 10969 | serviceDoneExecutingLocked(r, true); |
| 10970 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10971 | updateServiceForegroundLocked(r.app, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10972 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10973 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10974 | TAG, "Removed service that has no process: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10975 | } |
| 10976 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10977 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10978 | TAG, "Removed service that is not running: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10979 | } |
Vairavan Srinivasan | a207ce2 | 2010-12-23 13:51:48 -0800 | [diff] [blame] | 10980 | |
| 10981 | if (r.bindings.size() > 0) { |
| 10982 | r.bindings.clear(); |
| 10983 | } |
| 10984 | |
| 10985 | if (r.restarter instanceof ServiceRestarter) { |
| 10986 | ((ServiceRestarter)r.restarter).setService(null); |
| 10987 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10988 | } |
| 10989 | |
| 10990 | ComponentName startServiceLocked(IApplicationThread caller, |
| 10991 | Intent service, String resolvedType, |
| 10992 | int callingPid, int callingUid) { |
| 10993 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10994 | if (DEBUG_SERVICE) Slog.v(TAG, "startService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10995 | + " type=" + resolvedType + " args=" + service.getExtras()); |
| 10996 | |
| 10997 | if (caller != null) { |
| 10998 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 10999 | if (callerApp == null) { |
| 11000 | throw new SecurityException( |
| 11001 | "Unable to find app for caller " + caller |
| 11002 | + " (pid=" + Binder.getCallingPid() |
| 11003 | + ") when starting service " + service); |
| 11004 | } |
| 11005 | } |
| 11006 | |
| 11007 | ServiceLookupResult res = |
| 11008 | retrieveServiceLocked(service, resolvedType, |
| 11009 | callingPid, callingUid); |
| 11010 | if (res == null) { |
| 11011 | return null; |
| 11012 | } |
| 11013 | if (res.record == null) { |
| 11014 | return new ComponentName("!", res.permission != null |
| 11015 | ? res.permission : "private to package"); |
| 11016 | } |
| 11017 | ServiceRecord r = res.record; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11018 | int targetPermissionUid = checkGrantUriPermissionFromIntentLocked( |
| 11019 | callingUid, r.packageName, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11020 | if (unscheduleServiceRestartLocked(r)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11021 | if (DEBUG_SERVICE) Slog.v(TAG, "START SERVICE WHILE RESTART PENDING: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11022 | } |
| 11023 | r.startRequested = true; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11024 | r.callStart = false; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11025 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11026 | service, targetPermissionUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11027 | r.lastActivity = SystemClock.uptimeMillis(); |
| 11028 | synchronized (r.stats.getBatteryStats()) { |
| 11029 | r.stats.startRunningLocked(); |
| 11030 | } |
| 11031 | if (!bringUpServiceLocked(r, service.getFlags(), false)) { |
| 11032 | return new ComponentName("!", "Service process is bad"); |
| 11033 | } |
| 11034 | return r.name; |
| 11035 | } |
| 11036 | } |
| 11037 | |
| 11038 | public ComponentName startService(IApplicationThread caller, Intent service, |
| 11039 | String resolvedType) { |
| 11040 | // Refuse possible leaked file descriptors |
| 11041 | if (service != null && service.hasFileDescriptors() == true) { |
| 11042 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11043 | } |
| 11044 | |
| 11045 | synchronized(this) { |
| 11046 | final int callingPid = Binder.getCallingPid(); |
| 11047 | final int callingUid = Binder.getCallingUid(); |
| 11048 | final long origId = Binder.clearCallingIdentity(); |
| 11049 | ComponentName res = startServiceLocked(caller, service, |
| 11050 | resolvedType, callingPid, callingUid); |
| 11051 | Binder.restoreCallingIdentity(origId); |
| 11052 | return res; |
| 11053 | } |
| 11054 | } |
| 11055 | |
| 11056 | ComponentName startServiceInPackage(int uid, |
| 11057 | Intent service, String resolvedType) { |
| 11058 | synchronized(this) { |
| 11059 | final long origId = Binder.clearCallingIdentity(); |
| 11060 | ComponentName res = startServiceLocked(null, service, |
| 11061 | resolvedType, -1, uid); |
| 11062 | Binder.restoreCallingIdentity(origId); |
| 11063 | return res; |
| 11064 | } |
| 11065 | } |
| 11066 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11067 | private void stopServiceLocked(ServiceRecord service) { |
| 11068 | synchronized (service.stats.getBatteryStats()) { |
| 11069 | service.stats.stopRunningLocked(); |
| 11070 | } |
| 11071 | service.startRequested = false; |
| 11072 | service.callStart = false; |
| 11073 | bringDownServiceLocked(service, false); |
| 11074 | } |
| 11075 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11076 | public int stopService(IApplicationThread caller, Intent service, |
| 11077 | String resolvedType) { |
| 11078 | // Refuse possible leaked file descriptors |
| 11079 | if (service != null && service.hasFileDescriptors() == true) { |
| 11080 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11081 | } |
| 11082 | |
| 11083 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11084 | if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11085 | + " type=" + resolvedType); |
| 11086 | |
| 11087 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11088 | if (caller != null && callerApp == null) { |
| 11089 | throw new SecurityException( |
| 11090 | "Unable to find app for caller " + caller |
| 11091 | + " (pid=" + Binder.getCallingPid() |
| 11092 | + ") when stopping service " + service); |
| 11093 | } |
| 11094 | |
| 11095 | // If this service is active, make sure it is stopped. |
| 11096 | ServiceLookupResult r = findServiceLocked(service, resolvedType); |
| 11097 | if (r != null) { |
| 11098 | if (r.record != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11099 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11100 | try { |
| 11101 | stopServiceLocked(r.record); |
| 11102 | } finally { |
| 11103 | Binder.restoreCallingIdentity(origId); |
| 11104 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11105 | return 1; |
| 11106 | } |
| 11107 | return -1; |
| 11108 | } |
| 11109 | } |
| 11110 | |
| 11111 | return 0; |
| 11112 | } |
| 11113 | |
| 11114 | public IBinder peekService(Intent service, String resolvedType) { |
| 11115 | // Refuse possible leaked file descriptors |
| 11116 | if (service != null && service.hasFileDescriptors() == true) { |
| 11117 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11118 | } |
| 11119 | |
| 11120 | IBinder ret = null; |
| 11121 | |
| 11122 | synchronized(this) { |
| 11123 | ServiceLookupResult r = findServiceLocked(service, resolvedType); |
| 11124 | |
| 11125 | if (r != null) { |
| 11126 | // r.record is null if findServiceLocked() failed the caller permission check |
| 11127 | if (r.record == null) { |
| 11128 | throw new SecurityException( |
| 11129 | "Permission Denial: Accessing service " + r.record.name |
| 11130 | + " from pid=" + Binder.getCallingPid() |
| 11131 | + ", uid=" + Binder.getCallingUid() |
| 11132 | + " requires " + r.permission); |
| 11133 | } |
| 11134 | IntentBindRecord ib = r.record.bindings.get(r.record.intent); |
| 11135 | if (ib != null) { |
| 11136 | ret = ib.binder; |
| 11137 | } |
| 11138 | } |
| 11139 | } |
| 11140 | |
| 11141 | return ret; |
| 11142 | } |
| 11143 | |
| 11144 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 11145 | int startId) { |
| 11146 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11147 | if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11148 | + " " + token + " startId=" + startId); |
| 11149 | ServiceRecord r = findServiceLocked(className, token); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11150 | if (r != null) { |
| 11151 | if (startId >= 0) { |
| 11152 | // Asked to only stop if done with all work. Note that |
| 11153 | // to avoid leaks, we will take this as dropping all |
| 11154 | // start items up to and including this one. |
| 11155 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 11156 | if (si != null) { |
| 11157 | while (r.deliveredStarts.size() > 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11158 | ServiceRecord.StartItem cur = r.deliveredStarts.remove(0); |
| 11159 | cur.removeUriPermissionsLocked(); |
| 11160 | if (cur == si) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11161 | break; |
| 11162 | } |
| 11163 | } |
| 11164 | } |
| 11165 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11166 | if (r.getLastStartId() != startId) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11167 | return false; |
| 11168 | } |
| 11169 | |
| 11170 | if (r.deliveredStarts.size() > 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11171 | Slog.w(TAG, "stopServiceToken startId " + startId |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11172 | + " is last, but have " + r.deliveredStarts.size() |
| 11173 | + " remaining args"); |
| 11174 | } |
| 11175 | } |
| 11176 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11177 | synchronized (r.stats.getBatteryStats()) { |
| 11178 | r.stats.stopRunningLocked(); |
| 11179 | r.startRequested = false; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11180 | r.callStart = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11181 | } |
| 11182 | final long origId = Binder.clearCallingIdentity(); |
| 11183 | bringDownServiceLocked(r, false); |
| 11184 | Binder.restoreCallingIdentity(origId); |
| 11185 | return true; |
| 11186 | } |
| 11187 | } |
| 11188 | return false; |
| 11189 | } |
| 11190 | |
| 11191 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11192 | int id, Notification notification, boolean removeNotification) { |
| 11193 | final long origId = Binder.clearCallingIdentity(); |
| 11194 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11195 | synchronized(this) { |
| 11196 | ServiceRecord r = findServiceLocked(className, token); |
| 11197 | if (r != null) { |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11198 | if (id != 0) { |
| 11199 | if (notification == null) { |
| 11200 | throw new IllegalArgumentException("null notification"); |
| 11201 | } |
| 11202 | if (r.foregroundId != id) { |
| 11203 | r.cancelNotification(); |
| 11204 | r.foregroundId = id; |
| 11205 | } |
| 11206 | notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 11207 | r.foregroundNoti = notification; |
| 11208 | r.isForeground = true; |
| 11209 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11210 | if (r.app != null) { |
| 11211 | updateServiceForegroundLocked(r.app, true); |
| 11212 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11213 | } else { |
| 11214 | if (r.isForeground) { |
| 11215 | r.isForeground = false; |
| 11216 | if (r.app != null) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 11217 | updateLruProcessLocked(r.app, false, true); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11218 | updateServiceForegroundLocked(r.app, true); |
| 11219 | } |
| 11220 | } |
| 11221 | if (removeNotification) { |
| 11222 | r.cancelNotification(); |
| 11223 | r.foregroundId = 0; |
| 11224 | r.foregroundNoti = null; |
| 11225 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11226 | } |
| 11227 | } |
| 11228 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11229 | } finally { |
| 11230 | Binder.restoreCallingIdentity(origId); |
| 11231 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11232 | } |
| 11233 | |
| 11234 | public void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) { |
| 11235 | boolean anyForeground = false; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 11236 | for (ServiceRecord sr : proc.services) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11237 | if (sr.isForeground) { |
| 11238 | anyForeground = true; |
| 11239 | break; |
| 11240 | } |
| 11241 | } |
| 11242 | if (anyForeground != proc.foregroundServices) { |
| 11243 | proc.foregroundServices = anyForeground; |
| 11244 | if (oomAdj) { |
| 11245 | updateOomAdjLocked(); |
| 11246 | } |
| 11247 | } |
| 11248 | } |
| 11249 | |
| 11250 | public int bindService(IApplicationThread caller, IBinder token, |
| 11251 | Intent service, String resolvedType, |
| 11252 | IServiceConnection connection, int flags) { |
| 11253 | // Refuse possible leaked file descriptors |
| 11254 | if (service != null && service.hasFileDescriptors() == true) { |
| 11255 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11256 | } |
| 11257 | |
| 11258 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11259 | if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11260 | + " type=" + resolvedType + " conn=" + connection.asBinder() |
| 11261 | + " flags=0x" + Integer.toHexString(flags)); |
| 11262 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11263 | if (callerApp == null) { |
| 11264 | throw new SecurityException( |
| 11265 | "Unable to find app for caller " + caller |
| 11266 | + " (pid=" + Binder.getCallingPid() |
| 11267 | + ") when binding service " + service); |
| 11268 | } |
| 11269 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11270 | ActivityRecord activity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11271 | if (token != null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 11272 | activity = mMainStack.isInStackLocked(token); |
| 11273 | if (activity == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11274 | Slog.w(TAG, "Binding with unknown activity: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11275 | return 0; |
| 11276 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11277 | } |
| 11278 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11279 | int clientLabel = 0; |
| 11280 | PendingIntent clientIntent = null; |
| 11281 | |
| 11282 | if (callerApp.info.uid == Process.SYSTEM_UID) { |
| 11283 | // Hacky kind of thing -- allow system stuff to tell us |
| 11284 | // what they are, so we can report this elsewhere for |
| 11285 | // others to know why certain services are running. |
| 11286 | try { |
| 11287 | clientIntent = (PendingIntent)service.getParcelableExtra( |
| 11288 | Intent.EXTRA_CLIENT_INTENT); |
| 11289 | } catch (RuntimeException e) { |
| 11290 | } |
| 11291 | if (clientIntent != null) { |
| 11292 | clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0); |
| 11293 | if (clientLabel != 0) { |
| 11294 | // There are no useful extras in the intent, trash them. |
| 11295 | // System code calling with this stuff just needs to know |
| 11296 | // this will happen. |
| 11297 | service = service.cloneFilter(); |
| 11298 | } |
| 11299 | } |
| 11300 | } |
| 11301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11302 | ServiceLookupResult res = |
| 11303 | retrieveServiceLocked(service, resolvedType, |
| 11304 | Binder.getCallingPid(), Binder.getCallingUid()); |
| 11305 | if (res == null) { |
| 11306 | return 0; |
| 11307 | } |
| 11308 | if (res.record == null) { |
| 11309 | return -1; |
| 11310 | } |
| 11311 | ServiceRecord s = res.record; |
| 11312 | |
| 11313 | final long origId = Binder.clearCallingIdentity(); |
| 11314 | |
| 11315 | if (unscheduleServiceRestartLocked(s)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11316 | if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: " |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11317 | + s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11318 | } |
| 11319 | |
| 11320 | AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp); |
| 11321 | ConnectionRecord c = new ConnectionRecord(b, activity, |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11322 | connection, flags, clientLabel, clientIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11323 | |
| 11324 | IBinder binder = connection.asBinder(); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11325 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 11326 | if (clist == null) { |
| 11327 | clist = new ArrayList<ConnectionRecord>(); |
| 11328 | s.connections.put(binder, clist); |
| 11329 | } |
| 11330 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11331 | b.connections.add(c); |
| 11332 | if (activity != null) { |
| 11333 | if (activity.connections == null) { |
| 11334 | activity.connections = new HashSet<ConnectionRecord>(); |
| 11335 | } |
| 11336 | activity.connections.add(c); |
| 11337 | } |
| 11338 | b.client.connections.add(c); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 11339 | if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { |
| 11340 | b.client.hasAboveClient = true; |
| 11341 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11342 | clist = mServiceConnections.get(binder); |
| 11343 | if (clist == null) { |
| 11344 | clist = new ArrayList<ConnectionRecord>(); |
| 11345 | mServiceConnections.put(binder, clist); |
| 11346 | } |
| 11347 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11348 | |
| 11349 | if ((flags&Context.BIND_AUTO_CREATE) != 0) { |
| 11350 | s.lastActivity = SystemClock.uptimeMillis(); |
| 11351 | if (!bringUpServiceLocked(s, service.getFlags(), false)) { |
| 11352 | return 0; |
| 11353 | } |
| 11354 | } |
| 11355 | |
| 11356 | if (s.app != null) { |
| 11357 | // This could have made the service more important. |
| 11358 | updateOomAdjLocked(s.app); |
| 11359 | } |
| 11360 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11361 | if (DEBUG_SERVICE) Slog.v(TAG, "Bind " + s + " with " + b |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11362 | + ": received=" + b.intent.received |
| 11363 | + " apps=" + b.intent.apps.size() |
| 11364 | + " doRebind=" + b.intent.doRebind); |
| 11365 | |
| 11366 | if (s.app != null && b.intent.received) { |
| 11367 | // Service is already running, so we can immediately |
| 11368 | // publish the connection. |
| 11369 | try { |
| 11370 | c.conn.connected(s.name, b.intent.binder); |
| 11371 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11372 | Slog.w(TAG, "Failure sending service " + s.shortName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11373 | + " to connection " + c.conn.asBinder() |
| 11374 | + " (in " + c.binding.client.processName + ")", e); |
| 11375 | } |
| 11376 | |
| 11377 | // If this is the first app connected back to this binding, |
| 11378 | // and the service had previously asked to be told when |
| 11379 | // rebound, then do so. |
| 11380 | if (b.intent.apps.size() == 1 && b.intent.doRebind) { |
| 11381 | requestServiceBindingLocked(s, b.intent, true); |
| 11382 | } |
| 11383 | } else if (!b.intent.requested) { |
| 11384 | requestServiceBindingLocked(s, b.intent, false); |
| 11385 | } |
| 11386 | |
| 11387 | Binder.restoreCallingIdentity(origId); |
| 11388 | } |
| 11389 | |
| 11390 | return 1; |
| 11391 | } |
| 11392 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 11393 | void removeConnectionLocked( |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11394 | ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11395 | IBinder binder = c.conn.asBinder(); |
| 11396 | AppBindRecord b = c.binding; |
| 11397 | ServiceRecord s = b.service; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11398 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 11399 | if (clist != null) { |
| 11400 | clist.remove(c); |
| 11401 | if (clist.size() == 0) { |
| 11402 | s.connections.remove(binder); |
| 11403 | } |
| 11404 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11405 | b.connections.remove(c); |
| 11406 | if (c.activity != null && c.activity != skipAct) { |
| 11407 | if (c.activity.connections != null) { |
| 11408 | c.activity.connections.remove(c); |
| 11409 | } |
| 11410 | } |
| 11411 | if (b.client != skipApp) { |
| 11412 | b.client.connections.remove(c); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 11413 | if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { |
| 11414 | b.client.updateHasAboveClientLocked(); |
| 11415 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11416 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11417 | clist = mServiceConnections.get(binder); |
| 11418 | if (clist != null) { |
| 11419 | clist.remove(c); |
| 11420 | if (clist.size() == 0) { |
| 11421 | mServiceConnections.remove(binder); |
| 11422 | } |
| 11423 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11424 | |
| 11425 | if (b.connections.size() == 0) { |
| 11426 | b.intent.apps.remove(b.client); |
| 11427 | } |
| 11428 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11429 | if (!c.serviceDead) { |
| 11430 | if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent |
| 11431 | + ": shouldUnbind=" + b.intent.hasBound); |
| 11432 | if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0 |
| 11433 | && b.intent.hasBound) { |
| 11434 | try { |
| 11435 | bumpServiceExecutingLocked(s, "unbind"); |
| 11436 | updateOomAdjLocked(s.app); |
| 11437 | b.intent.hasBound = false; |
| 11438 | // Assume the client doesn't want to know about a rebind; |
| 11439 | // we will deal with that later if it asks for one. |
| 11440 | b.intent.doRebind = false; |
| 11441 | s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent()); |
| 11442 | } catch (Exception e) { |
| 11443 | Slog.w(TAG, "Exception when unbinding service " + s.shortName, e); |
| 11444 | serviceDoneExecutingLocked(s, true); |
| 11445 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11446 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11447 | |
| 11448 | if ((c.flags&Context.BIND_AUTO_CREATE) != 0) { |
| 11449 | bringDownServiceLocked(s, false); |
| 11450 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11451 | } |
| 11452 | } |
| 11453 | |
| 11454 | public boolean unbindService(IServiceConnection connection) { |
| 11455 | synchronized (this) { |
| 11456 | IBinder binder = connection.asBinder(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11457 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11458 | ArrayList<ConnectionRecord> clist = mServiceConnections.get(binder); |
| 11459 | if (clist == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11460 | Slog.w(TAG, "Unbind failed: could not find connection for " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11461 | + connection.asBinder()); |
| 11462 | return false; |
| 11463 | } |
| 11464 | |
| 11465 | final long origId = Binder.clearCallingIdentity(); |
| 11466 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11467 | while (clist.size() > 0) { |
| 11468 | ConnectionRecord r = clist.get(0); |
| 11469 | removeConnectionLocked(r, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11470 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11471 | if (r.binding.service.app != null) { |
| 11472 | // This could have made the service less important. |
| 11473 | updateOomAdjLocked(r.binding.service.app); |
| 11474 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11475 | } |
| 11476 | |
| 11477 | Binder.restoreCallingIdentity(origId); |
| 11478 | } |
| 11479 | |
| 11480 | return true; |
| 11481 | } |
| 11482 | |
| 11483 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 11484 | // Refuse possible leaked file descriptors |
| 11485 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11486 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11487 | } |
| 11488 | |
| 11489 | synchronized(this) { |
| 11490 | if (!(token instanceof ServiceRecord)) { |
| 11491 | throw new IllegalArgumentException("Invalid service token"); |
| 11492 | } |
| 11493 | ServiceRecord r = (ServiceRecord)token; |
| 11494 | |
| 11495 | final long origId = Binder.clearCallingIdentity(); |
| 11496 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11497 | if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11498 | + " " + intent + ": " + service); |
| 11499 | if (r != null) { |
| 11500 | Intent.FilterComparison filter |
| 11501 | = new Intent.FilterComparison(intent); |
| 11502 | IntentBindRecord b = r.bindings.get(filter); |
| 11503 | if (b != null && !b.received) { |
| 11504 | b.binder = service; |
| 11505 | b.requested = true; |
| 11506 | b.received = true; |
| 11507 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11508 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11509 | = r.connections.values().iterator(); |
| 11510 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11511 | ArrayList<ConnectionRecord> clist = it.next(); |
| 11512 | for (int i=0; i<clist.size(); i++) { |
| 11513 | ConnectionRecord c = clist.get(i); |
| 11514 | if (!filter.equals(c.binding.intent.intent)) { |
| 11515 | if (DEBUG_SERVICE) Slog.v( |
| 11516 | TAG, "Not publishing to: " + c); |
| 11517 | if (DEBUG_SERVICE) Slog.v( |
| 11518 | TAG, "Bound intent: " + c.binding.intent.intent); |
| 11519 | if (DEBUG_SERVICE) Slog.v( |
| 11520 | TAG, "Published intent: " + intent); |
| 11521 | continue; |
| 11522 | } |
| 11523 | if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c); |
| 11524 | try { |
| 11525 | c.conn.connected(r.name, service); |
| 11526 | } catch (Exception e) { |
| 11527 | Slog.w(TAG, "Failure sending service " + r.name + |
| 11528 | " to connection " + c.conn.asBinder() + |
| 11529 | " (in " + c.binding.client.processName + ")", e); |
| 11530 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11531 | } |
| 11532 | } |
| 11533 | } |
| 11534 | } |
| 11535 | |
| 11536 | serviceDoneExecutingLocked(r, mStoppingServices.contains(r)); |
| 11537 | |
| 11538 | Binder.restoreCallingIdentity(origId); |
| 11539 | } |
| 11540 | } |
| 11541 | } |
| 11542 | |
| 11543 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 11544 | // Refuse possible leaked file descriptors |
| 11545 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11546 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11547 | } |
| 11548 | |
| 11549 | synchronized(this) { |
| 11550 | if (!(token instanceof ServiceRecord)) { |
| 11551 | throw new IllegalArgumentException("Invalid service token"); |
| 11552 | } |
| 11553 | ServiceRecord r = (ServiceRecord)token; |
| 11554 | |
| 11555 | final long origId = Binder.clearCallingIdentity(); |
| 11556 | |
| 11557 | if (r != null) { |
| 11558 | Intent.FilterComparison filter |
| 11559 | = new Intent.FilterComparison(intent); |
| 11560 | IntentBindRecord b = r.bindings.get(filter); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11561 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11562 | + " at " + b + ": apps=" |
| 11563 | + (b != null ? b.apps.size() : 0)); |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11564 | |
| 11565 | boolean inStopping = mStoppingServices.contains(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11566 | if (b != null) { |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11567 | if (b.apps.size() > 0 && !inStopping) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11568 | // Applications have already bound since the last |
| 11569 | // unbind, so just rebind right here. |
| 11570 | requestServiceBindingLocked(r, b, true); |
| 11571 | } else { |
| 11572 | // Note to tell the service the next time there is |
| 11573 | // a new client. |
| 11574 | b.doRebind = true; |
| 11575 | } |
| 11576 | } |
| 11577 | |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11578 | serviceDoneExecutingLocked(r, inStopping); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11579 | |
| 11580 | Binder.restoreCallingIdentity(origId); |
| 11581 | } |
| 11582 | } |
| 11583 | } |
| 11584 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11585 | public void serviceDoneExecuting(IBinder token, int type, int startId, int res) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11586 | synchronized(this) { |
| 11587 | if (!(token instanceof ServiceRecord)) { |
| 11588 | throw new IllegalArgumentException("Invalid service token"); |
| 11589 | } |
| 11590 | ServiceRecord r = (ServiceRecord)token; |
| 11591 | boolean inStopping = mStoppingServices.contains(token); |
| 11592 | if (r != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11593 | if (r != token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11594 | Slog.w(TAG, "Done executing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11595 | + " with incorrect token: given " + token |
| 11596 | + ", expected " + r); |
| 11597 | return; |
| 11598 | } |
| 11599 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11600 | if (type == 1) { |
| 11601 | // This is a call from a service start... take care of |
| 11602 | // book-keeping. |
| 11603 | r.callStart = true; |
| 11604 | switch (res) { |
| 11605 | case Service.START_STICKY_COMPATIBILITY: |
| 11606 | case Service.START_STICKY: { |
| 11607 | // We are done with the associated start arguments. |
| 11608 | r.findDeliveredStart(startId, true); |
| 11609 | // Don't stop if killed. |
| 11610 | r.stopIfKilled = false; |
| 11611 | break; |
| 11612 | } |
| 11613 | case Service.START_NOT_STICKY: { |
| 11614 | // We are done with the associated start arguments. |
| 11615 | r.findDeliveredStart(startId, true); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11616 | if (r.getLastStartId() == startId) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11617 | // There is no more work, and this service |
| 11618 | // doesn't want to hang around if killed. |
| 11619 | r.stopIfKilled = true; |
| 11620 | } |
| 11621 | break; |
| 11622 | } |
| 11623 | case Service.START_REDELIVER_INTENT: { |
| 11624 | // We'll keep this item until they explicitly |
| 11625 | // call stop for it, but keep track of the fact |
| 11626 | // that it was delivered. |
| 11627 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 11628 | if (si != null) { |
| 11629 | si.deliveryCount = 0; |
| 11630 | si.doneExecutingCount++; |
| 11631 | // Don't stop if killed. |
| 11632 | r.stopIfKilled = true; |
| 11633 | } |
| 11634 | break; |
| 11635 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11636 | case Service.START_TASK_REMOVED_COMPLETE: { |
| 11637 | // Special processing for onTaskRemoved(). Don't |
| 11638 | // impact normal onStartCommand() processing. |
| 11639 | r.findDeliveredStart(startId, true); |
| 11640 | break; |
| 11641 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11642 | default: |
| 11643 | throw new IllegalArgumentException( |
| 11644 | "Unknown service start result: " + res); |
| 11645 | } |
| 11646 | if (res == Service.START_STICKY_COMPATIBILITY) { |
| 11647 | r.callStart = false; |
| 11648 | } |
| 11649 | } |
| 11650 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11651 | final long origId = Binder.clearCallingIdentity(); |
| 11652 | serviceDoneExecutingLocked(r, inStopping); |
| 11653 | Binder.restoreCallingIdentity(origId); |
| 11654 | } else { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11655 | Slog.w(TAG, "Done executing unknown service from pid " |
| 11656 | + Binder.getCallingPid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11657 | } |
| 11658 | } |
| 11659 | } |
| 11660 | |
| 11661 | public void serviceDoneExecutingLocked(ServiceRecord r, boolean inStopping) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11662 | if (DEBUG_SERVICE) Slog.v(TAG, "<<< DONE EXECUTING " + r |
| 11663 | + ": nesting=" + r.executeNesting |
| 11664 | + ", inStopping=" + inStopping + ", app=" + r.app); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11665 | else if (DEBUG_SERVICE_EXECUTING) Slog.v(TAG, "<<< DONE EXECUTING " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11666 | r.executeNesting--; |
| 11667 | if (r.executeNesting <= 0 && r.app != null) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11668 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 11669 | "Nesting at 0 of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11670 | r.app.executingServices.remove(r); |
| 11671 | if (r.app.executingServices.size() == 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11672 | if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG, |
| 11673 | "No more executingServices of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11674 | mHandler.removeMessages(SERVICE_TIMEOUT_MSG, r.app); |
| 11675 | } |
| 11676 | if (inStopping) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11677 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 11678 | "doneExecuting remove stopping " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11679 | mStoppingServices.remove(r); |
Mattias Petersson | 3996b41 | 2010-10-27 09:32:51 +0200 | [diff] [blame] | 11680 | r.bindings.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11681 | } |
| 11682 | updateOomAdjLocked(r.app); |
| 11683 | } |
| 11684 | } |
| 11685 | |
| 11686 | void serviceTimeout(ProcessRecord proc) { |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11687 | String anrMessage = null; |
| 11688 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11689 | synchronized(this) { |
| 11690 | if (proc.executingServices.size() == 0 || proc.thread == null) { |
| 11691 | return; |
| 11692 | } |
| 11693 | long maxTime = SystemClock.uptimeMillis() - SERVICE_TIMEOUT; |
| 11694 | Iterator<ServiceRecord> it = proc.executingServices.iterator(); |
| 11695 | ServiceRecord timeout = null; |
| 11696 | long nextTime = 0; |
| 11697 | while (it.hasNext()) { |
| 11698 | ServiceRecord sr = it.next(); |
| 11699 | if (sr.executingStart < maxTime) { |
| 11700 | timeout = sr; |
| 11701 | break; |
| 11702 | } |
| 11703 | if (sr.executingStart > nextTime) { |
| 11704 | nextTime = sr.executingStart; |
| 11705 | } |
| 11706 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11707 | if (timeout != null && mLruProcesses.contains(proc)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11708 | Slog.w(TAG, "Timeout executing service: " + timeout); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11709 | anrMessage = "Executing service " + timeout.shortName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11710 | } else { |
| 11711 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 11712 | msg.obj = proc; |
| 11713 | mHandler.sendMessageAtTime(msg, nextTime+SERVICE_TIMEOUT); |
| 11714 | } |
| 11715 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11716 | |
| 11717 | if (anrMessage != null) { |
| 11718 | appNotResponding(proc, null, null, anrMessage); |
| 11719 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11720 | } |
| 11721 | |
| 11722 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11723 | // BACKUP AND RESTORE |
| 11724 | // ========================================================= |
| 11725 | |
| 11726 | // Cause the target app to be launched if necessary and its backup agent |
| 11727 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 11728 | // activity manager to announce its creation. |
| 11729 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11730 | if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11731 | enforceCallingPermission("android.permission.BACKUP", "startBackupAgent"); |
| 11732 | |
| 11733 | synchronized(this) { |
| 11734 | // !!! TODO: currently no check here that we're already bound |
| 11735 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 11736 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 11737 | synchronized (stats) { |
| 11738 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 11739 | } |
| 11740 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11741 | // Backup agent is now in use, its package can't be stopped. |
| 11742 | try { |
| 11743 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 11744 | app.packageName, false); |
| 11745 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 11746 | } catch (IllegalArgumentException e) { |
| 11747 | Slog.w(TAG, "Failed trying to unstop package " |
| 11748 | + app.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11749 | } |
| 11750 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11751 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 11752 | ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) |
| 11753 | ? new ComponentName(app.packageName, app.backupAgentName) |
| 11754 | : new ComponentName("android", "FullBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11755 | // startProcessLocked() returns existing proc's record if it's already running |
| 11756 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 11757 | false, 0, "backup", hostingName, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11758 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11759 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11760 | return false; |
| 11761 | } |
| 11762 | |
| 11763 | r.app = proc; |
| 11764 | mBackupTarget = r; |
| 11765 | mBackupAppName = app.packageName; |
| 11766 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11767 | // Try not to kill the process during backup |
| 11768 | updateOomAdjLocked(proc); |
| 11769 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11770 | // If the process is already attached, schedule the creation of the backup agent now. |
| 11771 | // If it is not yet live, this will be done when it attaches to the framework. |
| 11772 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11773 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11774 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11775 | proc.thread.scheduleCreateBackupAgent(app, |
| 11776 | compatibilityInfoForPackageLocked(app), backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11777 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 11778 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11779 | } |
| 11780 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11781 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc not running, waiting for attach"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11782 | } |
| 11783 | // Invariants: at this point, the target app process exists and the application |
| 11784 | // is either already running or in the process of coming up. mBackupTarget and |
| 11785 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 11786 | // know that it's scheduled for a backup-agent operation. |
| 11787 | } |
| 11788 | |
| 11789 | return true; |
| 11790 | } |
| 11791 | |
| 11792 | // A backup agent has just come up |
| 11793 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11794 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11795 | + " = " + agent); |
| 11796 | |
| 11797 | synchronized(this) { |
| 11798 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11799 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11800 | return; |
| 11801 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11802 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11803 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11804 | long oldIdent = Binder.clearCallingIdentity(); |
| 11805 | try { |
| 11806 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 11807 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 11808 | bm.agentConnected(agentPackageName, agent); |
| 11809 | } catch (RemoteException e) { |
| 11810 | // can't happen; the backup manager service is local |
| 11811 | } catch (Exception e) { |
| 11812 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 11813 | e.printStackTrace(); |
| 11814 | } finally { |
| 11815 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11816 | } |
| 11817 | } |
| 11818 | |
| 11819 | // done with this agent |
| 11820 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11821 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11822 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11823 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11824 | return; |
| 11825 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11826 | |
| 11827 | synchronized(this) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11828 | if (mBackupAppName == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11829 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11830 | return; |
| 11831 | } |
| 11832 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11833 | if (!mBackupAppName.equals(appInfo.packageName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11834 | Slog.e(TAG, "Unbind of " + appInfo + " but is not the current backup target"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11835 | return; |
| 11836 | } |
| 11837 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11838 | ProcessRecord proc = mBackupTarget.app; |
| 11839 | mBackupTarget = null; |
| 11840 | mBackupAppName = null; |
| 11841 | |
| 11842 | // Not backing this app up any more; reset its OOM adjustment |
| 11843 | updateOomAdjLocked(proc); |
| 11844 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11845 | // If the app crashed during backup, 'thread' will be null here |
| 11846 | if (proc.thread != null) { |
| 11847 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11848 | proc.thread.scheduleDestroyBackupAgent(appInfo, |
| 11849 | compatibilityInfoForPackageLocked(appInfo)); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11850 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11851 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11852 | e.printStackTrace(); |
| 11853 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11854 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11855 | } |
| 11856 | } |
| 11857 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11858 | // BROADCASTS |
| 11859 | // ========================================================= |
| 11860 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 11861 | private final List getStickiesLocked(String action, IntentFilter filter, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11862 | List cur) { |
| 11863 | final ContentResolver resolver = mContext.getContentResolver(); |
| 11864 | final ArrayList<Intent> list = mStickyBroadcasts.get(action); |
| 11865 | if (list == null) { |
| 11866 | return cur; |
| 11867 | } |
| 11868 | int N = list.size(); |
| 11869 | for (int i=0; i<N; i++) { |
| 11870 | Intent intent = list.get(i); |
| 11871 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 11872 | if (cur == null) { |
| 11873 | cur = new ArrayList<Intent>(); |
| 11874 | } |
| 11875 | cur.add(intent); |
| 11876 | } |
| 11877 | } |
| 11878 | return cur; |
| 11879 | } |
| 11880 | |
| 11881 | private final void scheduleBroadcastsLocked() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11882 | if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts: current=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11883 | + mBroadcastsScheduled); |
| 11884 | |
| 11885 | if (mBroadcastsScheduled) { |
| 11886 | return; |
| 11887 | } |
| 11888 | mHandler.sendEmptyMessage(BROADCAST_INTENT_MSG); |
| 11889 | mBroadcastsScheduled = true; |
| 11890 | } |
| 11891 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11892 | public Intent registerReceiver(IApplicationThread caller, String callerPackage, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11893 | IIntentReceiver receiver, IntentFilter filter, String permission) { |
| 11894 | synchronized(this) { |
| 11895 | ProcessRecord callerApp = null; |
| 11896 | if (caller != null) { |
| 11897 | callerApp = getRecordForAppLocked(caller); |
| 11898 | if (callerApp == null) { |
| 11899 | throw new SecurityException( |
| 11900 | "Unable to find app for caller " + caller |
| 11901 | + " (pid=" + Binder.getCallingPid() |
| 11902 | + ") when registering receiver " + receiver); |
| 11903 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11904 | if (callerApp.info.uid != Process.SYSTEM_UID && |
| 11905 | !callerApp.pkgList.contains(callerPackage)) { |
| 11906 | throw new SecurityException("Given caller package " + callerPackage |
| 11907 | + " is not running in process " + callerApp); |
| 11908 | } |
| 11909 | } else { |
| 11910 | callerPackage = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11911 | } |
| 11912 | |
| 11913 | List allSticky = null; |
| 11914 | |
| 11915 | // Look for any matching sticky broadcasts... |
| 11916 | Iterator actions = filter.actionsIterator(); |
| 11917 | if (actions != null) { |
| 11918 | while (actions.hasNext()) { |
| 11919 | String action = (String)actions.next(); |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 11920 | allSticky = getStickiesLocked(action, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11921 | } |
| 11922 | } else { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 11923 | allSticky = getStickiesLocked(null, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11924 | } |
| 11925 | |
| 11926 | // The first sticky in the list is returned directly back to |
| 11927 | // the client. |
| 11928 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 11929 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11930 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11931 | + ": " + sticky); |
| 11932 | |
| 11933 | if (receiver == null) { |
| 11934 | return sticky; |
| 11935 | } |
| 11936 | |
| 11937 | ReceiverList rl |
| 11938 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 11939 | if (rl == null) { |
| 11940 | rl = new ReceiverList(this, callerApp, |
| 11941 | Binder.getCallingPid(), |
| 11942 | Binder.getCallingUid(), receiver); |
| 11943 | if (rl.app != null) { |
| 11944 | rl.app.receivers.add(rl); |
| 11945 | } else { |
| 11946 | try { |
| 11947 | receiver.asBinder().linkToDeath(rl, 0); |
| 11948 | } catch (RemoteException e) { |
| 11949 | return sticky; |
| 11950 | } |
| 11951 | rl.linkedToDeath = true; |
| 11952 | } |
| 11953 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
| 11954 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11955 | BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, permission); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11956 | rl.add(bf); |
| 11957 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11958 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11959 | } |
| 11960 | mReceiverResolver.addFilter(bf); |
| 11961 | |
| 11962 | // Enqueue broadcasts for all existing stickies that match |
| 11963 | // this filter. |
| 11964 | if (allSticky != null) { |
| 11965 | ArrayList receivers = new ArrayList(); |
| 11966 | receivers.add(bf); |
| 11967 | |
| 11968 | int N = allSticky.size(); |
| 11969 | for (int i=0; i<N; i++) { |
| 11970 | Intent intent = (Intent)allSticky.get(i); |
| 11971 | BroadcastRecord r = new BroadcastRecord(intent, null, |
| 11972 | null, -1, -1, null, receivers, null, 0, null, null, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11973 | false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11974 | if (mParallelBroadcasts.size() == 0) { |
| 11975 | scheduleBroadcastsLocked(); |
| 11976 | } |
| 11977 | mParallelBroadcasts.add(r); |
| 11978 | } |
| 11979 | } |
| 11980 | |
| 11981 | return sticky; |
| 11982 | } |
| 11983 | } |
| 11984 | |
| 11985 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11986 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11987 | |
| 11988 | boolean doNext = false; |
| 11989 | |
| 11990 | synchronized(this) { |
| 11991 | ReceiverList rl |
| 11992 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 11993 | if (rl != null) { |
| 11994 | if (rl.curBroadcast != null) { |
| 11995 | BroadcastRecord r = rl.curBroadcast; |
| 11996 | doNext = finishReceiverLocked( |
| 11997 | receiver.asBinder(), r.resultCode, r.resultData, |
| 11998 | r.resultExtras, r.resultAbort, true); |
| 11999 | } |
| 12000 | |
| 12001 | if (rl.app != null) { |
| 12002 | rl.app.receivers.remove(rl); |
| 12003 | } |
| 12004 | removeReceiverLocked(rl); |
| 12005 | if (rl.linkedToDeath) { |
| 12006 | rl.linkedToDeath = false; |
| 12007 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 12008 | } |
| 12009 | } |
| 12010 | } |
| 12011 | |
| 12012 | if (!doNext) { |
| 12013 | return; |
| 12014 | } |
| 12015 | |
| 12016 | final long origId = Binder.clearCallingIdentity(); |
| 12017 | processNextBroadcast(false); |
| 12018 | trimApplications(); |
| 12019 | Binder.restoreCallingIdentity(origId); |
| 12020 | } |
| 12021 | |
| 12022 | void removeReceiverLocked(ReceiverList rl) { |
| 12023 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 12024 | int N = rl.size(); |
| 12025 | for (int i=0; i<N; i++) { |
| 12026 | mReceiverResolver.removeFilter(rl.get(i)); |
| 12027 | } |
| 12028 | } |
| 12029 | |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 12030 | private final void sendPackageBroadcastLocked(int cmd, String[] packages) { |
| 12031 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 12032 | ProcessRecord r = mLruProcesses.get(i); |
| 12033 | if (r.thread != null) { |
| 12034 | try { |
| 12035 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 12036 | } catch (RemoteException ex) { |
| 12037 | } |
| 12038 | } |
| 12039 | } |
| 12040 | } |
| 12041 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12042 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 12043 | String callerPackage, Intent intent, String resolvedType, |
| 12044 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 12045 | Bundle map, String requiredPermission, |
| 12046 | boolean ordered, boolean sticky, int callingPid, int callingUid) { |
| 12047 | intent = new Intent(intent); |
| 12048 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 12049 | // By default broadcasts do not go to stopped apps. |
| 12050 | intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES); |
| 12051 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12052 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12053 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
| 12054 | + " ordered=" + ordered); |
| 12055 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12056 | Slog.w(TAG, "Broadcast " + intent + " not ordered but result callback requested!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12057 | } |
| 12058 | |
| 12059 | // Handle special intents: if this broadcast is from the package |
| 12060 | // manager about a package being removed, we need to remove all of |
| 12061 | // its activities from the history stack. |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12062 | final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12063 | intent.getAction()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12064 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 12065 | || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 12066 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12067 | || uidRemoved) { |
| 12068 | if (checkComponentPermission( |
| 12069 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12070 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12071 | == PackageManager.PERMISSION_GRANTED) { |
| 12072 | if (uidRemoved) { |
| 12073 | final Bundle intentExtras = intent.getExtras(); |
| 12074 | final int uid = intentExtras != null |
| 12075 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 12076 | if (uid >= 0) { |
| 12077 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 12078 | synchronized (bs) { |
| 12079 | bs.removeUidStatsLocked(uid); |
| 12080 | } |
| 12081 | } |
| 12082 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12083 | // If resources are unvailble just force stop all |
| 12084 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 12085 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12086 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 12087 | if (list != null && (list.length > 0)) { |
| 12088 | for (String pkg : list) { |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12089 | forceStopPackageLocked(pkg, -1, false, true, true, false); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12090 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 12091 | sendPackageBroadcastLocked( |
| 12092 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12093 | } |
| 12094 | } else { |
| 12095 | Uri data = intent.getData(); |
| 12096 | String ssp; |
| 12097 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 12098 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 12099 | forceStopPackageLocked(ssp, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12100 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true, false); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 12101 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12102 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 12103 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
| 12104 | new String[] {ssp}); |
| 12105 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12106 | } |
| 12107 | } |
| 12108 | } |
| 12109 | } else { |
| 12110 | String msg = "Permission Denial: " + intent.getAction() |
| 12111 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 12112 | + ", uid=" + callingUid + ")" |
| 12113 | + " requires " |
| 12114 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12115 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12116 | throw new SecurityException(msg); |
| 12117 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12118 | |
| 12119 | // Special case for adding a package: by default turn on compatibility |
| 12120 | // mode. |
| 12121 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 12122 | Uri data = intent.getData(); |
| 12123 | String ssp; |
| 12124 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 12125 | mCompatModePackages.handlePackageAddedLocked(ssp, |
| 12126 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12127 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12128 | } |
| 12129 | |
| 12130 | /* |
| 12131 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 12132 | * of all currently running processes. This message will get queued up before the broadcast |
| 12133 | * happens. |
| 12134 | */ |
| 12135 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 12136 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 12137 | } |
| 12138 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 12139 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 12140 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 12141 | } |
| 12142 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 12143 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 12144 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 12145 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 12146 | } |
| 12147 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12148 | /* |
| 12149 | * Prevent non-system code (defined here to be non-persistent |
| 12150 | * processes) from sending protected broadcasts. |
| 12151 | */ |
| 12152 | if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID |
| 12153 | || callingUid == Process.SHELL_UID || callingUid == 0) { |
| 12154 | // Always okay. |
| 12155 | } else if (callerApp == null || !callerApp.persistent) { |
| 12156 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12157 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12158 | intent.getAction())) { |
| 12159 | String msg = "Permission Denial: not allowed to send broadcast " |
| 12160 | + intent.getAction() + " from pid=" |
| 12161 | + callingPid + ", uid=" + callingUid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12162 | Slog.w(TAG, msg); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12163 | throw new SecurityException(msg); |
| 12164 | } |
| 12165 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12166 | Slog.w(TAG, "Remote exception", e); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12167 | return BROADCAST_SUCCESS; |
| 12168 | } |
| 12169 | } |
| 12170 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12171 | // Add to the sticky list if requested. |
| 12172 | if (sticky) { |
| 12173 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 12174 | callingPid, callingUid) |
| 12175 | != PackageManager.PERMISSION_GRANTED) { |
| 12176 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 12177 | + callingPid + ", uid=" + callingUid |
| 12178 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12179 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12180 | throw new SecurityException(msg); |
| 12181 | } |
| 12182 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12183 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12184 | + " and enforce permission " + requiredPermission); |
| 12185 | return BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
| 12186 | } |
| 12187 | if (intent.getComponent() != null) { |
| 12188 | throw new SecurityException( |
| 12189 | "Sticky broadcasts can't target a specific component"); |
| 12190 | } |
| 12191 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 12192 | if (list == null) { |
| 12193 | list = new ArrayList<Intent>(); |
| 12194 | mStickyBroadcasts.put(intent.getAction(), list); |
| 12195 | } |
| 12196 | int N = list.size(); |
| 12197 | int i; |
| 12198 | for (i=0; i<N; i++) { |
| 12199 | if (intent.filterEquals(list.get(i))) { |
| 12200 | // This sticky already exists, replace it. |
| 12201 | list.set(i, new Intent(intent)); |
| 12202 | break; |
| 12203 | } |
| 12204 | } |
| 12205 | if (i >= N) { |
| 12206 | list.add(new Intent(intent)); |
| 12207 | } |
| 12208 | } |
| 12209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12210 | // Figure out who all will receive this broadcast. |
| 12211 | List receivers = null; |
| 12212 | List<BroadcastFilter> registeredReceivers = null; |
| 12213 | try { |
| 12214 | if (intent.getComponent() != null) { |
| 12215 | // Broadcast is going to one specific receiver class... |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12216 | ActivityInfo ai = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12217 | getReceiverInfo(intent.getComponent(), STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12218 | if (ai != null) { |
| 12219 | receivers = new ArrayList(); |
| 12220 | ResolveInfo ri = new ResolveInfo(); |
| 12221 | ri.activityInfo = ai; |
| 12222 | receivers.add(ri); |
| 12223 | } |
| 12224 | } else { |
| 12225 | // Need to resolve the intent to interested receivers... |
| 12226 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 12227 | == 0) { |
| 12228 | receivers = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12229 | AppGlobals.getPackageManager().queryIntentReceivers( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12230 | intent, resolvedType, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12231 | } |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 12232 | registeredReceivers = mReceiverResolver.queryIntent(intent, resolvedType, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12233 | } |
| 12234 | } catch (RemoteException ex) { |
| 12235 | // pm is in same process, this will never happen. |
| 12236 | } |
| 12237 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12238 | final boolean replacePending = |
| 12239 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 12240 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12241 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12242 | + " replacePending=" + replacePending); |
| 12243 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12244 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 12245 | if (!ordered && NR > 0) { |
| 12246 | // If we are not serializing this broadcast, then send the |
| 12247 | // registered receivers separately so they don't wait for the |
| 12248 | // components to be launched. |
| 12249 | BroadcastRecord r = new BroadcastRecord(intent, callerApp, |
| 12250 | callerPackage, callingPid, callingUid, requiredPermission, |
| 12251 | registeredReceivers, resultTo, resultCode, resultData, map, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12252 | ordered, sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12253 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12254 | TAG, "Enqueueing parallel broadcast " + r |
| 12255 | + ": prev had " + mParallelBroadcasts.size()); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12256 | boolean replaced = false; |
| 12257 | if (replacePending) { |
| 12258 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 12259 | if (intent.filterEquals(mParallelBroadcasts.get(i).intent)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12260 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12261 | "***** DROPPING PARALLEL: " + intent); |
| 12262 | mParallelBroadcasts.set(i, r); |
| 12263 | replaced = true; |
| 12264 | break; |
| 12265 | } |
| 12266 | } |
| 12267 | } |
| 12268 | if (!replaced) { |
| 12269 | mParallelBroadcasts.add(r); |
| 12270 | scheduleBroadcastsLocked(); |
| 12271 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12272 | registeredReceivers = null; |
| 12273 | NR = 0; |
| 12274 | } |
| 12275 | |
| 12276 | // Merge into one list. |
| 12277 | int ir = 0; |
| 12278 | if (receivers != null) { |
| 12279 | // A special case for PACKAGE_ADDED: do not allow the package |
| 12280 | // being added to see this broadcast. This prevents them from |
| 12281 | // using this as a back door to get run as soon as they are |
| 12282 | // installed. Maybe in the future we want to have a special install |
| 12283 | // broadcast or such for apps, but we'd like to deliberately make |
| 12284 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12285 | String skipPackages[] = null; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12286 | if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 12287 | || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 12288 | || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12289 | Uri data = intent.getData(); |
| 12290 | if (data != null) { |
| 12291 | String pkgName = data.getSchemeSpecificPart(); |
| 12292 | if (pkgName != null) { |
| 12293 | skipPackages = new String[] { pkgName }; |
| 12294 | } |
| 12295 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12296 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12297 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 12298 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12299 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 12300 | for (String skipPackage : skipPackages) { |
| 12301 | if (skipPackage != null) { |
| 12302 | int NT = receivers.size(); |
| 12303 | for (int it=0; it<NT; it++) { |
| 12304 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 12305 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 12306 | receivers.remove(it); |
| 12307 | it--; |
| 12308 | NT--; |
| 12309 | } |
| 12310 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12311 | } |
| 12312 | } |
| 12313 | } |
| 12314 | |
| 12315 | int NT = receivers != null ? receivers.size() : 0; |
| 12316 | int it = 0; |
| 12317 | ResolveInfo curt = null; |
| 12318 | BroadcastFilter curr = null; |
| 12319 | while (it < NT && ir < NR) { |
| 12320 | if (curt == null) { |
| 12321 | curt = (ResolveInfo)receivers.get(it); |
| 12322 | } |
| 12323 | if (curr == null) { |
| 12324 | curr = registeredReceivers.get(ir); |
| 12325 | } |
| 12326 | if (curr.getPriority() >= curt.priority) { |
| 12327 | // Insert this broadcast record into the final list. |
| 12328 | receivers.add(it, curr); |
| 12329 | ir++; |
| 12330 | curr = null; |
| 12331 | it++; |
| 12332 | NT++; |
| 12333 | } else { |
| 12334 | // Skip to the next ResolveInfo in the final list. |
| 12335 | it++; |
| 12336 | curt = null; |
| 12337 | } |
| 12338 | } |
| 12339 | } |
| 12340 | while (ir < NR) { |
| 12341 | if (receivers == null) { |
| 12342 | receivers = new ArrayList(); |
| 12343 | } |
| 12344 | receivers.add(registeredReceivers.get(ir)); |
| 12345 | ir++; |
| 12346 | } |
| 12347 | |
| 12348 | if ((receivers != null && receivers.size() > 0) |
| 12349 | || resultTo != null) { |
| 12350 | BroadcastRecord r = new BroadcastRecord(intent, callerApp, |
| 12351 | callerPackage, callingPid, callingUid, requiredPermission, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12352 | receivers, resultTo, resultCode, resultData, map, ordered, |
| 12353 | sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12354 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12355 | TAG, "Enqueueing ordered broadcast " + r |
| 12356 | + ": prev had " + mOrderedBroadcasts.size()); |
| 12357 | if (DEBUG_BROADCAST) { |
| 12358 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12359 | Slog.i(TAG, "Enqueueing broadcast " + r.intent.getAction() + " seq=" + seq); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12360 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12361 | boolean replaced = false; |
| 12362 | if (replacePending) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12363 | for (int i=mOrderedBroadcasts.size()-1; i>0; i--) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12364 | if (intent.filterEquals(mOrderedBroadcasts.get(i).intent)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12365 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12366 | "***** DROPPING ORDERED: " + intent); |
| 12367 | mOrderedBroadcasts.set(i, r); |
| 12368 | replaced = true; |
| 12369 | break; |
| 12370 | } |
| 12371 | } |
| 12372 | } |
| 12373 | if (!replaced) { |
| 12374 | mOrderedBroadcasts.add(r); |
| 12375 | scheduleBroadcastsLocked(); |
| 12376 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12377 | } |
| 12378 | |
| 12379 | return BROADCAST_SUCCESS; |
| 12380 | } |
| 12381 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12382 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12383 | // Refuse possible leaked file descriptors |
| 12384 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 12385 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12386 | } |
| 12387 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12388 | int flags = intent.getFlags(); |
| 12389 | |
| 12390 | if (!mProcessesReady) { |
| 12391 | // if the caller really truly claims to know what they're doing, go |
| 12392 | // ahead and allow the broadcast without launching any receivers |
| 12393 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 12394 | intent = new Intent(intent); |
| 12395 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 12396 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 12397 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 12398 | + " before boot completion"); |
| 12399 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 12400 | } |
| 12401 | } |
| 12402 | |
| 12403 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 12404 | throw new IllegalArgumentException( |
| 12405 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 12406 | } |
| 12407 | |
| 12408 | return intent; |
| 12409 | } |
| 12410 | |
| 12411 | public final int broadcastIntent(IApplicationThread caller, |
| 12412 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 12413 | int resultCode, String resultData, Bundle map, |
| 12414 | String requiredPermission, boolean serialized, boolean sticky) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12415 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12416 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 12417 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12418 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 12419 | final int callingPid = Binder.getCallingPid(); |
| 12420 | final int callingUid = Binder.getCallingUid(); |
| 12421 | final long origId = Binder.clearCallingIdentity(); |
| 12422 | int res = broadcastIntentLocked(callerApp, |
| 12423 | callerApp != null ? callerApp.info.packageName : null, |
| 12424 | intent, resolvedType, resultTo, |
| 12425 | resultCode, resultData, map, requiredPermission, serialized, |
| 12426 | sticky, callingPid, callingUid); |
| 12427 | Binder.restoreCallingIdentity(origId); |
| 12428 | return res; |
| 12429 | } |
| 12430 | } |
| 12431 | |
| 12432 | int broadcastIntentInPackage(String packageName, int uid, |
| 12433 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 12434 | int resultCode, String resultData, Bundle map, |
| 12435 | String requiredPermission, boolean serialized, boolean sticky) { |
| 12436 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12437 | intent = verifyBroadcastLocked(intent); |
| 12438 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12439 | final long origId = Binder.clearCallingIdentity(); |
| 12440 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 12441 | resultTo, resultCode, resultData, map, requiredPermission, |
| 12442 | serialized, sticky, -1, uid); |
| 12443 | Binder.restoreCallingIdentity(origId); |
| 12444 | return res; |
| 12445 | } |
| 12446 | } |
| 12447 | |
| 12448 | public final void unbroadcastIntent(IApplicationThread caller, |
| 12449 | Intent intent) { |
| 12450 | // Refuse possible leaked file descriptors |
| 12451 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 12452 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12453 | } |
| 12454 | |
| 12455 | synchronized(this) { |
| 12456 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 12457 | != PackageManager.PERMISSION_GRANTED) { |
| 12458 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 12459 | + Binder.getCallingPid() |
| 12460 | + ", uid=" + Binder.getCallingUid() |
| 12461 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12462 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12463 | throw new SecurityException(msg); |
| 12464 | } |
| 12465 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 12466 | if (list != null) { |
| 12467 | int N = list.size(); |
| 12468 | int i; |
| 12469 | for (i=0; i<N; i++) { |
| 12470 | if (intent.filterEquals(list.get(i))) { |
| 12471 | list.remove(i); |
| 12472 | break; |
| 12473 | } |
| 12474 | } |
| 12475 | } |
| 12476 | } |
| 12477 | } |
| 12478 | |
| 12479 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 12480 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 12481 | boolean explicit) { |
| 12482 | if (mOrderedBroadcasts.size() == 0) { |
| 12483 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12484 | Slog.w(TAG, "finishReceiver called but no pending broadcasts"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12485 | } |
| 12486 | return false; |
| 12487 | } |
| 12488 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 12489 | if (r.receiver == null) { |
| 12490 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12491 | Slog.w(TAG, "finishReceiver called but none active"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12492 | } |
| 12493 | return false; |
| 12494 | } |
| 12495 | if (r.receiver != receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12496 | Slog.w(TAG, "finishReceiver called but active receiver is different"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12497 | return false; |
| 12498 | } |
| 12499 | int state = r.state; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12500 | r.state = BroadcastRecord.IDLE; |
| 12501 | if (state == BroadcastRecord.IDLE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12502 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12503 | Slog.w(TAG, "finishReceiver called but state is IDLE"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12504 | } |
| 12505 | } |
| 12506 | r.receiver = null; |
| 12507 | r.intent.setComponent(null); |
| 12508 | if (r.curApp != null) { |
| 12509 | r.curApp.curReceiver = null; |
| 12510 | } |
| 12511 | if (r.curFilter != null) { |
| 12512 | r.curFilter.receiverList.curBroadcast = null; |
| 12513 | } |
| 12514 | r.curFilter = null; |
| 12515 | r.curApp = null; |
| 12516 | r.curComponent = null; |
| 12517 | r.curReceiver = null; |
| 12518 | mPendingBroadcast = null; |
| 12519 | |
| 12520 | r.resultCode = resultCode; |
| 12521 | r.resultData = resultData; |
| 12522 | r.resultExtras = resultExtras; |
| 12523 | r.resultAbort = resultAbort; |
| 12524 | |
| 12525 | // We will process the next receiver right now if this is finishing |
| 12526 | // an app receiver (which is always asynchronous) or after we have |
| 12527 | // come back from calling a receiver. |
| 12528 | return state == BroadcastRecord.APP_RECEIVE |
| 12529 | || state == BroadcastRecord.CALL_DONE_RECEIVE; |
| 12530 | } |
| 12531 | |
| 12532 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 12533 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12534 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12535 | |
| 12536 | // Refuse possible leaked file descriptors |
| 12537 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 12538 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12539 | } |
| 12540 | |
| 12541 | boolean doNext; |
| 12542 | |
| 12543 | final long origId = Binder.clearCallingIdentity(); |
| 12544 | |
| 12545 | synchronized(this) { |
| 12546 | doNext = finishReceiverLocked( |
| 12547 | who, resultCode, resultData, resultExtras, resultAbort, true); |
| 12548 | } |
| 12549 | |
| 12550 | if (doNext) { |
| 12551 | processNextBroadcast(false); |
| 12552 | } |
| 12553 | trimApplications(); |
| 12554 | |
| 12555 | Binder.restoreCallingIdentity(origId); |
| 12556 | } |
| 12557 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12558 | private final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12559 | if (r.nextReceiver > 0) { |
| 12560 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 12561 | if (curReceiver instanceof BroadcastFilter) { |
| 12562 | BroadcastFilter bf = (BroadcastFilter) curReceiver; |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12563 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12564 | System.identityHashCode(r), |
| 12565 | r.intent.getAction(), |
| 12566 | r.nextReceiver - 1, |
| 12567 | System.identityHashCode(bf)); |
| 12568 | } else { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12569 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12570 | System.identityHashCode(r), |
| 12571 | r.intent.getAction(), |
| 12572 | r.nextReceiver - 1, |
| 12573 | ((ResolveInfo)curReceiver).toString()); |
| 12574 | } |
| 12575 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12576 | Slog.w(TAG, "Discarding broadcast before first receiver is invoked: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12577 | + r); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12578 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12579 | System.identityHashCode(r), |
| 12580 | r.intent.getAction(), |
| 12581 | r.nextReceiver, |
| 12582 | "NONE"); |
| 12583 | } |
| 12584 | } |
| 12585 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12586 | private final void setBroadcastTimeoutLocked(long timeoutTime) { |
| 12587 | if (! mPendingBroadcastTimeoutMessage) { |
| 12588 | Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG); |
| 12589 | mHandler.sendMessageAtTime(msg, timeoutTime); |
| 12590 | mPendingBroadcastTimeoutMessage = true; |
| 12591 | } |
| 12592 | } |
| 12593 | |
| 12594 | private final void cancelBroadcastTimeoutLocked() { |
| 12595 | if (mPendingBroadcastTimeoutMessage) { |
| 12596 | mHandler.removeMessages(BROADCAST_TIMEOUT_MSG); |
| 12597 | mPendingBroadcastTimeoutMessage = false; |
| 12598 | } |
| 12599 | } |
| 12600 | |
| 12601 | private final void broadcastTimeoutLocked(boolean fromMsg) { |
| 12602 | if (fromMsg) { |
| 12603 | mPendingBroadcastTimeoutMessage = false; |
| 12604 | } |
| 12605 | |
| 12606 | if (mOrderedBroadcasts.size() == 0) { |
| 12607 | return; |
| 12608 | } |
| 12609 | |
| 12610 | long now = SystemClock.uptimeMillis(); |
| 12611 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 12612 | if (fromMsg) { |
| 12613 | if (mDidDexOpt) { |
| 12614 | // Delay timeouts until dexopt finishes. |
| 12615 | mDidDexOpt = false; |
| 12616 | long timeoutTime = SystemClock.uptimeMillis() + BROADCAST_TIMEOUT; |
| 12617 | setBroadcastTimeoutLocked(timeoutTime); |
| 12618 | return; |
| 12619 | } |
| 12620 | if (! mProcessesReady) { |
| 12621 | // Only process broadcast timeouts if the system is ready. That way |
| 12622 | // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended |
| 12623 | // to do heavy lifting for system up. |
| 12624 | return; |
| 12625 | } |
| 12626 | |
| 12627 | long timeoutTime = r.receiverTime + BROADCAST_TIMEOUT; |
| 12628 | if (timeoutTime > now) { |
| 12629 | // We can observe premature timeouts because we do not cancel and reset the |
| 12630 | // broadcast timeout message after each receiver finishes. Instead, we set up |
| 12631 | // an initial timeout then kick it down the road a little further as needed |
| 12632 | // when it expires. |
| 12633 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12634 | "Premature timeout @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for " |
| 12635 | + timeoutTime); |
| 12636 | setBroadcastTimeoutLocked(timeoutTime); |
| 12637 | return; |
| 12638 | } |
| 12639 | } |
| 12640 | |
| 12641 | Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r.receiver |
| 12642 | + ", started " + (now - r.receiverTime) + "ms ago"); |
| 12643 | r.receiverTime = now; |
| 12644 | r.anrCount++; |
| 12645 | |
| 12646 | // Current receiver has passed its expiration date. |
| 12647 | if (r.nextReceiver <= 0) { |
| 12648 | Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0"); |
| 12649 | return; |
| 12650 | } |
| 12651 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12652 | ProcessRecord app = null; |
| 12653 | String anrMessage = null; |
| 12654 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12655 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 12656 | Slog.w(TAG, "Receiver during timeout: " + curReceiver); |
| 12657 | logBroadcastReceiverDiscardLocked(r); |
| 12658 | if (curReceiver instanceof BroadcastFilter) { |
| 12659 | BroadcastFilter bf = (BroadcastFilter)curReceiver; |
| 12660 | if (bf.receiverList.pid != 0 |
| 12661 | && bf.receiverList.pid != MY_PID) { |
| 12662 | synchronized (this.mPidsSelfLocked) { |
| 12663 | app = this.mPidsSelfLocked.get( |
| 12664 | bf.receiverList.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12665 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12666 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12667 | } else { |
| 12668 | app = r.curApp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12669 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12670 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12671 | if (app != null) { |
| 12672 | anrMessage = "Broadcast of " + r.intent.toString(); |
| 12673 | } |
| 12674 | |
| 12675 | if (mPendingBroadcast == r) { |
| 12676 | mPendingBroadcast = null; |
| 12677 | } |
| 12678 | |
| 12679 | // Move on to the next receiver. |
| 12680 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 12681 | r.resultExtras, r.resultAbort, true); |
| 12682 | scheduleBroadcastsLocked(); |
| 12683 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12684 | if (anrMessage != null) { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12685 | // Post the ANR to the handler since we do not want to process ANRs while |
| 12686 | // potentially holding our lock. |
| 12687 | mHandler.post(new AppNotResponding(app, anrMessage)); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12688 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12689 | } |
| 12690 | |
| 12691 | private final void processCurBroadcastLocked(BroadcastRecord r, |
| 12692 | ProcessRecord app) throws RemoteException { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12693 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12694 | "Process cur broadcast " + r + " for app " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12695 | if (app.thread == null) { |
| 12696 | throw new RemoteException(); |
| 12697 | } |
| 12698 | r.receiver = app.thread.asBinder(); |
| 12699 | r.curApp = app; |
| 12700 | app.curReceiver = r; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12701 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12702 | |
| 12703 | // Tell the application to launch this receiver. |
| 12704 | r.intent.setComponent(r.curComponent); |
| 12705 | |
| 12706 | boolean started = false; |
| 12707 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12708 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12709 | "Delivering to component " + r.curComponent |
| 12710 | + ": " + r); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 12711 | ensurePackageDexOpt(r.intent.getComponent().getPackageName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12712 | app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver, |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 12713 | compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12714 | r.resultCode, r.resultData, r.resultExtras, r.ordered); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12715 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12716 | "Process cur broadcast " + r + " DELIVERED for app " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12717 | started = true; |
| 12718 | } finally { |
| 12719 | if (!started) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12720 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12721 | "Process cur broadcast " + r + ": NOT STARTED!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12722 | r.receiver = null; |
| 12723 | r.curApp = null; |
| 12724 | app.curReceiver = null; |
| 12725 | } |
| 12726 | } |
| 12727 | |
| 12728 | } |
| 12729 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12730 | static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 12731 | Intent intent, int resultCode, String data, Bundle extras, |
| 12732 | boolean ordered, boolean sticky) throws RemoteException { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12733 | // Send the intent to the receiver asynchronously using one-way binder calls. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12734 | if (app != null && app.thread != null) { |
| 12735 | // If we have an app thread, do the call through that so it is |
| 12736 | // correctly ordered with other one-way calls. |
| 12737 | app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 12738 | data, extras, ordered, sticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12739 | } else { |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 12740 | receiver.performReceive(intent, resultCode, data, extras, ordered, sticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12741 | } |
| 12742 | } |
| 12743 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12744 | private final void deliverToRegisteredReceiverLocked(BroadcastRecord r, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12745 | BroadcastFilter filter, boolean ordered) { |
| 12746 | boolean skip = false; |
| 12747 | if (filter.requiredPermission != null) { |
| 12748 | int perm = checkComponentPermission(filter.requiredPermission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12749 | r.callingPid, r.callingUid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12750 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12751 | Slog.w(TAG, "Permission Denial: broadcasting " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12752 | + r.intent.toString() |
| 12753 | + " from " + r.callerPackage + " (pid=" |
| 12754 | + r.callingPid + ", uid=" + r.callingUid + ")" |
| 12755 | + " requires " + filter.requiredPermission |
| 12756 | + " due to registered receiver " + filter); |
| 12757 | skip = true; |
| 12758 | } |
| 12759 | } |
| 12760 | if (r.requiredPermission != null) { |
| 12761 | int perm = checkComponentPermission(r.requiredPermission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12762 | filter.receiverList.pid, filter.receiverList.uid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12763 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12764 | Slog.w(TAG, "Permission Denial: receiving " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12765 | + r.intent.toString() |
| 12766 | + " to " + filter.receiverList.app |
| 12767 | + " (pid=" + filter.receiverList.pid |
| 12768 | + ", uid=" + filter.receiverList.uid + ")" |
| 12769 | + " requires " + r.requiredPermission |
| 12770 | + " due to sender " + r.callerPackage |
| 12771 | + " (uid " + r.callingUid + ")"); |
| 12772 | skip = true; |
| 12773 | } |
| 12774 | } |
| 12775 | |
| 12776 | if (!skip) { |
| 12777 | // If this is not being sent as an ordered broadcast, then we |
| 12778 | // don't want to touch the fields that keep track of the current |
| 12779 | // state of ordered broadcasts. |
| 12780 | if (ordered) { |
| 12781 | r.receiver = filter.receiverList.receiver.asBinder(); |
| 12782 | r.curFilter = filter; |
| 12783 | filter.receiverList.curBroadcast = r; |
| 12784 | r.state = BroadcastRecord.CALL_IN_RECEIVE; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12785 | if (filter.receiverList.app != null) { |
| 12786 | // Bump hosting application to no longer be in background |
| 12787 | // scheduling class. Note that we can't do that if there |
| 12788 | // isn't an app... but we can only be in that case for |
| 12789 | // things that directly call the IActivityManager API, which |
| 12790 | // are already core system stuff so don't matter for this. |
| 12791 | r.curApp = filter.receiverList.app; |
| 12792 | filter.receiverList.app.curReceiver = r; |
| 12793 | updateOomAdjLocked(); |
| 12794 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12795 | } |
| 12796 | try { |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12797 | if (DEBUG_BROADCAST_LIGHT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12798 | int seq = r.intent.getIntExtra("seq", -1); |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 12799 | Slog.i(TAG, "Delivering to " + filter |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12800 | + " (seq=" + seq + "): " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12801 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12802 | performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12803 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12804 | r.resultData, r.resultExtras, r.ordered, r.initialSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12805 | if (ordered) { |
| 12806 | r.state = BroadcastRecord.CALL_DONE_RECEIVE; |
| 12807 | } |
| 12808 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12809 | Slog.w(TAG, "Failure sending broadcast " + r.intent, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12810 | if (ordered) { |
| 12811 | r.receiver = null; |
| 12812 | r.curFilter = null; |
| 12813 | filter.receiverList.curBroadcast = null; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12814 | if (filter.receiverList.app != null) { |
| 12815 | filter.receiverList.app.curReceiver = null; |
| 12816 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12817 | } |
| 12818 | } |
| 12819 | } |
| 12820 | } |
| 12821 | |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12822 | private final void addBroadcastToHistoryLocked(BroadcastRecord r) { |
| 12823 | if (r.callingUid < 0) { |
| 12824 | // This was from a registerReceiver() call; ignore it. |
| 12825 | return; |
| 12826 | } |
| 12827 | System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1, |
| 12828 | MAX_BROADCAST_HISTORY-1); |
| 12829 | r.finishTime = SystemClock.uptimeMillis(); |
| 12830 | mBroadcastHistory[0] = r; |
| 12831 | } |
| 12832 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12833 | private final void processNextBroadcast(boolean fromMsg) { |
| 12834 | synchronized(this) { |
| 12835 | BroadcastRecord r; |
| 12836 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12837 | if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12838 | + mParallelBroadcasts.size() + " broadcasts, " |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 12839 | + mOrderedBroadcasts.size() + " ordered broadcasts"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12840 | |
| 12841 | updateCpuStats(); |
| 12842 | |
| 12843 | if (fromMsg) { |
| 12844 | mBroadcastsScheduled = false; |
| 12845 | } |
| 12846 | |
| 12847 | // First, deliver any non-serialized broadcasts right away. |
| 12848 | while (mParallelBroadcasts.size() > 0) { |
| 12849 | r = mParallelBroadcasts.remove(0); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12850 | r.dispatchTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 12851 | r.dispatchClockTime = System.currentTimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12852 | final int N = r.receivers.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12853 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12854 | + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12855 | for (int i=0; i<N; i++) { |
| 12856 | Object target = r.receivers.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12857 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 12858 | "Delivering non-ordered to registered " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12859 | + target + ": " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12860 | deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12861 | } |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12862 | addBroadcastToHistoryLocked(r); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12863 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12864 | + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12865 | } |
| 12866 | |
| 12867 | // Now take care of the next serialized one... |
| 12868 | |
| 12869 | // If we are waiting for a process to come up to handle the next |
| 12870 | // broadcast, then do nothing at this point. Just in case, we |
| 12871 | // check that the process we're waiting for still exists. |
| 12872 | if (mPendingBroadcast != null) { |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 12873 | if (DEBUG_BROADCAST_LIGHT) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12874 | Slog.v(TAG, "processNextBroadcast: waiting for " |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 12875 | + mPendingBroadcast.curApp); |
| 12876 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12877 | |
| 12878 | boolean isDead; |
| 12879 | synchronized (mPidsSelfLocked) { |
| 12880 | isDead = (mPidsSelfLocked.get(mPendingBroadcast.curApp.pid) == null); |
| 12881 | } |
| 12882 | if (!isDead) { |
| 12883 | // It's still alive, so keep waiting |
| 12884 | return; |
| 12885 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12886 | Slog.w(TAG, "pending app " + mPendingBroadcast.curApp |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12887 | + " died before responding to broadcast"); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12888 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 12889 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12890 | mPendingBroadcast = null; |
| 12891 | } |
| 12892 | } |
| 12893 | |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12894 | boolean looped = false; |
| 12895 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12896 | do { |
| 12897 | if (mOrderedBroadcasts.size() == 0) { |
| 12898 | // No more broadcasts pending, so all done! |
| 12899 | scheduleAppGcsLocked(); |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12900 | if (looped) { |
| 12901 | // If we had finished the last ordered broadcast, then |
| 12902 | // make sure all processes have correct oom and sched |
| 12903 | // adjustments. |
| 12904 | updateOomAdjLocked(); |
| 12905 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12906 | return; |
| 12907 | } |
| 12908 | r = mOrderedBroadcasts.get(0); |
| 12909 | boolean forceReceive = false; |
| 12910 | |
| 12911 | // Ensure that even if something goes awry with the timeout |
| 12912 | // detection, we catch "hung" broadcasts here, discard them, |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 12913 | // and continue to make progress. |
| 12914 | // |
| 12915 | // This is only done if the system is ready so that PRE_BOOT_COMPLETED |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12916 | // receivers don't get executed with timeouts. They're intended for |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 12917 | // one time heavy lifting after system upgrades and can take |
| 12918 | // significant amounts of time. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12919 | int numReceivers = (r.receivers != null) ? r.receivers.size() : 0; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12920 | if (mProcessesReady && r.dispatchTime > 0) { |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 12921 | long now = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12922 | if ((numReceivers > 0) && |
| 12923 | (now > r.dispatchTime + (2*BROADCAST_TIMEOUT*numReceivers))) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12924 | Slog.w(TAG, "Hung broadcast discarded after timeout failure:" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12925 | + " now=" + now |
| 12926 | + " dispatchTime=" + r.dispatchTime |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12927 | + " startTime=" + r.receiverTime |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12928 | + " intent=" + r.intent |
| 12929 | + " numReceivers=" + numReceivers |
| 12930 | + " nextReceiver=" + r.nextReceiver |
| 12931 | + " state=" + r.state); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12932 | broadcastTimeoutLocked(false); // forcibly finish this broadcast |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12933 | forceReceive = true; |
| 12934 | r.state = BroadcastRecord.IDLE; |
| 12935 | } |
| 12936 | } |
| 12937 | |
| 12938 | if (r.state != BroadcastRecord.IDLE) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12939 | if (DEBUG_BROADCAST) Slog.d(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12940 | "processNextBroadcast() called when not idle (state=" |
| 12941 | + r.state + ")"); |
| 12942 | return; |
| 12943 | } |
| 12944 | |
| 12945 | if (r.receivers == null || r.nextReceiver >= numReceivers |
| 12946 | || r.resultAbort || forceReceive) { |
| 12947 | // No more receivers for this broadcast! Send the final |
| 12948 | // result if requested... |
| 12949 | if (r.resultTo != null) { |
| 12950 | try { |
| 12951 | if (DEBUG_BROADCAST) { |
| 12952 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12953 | Slog.i(TAG, "Finishing broadcast " + r.intent.getAction() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12954 | + " seq=" + seq + " app=" + r.callerApp); |
| 12955 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12956 | performReceiveLocked(r.callerApp, r.resultTo, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12957 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 12958 | r.resultData, r.resultExtras, false, false); |
Johannes Carlsson | b5a8654 | 2010-10-27 10:08:10 +0200 | [diff] [blame] | 12959 | // Set this to null so that the reference |
| 12960 | // (local and remote) isnt kept in the mBroadcastHistory. |
| 12961 | r.resultTo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12962 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12963 | Slog.w(TAG, "Failure sending broadcast result of " + r.intent, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12964 | } |
| 12965 | } |
| 12966 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12967 | if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG"); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12968 | cancelBroadcastTimeoutLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12969 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12970 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12971 | + r); |
| 12972 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12973 | // ... and on to the next... |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12974 | addBroadcastToHistoryLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12975 | mOrderedBroadcasts.remove(0); |
| 12976 | r = null; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12977 | looped = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12978 | continue; |
| 12979 | } |
| 12980 | } while (r == null); |
| 12981 | |
| 12982 | // Get the next receiver... |
| 12983 | int recIdx = r.nextReceiver++; |
| 12984 | |
| 12985 | // Keep track of when this receiver started, and make sure there |
| 12986 | // is a timeout message pending to kill it if need be. |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12987 | r.receiverTime = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12988 | if (recIdx == 0) { |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12989 | r.dispatchTime = r.receiverTime; |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 12990 | r.dispatchClockTime = System.currentTimeMillis(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12991 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12992 | + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12993 | } |
| 12994 | if (! mPendingBroadcastTimeoutMessage) { |
| 12995 | long timeoutTime = r.receiverTime + BROADCAST_TIMEOUT; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12996 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12997 | "Submitting BROADCAST_TIMEOUT_MSG for " + r + " at " + timeoutTime); |
| 12998 | setBroadcastTimeoutLocked(timeoutTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12999 | } |
| 13000 | |
| 13001 | Object nextReceiver = r.receivers.get(recIdx); |
| 13002 | if (nextReceiver instanceof BroadcastFilter) { |
| 13003 | // Simple case: this is a registered receiver who gets |
| 13004 | // a direct call. |
| 13005 | BroadcastFilter filter = (BroadcastFilter)nextReceiver; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13006 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 13007 | "Delivering ordered to registered " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13008 | + filter + ": " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 13009 | deliverToRegisteredReceiverLocked(r, filter, r.ordered); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13010 | if (r.receiver == null || !r.ordered) { |
| 13011 | // The receiver has already finished, so schedule to |
| 13012 | // process the next one. |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 13013 | if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing: ordered=" |
| 13014 | + r.ordered + " receiver=" + r.receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13015 | r.state = BroadcastRecord.IDLE; |
| 13016 | scheduleBroadcastsLocked(); |
| 13017 | } |
| 13018 | return; |
| 13019 | } |
| 13020 | |
| 13021 | // Hard case: need to instantiate the receiver, possibly |
| 13022 | // starting its application process to host it. |
| 13023 | |
| 13024 | ResolveInfo info = |
| 13025 | (ResolveInfo)nextReceiver; |
| 13026 | |
| 13027 | boolean skip = false; |
| 13028 | int perm = checkComponentPermission(info.activityInfo.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 13029 | r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid, |
| 13030 | info.activityInfo.exported); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13031 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 13032 | if (!info.activityInfo.exported) { |
| 13033 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 13034 | + r.intent.toString() |
| 13035 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 13036 | + ", uid=" + r.callingUid + ")" |
| 13037 | + " is not exported from uid " + info.activityInfo.applicationInfo.uid |
| 13038 | + " due to receiver " + info.activityInfo.packageName |
| 13039 | + "/" + info.activityInfo.name); |
| 13040 | } else { |
| 13041 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 13042 | + r.intent.toString() |
| 13043 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 13044 | + ", uid=" + r.callingUid + ")" |
| 13045 | + " requires " + info.activityInfo.permission |
| 13046 | + " due to receiver " + info.activityInfo.packageName |
| 13047 | + "/" + info.activityInfo.name); |
| 13048 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13049 | skip = true; |
| 13050 | } |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 13051 | if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID && |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13052 | r.requiredPermission != null) { |
| 13053 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13054 | perm = AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13055 | checkPermission(r.requiredPermission, |
| 13056 | info.activityInfo.applicationInfo.packageName); |
| 13057 | } catch (RemoteException e) { |
| 13058 | perm = PackageManager.PERMISSION_DENIED; |
| 13059 | } |
| 13060 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13061 | Slog.w(TAG, "Permission Denial: receiving " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13062 | + r.intent + " to " |
| 13063 | + info.activityInfo.applicationInfo.packageName |
| 13064 | + " requires " + r.requiredPermission |
| 13065 | + " due to sender " + r.callerPackage |
| 13066 | + " (uid " + r.callingUid + ")"); |
| 13067 | skip = true; |
| 13068 | } |
| 13069 | } |
| 13070 | if (r.curApp != null && r.curApp.crashing) { |
| 13071 | // If the target process is crashing, just skip it. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 13072 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 13073 | "Skipping deliver ordered " + r + " to " + r.curApp |
| 13074 | + ": process crashing"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13075 | skip = true; |
| 13076 | } |
| 13077 | |
| 13078 | if (skip) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 13079 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 13080 | "Skipping delivery of ordered " + r + " for whatever reason"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13081 | r.receiver = null; |
| 13082 | r.curFilter = null; |
| 13083 | r.state = BroadcastRecord.IDLE; |
| 13084 | scheduleBroadcastsLocked(); |
| 13085 | return; |
| 13086 | } |
| 13087 | |
| 13088 | r.state = BroadcastRecord.APP_RECEIVE; |
| 13089 | String targetProcess = info.activityInfo.processName; |
| 13090 | r.curComponent = new ComponentName( |
| 13091 | info.activityInfo.applicationInfo.packageName, |
| 13092 | info.activityInfo.name); |
| 13093 | r.curReceiver = info.activityInfo; |
| 13094 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 13095 | // Broadcast is being executed, its package can't be stopped. |
| 13096 | try { |
| 13097 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 13098 | r.curComponent.getPackageName(), false); |
| 13099 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 13100 | } catch (IllegalArgumentException e) { |
| 13101 | Slog.w(TAG, "Failed trying to unstop package " |
| 13102 | + r.curComponent.getPackageName() + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 13103 | } |
| 13104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13105 | // Is this receiver's application already running? |
| 13106 | ProcessRecord app = getProcessRecordLocked(targetProcess, |
| 13107 | info.activityInfo.applicationInfo.uid); |
| 13108 | if (app != null && app.thread != null) { |
| 13109 | try { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 13110 | app.addPackage(info.activityInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13111 | processCurBroadcastLocked(r, app); |
| 13112 | return; |
| 13113 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13114 | Slog.w(TAG, "Exception when sending broadcast to " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13115 | + r.curComponent, e); |
| 13116 | } |
| 13117 | |
| 13118 | // If a dead object exception was thrown -- fall through to |
| 13119 | // restart the application. |
| 13120 | } |
| 13121 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 13122 | // Not running -- get it started, to be executed when the app comes up. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 13123 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 13124 | "Need to start app " + targetProcess + " for broadcast " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13125 | if ((r.curApp=startProcessLocked(targetProcess, |
| 13126 | info.activityInfo.applicationInfo, true, |
| 13127 | r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 13128 | "broadcast", r.curComponent, |
| 13129 | (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0)) |
| 13130 | == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13131 | // Ah, this recipient is unavailable. Finish it if necessary, |
| 13132 | // and mark the broadcast record as ready for the next. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13133 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13134 | + info.activityInfo.applicationInfo.packageName + "/" |
| 13135 | + info.activityInfo.applicationInfo.uid + " for broadcast " |
| 13136 | + r.intent + ": process is bad"); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 13137 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13138 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 13139 | r.resultExtras, r.resultAbort, true); |
| 13140 | scheduleBroadcastsLocked(); |
| 13141 | r.state = BroadcastRecord.IDLE; |
| 13142 | return; |
| 13143 | } |
| 13144 | |
| 13145 | mPendingBroadcast = r; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 13146 | mPendingBroadcastRecvIndex = recIdx; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13147 | } |
| 13148 | } |
| 13149 | |
| 13150 | // ========================================================= |
| 13151 | // INSTRUMENTATION |
| 13152 | // ========================================================= |
| 13153 | |
| 13154 | public boolean startInstrumentation(ComponentName className, |
| 13155 | String profileFile, int flags, Bundle arguments, |
| 13156 | IInstrumentationWatcher watcher) { |
| 13157 | // Refuse possible leaked file descriptors |
| 13158 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 13159 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 13160 | } |
| 13161 | |
| 13162 | synchronized(this) { |
| 13163 | InstrumentationInfo ii = null; |
| 13164 | ApplicationInfo ai = null; |
| 13165 | try { |
| 13166 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 13167 | className, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13168 | ai = mContext.getPackageManager().getApplicationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 13169 | ii.targetPackage, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13170 | } catch (PackageManager.NameNotFoundException e) { |
| 13171 | } |
| 13172 | if (ii == null) { |
| 13173 | reportStartInstrumentationFailure(watcher, className, |
| 13174 | "Unable to find instrumentation info for: " + className); |
| 13175 | return false; |
| 13176 | } |
| 13177 | if (ai == null) { |
| 13178 | reportStartInstrumentationFailure(watcher, className, |
| 13179 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 13180 | return false; |
| 13181 | } |
| 13182 | |
| 13183 | int match = mContext.getPackageManager().checkSignatures( |
| 13184 | ii.targetPackage, ii.packageName); |
| 13185 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 13186 | String msg = "Permission Denial: starting instrumentation " |
| 13187 | + className + " from pid=" |
| 13188 | + Binder.getCallingPid() |
| 13189 | + ", uid=" + Binder.getCallingPid() |
| 13190 | + " not allowed because package " + ii.packageName |
| 13191 | + " does not have a signature matching the target " |
| 13192 | + ii.targetPackage; |
| 13193 | reportStartInstrumentationFailure(watcher, className, msg); |
| 13194 | throw new SecurityException(msg); |
| 13195 | } |
| 13196 | |
| 13197 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 13198 | // Instrumentation can kill and relaunch even persistent processes |
| 13199 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13200 | ProcessRecord app = addAppLocked(ai); |
| 13201 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 13202 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13203 | app.instrumentationProfileFile = profileFile; |
| 13204 | app.instrumentationArguments = arguments; |
| 13205 | app.instrumentationWatcher = watcher; |
| 13206 | app.instrumentationResultClass = className; |
| 13207 | Binder.restoreCallingIdentity(origId); |
| 13208 | } |
| 13209 | |
| 13210 | return true; |
| 13211 | } |
| 13212 | |
| 13213 | /** |
| 13214 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 13215 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 13216 | * the "am" command to report errors with more information. |
| 13217 | * |
| 13218 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 13219 | * @param cn The component name of the instrumentation. |
| 13220 | * @param report The error report. |
| 13221 | */ |
| 13222 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 13223 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13224 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13225 | try { |
| 13226 | if (watcher != null) { |
| 13227 | Bundle results = new Bundle(); |
| 13228 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 13229 | results.putString("Error", report); |
| 13230 | watcher.instrumentationStatus(cn, -1, results); |
| 13231 | } |
| 13232 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13233 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13234 | } |
| 13235 | } |
| 13236 | |
| 13237 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 13238 | if (app.instrumentationWatcher != null) { |
| 13239 | try { |
| 13240 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 13241 | app.instrumentationWatcher.instrumentationFinished( |
| 13242 | app.instrumentationClass, |
| 13243 | resultCode, |
| 13244 | results); |
| 13245 | } catch (RemoteException e) { |
| 13246 | } |
| 13247 | } |
| 13248 | app.instrumentationWatcher = null; |
| 13249 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 13250 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13251 | app.instrumentationProfileFile = null; |
| 13252 | app.instrumentationArguments = null; |
| 13253 | |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 13254 | forceStopPackageLocked(app.processName, -1, false, false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13255 | } |
| 13256 | |
| 13257 | public void finishInstrumentation(IApplicationThread target, |
| 13258 | int resultCode, Bundle results) { |
| 13259 | // Refuse possible leaked file descriptors |
| 13260 | if (results != null && results.hasFileDescriptors()) { |
| 13261 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 13262 | } |
| 13263 | |
| 13264 | synchronized(this) { |
| 13265 | ProcessRecord app = getRecordForAppLocked(target); |
| 13266 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13267 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13268 | return; |
| 13269 | } |
| 13270 | final long origId = Binder.clearCallingIdentity(); |
| 13271 | finishInstrumentationLocked(app, resultCode, results); |
| 13272 | Binder.restoreCallingIdentity(origId); |
| 13273 | } |
| 13274 | } |
| 13275 | |
| 13276 | // ========================================================= |
| 13277 | // CONFIGURATION |
| 13278 | // ========================================================= |
| 13279 | |
| 13280 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 13281 | ConfigurationInfo config = new ConfigurationInfo(); |
| 13282 | synchronized (this) { |
| 13283 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 13284 | config.reqKeyboardType = mConfiguration.keyboard; |
| 13285 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 13286 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 13287 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13288 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 13289 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 13290 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 13291 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13292 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 13293 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 13294 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13295 | } |
| 13296 | return config; |
| 13297 | } |
| 13298 | |
| 13299 | public Configuration getConfiguration() { |
| 13300 | Configuration ci; |
| 13301 | synchronized(this) { |
| 13302 | ci = new Configuration(mConfiguration); |
| 13303 | } |
| 13304 | return ci; |
| 13305 | } |
| 13306 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13307 | public void updatePersistentConfiguration(Configuration values) { |
| 13308 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 13309 | "updateConfiguration()"); |
| 13310 | enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS, |
| 13311 | "updateConfiguration()"); |
| 13312 | if (values == null) { |
| 13313 | throw new NullPointerException("Configuration must not be null"); |
| 13314 | } |
| 13315 | |
| 13316 | synchronized(this) { |
| 13317 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13318 | updateConfigurationLocked(values, null, true, false); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13319 | Binder.restoreCallingIdentity(origId); |
| 13320 | } |
| 13321 | } |
| 13322 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13323 | public void updateConfiguration(Configuration values) { |
| 13324 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 13325 | "updateConfiguration()"); |
| 13326 | |
| 13327 | synchronized(this) { |
| 13328 | if (values == null && mWindowManager != null) { |
| 13329 | // sentinel: fetch the current configuration from the window manager |
| 13330 | values = mWindowManager.computeNewConfiguration(); |
| 13331 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13332 | |
| 13333 | if (mWindowManager != null) { |
| 13334 | mProcessList.applyDisplaySize(mWindowManager); |
| 13335 | } |
| 13336 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13337 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13338 | if (values != null) { |
| 13339 | Settings.System.clearConfiguration(values); |
| 13340 | } |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13341 | updateConfigurationLocked(values, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13342 | Binder.restoreCallingIdentity(origId); |
| 13343 | } |
| 13344 | } |
| 13345 | |
| 13346 | /** |
| 13347 | * Do either or both things: (1) change the current configuration, and (2) |
| 13348 | * make sure the given activity is running with the (now) current |
| 13349 | * configuration. Returns true if the activity has been left running, or |
| 13350 | * false if <var>starting</var> is being destroyed to match the new |
| 13351 | * configuration. |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13352 | * @param persistent TODO |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13353 | */ |
| 13354 | public boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13355 | ActivityRecord starting, boolean persistent, boolean initLocale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13356 | int changes = 0; |
| 13357 | |
| 13358 | boolean kept = true; |
| 13359 | |
| 13360 | if (values != null) { |
| 13361 | Configuration newConfig = new Configuration(mConfiguration); |
| 13362 | changes = newConfig.updateFrom(values); |
| 13363 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 13364 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13365 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13366 | } |
| 13367 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 13368 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13369 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13370 | if (values.locale != null && !initLocale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13371 | saveLocaleLocked(values.locale, |
| 13372 | !values.locale.equals(mConfiguration.locale), |
| 13373 | values.userSetLocale); |
| 13374 | } |
| 13375 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 13376 | mConfigurationSeq++; |
| 13377 | if (mConfigurationSeq <= 0) { |
| 13378 | mConfigurationSeq = 1; |
| 13379 | } |
| 13380 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13381 | mConfiguration = newConfig; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13382 | Slog.i(TAG, "Config changed: " + newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13383 | |
| 13384 | final Configuration configCopy = new Configuration(mConfiguration); |
| 13385 | |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 13386 | AttributeCache ac = AttributeCache.instance(); |
| 13387 | if (ac != null) { |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13388 | ac.updateConfiguration(configCopy); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 13389 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13390 | |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 13391 | // Make sure all resources in our process are updated |
| 13392 | // right now, so that anyone who is going to retrieve |
| 13393 | // resource values after we return will be sure to get |
| 13394 | // the new ones. This is especially important during |
| 13395 | // boot, where the first config change needs to guarantee |
| 13396 | // all resources have that config before following boot |
| 13397 | // code is executed. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13398 | mSystemThread.applyConfigurationToResources(configCopy); |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 13399 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13400 | if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 13401 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13402 | msg.obj = new Configuration(configCopy); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 13403 | mHandler.sendMessage(msg); |
| 13404 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13405 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13406 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 13407 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13408 | try { |
| 13409 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13410 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 13411 | + app.processName + " new config " + mConfiguration); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13412 | app.thread.scheduleConfigurationChanged(configCopy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13413 | } |
| 13414 | } catch (Exception e) { |
| 13415 | } |
| 13416 | } |
| 13417 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 13418 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 13419 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13420 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
| 13421 | null, false, false, MY_PID, Process.SYSTEM_UID); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 13422 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
| 13423 | broadcastIntentLocked(null, null, |
| 13424 | new Intent(Intent.ACTION_LOCALE_CHANGED), |
| 13425 | null, null, 0, null, null, |
| 13426 | null, false, false, MY_PID, Process.SYSTEM_UID); |
| 13427 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13428 | } |
| 13429 | } |
| 13430 | |
| 13431 | if (changes != 0 && starting == null) { |
| 13432 | // If the configuration changed, and the caller is not already |
| 13433 | // in the process of starting an activity, then find the top |
| 13434 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13435 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13436 | } |
| 13437 | |
| 13438 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13439 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 13440 | // And we need to make sure at this point that all other activities |
| 13441 | // are made visible with the correct configuration. |
| 13442 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13443 | } |
| 13444 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 13445 | if (values != null && mWindowManager != null) { |
| 13446 | mWindowManager.setNewConfiguration(mConfiguration); |
| 13447 | } |
| 13448 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13449 | return kept; |
| 13450 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13451 | |
| 13452 | /** |
| 13453 | * Save the locale. You must be inside a synchronized (this) block. |
| 13454 | */ |
| 13455 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 13456 | if(isDiff) { |
| 13457 | SystemProperties.set("user.language", l.getLanguage()); |
| 13458 | SystemProperties.set("user.region", l.getCountry()); |
| 13459 | } |
| 13460 | |
| 13461 | if(isPersist) { |
| 13462 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 13463 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 13464 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 13465 | } |
| 13466 | } |
| 13467 | |
| 13468 | // ========================================================= |
| 13469 | // LIFETIME MANAGEMENT |
| 13470 | // ========================================================= |
| 13471 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13472 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13473 | ProcessRecord TOP_APP, boolean recursed, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13474 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13475 | // This adjustment has already been computed. If we are calling |
| 13476 | // from the top, we may have already computed our adjustment with |
| 13477 | // an earlier hidden adjustment that isn't really for us... if |
| 13478 | // so, use the new hidden adjustment. |
| 13479 | if (!recursed && app.hidden) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13480 | app.curAdj = app.curRawAdj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13481 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13482 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13483 | } |
| 13484 | |
| 13485 | if (app.thread == null) { |
| 13486 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13487 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13488 | return (app.curAdj=ProcessList.HIDDEN_APP_MAX_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13489 | } |
| 13490 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13491 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
| 13492 | app.adjSource = null; |
| 13493 | app.adjTarget = null; |
| 13494 | app.empty = false; |
| 13495 | app.hidden = false; |
| 13496 | |
| 13497 | final int activitiesSize = app.activities.size(); |
| 13498 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13499 | if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13500 | // The max adjustment doesn't allow this app to be anything |
| 13501 | // below foreground, so it is not worth doing work for it. |
| 13502 | app.adjType = "fixed"; |
| 13503 | app.adjSeq = mAdjSeq; |
| 13504 | app.curRawAdj = app.maxAdj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13505 | app.foregroundActivities = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13506 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13507 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13508 | // System process can do UI, and when they do we want to have |
| 13509 | // them trim their memory after the user leaves the UI. To |
| 13510 | // facilitate this, here we need to determine whether or not it |
| 13511 | // is currently showing UI. |
| 13512 | app.systemNoUi = true; |
| 13513 | if (app == TOP_APP) { |
| 13514 | app.systemNoUi = false; |
| 13515 | } else if (activitiesSize > 0) { |
| 13516 | for (int j = 0; j < activitiesSize; j++) { |
| 13517 | final ActivityRecord r = app.activities.get(j); |
| 13518 | if (r.visible) { |
| 13519 | app.systemNoUi = false; |
| 13520 | break; |
| 13521 | } |
| 13522 | } |
| 13523 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13524 | return (app.curAdj=app.maxAdj); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13525 | } |
| 13526 | |
| 13527 | final boolean hadForegroundActivities = app.foregroundActivities; |
| 13528 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13529 | app.foregroundActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13530 | app.keeping = false; |
| 13531 | app.systemNoUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13532 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 13533 | // Determine the importance of the process, starting with most |
| 13534 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13535 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13536 | int schedGroup; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13537 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13538 | // The last app on the list is the foreground app. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13539 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13540 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13541 | app.adjType = "top-activity"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13542 | app.foregroundActivities = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13543 | } else if (app.instrumentationClass != null) { |
| 13544 | // Don't want to kill running instrumentation. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13545 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13546 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13547 | app.adjType = "instrumentation"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13548 | } else if (app.curReceiver != null || |
| 13549 | (mPendingBroadcast != null && mPendingBroadcast.curApp == app)) { |
| 13550 | // An app that is currently receiving a broadcast also |
| 13551 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13552 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13553 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13554 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13555 | } else if (app.executingServices.size() > 0) { |
| 13556 | // An app that is currently executing a service callback also |
| 13557 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13558 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13559 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13560 | app.adjType = "exec-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13561 | } else if (activitiesSize > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13562 | // This app is in the background with paused activities. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13563 | // We inspect activities to potentially upgrade adjustment further below. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13564 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13565 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13566 | app.hidden = true; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13567 | app.adjType = "bg-activities"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13568 | } else { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13569 | // A very not-needed process. If this is lower in the lru list, |
| 13570 | // we will push it in to the empty bucket. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13571 | adj = hiddenAdj; |
| 13572 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13573 | app.hidden = true; |
| 13574 | app.empty = true; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13575 | app.adjType = "bg-empty"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13576 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13577 | |
| 13578 | // Examine all activities if not already foreground. |
| 13579 | if (!app.foregroundActivities && activitiesSize > 0) { |
| 13580 | for (int j = 0; j < activitiesSize; j++) { |
| 13581 | final ActivityRecord r = app.activities.get(j); |
| 13582 | if (r.visible) { |
| 13583 | // App has a visible activity; only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13584 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 13585 | adj = ProcessList.VISIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13586 | app.adjType = "visible"; |
| 13587 | } |
| 13588 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13589 | app.hidden = false; |
| 13590 | app.foregroundActivities = true; |
| 13591 | break; |
| 13592 | } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED |
| 13593 | || r.state == ActivityState.STOPPING) { |
| 13594 | // Only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13595 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13596 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13597 | app.adjType = "stopping"; |
| 13598 | } |
| 13599 | app.foregroundActivities = true; |
| 13600 | } |
| 13601 | } |
| 13602 | } |
| 13603 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13604 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13605 | if (app.foregroundServices) { |
| 13606 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13607 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13608 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13609 | app.adjType = "foreground-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13610 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13611 | } else if (app.forcingToForeground != null) { |
| 13612 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13613 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13614 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13615 | app.adjType = "force-foreground"; |
| 13616 | app.adjSource = app.forcingToForeground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13617 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13618 | } |
| 13619 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13620 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13621 | if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13622 | // We don't want to kill the current heavy-weight process. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13623 | adj = ProcessList.HEAVY_WEIGHT_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13624 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13625 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13626 | app.adjType = "heavy"; |
| 13627 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13628 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13629 | if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13630 | // This process is hosting what we currently consider to be the |
| 13631 | // home app, so we don't want to let it go into the background. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13632 | adj = ProcessList.HOME_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13633 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13634 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13635 | app.adjType = "home"; |
| 13636 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13637 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 13638 | if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess |
| 13639 | && app.activities.size() > 0) { |
| 13640 | // This was the previous process that showed UI to the user. |
| 13641 | // We want to try to keep it around more aggressively, to give |
| 13642 | // a good experience around switching between two apps. |
| 13643 | adj = ProcessList.PREVIOUS_APP_ADJ; |
| 13644 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 13645 | app.hidden = false; |
| 13646 | app.adjType = "previous"; |
| 13647 | } |
| 13648 | |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13649 | if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj |
| 13650 | + " reason=" + app.adjType); |
| 13651 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 13652 | // By default, we use the computed adjustment. It may be changed if |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13653 | // there are applications dependent on our services or providers, but |
| 13654 | // this gives us a baseline and makes sure we don't get into an |
| 13655 | // infinite recursion. |
| 13656 | app.adjSeq = mAdjSeq; |
| 13657 | app.curRawAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13658 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 13659 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 13660 | // If possible we want to avoid killing apps while they're being backed up |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13661 | if (adj > ProcessList.BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13662 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13663 | adj = ProcessList.BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13664 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13665 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 13666 | } |
| 13667 | } |
| 13668 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13669 | if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13670 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13671 | final long now = SystemClock.uptimeMillis(); |
| 13672 | // This process is more important if the top activity is |
| 13673 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13674 | Iterator<ServiceRecord> jt = app.services.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13675 | while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13676 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13677 | if (s.startRequested) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13678 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13679 | // If this process has shown some UI, let it immediately |
| 13680 | // go to the LRU list because it may be pretty heavy with |
| 13681 | // UI stuff. We'll tag it with a label just to help |
| 13682 | // debug and understand what is going on. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13683 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13684 | app.adjType = "started-bg-ui-services"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13685 | } |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13686 | } else { |
| 13687 | if (now < (s.lastActivity+MAX_SERVICE_INACTIVITY)) { |
| 13688 | // This service has seen some activity within |
| 13689 | // recent memory, so we will keep its process ahead |
| 13690 | // of the background processes. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13691 | if (adj > ProcessList.SERVICE_ADJ) { |
| 13692 | adj = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13693 | app.adjType = "started-services"; |
| 13694 | app.hidden = false; |
| 13695 | } |
| 13696 | } |
| 13697 | // If we have let the service slide into the background |
| 13698 | // state, still have some text describing what it is doing |
| 13699 | // even though the service no longer has an impact. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13700 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13701 | app.adjType = "started-bg-services"; |
| 13702 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13703 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13704 | // Don't kill this process because it is doing work; it |
| 13705 | // has said it is doing work. |
| 13706 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13707 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13708 | if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13709 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13710 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13711 | = s.connections.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13712 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13713 | ArrayList<ConnectionRecord> clist = kt.next(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13714 | for (int i=0; i<clist.size() && adj > ProcessList.FOREGROUND_APP_ADJ; i++) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13715 | // XXX should compute this based on the max of |
| 13716 | // all connected clients. |
| 13717 | ConnectionRecord cr = clist.get(i); |
| 13718 | if (cr.binding.client == app) { |
| 13719 | // Binding to ourself is not interesting. |
| 13720 | continue; |
| 13721 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13722 | if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13723 | ProcessRecord client = cr.binding.client; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13724 | int clientAdj = adj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13725 | int myHiddenAdj = hiddenAdj; |
| 13726 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13727 | if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13728 | myHiddenAdj = client.hiddenAdj; |
| 13729 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13730 | myHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13731 | } |
| 13732 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13733 | clientAdj = computeOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13734 | client, myHiddenAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13735 | String adjType = null; |
| 13736 | if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) { |
| 13737 | // Not doing bind OOM management, so treat |
| 13738 | // this guy more like a started service. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13739 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13740 | // If this process has shown some UI, let it immediately |
| 13741 | // go to the LRU list because it may be pretty heavy with |
| 13742 | // UI stuff. We'll tag it with a label just to help |
| 13743 | // debug and understand what is going on. |
| 13744 | if (adj > clientAdj) { |
| 13745 | adjType = "bound-bg-ui-services"; |
| 13746 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13747 | app.hidden = false; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13748 | clientAdj = adj; |
| 13749 | } else { |
| 13750 | if (now >= (s.lastActivity+MAX_SERVICE_INACTIVITY)) { |
| 13751 | // This service has not seen activity within |
| 13752 | // recent memory, so allow it to drop to the |
| 13753 | // LRU list if there is no other reason to keep |
| 13754 | // it around. We'll also tag it with a label just |
| 13755 | // to help debug and undertand what is going on. |
| 13756 | if (adj > clientAdj) { |
| 13757 | adjType = "bound-bg-services"; |
| 13758 | } |
| 13759 | clientAdj = adj; |
| 13760 | } |
| 13761 | } |
| 13762 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13763 | if (adj > clientAdj) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13764 | // If this process has recently shown UI, and |
| 13765 | // the process that is binding to it is less |
| 13766 | // important than being visible, then we don't |
| 13767 | // care about the binding as much as we care |
| 13768 | // about letting this process get into the LRU |
| 13769 | // list to be killed and restarted if needed for |
| 13770 | // memory. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13771 | if (app.hasShownUi && app != mHomeProcess |
| 13772 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13773 | adjType = "bound-bg-ui-services"; |
| 13774 | } else { |
| 13775 | if ((cr.flags&(Context.BIND_ABOVE_CLIENT |
| 13776 | |Context.BIND_IMPORTANT)) != 0) { |
| 13777 | adj = clientAdj; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 13778 | } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0 |
| 13779 | && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ |
| 13780 | && adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13781 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13782 | } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13783 | adj = clientAdj; |
| 13784 | } else { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 13785 | app.pendingUiClean = true; |
| 13786 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 13787 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13788 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13789 | } |
| 13790 | if (!client.hidden) { |
| 13791 | app.hidden = false; |
| 13792 | } |
| 13793 | if (client.keeping) { |
| 13794 | app.keeping = true; |
| 13795 | } |
| 13796 | adjType = "service"; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13797 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13798 | } |
| 13799 | if (adjType != null) { |
| 13800 | app.adjType = adjType; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13801 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13802 | .REASON_SERVICE_IN_USE; |
| 13803 | app.adjSource = cr.binding.client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13804 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13805 | app.adjTarget = s.name; |
| 13806 | } |
| 13807 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 13808 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 13809 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13810 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13811 | } |
| 13812 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13813 | if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) { |
| 13814 | ActivityRecord a = cr.activity; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13815 | if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ && |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13816 | (a.visible || a.state == ActivityState.RESUMED |
| 13817 | || a.state == ActivityState.PAUSING)) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13818 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13819 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 13820 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13821 | } |
| 13822 | app.hidden = false; |
| 13823 | app.adjType = "service"; |
| 13824 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13825 | .REASON_SERVICE_IN_USE; |
| 13826 | app.adjSource = a; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13827 | app.adjSourceOom = adj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13828 | app.adjTarget = s.name; |
| 13829 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13830 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13831 | } |
| 13832 | } |
| 13833 | } |
| 13834 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13835 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13836 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13837 | // would like to avoid killing it unless it would prevent the current |
| 13838 | // application from running. By default we put the process in |
| 13839 | // with the rest of the background processes; as we scan through |
| 13840 | // its services we may bump it up from there. |
| 13841 | if (adj > hiddenAdj) { |
| 13842 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13843 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13844 | app.adjType = "bg-services"; |
| 13845 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13846 | } |
| 13847 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13848 | if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13849 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13850 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13851 | while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13852 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13853 | ContentProviderRecord cpr = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13854 | if (cpr.clients.size() != 0) { |
| 13855 | Iterator<ProcessRecord> kt = cpr.clients.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13856 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13857 | ProcessRecord client = kt.next(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 13858 | if (client == app) { |
| 13859 | // Being our own client is not interesting. |
| 13860 | continue; |
| 13861 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13862 | int myHiddenAdj = hiddenAdj; |
| 13863 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13864 | if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13865 | myHiddenAdj = client.hiddenAdj; |
| 13866 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13867 | myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13868 | } |
| 13869 | } |
| 13870 | int clientAdj = computeOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13871 | client, myHiddenAdj, TOP_APP, true, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13872 | if (adj > clientAdj) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13873 | if (app.hasShownUi && app != mHomeProcess |
| 13874 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13875 | app.adjType = "bg-ui-provider"; |
| 13876 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13877 | adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ |
| 13878 | ? clientAdj : ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13879 | app.adjType = "provider"; |
| 13880 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13881 | if (!client.hidden) { |
| 13882 | app.hidden = false; |
| 13883 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13884 | if (client.keeping) { |
| 13885 | app.keeping = true; |
| 13886 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 13887 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13888 | .REASON_PROVIDER_IN_USE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13889 | app.adjSource = client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13890 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 13891 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13892 | } |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13893 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 13894 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13895 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13896 | } |
| 13897 | } |
| 13898 | // If the provider has external (non-framework) process |
| 13899 | // dependencies, ensure that its adjustment is at least |
| 13900 | // FOREGROUND_APP_ADJ. |
| 13901 | if (cpr.externals != 0) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13902 | if (adj > ProcessList.FOREGROUND_APP_ADJ) { |
| 13903 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13904 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13905 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13906 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13907 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 13908 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13909 | } |
| 13910 | } |
| 13911 | } |
| 13912 | } |
| 13913 | |
| 13914 | app.curRawAdj = adj; |
| 13915 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13916 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13917 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 13918 | if (adj > app.maxAdj) { |
| 13919 | adj = app.maxAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13920 | if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13921 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13922 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13923 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13924 | if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13925 | app.keeping = true; |
| 13926 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13927 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13928 | if (app.hasAboveClient) { |
| 13929 | // If this process has bound to any services with BIND_ABOVE_CLIENT, |
| 13930 | // then we need to drop its adjustment to be lower than the service's |
| 13931 | // in order to honor the request. We want to drop it by one adjustment |
| 13932 | // level... but there is special meaning applied to various levels so |
| 13933 | // we will skip some of them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13934 | if (adj < ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13935 | // System process will not get dropped, ever |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13936 | } else if (adj < ProcessList.VISIBLE_APP_ADJ) { |
| 13937 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13938 | } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13939 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13940 | } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13941 | adj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13942 | } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13943 | adj++; |
| 13944 | } |
| 13945 | } |
| 13946 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13947 | if (adj == ProcessList.SERVICE_ADJ) { |
| 13948 | if (doingAll) { |
| 13949 | app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3); |
| 13950 | mNewNumServiceProcs++; |
| 13951 | } |
| 13952 | if (app.serviceb) { |
| 13953 | adj = ProcessList.SERVICE_B_ADJ; |
| 13954 | } |
| 13955 | } else { |
| 13956 | app.serviceb = false; |
| 13957 | } |
| 13958 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13959 | app.curAdj = adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13960 | app.curSchedGroup = schedGroup; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13961 | |
| 13962 | if (hadForegroundActivities != app.foregroundActivities) { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 13963 | mHandler.obtainMessage(DISPATCH_FOREGROUND_ACTIVITIES_CHANGED, app.pid, app.info.uid, |
| 13964 | app.foregroundActivities).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13965 | } |
| 13966 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13967 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13968 | } |
| 13969 | |
| 13970 | /** |
| 13971 | * Ask a given process to GC right now. |
| 13972 | */ |
| 13973 | final void performAppGcLocked(ProcessRecord app) { |
| 13974 | try { |
| 13975 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 13976 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13977 | if (app.reportLowMemory) { |
| 13978 | app.reportLowMemory = false; |
| 13979 | app.thread.scheduleLowMemory(); |
| 13980 | } else { |
| 13981 | app.thread.processInBackground(); |
| 13982 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13983 | } |
| 13984 | } catch (Exception e) { |
| 13985 | // whatever. |
| 13986 | } |
| 13987 | } |
| 13988 | |
| 13989 | /** |
| 13990 | * Returns true if things are idle enough to perform GCs. |
| 13991 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13992 | private final boolean canGcNowLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13993 | return mParallelBroadcasts.size() == 0 |
| 13994 | && mOrderedBroadcasts.size() == 0 |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13995 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 13996 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13997 | } |
| 13998 | |
| 13999 | /** |
| 14000 | * Perform GCs on all processes that are waiting for it, but only |
| 14001 | * if things are idle. |
| 14002 | */ |
| 14003 | final void performAppGcsLocked() { |
| 14004 | final int N = mProcessesToGc.size(); |
| 14005 | if (N <= 0) { |
| 14006 | return; |
| 14007 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 14008 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14009 | while (mProcessesToGc.size() > 0) { |
| 14010 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14011 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14012 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 14013 | <= SystemClock.uptimeMillis()) { |
| 14014 | // To avoid spamming the system, we will GC processes one |
| 14015 | // at a time, waiting a few seconds between each. |
| 14016 | performAppGcLocked(proc); |
| 14017 | scheduleAppGcsLocked(); |
| 14018 | return; |
| 14019 | } else { |
| 14020 | // It hasn't been long enough since we last GCed this |
| 14021 | // process... put it in the list to wait for its time. |
| 14022 | addProcessToGcListLocked(proc); |
| 14023 | break; |
| 14024 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14025 | } |
| 14026 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14027 | |
| 14028 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14029 | } |
| 14030 | } |
| 14031 | |
| 14032 | /** |
| 14033 | * If all looks good, perform GCs on all processes waiting for them. |
| 14034 | */ |
| 14035 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 14036 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14037 | performAppGcsLocked(); |
| 14038 | return; |
| 14039 | } |
| 14040 | // Still not idle, wait some more. |
| 14041 | scheduleAppGcsLocked(); |
| 14042 | } |
| 14043 | |
| 14044 | /** |
| 14045 | * Schedule the execution of all pending app GCs. |
| 14046 | */ |
| 14047 | final void scheduleAppGcsLocked() { |
| 14048 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14049 | |
| 14050 | if (mProcessesToGc.size() > 0) { |
| 14051 | // Schedule a GC for the time to the next process. |
| 14052 | ProcessRecord proc = mProcessesToGc.get(0); |
| 14053 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 14054 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14055 | long when = proc.lastRequestedGc + GC_MIN_INTERVAL; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14056 | long now = SystemClock.uptimeMillis(); |
| 14057 | if (when < (now+GC_TIMEOUT)) { |
| 14058 | when = now + GC_TIMEOUT; |
| 14059 | } |
| 14060 | mHandler.sendMessageAtTime(msg, when); |
| 14061 | } |
| 14062 | } |
| 14063 | |
| 14064 | /** |
| 14065 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 14066 | * list in sorted order by the last GC time. The process can't already be |
| 14067 | * on the list. |
| 14068 | */ |
| 14069 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 14070 | boolean added = false; |
| 14071 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 14072 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 14073 | proc.lastRequestedGc) { |
| 14074 | added = true; |
| 14075 | mProcessesToGc.add(i+1, proc); |
| 14076 | break; |
| 14077 | } |
| 14078 | } |
| 14079 | if (!added) { |
| 14080 | mProcessesToGc.add(0, proc); |
| 14081 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14082 | } |
| 14083 | |
| 14084 | /** |
| 14085 | * Set up to ask a process to GC itself. This will either do it |
| 14086 | * immediately, or put it on the list of processes to gc the next |
| 14087 | * time things are idle. |
| 14088 | */ |
| 14089 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 14090 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14091 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14092 | return; |
| 14093 | } |
| 14094 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14095 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14096 | scheduleAppGcsLocked(); |
| 14097 | } |
| 14098 | } |
| 14099 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14100 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 14101 | updateCpuStatsNow(); |
| 14102 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14103 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14104 | boolean doWakeKills = doKills; |
| 14105 | boolean doCpuKills = doKills; |
| 14106 | if (mLastPowerCheckRealtime == 0) { |
| 14107 | doWakeKills = false; |
| 14108 | } |
| 14109 | if (mLastPowerCheckUptime == 0) { |
| 14110 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14111 | } |
| 14112 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14113 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14114 | } |
| 14115 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14116 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 14117 | final long curUptime = SystemClock.uptimeMillis(); |
| 14118 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 14119 | mLastPowerCheckRealtime = curRealtime; |
| 14120 | mLastPowerCheckUptime = curUptime; |
| 14121 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 14122 | doWakeKills = false; |
| 14123 | } |
| 14124 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 14125 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14126 | } |
| 14127 | int i = mLruProcesses.size(); |
| 14128 | while (i > 0) { |
| 14129 | i--; |
| 14130 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14131 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14132 | long wtime; |
| 14133 | synchronized (stats) { |
| 14134 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 14135 | app.pid, curRealtime); |
| 14136 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14137 | long wtimeUsed = wtime - app.lastWakeTime; |
| 14138 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 14139 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 14140 | StringBuilder sb = new StringBuilder(128); |
| 14141 | sb.append("Wake for "); |
| 14142 | app.toShortString(sb); |
| 14143 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14144 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 14145 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14146 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 14147 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14148 | sb.append((wtimeUsed*100)/realtimeSince); |
| 14149 | sb.append("%)"); |
| 14150 | Slog.i(TAG, sb.toString()); |
| 14151 | sb.setLength(0); |
| 14152 | sb.append("CPU for "); |
| 14153 | app.toShortString(sb); |
| 14154 | sb.append(": over "); |
| 14155 | TimeUtils.formatDuration(uptimeSince, sb); |
| 14156 | sb.append(" used "); |
| 14157 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 14158 | sb.append(" ("); |
| 14159 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 14160 | sb.append("%)"); |
| 14161 | Slog.i(TAG, sb.toString()); |
| 14162 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14163 | // If a process has held a wake lock for more |
| 14164 | // than 50% of the time during this period, |
| 14165 | // that sounds pad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14166 | if (doWakeKills && realtimeSince > 0 |
| 14167 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 14168 | synchronized (stats) { |
| 14169 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 14170 | realtimeSince, wtimeUsed); |
| 14171 | } |
| 14172 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 14173 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 14174 | + " during " + realtimeSince); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14175 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14176 | app.processName, app.setAdj, "excessive wake lock"); |
| 14177 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14178 | } else if (doCpuKills && uptimeSince > 0 |
| 14179 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 14180 | synchronized (stats) { |
| 14181 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 14182 | uptimeSince, cputimeUsed); |
| 14183 | } |
| 14184 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 14185 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 14186 | + " during " + uptimeSince); |
| 14187 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14188 | app.processName, app.setAdj, "excessive cpu"); |
| 14189 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14190 | } else { |
| 14191 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14192 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14193 | } |
| 14194 | } |
| 14195 | } |
| 14196 | } |
| 14197 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14198 | private final boolean updateOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14199 | ProcessRecord app, int hiddenAdj, ProcessRecord TOP_APP, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14200 | app.hiddenAdj = hiddenAdj; |
| 14201 | |
| 14202 | if (app.thread == null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14203 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14204 | } |
| 14205 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14206 | final boolean wasKeeping = app.keeping; |
| 14207 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14208 | boolean success = true; |
| 14209 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14210 | computeOomAdjLocked(app, hiddenAdj, TOP_APP, false, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14211 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14212 | if (app.curRawAdj != app.setRawAdj) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 14213 | if (false) { |
| 14214 | // Removing for now. Forcing GCs is not so useful anymore |
| 14215 | // with Dalvik, and the new memory level hint facility is |
| 14216 | // better for what we need to do these days. |
| 14217 | if (app.curRawAdj > ProcessList.FOREGROUND_APP_ADJ |
| 14218 | && app.setRawAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
| 14219 | // If this app is transitioning from foreground to |
| 14220 | // non-foreground, have it do a gc. |
| 14221 | scheduleAppGcLocked(app); |
| 14222 | } else if (app.curRawAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 14223 | && app.setRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 14224 | // Likewise do a gc when an app is moving in to the |
| 14225 | // background (such as a service stopping). |
| 14226 | scheduleAppGcLocked(app); |
| 14227 | } |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14228 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14229 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14230 | if (wasKeeping && !app.keeping) { |
| 14231 | // This app is no longer something we want to keep. Note |
| 14232 | // its current wake lock time to later know to kill it if |
| 14233 | // it is not behaving well. |
| 14234 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 14235 | synchronized (stats) { |
| 14236 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 14237 | app.pid, SystemClock.elapsedRealtime()); |
| 14238 | } |
| 14239 | app.lastCpuTime = app.curCpuTime; |
| 14240 | } |
| 14241 | |
| 14242 | app.setRawAdj = app.curRawAdj; |
| 14243 | } |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14244 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14245 | if (app.curAdj != app.setAdj) { |
| 14246 | if (Process.setOomAdj(app.pid, app.curAdj)) { |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14247 | if (true || DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
| 14248 | TAG, "Set " + app.pid + " " + app.processName + |
| 14249 | " adj " + app.curAdj + ": " + app.adjType); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14250 | app.setAdj = app.curAdj; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14251 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14252 | success = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14253 | Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14254 | } |
| 14255 | } |
| 14256 | if (app.setSchedGroup != app.curSchedGroup) { |
| 14257 | app.setSchedGroup = app.curSchedGroup; |
| 14258 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
| 14259 | "Setting process group of " + app.processName |
| 14260 | + " to " + app.curSchedGroup); |
| 14261 | if (app.waitingToKill != null && |
| 14262 | app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 14263 | Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill); |
| 14264 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14265 | app.processName, app.setAdj, app.waitingToKill); |
| 14266 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14267 | success = false; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14268 | } else { |
| 14269 | if (true) { |
| 14270 | long oldId = Binder.clearCallingIdentity(); |
| 14271 | try { |
| 14272 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 14273 | } catch (Exception e) { |
| 14274 | Slog.w(TAG, "Failed setting process group of " + app.pid |
| 14275 | + " to " + app.curSchedGroup); |
| 14276 | e.printStackTrace(); |
| 14277 | } finally { |
| 14278 | Binder.restoreCallingIdentity(oldId); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14279 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14280 | } else { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14281 | if (app.thread != null) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 14282 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14283 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 14284 | } catch (RemoteException e) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 14285 | } |
| 14286 | } |
| 14287 | } |
| 14288 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14289 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14290 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14291 | } |
| 14292 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 14293 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14294 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14295 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14296 | resumedActivity = mMainStack.mPausingActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14297 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14298 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14299 | } |
| 14300 | } |
| 14301 | return resumedActivity; |
| 14302 | } |
| 14303 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14304 | private final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 14305 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14306 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 14307 | int curAdj = app.curAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14308 | final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 14309 | && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14310 | |
| 14311 | mAdjSeq++; |
| 14312 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14313 | boolean success = updateOomAdjLocked(app, app.hiddenAdj, TOP_APP, false); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14314 | final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 14315 | && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14316 | if (nowHidden != wasHidden) { |
| 14317 | // Changed to/from hidden state, so apps after it in the LRU |
| 14318 | // list may also be changed. |
| 14319 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14320 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14321 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14322 | } |
| 14323 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14324 | final void updateOomAdjLocked() { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 14325 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14326 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 14327 | |
| 14328 | if (false) { |
| 14329 | RuntimeException e = new RuntimeException(); |
| 14330 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 14331 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14332 | } |
| 14333 | |
| 14334 | mAdjSeq++; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14335 | mNewNumServiceProcs = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14336 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 14337 | // Let's determine how many processes we have running vs. |
| 14338 | // how many slots we have for background processes; we may want |
| 14339 | // to put multiple processes in a slot of there are enough of |
| 14340 | // them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14341 | int numSlots = ProcessList.HIDDEN_APP_MAX_ADJ - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 14342 | int factor = (mLruProcesses.size()-4)/numSlots; |
| 14343 | if (factor < 1) factor = 1; |
| 14344 | int step = 0; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 14345 | int numHidden = 0; |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 14346 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14347 | // First update the OOM adjustment for each of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14348 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14349 | int i = mLruProcesses.size(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14350 | int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14351 | while (i > 0) { |
| 14352 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14353 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 14354 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14355 | updateOomAdjLocked(app, curHiddenAdj, TOP_APP, true); |
| 14356 | if (curHiddenAdj < ProcessList.HIDDEN_APP_MAX_ADJ |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14357 | && app.curAdj == curHiddenAdj) { |
| 14358 | step++; |
| 14359 | if (step >= factor) { |
| 14360 | step = 0; |
| 14361 | curHiddenAdj++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14362 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14363 | } |
| 14364 | if (!app.killedBackground) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14365 | if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14366 | numHidden++; |
| 14367 | if (numHidden > mProcessLimit) { |
| 14368 | Slog.i(TAG, "No longer want " + app.processName |
| 14369 | + " (pid " + app.pid + "): hidden #" + numHidden); |
| 14370 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14371 | app.processName, app.setAdj, "too many background"); |
| 14372 | app.killedBackground = true; |
| 14373 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 14374 | } |
| 14375 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14376 | } |
| 14377 | } |
| 14378 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14379 | mNumServiceProcs = mNewNumServiceProcs; |
| 14380 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14381 | // Now determine the memory trimming level of background processes. |
| 14382 | // Unfortunately we need to start at the back of the list to do this |
| 14383 | // properly. We only do this if the number of background apps we |
| 14384 | // are managing to keep around is less than half the maximum we desire; |
| 14385 | // if we are keeping a good number around, we'll let them use whatever |
| 14386 | // memory they want. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14387 | if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/2)) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14388 | final int N = mLruProcesses.size(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 14389 | factor = numHidden/3; |
| 14390 | int minFactor = 2; |
| 14391 | if (mHomeProcess != null) minFactor++; |
| 14392 | if (mPreviousProcess != null) minFactor++; |
| 14393 | if (factor < minFactor) factor = minFactor; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14394 | step = 0; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14395 | int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14396 | for (i=0; i<N; i++) { |
| 14397 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 14398 | if (app.curAdj >= ProcessList.HOME_APP_ADJ |
| 14399 | && app.curAdj != ProcessList.SERVICE_B_ADJ |
| 14400 | && !app.killedBackground) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14401 | if (app.trimMemoryLevel < curLevel && app.thread != null) { |
| 14402 | try { |
| 14403 | app.thread.scheduleTrimMemory(curLevel); |
| 14404 | } catch (RemoteException e) { |
| 14405 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14406 | if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14407 | // For these apps we will also finish their activities |
| 14408 | // to help them free memory. |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 14409 | mMainStack.destroyActivitiesLocked(app, false, "trim"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14410 | } |
| 14411 | } |
| 14412 | app.trimMemoryLevel = curLevel; |
| 14413 | step++; |
| 14414 | if (step >= factor) { |
| 14415 | switch (curLevel) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14416 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 14417 | curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14418 | break; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14419 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 14420 | curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14421 | break; |
| 14422 | } |
| 14423 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14424 | } else if (app.curAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14425 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14426 | && app.thread != null) { |
| 14427 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14428 | app.thread.scheduleTrimMemory( |
| 14429 | ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14430 | } catch (RemoteException e) { |
| 14431 | } |
| 14432 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14433 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14434 | } else if ((app.curAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14435 | && app.pendingUiClean) { |
| 14436 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 14437 | && app.thread != null) { |
| 14438 | try { |
| 14439 | app.thread.scheduleTrimMemory( |
| 14440 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 14441 | } catch (RemoteException e) { |
| 14442 | } |
| 14443 | } |
| 14444 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 14445 | app.pendingUiClean = false; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14446 | } else { |
| 14447 | app.trimMemoryLevel = 0; |
| 14448 | } |
| 14449 | } |
| 14450 | } else { |
| 14451 | final int N = mLruProcesses.size(); |
| 14452 | for (i=0; i<N; i++) { |
| 14453 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14454 | if ((app.curAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14455 | && app.pendingUiClean) { |
| 14456 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 14457 | && app.thread != null) { |
| 14458 | try { |
| 14459 | app.thread.scheduleTrimMemory( |
| 14460 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 14461 | } catch (RemoteException e) { |
| 14462 | } |
| 14463 | } |
| 14464 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 14465 | app.pendingUiClean = false; |
| 14466 | } else { |
| 14467 | app.trimMemoryLevel = 0; |
| 14468 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14469 | } |
| 14470 | } |
| 14471 | |
| 14472 | if (mAlwaysFinishActivities) { |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 14473 | mMainStack.destroyActivitiesLocked(null, false, "always-finish"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14474 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14475 | } |
| 14476 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14477 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14478 | synchronized (this) { |
| 14479 | int i; |
| 14480 | |
| 14481 | // First remove any unused application processes whose package |
| 14482 | // has been removed. |
| 14483 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 14484 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 14485 | if (app.activities.size() == 0 |
| 14486 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 14487 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14488 | TAG, "Exiting empty application process " |
| 14489 | + app.processName + " (" |
| 14490 | + (app.thread != null ? app.thread.asBinder() : null) |
| 14491 | + ")\n"); |
| 14492 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 14493 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14494 | app.processName, app.setAdj, "empty"); |
| 14495 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14496 | } else { |
| 14497 | try { |
| 14498 | app.thread.scheduleExit(); |
| 14499 | } catch (Exception e) { |
| 14500 | // Ignore exceptions. |
| 14501 | } |
| 14502 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 14503 | cleanUpApplicationRecordLocked(app, false, true, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14504 | mRemovedProcesses.remove(i); |
| 14505 | |
| 14506 | if (app.persistent) { |
| 14507 | if (app.persistent) { |
| 14508 | addAppLocked(app.info); |
| 14509 | } |
| 14510 | } |
| 14511 | } |
| 14512 | } |
| 14513 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14514 | // Now update the oom adj for all processes. |
| 14515 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14516 | } |
| 14517 | } |
| 14518 | |
| 14519 | /** This method sends the specified signal to each of the persistent apps */ |
| 14520 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 14521 | if (sig != Process.SIGNAL_USR1) { |
| 14522 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 14523 | } |
| 14524 | |
| 14525 | synchronized (this) { |
| 14526 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 14527 | != PackageManager.PERMISSION_GRANTED) { |
| 14528 | throw new SecurityException("Requires permission " |
| 14529 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 14530 | } |
| 14531 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14532 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 14533 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14534 | if (r.thread != null && r.persistent) { |
| 14535 | Process.sendSignal(r.pid, sig); |
| 14536 | } |
| 14537 | } |
| 14538 | } |
| 14539 | } |
| 14540 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14541 | private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) { |
| 14542 | if (proc == null || proc == mProfileProc) { |
| 14543 | proc = mProfileProc; |
| 14544 | path = mProfileFile; |
| 14545 | profileType = mProfileType; |
| 14546 | clearProfilerLocked(); |
| 14547 | } |
| 14548 | if (proc == null) { |
| 14549 | return; |
| 14550 | } |
| 14551 | try { |
| 14552 | proc.thread.profilerControl(false, path, null, profileType); |
| 14553 | } catch (RemoteException e) { |
| 14554 | throw new IllegalStateException("Process disappeared"); |
| 14555 | } |
| 14556 | } |
| 14557 | |
| 14558 | private void clearProfilerLocked() { |
| 14559 | if (mProfileFd != null) { |
| 14560 | try { |
| 14561 | mProfileFd.close(); |
| 14562 | } catch (IOException e) { |
| 14563 | } |
| 14564 | } |
| 14565 | mProfileApp = null; |
| 14566 | mProfileProc = null; |
| 14567 | mProfileFile = null; |
| 14568 | mProfileType = 0; |
| 14569 | mAutoStopProfiler = false; |
| 14570 | } |
| 14571 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14572 | public boolean profileControl(String process, boolean start, |
Romain Guy | 7eabe55 | 2011-07-21 14:56:34 -0700 | [diff] [blame] | 14573 | String path, ParcelFileDescriptor fd, int profileType) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14574 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14575 | try { |
| 14576 | synchronized (this) { |
| 14577 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14578 | // its own permission. |
| 14579 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14580 | != PackageManager.PERMISSION_GRANTED) { |
| 14581 | throw new SecurityException("Requires permission " |
| 14582 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14583 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14584 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14585 | if (start && fd == null) { |
| 14586 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14587 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14588 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14589 | ProcessRecord proc = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14590 | if (process != null) { |
| 14591 | try { |
| 14592 | int pid = Integer.parseInt(process); |
| 14593 | synchronized (mPidsSelfLocked) { |
| 14594 | proc = mPidsSelfLocked.get(pid); |
| 14595 | } |
| 14596 | } catch (NumberFormatException e) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14597 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14598 | |
| 14599 | if (proc == null) { |
| 14600 | HashMap<String, SparseArray<ProcessRecord>> all |
| 14601 | = mProcessNames.getMap(); |
| 14602 | SparseArray<ProcessRecord> procs = all.get(process); |
| 14603 | if (procs != null && procs.size() > 0) { |
| 14604 | proc = procs.valueAt(0); |
| 14605 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14606 | } |
| 14607 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14608 | |
| 14609 | if (start && (proc == null || proc.thread == null)) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14610 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14611 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14612 | |
| 14613 | if (start) { |
| 14614 | stopProfilerLocked(null, null, 0); |
| 14615 | setProfileApp(proc.info, proc.processName, path, fd, false); |
| 14616 | mProfileProc = proc; |
| 14617 | mProfileType = profileType; |
| 14618 | try { |
| 14619 | fd = fd.dup(); |
| 14620 | } catch (IOException e) { |
| 14621 | fd = null; |
| 14622 | } |
| 14623 | proc.thread.profilerControl(start, path, fd, profileType); |
| 14624 | fd = null; |
| 14625 | mProfileFd = null; |
| 14626 | } else { |
| 14627 | stopProfilerLocked(proc, path, profileType); |
| 14628 | if (fd != null) { |
| 14629 | try { |
| 14630 | fd.close(); |
| 14631 | } catch (IOException e) { |
| 14632 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14633 | } |
| 14634 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14635 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14636 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14637 | } |
| 14638 | } catch (RemoteException e) { |
| 14639 | throw new IllegalStateException("Process disappeared"); |
| 14640 | } finally { |
| 14641 | if (fd != null) { |
| 14642 | try { |
| 14643 | fd.close(); |
| 14644 | } catch (IOException e) { |
| 14645 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14646 | } |
| 14647 | } |
| 14648 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14649 | |
| 14650 | public boolean dumpHeap(String process, boolean managed, |
| 14651 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 14652 | |
| 14653 | try { |
| 14654 | synchronized (this) { |
| 14655 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14656 | // its own permission (same as profileControl). |
| 14657 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14658 | != PackageManager.PERMISSION_GRANTED) { |
| 14659 | throw new SecurityException("Requires permission " |
| 14660 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 14661 | } |
| 14662 | |
| 14663 | if (fd == null) { |
| 14664 | throw new IllegalArgumentException("null fd"); |
| 14665 | } |
| 14666 | |
| 14667 | ProcessRecord proc = null; |
| 14668 | try { |
| 14669 | int pid = Integer.parseInt(process); |
| 14670 | synchronized (mPidsSelfLocked) { |
| 14671 | proc = mPidsSelfLocked.get(pid); |
| 14672 | } |
| 14673 | } catch (NumberFormatException e) { |
| 14674 | } |
| 14675 | |
| 14676 | if (proc == null) { |
| 14677 | HashMap<String, SparseArray<ProcessRecord>> all |
| 14678 | = mProcessNames.getMap(); |
| 14679 | SparseArray<ProcessRecord> procs = all.get(process); |
| 14680 | if (procs != null && procs.size() > 0) { |
| 14681 | proc = procs.valueAt(0); |
| 14682 | } |
| 14683 | } |
| 14684 | |
| 14685 | if (proc == null || proc.thread == null) { |
| 14686 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14687 | } |
| 14688 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 14689 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 14690 | if (!isDebuggable) { |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14691 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 14692 | throw new SecurityException("Process not debuggable: " + proc); |
| 14693 | } |
| 14694 | } |
| 14695 | |
| 14696 | proc.thread.dumpHeap(managed, path, fd); |
| 14697 | fd = null; |
| 14698 | return true; |
| 14699 | } |
| 14700 | } catch (RemoteException e) { |
| 14701 | throw new IllegalStateException("Process disappeared"); |
| 14702 | } finally { |
| 14703 | if (fd != null) { |
| 14704 | try { |
| 14705 | fd.close(); |
| 14706 | } catch (IOException e) { |
| 14707 | } |
| 14708 | } |
| 14709 | } |
| 14710 | } |
| 14711 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14712 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 14713 | public void monitor() { |
| 14714 | synchronized (this) { } |
| 14715 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 14716 | |
| 14717 | public void onCoreSettingsChange(Bundle settings) { |
| 14718 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 14719 | ProcessRecord processRecord = mLruProcesses.get(i); |
| 14720 | try { |
| 14721 | if (processRecord.thread != null) { |
| 14722 | processRecord.thread.setCoreSettings(settings); |
| 14723 | } |
| 14724 | } catch (RemoteException re) { |
| 14725 | /* ignore */ |
| 14726 | } |
| 14727 | } |
| 14728 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14729 | |
| 14730 | // Multi-user methods |
| 14731 | |
| 14732 | public boolean switchUser(int userid) { |
| 14733 | // TODO |
| 14734 | return true; |
| 14735 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14736 | } |