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 | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 21 | import com.android.server.AttributeCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import com.android.server.IntentResolver; |
| 23 | import com.android.server.ProcessMap; |
| 24 | import com.android.server.ProcessStats; |
| 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; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 48 | import android.app.IThumbnailRetriever; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.app.Instrumentation; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 50 | import android.app.Notification; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 51 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.app.PendingIntent; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 53 | import android.app.Service; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 54 | import android.app.backup.IBackupManager; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 55 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 56 | import android.content.BroadcastReceiver; |
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; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 95 | import android.os.IInterface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | import android.os.IPermissionController; |
| 97 | import android.os.Looper; |
| 98 | import android.os.Message; |
| 99 | import android.os.Parcel; |
| 100 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.os.Process; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 102 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | import android.os.RemoteException; |
| 104 | import android.os.ServiceManager; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 105 | import android.os.StrictMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | import android.os.SystemClock; |
| 107 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | import android.util.EventLog; |
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; |
| 123 | import java.io.DataInputStream; |
| 124 | import java.io.DataOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | import java.io.File; |
| 126 | import java.io.FileDescriptor; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 127 | import java.io.FileInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | import java.io.FileNotFoundException; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 129 | import java.io.FileOutputStream; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 130 | import java.io.IOException; |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 131 | import java.io.InputStreamReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | import java.io.PrintWriter; |
| 133 | import java.lang.IllegalStateException; |
| 134 | import java.lang.ref.WeakReference; |
| 135 | import java.util.ArrayList; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 136 | import java.util.Collections; |
| 137 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | import java.util.HashMap; |
| 139 | import java.util.HashSet; |
| 140 | import java.util.Iterator; |
| 141 | import java.util.List; |
| 142 | import java.util.Locale; |
| 143 | import java.util.Map; |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 144 | import java.util.Set; |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 145 | import java.util.concurrent.atomic.AtomicBoolean; |
| 146 | import java.util.concurrent.atomic.AtomicLong; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 148 | public final class ActivityManagerService extends ActivityManagerNative |
| 149 | implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | static final String TAG = "ActivityManager"; |
| 151 | static final boolean DEBUG = false; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 152 | static final boolean localLOGV = DEBUG; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | static final boolean DEBUG_SWITCH = localLOGV || false; |
| 154 | static final boolean DEBUG_TASKS = localLOGV || false; |
| 155 | static final boolean DEBUG_PAUSE = localLOGV || false; |
| 156 | static final boolean DEBUG_OOM_ADJ = localLOGV || false; |
| 157 | static final boolean DEBUG_TRANSITION = localLOGV || false; |
| 158 | static final boolean DEBUG_BROADCAST = localLOGV || false; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 159 | static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | static final boolean DEBUG_SERVICE = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 161 | static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | static final boolean DEBUG_VISBILITY = localLOGV || false; |
| 163 | static final boolean DEBUG_PROCESSES = localLOGV || false; |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 164 | static final boolean DEBUG_PROVIDER = localLOGV || false; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 165 | static final boolean DEBUG_URI_PERMISSION = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | static final boolean DEBUG_USER_LEAVING = localLOGV || false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 167 | static final boolean DEBUG_RESULTS = localLOGV || false; |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 168 | static final boolean DEBUG_BACKUP = localLOGV || true; |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 169 | static final boolean DEBUG_CONFIGURATION = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 170 | static final boolean DEBUG_POWER = localLOGV || false; |
| 171 | static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | static final boolean VALIDATE_TOKENS = false; |
| 173 | static final boolean SHOW_ACTIVITY_START_TIME = true; |
| 174 | |
| 175 | // Control over CPU and battery monitoring. |
| 176 | static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes. |
| 177 | static final boolean MONITOR_CPU_USAGE = true; |
| 178 | static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds. |
| 179 | static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample. |
| 180 | static final boolean MONITOR_THREAD_CPU_USAGE = false; |
| 181 | |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 182 | // 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] | 183 | static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 184 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | private static final String SYSTEM_SECURE = "ro.secure"; |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 186 | private static final String SYSTEM_DEBUGGABLE = "ro.debuggable"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | |
| 188 | // This is the maximum number of application processes we would like |
| 189 | // to have running. Due to the asynchronous nature of things, we can |
| 190 | // temporarily go beyond this limit. |
| 191 | static final int MAX_PROCESSES = 2; |
| 192 | |
| 193 | // Set to false to leave processes running indefinitely, relying on |
| 194 | // the kernel killing them as resources are required. |
| 195 | static final boolean ENFORCE_PROCESS_LIMIT = false; |
| 196 | |
| 197 | // This is the maximum number of activities that we would like to have |
| 198 | // running at a given time. |
| 199 | static final int MAX_ACTIVITIES = 20; |
| 200 | |
| 201 | // Maximum number of recent tasks that we can remember. |
| 202 | static final int MAX_RECENT_TASKS = 20; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 203 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 204 | // Amount of time after a call to stopAppSwitches() during which we will |
| 205 | // prevent further untrusted switches from happening. |
| 206 | static final long APP_SWITCH_DELAY_TIME = 5*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | |
| 208 | // How long we wait for a launched process to attach to the activity manager |
| 209 | // before we decide it's never going to come up for real. |
| 210 | static final int PROC_START_TIMEOUT = 10*1000; |
| 211 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | // How long to wait after going idle before forcing apps to GC. |
| 213 | static final int GC_TIMEOUT = 5*1000; |
| 214 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 215 | // The minimum amount of time between successive GC requests for a process. |
| 216 | static final int GC_MIN_INTERVAL = 60*1000; |
| 217 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 218 | // The rate at which we check for apps using excessive power -- 15 mins. |
| 219 | static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000; |
| 220 | |
| 221 | // The minimum sample duration we will allow before deciding we have |
| 222 | // enough data on wake locks to start killing things. |
| 223 | static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
| 224 | |
| 225 | // The minimum sample duration we will allow before deciding we have |
| 226 | // enough data on CPU usage to start killing things. |
| 227 | 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] | 228 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | // How long we allow a receiver to run before giving up on it. |
| 230 | static final int BROADCAST_TIMEOUT = 10*1000; |
| 231 | |
| 232 | // How long we wait for a service to finish executing. |
| 233 | static final int SERVICE_TIMEOUT = 20*1000; |
| 234 | |
| 235 | // How long a service needs to be running until restarting its process |
| 236 | // is no longer considered to be a relaunch of the service. |
| 237 | static final int SERVICE_RESTART_DURATION = 5*1000; |
| 238 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 239 | // How long a service needs to be running until it will start back at |
| 240 | // SERVICE_RESTART_DURATION after being killed. |
| 241 | static final int SERVICE_RESET_RUN_DURATION = 60*1000; |
| 242 | |
| 243 | // Multiplying factor to increase restart duration time by, for each time |
| 244 | // a service is killed before it has run for SERVICE_RESET_RUN_DURATION. |
| 245 | static final int SERVICE_RESTART_DURATION_FACTOR = 4; |
| 246 | |
| 247 | // The minimum amount of time between restarting services that we allow. |
| 248 | // That is, when multiple services are restarting, we won't allow each |
| 249 | // to restart less than this amount of time from the last one. |
| 250 | static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000; |
| 251 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | // Maximum amount of time for there to be no activity on a service before |
| 253 | // we consider it non-essential and allow its process to go on the |
| 254 | // LRU background list. |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 255 | static final int MAX_SERVICE_INACTIVITY = 30*60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | |
| 257 | // How long we wait until we timeout on key dispatching. |
| 258 | static final int KEY_DISPATCHING_TIMEOUT = 5*1000; |
| 259 | |
| 260 | // The minimum time we allow between crashes, for us to consider this |
| 261 | // application to be bad and stop and its services and reject broadcasts. |
| 262 | static final int MIN_CRASH_INTERVAL = 60*1000; |
| 263 | |
| 264 | // How long we wait until we timeout on key dispatching during instrumentation. |
| 265 | static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000; |
| 266 | |
| 267 | // OOM adjustments for processes in various states: |
| 268 | |
| 269 | // This is a process without anything currently running in it. Definitely |
| 270 | // the first to go! Value set in system/rootdir/init.rc on startup. |
| 271 | // This value is initalized in the constructor, careful when refering to |
| 272 | // this static variable externally. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 273 | static final int EMPTY_APP_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | |
| 275 | // This is a process only hosting activities that are not visible, |
| 276 | // so it can be killed without any disruption. Value set in |
| 277 | // system/rootdir/init.rc on startup. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 278 | static final int HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | static int HIDDEN_APP_MIN_ADJ; |
| 280 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 281 | // This is a process holding the home application -- we want to try |
| 282 | // avoiding killing it, even if it would normally be in the background, |
| 283 | // because the user interacts with it so much. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 284 | static final int HOME_APP_ADJ; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 285 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 286 | // This is a process currently hosting a backup operation. Killing it |
| 287 | // is not entirely fatal but is generally a bad idea. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 288 | static final int BACKUP_APP_ADJ; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 289 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | // This is a process holding a secondary server -- killing it will not |
| 291 | // have much of an impact as far as the user is concerned. Value set in |
| 292 | // system/rootdir/init.rc on startup. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 293 | static final int SECONDARY_SERVER_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 295 | // This is a process with a heavy-weight application. It is in the |
| 296 | // background, but we want to try to avoid killing it. Value set in |
| 297 | // system/rootdir/init.rc on startup. |
| 298 | static final int HEAVY_WEIGHT_APP_ADJ; |
| 299 | |
| 300 | // This is a process only hosting components that are perceptible to the |
| 301 | // user, and we really want to avoid killing them, but they are not |
| 302 | // immediately visible. An example is background music playback. Value set in |
| 303 | // system/rootdir/init.rc on startup. |
| 304 | static final int PERCEPTIBLE_APP_ADJ; |
| 305 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | // This is a process only hosting activities that are visible to the |
| 307 | // user, so we'd prefer they don't disappear. Value set in |
| 308 | // system/rootdir/init.rc on startup. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 309 | static final int VISIBLE_APP_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | |
| 311 | // This is the process running the current foreground app. We'd really |
| 312 | // rather not kill it! Value set in system/rootdir/init.rc on startup. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 313 | static final int FOREGROUND_APP_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | |
| 315 | // This is a process running a core server, such as telephony. Definitely |
| 316 | // don't want to kill it, but doing so is not completely fatal. |
| 317 | static final int CORE_SERVER_ADJ = -12; |
| 318 | |
| 319 | // The system process runs at the default adjustment. |
| 320 | static final int SYSTEM_ADJ = -16; |
| 321 | |
| 322 | // Memory pages are 4K. |
| 323 | static final int PAGE_SIZE = 4*1024; |
| 324 | |
| 325 | // Corresponding memory levels for above adjustments. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 326 | static final int EMPTY_APP_MEM; |
| 327 | static final int HIDDEN_APP_MEM; |
| 328 | static final int HOME_APP_MEM; |
| 329 | static final int BACKUP_APP_MEM; |
| 330 | static final int SECONDARY_SERVER_MEM; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 331 | static final int HEAVY_WEIGHT_APP_MEM; |
| 332 | static final int PERCEPTIBLE_APP_MEM; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 333 | static final int VISIBLE_APP_MEM; |
| 334 | static final int FOREGROUND_APP_MEM; |
| 335 | |
| 336 | // The minimum number of hidden apps we want to be able to keep around, |
| 337 | // without empty apps being able to push them out of memory. |
| 338 | static final int MIN_HIDDEN_APPS = 2; |
| 339 | |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 340 | // The maximum number of hidden processes we will keep around before |
| 341 | // killing them; this is just a control to not let us go too crazy with |
| 342 | // keeping around processes on devices with large amounts of RAM. |
| 343 | static final int MAX_HIDDEN_APPS = 15; |
| 344 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 345 | // We put empty content processes after any hidden processes that have |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 346 | // been idle for less than 15 seconds. |
| 347 | static final long CONTENT_APP_IDLE_OFFSET = 15*1000; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 348 | |
| 349 | // We put empty content processes after any hidden processes that have |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 350 | // been idle for less than 120 seconds. |
| 351 | static final long EMPTY_APP_IDLE_OFFSET = 120*1000; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 352 | |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 353 | static int getIntProp(String name, boolean allowZero) { |
| 354 | String str = SystemProperties.get(name); |
| 355 | if (str == null) { |
| 356 | throw new IllegalArgumentException("Property not defined: " + name); |
| 357 | } |
| 358 | int val = Integer.valueOf(str); |
| 359 | if (val == 0 && !allowZero) { |
| 360 | throw new IllegalArgumentException("Property must not be zero: " + name); |
| 361 | } |
| 362 | return val; |
| 363 | } |
| 364 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 365 | static { |
| 366 | // These values are set in system/rootdir/init.rc on startup. |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 367 | FOREGROUND_APP_ADJ = getIntProp("ro.FOREGROUND_APP_ADJ", true); |
| 368 | VISIBLE_APP_ADJ = getIntProp("ro.VISIBLE_APP_ADJ", true); |
| 369 | PERCEPTIBLE_APP_ADJ = getIntProp("ro.PERCEPTIBLE_APP_ADJ", true); |
| 370 | HEAVY_WEIGHT_APP_ADJ = getIntProp("ro.HEAVY_WEIGHT_APP_ADJ", true); |
| 371 | SECONDARY_SERVER_ADJ = getIntProp("ro.SECONDARY_SERVER_ADJ", true); |
| 372 | BACKUP_APP_ADJ = getIntProp("ro.BACKUP_APP_ADJ", true); |
| 373 | HOME_APP_ADJ = getIntProp("ro.HOME_APP_ADJ", true); |
| 374 | HIDDEN_APP_MIN_ADJ = getIntProp("ro.HIDDEN_APP_MIN_ADJ", true); |
| 375 | EMPTY_APP_ADJ = getIntProp("ro.EMPTY_APP_ADJ", true); |
| 376 | // These days we use the last empty slot for hidden apps as well. |
| 377 | HIDDEN_APP_MAX_ADJ = EMPTY_APP_ADJ; |
| 378 | FOREGROUND_APP_MEM = getIntProp("ro.FOREGROUND_APP_MEM", false)*PAGE_SIZE; |
| 379 | VISIBLE_APP_MEM = getIntProp("ro.VISIBLE_APP_MEM", false)*PAGE_SIZE; |
| 380 | PERCEPTIBLE_APP_MEM = getIntProp("ro.PERCEPTIBLE_APP_MEM", false)*PAGE_SIZE; |
| 381 | HEAVY_WEIGHT_APP_MEM = getIntProp("ro.HEAVY_WEIGHT_APP_MEM", false)*PAGE_SIZE; |
| 382 | SECONDARY_SERVER_MEM = getIntProp("ro.SECONDARY_SERVER_MEM", false)*PAGE_SIZE; |
| 383 | BACKUP_APP_MEM = getIntProp("ro.BACKUP_APP_MEM", false)*PAGE_SIZE; |
| 384 | HOME_APP_MEM = getIntProp("ro.HOME_APP_MEM", false)*PAGE_SIZE; |
| 385 | HIDDEN_APP_MEM = getIntProp("ro.HIDDEN_APP_MEM", false)*PAGE_SIZE; |
| 386 | EMPTY_APP_MEM = getIntProp("ro.EMPTY_APP_MEM", false)*PAGE_SIZE; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 387 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 389 | static final int MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | |
| 391 | static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 392 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 393 | public ActivityStack mMainStack; |
| 394 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 396 | * Description of a request to start a new activity, which has been held |
| 397 | * due to app switches being disabled. |
| 398 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 399 | static class PendingActivityLaunch { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 400 | ActivityRecord r; |
| 401 | ActivityRecord sourceRecord; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 402 | Uri[] grantedUriPermissions; |
| 403 | int grantedMode; |
| 404 | boolean onlyIfNeeded; |
| 405 | } |
| 406 | |
| 407 | final ArrayList<PendingActivityLaunch> mPendingActivityLaunches |
| 408 | = new ArrayList<PendingActivityLaunch>(); |
| 409 | |
| 410 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | * List of all active broadcasts that are to be executed immediately |
| 412 | * (without waiting for another broadcast to finish). Currently this only |
| 413 | * contains broadcasts to registered receivers, to avoid spinning up |
| 414 | * a bunch of processes to execute IntentReceiver components. |
| 415 | */ |
| 416 | final ArrayList<BroadcastRecord> mParallelBroadcasts |
| 417 | = new ArrayList<BroadcastRecord>(); |
| 418 | |
| 419 | /** |
| 420 | * List of all active broadcasts that are to be executed one at a time. |
| 421 | * The object at the top of the list is the currently activity broadcasts; |
| 422 | * those after it are waiting for the top to finish.. |
| 423 | */ |
| 424 | final ArrayList<BroadcastRecord> mOrderedBroadcasts |
| 425 | = new ArrayList<BroadcastRecord>(); |
| 426 | |
| 427 | /** |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 428 | * Historical data of past broadcasts, for debugging. |
| 429 | */ |
| 430 | static final int MAX_BROADCAST_HISTORY = 100; |
| 431 | final BroadcastRecord[] mBroadcastHistory |
| 432 | = new BroadcastRecord[MAX_BROADCAST_HISTORY]; |
| 433 | |
| 434 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | * Set when we current have a BROADCAST_INTENT_MSG in flight. |
| 436 | */ |
| 437 | boolean mBroadcastsScheduled = false; |
| 438 | |
| 439 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | * Activity we have told the window manager to have key focus. |
| 441 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 442 | ActivityRecord mFocusedActivity = null; |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 443 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | * List of intents that were used to start the most recent tasks. |
| 445 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 446 | final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | |
| 448 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | * All of the applications we currently have running organized by name. |
| 450 | * The keys are strings of the application package name (as |
| 451 | * returned by the package manager), and the keys are ApplicationRecord |
| 452 | * objects. |
| 453 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 454 | final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | |
| 456 | /** |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 457 | * The currently running heavy-weight process, if any. |
| 458 | */ |
| 459 | ProcessRecord mHeavyWeightProcess = null; |
| 460 | |
| 461 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | * The last time that various processes have crashed. |
| 463 | */ |
| 464 | final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>(); |
| 465 | |
| 466 | /** |
| 467 | * Set of applications that we consider to be bad, and will reject |
| 468 | * incoming broadcasts from (which the user has no control over). |
| 469 | * Processes are added to this set when they have crashed twice within |
| 470 | * a minimum amount of time; they are removed from it when they are |
| 471 | * later restarted (hopefully due to some user action). The value is the |
| 472 | * time it was added to the list. |
| 473 | */ |
| 474 | final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>(); |
| 475 | |
| 476 | /** |
| 477 | * All of the processes we currently have running organized by pid. |
| 478 | * The keys are the pid running the application. |
| 479 | * |
| 480 | * <p>NOTE: This object is protected by its own lock, NOT the global |
| 481 | * activity manager lock! |
| 482 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 483 | final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | |
| 485 | /** |
| 486 | * All of the processes that have been forced to be foreground. The key |
| 487 | * is the pid of the caller who requested it (we hold a death |
| 488 | * link on it). |
| 489 | */ |
| 490 | abstract class ForegroundToken implements IBinder.DeathRecipient { |
| 491 | int pid; |
| 492 | IBinder token; |
| 493 | } |
| 494 | final SparseArray<ForegroundToken> mForegroundProcesses |
| 495 | = new SparseArray<ForegroundToken>(); |
| 496 | |
| 497 | /** |
| 498 | * List of records for processes that someone had tried to start before the |
| 499 | * system was ready. We don't start them at that point, but ensure they |
| 500 | * are started by the time booting is complete. |
| 501 | */ |
| 502 | final ArrayList<ProcessRecord> mProcessesOnHold |
| 503 | = new ArrayList<ProcessRecord>(); |
| 504 | |
| 505 | /** |
| 506 | * List of records for processes that we have started and are waiting |
| 507 | * for them to call back. This is really only needed when running in |
| 508 | * single processes mode, in which case we do not have a unique pid for |
| 509 | * each process. |
| 510 | */ |
| 511 | final ArrayList<ProcessRecord> mStartingProcesses |
| 512 | = new ArrayList<ProcessRecord>(); |
| 513 | |
| 514 | /** |
| 515 | * List of persistent applications that are in the process |
| 516 | * of being started. |
| 517 | */ |
| 518 | final ArrayList<ProcessRecord> mPersistentStartingProcesses |
| 519 | = new ArrayList<ProcessRecord>(); |
| 520 | |
| 521 | /** |
| 522 | * Processes that are being forcibly torn down. |
| 523 | */ |
| 524 | final ArrayList<ProcessRecord> mRemovedProcesses |
| 525 | = new ArrayList<ProcessRecord>(); |
| 526 | |
| 527 | /** |
| 528 | * List of running applications, sorted by recent usage. |
| 529 | * The first entry in the list is the least recently used. |
| 530 | * It contains ApplicationRecord objects. This list does NOT include |
| 531 | * any persistent application records (since we never want to exit them). |
| 532 | */ |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 533 | final ArrayList<ProcessRecord> mLruProcesses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | = new ArrayList<ProcessRecord>(); |
| 535 | |
| 536 | /** |
| 537 | * List of processes that should gc as soon as things are idle. |
| 538 | */ |
| 539 | final ArrayList<ProcessRecord> mProcessesToGc |
| 540 | = new ArrayList<ProcessRecord>(); |
| 541 | |
| 542 | /** |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 543 | * This is the process holding what we currently consider to be |
| 544 | * the "home" activity. |
| 545 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 546 | ProcessRecord mHomeProcess; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 547 | |
| 548 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 549 | * Packages that the user has asked to have run in screen size |
| 550 | * compatibility mode instead of filling the screen. |
| 551 | */ |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 552 | final CompatModePackages mCompatModePackages; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 553 | |
| 554 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | * Set of PendingResultRecord objects that are currently active. |
| 556 | */ |
| 557 | final HashSet mPendingResultRecords = new HashSet(); |
| 558 | |
| 559 | /** |
| 560 | * Set of IntentSenderRecord objects that are currently active. |
| 561 | */ |
| 562 | final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords |
| 563 | = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>(); |
| 564 | |
| 565 | /** |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 566 | * Fingerprints (hashCode()) of stack traces that we've |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 567 | * already logged DropBox entries for. Guarded by itself. If |
| 568 | * something (rogue user app) forces this over |
| 569 | * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared. |
| 570 | */ |
| 571 | private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>(); |
| 572 | private static final int MAX_DUP_SUPPRESSED_STACKS = 5000; |
| 573 | |
| 574 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 575 | * Strict Mode background batched logging state. |
| 576 | * |
| 577 | * The string buffer is guarded by itself, and its lock is also |
| 578 | * used to determine if another batched write is already |
| 579 | * in-flight. |
| 580 | */ |
| 581 | private final StringBuilder mStrictModeBuffer = new StringBuilder(); |
| 582 | |
| 583 | /** |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 584 | * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler. |
| 585 | */ |
| 586 | private boolean mPendingBroadcastTimeoutMessage; |
| 587 | |
| 588 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | * Intent broadcast that we have tried to start, but are |
| 590 | * waiting for its application's process to be created. We only |
| 591 | * need one (instead of a list) because we always process broadcasts |
| 592 | * one at a time, so no others can be started while waiting for this |
| 593 | * one. |
| 594 | */ |
| 595 | BroadcastRecord mPendingBroadcast = null; |
| 596 | |
| 597 | /** |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 598 | * The receiver index that is pending, to restart the broadcast if needed. |
| 599 | */ |
| 600 | int mPendingBroadcastRecvIndex; |
| 601 | |
| 602 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | * Keeps track of all IIntentReceivers that have been registered for |
| 604 | * broadcasts. Hash keys are the receiver IBinder, hash value is |
| 605 | * a ReceiverList. |
| 606 | */ |
| 607 | final HashMap mRegisteredReceivers = new HashMap(); |
| 608 | |
| 609 | /** |
| 610 | * Resolver for broadcast intents to registered receivers. |
| 611 | * Holds BroadcastFilter (subclass of IntentFilter). |
| 612 | */ |
| 613 | final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver |
| 614 | = new IntentResolver<BroadcastFilter, BroadcastFilter>() { |
| 615 | @Override |
| 616 | protected boolean allowFilterResult( |
| 617 | BroadcastFilter filter, List<BroadcastFilter> dest) { |
| 618 | IBinder target = filter.receiverList.receiver.asBinder(); |
| 619 | for (int i=dest.size()-1; i>=0; i--) { |
| 620 | if (dest.get(i).receiverList.receiver.asBinder() == target) { |
| 621 | return false; |
| 622 | } |
| 623 | } |
| 624 | return true; |
| 625 | } |
| 626 | }; |
| 627 | |
| 628 | /** |
| 629 | * State of all active sticky broadcasts. Keys are the action of the |
| 630 | * sticky Intent, values are an ArrayList of all broadcasted intents with |
| 631 | * that action (which should usually be one). |
| 632 | */ |
| 633 | final HashMap<String, ArrayList<Intent>> mStickyBroadcasts = |
| 634 | new HashMap<String, ArrayList<Intent>>(); |
| 635 | |
| 636 | /** |
| 637 | * All currently running services. |
| 638 | */ |
| 639 | final HashMap<ComponentName, ServiceRecord> mServices = |
| 640 | new HashMap<ComponentName, ServiceRecord>(); |
| 641 | |
| 642 | /** |
| 643 | * All currently running services indexed by the Intent used to start them. |
| 644 | */ |
| 645 | final HashMap<Intent.FilterComparison, ServiceRecord> mServicesByIntent = |
| 646 | new HashMap<Intent.FilterComparison, ServiceRecord>(); |
| 647 | |
| 648 | /** |
| 649 | * All currently bound service connections. Keys are the IBinder of |
| 650 | * the client's IServiceConnection. |
| 651 | */ |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 652 | final HashMap<IBinder, ArrayList<ConnectionRecord>> mServiceConnections |
| 653 | = new HashMap<IBinder, ArrayList<ConnectionRecord>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | |
| 655 | /** |
| 656 | * List of services that we have been asked to start, |
| 657 | * but haven't yet been able to. It is used to hold start requests |
| 658 | * while waiting for their corresponding application thread to get |
| 659 | * going. |
| 660 | */ |
| 661 | final ArrayList<ServiceRecord> mPendingServices |
| 662 | = new ArrayList<ServiceRecord>(); |
| 663 | |
| 664 | /** |
| 665 | * List of services that are scheduled to restart following a crash. |
| 666 | */ |
| 667 | final ArrayList<ServiceRecord> mRestartingServices |
| 668 | = new ArrayList<ServiceRecord>(); |
| 669 | |
| 670 | /** |
| 671 | * List of services that are in the process of being stopped. |
| 672 | */ |
| 673 | final ArrayList<ServiceRecord> mStoppingServices |
| 674 | = new ArrayList<ServiceRecord>(); |
| 675 | |
| 676 | /** |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 677 | * Backup/restore process management |
| 678 | */ |
| 679 | String mBackupAppName = null; |
| 680 | BackupRecord mBackupTarget = null; |
| 681 | |
| 682 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | * List of PendingThumbnailsRecord objects of clients who are still |
| 684 | * waiting to receive all of the thumbnails for a task. |
| 685 | */ |
| 686 | final ArrayList mPendingThumbnails = new ArrayList(); |
| 687 | |
| 688 | /** |
| 689 | * List of HistoryRecord objects that have been finished and must |
| 690 | * still report back to a pending thumbnail receiver. |
| 691 | */ |
| 692 | final ArrayList mCancelledThumbnails = new ArrayList(); |
| 693 | |
| 694 | /** |
| 695 | * All of the currently running global content providers. Keys are a |
| 696 | * string containing the provider name and values are a |
| 697 | * ContentProviderRecord object containing the data about it. Note |
| 698 | * that a single provider may be published under multiple names, so |
| 699 | * there may be multiple entries here for a single one in mProvidersByClass. |
| 700 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 701 | final HashMap<String, ContentProviderRecord> mProvidersByName |
| 702 | = new HashMap<String, ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | |
| 704 | /** |
| 705 | * All of the currently running global content providers. Keys are a |
| 706 | * string containing the provider's implementation class and values are a |
| 707 | * ContentProviderRecord object containing the data about it. |
| 708 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 709 | final HashMap<String, ContentProviderRecord> mProvidersByClass |
| 710 | = new HashMap<String, ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | |
| 712 | /** |
| 713 | * List of content providers who have clients waiting for them. The |
| 714 | * application is currently being launched and the provider will be |
| 715 | * removed from this list once it is published. |
| 716 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 717 | final ArrayList<ContentProviderRecord> mLaunchingProviders |
| 718 | = new ArrayList<ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | |
| 720 | /** |
| 721 | * Global set of specific Uri permissions that have been granted. |
| 722 | */ |
| 723 | final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions |
| 724 | = new SparseArray<HashMap<Uri, UriPermission>>(); |
| 725 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 726 | CoreSettingsObserver mCoreSettingsObserver; |
| 727 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | /** |
| 729 | * Thread-local storage used to carry caller permissions over through |
| 730 | * indirect content-provider access. |
| 731 | * @see #ActivityManagerService.openContentUri() |
| 732 | */ |
| 733 | private class Identity { |
| 734 | public int pid; |
| 735 | public int uid; |
| 736 | |
| 737 | Identity(int _pid, int _uid) { |
| 738 | pid = _pid; |
| 739 | uid = _uid; |
| 740 | } |
| 741 | } |
| 742 | private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>(); |
| 743 | |
| 744 | /** |
| 745 | * All information we have collected about the runtime performance of |
| 746 | * any user id that can impact battery performance. |
| 747 | */ |
| 748 | final BatteryStatsService mBatteryStatsService; |
| 749 | |
| 750 | /** |
| 751 | * information about component usage |
| 752 | */ |
| 753 | final UsageStatsService mUsageStatsService; |
| 754 | |
| 755 | /** |
| 756 | * Current configuration information. HistoryRecord objects are given |
| 757 | * a reference to this object to indicate which configuration they are |
| 758 | * currently running in, so this object must be kept immutable. |
| 759 | */ |
| 760 | Configuration mConfiguration = new Configuration(); |
| 761 | |
| 762 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 763 | * Current sequencing integer of the configuration, for skipping old |
| 764 | * configurations. |
| 765 | */ |
| 766 | int mConfigurationSeq = 0; |
| 767 | |
| 768 | /** |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 769 | * Hardware-reported OpenGLES version. |
| 770 | */ |
| 771 | final int GL_ES_VERSION; |
| 772 | |
| 773 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 774 | * List of initialization arguments to pass to all processes when binding applications to them. |
| 775 | * For example, references to the commonly used services. |
| 776 | */ |
| 777 | HashMap<String, IBinder> mAppBindArgs; |
| 778 | |
| 779 | /** |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 780 | * Temporary to avoid allocations. Protected by main lock. |
| 781 | */ |
| 782 | final StringBuilder mStringBuilder = new StringBuilder(256); |
| 783 | |
| 784 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | * Used to control how we initialize the service. |
| 786 | */ |
| 787 | boolean mStartRunning = false; |
| 788 | ComponentName mTopComponent; |
| 789 | String mTopAction; |
| 790 | String mTopData; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 791 | boolean mProcessesReady = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | boolean mSystemReady = false; |
| 793 | boolean mBooting = false; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 794 | boolean mWaitingUpdate = false; |
| 795 | boolean mDidUpdate = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 796 | boolean mOnBattery = false; |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 797 | boolean mLaunchWarningShown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | |
| 799 | Context mContext; |
| 800 | |
| 801 | int mFactoryTest; |
| 802 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 803 | boolean mCheckedForSetup; |
| 804 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 806 | * The time at which we will allow normal application switches again, |
| 807 | * after a call to {@link #stopAppSwitches()}. |
| 808 | */ |
| 809 | long mAppSwitchesAllowedTime; |
| 810 | |
| 811 | /** |
| 812 | * This is set to true after the first switch after mAppSwitchesAllowedTime |
| 813 | * is set; any switches after that will clear the time. |
| 814 | */ |
| 815 | boolean mDidAppSwitch; |
| 816 | |
| 817 | /** |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 818 | * Last time (in realtime) at which we checked for power usage. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 819 | */ |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 820 | long mLastPowerCheckRealtime; |
| 821 | |
| 822 | /** |
| 823 | * Last time (in uptime) at which we checked for power usage. |
| 824 | */ |
| 825 | long mLastPowerCheckUptime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 826 | |
| 827 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | * Set while we are wanting to sleep, to prevent any |
| 829 | * activities from being started/resumed. |
| 830 | */ |
| 831 | boolean mSleeping = false; |
| 832 | |
| 833 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 834 | * Set if we are shutting down the system, similar to sleeping. |
| 835 | */ |
| 836 | boolean mShuttingDown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | |
| 838 | /** |
| 839 | * Task identifier that activities are currently being started |
| 840 | * in. Incremented each time a new task is created. |
| 841 | * todo: Replace this with a TokenSpace class that generates non-repeating |
| 842 | * integers that won't wrap. |
| 843 | */ |
| 844 | int mCurTask = 1; |
| 845 | |
| 846 | /** |
| 847 | * Current sequence id for oom_adj computation traversal. |
| 848 | */ |
| 849 | int mAdjSeq = 0; |
| 850 | |
| 851 | /** |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 852 | * Current sequence id for process LRU updating. |
| 853 | */ |
| 854 | int mLruSeq = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 855 | |
| 856 | /** |
| 857 | * System monitoring: number of processes that died since the last |
| 858 | * N procs were started. |
| 859 | */ |
| 860 | int[] mProcDeaths = new int[20]; |
| 861 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 862 | /** |
| 863 | * This is set if we had to do a delayed dexopt of an app before launching |
| 864 | * it, to increasing the ANR timeouts in that case. |
| 865 | */ |
| 866 | boolean mDidDexOpt; |
| 867 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | String mDebugApp = null; |
| 869 | boolean mWaitForDebugger = false; |
| 870 | boolean mDebugTransient = false; |
| 871 | String mOrigDebugApp = null; |
| 872 | boolean mOrigWaitForDebugger = false; |
| 873 | boolean mAlwaysFinishActivities = false; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 874 | IActivityController mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 875 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 876 | final RemoteCallbackList<IActivityWatcher> mWatchers |
| 877 | = new RemoteCallbackList<IActivityWatcher>(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 878 | |
| 879 | final RemoteCallbackList<IProcessObserver> mProcessObservers |
| 880 | = new RemoteCallbackList<IProcessObserver>(); |
| 881 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | /** |
| 883 | * Callback of last caller to {@link #requestPss}. |
| 884 | */ |
| 885 | Runnable mRequestPssCallback; |
| 886 | |
| 887 | /** |
| 888 | * Remaining processes for which we are waiting results from the last |
| 889 | * call to {@link #requestPss}. |
| 890 | */ |
| 891 | final ArrayList<ProcessRecord> mRequestPssList |
| 892 | = new ArrayList<ProcessRecord>(); |
| 893 | |
| 894 | /** |
| 895 | * Runtime statistics collection thread. This object's lock is used to |
| 896 | * protect all related state. |
| 897 | */ |
| 898 | final Thread mProcessStatsThread; |
| 899 | |
| 900 | /** |
| 901 | * Used to collect process stats when showing not responding dialog. |
| 902 | * Protected by mProcessStatsThread. |
| 903 | */ |
| 904 | final ProcessStats mProcessStats = new ProcessStats( |
| 905 | MONITOR_THREAD_CPU_USAGE); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 906 | final AtomicLong mLastCpuTime = new AtomicLong(0); |
| 907 | final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true); |
| 908 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | long mLastWriteTime = 0; |
| 910 | |
| 911 | /** |
| 912 | * Set to true after the system has finished booting. |
| 913 | */ |
| 914 | boolean mBooted = false; |
| 915 | |
| 916 | int mProcessLimit = 0; |
| 917 | |
| 918 | WindowManagerService mWindowManager; |
| 919 | |
| 920 | static ActivityManagerService mSelf; |
| 921 | static ActivityThread mSystemThread; |
| 922 | |
| 923 | private final class AppDeathRecipient implements IBinder.DeathRecipient { |
| 924 | final ProcessRecord mApp; |
| 925 | final int mPid; |
| 926 | final IApplicationThread mAppThread; |
| 927 | |
| 928 | AppDeathRecipient(ProcessRecord app, int pid, |
| 929 | IApplicationThread thread) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 930 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | TAG, "New death recipient " + this |
| 932 | + " for thread " + thread.asBinder()); |
| 933 | mApp = app; |
| 934 | mPid = pid; |
| 935 | mAppThread = thread; |
| 936 | } |
| 937 | |
| 938 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 939 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | TAG, "Death received in " + this |
| 941 | + " for thread " + mAppThread.asBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | synchronized(ActivityManagerService.this) { |
| 943 | appDiedLocked(mApp, mPid, mAppThread); |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | static final int SHOW_ERROR_MSG = 1; |
| 949 | static final int SHOW_NOT_RESPONDING_MSG = 2; |
| 950 | static final int SHOW_FACTORY_ERROR_MSG = 3; |
| 951 | static final int UPDATE_CONFIGURATION_MSG = 4; |
| 952 | static final int GC_BACKGROUND_PROCESSES_MSG = 5; |
| 953 | static final int WAIT_FOR_DEBUGGER_MSG = 6; |
| 954 | static final int BROADCAST_INTENT_MSG = 7; |
| 955 | static final int BROADCAST_TIMEOUT_MSG = 8; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | static final int SERVICE_TIMEOUT_MSG = 12; |
| 957 | static final int UPDATE_TIME_ZONE = 13; |
| 958 | static final int SHOW_UID_ERROR_MSG = 14; |
| 959 | static final int IM_FEELING_LUCKY_MSG = 15; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | static final int PROC_START_TIMEOUT_MSG = 20; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 961 | static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 962 | static final int KILL_APPLICATION_MSG = 22; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 963 | static final int FINALIZE_PENDING_INTENT_MSG = 23; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 964 | static final int POST_HEAVY_NOTIFICATION_MSG = 24; |
| 965 | static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 966 | static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 967 | static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 968 | static final int CLEAR_DNS_CACHE = 28; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 969 | static final int UPDATE_HTTP_PROXY = 29; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 970 | static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30; |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 971 | static final int DISPATCH_FOREGROUND_ACTIVITIES_CHANGED = 31; |
| 972 | static final int DISPATCH_PROCESS_DIED = 32; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | |
| 974 | AlertDialog mUidAlert; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 975 | CompatModeDialog mCompatModeDialog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | |
| 977 | final Handler mHandler = new Handler() { |
| 978 | //public Handler() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 979 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | //} |
| 981 | |
| 982 | public void handleMessage(Message msg) { |
| 983 | switch (msg.what) { |
| 984 | case SHOW_ERROR_MSG: { |
| 985 | HashMap data = (HashMap) msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | synchronized (ActivityManagerService.this) { |
| 987 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 988 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 989 | Slog.e(TAG, "App already has crash dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | return; |
| 991 | } |
| 992 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 993 | if (!mSleeping && !mShuttingDown) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 994 | Dialog d = new AppErrorDialog(mContext, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | d.show(); |
| 996 | proc.crashDialog = d; |
| 997 | } else { |
| 998 | // The device is asleep, so just pretend that the user |
| 999 | // saw a crash dialog and hit "force quit". |
| 1000 | res.set(0); |
| 1001 | } |
| 1002 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1003 | |
| 1004 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | } break; |
| 1006 | case SHOW_NOT_RESPONDING_MSG: { |
| 1007 | synchronized (ActivityManagerService.this) { |
| 1008 | HashMap data = (HashMap) msg.obj; |
| 1009 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 1010 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1011 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1012 | return; |
| 1013 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1014 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1015 | Intent intent = new Intent("android.intent.action.ANR"); |
| 1016 | if (!mProcessesReady) { |
| 1017 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 1018 | } |
| 1019 | broadcastIntentLocked(null, null, intent, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1020 | null, null, 0, null, null, null, |
| 1021 | false, false, MY_PID, Process.SYSTEM_UID); |
| 1022 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1024 | mContext, proc, (ActivityRecord)data.get("activity")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | d.show(); |
| 1026 | proc.anrDialog = d; |
| 1027 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1028 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1029 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1030 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1031 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 1032 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 1033 | synchronized (ActivityManagerService.this) { |
| 1034 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 1035 | if (proc == null) { |
| 1036 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 1037 | break; |
| 1038 | } |
| 1039 | if (proc.crashDialog != null) { |
| 1040 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 1041 | return; |
| 1042 | } |
| 1043 | AppErrorResult res = (AppErrorResult) data.get("result"); |
| 1044 | if (!mSleeping && !mShuttingDown) { |
| 1045 | Dialog d = new StrictModeViolationDialog(mContext, res, proc); |
| 1046 | d.show(); |
| 1047 | proc.crashDialog = d; |
| 1048 | } else { |
| 1049 | // The device is asleep, so just pretend that the user |
| 1050 | // saw a crash dialog and hit "force quit". |
| 1051 | res.set(0); |
| 1052 | } |
| 1053 | } |
| 1054 | ensureBootCompleted(); |
| 1055 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | case SHOW_FACTORY_ERROR_MSG: { |
| 1057 | Dialog d = new FactoryErrorDialog( |
| 1058 | mContext, msg.getData().getCharSequence("msg")); |
| 1059 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1060 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1061 | } break; |
| 1062 | case UPDATE_CONFIGURATION_MSG: { |
| 1063 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1064 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 1065 | } break; |
| 1066 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 1067 | synchronized (ActivityManagerService.this) { |
| 1068 | performAppGcsIfAppropriateLocked(); |
| 1069 | } |
| 1070 | } break; |
| 1071 | case WAIT_FOR_DEBUGGER_MSG: { |
| 1072 | synchronized (ActivityManagerService.this) { |
| 1073 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1074 | if (msg.arg1 != 0) { |
| 1075 | if (!app.waitedForDebugger) { |
| 1076 | Dialog d = new AppWaitingForDebuggerDialog( |
| 1077 | ActivityManagerService.this, |
| 1078 | mContext, app); |
| 1079 | app.waitDialog = d; |
| 1080 | app.waitedForDebugger = true; |
| 1081 | d.show(); |
| 1082 | } |
| 1083 | } else { |
| 1084 | if (app.waitDialog != null) { |
| 1085 | app.waitDialog.dismiss(); |
| 1086 | app.waitDialog = null; |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | } break; |
| 1091 | case BROADCAST_INTENT_MSG: { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1092 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | TAG, "Received BROADCAST_INTENT_MSG"); |
| 1094 | processNextBroadcast(true); |
| 1095 | } break; |
| 1096 | case BROADCAST_TIMEOUT_MSG: { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 1097 | synchronized (ActivityManagerService.this) { |
| 1098 | broadcastTimeoutLocked(true); |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 1099 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1101 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1102 | if (mDidDexOpt) { |
| 1103 | mDidDexOpt = false; |
| 1104 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 1105 | nmsg.obj = msg.obj; |
| 1106 | mHandler.sendMessageDelayed(nmsg, SERVICE_TIMEOUT); |
| 1107 | return; |
| 1108 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1109 | serviceTimeout((ProcessRecord)msg.obj); |
| 1110 | } break; |
| 1111 | case UPDATE_TIME_ZONE: { |
| 1112 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1113 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1114 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | if (r.thread != null) { |
| 1116 | try { |
| 1117 | r.thread.updateTimeZone(); |
| 1118 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1119 | 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] | 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1124 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1125 | case CLEAR_DNS_CACHE: { |
| 1126 | synchronized (ActivityManagerService.this) { |
| 1127 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1128 | ProcessRecord r = mLruProcesses.get(i); |
| 1129 | if (r.thread != null) { |
| 1130 | try { |
| 1131 | r.thread.clearDnsCache(); |
| 1132 | } catch (RemoteException ex) { |
| 1133 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1139 | case UPDATE_HTTP_PROXY: { |
| 1140 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 1141 | String host = ""; |
| 1142 | String port = ""; |
| 1143 | String exclList = ""; |
| 1144 | if (proxy != null) { |
| 1145 | host = proxy.getHost(); |
| 1146 | port = Integer.toString(proxy.getPort()); |
| 1147 | exclList = proxy.getExclusionList(); |
| 1148 | } |
| 1149 | synchronized (ActivityManagerService.this) { |
| 1150 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1151 | ProcessRecord r = mLruProcesses.get(i); |
| 1152 | if (r.thread != null) { |
| 1153 | try { |
| 1154 | r.thread.setHttpProxy(host, port, exclList); |
| 1155 | } catch (RemoteException ex) { |
| 1156 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1157 | r.info.processName); |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | case SHOW_UID_ERROR_MSG: { |
| 1164 | // XXX This is a temporary dialog, no need to localize. |
| 1165 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1166 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1167 | d.setCancelable(false); |
| 1168 | d.setTitle("System UIDs Inconsistent"); |
| 1169 | 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] | 1170 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1172 | mUidAlert = d; |
| 1173 | d.show(); |
| 1174 | } break; |
| 1175 | case IM_FEELING_LUCKY_MSG: { |
| 1176 | if (mUidAlert != null) { |
| 1177 | mUidAlert.dismiss(); |
| 1178 | mUidAlert = null; |
| 1179 | } |
| 1180 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1181 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1182 | if (mDidDexOpt) { |
| 1183 | mDidDexOpt = false; |
| 1184 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1185 | nmsg.obj = msg.obj; |
| 1186 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1187 | return; |
| 1188 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1190 | synchronized (ActivityManagerService.this) { |
| 1191 | processStartTimedOutLocked(app); |
| 1192 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1193 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1194 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1195 | synchronized (ActivityManagerService.this) { |
| 1196 | doPendingActivityLaunchesLocked(true); |
| 1197 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1198 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1199 | case KILL_APPLICATION_MSG: { |
| 1200 | synchronized (ActivityManagerService.this) { |
| 1201 | int uid = msg.arg1; |
| 1202 | boolean restart = (msg.arg2 == 1); |
| 1203 | String pkg = (String) msg.obj; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1204 | forceStopPackageLocked(pkg, uid, restart, false, true); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1207 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1208 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1209 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1210 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1211 | INotificationManager inm = NotificationManager.getService(); |
| 1212 | if (inm == null) { |
| 1213 | return; |
| 1214 | } |
| 1215 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1216 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1217 | ProcessRecord process = root.app; |
| 1218 | if (process == null) { |
| 1219 | return; |
| 1220 | } |
| 1221 | |
| 1222 | try { |
| 1223 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1224 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1225 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1226 | Notification notification = new Notification(); |
| 1227 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1228 | notification.when = 0; |
| 1229 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1230 | notification.tickerText = text; |
| 1231 | notification.defaults = 0; // please be quiet |
| 1232 | notification.sound = null; |
| 1233 | notification.vibrate = null; |
| 1234 | notification.setLatestEventInfo(context, text, |
| 1235 | mContext.getText(R.string.heavy_weight_notification_detail), |
| 1236 | PendingIntent.getActivity(mContext, 0, root.intent, |
| 1237 | PendingIntent.FLAG_CANCEL_CURRENT)); |
| 1238 | |
| 1239 | try { |
| 1240 | int[] outId = new int[1]; |
| 1241 | inm.enqueueNotification("android", R.string.heavy_weight_notification, |
| 1242 | notification, outId); |
| 1243 | } catch (RuntimeException e) { |
| 1244 | Slog.w(ActivityManagerService.TAG, |
| 1245 | "Error showing notification for heavy-weight app", e); |
| 1246 | } catch (RemoteException e) { |
| 1247 | } |
| 1248 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1249 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1250 | } |
| 1251 | } break; |
| 1252 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1253 | INotificationManager inm = NotificationManager.getService(); |
| 1254 | if (inm == null) { |
| 1255 | return; |
| 1256 | } |
| 1257 | try { |
| 1258 | inm.cancelNotification("android", |
| 1259 | R.string.heavy_weight_notification); |
| 1260 | } catch (RuntimeException e) { |
| 1261 | Slog.w(ActivityManagerService.TAG, |
| 1262 | "Error canceling notification for service", e); |
| 1263 | } catch (RemoteException e) { |
| 1264 | } |
| 1265 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1266 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1267 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1268 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1269 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1270 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1271 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1272 | } |
| 1273 | } break; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1274 | case SHOW_COMPAT_MODE_DIALOG_MSG: { |
| 1275 | synchronized (ActivityManagerService.this) { |
| 1276 | ActivityRecord ar = (ActivityRecord)msg.obj; |
| 1277 | if (mCompatModeDialog != null) { |
| 1278 | if (mCompatModeDialog.mAppInfo.packageName.equals( |
| 1279 | ar.info.applicationInfo.packageName)) { |
| 1280 | return; |
| 1281 | } |
| 1282 | mCompatModeDialog.dismiss(); |
| 1283 | mCompatModeDialog = null; |
| 1284 | } |
| 1285 | if (ar != null) { |
| 1286 | if (mCompatModePackages.getPackageAskCompatModeLocked( |
| 1287 | ar.packageName)) { |
| 1288 | int mode = mCompatModePackages.computeCompatModeLocked( |
| 1289 | ar.info.applicationInfo); |
| 1290 | if (mode == ActivityManager.COMPAT_MODE_DISABLED |
| 1291 | || mode == ActivityManager.COMPAT_MODE_ENABLED) { |
| 1292 | mCompatModeDialog = new CompatModeDialog( |
| 1293 | ActivityManagerService.this, mContext, |
| 1294 | ar.info.applicationInfo); |
| 1295 | mCompatModeDialog.show(); |
| 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | } |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 1300 | break; |
| 1301 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1302 | case DISPATCH_FOREGROUND_ACTIVITIES_CHANGED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame^] | 1303 | final int pid = msg.arg1; |
| 1304 | final int uid = msg.arg2; |
| 1305 | final boolean foregroundActivities = (Boolean) msg.obj; |
| 1306 | dispatchForegroundActivitiesChanged(pid, uid, foregroundActivities); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1307 | break; |
| 1308 | } |
| 1309 | case DISPATCH_PROCESS_DIED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame^] | 1310 | final int pid = msg.arg1; |
| 1311 | final int uid = msg.arg2; |
| 1312 | dispatchProcessDied(pid, uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1313 | break; |
| 1314 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | }; |
| 1318 | |
| 1319 | public static void setSystemProcess() { |
| 1320 | try { |
| 1321 | ActivityManagerService m = mSelf; |
| 1322 | |
| 1323 | ServiceManager.addService("activity", m); |
| 1324 | ServiceManager.addService("meminfo", new MemBinder(m)); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1325 | ServiceManager.addService("gfxinfo", new GraphicsBinder(m)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1326 | if (MONITOR_CPU_USAGE) { |
| 1327 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1328 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1329 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1330 | |
| 1331 | ApplicationInfo info = |
| 1332 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 1333 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1334 | mSystemThread.installSystemApplicationInfo(info); |
| 1335 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1336 | synchronized (mSelf) { |
| 1337 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1338 | mSystemThread.getApplicationThread(), info, |
| 1339 | info.processName); |
| 1340 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1341 | app.pid = MY_PID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1342 | app.maxAdj = SYSTEM_ADJ; |
| 1343 | mSelf.mProcessNames.put(app.processName, app.info.uid, app); |
| 1344 | synchronized (mSelf.mPidsSelfLocked) { |
| 1345 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1346 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1347 | mSelf.updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1348 | } |
| 1349 | } catch (PackageManager.NameNotFoundException e) { |
| 1350 | throw new RuntimeException( |
| 1351 | "Unable to find android system package", e); |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | public void setWindowManager(WindowManagerService wm) { |
| 1356 | mWindowManager = wm; |
| 1357 | } |
| 1358 | |
| 1359 | public static final Context main(int factoryTest) { |
| 1360 | AThread thr = new AThread(); |
| 1361 | thr.start(); |
| 1362 | |
| 1363 | synchronized (thr) { |
| 1364 | while (thr.mService == null) { |
| 1365 | try { |
| 1366 | thr.wait(); |
| 1367 | } catch (InterruptedException e) { |
| 1368 | } |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | ActivityManagerService m = thr.mService; |
| 1373 | mSelf = m; |
| 1374 | ActivityThread at = ActivityThread.systemMain(); |
| 1375 | mSystemThread = at; |
| 1376 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1377 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1378 | m.mContext = context; |
| 1379 | m.mFactoryTest = factoryTest; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1380 | m.mMainStack = new ActivityStack(m, context, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1381 | |
| 1382 | m.mBatteryStatsService.publish(context); |
| 1383 | m.mUsageStatsService.publish(context); |
| 1384 | |
| 1385 | synchronized (thr) { |
| 1386 | thr.mReady = true; |
| 1387 | thr.notifyAll(); |
| 1388 | } |
| 1389 | |
| 1390 | m.startRunning(null, null, null, null); |
| 1391 | |
| 1392 | return context; |
| 1393 | } |
| 1394 | |
| 1395 | public static ActivityManagerService self() { |
| 1396 | return mSelf; |
| 1397 | } |
| 1398 | |
| 1399 | static class AThread extends Thread { |
| 1400 | ActivityManagerService mService; |
| 1401 | boolean mReady = false; |
| 1402 | |
| 1403 | public AThread() { |
| 1404 | super("ActivityManager"); |
| 1405 | } |
| 1406 | |
| 1407 | public void run() { |
| 1408 | Looper.prepare(); |
| 1409 | |
| 1410 | android.os.Process.setThreadPriority( |
| 1411 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1412 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1413 | |
| 1414 | ActivityManagerService m = new ActivityManagerService(); |
| 1415 | |
| 1416 | synchronized (this) { |
| 1417 | mService = m; |
| 1418 | notifyAll(); |
| 1419 | } |
| 1420 | |
| 1421 | synchronized (this) { |
| 1422 | while (!mReady) { |
| 1423 | try { |
| 1424 | wait(); |
| 1425 | } catch (InterruptedException e) { |
| 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1430 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1431 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1432 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1433 | } |
| 1434 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | Looper.loop(); |
| 1436 | } |
| 1437 | } |
| 1438 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | static class MemBinder extends Binder { |
| 1440 | ActivityManagerService mActivityManagerService; |
| 1441 | MemBinder(ActivityManagerService activityManagerService) { |
| 1442 | mActivityManagerService = activityManagerService; |
| 1443 | } |
| 1444 | |
| 1445 | @Override |
| 1446 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1447 | mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1451 | static class GraphicsBinder extends Binder { |
| 1452 | ActivityManagerService mActivityManagerService; |
| 1453 | GraphicsBinder(ActivityManagerService activityManagerService) { |
| 1454 | mActivityManagerService = activityManagerService; |
| 1455 | } |
| 1456 | |
| 1457 | @Override |
| 1458 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1459 | mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1463 | static class CpuBinder extends Binder { |
| 1464 | ActivityManagerService mActivityManagerService; |
| 1465 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1466 | mActivityManagerService = activityManagerService; |
| 1467 | } |
| 1468 | |
| 1469 | @Override |
| 1470 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1471 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1472 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1473 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1474 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | private ActivityManagerService() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1480 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1481 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1482 | File dataDir = Environment.getDataDirectory(); |
| 1483 | File systemDir = new File(dataDir, "system"); |
| 1484 | systemDir.mkdirs(); |
| 1485 | mBatteryStatsService = new BatteryStatsService(new File( |
| 1486 | systemDir, "batterystats.bin").toString()); |
| 1487 | mBatteryStatsService.getActiveStatistics().readLocked(); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1488 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1489 | mOnBattery = DEBUG_POWER ? true |
| 1490 | : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1491 | mBatteryStatsService.getActiveStatistics().setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1492 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1493 | mUsageStatsService = new UsageStatsService(new File( |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1494 | systemDir, "usagestats").toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1496 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1497 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1498 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1499 | mConfiguration.setToDefaults(); |
| 1500 | mConfiguration.locale = Locale.getDefault(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1501 | mProcessStats.init(); |
| 1502 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 1503 | mCompatModePackages = new CompatModePackages(this, systemDir); |
| 1504 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1505 | // Add ourself to the Watchdog monitors. |
| 1506 | Watchdog.getInstance().addMonitor(this); |
| 1507 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1508 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1509 | public void run() { |
| 1510 | while (true) { |
| 1511 | try { |
| 1512 | try { |
| 1513 | synchronized(this) { |
| 1514 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1515 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1516 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1517 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1518 | // + ", write delay=" + nextWriteDelay); |
| 1519 | if (nextWriteDelay < nextCpuDelay) { |
| 1520 | nextCpuDelay = nextWriteDelay; |
| 1521 | } |
| 1522 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1523 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1524 | this.wait(nextCpuDelay); |
| 1525 | } |
| 1526 | } |
| 1527 | } catch (InterruptedException e) { |
| 1528 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1529 | updateCpuStatsNow(); |
| 1530 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1531 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1532 | } |
| 1533 | } |
| 1534 | } |
| 1535 | }; |
| 1536 | mProcessStatsThread.start(); |
| 1537 | } |
| 1538 | |
| 1539 | @Override |
| 1540 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1541 | throws RemoteException { |
| 1542 | try { |
| 1543 | return super.onTransact(code, data, reply, flags); |
| 1544 | } catch (RuntimeException e) { |
| 1545 | // The activity manager only throws security exceptions, so let's |
| 1546 | // log all others. |
| 1547 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1548 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1549 | } |
| 1550 | throw e; |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1555 | final long now = SystemClock.uptimeMillis(); |
| 1556 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1557 | return; |
| 1558 | } |
| 1559 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1560 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1561 | mProcessStatsThread.notify(); |
| 1562 | } |
| 1563 | } |
| 1564 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1565 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1566 | void updateCpuStatsNow() { |
| 1567 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1568 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1569 | final long now = SystemClock.uptimeMillis(); |
| 1570 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1571 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1572 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1573 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1574 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1575 | haveNewCpuStats = true; |
| 1576 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1577 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1578 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1580 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1581 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1582 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1583 | int user = mProcessStats.getLastUserTime(); |
| 1584 | int system = mProcessStats.getLastSystemTime(); |
| 1585 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1586 | int irq = mProcessStats.getLastIrqTime(); |
| 1587 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1588 | int idle = mProcessStats.getLastIdleTime(); |
| 1589 | |
| 1590 | int total = user + system + iowait + irq + softIrq + idle; |
| 1591 | if (total == 0) total = 1; |
| 1592 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1593 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1595 | (user * 100) / total, |
| 1596 | (system * 100) / total, |
| 1597 | (iowait * 100) / total, |
| 1598 | (irq * 100) / total, |
| 1599 | (softIrq * 100) / total); |
| 1600 | } |
| 1601 | } |
| 1602 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1603 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1604 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1605 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | synchronized(mPidsSelfLocked) { |
| 1607 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1608 | if (mOnBattery) { |
| 1609 | int perc = bstats.startAddingCpuLocked(); |
| 1610 | int totalUTime = 0; |
| 1611 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1612 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1613 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1614 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1615 | if (!st.working) { |
| 1616 | continue; |
| 1617 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1619 | int otherUTime = (st.rel_utime*perc)/100; |
| 1620 | int otherSTime = (st.rel_stime*perc)/100; |
| 1621 | totalUTime += otherUTime; |
| 1622 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1623 | if (pr != null) { |
| 1624 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1625 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1626 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1627 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1628 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1629 | } else { |
| 1630 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1631 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1632 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1633 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1634 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1635 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1636 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1637 | } |
| 1638 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1639 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1640 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1644 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1645 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1646 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1647 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1648 | } |
| 1649 | } |
| 1650 | } |
| 1651 | } |
| 1652 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1653 | @Override |
| 1654 | public void batteryNeedsCpuUpdate() { |
| 1655 | updateCpuStatsNow(); |
| 1656 | } |
| 1657 | |
| 1658 | @Override |
| 1659 | public void batteryPowerChanged(boolean onBattery) { |
| 1660 | // When plugging in, update the CPU stats first before changing |
| 1661 | // the plug state. |
| 1662 | updateCpuStatsNow(); |
| 1663 | synchronized (this) { |
| 1664 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1665 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1670 | /** |
| 1671 | * Initialize the application bind args. These are passed to each |
| 1672 | * process when the bindApplication() IPC is sent to the process. They're |
| 1673 | * lazily setup to make sure the services are running when they're asked for. |
| 1674 | */ |
| 1675 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1676 | if (mAppBindArgs == null) { |
| 1677 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1678 | |
| 1679 | // Setup the application init args |
| 1680 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1681 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1682 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1683 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1684 | } |
| 1685 | return mAppBindArgs; |
| 1686 | } |
| 1687 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1688 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1689 | if (mFocusedActivity != r) { |
| 1690 | mFocusedActivity = r; |
| 1691 | mWindowManager.setFocusedApp(r, true); |
| 1692 | } |
| 1693 | } |
| 1694 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1695 | private final void updateLruProcessInternalLocked(ProcessRecord app, |
| 1696 | boolean oomAdj, boolean updateActivityTime, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1697 | // 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] | 1698 | int lrui = mLruProcesses.indexOf(app); |
| 1699 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1700 | |
| 1701 | int i = mLruProcesses.size()-1; |
| 1702 | int skipTop = 0; |
| 1703 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1704 | app.lruSeq = mLruSeq; |
| 1705 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1706 | // compute the new weight for this process. |
| 1707 | if (updateActivityTime) { |
| 1708 | app.lastActivityTime = SystemClock.uptimeMillis(); |
| 1709 | } |
| 1710 | if (app.activities.size() > 0) { |
| 1711 | // If this process has activities, we more strongly want to keep |
| 1712 | // it around. |
| 1713 | app.lruWeight = app.lastActivityTime; |
| 1714 | } else if (app.pubProviders.size() > 0) { |
| 1715 | // If this process contains content providers, we want to keep |
| 1716 | // it a little more strongly. |
| 1717 | app.lruWeight = app.lastActivityTime - CONTENT_APP_IDLE_OFFSET; |
| 1718 | // Also don't let it kick out the first few "real" hidden processes. |
| 1719 | skipTop = MIN_HIDDEN_APPS; |
| 1720 | } else { |
| 1721 | // If this process doesn't have activities, we less strongly |
| 1722 | // want to keep it around, and generally want to avoid getting |
| 1723 | // in front of any very recently used activities. |
| 1724 | app.lruWeight = app.lastActivityTime - EMPTY_APP_IDLE_OFFSET; |
| 1725 | // Also don't let it kick out the first few "real" hidden processes. |
| 1726 | skipTop = MIN_HIDDEN_APPS; |
| 1727 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1728 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1729 | while (i >= 0) { |
| 1730 | ProcessRecord p = mLruProcesses.get(i); |
| 1731 | // If this app shouldn't be in front of the first N background |
| 1732 | // apps, then skip over that many that are currently hidden. |
| 1733 | if (skipTop > 0 && p.setAdj >= HIDDEN_APP_MIN_ADJ) { |
| 1734 | skipTop--; |
| 1735 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1736 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1737 | mLruProcesses.add(i+1, app); |
| 1738 | break; |
| 1739 | } |
| 1740 | i--; |
| 1741 | } |
| 1742 | if (i < 0) { |
| 1743 | mLruProcesses.add(0, app); |
| 1744 | } |
| 1745 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1746 | // If the app is currently using a content provider or service, |
| 1747 | // bump those processes as well. |
| 1748 | if (app.connections.size() > 0) { |
| 1749 | for (ConnectionRecord cr : app.connections) { |
| 1750 | if (cr.binding != null && cr.binding.service != null |
| 1751 | && cr.binding.service.app != null |
| 1752 | && cr.binding.service.app.lruSeq != mLruSeq) { |
| 1753 | updateLruProcessInternalLocked(cr.binding.service.app, oomAdj, |
| 1754 | updateActivityTime, i+1); |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | if (app.conProviders.size() > 0) { |
| 1759 | for (ContentProviderRecord cpr : app.conProviders.keySet()) { |
| 1760 | if (cpr.app != null && cpr.app.lruSeq != mLruSeq) { |
| 1761 | updateLruProcessInternalLocked(cpr.app, oomAdj, |
| 1762 | updateActivityTime, i+1); |
| 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1767 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1768 | if (oomAdj) { |
| 1769 | updateOomAdjLocked(); |
| 1770 | } |
| 1771 | } |
| 1772 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1773 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1774 | boolean oomAdj, boolean updateActivityTime) { |
| 1775 | mLruSeq++; |
| 1776 | updateLruProcessInternalLocked(app, oomAdj, updateActivityTime, 0); |
| 1777 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1778 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1779 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1780 | String processName, int uid) { |
| 1781 | if (uid == Process.SYSTEM_UID) { |
| 1782 | // The system gets to run in any process. If there are multiple |
| 1783 | // processes with the same uid, just pick the first (this |
| 1784 | // should never happen). |
| 1785 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1786 | processName); |
| 1787 | return procs != null ? procs.valueAt(0) : null; |
| 1788 | } |
| 1789 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1790 | return proc; |
| 1791 | } |
| 1792 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1793 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1794 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1795 | try { |
| 1796 | if (pm.performDexOpt(packageName)) { |
| 1797 | mDidDexOpt = true; |
| 1798 | } |
| 1799 | } catch (RemoteException e) { |
| 1800 | } |
| 1801 | } |
| 1802 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1803 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1804 | int transit = mWindowManager.getPendingAppTransition(); |
| 1805 | return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN |
| 1806 | || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN |
| 1807 | || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT; |
| 1808 | } |
| 1809 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1810 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1811 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1812 | String hostingType, ComponentName hostingName, boolean allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1813 | ProcessRecord app = getProcessRecordLocked(processName, info.uid); |
| 1814 | // We don't have to do anything more if: |
| 1815 | // (1) There is an existing application record; and |
| 1816 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 1817 | // object attached to it so we know it couldn't have crashed; and |
| 1818 | // (3) There is a pid assigned to it, so it is either starting or |
| 1819 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1820 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1821 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 1822 | + " thread=" + (app != null ? app.thread : null) |
| 1823 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1824 | if (app != null && app.pid > 0) { |
| 1825 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1826 | // We already have the app running, or are waiting for it to |
| 1827 | // 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] | 1828 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1829 | return app; |
| 1830 | } else { |
| 1831 | // An application record is attached to a previous process, |
| 1832 | // clean it up now. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1833 | if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1834 | handleAppDiedLocked(app, true); |
| 1835 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1836 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1837 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1838 | String hostingNameStr = hostingName != null |
| 1839 | ? hostingName.flattenToShortString() : null; |
| 1840 | |
| 1841 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 1842 | // If we are in the background, then check to see if this process |
| 1843 | // is bad. If so, we will just silently fail. |
| 1844 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1845 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 1846 | + "/" + info.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1847 | return null; |
| 1848 | } |
| 1849 | } else { |
| 1850 | // When the user is explicitly starting a process, then clear its |
| 1851 | // crash count so that we won't make it bad until they see at |
| 1852 | // least one crash dialog again, and make the process good again |
| 1853 | // if it had been bad. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1854 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
| 1855 | + "/" + info.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1856 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 1857 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1858 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1859 | info.processName); |
| 1860 | mBadProcesses.remove(info.processName, info.uid); |
| 1861 | if (app != null) { |
| 1862 | app.bad = false; |
| 1863 | } |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | if (app == null) { |
| 1868 | app = newProcessRecordLocked(null, info, processName); |
| 1869 | mProcessNames.put(processName, info.uid, app); |
| 1870 | } else { |
| 1871 | // If this is a new package in the process, add the package to the list |
| 1872 | app.addPackage(info.packageName); |
| 1873 | } |
| 1874 | |
| 1875 | // If the system is not ready yet, then hold off on starting this |
| 1876 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1877 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1878 | && !isAllowedWhileBooting(info) |
| 1879 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1880 | if (!mProcessesOnHold.contains(app)) { |
| 1881 | mProcessesOnHold.add(app); |
| 1882 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1883 | 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] | 1884 | return app; |
| 1885 | } |
| 1886 | |
| 1887 | startProcessLocked(app, hostingType, hostingNameStr); |
| 1888 | return (app.pid != 0) ? app : null; |
| 1889 | } |
| 1890 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1891 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 1892 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 1893 | } |
| 1894 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1895 | private final void startProcessLocked(ProcessRecord app, |
| 1896 | String hostingType, String hostingNameStr) { |
| 1897 | if (app.pid > 0 && app.pid != MY_PID) { |
| 1898 | synchronized (mPidsSelfLocked) { |
| 1899 | mPidsSelfLocked.remove(app.pid); |
| 1900 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 1901 | } |
| 1902 | app.pid = 0; |
| 1903 | } |
| 1904 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1905 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 1906 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1907 | mProcessesOnHold.remove(app); |
| 1908 | |
| 1909 | updateCpuStats(); |
| 1910 | |
| 1911 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 1912 | mProcDeaths[0] = 0; |
| 1913 | |
| 1914 | try { |
| 1915 | int uid = app.info.uid; |
| 1916 | int[] gids = null; |
| 1917 | try { |
| 1918 | gids = mContext.getPackageManager().getPackageGids( |
| 1919 | app.info.packageName); |
| 1920 | } catch (PackageManager.NameNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1921 | Slog.w(TAG, "Unable to retrieve gids", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1922 | } |
| 1923 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 1924 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 1925 | && mTopComponent != null |
| 1926 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 1927 | uid = 0; |
| 1928 | } |
| 1929 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 1930 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 1931 | uid = 0; |
| 1932 | } |
| 1933 | } |
| 1934 | int debugFlags = 0; |
| 1935 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 1936 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
| 1937 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 1938 | // Run the app in safe mode if its manifest requests so or the |
| 1939 | // system is booted in safe mode. |
| 1940 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 1941 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 1942 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 1943 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1944 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 1945 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 1946 | } |
| 1947 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 1948 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 1949 | } |
| 1950 | int pid = Process.start("android.app.ActivityThread", |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 1951 | app.processName, uid, uid, gids, debugFlags, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1952 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 1953 | synchronized (bs) { |
| 1954 | if (bs.isOnBattery()) { |
| 1955 | app.batteryStats.incStartsLocked(); |
| 1956 | } |
| 1957 | } |
| 1958 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1959 | EventLog.writeEvent(EventLogTags.AM_PROC_START, pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1960 | app.processName, hostingType, |
| 1961 | hostingNameStr != null ? hostingNameStr : ""); |
| 1962 | |
| 1963 | if (app.persistent) { |
Christopher Tate | c27181c | 2010-06-30 14:41:09 -0700 | [diff] [blame] | 1964 | Watchdog.getInstance().processStarted(app.processName, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1965 | } |
| 1966 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 1967 | StringBuilder buf = mStringBuilder; |
| 1968 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1969 | buf.append("Start proc "); |
| 1970 | buf.append(app.processName); |
| 1971 | buf.append(" for "); |
| 1972 | buf.append(hostingType); |
| 1973 | if (hostingNameStr != null) { |
| 1974 | buf.append(" "); |
| 1975 | buf.append(hostingNameStr); |
| 1976 | } |
| 1977 | buf.append(": pid="); |
| 1978 | buf.append(pid); |
| 1979 | buf.append(" uid="); |
| 1980 | buf.append(uid); |
| 1981 | buf.append(" gids={"); |
| 1982 | if (gids != null) { |
| 1983 | for (int gi=0; gi<gids.length; gi++) { |
| 1984 | if (gi != 0) buf.append(", "); |
| 1985 | buf.append(gids[gi]); |
| 1986 | |
| 1987 | } |
| 1988 | } |
| 1989 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1990 | Slog.i(TAG, buf.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1991 | if (pid == 0 || pid == MY_PID) { |
| 1992 | // Processes are being emulated with threads. |
| 1993 | app.pid = MY_PID; |
| 1994 | app.removed = false; |
| 1995 | mStartingProcesses.add(app); |
| 1996 | } else if (pid > 0) { |
| 1997 | app.pid = pid; |
| 1998 | app.removed = false; |
| 1999 | synchronized (mPidsSelfLocked) { |
| 2000 | this.mPidsSelfLocked.put(pid, app); |
| 2001 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 2002 | msg.obj = app; |
| 2003 | mHandler.sendMessageDelayed(msg, PROC_START_TIMEOUT); |
| 2004 | } |
| 2005 | } else { |
| 2006 | app.pid = 0; |
| 2007 | RuntimeException e = new RuntimeException( |
| 2008 | "Failure starting process " + app.processName |
| 2009 | + ": returned pid=" + pid); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2010 | Slog.e(TAG, e.getMessage(), e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2011 | } |
| 2012 | } catch (RuntimeException e) { |
| 2013 | // XXX do better error recovery. |
| 2014 | app.pid = 0; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2015 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2016 | } |
| 2017 | } |
| 2018 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2019 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2020 | if (resumed) { |
| 2021 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 2022 | } else { |
| 2023 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 2024 | } |
| 2025 | } |
| 2026 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2027 | boolean startHomeActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2028 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2029 | && mTopAction == null) { |
| 2030 | // We are running in factory test mode, but unable to find |
| 2031 | // the factory test app, so just sit around displaying the |
| 2032 | // error message and don't try to start anything. |
| 2033 | return false; |
| 2034 | } |
| 2035 | Intent intent = new Intent( |
| 2036 | mTopAction, |
| 2037 | mTopData != null ? Uri.parse(mTopData) : null); |
| 2038 | intent.setComponent(mTopComponent); |
| 2039 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 2040 | intent.addCategory(Intent.CATEGORY_HOME); |
| 2041 | } |
| 2042 | ActivityInfo aInfo = |
| 2043 | intent.resolveActivityInfo(mContext.getPackageManager(), |
| 2044 | STOCK_PM_FLAGS); |
| 2045 | if (aInfo != null) { |
| 2046 | intent.setComponent(new ComponentName( |
| 2047 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2048 | // Don't do this if the home app is currently being |
| 2049 | // instrumented. |
| 2050 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 2051 | aInfo.applicationInfo.uid); |
| 2052 | if (app == null || app.instrumentationClass == null) { |
| 2053 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2054 | mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2055 | null, null, 0, 0, 0, false, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | |
| 2060 | return true; |
| 2061 | } |
| 2062 | |
| 2063 | /** |
| 2064 | * Starts the "new version setup screen" if appropriate. |
| 2065 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2066 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2067 | // Only do this once per boot. |
| 2068 | if (mCheckedForSetup) { |
| 2069 | return; |
| 2070 | } |
| 2071 | |
| 2072 | // We will show this screen if the current one is a different |
| 2073 | // version than the last one shown, and we are not running in |
| 2074 | // low-level factory test mode. |
| 2075 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2076 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
| 2077 | Settings.Secure.getInt(resolver, |
| 2078 | Settings.Secure.DEVICE_PROVISIONED, 0) != 0) { |
| 2079 | mCheckedForSetup = true; |
| 2080 | |
| 2081 | // See if we should be showing the platform update setup UI. |
| 2082 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 2083 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 2084 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 2085 | |
| 2086 | // We don't allow third party apps to replace this. |
| 2087 | ResolveInfo ri = null; |
| 2088 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 2089 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 2090 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2091 | ri = ris.get(i); |
| 2092 | break; |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | if (ri != null) { |
| 2097 | String vers = ri.activityInfo.metaData != null |
| 2098 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 2099 | : null; |
| 2100 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2101 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2102 | Intent.METADATA_SETUP_VERSION); |
| 2103 | } |
| 2104 | String lastVers = Settings.Secure.getString( |
| 2105 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2106 | if (vers != null && !vers.equals(lastVers)) { |
| 2107 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2108 | intent.setComponent(new ComponentName( |
| 2109 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2110 | mMainStack.startActivityLocked(null, intent, null, null, 0, ri.activityInfo, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2111 | null, null, 0, 0, 0, false, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2112 | } |
| 2113 | } |
| 2114 | } |
| 2115 | } |
| 2116 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2117 | CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2118 | return mCompatModePackages.compatibilityInfoForPackageLocked(ai); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2119 | } |
| 2120 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2121 | public int getFrontActivityScreenCompatMode() { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2122 | synchronized (this) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2123 | return mCompatModePackages.getFrontActivityScreenCompatModeLocked(); |
| 2124 | } |
| 2125 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2126 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2127 | public void setFrontActivityScreenCompatMode(int mode) { |
| 2128 | synchronized (this) { |
| 2129 | mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode); |
| 2130 | } |
| 2131 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2132 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2133 | public int getPackageScreenCompatMode(String packageName) { |
| 2134 | synchronized (this) { |
| 2135 | return mCompatModePackages.getPackageScreenCompatModeLocked(packageName); |
| 2136 | } |
| 2137 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2138 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2139 | public void setPackageScreenCompatMode(String packageName, int mode) { |
| 2140 | synchronized (this) { |
| 2141 | mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2142 | } |
| 2143 | } |
| 2144 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2145 | public boolean getPackageAskScreenCompat(String packageName) { |
| 2146 | synchronized (this) { |
| 2147 | return mCompatModePackages.getPackageAskCompatModeLocked(packageName); |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | public void setPackageAskScreenCompat(String packageName, boolean ask) { |
| 2152 | synchronized (this) { |
| 2153 | mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask); |
| 2154 | } |
| 2155 | } |
| 2156 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2157 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2158 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2159 | |
| 2160 | final int identHash = System.identityHashCode(r); |
| 2161 | updateUsageStats(r, true); |
| 2162 | |
| 2163 | int i = mWatchers.beginBroadcast(); |
| 2164 | while (i > 0) { |
| 2165 | i--; |
| 2166 | IActivityWatcher w = mWatchers.getBroadcastItem(i); |
| 2167 | if (w != null) { |
| 2168 | try { |
| 2169 | w.activityResuming(identHash); |
| 2170 | } catch (RemoteException e) { |
| 2171 | } |
| 2172 | } |
| 2173 | } |
| 2174 | mWatchers.finishBroadcast(); |
| 2175 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2176 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2177 | private void dispatchForegroundActivitiesChanged(int pid, int uid, boolean foregroundActivities) { |
| 2178 | int i = mProcessObservers.beginBroadcast(); |
| 2179 | while (i > 0) { |
| 2180 | i--; |
| 2181 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2182 | if (observer != null) { |
| 2183 | try { |
| 2184 | observer.onForegroundActivitiesChanged(pid, uid, foregroundActivities); |
| 2185 | } catch (RemoteException e) { |
| 2186 | } |
| 2187 | } |
| 2188 | } |
| 2189 | mProcessObservers.finishBroadcast(); |
| 2190 | } |
| 2191 | |
| 2192 | private void dispatchProcessDied(int pid, int uid) { |
| 2193 | int i = mProcessObservers.beginBroadcast(); |
| 2194 | while (i > 0) { |
| 2195 | i--; |
| 2196 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2197 | if (observer != null) { |
| 2198 | try { |
| 2199 | observer.onProcessDied(pid, uid); |
| 2200 | } catch (RemoteException e) { |
| 2201 | } |
| 2202 | } |
| 2203 | } |
| 2204 | mProcessObservers.finishBroadcast(); |
| 2205 | } |
| 2206 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2207 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2208 | final int N = mPendingActivityLaunches.size(); |
| 2209 | if (N <= 0) { |
| 2210 | return; |
| 2211 | } |
| 2212 | for (int i=0; i<N; i++) { |
| 2213 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2214 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2215 | pal.grantedUriPermissions, pal.grantedMode, pal.onlyIfNeeded, |
| 2216 | doResume && i == (N-1)); |
| 2217 | } |
| 2218 | mPendingActivityLaunches.clear(); |
| 2219 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2220 | |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2221 | public final int startActivity(IApplicationThread caller, |
| 2222 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2223 | int grantedMode, IBinder resultTo, |
| 2224 | String resultWho, int requestCode, boolean onlyIfNeeded, |
| 2225 | boolean debug) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2226 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2227 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
| 2228 | requestCode, onlyIfNeeded, debug, null, null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | public final WaitResult startActivityAndWait(IApplicationThread caller, |
| 2232 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2233 | int grantedMode, IBinder resultTo, |
| 2234 | String resultWho, int requestCode, boolean onlyIfNeeded, |
| 2235 | boolean debug) { |
| 2236 | WaitResult res = new WaitResult(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2237 | mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2238 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
| 2239 | requestCode, onlyIfNeeded, debug, res, null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2240 | return res; |
| 2241 | } |
| 2242 | |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2243 | public final int startActivityWithConfig(IApplicationThread caller, |
| 2244 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2245 | int grantedMode, IBinder resultTo, |
| 2246 | String resultWho, int requestCode, boolean onlyIfNeeded, |
| 2247 | boolean debug, Configuration config) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2248 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2249 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
| 2250 | requestCode, onlyIfNeeded, debug, null, config); |
| 2251 | } |
| 2252 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2253 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2254 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2255 | IBinder resultTo, String resultWho, int requestCode, |
| 2256 | int flagsMask, int flagsValues) { |
| 2257 | // Refuse possible leaked file descriptors |
| 2258 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2259 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2260 | } |
| 2261 | |
| 2262 | IIntentSender sender = intent.getTarget(); |
| 2263 | if (!(sender instanceof PendingIntentRecord)) { |
| 2264 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2265 | } |
| 2266 | |
| 2267 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2268 | |
| 2269 | synchronized (this) { |
| 2270 | // If this is coming from the currently resumed activity, it is |
| 2271 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2272 | if (mMainStack.mResumedActivity != null |
| 2273 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2274 | Binder.getCallingUid()) { |
| 2275 | mAppSwitchesAllowedTime = 0; |
| 2276 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | return pir.sendInner(0, fillInIntent, resolvedType, |
| 2280 | null, resultTo, resultWho, requestCode, flagsMask, flagsValues); |
| 2281 | } |
| 2282 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2283 | public boolean startNextMatchingActivity(IBinder callingActivity, |
| 2284 | Intent intent) { |
| 2285 | // Refuse possible leaked file descriptors |
| 2286 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2287 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2288 | } |
| 2289 | |
| 2290 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2291 | int index = mMainStack.indexOfTokenLocked(callingActivity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2292 | if (index < 0) { |
| 2293 | return false; |
| 2294 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2295 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2296 | if (r.app == null || r.app.thread == null) { |
| 2297 | // The caller is not running... d'oh! |
| 2298 | return false; |
| 2299 | } |
| 2300 | intent = new Intent(intent); |
| 2301 | // The caller is not allowed to change the data. |
| 2302 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2303 | // And we are resetting to find the next component... |
| 2304 | intent.setComponent(null); |
| 2305 | |
| 2306 | ActivityInfo aInfo = null; |
| 2307 | try { |
| 2308 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2309 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2310 | intent, r.resolvedType, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 2311 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2312 | |
| 2313 | // Look for the original activity in the list... |
| 2314 | final int N = resolves != null ? resolves.size() : 0; |
| 2315 | for (int i=0; i<N; i++) { |
| 2316 | ResolveInfo rInfo = resolves.get(i); |
| 2317 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2318 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2319 | // We found the current one... the next matching is |
| 2320 | // after it. |
| 2321 | i++; |
| 2322 | if (i<N) { |
| 2323 | aInfo = resolves.get(i).activityInfo; |
| 2324 | } |
| 2325 | break; |
| 2326 | } |
| 2327 | } |
| 2328 | } catch (RemoteException e) { |
| 2329 | } |
| 2330 | |
| 2331 | if (aInfo == null) { |
| 2332 | // Nobody who is next! |
| 2333 | return false; |
| 2334 | } |
| 2335 | |
| 2336 | intent.setComponent(new ComponentName( |
| 2337 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2338 | intent.setFlags(intent.getFlags()&~( |
| 2339 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2340 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2341 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2342 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2343 | |
| 2344 | // Okay now we need to start the new activity, replacing the |
| 2345 | // currently running activity. This is a little tricky because |
| 2346 | // we want to start the new one as if the current one is finished, |
| 2347 | // but not finish the current one first so that there is no flicker. |
| 2348 | // And thus... |
| 2349 | final boolean wasFinishing = r.finishing; |
| 2350 | r.finishing = true; |
| 2351 | |
| 2352 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2353 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2354 | final String resultWho = r.resultWho; |
| 2355 | final int requestCode = r.requestCode; |
| 2356 | r.resultTo = null; |
| 2357 | if (resultTo != null) { |
| 2358 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2359 | } |
| 2360 | |
| 2361 | final long origId = Binder.clearCallingIdentity(); |
| 2362 | // XXX we are not dealing with propagating grantedUriPermissions... |
| 2363 | // 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] | 2364 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2365 | r.resolvedType, null, 0, aInfo, resultTo, resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2366 | requestCode, -1, r.launchedFromUid, false, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2367 | Binder.restoreCallingIdentity(origId); |
| 2368 | |
| 2369 | r.finishing = wasFinishing; |
| 2370 | if (res != START_SUCCESS) { |
| 2371 | return false; |
| 2372 | } |
| 2373 | return true; |
| 2374 | } |
| 2375 | } |
| 2376 | |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2377 | public final int startActivityInPackage(int uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2378 | Intent intent, String resolvedType, IBinder resultTo, |
| 2379 | String resultWho, int requestCode, boolean onlyIfNeeded) { |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2380 | |
| 2381 | // This is so super not safe, that only the system (or okay root) |
| 2382 | // can do it. |
| 2383 | final int callingUid = Binder.getCallingUid(); |
| 2384 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2385 | throw new SecurityException( |
| 2386 | "startActivityInPackage only available to the system"); |
| 2387 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2388 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2389 | return mMainStack.startActivityMayWait(null, uid, intent, resolvedType, |
| 2390 | null, 0, resultTo, resultWho, requestCode, onlyIfNeeded, false, null, null); |
| 2391 | } |
| 2392 | |
| 2393 | public final int startActivities(IApplicationThread caller, |
| 2394 | Intent[] intents, String[] resolvedTypes, IBinder resultTo) { |
| 2395 | return mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo); |
| 2396 | } |
| 2397 | |
| 2398 | public final int startActivitiesInPackage(int uid, |
| 2399 | Intent[] intents, String[] resolvedTypes, IBinder resultTo) { |
| 2400 | |
| 2401 | // This is so super not safe, that only the system (or okay root) |
| 2402 | // can do it. |
| 2403 | final int callingUid = Binder.getCallingUid(); |
| 2404 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2405 | throw new SecurityException( |
| 2406 | "startActivityInPackage only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2407 | } |
| 2408 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2409 | return mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2410 | } |
| 2411 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2412 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2413 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2414 | // Quick case: check if the top-most recent task is the same. |
| 2415 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2416 | return; |
| 2417 | } |
| 2418 | // 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] | 2419 | for (int i=0; i<N; i++) { |
| 2420 | TaskRecord tr = mRecentTasks.get(i); |
| 2421 | if ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2422 | || (task.intent != null && task.intent.filterEquals(tr.intent))) { |
| 2423 | mRecentTasks.remove(i); |
| 2424 | i--; |
| 2425 | N--; |
| 2426 | if (task.intent == null) { |
| 2427 | // If the new recent task we are adding is not fully |
| 2428 | // specified, then replace it with the existing recent task. |
| 2429 | task = tr; |
| 2430 | } |
| 2431 | } |
| 2432 | } |
| 2433 | if (N >= MAX_RECENT_TASKS) { |
| 2434 | mRecentTasks.remove(N-1); |
| 2435 | } |
| 2436 | mRecentTasks.add(0, task); |
| 2437 | } |
| 2438 | |
| 2439 | public void setRequestedOrientation(IBinder token, |
| 2440 | int requestedOrientation) { |
| 2441 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2442 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2443 | if (index < 0) { |
| 2444 | return; |
| 2445 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2446 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2447 | final long origId = Binder.clearCallingIdentity(); |
| 2448 | mWindowManager.setAppOrientation(r, requestedOrientation); |
| 2449 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2450 | mConfiguration, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2451 | r.mayFreezeScreenLocked(r.app) ? r : null); |
| 2452 | if (config != null) { |
| 2453 | r.frozenBeforeDestroy = true; |
| 2454 | if (!updateConfigurationLocked(config, r)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2455 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2456 | } |
| 2457 | } |
| 2458 | Binder.restoreCallingIdentity(origId); |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | public int getRequestedOrientation(IBinder token) { |
| 2463 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2464 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2465 | if (index < 0) { |
| 2466 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2467 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2468 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2469 | return mWindowManager.getAppOrientation(r); |
| 2470 | } |
| 2471 | } |
| 2472 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2473 | /** |
| 2474 | * This is the internal entry point for handling Activity.finish(). |
| 2475 | * |
| 2476 | * @param token The Binder token referencing the Activity we want to finish. |
| 2477 | * @param resultCode Result code, if any, from this Activity. |
| 2478 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2479 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2480 | * @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] | 2481 | */ |
| 2482 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2483 | // Refuse possible leaked file descriptors |
| 2484 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2485 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2486 | } |
| 2487 | |
| 2488 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2489 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2490 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2491 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2492 | if (next != null) { |
| 2493 | // ask watcher if this is allowed |
| 2494 | boolean resumeOK = true; |
| 2495 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2496 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2497 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2498 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | if (!resumeOK) { |
| 2502 | return false; |
| 2503 | } |
| 2504 | } |
| 2505 | } |
| 2506 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2507 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2508 | resultData, "app-request"); |
| 2509 | Binder.restoreCallingIdentity(origId); |
| 2510 | return res; |
| 2511 | } |
| 2512 | } |
| 2513 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2514 | public final void finishHeavyWeightApp() { |
| 2515 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2516 | != PackageManager.PERMISSION_GRANTED) { |
| 2517 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2518 | + Binder.getCallingPid() |
| 2519 | + ", uid=" + Binder.getCallingUid() |
| 2520 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2521 | Slog.w(TAG, msg); |
| 2522 | throw new SecurityException(msg); |
| 2523 | } |
| 2524 | |
| 2525 | synchronized(this) { |
| 2526 | if (mHeavyWeightProcess == null) { |
| 2527 | return; |
| 2528 | } |
| 2529 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2530 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2531 | mHeavyWeightProcess.activities); |
| 2532 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2533 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2534 | if (!r.finishing) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2535 | int index = mMainStack.indexOfTokenLocked(r); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2536 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2537 | mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED, |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2538 | null, "finish-heavy"); |
| 2539 | } |
| 2540 | } |
| 2541 | } |
| 2542 | |
| 2543 | mHeavyWeightProcess = null; |
| 2544 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 2545 | } |
| 2546 | } |
| 2547 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2548 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2549 | String message) { |
| 2550 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2551 | != PackageManager.PERMISSION_GRANTED) { |
| 2552 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2553 | + Binder.getCallingPid() |
| 2554 | + ", uid=" + Binder.getCallingUid() |
| 2555 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2556 | Slog.w(TAG, msg); |
| 2557 | throw new SecurityException(msg); |
| 2558 | } |
| 2559 | |
| 2560 | synchronized(this) { |
| 2561 | ProcessRecord proc = null; |
| 2562 | |
| 2563 | // Figure out which process to kill. We don't trust that initialPid |
| 2564 | // still has any relation to current pids, so must scan through the |
| 2565 | // list. |
| 2566 | synchronized (mPidsSelfLocked) { |
| 2567 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2568 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
| 2569 | if (p.info.uid != uid) { |
| 2570 | continue; |
| 2571 | } |
| 2572 | if (p.pid == initialPid) { |
| 2573 | proc = p; |
| 2574 | break; |
| 2575 | } |
| 2576 | for (String str : p.pkgList) { |
| 2577 | if (str.equals(packageName)) { |
| 2578 | proc = p; |
| 2579 | } |
| 2580 | } |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2585 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2586 | + " initialPid=" + initialPid |
| 2587 | + " packageName=" + packageName); |
| 2588 | return; |
| 2589 | } |
| 2590 | |
| 2591 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2592 | if (proc.pid == Process.myPid()) { |
| 2593 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2594 | return; |
| 2595 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2596 | long ident = Binder.clearCallingIdentity(); |
| 2597 | try { |
| 2598 | proc.thread.scheduleCrash(message); |
| 2599 | } catch (RemoteException e) { |
| 2600 | } |
| 2601 | Binder.restoreCallingIdentity(ident); |
| 2602 | } |
| 2603 | } |
| 2604 | } |
| 2605 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2606 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2607 | int requestCode) { |
| 2608 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2609 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2610 | if (index < 0) { |
| 2611 | return; |
| 2612 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2613 | ActivityRecord self = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2614 | |
| 2615 | final long origId = Binder.clearCallingIdentity(); |
| 2616 | |
| 2617 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2618 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2619 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2620 | if (r.resultTo == self && r.requestCode == requestCode) { |
| 2621 | if ((r.resultWho == null && resultWho == null) || |
| 2622 | (r.resultWho != null && r.resultWho.equals(resultWho))) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2623 | mMainStack.finishActivityLocked(r, i, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2624 | Activity.RESULT_CANCELED, null, "request-sub"); |
| 2625 | } |
| 2626 | } |
| 2627 | } |
| 2628 | |
| 2629 | Binder.restoreCallingIdentity(origId); |
| 2630 | } |
| 2631 | } |
| 2632 | |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2633 | public boolean willActivityBeVisible(IBinder token) { |
| 2634 | synchronized(this) { |
| 2635 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2636 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2637 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2638 | if (r == token) { |
| 2639 | return true; |
| 2640 | } |
| 2641 | if (r.fullscreen && !r.finishing) { |
| 2642 | return false; |
| 2643 | } |
| 2644 | } |
| 2645 | return true; |
| 2646 | } |
| 2647 | } |
| 2648 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2649 | public void overridePendingTransition(IBinder token, String packageName, |
| 2650 | int enterAnim, int exitAnim) { |
| 2651 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2652 | int index = mMainStack.indexOfTokenLocked(token); |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2653 | if (index < 0) { |
| 2654 | return; |
| 2655 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2656 | ActivityRecord self = (ActivityRecord)mMainStack.mHistory.get(index); |
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, |
| 2676 | boolean restarting) { |
| 2677 | cleanUpApplicationRecordLocked(app, restarting, -1); |
| 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 | |
| 2682 | // Just in case... |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2683 | if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) { |
| 2684 | if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity); |
| 2685 | mMainStack.mPausingActivity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2686 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2687 | if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) { |
| 2688 | mMainStack.mLastPausedActivity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2689 | } |
| 2690 | |
| 2691 | // Remove this application's activities from active lists. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2692 | mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2693 | |
| 2694 | boolean atTop = true; |
| 2695 | boolean hasVisibleActivities = false; |
| 2696 | |
| 2697 | // Clean out the history list. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2698 | int i = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2699 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2700 | TAG, "Removing app " + app + " from history with " + i + " entries"); |
| 2701 | while (i > 0) { |
| 2702 | i--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2703 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2704 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2705 | TAG, "Record #" + i + " " + r + ": app=" + r.app); |
| 2706 | if (r.app == app) { |
| 2707 | if ((!r.haveState && !r.stateNotNeeded) || r.finishing) { |
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, "Removing this entry! frozen=" + r.haveState |
| 2710 | + " finishing=" + r.finishing); |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 2711 | r.makeFinishing(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2712 | mMainStack.mHistory.remove(i); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 2713 | r.takeFromHistory(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2714 | mWindowManager.removeAppToken(r); |
| 2715 | if (VALIDATE_TOKENS) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2716 | mWindowManager.validateAppTokens(mMainStack.mHistory); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2717 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2718 | r.removeUriPermissionsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2719 | |
| 2720 | } else { |
| 2721 | // We have the current state for this activity, so |
| 2722 | // it can be restarted later when needed. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2723 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2724 | TAG, "Keeping entry, setting app to null"); |
| 2725 | if (r.visible) { |
| 2726 | hasVisibleActivities = true; |
| 2727 | } |
| 2728 | r.app = null; |
| 2729 | r.nowVisible = false; |
| 2730 | if (!r.haveState) { |
| 2731 | r.icicle = null; |
| 2732 | } |
| 2733 | } |
| 2734 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2735 | r.stack.cleanUpActivityLocked(r, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2736 | r.state = ActivityState.STOPPED; |
| 2737 | } |
| 2738 | atTop = false; |
| 2739 | } |
| 2740 | |
| 2741 | app.activities.clear(); |
| 2742 | |
| 2743 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2744 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2745 | + " running instrumentation " + app.instrumentationClass); |
| 2746 | Bundle info = new Bundle(); |
| 2747 | info.putString("shortMsg", "Process crashed."); |
| 2748 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 2749 | } |
| 2750 | |
| 2751 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2752 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2753 | // If there was nothing to resume, and we are not already |
| 2754 | // restarting this process, but there is a visible activity that |
| 2755 | // is hosted by the process... then make sure all visible |
| 2756 | // activities are running, taking care of restarting this |
| 2757 | // process. |
| 2758 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2759 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2760 | } |
| 2761 | } |
| 2762 | } |
| 2763 | } |
| 2764 | |
| 2765 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 2766 | IBinder threadBinder = thread.asBinder(); |
| 2767 | |
| 2768 | // Find the application record. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2769 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2770 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2771 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 2772 | return i; |
| 2773 | } |
| 2774 | } |
| 2775 | return -1; |
| 2776 | } |
| 2777 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2778 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2779 | IApplicationThread thread) { |
| 2780 | if (thread == null) { |
| 2781 | return null; |
| 2782 | } |
| 2783 | |
| 2784 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2785 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2786 | } |
| 2787 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2788 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2789 | IApplicationThread thread) { |
| 2790 | |
| 2791 | mProcDeaths[0]++; |
| 2792 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2793 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 2794 | synchronized (stats) { |
| 2795 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 2796 | } |
| 2797 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2798 | // Clean up already done if the process has been re-started. |
| 2799 | if (app.pid == pid && app.thread != null && |
| 2800 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 2801 | if (!app.killedBackground) { |
| 2802 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 2803 | + ") has died."); |
| 2804 | } |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 2805 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2806 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2807 | TAG, "Dying app: " + app + ", pid: " + pid |
| 2808 | + ", thread: " + thread.asBinder()); |
| 2809 | boolean doLowMem = app.instrumentationClass == null; |
| 2810 | handleAppDiedLocked(app, false); |
| 2811 | |
| 2812 | if (doLowMem) { |
| 2813 | // If there are no longer any background processes running, |
| 2814 | // and the app that died was not running instrumentation, |
| 2815 | // then tell everyone we are now low on memory. |
| 2816 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2817 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2818 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2819 | if (rec.thread != null && rec.setAdj >= HIDDEN_APP_MIN_ADJ) { |
| 2820 | haveBg = true; |
| 2821 | break; |
| 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | if (!haveBg) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2826 | Slog.i(TAG, "Low Memory: No more background processes."); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2827 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2828 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2829 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2830 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 2831 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2832 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 2833 | // The low memory report is overriding any current |
| 2834 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2835 | // heavy/important/visible/foreground processes first. |
| 2836 | if (rec.setAdj <= HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2837 | rec.lastRequestedGc = 0; |
| 2838 | } else { |
| 2839 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2840 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2841 | rec.reportLowMemory = true; |
| 2842 | rec.lastLowMemory = now; |
| 2843 | mProcessesToGc.remove(rec); |
| 2844 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2845 | } |
| 2846 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2847 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2848 | } |
| 2849 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2850 | } else if (app.pid != pid) { |
| 2851 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2852 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2853 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | 28a8c2b | 2010-03-01 11:30:02 -0800 | [diff] [blame] | 2854 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 2855 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2856 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2857 | + thread.asBinder()); |
| 2858 | } |
| 2859 | } |
| 2860 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2861 | /** |
| 2862 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2863 | * @param clearTraces causes the dump file to be erased prior to the new |
| 2864 | * traces being written, if true; when false, the new traces will be |
| 2865 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2866 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 2867 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2868 | * @return file containing stack traces, or null if no dump file is configured |
| 2869 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2870 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
| 2871 | ProcessStats processStats, SparseArray<Boolean> lastPids) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2872 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 2873 | if (tracesPath == null || tracesPath.length() == 0) { |
| 2874 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2875 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2876 | |
| 2877 | File tracesFile = new File(tracesPath); |
| 2878 | try { |
| 2879 | File tracesDir = tracesFile.getParentFile(); |
| 2880 | if (!tracesDir.exists()) tracesFile.mkdirs(); |
| 2881 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 2882 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2883 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2884 | tracesFile.createNewFile(); |
| 2885 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 2886 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2887 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2888 | return null; |
| 2889 | } |
| 2890 | |
| 2891 | // Use a FileObserver to detect when traces finish writing. |
| 2892 | // The order of traces is considered important to maintain for legibility. |
| 2893 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 2894 | public synchronized void onEvent(int event, String path) { notify(); } |
| 2895 | }; |
| 2896 | |
| 2897 | try { |
| 2898 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2899 | |
| 2900 | // First collect all of the stacks of the most important pids. |
| 2901 | try { |
| 2902 | int num = firstPids.size(); |
| 2903 | for (int i = 0; i < num; i++) { |
| 2904 | synchronized (observer) { |
| 2905 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 2906 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 2907 | } |
| 2908 | } |
| 2909 | } catch (InterruptedException e) { |
| 2910 | Log.wtf(TAG, e); |
| 2911 | } |
| 2912 | |
| 2913 | // Next measure CPU usage. |
| 2914 | if (processStats != null) { |
| 2915 | processStats.init(); |
| 2916 | System.gc(); |
| 2917 | processStats.update(); |
| 2918 | try { |
| 2919 | synchronized (processStats) { |
| 2920 | processStats.wait(500); // measure over 1/2 second. |
| 2921 | } |
| 2922 | } catch (InterruptedException e) { |
| 2923 | } |
| 2924 | processStats.update(); |
| 2925 | |
| 2926 | // We'll take the stack crawls of just the top apps using CPU. |
| 2927 | final int N = processStats.countWorkingStats(); |
| 2928 | int numProcs = 0; |
| 2929 | for (int i=0; i<N && numProcs<5; i++) { |
| 2930 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 2931 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 2932 | numProcs++; |
| 2933 | try { |
| 2934 | synchronized (observer) { |
| 2935 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 2936 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 2937 | } |
| 2938 | } catch (InterruptedException e) { |
| 2939 | Log.wtf(TAG, e); |
| 2940 | } |
| 2941 | |
| 2942 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2943 | } |
| 2944 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2945 | |
| 2946 | return tracesFile; |
| 2947 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2948 | } finally { |
| 2949 | observer.stopWatching(); |
| 2950 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2951 | } |
| 2952 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 2953 | private final class AppNotResponding implements Runnable { |
| 2954 | private final ProcessRecord mApp; |
| 2955 | private final String mAnnotation; |
| 2956 | |
| 2957 | public AppNotResponding(ProcessRecord app, String annotation) { |
| 2958 | mApp = app; |
| 2959 | mAnnotation = annotation; |
| 2960 | } |
| 2961 | |
| 2962 | @Override |
| 2963 | public void run() { |
| 2964 | appNotResponding(mApp, null, null, mAnnotation); |
| 2965 | } |
| 2966 | } |
| 2967 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2968 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
| 2969 | ActivityRecord parent, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2970 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 2971 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 2972 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 2973 | if (mController != null) { |
| 2974 | try { |
| 2975 | // 0 == continue, -1 = kill process immediately |
| 2976 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 2977 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 2978 | } catch (RemoteException e) { |
| 2979 | mController = null; |
| 2980 | } |
| 2981 | } |
| 2982 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2983 | long anrTime = SystemClock.uptimeMillis(); |
| 2984 | if (MONITOR_CPU_USAGE) { |
| 2985 | updateCpuStatsNow(); |
| 2986 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2987 | |
| 2988 | synchronized (this) { |
| 2989 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 2990 | if (mShuttingDown) { |
| 2991 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 2992 | return; |
| 2993 | } else if (app.notResponding) { |
| 2994 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 2995 | return; |
| 2996 | } else if (app.crashing) { |
| 2997 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 2998 | return; |
| 2999 | } |
| 3000 | |
| 3001 | // In case we come through here for the same app before completing |
| 3002 | // this one, mark as anring now so we will bail out. |
| 3003 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3004 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3005 | // Log the ANR to the event log. |
| 3006 | EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags, |
| 3007 | annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3008 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3009 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3010 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3011 | |
| 3012 | int parentPid = app.pid; |
| 3013 | 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] | 3014 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3015 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3016 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3017 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3018 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 3019 | ProcessRecord r = mLruProcesses.get(i); |
| 3020 | if (r != null && r.thread != null) { |
| 3021 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3022 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 3023 | if (r.persistent) { |
| 3024 | firstPids.add(pid); |
| 3025 | } else { |
| 3026 | lastPids.put(pid, Boolean.TRUE); |
| 3027 | } |
| 3028 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3029 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3030 | } |
| 3031 | } |
| 3032 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3033 | // Log the ANR to the main log. |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3034 | StringBuilder info = mStringBuilder; |
| 3035 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3036 | info.append("ANR in ").append(app.processName); |
| 3037 | if (activity != null && activity.shortComponentName != null) { |
| 3038 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 3039 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3040 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3041 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3042 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3043 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3044 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3045 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3046 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3047 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3048 | final ProcessStats processStats = new ProcessStats(true); |
| 3049 | |
| 3050 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids); |
| 3051 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3052 | String cpuInfo = null; |
| 3053 | if (MONITOR_CPU_USAGE) { |
| 3054 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3055 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3056 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3057 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3058 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3059 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3060 | } |
| 3061 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3062 | info.append(processStats.printCurrentState(anrTime)); |
| 3063 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3064 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3065 | if (tracesFile == null) { |
| 3066 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 3067 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 3068 | } |
| 3069 | |
| 3070 | addErrorToDropBox("anr", app, activity, parent, annotation, cpuInfo, tracesFile, null); |
| 3071 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3072 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3073 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3074 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 3075 | 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] | 3076 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3077 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3078 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3079 | } |
| 3080 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3081 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3082 | } |
| 3083 | } |
| 3084 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3085 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 3086 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 3087 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3088 | |
| 3089 | synchronized (this) { |
| 3090 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3091 | Slog.w(TAG, "Killing " + app + ": background ANR"); |
| 3092 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 3093 | app.processName, app.setAdj, "background ANR"); |
| 3094 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3095 | return; |
| 3096 | } |
| 3097 | |
| 3098 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 3099 | makeAppNotRespondingLocked(app, |
| 3100 | activity != null ? activity.shortComponentName : null, |
| 3101 | annotation != null ? "ANR " + annotation : "ANR", |
| 3102 | info.toString()); |
| 3103 | |
| 3104 | // Bring up the infamous App Not Responding dialog |
| 3105 | Message msg = Message.obtain(); |
| 3106 | HashMap map = new HashMap(); |
| 3107 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3108 | msg.obj = map; |
| 3109 | map.put("app", app); |
| 3110 | if (activity != null) { |
| 3111 | map.put("activity", activity); |
| 3112 | } |
| 3113 | |
| 3114 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3115 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3116 | } |
| 3117 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3118 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3119 | if (!mLaunchWarningShown) { |
| 3120 | mLaunchWarningShown = true; |
| 3121 | mHandler.post(new Runnable() { |
| 3122 | @Override |
| 3123 | public void run() { |
| 3124 | synchronized (ActivityManagerService.this) { |
| 3125 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3126 | d.show(); |
| 3127 | mHandler.postDelayed(new Runnable() { |
| 3128 | @Override |
| 3129 | public void run() { |
| 3130 | synchronized (ActivityManagerService.this) { |
| 3131 | d.dismiss(); |
| 3132 | mLaunchWarningShown = false; |
| 3133 | } |
| 3134 | } |
| 3135 | }, 4000); |
| 3136 | } |
| 3137 | } |
| 3138 | }); |
| 3139 | } |
| 3140 | } |
| 3141 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3142 | public boolean clearApplicationUserData(final String packageName, |
| 3143 | final IPackageDataObserver observer) { |
| 3144 | int uid = Binder.getCallingUid(); |
| 3145 | int pid = Binder.getCallingPid(); |
| 3146 | long callingId = Binder.clearCallingIdentity(); |
| 3147 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3148 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3149 | int pkgUid = -1; |
| 3150 | synchronized(this) { |
| 3151 | try { |
| 3152 | pkgUid = pm.getPackageUid(packageName); |
| 3153 | } catch (RemoteException e) { |
| 3154 | } |
| 3155 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3156 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3157 | return false; |
| 3158 | } |
| 3159 | if (uid == pkgUid || checkComponentPermission( |
| 3160 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3161 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3162 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3163 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3164 | } else { |
| 3165 | throw new SecurityException(pid+" does not have permission:"+ |
| 3166 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3167 | "for process:"+packageName); |
| 3168 | } |
| 3169 | } |
| 3170 | |
| 3171 | try { |
| 3172 | //clear application user data |
| 3173 | pm.clearApplicationUserData(packageName, observer); |
| 3174 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3175 | Uri.fromParts("package", packageName, null)); |
| 3176 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3177 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
| 3178 | null, null, 0, null, null, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3179 | } catch (RemoteException e) { |
| 3180 | } |
| 3181 | } finally { |
| 3182 | Binder.restoreCallingIdentity(callingId); |
| 3183 | } |
| 3184 | return true; |
| 3185 | } |
| 3186 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3187 | public void killBackgroundProcesses(final String packageName) { |
| 3188 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3189 | != PackageManager.PERMISSION_GRANTED && |
| 3190 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3191 | != PackageManager.PERMISSION_GRANTED) { |
| 3192 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3193 | + Binder.getCallingPid() |
| 3194 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3195 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3196 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3197 | throw new SecurityException(msg); |
| 3198 | } |
| 3199 | |
| 3200 | long callingId = Binder.clearCallingIdentity(); |
| 3201 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3202 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3203 | int pkgUid = -1; |
| 3204 | synchronized(this) { |
| 3205 | try { |
| 3206 | pkgUid = pm.getPackageUid(packageName); |
| 3207 | } catch (RemoteException e) { |
| 3208 | } |
| 3209 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3210 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3211 | return; |
| 3212 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3213 | killPackageProcessesLocked(packageName, pkgUid, |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3214 | SECONDARY_SERVER_ADJ, false, true); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3215 | } |
| 3216 | } finally { |
| 3217 | Binder.restoreCallingIdentity(callingId); |
| 3218 | } |
| 3219 | } |
| 3220 | |
| 3221 | public void forceStopPackage(final String packageName) { |
| 3222 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3223 | != PackageManager.PERMISSION_GRANTED) { |
| 3224 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3225 | + Binder.getCallingPid() |
| 3226 | + ", uid=" + Binder.getCallingUid() |
| 3227 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3228 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3229 | throw new SecurityException(msg); |
| 3230 | } |
| 3231 | |
| 3232 | long callingId = Binder.clearCallingIdentity(); |
| 3233 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3234 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3235 | int pkgUid = -1; |
| 3236 | synchronized(this) { |
| 3237 | try { |
| 3238 | pkgUid = pm.getPackageUid(packageName); |
| 3239 | } catch (RemoteException e) { |
| 3240 | } |
| 3241 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3242 | Slog.w(TAG, "Invalid packageName: " + packageName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3243 | return; |
| 3244 | } |
| 3245 | forceStopPackageLocked(packageName, pkgUid); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3246 | try { |
| 3247 | pm.setPackageStoppedState(packageName, true); |
| 3248 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 3249 | } catch (IllegalArgumentException e) { |
| 3250 | Slog.w(TAG, "Failed trying to unstop package " |
| 3251 | + packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3252 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3253 | } |
| 3254 | } finally { |
| 3255 | Binder.restoreCallingIdentity(callingId); |
| 3256 | } |
| 3257 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3258 | |
| 3259 | /* |
| 3260 | * The pkg name and uid have to be specified. |
| 3261 | * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int) |
| 3262 | */ |
| 3263 | public void killApplicationWithUid(String pkg, int uid) { |
| 3264 | if (pkg == null) { |
| 3265 | return; |
| 3266 | } |
| 3267 | // Make sure the uid is valid. |
| 3268 | if (uid < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3269 | Slog.w(TAG, "Invalid uid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3270 | return; |
| 3271 | } |
| 3272 | int callerUid = Binder.getCallingUid(); |
| 3273 | // Only the system server can kill an application |
| 3274 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3275 | // Post an aysnc message to kill the application |
| 3276 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
| 3277 | msg.arg1 = uid; |
| 3278 | msg.arg2 = 0; |
| 3279 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3280 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3281 | } else { |
| 3282 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3283 | pkg); |
| 3284 | } |
| 3285 | } |
| 3286 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3287 | public void closeSystemDialogs(String reason) { |
| 3288 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3289 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3290 | if (reason != null) { |
| 3291 | intent.putExtra("reason", reason); |
| 3292 | } |
| 3293 | |
| 3294 | final int uid = Binder.getCallingUid(); |
| 3295 | final long origId = Binder.clearCallingIdentity(); |
| 3296 | synchronized (this) { |
| 3297 | int i = mWatchers.beginBroadcast(); |
| 3298 | while (i > 0) { |
| 3299 | i--; |
| 3300 | IActivityWatcher w = mWatchers.getBroadcastItem(i); |
| 3301 | if (w != null) { |
| 3302 | try { |
| 3303 | w.closingSystemDialogs(reason); |
| 3304 | } catch (RemoteException e) { |
| 3305 | } |
| 3306 | } |
| 3307 | } |
| 3308 | mWatchers.finishBroadcast(); |
| 3309 | |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3310 | mWindowManager.closeSystemDialogs(reason); |
| 3311 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3312 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 3313 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3314 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3315 | r.stack.finishActivityLocked(r, i, |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3316 | Activity.RESULT_CANCELED, null, "close-sys"); |
| 3317 | } |
| 3318 | } |
| 3319 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3320 | broadcastIntentLocked(null, null, intent, null, |
| 3321 | null, 0, null, null, null, false, false, -1, uid); |
| 3322 | } |
| 3323 | Binder.restoreCallingIdentity(origId); |
| 3324 | } |
| 3325 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3326 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3327 | throws RemoteException { |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3328 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3329 | for (int i=pids.length-1; i>=0; i--) { |
| 3330 | infos[i] = new Debug.MemoryInfo(); |
| 3331 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3332 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3333 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3334 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3335 | |
| 3336 | public void killApplicationProcess(String processName, int uid) { |
| 3337 | if (processName == null) { |
| 3338 | return; |
| 3339 | } |
| 3340 | |
| 3341 | int callerUid = Binder.getCallingUid(); |
| 3342 | // Only the system server can kill an application |
| 3343 | if (callerUid == Process.SYSTEM_UID) { |
| 3344 | synchronized (this) { |
| 3345 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3346 | if (app != null && app.thread != null) { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3347 | try { |
| 3348 | app.thread.scheduleSuicide(); |
| 3349 | } catch (RemoteException e) { |
| 3350 | // If the other end already died, then our work here is done. |
| 3351 | } |
| 3352 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3353 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3354 | + processName + " / " + uid); |
| 3355 | } |
| 3356 | } |
| 3357 | } else { |
| 3358 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3359 | processName); |
| 3360 | } |
| 3361 | } |
| 3362 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3363 | private void forceStopPackageLocked(final String packageName, int uid) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3364 | forceStopPackageLocked(packageName, uid, false, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3365 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3366 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3367 | if (!mProcessesReady) { |
| 3368 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3369 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3370 | intent.putExtra(Intent.EXTRA_UID, uid); |
| 3371 | broadcastIntentLocked(null, null, intent, |
| 3372 | null, null, 0, null, null, null, |
| 3373 | false, false, MY_PID, Process.SYSTEM_UID); |
| 3374 | } |
| 3375 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3376 | private final boolean killPackageProcessesLocked(String packageName, int uid, |
| 3377 | int minOomAdj, boolean callerWillRestart, boolean doit) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3378 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3379 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3380 | // Remove all processes this package may have touched: all with the |
| 3381 | // same UID (except for the system or root user), and all whose name |
| 3382 | // matches the package name. |
| 3383 | final String procNamePrefix = packageName + ":"; |
| 3384 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3385 | final int NA = apps.size(); |
| 3386 | for (int ia=0; ia<NA; ia++) { |
| 3387 | ProcessRecord app = apps.valueAt(ia); |
| 3388 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3389 | if (doit) { |
| 3390 | procs.add(app); |
| 3391 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3392 | } else if ((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid) |
| 3393 | || app.processName.equals(packageName) |
| 3394 | || app.processName.startsWith(procNamePrefix)) { |
| 3395 | if (app.setAdj >= minOomAdj) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3396 | if (!doit) { |
| 3397 | return true; |
| 3398 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3399 | app.removed = true; |
| 3400 | procs.add(app); |
| 3401 | } |
| 3402 | } |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | int N = procs.size(); |
| 3407 | for (int i=0; i<N; i++) { |
| 3408 | removeProcessLocked(procs.get(i), callerWillRestart); |
| 3409 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3410 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3411 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3412 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3413 | private final boolean forceStopPackageLocked(String name, int uid, |
| 3414 | boolean callerWillRestart, boolean purgeCache, boolean doit) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3415 | int i; |
| 3416 | int N; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3417 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3418 | if (uid < 0) { |
| 3419 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3420 | uid = AppGlobals.getPackageManager().getPackageUid(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3421 | } catch (RemoteException e) { |
| 3422 | } |
| 3423 | } |
| 3424 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3425 | if (doit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3426 | Slog.i(TAG, "Force stopping package " + name + " uid=" + uid); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3427 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3428 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3429 | while (badApps.hasNext()) { |
| 3430 | SparseArray<Long> ba = badApps.next(); |
| 3431 | if (ba.get(uid) != null) { |
| 3432 | badApps.remove(); |
| 3433 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3434 | } |
| 3435 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3436 | |
| 3437 | boolean didSomething = killPackageProcessesLocked(name, uid, -100, |
| 3438 | callerWillRestart, doit); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3439 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3440 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 3441 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3442 | if (r.packageName.equals(name)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3443 | if (!doit) { |
| 3444 | return true; |
| 3445 | } |
| 3446 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3447 | Slog.i(TAG, " Force finishing activity " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3448 | if (r.app != null) { |
| 3449 | r.app.removed = true; |
| 3450 | } |
| 3451 | r.app = null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3452 | r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, null, "uninstall"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3453 | } |
| 3454 | } |
| 3455 | |
| 3456 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
| 3457 | for (ServiceRecord service : mServices.values()) { |
| 3458 | if (service.packageName.equals(name)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3459 | if (!doit) { |
| 3460 | return true; |
| 3461 | } |
| 3462 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3463 | Slog.i(TAG, " Force stopping service " + service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3464 | if (service.app != null) { |
| 3465 | service.app.removed = true; |
| 3466 | } |
| 3467 | service.app = null; |
| 3468 | services.add(service); |
| 3469 | } |
| 3470 | } |
| 3471 | |
| 3472 | N = services.size(); |
| 3473 | for (i=0; i<N; i++) { |
| 3474 | bringDownServiceLocked(services.get(i), true); |
| 3475 | } |
| 3476 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3477 | if (doit) { |
| 3478 | if (purgeCache) { |
| 3479 | AttributeCache ac = AttributeCache.instance(); |
| 3480 | if (ac != null) { |
| 3481 | ac.removePackage(name); |
| 3482 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3483 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3484 | mMainStack.resumeTopActivityLocked(null); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3485 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3486 | |
| 3487 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | private final boolean removeProcessLocked(ProcessRecord app, boolean callerWillRestart) { |
| 3491 | final String name = app.processName; |
| 3492 | final int uid = app.info.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3493 | if (DEBUG_PROCESSES) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3494 | TAG, "Force removing process " + app + " (" + name |
| 3495 | + "/" + uid + ")"); |
| 3496 | |
| 3497 | mProcessNames.remove(name, uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3498 | if (mHeavyWeightProcess == app) { |
| 3499 | mHeavyWeightProcess = null; |
| 3500 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3501 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3502 | boolean needRestart = false; |
| 3503 | if (app.pid > 0 && app.pid != MY_PID) { |
| 3504 | int pid = app.pid; |
| 3505 | synchronized (mPidsSelfLocked) { |
| 3506 | mPidsSelfLocked.remove(pid); |
| 3507 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3508 | } |
| 3509 | handleAppDiedLocked(app, true); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3510 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3511 | Process.killProcess(pid); |
| 3512 | |
| 3513 | if (app.persistent) { |
| 3514 | if (!callerWillRestart) { |
| 3515 | addAppLocked(app.info); |
| 3516 | } else { |
| 3517 | needRestart = true; |
| 3518 | } |
| 3519 | } |
| 3520 | } else { |
| 3521 | mRemovedProcesses.add(app); |
| 3522 | } |
| 3523 | |
| 3524 | return needRestart; |
| 3525 | } |
| 3526 | |
| 3527 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 3528 | final int pid = app.pid; |
| 3529 | boolean gone = false; |
| 3530 | synchronized (mPidsSelfLocked) { |
| 3531 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 3532 | if (knownApp != null && knownApp.thread == null) { |
| 3533 | mPidsSelfLocked.remove(pid); |
| 3534 | gone = true; |
| 3535 | } |
| 3536 | } |
| 3537 | |
| 3538 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3539 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3540 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.info.uid, |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3541 | app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3542 | mProcessNames.remove(app.processName, app.info.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3543 | if (mHeavyWeightProcess == app) { |
| 3544 | mHeavyWeightProcess = null; |
| 3545 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3546 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3547 | // Take care of any launching providers waiting for this process. |
| 3548 | checkAppInLaunchingProvidersLocked(app, true); |
| 3549 | // Take care of any services that are waiting for the process. |
| 3550 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3551 | ServiceRecord sr = mPendingServices.get(i); |
| 3552 | if (app.info.uid == sr.appInfo.uid |
| 3553 | && app.processName.equals(sr.processName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3554 | Slog.w(TAG, "Forcing bringing down service: " + sr); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3555 | mPendingServices.remove(i); |
| 3556 | i--; |
| 3557 | bringDownServiceLocked(sr, true); |
| 3558 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3559 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3560 | EventLog.writeEvent(EventLogTags.AM_KILL, pid, |
| 3561 | app.processName, app.setAdj, "start timeout"); |
| 3562 | Process.killProcessQuiet(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3563 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3564 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3565 | try { |
| 3566 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 3567 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 3568 | bm.agentDisconnected(app.info.packageName); |
| 3569 | } catch (RemoteException e) { |
| 3570 | // Can't happen; the backup manager is local |
| 3571 | } |
| 3572 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3573 | if (mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3574 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3575 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 3576 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3577 | mPendingBroadcast = null; |
| 3578 | scheduleBroadcastsLocked(); |
| 3579 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3580 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3581 | 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] | 3582 | } |
| 3583 | } |
| 3584 | |
| 3585 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 3586 | int pid) { |
| 3587 | |
| 3588 | // Find the application record that is being attached... either via |
| 3589 | // the pid if we are running in multiple processes, or just pull the |
| 3590 | // next app record if we are emulating process with anonymous threads. |
| 3591 | ProcessRecord app; |
| 3592 | if (pid != MY_PID && pid >= 0) { |
| 3593 | synchronized (mPidsSelfLocked) { |
| 3594 | app = mPidsSelfLocked.get(pid); |
| 3595 | } |
| 3596 | } else if (mStartingProcesses.size() > 0) { |
| 3597 | app = mStartingProcesses.remove(0); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3598 | app.setPid(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3599 | } else { |
| 3600 | app = null; |
| 3601 | } |
| 3602 | |
| 3603 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3604 | 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] | 3605 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3606 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3607 | if (pid > 0 && pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3608 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3609 | } else { |
| 3610 | try { |
| 3611 | thread.scheduleExit(); |
| 3612 | } catch (Exception e) { |
| 3613 | // Ignore exceptions. |
| 3614 | } |
| 3615 | } |
| 3616 | return false; |
| 3617 | } |
| 3618 | |
| 3619 | // If this application record is still attached to a previous |
| 3620 | // process, clean it up now. |
| 3621 | if (app.thread != null) { |
| 3622 | handleAppDiedLocked(app, true); |
| 3623 | } |
| 3624 | |
| 3625 | // Tell the process all about itself. |
| 3626 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3627 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3628 | TAG, "Binding process pid " + pid + " to record " + app); |
| 3629 | |
| 3630 | String processName = app.processName; |
| 3631 | try { |
| 3632 | thread.asBinder().linkToDeath(new AppDeathRecipient( |
| 3633 | app, pid, thread), 0); |
| 3634 | } catch (RemoteException e) { |
| 3635 | app.resetPackageList(); |
| 3636 | startProcessLocked(app, "link fail", processName); |
| 3637 | return false; |
| 3638 | } |
| 3639 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3640 | 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] | 3641 | |
| 3642 | app.thread = thread; |
| 3643 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 3644 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 3645 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3646 | app.forcingToForeground = null; |
| 3647 | app.foregroundServices = false; |
| 3648 | app.debugging = false; |
| 3649 | |
| 3650 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3651 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3652 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3653 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3654 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3655 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3656 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3657 | } |
| 3658 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3659 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3660 | TAG, "New app record " + app |
| 3661 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 3662 | try { |
| 3663 | int testMode = IApplicationThread.DEBUG_OFF; |
| 3664 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 3665 | testMode = mWaitForDebugger |
| 3666 | ? IApplicationThread.DEBUG_WAIT |
| 3667 | : IApplicationThread.DEBUG_ON; |
| 3668 | app.debugging = true; |
| 3669 | if (mDebugTransient) { |
| 3670 | mDebugApp = mOrigDebugApp; |
| 3671 | mWaitForDebugger = mOrigWaitForDebugger; |
| 3672 | } |
| 3673 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3674 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3675 | // If the app is being launched for restore or full backup, set it up specially |
| 3676 | boolean isRestrictedBackupMode = false; |
| 3677 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 3678 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3679 | || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3680 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 3681 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3682 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 3683 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 3684 | ? app.instrumentationInfo.packageName |
| 3685 | : app.info.packageName); |
| 3686 | if (app.instrumentationClass != null) { |
| 3687 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3688 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3689 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 3690 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3691 | ApplicationInfo appInfo = app.instrumentationInfo != null |
| 3692 | ? app.instrumentationInfo : app.info; |
| 3693 | thread.bindApplication(processName, appInfo, providers, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3694 | app.instrumentationClass, app.instrumentationProfileFile, |
| 3695 | app.instrumentationArguments, app.instrumentationWatcher, testMode, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3696 | isRestrictedBackupMode || !normalMode, |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3697 | mConfiguration, compatibilityInfoForPackageLocked(appInfo), |
| 3698 | getCommonServicesLocked(), |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 3699 | mCoreSettingsObserver.getCoreSettingsLocked()); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3700 | updateLruProcessLocked(app, false, true); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3701 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3702 | } catch (Exception e) { |
| 3703 | // todo: Yikes! What should we do? For now we will try to |
| 3704 | // start another process, but that could easily get us in |
| 3705 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3706 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3707 | |
| 3708 | app.resetPackageList(); |
| 3709 | startProcessLocked(app, "bind fail", processName); |
| 3710 | return false; |
| 3711 | } |
| 3712 | |
| 3713 | // Remove this record from the list of starting applications. |
| 3714 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3715 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 3716 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3717 | mProcessesOnHold.remove(app); |
| 3718 | |
| 3719 | boolean badApp = false; |
| 3720 | boolean didSomething = false; |
| 3721 | |
| 3722 | // 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] | 3723 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 3724 | if (hr != null && normalMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3725 | if (hr.app == null && app.info.uid == hr.info.applicationInfo.uid |
| 3726 | && processName.equals(hr.processName)) { |
| 3727 | try { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3728 | if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3729 | didSomething = true; |
| 3730 | } |
| 3731 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3732 | 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] | 3733 | + hr.intent.getComponent().flattenToShortString(), e); |
| 3734 | badApp = true; |
| 3735 | } |
| 3736 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3737 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3738 | } |
| 3739 | } |
| 3740 | |
| 3741 | // Find any services that should be running in this process... |
| 3742 | if (!badApp && mPendingServices.size() > 0) { |
| 3743 | ServiceRecord sr = null; |
| 3744 | try { |
| 3745 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3746 | sr = mPendingServices.get(i); |
| 3747 | if (app.info.uid != sr.appInfo.uid |
| 3748 | || !processName.equals(sr.processName)) { |
| 3749 | continue; |
| 3750 | } |
| 3751 | |
| 3752 | mPendingServices.remove(i); |
| 3753 | i--; |
| 3754 | realStartServiceLocked(sr, app); |
| 3755 | didSomething = true; |
| 3756 | } |
| 3757 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3758 | 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] | 3759 | + sr.shortName, e); |
| 3760 | badApp = true; |
| 3761 | } |
| 3762 | } |
| 3763 | |
| 3764 | // Check if the next broadcast receiver is in this process... |
| 3765 | BroadcastRecord br = mPendingBroadcast; |
| 3766 | if (!badApp && br != null && br.curApp == app) { |
| 3767 | try { |
| 3768 | mPendingBroadcast = null; |
| 3769 | processCurBroadcastLocked(br, app); |
| 3770 | didSomething = true; |
| 3771 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3772 | 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] | 3773 | + br.curComponent.flattenToShortString(), e); |
| 3774 | badApp = true; |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 3775 | logBroadcastReceiverDiscardLocked(br); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3776 | finishReceiverLocked(br.receiver, br.resultCode, br.resultData, |
| 3777 | br.resultExtras, br.resultAbort, true); |
| 3778 | scheduleBroadcastsLocked(); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3779 | // We need to reset the state if we fails to start the receiver. |
| 3780 | br.state = BroadcastRecord.IDLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3781 | } |
| 3782 | } |
| 3783 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3784 | // Check whether the next backup agent is in this process... |
| 3785 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.info.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3786 | 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] | 3787 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3788 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3789 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, |
| 3790 | compatibilityInfoForPackageLocked(mBackupTarget.appInfo), |
| 3791 | mBackupTarget.backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3792 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3793 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3794 | e.printStackTrace(); |
| 3795 | } |
| 3796 | } |
| 3797 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3798 | if (badApp) { |
| 3799 | // todo: Also need to kill application to deal with all |
| 3800 | // kinds of exceptions. |
| 3801 | handleAppDiedLocked(app, false); |
| 3802 | return false; |
| 3803 | } |
| 3804 | |
| 3805 | if (!didSomething) { |
| 3806 | updateOomAdjLocked(); |
| 3807 | } |
| 3808 | |
| 3809 | return true; |
| 3810 | } |
| 3811 | |
| 3812 | public final void attachApplication(IApplicationThread thread) { |
| 3813 | synchronized (this) { |
| 3814 | int callingPid = Binder.getCallingPid(); |
| 3815 | final long origId = Binder.clearCallingIdentity(); |
| 3816 | attachApplicationLocked(thread, callingPid); |
| 3817 | Binder.restoreCallingIdentity(origId); |
| 3818 | } |
| 3819 | } |
| 3820 | |
Dianne Hackborn | e88846e | 2009-09-30 21:34:25 -0700 | [diff] [blame] | 3821 | public final void activityIdle(IBinder token, Configuration config) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3822 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3823 | mMainStack.activityIdleInternal(token, false, config); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3824 | Binder.restoreCallingIdentity(origId); |
| 3825 | } |
| 3826 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3827 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3828 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3829 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3830 | mWindowManager.enableScreenAfterBoot(); |
| 3831 | } |
| 3832 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3833 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3834 | IntentFilter pkgFilter = new IntentFilter(); |
| 3835 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 3836 | pkgFilter.addDataScheme("package"); |
| 3837 | mContext.registerReceiver(new BroadcastReceiver() { |
| 3838 | @Override |
| 3839 | public void onReceive(Context context, Intent intent) { |
| 3840 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 3841 | if (pkgs != null) { |
| 3842 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 3843 | synchronized (ActivityManagerService.this) { |
| 3844 | if (forceStopPackageLocked(pkg, -1, false, false, false)) { |
| 3845 | setResultCode(Activity.RESULT_OK); |
| 3846 | return; |
| 3847 | } |
| 3848 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3849 | } |
| 3850 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3851 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3852 | }, pkgFilter); |
| 3853 | |
| 3854 | synchronized (this) { |
| 3855 | // Ensure that any processes we had put on hold are now started |
| 3856 | // up. |
| 3857 | final int NP = mProcessesOnHold.size(); |
| 3858 | if (NP > 0) { |
| 3859 | ArrayList<ProcessRecord> procs = |
| 3860 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 3861 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3862 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 3863 | + procs.get(ip)); |
| 3864 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3865 | } |
| 3866 | } |
| 3867 | |
| 3868 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3869 | // Start looking for apps that are abusing wake locks. |
| 3870 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3871 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3872 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 3873 | SystemProperties.set("sys.boot_completed", "1"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3874 | broadcastIntentLocked(null, null, |
| 3875 | new Intent(Intent.ACTION_BOOT_COMPLETED, null), |
| 3876 | null, null, 0, null, null, |
| 3877 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
| 3878 | false, false, MY_PID, Process.SYSTEM_UID); |
| 3879 | } |
| 3880 | } |
| 3881 | } |
| 3882 | |
| 3883 | final void ensureBootCompleted() { |
| 3884 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 3885 | boolean enableScreen; |
| 3886 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3887 | booting = mBooting; |
| 3888 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 3889 | enableScreen = !mBooted; |
| 3890 | mBooted = true; |
| 3891 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3892 | |
| 3893 | if (booting) { |
| 3894 | finishBooting(); |
| 3895 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 3896 | |
| 3897 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 3898 | enableScreenAfterBoot(); |
| 3899 | } |
| 3900 | } |
| 3901 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 3902 | public final void activityPaused(IBinder token) { |
| 3903 | final long origId = Binder.clearCallingIdentity(); |
| 3904 | mMainStack.activityPaused(token, false); |
| 3905 | Binder.restoreCallingIdentity(origId); |
| 3906 | } |
| 3907 | |
| 3908 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 3909 | CharSequence description) { |
| 3910 | if (localLOGV) Slog.v( |
| 3911 | TAG, "Activity stopped: token=" + token); |
| 3912 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3913 | // Refuse possible leaked file descriptors |
| 3914 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 3915 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 3916 | } |
| 3917 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3918 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3919 | |
| 3920 | final long origId = Binder.clearCallingIdentity(); |
| 3921 | |
| 3922 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3923 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3924 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3925 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 3926 | r.icicle = icicle; |
| 3927 | r.haveState = true; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 3928 | r.updateThumbnail(thumbnail, description); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3929 | r.stopped = true; |
| 3930 | r.state = ActivityState.STOPPED; |
| 3931 | if (!r.finishing) { |
| 3932 | if (r.configDestroy) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3933 | r.stack.destroyActivityLocked(r, true); |
| 3934 | r.stack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3935 | } |
| 3936 | } |
| 3937 | } |
| 3938 | } |
| 3939 | |
| 3940 | if (r != null) { |
| 3941 | sendPendingThumbnail(r, null, null, null, false); |
| 3942 | } |
| 3943 | |
| 3944 | trimApplications(); |
| 3945 | |
| 3946 | Binder.restoreCallingIdentity(origId); |
| 3947 | } |
| 3948 | |
| 3949 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3950 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3951 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3952 | } |
| 3953 | |
| 3954 | public String getCallingPackage(IBinder token) { |
| 3955 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3956 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 3957 | 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] | 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | public ComponentName getCallingActivity(IBinder token) { |
| 3962 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3963 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3964 | return r != null ? r.intent.getComponent() : null; |
| 3965 | } |
| 3966 | } |
| 3967 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3968 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3969 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3970 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3971 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3972 | if (r != null) { |
| 3973 | return r.resultTo; |
| 3974 | } |
| 3975 | } |
| 3976 | return null; |
| 3977 | } |
| 3978 | |
| 3979 | public ComponentName getActivityClassForToken(IBinder token) { |
| 3980 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3981 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3982 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3983 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3984 | return r.intent.getComponent(); |
| 3985 | } |
| 3986 | return null; |
| 3987 | } |
| 3988 | } |
| 3989 | |
| 3990 | public String getPackageForToken(IBinder token) { |
| 3991 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3992 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3993 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3994 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3995 | return r.packageName; |
| 3996 | } |
| 3997 | return null; |
| 3998 | } |
| 3999 | } |
| 4000 | |
| 4001 | public IIntentSender getIntentSender(int type, |
| 4002 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4003 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4004 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4005 | if (intents != null) { |
| 4006 | if (intents.length < 1) { |
| 4007 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 4008 | } |
| 4009 | for (int i=0; i<intents.length; i++) { |
| 4010 | Intent intent = intents[i]; |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4011 | if (intent != null) { |
| 4012 | if (intent.hasFileDescriptors()) { |
| 4013 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 4014 | } |
| 4015 | if (type == INTENT_SENDER_BROADCAST && |
| 4016 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 4017 | throw new IllegalArgumentException( |
| 4018 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 4019 | } |
| 4020 | intents[i] = new Intent(intent); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4021 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4022 | } |
| 4023 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4024 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4025 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4026 | } |
| 4027 | } |
| 4028 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4029 | synchronized(this) { |
| 4030 | int callingUid = Binder.getCallingUid(); |
| 4031 | try { |
| 4032 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID && |
| 4033 | Process.supportsProcesses()) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4034 | int uid = AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4035 | .getPackageUid(packageName); |
| 4036 | if (uid != Binder.getCallingUid()) { |
| 4037 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 4038 | + Binder.getCallingPid() |
| 4039 | + ", uid=" + Binder.getCallingUid() |
| 4040 | + ", (need uid=" + uid + ")" |
| 4041 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4042 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4043 | throw new SecurityException(msg); |
| 4044 | } |
| 4045 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4046 | |
| 4047 | return getIntentSenderLocked(type, packageName, callingUid, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4048 | token, resultWho, requestCode, intents, resolvedTypes, flags); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4049 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4050 | } catch (RemoteException e) { |
| 4051 | throw new SecurityException(e); |
| 4052 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4053 | } |
| 4054 | } |
| 4055 | |
| 4056 | IIntentSender getIntentSenderLocked(int type, |
| 4057 | String packageName, int callingUid, IBinder token, String resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4058 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4059 | ActivityRecord activity = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4060 | if (type == INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4061 | int index = mMainStack.indexOfTokenLocked(token); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4062 | if (index < 0) { |
| 4063 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4064 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4065 | activity = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4066 | if (activity.finishing) { |
| 4067 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4068 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4069 | } |
| 4070 | |
| 4071 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 4072 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 4073 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 4074 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 4075 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 4076 | |
| 4077 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 4078 | type, packageName, activity, resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4079 | requestCode, intents, resolvedTypes, flags); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4080 | WeakReference<PendingIntentRecord> ref; |
| 4081 | ref = mIntentSenderRecords.get(key); |
| 4082 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 4083 | if (rec != null) { |
| 4084 | if (!cancelCurrent) { |
| 4085 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4086 | if (rec.key.requestIntent != null) { |
| 4087 | rec.key.requestIntent.replaceExtras(intents != null ? intents[0] : null); |
| 4088 | } |
| 4089 | if (intents != null) { |
| 4090 | intents[intents.length-1] = rec.key.requestIntent; |
| 4091 | rec.key.allIntents = intents; |
| 4092 | rec.key.allResolvedTypes = resolvedTypes; |
| 4093 | } else { |
| 4094 | rec.key.allIntents = null; |
| 4095 | rec.key.allResolvedTypes = null; |
| 4096 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4097 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4098 | return rec; |
| 4099 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4100 | rec.canceled = true; |
| 4101 | mIntentSenderRecords.remove(key); |
| 4102 | } |
| 4103 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4104 | return rec; |
| 4105 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4106 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4107 | mIntentSenderRecords.put(key, rec.ref); |
| 4108 | if (type == INTENT_SENDER_ACTIVITY_RESULT) { |
| 4109 | if (activity.pendingResults == null) { |
| 4110 | activity.pendingResults |
| 4111 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4112 | } |
| 4113 | activity.pendingResults.add(rec.ref); |
| 4114 | } |
| 4115 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | public void cancelIntentSender(IIntentSender sender) { |
| 4119 | if (!(sender instanceof PendingIntentRecord)) { |
| 4120 | return; |
| 4121 | } |
| 4122 | synchronized(this) { |
| 4123 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4124 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4125 | int uid = AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4126 | .getPackageUid(rec.key.packageName); |
| 4127 | if (uid != Binder.getCallingUid()) { |
| 4128 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4129 | + Binder.getCallingPid() |
| 4130 | + ", uid=" + Binder.getCallingUid() |
| 4131 | + " is not allowed to cancel packges " |
| 4132 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4133 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4134 | throw new SecurityException(msg); |
| 4135 | } |
| 4136 | } catch (RemoteException e) { |
| 4137 | throw new SecurityException(e); |
| 4138 | } |
| 4139 | cancelIntentSenderLocked(rec, true); |
| 4140 | } |
| 4141 | } |
| 4142 | |
| 4143 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4144 | rec.canceled = true; |
| 4145 | mIntentSenderRecords.remove(rec.key); |
| 4146 | if (cleanActivity && rec.key.activity != null) { |
| 4147 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4148 | } |
| 4149 | } |
| 4150 | |
| 4151 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4152 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4153 | return null; |
| 4154 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4155 | try { |
| 4156 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4157 | return res.key.packageName; |
| 4158 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4159 | } |
| 4160 | return null; |
| 4161 | } |
| 4162 | |
| 4163 | public void setProcessLimit(int max) { |
| 4164 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4165 | "setProcessLimit()"); |
| 4166 | mProcessLimit = max; |
| 4167 | } |
| 4168 | |
| 4169 | public int getProcessLimit() { |
| 4170 | return mProcessLimit; |
| 4171 | } |
| 4172 | |
| 4173 | void foregroundTokenDied(ForegroundToken token) { |
| 4174 | synchronized (ActivityManagerService.this) { |
| 4175 | synchronized (mPidsSelfLocked) { |
| 4176 | ForegroundToken cur |
| 4177 | = mForegroundProcesses.get(token.pid); |
| 4178 | if (cur != token) { |
| 4179 | return; |
| 4180 | } |
| 4181 | mForegroundProcesses.remove(token.pid); |
| 4182 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4183 | if (pr == null) { |
| 4184 | return; |
| 4185 | } |
| 4186 | pr.forcingToForeground = null; |
| 4187 | pr.foregroundServices = false; |
| 4188 | } |
| 4189 | updateOomAdjLocked(); |
| 4190 | } |
| 4191 | } |
| 4192 | |
| 4193 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4194 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4195 | "setProcessForeground()"); |
| 4196 | synchronized(this) { |
| 4197 | boolean changed = false; |
| 4198 | |
| 4199 | synchronized (mPidsSelfLocked) { |
| 4200 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
| 4201 | if (pr == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4202 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4203 | return; |
| 4204 | } |
| 4205 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4206 | if (oldToken != null) { |
| 4207 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4208 | mForegroundProcesses.remove(pid); |
| 4209 | pr.forcingToForeground = null; |
| 4210 | changed = true; |
| 4211 | } |
| 4212 | if (isForeground && token != null) { |
| 4213 | ForegroundToken newToken = new ForegroundToken() { |
| 4214 | public void binderDied() { |
| 4215 | foregroundTokenDied(this); |
| 4216 | } |
| 4217 | }; |
| 4218 | newToken.pid = pid; |
| 4219 | newToken.token = token; |
| 4220 | try { |
| 4221 | token.linkToDeath(newToken, 0); |
| 4222 | mForegroundProcesses.put(pid, newToken); |
| 4223 | pr.forcingToForeground = token; |
| 4224 | changed = true; |
| 4225 | } catch (RemoteException e) { |
| 4226 | // If the process died while doing this, we will later |
| 4227 | // do the cleanup with the process death link. |
| 4228 | } |
| 4229 | } |
| 4230 | } |
| 4231 | |
| 4232 | if (changed) { |
| 4233 | updateOomAdjLocked(); |
| 4234 | } |
| 4235 | } |
| 4236 | } |
| 4237 | |
| 4238 | // ========================================================= |
| 4239 | // PERMISSIONS |
| 4240 | // ========================================================= |
| 4241 | |
| 4242 | static class PermissionController extends IPermissionController.Stub { |
| 4243 | ActivityManagerService mActivityManagerService; |
| 4244 | PermissionController(ActivityManagerService activityManagerService) { |
| 4245 | mActivityManagerService = activityManagerService; |
| 4246 | } |
| 4247 | |
| 4248 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4249 | return mActivityManagerService.checkPermission(permission, pid, |
| 4250 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4251 | } |
| 4252 | } |
| 4253 | |
| 4254 | /** |
| 4255 | * This can be called with or without the global lock held. |
| 4256 | */ |
| 4257 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4258 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4259 | // We might be performing an operation on behalf of an indirect binder |
| 4260 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4261 | // client identity accordingly before proceeding. |
| 4262 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4263 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4264 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4265 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4266 | uid = tlsIdentity.uid; |
| 4267 | pid = tlsIdentity.pid; |
| 4268 | } |
| 4269 | |
| 4270 | // Root, system server and our own process get to do everything. |
| 4271 | if (uid == 0 || uid == Process.SYSTEM_UID || pid == MY_PID || |
| 4272 | !Process.supportsProcesses()) { |
| 4273 | return PackageManager.PERMISSION_GRANTED; |
| 4274 | } |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4275 | // If there is a uid that owns whatever is being accessed, it has |
| 4276 | // blanket access to it regardless of the permissions it requires. |
| 4277 | if (owningUid >= 0 && uid == owningUid) { |
| 4278 | return PackageManager.PERMISSION_GRANTED; |
| 4279 | } |
| 4280 | // If the target is not exported, then nobody else can get to it. |
| 4281 | if (!exported) { |
| 4282 | Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4283 | return PackageManager.PERMISSION_DENIED; |
| 4284 | } |
| 4285 | if (permission == null) { |
| 4286 | return PackageManager.PERMISSION_GRANTED; |
| 4287 | } |
| 4288 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4289 | return AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4290 | .checkUidPermission(permission, uid); |
| 4291 | } catch (RemoteException e) { |
| 4292 | // Should never happen, but if it does... deny! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4293 | Slog.e(TAG, "PackageManager is dead?!?", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4294 | } |
| 4295 | return PackageManager.PERMISSION_DENIED; |
| 4296 | } |
| 4297 | |
| 4298 | /** |
| 4299 | * As the only public entry point for permissions checking, this method |
| 4300 | * can enforce the semantic that requesting a check on a null global |
| 4301 | * permission is automatically denied. (Internally a null permission |
| 4302 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4303 | * when only uid-based security is needed.) |
| 4304 | * |
| 4305 | * This can be called with or without the global lock held. |
| 4306 | */ |
| 4307 | public int checkPermission(String permission, int pid, int uid) { |
| 4308 | if (permission == null) { |
| 4309 | return PackageManager.PERMISSION_DENIED; |
| 4310 | } |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4311 | return checkComponentPermission(permission, pid, uid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4312 | } |
| 4313 | |
| 4314 | /** |
| 4315 | * Binder IPC calls go through the public entry point. |
| 4316 | * This can be called with or without the global lock held. |
| 4317 | */ |
| 4318 | int checkCallingPermission(String permission) { |
| 4319 | return checkPermission(permission, |
| 4320 | Binder.getCallingPid(), |
| 4321 | Binder.getCallingUid()); |
| 4322 | } |
| 4323 | |
| 4324 | /** |
| 4325 | * This can be called with or without the global lock held. |
| 4326 | */ |
| 4327 | void enforceCallingPermission(String permission, String func) { |
| 4328 | if (checkCallingPermission(permission) |
| 4329 | == PackageManager.PERMISSION_GRANTED) { |
| 4330 | return; |
| 4331 | } |
| 4332 | |
| 4333 | String msg = "Permission Denial: " + func + " from pid=" |
| 4334 | + Binder.getCallingPid() |
| 4335 | + ", uid=" + Binder.getCallingUid() |
| 4336 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4337 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4338 | throw new SecurityException(msg); |
| 4339 | } |
| 4340 | |
| 4341 | private final boolean checkHoldingPermissionsLocked(IPackageManager pm, |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4342 | ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
| 4343 | boolean readPerm = (modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 4344 | boolean writePerm = (modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0; |
| 4345 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4346 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4347 | try { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4348 | // Is the component private from the target uid? |
| 4349 | final boolean prv = !pi.exported && pi.applicationInfo.uid != uid; |
| 4350 | |
| 4351 | // Acceptable if the there is no read permission needed from the |
| 4352 | // target or the target is holding the read permission. |
| 4353 | if (!readPerm) { |
| 4354 | if ((!prv && pi.readPermission == null) || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4355 | (pm.checkUidPermission(pi.readPermission, uid) |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4356 | == PackageManager.PERMISSION_GRANTED)) { |
| 4357 | readPerm = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4358 | } |
| 4359 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4360 | |
| 4361 | // Acceptable if the there is no write permission needed from the |
| 4362 | // target or the target is holding the read permission. |
| 4363 | if (!writePerm) { |
| 4364 | if (!prv && (pi.writePermission == null) || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4365 | (pm.checkUidPermission(pi.writePermission, uid) |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4366 | == PackageManager.PERMISSION_GRANTED)) { |
| 4367 | writePerm = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4368 | } |
| 4369 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4370 | |
| 4371 | // Acceptable if there is a path permission matching the URI that |
| 4372 | // the target holds the permission on. |
| 4373 | PathPermission[] pps = pi.pathPermissions; |
| 4374 | if (pps != null && (!readPerm || !writePerm)) { |
| 4375 | final String path = uri.getPath(); |
| 4376 | int i = pps.length; |
| 4377 | while (i > 0 && (!readPerm || !writePerm)) { |
| 4378 | i--; |
| 4379 | PathPermission pp = pps[i]; |
| 4380 | if (!readPerm) { |
| 4381 | final String pprperm = pp.getReadPermission(); |
| 4382 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 4383 | + pprperm + " for " + pp.getPath() |
| 4384 | + ": match=" + pp.match(path) |
| 4385 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 4386 | if (pprperm != null && pp.match(path) && |
| 4387 | (pm.checkUidPermission(pprperm, uid) |
| 4388 | == PackageManager.PERMISSION_GRANTED)) { |
| 4389 | readPerm = true; |
| 4390 | } |
| 4391 | } |
| 4392 | if (!writePerm) { |
| 4393 | final String ppwperm = pp.getWritePermission(); |
| 4394 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 4395 | + ppwperm + " for " + pp.getPath() |
| 4396 | + ": match=" + pp.match(path) |
| 4397 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 4398 | if (ppwperm != null && pp.match(path) && |
| 4399 | (pm.checkUidPermission(ppwperm, uid) |
| 4400 | == PackageManager.PERMISSION_GRANTED)) { |
| 4401 | writePerm = true; |
| 4402 | } |
| 4403 | } |
| 4404 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4405 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4406 | } catch (RemoteException e) { |
| 4407 | return false; |
| 4408 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4409 | |
| 4410 | return readPerm && writePerm; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4411 | } |
| 4412 | |
| 4413 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 4414 | int modeFlags) { |
| 4415 | // Root gets to do everything. |
| 4416 | if (uid == 0 || !Process.supportsProcesses()) { |
| 4417 | return true; |
| 4418 | } |
| 4419 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 4420 | if (perms == null) return false; |
| 4421 | UriPermission perm = perms.get(uri); |
| 4422 | if (perm == null) return false; |
| 4423 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 4424 | } |
| 4425 | |
| 4426 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
| 4427 | // Another redirected-binder-call permissions check as in |
| 4428 | // {@link checkComponentPermission}. |
| 4429 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4430 | if (tlsIdentity != null) { |
| 4431 | uid = tlsIdentity.uid; |
| 4432 | pid = tlsIdentity.pid; |
| 4433 | } |
| 4434 | |
| 4435 | // Our own process gets to do everything. |
| 4436 | if (pid == MY_PID) { |
| 4437 | return PackageManager.PERMISSION_GRANTED; |
| 4438 | } |
| 4439 | synchronized(this) { |
| 4440 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 4441 | ? PackageManager.PERMISSION_GRANTED |
| 4442 | : PackageManager.PERMISSION_DENIED; |
| 4443 | } |
| 4444 | } |
| 4445 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4446 | /** |
| 4447 | * Check if the targetPkg can be granted permission to access uri by |
| 4448 | * the callingUid using the given modeFlags. Throws a security exception |
| 4449 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 4450 | * if the URI permission grant should be performed; returns -1 if it is not |
| 4451 | * needed (for example targetPkg already has permission to access the URI). |
| 4452 | */ |
| 4453 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
| 4454 | Uri uri, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4455 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4456 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4457 | if (modeFlags == 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4458 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4459 | } |
| 4460 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4461 | if (targetPkg != null) { |
| 4462 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4463 | "Checking grant " + targetPkg + " permission to " + uri); |
| 4464 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4465 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4466 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4467 | |
| 4468 | // If this is not a content: uri, we can't do anything with it. |
| 4469 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4470 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4471 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4472 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | String name = uri.getAuthority(); |
| 4476 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4477 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4478 | if (cpr != null) { |
| 4479 | pi = cpr.info; |
| 4480 | } else { |
| 4481 | try { |
| 4482 | pi = pm.resolveContentProvider(name, |
| 4483 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4484 | } catch (RemoteException ex) { |
| 4485 | } |
| 4486 | } |
| 4487 | if (pi == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4488 | Slog.w(TAG, "No content provider found for: " + name); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4489 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | int targetUid; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4493 | if (targetPkg != null) { |
| 4494 | try { |
| 4495 | targetUid = pm.getPackageUid(targetPkg); |
| 4496 | if (targetUid < 0) { |
| 4497 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4498 | "Can't grant URI permission no uid for: " + targetPkg); |
| 4499 | return -1; |
| 4500 | } |
| 4501 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4502 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4503 | } |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4504 | } else { |
| 4505 | targetUid = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4506 | } |
| 4507 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4508 | if (targetUid >= 0) { |
| 4509 | // First... does the target actually need this permission? |
| 4510 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 4511 | // No need to grant the target this permission. |
| 4512 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4513 | "Target " + targetPkg + " already has full permission to " + uri); |
| 4514 | return -1; |
| 4515 | } |
| 4516 | } else { |
| 4517 | // First... there is no target package, so can anyone access it? |
| 4518 | boolean allowed = pi.exported; |
| 4519 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4520 | if (pi.readPermission != null) { |
| 4521 | allowed = false; |
| 4522 | } |
| 4523 | } |
| 4524 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4525 | if (pi.writePermission != null) { |
| 4526 | allowed = false; |
| 4527 | } |
| 4528 | } |
| 4529 | if (allowed) { |
| 4530 | return -1; |
| 4531 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4532 | } |
| 4533 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4534 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4535 | if (!pi.grantUriPermissions) { |
| 4536 | throw new SecurityException("Provider " + pi.packageName |
| 4537 | + "/" + pi.name |
| 4538 | + " does not allow granting of Uri permissions (uri " |
| 4539 | + uri + ")"); |
| 4540 | } |
| 4541 | if (pi.uriPermissionPatterns != null) { |
| 4542 | final int N = pi.uriPermissionPatterns.length; |
| 4543 | boolean allowed = false; |
| 4544 | for (int i=0; i<N; i++) { |
| 4545 | if (pi.uriPermissionPatterns[i] != null |
| 4546 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 4547 | allowed = true; |
| 4548 | break; |
| 4549 | } |
| 4550 | } |
| 4551 | if (!allowed) { |
| 4552 | throw new SecurityException("Provider " + pi.packageName |
| 4553 | + "/" + pi.name |
| 4554 | + " does not allow granting of permission to path of Uri " |
| 4555 | + uri); |
| 4556 | } |
| 4557 | } |
| 4558 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4559 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4560 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4561 | if (callingUid != Process.myUid()) { |
| 4562 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 4563 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4564 | throw new SecurityException("Uid " + callingUid |
| 4565 | + " does not have permission to uri " + uri); |
| 4566 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4567 | } |
| 4568 | } |
| 4569 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4570 | return targetUid; |
| 4571 | } |
| 4572 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4573 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 4574 | Uri uri, int modeFlags) { |
| 4575 | synchronized(this) { |
| 4576 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags); |
| 4577 | } |
| 4578 | } |
| 4579 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4580 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 4581 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 4582 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4583 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4584 | if (modeFlags == 0) { |
| 4585 | return; |
| 4586 | } |
| 4587 | |
| 4588 | // 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] | 4589 | // to the uri, and the target doesn't. Let's now give this to |
| 4590 | // the target. |
| 4591 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4592 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4593 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4595 | HashMap<Uri, UriPermission> targetUris |
| 4596 | = mGrantedUriPermissions.get(targetUid); |
| 4597 | if (targetUris == null) { |
| 4598 | targetUris = new HashMap<Uri, UriPermission>(); |
| 4599 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 4600 | } |
| 4601 | |
| 4602 | UriPermission perm = targetUris.get(uri); |
| 4603 | if (perm == null) { |
| 4604 | perm = new UriPermission(targetUid, uri); |
| 4605 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4606 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4607 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4608 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4609 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4610 | perm.globalModeFlags |= modeFlags; |
Vairavan Srinivasan | 91c12c2 | 2011-01-21 18:26:06 -0800 | [diff] [blame] | 4611 | } else { |
| 4612 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4613 | perm.readOwners.add(owner); |
| 4614 | owner.addReadPermission(perm); |
| 4615 | } |
| 4616 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4617 | perm.writeOwners.add(owner); |
| 4618 | owner.addWritePermission(perm); |
| 4619 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4620 | } |
| 4621 | } |
| 4622 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4623 | void grantUriPermissionLocked(int callingUid, |
| 4624 | String targetPkg, Uri uri, int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4625 | if (targetPkg == null) { |
| 4626 | throw new NullPointerException("targetPkg"); |
| 4627 | } |
| 4628 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4629 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags); |
| 4630 | if (targetUid < 0) { |
| 4631 | return; |
| 4632 | } |
| 4633 | |
| 4634 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 4635 | } |
| 4636 | |
| 4637 | /** |
| 4638 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 4639 | */ |
| 4640 | int checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 4641 | String targetPkg, Intent intent) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4642 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4643 | "Checking URI perm to " + (intent != null ? intent.getData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4644 | + " from " + intent + "; flags=0x" |
| 4645 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 4646 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4647 | if (targetPkg == null) { |
| 4648 | throw new NullPointerException("targetPkg"); |
| 4649 | } |
| 4650 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4651 | if (intent == null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4652 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4653 | } |
| 4654 | Uri data = intent.getData(); |
| 4655 | if (data == null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4656 | return -1; |
| 4657 | } |
| 4658 | return checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 4659 | intent.getFlags()); |
| 4660 | } |
| 4661 | |
| 4662 | /** |
| 4663 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 4664 | */ |
| 4665 | void grantUriPermissionUncheckedFromIntentLocked(int targetUid, |
| 4666 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
| 4667 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, intent.getData(), |
| 4668 | intent.getFlags(), owner); |
| 4669 | } |
| 4670 | |
| 4671 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 4672 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
| 4673 | int targetUid = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, intent); |
| 4674 | if (targetUid < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4675 | return; |
| 4676 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4677 | |
| 4678 | grantUriPermissionUncheckedFromIntentLocked(targetUid, targetPkg, intent, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4679 | } |
| 4680 | |
| 4681 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 4682 | Uri uri, int modeFlags) { |
| 4683 | synchronized(this) { |
| 4684 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 4685 | if (r == null) { |
| 4686 | throw new SecurityException("Unable to find app for caller " |
| 4687 | + caller |
| 4688 | + " when granting permission to uri " + uri); |
| 4689 | } |
| 4690 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4691 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4692 | } |
| 4693 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4694 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4695 | } |
| 4696 | |
| 4697 | grantUriPermissionLocked(r.info.uid, targetPkg, uri, modeFlags, |
| 4698 | null); |
| 4699 | } |
| 4700 | } |
| 4701 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4702 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4703 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4704 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 4705 | HashMap<Uri, UriPermission> perms |
| 4706 | = mGrantedUriPermissions.get(perm.uid); |
| 4707 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4708 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4709 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4710 | perms.remove(perm.uri); |
| 4711 | if (perms.size() == 0) { |
| 4712 | mGrantedUriPermissions.remove(perm.uid); |
| 4713 | } |
| 4714 | } |
| 4715 | } |
| 4716 | } |
| 4717 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4718 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 4719 | int modeFlags) { |
| 4720 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4721 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4722 | if (modeFlags == 0) { |
| 4723 | return; |
| 4724 | } |
| 4725 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4726 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4727 | "Revoking all granted permissions to " + uri); |
| 4728 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4729 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4730 | |
| 4731 | final String authority = uri.getAuthority(); |
| 4732 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4733 | ContentProviderRecord cpr = mProvidersByName.get(authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4734 | if (cpr != null) { |
| 4735 | pi = cpr.info; |
| 4736 | } else { |
| 4737 | try { |
| 4738 | pi = pm.resolveContentProvider(authority, |
| 4739 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4740 | } catch (RemoteException ex) { |
| 4741 | } |
| 4742 | } |
| 4743 | if (pi == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4744 | Slog.w(TAG, "No content provider found for: " + authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4745 | return; |
| 4746 | } |
| 4747 | |
| 4748 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4749 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4750 | // Right now, if you are not the original owner of the permission, |
| 4751 | // you are not allowed to revoke it. |
| 4752 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4753 | throw new SecurityException("Uid " + callingUid |
| 4754 | + " does not have permission to uri " + uri); |
| 4755 | //} |
| 4756 | } |
| 4757 | |
| 4758 | // Go through all of the permissions and remove any that match. |
| 4759 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 4760 | if (SEGMENTS != null) { |
| 4761 | final int NS = SEGMENTS.size(); |
| 4762 | int N = mGrantedUriPermissions.size(); |
| 4763 | for (int i=0; i<N; i++) { |
| 4764 | HashMap<Uri, UriPermission> perms |
| 4765 | = mGrantedUriPermissions.valueAt(i); |
| 4766 | Iterator<UriPermission> it = perms.values().iterator(); |
| 4767 | toploop: |
| 4768 | while (it.hasNext()) { |
| 4769 | UriPermission perm = it.next(); |
| 4770 | Uri targetUri = perm.uri; |
| 4771 | if (!authority.equals(targetUri.getAuthority())) { |
| 4772 | continue; |
| 4773 | } |
| 4774 | List<String> targetSegments = targetUri.getPathSegments(); |
| 4775 | if (targetSegments == null) { |
| 4776 | continue; |
| 4777 | } |
| 4778 | if (targetSegments.size() < NS) { |
| 4779 | continue; |
| 4780 | } |
| 4781 | for (int j=0; j<NS; j++) { |
| 4782 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 4783 | continue toploop; |
| 4784 | } |
| 4785 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4786 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4787 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4788 | perm.clearModes(modeFlags); |
| 4789 | if (perm.modeFlags == 0) { |
| 4790 | it.remove(); |
| 4791 | } |
| 4792 | } |
| 4793 | if (perms.size() == 0) { |
| 4794 | mGrantedUriPermissions.remove( |
| 4795 | mGrantedUriPermissions.keyAt(i)); |
| 4796 | N--; |
| 4797 | i--; |
| 4798 | } |
| 4799 | } |
| 4800 | } |
| 4801 | } |
| 4802 | |
| 4803 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 4804 | int modeFlags) { |
| 4805 | synchronized(this) { |
| 4806 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 4807 | if (r == null) { |
| 4808 | throw new SecurityException("Unable to find app for caller " |
| 4809 | + caller |
| 4810 | + " when revoking permission to uri " + uri); |
| 4811 | } |
| 4812 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4813 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4814 | return; |
| 4815 | } |
| 4816 | |
| 4817 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4818 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4819 | if (modeFlags == 0) { |
| 4820 | return; |
| 4821 | } |
| 4822 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4823 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4824 | |
| 4825 | final String authority = uri.getAuthority(); |
| 4826 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4827 | ContentProviderRecord cpr = mProvidersByName.get(authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4828 | if (cpr != null) { |
| 4829 | pi = cpr.info; |
| 4830 | } else { |
| 4831 | try { |
| 4832 | pi = pm.resolveContentProvider(authority, |
| 4833 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4834 | } catch (RemoteException ex) { |
| 4835 | } |
| 4836 | } |
| 4837 | if (pi == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4838 | Slog.w(TAG, "No content provider found for: " + authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4839 | return; |
| 4840 | } |
| 4841 | |
| 4842 | revokeUriPermissionLocked(r.info.uid, uri, modeFlags); |
| 4843 | } |
| 4844 | } |
| 4845 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4846 | @Override |
| 4847 | public IBinder newUriPermissionOwner(String name) { |
| 4848 | synchronized(this) { |
| 4849 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 4850 | return owner.getExternalTokenLocked(); |
| 4851 | } |
| 4852 | } |
| 4853 | |
| 4854 | @Override |
| 4855 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 4856 | Uri uri, int modeFlags) { |
| 4857 | synchronized(this) { |
| 4858 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 4859 | if (owner == null) { |
| 4860 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 4861 | } |
| 4862 | if (fromUid != Binder.getCallingUid()) { |
| 4863 | if (Binder.getCallingUid() != Process.myUid()) { |
| 4864 | // Only system code can grant URI permissions on behalf |
| 4865 | // of other users. |
| 4866 | throw new SecurityException("nice try"); |
| 4867 | } |
| 4868 | } |
| 4869 | if (targetPkg == null) { |
| 4870 | throw new IllegalArgumentException("null target"); |
| 4871 | } |
| 4872 | if (uri == null) { |
| 4873 | throw new IllegalArgumentException("null uri"); |
| 4874 | } |
| 4875 | |
| 4876 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 4877 | } |
| 4878 | } |
| 4879 | |
| 4880 | @Override |
| 4881 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 4882 | synchronized(this) { |
| 4883 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 4884 | if (owner == null) { |
| 4885 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 4886 | } |
| 4887 | |
| 4888 | if (uri == null) { |
| 4889 | owner.removeUriPermissionsLocked(mode); |
| 4890 | } else { |
| 4891 | owner.removeUriPermissionLocked(uri, mode); |
| 4892 | } |
| 4893 | } |
| 4894 | } |
| 4895 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4896 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 4897 | synchronized (this) { |
| 4898 | ProcessRecord app = |
| 4899 | who != null ? getRecordForAppLocked(who) : null; |
| 4900 | if (app == null) return; |
| 4901 | |
| 4902 | Message msg = Message.obtain(); |
| 4903 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 4904 | msg.obj = app; |
| 4905 | msg.arg1 = waiting ? 1 : 0; |
| 4906 | mHandler.sendMessage(msg); |
| 4907 | } |
| 4908 | } |
| 4909 | |
| 4910 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
| 4911 | outInfo.availMem = Process.getFreeMemory(); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 4912 | outInfo.threshold = HOME_APP_MEM; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4913 | outInfo.lowMemory = outInfo.availMem < |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 4914 | (HOME_APP_MEM + ((HIDDEN_APP_MEM-HOME_APP_MEM)/2)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | // ========================================================= |
| 4918 | // TASK MANAGEMENT |
| 4919 | // ========================================================= |
| 4920 | |
| 4921 | public List getTasks(int maxNum, int flags, |
| 4922 | IThumbnailReceiver receiver) { |
| 4923 | ArrayList list = new ArrayList(); |
| 4924 | |
| 4925 | PendingThumbnailsRecord pending = null; |
| 4926 | IApplicationThread topThumbnail = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4927 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4928 | |
| 4929 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4930 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4931 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 4932 | + ", receiver=" + receiver); |
| 4933 | |
| 4934 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 4935 | != PackageManager.PERMISSION_GRANTED) { |
| 4936 | if (receiver != null) { |
| 4937 | // If the caller wants to wait for pending thumbnails, |
| 4938 | // it ain't gonna get them. |
| 4939 | try { |
| 4940 | receiver.finished(); |
| 4941 | } catch (RemoteException ex) { |
| 4942 | } |
| 4943 | } |
| 4944 | String msg = "Permission Denial: getTasks() from pid=" |
| 4945 | + Binder.getCallingPid() |
| 4946 | + ", uid=" + Binder.getCallingUid() |
| 4947 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4948 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4949 | throw new SecurityException(msg); |
| 4950 | } |
| 4951 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4952 | int pos = mMainStack.mHistory.size()-1; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4953 | ActivityRecord next = |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4954 | pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4955 | ActivityRecord top = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4956 | TaskRecord curTask = null; |
| 4957 | int numActivities = 0; |
| 4958 | int numRunning = 0; |
| 4959 | while (pos >= 0 && maxNum > 0) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4960 | final ActivityRecord r = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4961 | pos--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4962 | next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4963 | |
| 4964 | // Initialize state for next task if needed. |
| 4965 | if (top == null || |
| 4966 | (top.state == ActivityState.INITIALIZING |
| 4967 | && top.task == r.task)) { |
| 4968 | top = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4969 | curTask = r.task; |
| 4970 | numActivities = numRunning = 0; |
| 4971 | } |
| 4972 | |
| 4973 | // Add 'r' into the current task. |
| 4974 | numActivities++; |
| 4975 | if (r.app != null && r.app.thread != null) { |
| 4976 | numRunning++; |
| 4977 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4978 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4979 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4980 | TAG, r.intent.getComponent().flattenToShortString() |
| 4981 | + ": task=" + r.task); |
| 4982 | |
| 4983 | // If the next one is a different task, generate a new |
| 4984 | // TaskInfo entry for what we have. |
| 4985 | if (next == null || next.task != curTask) { |
| 4986 | ActivityManager.RunningTaskInfo ci |
| 4987 | = new ActivityManager.RunningTaskInfo(); |
| 4988 | ci.id = curTask.taskId; |
| 4989 | ci.baseActivity = r.intent.getComponent(); |
| 4990 | ci.topActivity = top.intent.getComponent(); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 4991 | if (top.thumbHolder != null) { |
| 4992 | ci.description = top.thumbHolder.lastDescription; |
| 4993 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4994 | ci.numActivities = numActivities; |
| 4995 | ci.numRunning = numRunning; |
| 4996 | //System.out.println( |
| 4997 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 4998 | if (ci.thumbnail == null && receiver != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4999 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5000 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 5001 | + " app=" + top.app |
| 5002 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 5003 | if (top.state == ActivityState.RESUMED |
| 5004 | || top.state == ActivityState.PAUSING) { |
| 5005 | if (top.idle && top.app != null |
| 5006 | && top.app.thread != null) { |
| 5007 | topRecord = top; |
| 5008 | topThumbnail = top.app.thread; |
| 5009 | } else { |
| 5010 | top.thumbnailNeeded = true; |
| 5011 | } |
| 5012 | } |
| 5013 | if (pending == null) { |
| 5014 | pending = new PendingThumbnailsRecord(receiver); |
| 5015 | } |
| 5016 | pending.pendingRecords.add(top); |
| 5017 | } |
| 5018 | list.add(ci); |
| 5019 | maxNum--; |
| 5020 | top = null; |
| 5021 | } |
| 5022 | } |
| 5023 | |
| 5024 | if (pending != null) { |
| 5025 | mPendingThumbnails.add(pending); |
| 5026 | } |
| 5027 | } |
| 5028 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5029 | 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] | 5030 | |
| 5031 | if (topThumbnail != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5032 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5033 | try { |
| 5034 | topThumbnail.requestThumbnail(topRecord); |
| 5035 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5036 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5037 | sendPendingThumbnail(null, topRecord, null, null, true); |
| 5038 | } |
| 5039 | } |
| 5040 | |
| 5041 | if (pending == null && receiver != null) { |
| 5042 | // In this case all thumbnails were available and the client |
| 5043 | // is being asked to be told when the remaining ones come in... |
| 5044 | // which is unusually, since the top-most currently running |
| 5045 | // activity should never have a canned thumbnail! Oh well. |
| 5046 | try { |
| 5047 | receiver.finished(); |
| 5048 | } catch (RemoteException ex) { |
| 5049 | } |
| 5050 | } |
| 5051 | |
| 5052 | return list; |
| 5053 | } |
| 5054 | |
| 5055 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
| 5056 | int flags) { |
| 5057 | synchronized (this) { |
| 5058 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 5059 | "getRecentTasks()"); |
| 5060 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5061 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5062 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5063 | final int N = mRecentTasks.size(); |
| 5064 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 5065 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 5066 | maxNum < N ? maxNum : N); |
| 5067 | for (int i=0; i<N && maxNum > 0; i++) { |
| 5068 | TaskRecord tr = mRecentTasks.get(i); |
| 5069 | if (((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
| 5070 | || (tr.intent == null) |
| 5071 | || ((tr.intent.getFlags() |
| 5072 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 5073 | ActivityManager.RecentTaskInfo rti |
| 5074 | = new ActivityManager.RecentTaskInfo(); |
| 5075 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5076 | rti.persistentId = tr.taskId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5077 | rti.baseIntent = new Intent( |
| 5078 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5079 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 5080 | rti.description = tr.lastDescription; |
| 5081 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5082 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5083 | // Check whether this activity is currently available. |
| 5084 | try { |
| 5085 | if (rti.origActivity != null) { |
| 5086 | if (pm.getActivityInfo(rti.origActivity, 0) == null) { |
| 5087 | continue; |
| 5088 | } |
| 5089 | } else if (rti.baseIntent != null) { |
| 5090 | if (pm.queryIntentActivities(rti.baseIntent, |
| 5091 | null, 0) == null) { |
| 5092 | continue; |
| 5093 | } |
| 5094 | } |
| 5095 | } catch (RemoteException e) { |
| 5096 | // Will never happen. |
| 5097 | } |
| 5098 | } |
| 5099 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5100 | res.add(rti); |
| 5101 | maxNum--; |
| 5102 | } |
| 5103 | } |
| 5104 | return res; |
| 5105 | } |
| 5106 | } |
| 5107 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5108 | private TaskRecord taskForIdLocked(int id) { |
| 5109 | final int N = mRecentTasks.size(); |
| 5110 | for (int i=0; i<N; i++) { |
| 5111 | TaskRecord tr = mRecentTasks.get(i); |
| 5112 | if (tr.taskId == id) { |
| 5113 | return tr; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5114 | } |
| 5115 | } |
| 5116 | return null; |
| 5117 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5118 | |
| 5119 | public ActivityManager.TaskThumbnails getTaskThumbnails(int id) { |
| 5120 | synchronized (this) { |
| 5121 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5122 | "getTaskThumbnails()"); |
| 5123 | TaskRecord tr = taskForIdLocked(id); |
| 5124 | if (tr != null) { |
| 5125 | return mMainStack.getTaskThumbnailsLocked(tr); |
| 5126 | } |
| 5127 | } |
| 5128 | return null; |
| 5129 | } |
| 5130 | |
| 5131 | public boolean removeSubTask(int taskId, int subTaskIndex) { |
| 5132 | synchronized (this) { |
| 5133 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5134 | "removeSubTask()"); |
| 5135 | long ident = Binder.clearCallingIdentity(); |
| 5136 | try { |
| 5137 | return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex) != null; |
| 5138 | } finally { |
| 5139 | Binder.restoreCallingIdentity(ident); |
| 5140 | } |
| 5141 | } |
| 5142 | } |
| 5143 | |
| 5144 | private void removeTaskProcessesLocked(ActivityRecord root) { |
| 5145 | TaskRecord tr = root.task; |
| 5146 | Intent baseIntent = new Intent( |
| 5147 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5148 | ComponentName component = baseIntent.getComponent(); |
| 5149 | if (component == null) { |
| 5150 | Slog.w(TAG, "Now component for base intent of task: " + tr); |
| 5151 | return; |
| 5152 | } |
| 5153 | |
| 5154 | // Find any running services associated with this app. |
| 5155 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
| 5156 | for (ServiceRecord sr : mServices.values()) { |
| 5157 | if (sr.packageName.equals(component.getPackageName())) { |
| 5158 | services.add(sr); |
| 5159 | } |
| 5160 | } |
| 5161 | |
| 5162 | // Take care of any running services associated with the app. |
| 5163 | for (int i=0; i<services.size(); i++) { |
| 5164 | ServiceRecord sr = services.get(i); |
| 5165 | if (sr.startRequested) { |
| 5166 | if ((sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0) { |
| 5167 | stopServiceLocked(sr); |
| 5168 | } else { |
| 5169 | sr.pendingStarts.add(new ServiceRecord.StartItem(sr, true, |
| 5170 | sr.makeNextStartId(), baseIntent, -1)); |
| 5171 | if (sr.app != null && sr.app.thread != null) { |
| 5172 | sendServiceArgsLocked(sr, false); |
| 5173 | } |
| 5174 | } |
| 5175 | } |
| 5176 | } |
| 5177 | |
| 5178 | // Find any running processes associated with this app. |
| 5179 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 5180 | SparseArray<ProcessRecord> appProcs |
| 5181 | = mProcessNames.getMap().get(component.getPackageName()); |
| 5182 | if (appProcs != null) { |
| 5183 | for (int i=0; i<appProcs.size(); i++) { |
| 5184 | procs.add(appProcs.valueAt(i)); |
| 5185 | } |
| 5186 | } |
| 5187 | |
| 5188 | // Kill the running processes. |
| 5189 | for (int i=0; i<procs.size(); i++) { |
| 5190 | ProcessRecord pr = procs.get(i); |
| 5191 | if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 5192 | Slog.i(TAG, "Killing " + pr + ": remove task"); |
| 5193 | EventLog.writeEvent(EventLogTags.AM_KILL, pr.pid, |
| 5194 | pr.processName, pr.setAdj, "remove task"); |
| 5195 | Process.killProcessQuiet(pr.pid); |
| 5196 | } else { |
| 5197 | pr.waitingToKill = "remove task"; |
| 5198 | } |
| 5199 | } |
| 5200 | } |
| 5201 | |
| 5202 | public boolean removeTask(int taskId, int flags) { |
| 5203 | synchronized (this) { |
| 5204 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5205 | "removeTask()"); |
| 5206 | long ident = Binder.clearCallingIdentity(); |
| 5207 | try { |
| 5208 | ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1); |
| 5209 | if (r != null) { |
| 5210 | mRecentTasks.remove(r.task); |
| 5211 | |
| 5212 | if ((flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0) { |
| 5213 | removeTaskProcessesLocked(r); |
| 5214 | } |
| 5215 | |
| 5216 | return true; |
| 5217 | } |
| 5218 | } finally { |
| 5219 | Binder.restoreCallingIdentity(ident); |
| 5220 | } |
| 5221 | } |
| 5222 | return false; |
| 5223 | } |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5224 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5225 | private final int findAffinityTaskTopLocked(int startIndex, String affinity) { |
| 5226 | int j; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5227 | TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5228 | TaskRecord jt = startTask; |
| 5229 | |
| 5230 | // First look backwards |
| 5231 | for (j=startIndex-1; j>=0; j--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5232 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5233 | if (r.task != jt) { |
| 5234 | jt = r.task; |
| 5235 | if (affinity.equals(jt.affinity)) { |
| 5236 | return j; |
| 5237 | } |
| 5238 | } |
| 5239 | } |
| 5240 | |
| 5241 | // Now look forwards |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5242 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5243 | jt = startTask; |
| 5244 | for (j=startIndex+1; j<N; j++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5245 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5246 | if (r.task != jt) { |
| 5247 | if (affinity.equals(jt.affinity)) { |
| 5248 | return j; |
| 5249 | } |
| 5250 | jt = r.task; |
| 5251 | } |
| 5252 | } |
| 5253 | |
| 5254 | // Might it be at the top? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5255 | 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] | 5256 | return N-1; |
| 5257 | } |
| 5258 | |
| 5259 | return -1; |
| 5260 | } |
| 5261 | |
| 5262 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5263 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5264 | */ |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5265 | public void moveTaskToFront(int task, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5266 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5267 | "moveTaskToFront()"); |
| 5268 | |
| 5269 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5270 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5271 | Binder.getCallingUid(), "Task to front")) { |
| 5272 | return; |
| 5273 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5274 | final long origId = Binder.clearCallingIdentity(); |
| 5275 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5276 | TaskRecord tr = taskForIdLocked(task); |
| 5277 | if (tr != null) { |
| 5278 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5279 | mMainStack.mUserLeaving = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5280 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5281 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5282 | // Caller wants the home activity moved with it. To accomplish this, |
| 5283 | // we'll just move the home task to the top first. |
| 5284 | mMainStack.moveHomeToFrontLocked(); |
| 5285 | } |
| 5286 | mMainStack.moveTaskToFrontLocked(tr, null); |
| 5287 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5288 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5289 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 5290 | ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5291 | if (hr.task.taskId == task) { |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5292 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5293 | mMainStack.mUserLeaving = true; |
| 5294 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5295 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5296 | // Caller wants the home activity moved with it. To accomplish this, |
| 5297 | // we'll just move the home task to the top first. |
| 5298 | mMainStack.moveHomeToFrontLocked(); |
| 5299 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5300 | mMainStack.moveTaskToFrontLocked(hr.task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5301 | return; |
| 5302 | } |
| 5303 | } |
| 5304 | } finally { |
| 5305 | Binder.restoreCallingIdentity(origId); |
| 5306 | } |
| 5307 | } |
| 5308 | } |
| 5309 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5310 | public void moveTaskToBack(int task) { |
| 5311 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5312 | "moveTaskToBack()"); |
| 5313 | |
| 5314 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5315 | if (mMainStack.mResumedActivity != null |
| 5316 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5317 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5318 | Binder.getCallingUid(), "Task to back")) { |
| 5319 | return; |
| 5320 | } |
| 5321 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5322 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5323 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5324 | Binder.restoreCallingIdentity(origId); |
| 5325 | } |
| 5326 | } |
| 5327 | |
| 5328 | /** |
| 5329 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 5330 | * of the history stack. The activity's order within the task is unchanged. |
| 5331 | * |
| 5332 | * @param token A reference to the activity we wish to move |
| 5333 | * @param nonRoot If false then this only works if the activity is the root |
| 5334 | * of a task; if true it will work for any activity in a task. |
| 5335 | * @return Returns true if the move completed, false if not. |
| 5336 | */ |
| 5337 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
| 5338 | synchronized(this) { |
| 5339 | final long origId = Binder.clearCallingIdentity(); |
| 5340 | int taskId = getTaskForActivityLocked(token, !nonRoot); |
| 5341 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5342 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5343 | } |
| 5344 | Binder.restoreCallingIdentity(origId); |
| 5345 | } |
| 5346 | return false; |
| 5347 | } |
| 5348 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5349 | public void moveTaskBackwards(int task) { |
| 5350 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5351 | "moveTaskBackwards()"); |
| 5352 | |
| 5353 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5354 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5355 | Binder.getCallingUid(), "Task backwards")) { |
| 5356 | return; |
| 5357 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5358 | final long origId = Binder.clearCallingIdentity(); |
| 5359 | moveTaskBackwardsLocked(task); |
| 5360 | Binder.restoreCallingIdentity(origId); |
| 5361 | } |
| 5362 | } |
| 5363 | |
| 5364 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5365 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5366 | } |
| 5367 | |
| 5368 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 5369 | synchronized(this) { |
| 5370 | return getTaskForActivityLocked(token, onlyRoot); |
| 5371 | } |
| 5372 | } |
| 5373 | |
| 5374 | int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5375 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5376 | TaskRecord lastTask = null; |
| 5377 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5378 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5379 | if (r == token) { |
| 5380 | if (!onlyRoot || lastTask != r.task) { |
| 5381 | return r.task.taskId; |
| 5382 | } |
| 5383 | return -1; |
| 5384 | } |
| 5385 | lastTask = r.task; |
| 5386 | } |
| 5387 | |
| 5388 | return -1; |
| 5389 | } |
| 5390 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5391 | public void finishOtherInstances(IBinder token, ComponentName className) { |
| 5392 | synchronized(this) { |
| 5393 | final long origId = Binder.clearCallingIdentity(); |
| 5394 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5395 | int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5396 | TaskRecord lastTask = null; |
| 5397 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5398 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5399 | if (r.realActivity.equals(className) |
| 5400 | && r != token && lastTask != r.task) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5401 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5402 | null, "others")) { |
| 5403 | i--; |
| 5404 | N--; |
| 5405 | } |
| 5406 | } |
| 5407 | lastTask = r.task; |
| 5408 | } |
| 5409 | |
| 5410 | Binder.restoreCallingIdentity(origId); |
| 5411 | } |
| 5412 | } |
| 5413 | |
| 5414 | // ========================================================= |
| 5415 | // THUMBNAILS |
| 5416 | // ========================================================= |
| 5417 | |
| 5418 | public void reportThumbnail(IBinder token, |
| 5419 | Bitmap thumbnail, CharSequence description) { |
| 5420 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 5421 | final long origId = Binder.clearCallingIdentity(); |
| 5422 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 5423 | Binder.restoreCallingIdentity(origId); |
| 5424 | } |
| 5425 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5426 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5427 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 5428 | TaskRecord task = null; |
| 5429 | ArrayList receivers = null; |
| 5430 | |
| 5431 | //System.out.println("Send pending thumbnail: " + r); |
| 5432 | |
| 5433 | synchronized(this) { |
| 5434 | if (r == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5435 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5436 | if (index < 0) { |
| 5437 | return; |
| 5438 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5439 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5440 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5441 | if (thumbnail == null && r.thumbHolder != null) { |
| 5442 | thumbnail = r.thumbHolder.lastThumbnail; |
| 5443 | description = r.thumbHolder.lastDescription; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5444 | } |
| 5445 | if (thumbnail == null && !always) { |
| 5446 | // If there is no thumbnail, and this entry is not actually |
| 5447 | // going away, then abort for now and pick up the next |
| 5448 | // thumbnail we get. |
| 5449 | return; |
| 5450 | } |
| 5451 | task = r.task; |
| 5452 | |
| 5453 | int N = mPendingThumbnails.size(); |
| 5454 | int i=0; |
| 5455 | while (i<N) { |
| 5456 | PendingThumbnailsRecord pr = |
| 5457 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 5458 | //System.out.println("Looking in " + pr.pendingRecords); |
| 5459 | if (pr.pendingRecords.remove(r)) { |
| 5460 | if (receivers == null) { |
| 5461 | receivers = new ArrayList(); |
| 5462 | } |
| 5463 | receivers.add(pr); |
| 5464 | if (pr.pendingRecords.size() == 0) { |
| 5465 | pr.finished = true; |
| 5466 | mPendingThumbnails.remove(i); |
| 5467 | N--; |
| 5468 | continue; |
| 5469 | } |
| 5470 | } |
| 5471 | i++; |
| 5472 | } |
| 5473 | } |
| 5474 | |
| 5475 | if (receivers != null) { |
| 5476 | final int N = receivers.size(); |
| 5477 | for (int i=0; i<N; i++) { |
| 5478 | try { |
| 5479 | PendingThumbnailsRecord pr = |
| 5480 | (PendingThumbnailsRecord)receivers.get(i); |
| 5481 | pr.receiver.newThumbnail( |
| 5482 | task != null ? task.taskId : -1, thumbnail, description); |
| 5483 | if (pr.finished) { |
| 5484 | pr.receiver.finished(); |
| 5485 | } |
| 5486 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5487 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5488 | } |
| 5489 | } |
| 5490 | } |
| 5491 | } |
| 5492 | |
| 5493 | // ========================================================= |
| 5494 | // CONTENT PROVIDERS |
| 5495 | // ========================================================= |
| 5496 | |
| 5497 | private final List generateApplicationProvidersLocked(ProcessRecord app) { |
| 5498 | List providers = null; |
| 5499 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5500 | providers = AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5501 | queryContentProviders(app.processName, app.info.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5502 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5503 | } catch (RemoteException ex) { |
| 5504 | } |
| 5505 | if (providers != null) { |
| 5506 | final int N = providers.size(); |
| 5507 | for (int i=0; i<N; i++) { |
| 5508 | ProviderInfo cpi = |
| 5509 | (ProviderInfo)providers.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5510 | ContentProviderRecord cpr = mProvidersByClass.get(cpi.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5511 | if (cpr == null) { |
| 5512 | cpr = new ContentProviderRecord(cpi, app.info); |
| 5513 | mProvidersByClass.put(cpi.name, cpr); |
| 5514 | } |
| 5515 | app.pubProviders.put(cpi.name, cpr); |
| 5516 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 5517 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5518 | } |
| 5519 | } |
| 5520 | return providers; |
| 5521 | } |
| 5522 | |
| 5523 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5524 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5525 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
| 5526 | final int callingUid = (r != null) ? r.info.uid : Binder.getCallingUid(); |
| 5527 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5528 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5529 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5530 | return null; |
| 5531 | } |
| 5532 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5533 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5534 | == PackageManager.PERMISSION_GRANTED) { |
| 5535 | return null; |
| 5536 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5537 | |
| 5538 | PathPermission[] pps = cpi.pathPermissions; |
| 5539 | if (pps != null) { |
| 5540 | int i = pps.length; |
| 5541 | while (i > 0) { |
| 5542 | i--; |
| 5543 | PathPermission pp = pps[i]; |
| 5544 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5545 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5546 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5547 | return null; |
| 5548 | } |
| 5549 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5550 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5551 | == PackageManager.PERMISSION_GRANTED) { |
| 5552 | return null; |
| 5553 | } |
| 5554 | } |
| 5555 | } |
| 5556 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5557 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 5558 | if (perms != null) { |
| 5559 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 5560 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 5561 | return null; |
| 5562 | } |
| 5563 | } |
| 5564 | } |
| 5565 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5566 | String msg; |
| 5567 | if (!cpi.exported) { |
| 5568 | msg = "Permission Denial: opening provider " + cpi.name |
| 5569 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 5570 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 5571 | + cpi.applicationInfo.uid; |
| 5572 | } else { |
| 5573 | msg = "Permission Denial: opening provider " + cpi.name |
| 5574 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 5575 | + ", uid=" + callingUid + ") requires " |
| 5576 | + cpi.readPermission + " or " + cpi.writePermission; |
| 5577 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5578 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5579 | return msg; |
| 5580 | } |
| 5581 | |
| 5582 | private final ContentProviderHolder getContentProviderImpl( |
| 5583 | IApplicationThread caller, String name) { |
| 5584 | ContentProviderRecord cpr; |
| 5585 | ProviderInfo cpi = null; |
| 5586 | |
| 5587 | synchronized(this) { |
| 5588 | ProcessRecord r = null; |
| 5589 | if (caller != null) { |
| 5590 | r = getRecordForAppLocked(caller); |
| 5591 | if (r == null) { |
| 5592 | throw new SecurityException( |
| 5593 | "Unable to find app for caller " + caller |
| 5594 | + " (pid=" + Binder.getCallingPid() |
| 5595 | + ") when getting content provider " + name); |
| 5596 | } |
| 5597 | } |
| 5598 | |
| 5599 | // First check if this content provider has been published... |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5600 | cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5601 | if (cpr != null) { |
| 5602 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5603 | String msg; |
| 5604 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 5605 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5606 | } |
| 5607 | |
| 5608 | if (r != null && cpr.canRunHere(r)) { |
| 5609 | // This provider has been published or is in the process |
| 5610 | // of being published... but it is also allowed to run |
| 5611 | // in the caller's process, so don't make a connection |
| 5612 | // and just let the caller instantiate its own instance. |
| 5613 | if (cpr.provider != null) { |
| 5614 | // don't give caller the provider object, it needs |
| 5615 | // to make its own. |
| 5616 | cpr = new ContentProviderRecord(cpr); |
| 5617 | } |
| 5618 | return cpr; |
| 5619 | } |
| 5620 | |
| 5621 | final long origId = Binder.clearCallingIdentity(); |
| 5622 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5623 | // 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] | 5624 | // return it right away. |
| 5625 | if (r != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5626 | if (DEBUG_PROVIDER) Slog.v(TAG, |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5627 | "Adding provider requested by " |
| 5628 | + r.processName + " from process " |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 5629 | + cpr.info.processName); |
| 5630 | Integer cnt = r.conProviders.get(cpr); |
| 5631 | if (cnt == null) { |
| 5632 | r.conProviders.put(cpr, new Integer(1)); |
| 5633 | } else { |
| 5634 | r.conProviders.put(cpr, new Integer(cnt.intValue()+1)); |
| 5635 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5636 | cpr.clients.add(r); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 5637 | if (cpr.app != null && r.setAdj <= PERCEPTIBLE_APP_ADJ) { |
| 5638 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 5639 | // make sure to count it as being accessed and thus |
| 5640 | // back up on the LRU list. This is good because |
| 5641 | // content providers are often expensive to start. |
| 5642 | updateLruProcessLocked(cpr.app, false, true); |
| 5643 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 5644 | } else { |
| 5645 | cpr.externals++; |
| 5646 | } |
| 5647 | |
| 5648 | if (cpr.app != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5649 | updateOomAdjLocked(cpr.app); |
| 5650 | } |
| 5651 | |
| 5652 | Binder.restoreCallingIdentity(origId); |
| 5653 | |
| 5654 | } else { |
| 5655 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5656 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5657 | resolveContentProvider(name, |
| 5658 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5659 | } catch (RemoteException ex) { |
| 5660 | } |
| 5661 | if (cpi == null) { |
| 5662 | return null; |
| 5663 | } |
| 5664 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5665 | String msg; |
| 5666 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 5667 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5668 | } |
| 5669 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 5670 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 5671 | && !cpi.processName.equals("system")) { |
| 5672 | // If this content provider does not run in the system |
| 5673 | // process, and the system is not yet ready to run other |
| 5674 | // processes, then fail fast instead of hanging. |
| 5675 | throw new IllegalArgumentException( |
| 5676 | "Attempt to launch content provider before system ready"); |
| 5677 | } |
| 5678 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5679 | cpr = mProvidersByClass.get(cpi.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5680 | final boolean firstClass = cpr == null; |
| 5681 | if (firstClass) { |
| 5682 | try { |
| 5683 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5684 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5685 | getApplicationInfo( |
| 5686 | cpi.applicationInfo.packageName, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5687 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5688 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5689 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5690 | + cpi.name); |
| 5691 | return null; |
| 5692 | } |
| 5693 | cpr = new ContentProviderRecord(cpi, ai); |
| 5694 | } catch (RemoteException ex) { |
| 5695 | // pm is in same process, this will never happen. |
| 5696 | } |
| 5697 | } |
| 5698 | |
| 5699 | if (r != null && cpr.canRunHere(r)) { |
| 5700 | // If this is a multiprocess provider, then just return its |
| 5701 | // info and allow the caller to instantiate it. Only do |
| 5702 | // this if the provider is the same user as the caller's |
| 5703 | // process, or can run as root (so can be in any process). |
| 5704 | return cpr; |
| 5705 | } |
| 5706 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5707 | if (DEBUG_PROVIDER) { |
| 5708 | RuntimeException e = new RuntimeException("here"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5709 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.info.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5710 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5711 | } |
| 5712 | |
| 5713 | // This is single process, and our app is now connecting to it. |
| 5714 | // See if we are already in the process of launching this |
| 5715 | // provider. |
| 5716 | final int N = mLaunchingProviders.size(); |
| 5717 | int i; |
| 5718 | for (i=0; i<N; i++) { |
| 5719 | if (mLaunchingProviders.get(i) == cpr) { |
| 5720 | break; |
| 5721 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5722 | } |
| 5723 | |
| 5724 | // If the provider is not already being launched, then get it |
| 5725 | // started. |
| 5726 | if (i >= N) { |
| 5727 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 5728 | |
| 5729 | try { |
| 5730 | // Content provider is now in use, its package can't be stopped. |
| 5731 | try { |
| 5732 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 5733 | cpr.appInfo.packageName, false); |
| 5734 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 5735 | } catch (IllegalArgumentException e) { |
| 5736 | Slog.w(TAG, "Failed trying to unstop package " |
| 5737 | + cpr.appInfo.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 5738 | } |
| 5739 | |
| 5740 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 5741 | cpr.appInfo, false, 0, "content provider", |
| 5742 | new ComponentName(cpi.applicationInfo.packageName, |
| 5743 | cpi.name), false); |
| 5744 | if (proc == null) { |
| 5745 | Slog.w(TAG, "Unable to launch app " |
| 5746 | + cpi.applicationInfo.packageName + "/" |
| 5747 | + cpi.applicationInfo.uid + " for provider " |
| 5748 | + name + ": process is bad"); |
| 5749 | return null; |
| 5750 | } |
| 5751 | cpr.launchingApp = proc; |
| 5752 | mLaunchingProviders.add(cpr); |
| 5753 | } finally { |
| 5754 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5755 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5756 | } |
| 5757 | |
| 5758 | // Make sure the provider is published (the same provider class |
| 5759 | // may be published under multiple names). |
| 5760 | if (firstClass) { |
| 5761 | mProvidersByClass.put(cpi.name, cpr); |
| 5762 | } |
| 5763 | mProvidersByName.put(name, cpr); |
| 5764 | |
| 5765 | if (r != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5766 | if (DEBUG_PROVIDER) Slog.v(TAG, |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5767 | "Adding provider requested by " |
| 5768 | + r.processName + " from process " |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 5769 | + cpr.info.processName); |
| 5770 | Integer cnt = r.conProviders.get(cpr); |
| 5771 | if (cnt == null) { |
| 5772 | r.conProviders.put(cpr, new Integer(1)); |
| 5773 | } else { |
| 5774 | r.conProviders.put(cpr, new Integer(cnt.intValue()+1)); |
| 5775 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5776 | cpr.clients.add(r); |
| 5777 | } else { |
| 5778 | cpr.externals++; |
| 5779 | } |
| 5780 | } |
| 5781 | } |
| 5782 | |
| 5783 | // Wait for the provider to be published... |
| 5784 | synchronized (cpr) { |
| 5785 | while (cpr.provider == null) { |
| 5786 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5787 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5788 | + cpi.applicationInfo.packageName + "/" |
| 5789 | + cpi.applicationInfo.uid + " for provider " |
| 5790 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 5791 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5792 | cpi.applicationInfo.packageName, |
| 5793 | cpi.applicationInfo.uid, name); |
| 5794 | return null; |
| 5795 | } |
| 5796 | try { |
| 5797 | cpr.wait(); |
| 5798 | } catch (InterruptedException ex) { |
| 5799 | } |
| 5800 | } |
| 5801 | } |
| 5802 | return cpr; |
| 5803 | } |
| 5804 | |
| 5805 | public final ContentProviderHolder getContentProvider( |
| 5806 | IApplicationThread caller, String name) { |
| 5807 | if (caller == null) { |
| 5808 | String msg = "null IApplicationThread when getting content provider " |
| 5809 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5810 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5811 | throw new SecurityException(msg); |
| 5812 | } |
| 5813 | |
| 5814 | return getContentProviderImpl(caller, name); |
| 5815 | } |
| 5816 | |
| 5817 | private ContentProviderHolder getContentProviderExternal(String name) { |
| 5818 | return getContentProviderImpl(null, name); |
| 5819 | } |
| 5820 | |
| 5821 | /** |
| 5822 | * Drop a content provider from a ProcessRecord's bookkeeping |
| 5823 | * @param cpr |
| 5824 | */ |
| 5825 | public void removeContentProvider(IApplicationThread caller, String name) { |
| 5826 | synchronized (this) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5827 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5828 | if(cpr == null) { |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5829 | // remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5830 | if (DEBUG_PROVIDER) Slog.v(TAG, name + |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5831 | " provider not found in providers list"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5832 | return; |
| 5833 | } |
| 5834 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5835 | if (r == null) { |
| 5836 | throw new SecurityException( |
| 5837 | "Unable to find app for caller " + caller + |
| 5838 | " when removing content provider " + name); |
| 5839 | } |
| 5840 | //update content provider record entry info |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5841 | ContentProviderRecord localCpr = mProvidersByClass.get(cpr.info.name); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5842 | if (DEBUG_PROVIDER) Slog.v(TAG, "Removing provider requested by " |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5843 | + r.info.processName + " from process " |
| 5844 | + localCpr.appInfo.processName); |
| 5845 | if (localCpr.app == r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5846 | //should not happen. taken care of as a local provider |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5847 | Slog.w(TAG, "removeContentProvider called on local provider: " |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5848 | + cpr.info.name + " in process " + r.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5849 | return; |
| 5850 | } else { |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 5851 | Integer cnt = r.conProviders.get(localCpr); |
| 5852 | if (cnt == null || cnt.intValue() <= 1) { |
| 5853 | localCpr.clients.remove(r); |
| 5854 | r.conProviders.remove(localCpr); |
| 5855 | } else { |
| 5856 | r.conProviders.put(localCpr, new Integer(cnt.intValue()-1)); |
| 5857 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5858 | } |
| 5859 | updateOomAdjLocked(); |
| 5860 | } |
| 5861 | } |
| 5862 | |
| 5863 | private void removeContentProviderExternal(String name) { |
| 5864 | synchronized (this) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5865 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5866 | if(cpr == null) { |
| 5867 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5868 | 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] | 5869 | return; |
| 5870 | } |
| 5871 | |
| 5872 | //update content provider record entry info |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5873 | ContentProviderRecord localCpr = mProvidersByClass.get(cpr.info.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5874 | localCpr.externals--; |
| 5875 | if (localCpr.externals < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5876 | Slog.e(TAG, "Externals < 0 for content provider " + localCpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5877 | } |
| 5878 | updateOomAdjLocked(); |
| 5879 | } |
| 5880 | } |
| 5881 | |
| 5882 | public final void publishContentProviders(IApplicationThread caller, |
| 5883 | List<ContentProviderHolder> providers) { |
| 5884 | if (providers == null) { |
| 5885 | return; |
| 5886 | } |
| 5887 | |
| 5888 | synchronized(this) { |
| 5889 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5890 | if (r == null) { |
| 5891 | throw new SecurityException( |
| 5892 | "Unable to find app for caller " + caller |
| 5893 | + " (pid=" + Binder.getCallingPid() |
| 5894 | + ") when publishing content providers"); |
| 5895 | } |
| 5896 | |
| 5897 | final long origId = Binder.clearCallingIdentity(); |
| 5898 | |
| 5899 | final int N = providers.size(); |
| 5900 | for (int i=0; i<N; i++) { |
| 5901 | ContentProviderHolder src = providers.get(i); |
| 5902 | if (src == null || src.info == null || src.provider == null) { |
| 5903 | continue; |
| 5904 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5905 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5906 | if (dst != null) { |
| 5907 | mProvidersByClass.put(dst.info.name, dst); |
| 5908 | String names[] = dst.info.authority.split(";"); |
| 5909 | for (int j = 0; j < names.length; j++) { |
| 5910 | mProvidersByName.put(names[j], dst); |
| 5911 | } |
| 5912 | |
| 5913 | int NL = mLaunchingProviders.size(); |
| 5914 | int j; |
| 5915 | for (j=0; j<NL; j++) { |
| 5916 | if (mLaunchingProviders.get(j) == dst) { |
| 5917 | mLaunchingProviders.remove(j); |
| 5918 | j--; |
| 5919 | NL--; |
| 5920 | } |
| 5921 | } |
| 5922 | synchronized (dst) { |
| 5923 | dst.provider = src.provider; |
| 5924 | dst.app = r; |
| 5925 | dst.notifyAll(); |
| 5926 | } |
| 5927 | updateOomAdjLocked(r); |
| 5928 | } |
| 5929 | } |
| 5930 | |
| 5931 | Binder.restoreCallingIdentity(origId); |
| 5932 | } |
| 5933 | } |
| 5934 | |
| 5935 | public static final void installSystemProviders() { |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 5936 | List providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 5937 | synchronized (mSelf) { |
| 5938 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 5939 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 5940 | if (providers != null) { |
| 5941 | for (int i=providers.size()-1; i>=0; i--) { |
| 5942 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 5943 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 5944 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 5945 | + ": not system .apk"); |
| 5946 | providers.remove(i); |
| 5947 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 5948 | } |
| 5949 | } |
| 5950 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 5951 | if (providers != null) { |
| 5952 | mSystemThread.installSystemProviders(providers); |
| 5953 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 5954 | |
| 5955 | mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5956 | } |
| 5957 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 5958 | /** |
| 5959 | * Allows app to retrieve the MIME type of a URI without having permission |
| 5960 | * to access its content provider. |
| 5961 | * |
| 5962 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 5963 | * |
| 5964 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 5965 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 5966 | */ |
| 5967 | public String getProviderMimeType(Uri uri) { |
| 5968 | final String name = uri.getAuthority(); |
| 5969 | final long ident = Binder.clearCallingIdentity(); |
| 5970 | ContentProviderHolder holder = null; |
| 5971 | |
| 5972 | try { |
| 5973 | holder = getContentProviderExternal(name); |
| 5974 | if (holder != null) { |
| 5975 | return holder.provider.getType(uri); |
| 5976 | } |
| 5977 | } catch (RemoteException e) { |
| 5978 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 5979 | return null; |
| 5980 | } finally { |
| 5981 | if (holder != null) { |
| 5982 | removeContentProviderExternal(name); |
| 5983 | } |
| 5984 | Binder.restoreCallingIdentity(ident); |
| 5985 | } |
| 5986 | |
| 5987 | return null; |
| 5988 | } |
| 5989 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5990 | // ========================================================= |
| 5991 | // GLOBAL MANAGEMENT |
| 5992 | // ========================================================= |
| 5993 | |
| 5994 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
| 5995 | ApplicationInfo info, String customProcess) { |
| 5996 | String proc = customProcess != null ? customProcess : info.processName; |
| 5997 | BatteryStatsImpl.Uid.Proc ps = null; |
| 5998 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 5999 | synchronized (stats) { |
| 6000 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 6001 | } |
| 6002 | return new ProcessRecord(ps, thread, info, proc); |
| 6003 | } |
| 6004 | |
| 6005 | final ProcessRecord addAppLocked(ApplicationInfo info) { |
| 6006 | ProcessRecord app = getProcessRecordLocked(info.processName, info.uid); |
| 6007 | |
| 6008 | if (app == null) { |
| 6009 | app = newProcessRecordLocked(null, info, null); |
| 6010 | mProcessNames.put(info.processName, info.uid, app); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 6011 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6012 | } |
| 6013 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6014 | // This package really, really can not be stopped. |
| 6015 | try { |
| 6016 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 6017 | info.packageName, false); |
| 6018 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6019 | } catch (IllegalArgumentException e) { |
| 6020 | Slog.w(TAG, "Failed trying to unstop package " |
| 6021 | + info.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6022 | } |
| 6023 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6024 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 6025 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 6026 | app.persistent = true; |
| 6027 | app.maxAdj = CORE_SERVER_ADJ; |
| 6028 | } |
| 6029 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 6030 | mPersistentStartingProcesses.add(app); |
| 6031 | startProcessLocked(app, "added application", app.processName); |
| 6032 | } |
| 6033 | |
| 6034 | return app; |
| 6035 | } |
| 6036 | |
| 6037 | public void unhandledBack() { |
| 6038 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 6039 | "unhandledBack()"); |
| 6040 | |
| 6041 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6042 | int count = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6043 | if (DEBUG_SWITCH) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6044 | TAG, "Performing unhandledBack(): stack size = " + count); |
| 6045 | if (count > 1) { |
| 6046 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6047 | mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6048 | count-1, Activity.RESULT_CANCELED, null, "unhandled-back"); |
| 6049 | Binder.restoreCallingIdentity(origId); |
| 6050 | } |
| 6051 | } |
| 6052 | } |
| 6053 | |
| 6054 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
| 6055 | String name = uri.getAuthority(); |
| 6056 | ContentProviderHolder cph = getContentProviderExternal(name); |
| 6057 | ParcelFileDescriptor pfd = null; |
| 6058 | if (cph != null) { |
| 6059 | // We record the binder invoker's uid in thread-local storage before |
| 6060 | // going to the content provider to open the file. Later, in the code |
| 6061 | // that handles all permissions checks, we look for this uid and use |
| 6062 | // that rather than the Activity Manager's own uid. The effect is that |
| 6063 | // we do the check against the caller's permissions even though it looks |
| 6064 | // to the content provider like the Activity Manager itself is making |
| 6065 | // the request. |
| 6066 | sCallerIdentity.set(new Identity( |
| 6067 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 6068 | try { |
| 6069 | pfd = cph.provider.openFile(uri, "r"); |
| 6070 | } catch (FileNotFoundException e) { |
| 6071 | // do nothing; pfd will be returned null |
| 6072 | } finally { |
| 6073 | // Ensure that whatever happens, we clean up the identity state |
| 6074 | sCallerIdentity.remove(); |
| 6075 | } |
| 6076 | |
| 6077 | // We've got the fd now, so we're done with the provider. |
| 6078 | removeContentProviderExternal(name); |
| 6079 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6080 | 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] | 6081 | } |
| 6082 | return pfd; |
| 6083 | } |
| 6084 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6085 | // Actually is sleeping or shutting down or whatever else in the future |
| 6086 | // is an inactive state. |
| 6087 | public boolean isSleeping() { |
| 6088 | return mSleeping || mShuttingDown; |
| 6089 | } |
| 6090 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6091 | public void goingToSleep() { |
| 6092 | synchronized(this) { |
| 6093 | mSleeping = true; |
| 6094 | mWindowManager.setEventDispatching(false); |
| 6095 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6096 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6097 | |
| 6098 | // Initialize the wake times of all processes. |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 6099 | checkExcessivePowerUsageLocked(false); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6100 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 6101 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 6102 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6103 | } |
| 6104 | } |
| 6105 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6106 | public boolean shutdown(int timeout) { |
| 6107 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 6108 | != PackageManager.PERMISSION_GRANTED) { |
| 6109 | throw new SecurityException("Requires permission " |
| 6110 | + android.Manifest.permission.SHUTDOWN); |
| 6111 | } |
| 6112 | |
| 6113 | boolean timedout = false; |
| 6114 | |
| 6115 | synchronized(this) { |
| 6116 | mShuttingDown = true; |
| 6117 | mWindowManager.setEventDispatching(false); |
| 6118 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6119 | if (mMainStack.mResumedActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6120 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6121 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6122 | while (mMainStack.mResumedActivity != null |
| 6123 | || mMainStack.mPausingActivity != null) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6124 | long delay = endTime - System.currentTimeMillis(); |
| 6125 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6126 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6127 | timedout = true; |
| 6128 | break; |
| 6129 | } |
| 6130 | try { |
| 6131 | this.wait(); |
| 6132 | } catch (InterruptedException e) { |
| 6133 | } |
| 6134 | } |
| 6135 | } |
| 6136 | } |
| 6137 | |
| 6138 | mUsageStatsService.shutdown(); |
| 6139 | mBatteryStatsService.shutdown(); |
| 6140 | |
| 6141 | return timedout; |
| 6142 | } |
| 6143 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6144 | public final void activitySlept(IBinder token) { |
| 6145 | if (localLOGV) Slog.v( |
| 6146 | TAG, "Activity slept: token=" + token); |
| 6147 | |
| 6148 | ActivityRecord r = null; |
| 6149 | |
| 6150 | final long origId = Binder.clearCallingIdentity(); |
| 6151 | |
| 6152 | synchronized (this) { |
| 6153 | int index = mMainStack.indexOfTokenLocked(token); |
| 6154 | if (index >= 0) { |
| 6155 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
| 6156 | mMainStack.activitySleptLocked(r); |
| 6157 | } |
| 6158 | } |
| 6159 | |
| 6160 | Binder.restoreCallingIdentity(origId); |
| 6161 | } |
| 6162 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6163 | public void wakingUp() { |
| 6164 | synchronized(this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6165 | mWindowManager.setEventDispatching(true); |
| 6166 | mSleeping = false; |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6167 | mMainStack.awakeFromSleepingLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6168 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6169 | } |
| 6170 | } |
| 6171 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6172 | public void stopAppSwitches() { |
| 6173 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6174 | != PackageManager.PERMISSION_GRANTED) { |
| 6175 | throw new SecurityException("Requires permission " |
| 6176 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6177 | } |
| 6178 | |
| 6179 | synchronized(this) { |
| 6180 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 6181 | + APP_SWITCH_DELAY_TIME; |
| 6182 | mDidAppSwitch = false; |
| 6183 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6184 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6185 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 6186 | } |
| 6187 | } |
| 6188 | |
| 6189 | public void resumeAppSwitches() { |
| 6190 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6191 | != PackageManager.PERMISSION_GRANTED) { |
| 6192 | throw new SecurityException("Requires permission " |
| 6193 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6194 | } |
| 6195 | |
| 6196 | synchronized(this) { |
| 6197 | // Note that we don't execute any pending app switches... we will |
| 6198 | // let those wait until either the timeout, or the next start |
| 6199 | // activity request. |
| 6200 | mAppSwitchesAllowedTime = 0; |
| 6201 | } |
| 6202 | } |
| 6203 | |
| 6204 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 6205 | String name) { |
| 6206 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 6207 | return true; |
| 6208 | } |
| 6209 | |
| 6210 | final int perm = checkComponentPermission( |
| 6211 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6212 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6213 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 6214 | return true; |
| 6215 | } |
| 6216 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6217 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6218 | return false; |
| 6219 | } |
| 6220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6221 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 6222 | boolean persistent) { |
| 6223 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 6224 | "setDebugApp()"); |
| 6225 | |
| 6226 | // Note that this is not really thread safe if there are multiple |
| 6227 | // callers into it at the same time, but that's not a situation we |
| 6228 | // care about. |
| 6229 | if (persistent) { |
| 6230 | final ContentResolver resolver = mContext.getContentResolver(); |
| 6231 | Settings.System.putString( |
| 6232 | resolver, Settings.System.DEBUG_APP, |
| 6233 | packageName); |
| 6234 | Settings.System.putInt( |
| 6235 | resolver, Settings.System.WAIT_FOR_DEBUGGER, |
| 6236 | waitForDebugger ? 1 : 0); |
| 6237 | } |
| 6238 | |
| 6239 | synchronized (this) { |
| 6240 | if (!persistent) { |
| 6241 | mOrigDebugApp = mDebugApp; |
| 6242 | mOrigWaitForDebugger = mWaitForDebugger; |
| 6243 | } |
| 6244 | mDebugApp = packageName; |
| 6245 | mWaitForDebugger = waitForDebugger; |
| 6246 | mDebugTransient = !persistent; |
| 6247 | if (packageName != null) { |
| 6248 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 6249 | forceStopPackageLocked(packageName, -1, false, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6250 | Binder.restoreCallingIdentity(origId); |
| 6251 | } |
| 6252 | } |
| 6253 | } |
| 6254 | |
| 6255 | public void setAlwaysFinish(boolean enabled) { |
| 6256 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 6257 | "setAlwaysFinish()"); |
| 6258 | |
| 6259 | Settings.System.putInt( |
| 6260 | mContext.getContentResolver(), |
| 6261 | Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
| 6262 | |
| 6263 | synchronized (this) { |
| 6264 | mAlwaysFinishActivities = enabled; |
| 6265 | } |
| 6266 | } |
| 6267 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6268 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6269 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6270 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6271 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6272 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6273 | } |
| 6274 | } |
| 6275 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 6276 | public boolean isUserAMonkey() { |
| 6277 | // For now the fact that there is a controller implies |
| 6278 | // we have a monkey. |
| 6279 | synchronized (this) { |
| 6280 | return mController != null; |
| 6281 | } |
| 6282 | } |
| 6283 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6284 | public void registerActivityWatcher(IActivityWatcher watcher) { |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6285 | synchronized (this) { |
| 6286 | mWatchers.register(watcher); |
| 6287 | } |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6288 | } |
| 6289 | |
| 6290 | public void unregisterActivityWatcher(IActivityWatcher watcher) { |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6291 | synchronized (this) { |
| 6292 | mWatchers.unregister(watcher); |
| 6293 | } |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6294 | } |
| 6295 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 6296 | public void registerProcessObserver(IProcessObserver observer) { |
| 6297 | mProcessObservers.register(observer); |
| 6298 | } |
| 6299 | |
| 6300 | public void unregisterProcessObserver(IProcessObserver observer) { |
| 6301 | mProcessObservers.unregister(observer); |
| 6302 | } |
| 6303 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6304 | public void setImmersive(IBinder token, boolean immersive) { |
| 6305 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6306 | int index = (token != null) ? mMainStack.indexOfTokenLocked(token) : -1; |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6307 | if (index < 0) { |
| 6308 | throw new IllegalArgumentException(); |
| 6309 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6310 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6311 | r.immersive = immersive; |
| 6312 | } |
| 6313 | } |
| 6314 | |
| 6315 | public boolean isImmersive(IBinder token) { |
| 6316 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6317 | int index = (token != null) ? mMainStack.indexOfTokenLocked(token) : -1; |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6318 | if (index < 0) { |
| 6319 | throw new IllegalArgumentException(); |
| 6320 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6321 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6322 | return r.immersive; |
| 6323 | } |
| 6324 | } |
| 6325 | |
| 6326 | public boolean isTopActivityImmersive() { |
| 6327 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6328 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6329 | return (r != null) ? r.immersive : false; |
| 6330 | } |
| 6331 | } |
| 6332 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6333 | public final void enterSafeMode() { |
| 6334 | synchronized(this) { |
| 6335 | // It only makes sense to do this before the system is ready |
| 6336 | // and started launching other packages. |
| 6337 | if (!mSystemReady) { |
| 6338 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6339 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6340 | } catch (RemoteException e) { |
| 6341 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6342 | } |
| 6343 | } |
| 6344 | } |
| 6345 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 6346 | public final void showSafeModeOverlay() { |
| 6347 | View v = LayoutInflater.from(mContext).inflate( |
| 6348 | com.android.internal.R.layout.safe_mode, null); |
| 6349 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 6350 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 6351 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6352 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6353 | lp.gravity = Gravity.BOTTOM | Gravity.LEFT; |
| 6354 | lp.format = v.getBackground().getOpacity(); |
| 6355 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 6356 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 6357 | ((WindowManager)mContext.getSystemService( |
| 6358 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 6359 | } |
| 6360 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6361 | public void noteWakeupAlarm(IIntentSender sender) { |
| 6362 | if (!(sender instanceof PendingIntentRecord)) { |
| 6363 | return; |
| 6364 | } |
| 6365 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 6366 | synchronized (stats) { |
| 6367 | if (mBatteryStatsService.isOnBattery()) { |
| 6368 | mBatteryStatsService.enforceCallingPermission(); |
| 6369 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 6370 | int MY_UID = Binder.getCallingUid(); |
| 6371 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 6372 | BatteryStatsImpl.Uid.Pkg pkg = |
| 6373 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 6374 | pkg.incWakeupsLocked(); |
| 6375 | } |
| 6376 | } |
| 6377 | } |
| 6378 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6379 | public boolean killPids(int[] pids, String pReason, boolean secure) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6380 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6381 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6382 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6383 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6384 | // XXX Note: don't acquire main activity lock here, because the window |
| 6385 | // manager calls in with its locks held. |
| 6386 | |
| 6387 | boolean killed = false; |
| 6388 | synchronized (mPidsSelfLocked) { |
| 6389 | int[] types = new int[pids.length]; |
| 6390 | int worstType = 0; |
| 6391 | for (int i=0; i<pids.length; i++) { |
| 6392 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 6393 | if (proc != null) { |
| 6394 | int type = proc.setAdj; |
| 6395 | types[i] = type; |
| 6396 | if (type > worstType) { |
| 6397 | worstType = type; |
| 6398 | } |
| 6399 | } |
| 6400 | } |
| 6401 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6402 | // 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] | 6403 | // then constrain it so we will kill all hidden procs. |
| 6404 | if (worstType < EMPTY_APP_ADJ && worstType > HIDDEN_APP_MIN_ADJ) { |
| 6405 | worstType = HIDDEN_APP_MIN_ADJ; |
| 6406 | } |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6407 | |
| 6408 | // If this is not a secure call, don't let it kill processes that |
| 6409 | // are important. |
| 6410 | if (!secure && worstType < SECONDARY_SERVER_ADJ) { |
| 6411 | worstType = SECONDARY_SERVER_ADJ; |
| 6412 | } |
| 6413 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6414 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6415 | for (int i=0; i<pids.length; i++) { |
| 6416 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 6417 | if (proc == null) { |
| 6418 | continue; |
| 6419 | } |
| 6420 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6421 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 6422 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6423 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid, |
| 6424 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6425 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6426 | proc.killedBackground = true; |
| 6427 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6428 | } |
| 6429 | } |
| 6430 | } |
| 6431 | return killed; |
| 6432 | } |
| 6433 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6434 | public final void startRunning(String pkg, String cls, String action, |
| 6435 | String data) { |
| 6436 | synchronized(this) { |
| 6437 | if (mStartRunning) { |
| 6438 | return; |
| 6439 | } |
| 6440 | mStartRunning = true; |
| 6441 | mTopComponent = pkg != null && cls != null |
| 6442 | ? new ComponentName(pkg, cls) : null; |
| 6443 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 6444 | mTopData = data; |
| 6445 | if (!mSystemReady) { |
| 6446 | return; |
| 6447 | } |
| 6448 | } |
| 6449 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6450 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6451 | } |
| 6452 | |
| 6453 | private void retrieveSettings() { |
| 6454 | final ContentResolver resolver = mContext.getContentResolver(); |
| 6455 | String debugApp = Settings.System.getString( |
| 6456 | resolver, Settings.System.DEBUG_APP); |
| 6457 | boolean waitForDebugger = Settings.System.getInt( |
| 6458 | resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0; |
| 6459 | boolean alwaysFinishActivities = Settings.System.getInt( |
| 6460 | resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
| 6461 | |
| 6462 | Configuration configuration = new Configuration(); |
| 6463 | Settings.System.getConfiguration(resolver, configuration); |
| 6464 | |
| 6465 | synchronized (this) { |
| 6466 | mDebugApp = mOrigDebugApp = debugApp; |
| 6467 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 6468 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 6469 | // This happens before any activities are started, so we can |
| 6470 | // change mConfiguration in-place. |
| 6471 | mConfiguration.updateFrom(configuration); |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 6472 | mConfigurationSeq = mConfiguration.seq = 1; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6473 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6474 | } |
| 6475 | } |
| 6476 | |
| 6477 | public boolean testIsSystemReady() { |
| 6478 | // no need to synchronize(this) just to read & return the value |
| 6479 | return mSystemReady; |
| 6480 | } |
| 6481 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6482 | private static File getCalledPreBootReceiversFile() { |
| 6483 | File dataDir = Environment.getDataDirectory(); |
| 6484 | File systemDir = new File(dataDir, "system"); |
| 6485 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 6486 | return fname; |
| 6487 | } |
| 6488 | |
| 6489 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 6490 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 6491 | File file = getCalledPreBootReceiversFile(); |
| 6492 | FileInputStream fis = null; |
| 6493 | try { |
| 6494 | fis = new FileInputStream(file); |
| 6495 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
| 6496 | int vers = dis.readInt(); |
| 6497 | String codename = dis.readUTF(); |
| 6498 | if (vers == android.os.Build.VERSION.SDK_INT |
| 6499 | && codename.equals(android.os.Build.VERSION.CODENAME)) { |
| 6500 | int num = dis.readInt(); |
| 6501 | while (num > 0) { |
| 6502 | num--; |
| 6503 | String pkg = dis.readUTF(); |
| 6504 | String cls = dis.readUTF(); |
| 6505 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 6506 | } |
| 6507 | } |
| 6508 | } catch (FileNotFoundException e) { |
| 6509 | } catch (IOException e) { |
| 6510 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 6511 | } finally { |
| 6512 | if (fis != null) { |
| 6513 | try { |
| 6514 | fis.close(); |
| 6515 | } catch (IOException e) { |
| 6516 | } |
| 6517 | } |
| 6518 | } |
| 6519 | return lastDoneReceivers; |
| 6520 | } |
| 6521 | |
| 6522 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 6523 | File file = getCalledPreBootReceiversFile(); |
| 6524 | FileOutputStream fos = null; |
| 6525 | DataOutputStream dos = null; |
| 6526 | try { |
| 6527 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 6528 | fos = new FileOutputStream(file); |
| 6529 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
| 6530 | dos.writeInt(android.os.Build.VERSION.SDK_INT); |
| 6531 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
| 6532 | dos.writeInt(list.size()); |
| 6533 | for (int i=0; i<list.size(); i++) { |
| 6534 | dos.writeUTF(list.get(i).getPackageName()); |
| 6535 | dos.writeUTF(list.get(i).getClassName()); |
| 6536 | } |
| 6537 | } catch (IOException e) { |
| 6538 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 6539 | file.delete(); |
| 6540 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 6541 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6542 | if (dos != null) { |
| 6543 | try { |
| 6544 | dos.close(); |
| 6545 | } catch (IOException e) { |
| 6546 | // TODO Auto-generated catch block |
| 6547 | e.printStackTrace(); |
| 6548 | } |
| 6549 | } |
| 6550 | } |
| 6551 | } |
| 6552 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6553 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6554 | // In the simulator, startRunning will never have been called, which |
| 6555 | // normally sets a few crucial variables. Do it here instead. |
| 6556 | if (!Process.supportsProcesses()) { |
| 6557 | mStartRunning = true; |
| 6558 | mTopAction = Intent.ACTION_MAIN; |
| 6559 | } |
| 6560 | |
| 6561 | synchronized(this) { |
| 6562 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6563 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6564 | return; |
| 6565 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6566 | |
| 6567 | // Check to see if there are any update receivers to run. |
| 6568 | if (!mDidUpdate) { |
| 6569 | if (mWaitingUpdate) { |
| 6570 | return; |
| 6571 | } |
| 6572 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 6573 | List<ResolveInfo> ris = null; |
| 6574 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6575 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6576 | intent, null, 0); |
| 6577 | } catch (RemoteException e) { |
| 6578 | } |
| 6579 | if (ris != null) { |
| 6580 | for (int i=ris.size()-1; i>=0; i--) { |
| 6581 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 6582 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6583 | ris.remove(i); |
| 6584 | } |
| 6585 | } |
| 6586 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6587 | |
| 6588 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
| 6589 | |
| 6590 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6591 | for (int i=0; i<ris.size(); i++) { |
| 6592 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6593 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 6594 | if (lastDoneReceivers.contains(comp)) { |
| 6595 | ris.remove(i); |
| 6596 | i--; |
| 6597 | } |
| 6598 | } |
| 6599 | |
| 6600 | for (int i=0; i<ris.size(); i++) { |
| 6601 | ActivityInfo ai = ris.get(i).activityInfo; |
| 6602 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 6603 | doneReceivers.add(comp); |
| 6604 | intent.setComponent(comp); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6605 | IIntentReceiver finisher = null; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 6606 | if (i == ris.size()-1) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6607 | finisher = new IIntentReceiver.Stub() { |
| 6608 | public void performReceive(Intent intent, int resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 6609 | String data, Bundle extras, boolean ordered, |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 6610 | boolean sticky) { |
| 6611 | // The raw IIntentReceiver interface is called |
| 6612 | // with the AM lock held, so redispatch to |
| 6613 | // execute our code without the lock. |
| 6614 | mHandler.post(new Runnable() { |
| 6615 | public void run() { |
| 6616 | synchronized (ActivityManagerService.this) { |
| 6617 | mDidUpdate = true; |
| 6618 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6619 | writeLastDonePreBootReceivers(doneReceivers); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 6620 | systemReady(goingCallback); |
| 6621 | } |
| 6622 | }); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6623 | } |
| 6624 | }; |
| 6625 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6626 | Slog.i(TAG, "Sending system update to: " + intent.getComponent()); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6627 | broadcastIntentLocked(null, null, intent, null, finisher, |
| 6628 | 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 6629 | if (finisher != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6630 | mWaitingUpdate = true; |
| 6631 | } |
| 6632 | } |
| 6633 | } |
| 6634 | if (mWaitingUpdate) { |
| 6635 | return; |
| 6636 | } |
| 6637 | mDidUpdate = true; |
| 6638 | } |
| 6639 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6640 | mSystemReady = true; |
| 6641 | if (!mStartRunning) { |
| 6642 | return; |
| 6643 | } |
| 6644 | } |
| 6645 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6646 | ArrayList<ProcessRecord> procsToKill = null; |
| 6647 | synchronized(mPidsSelfLocked) { |
| 6648 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 6649 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 6650 | if (!isAllowedWhileBooting(proc.info)){ |
| 6651 | if (procsToKill == null) { |
| 6652 | procsToKill = new ArrayList<ProcessRecord>(); |
| 6653 | } |
| 6654 | procsToKill.add(proc); |
| 6655 | } |
| 6656 | } |
| 6657 | } |
| 6658 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6659 | synchronized(this) { |
| 6660 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6661 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 6662 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6663 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6664 | removeProcessLocked(proc, true); |
| 6665 | } |
| 6666 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6667 | |
| 6668 | // Now that we have cleaned up any update processes, we |
| 6669 | // are ready to start launching real processes and know that |
| 6670 | // we won't trample on them any more. |
| 6671 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6672 | } |
| 6673 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6674 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6675 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6676 | SystemClock.uptimeMillis()); |
| 6677 | |
| 6678 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6679 | // Make sure we have no pre-ready processes sitting around. |
| 6680 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6681 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 6682 | ResolveInfo ri = mContext.getPackageManager() |
| 6683 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6684 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6685 | CharSequence errorMsg = null; |
| 6686 | if (ri != null) { |
| 6687 | ActivityInfo ai = ri.activityInfo; |
| 6688 | ApplicationInfo app = ai.applicationInfo; |
| 6689 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 6690 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 6691 | mTopData = null; |
| 6692 | mTopComponent = new ComponentName(app.packageName, |
| 6693 | ai.name); |
| 6694 | } else { |
| 6695 | errorMsg = mContext.getResources().getText( |
| 6696 | com.android.internal.R.string.factorytest_not_system); |
| 6697 | } |
| 6698 | } else { |
| 6699 | errorMsg = mContext.getResources().getText( |
| 6700 | com.android.internal.R.string.factorytest_no_action); |
| 6701 | } |
| 6702 | if (errorMsg != null) { |
| 6703 | mTopAction = null; |
| 6704 | mTopData = null; |
| 6705 | mTopComponent = null; |
| 6706 | Message msg = Message.obtain(); |
| 6707 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 6708 | msg.getData().putCharSequence("msg", errorMsg); |
| 6709 | mHandler.sendMessage(msg); |
| 6710 | } |
| 6711 | } |
| 6712 | } |
| 6713 | |
| 6714 | retrieveSettings(); |
| 6715 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6716 | if (goingCallback != null) goingCallback.run(); |
| 6717 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6718 | synchronized (this) { |
| 6719 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 6720 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6721 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6722 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6723 | if (apps != null) { |
| 6724 | int N = apps.size(); |
| 6725 | int i; |
| 6726 | for (i=0; i<N; i++) { |
| 6727 | ApplicationInfo info |
| 6728 | = (ApplicationInfo)apps.get(i); |
| 6729 | if (info != null && |
| 6730 | !info.packageName.equals("android")) { |
| 6731 | addAppLocked(info); |
| 6732 | } |
| 6733 | } |
| 6734 | } |
| 6735 | } catch (RemoteException ex) { |
| 6736 | // pm is in same process, this will never happen. |
| 6737 | } |
| 6738 | } |
| 6739 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6740 | // Start up initial activity. |
| 6741 | mBooting = true; |
| 6742 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6743 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6744 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6745 | Message msg = Message.obtain(); |
| 6746 | msg.what = SHOW_UID_ERROR_MSG; |
| 6747 | mHandler.sendMessage(msg); |
| 6748 | } |
| 6749 | } catch (RemoteException e) { |
| 6750 | } |
| 6751 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6752 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6753 | } |
| 6754 | } |
| 6755 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6756 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6757 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6758 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6759 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6760 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6761 | startAppProblemLocked(app); |
| 6762 | app.stopFreezingAllLocked(); |
| 6763 | return handleAppCrashLocked(app); |
| 6764 | } |
| 6765 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6766 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6767 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6768 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6769 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6770 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 6771 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6772 | startAppProblemLocked(app); |
| 6773 | app.stopFreezingAllLocked(); |
| 6774 | } |
| 6775 | |
| 6776 | /** |
| 6777 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 6778 | * |
| 6779 | * @param app The ProcessRecord in which the error occurred. |
| 6780 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 6781 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6782 | * @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] | 6783 | * @param shortMsg Short message describing the crash. |
| 6784 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6785 | * @param stackTrace Full crash stack trace, may be null. |
| 6786 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6787 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 6788 | */ |
| 6789 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6790 | 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] | 6791 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6792 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6793 | report.condition = condition; |
| 6794 | report.processName = app.processName; |
| 6795 | report.pid = app.pid; |
| 6796 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6797 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6798 | report.shortMsg = shortMsg; |
| 6799 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6800 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6801 | |
| 6802 | return report; |
| 6803 | } |
| 6804 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 6805 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6806 | synchronized (this) { |
| 6807 | app.crashing = false; |
| 6808 | app.crashingReport = null; |
| 6809 | app.notResponding = false; |
| 6810 | app.notRespondingReport = null; |
| 6811 | if (app.anrDialog == fromDialog) { |
| 6812 | app.anrDialog = null; |
| 6813 | } |
| 6814 | if (app.waitDialog == fromDialog) { |
| 6815 | app.waitDialog = null; |
| 6816 | } |
| 6817 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 6818 | handleAppCrashLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6819 | Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request"); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 6820 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 6821 | app.processName, app.setAdj, "user's request after error"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6822 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6823 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6824 | } |
| 6825 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 6826 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6827 | private boolean handleAppCrashLocked(ProcessRecord app) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6828 | long now = SystemClock.uptimeMillis(); |
| 6829 | |
| 6830 | Long crashTime = mProcessCrashTimes.get(app.info.processName, |
| 6831 | app.info.uid); |
| 6832 | if (crashTime != null && now < crashTime+MIN_CRASH_INTERVAL) { |
| 6833 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6834 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6835 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6836 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6837 | app.info.processName, app.info.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6838 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 6839 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6840 | if (r.app == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6841 | Slog.w(TAG, " Force finishing activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6842 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6843 | 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] | 6844 | } |
| 6845 | } |
| 6846 | if (!app.persistent) { |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 6847 | // Don't let services in this process be restarted and potentially |
| 6848 | // annoy the user repeatedly. Unless it is persistent, since those |
| 6849 | // processes run critical code. |
| 6850 | killServicesLocked(app, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6851 | // We don't want to start this process again until the user |
| 6852 | // explicitly does so... but for persistent process, we really |
| 6853 | // need to keep it running. If a persistent process is actually |
| 6854 | // repeatedly crashing, then badness for everyone. |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6855 | EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.info.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6856 | app.info.processName); |
| 6857 | mBadProcesses.put(app.info.processName, app.info.uid, now); |
| 6858 | app.bad = true; |
| 6859 | mProcessCrashTimes.remove(app.info.processName, app.info.uid); |
| 6860 | app.removed = true; |
| 6861 | removeProcessLocked(app, false); |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 6862 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6863 | return false; |
| 6864 | } |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 6865 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6866 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6867 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6868 | if (r.app == app) { |
| 6869 | // If the top running activity is from this crashing |
| 6870 | // process, then terminate it to avoid getting in a loop. |
| 6871 | Slog.w(TAG, " Force finishing activity " |
| 6872 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6873 | int index = mMainStack.indexOfTokenLocked(r); |
| 6874 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6875 | Activity.RESULT_CANCELED, null, "crashed"); |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 6876 | // Also terminate any activities below it that aren't yet |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6877 | // stopped, to avoid a situation where one will get |
| 6878 | // re-start our crashing activity once it gets resumed again. |
| 6879 | index--; |
| 6880 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6881 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6882 | if (r.state == ActivityState.RESUMED |
| 6883 | || r.state == ActivityState.PAUSING |
| 6884 | || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 6885 | if (!r.isHomeActivity || mHomeProcess != r.app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6886 | Slog.w(TAG, " Force finishing activity " |
| 6887 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6888 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6889 | Activity.RESULT_CANCELED, null, "crashed"); |
| 6890 | } |
| 6891 | } |
| 6892 | } |
| 6893 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6894 | } |
| 6895 | |
| 6896 | // Bump up the crash count of any services currently running in the proc. |
| 6897 | if (app.services.size() != 0) { |
| 6898 | // Any services running in the application need to be placed |
| 6899 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6900 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6901 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6902 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6903 | sr.crashCount++; |
| 6904 | } |
| 6905 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 6906 | |
| 6907 | // If the crashing process is what we consider to be the "home process" and it has been |
| 6908 | // replaced by a third-party app, clear the package preferred activities from packages |
| 6909 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 6910 | // from blocking the user to manually clear the list. |
| 6911 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 6912 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6913 | Iterator it = mHomeProcess.activities.iterator(); |
| 6914 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 6915 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 6916 | if (r.isHomeActivity) { |
| 6917 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 6918 | try { |
| 6919 | ActivityThread.getPackageManager() |
| 6920 | .clearPackagePreferredActivities(r.packageName); |
| 6921 | } catch (RemoteException c) { |
| 6922 | // pm is in same process, this will never happen. |
| 6923 | } |
| 6924 | } |
| 6925 | } |
| 6926 | } |
| 6927 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6928 | mProcessCrashTimes.put(app.info.processName, app.info.uid, now); |
| 6929 | return true; |
| 6930 | } |
| 6931 | |
| 6932 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 6933 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 6934 | mContext, app.info.packageName, app.info.flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6935 | skipCurrentReceiverLocked(app); |
| 6936 | } |
| 6937 | |
| 6938 | void skipCurrentReceiverLocked(ProcessRecord app) { |
| 6939 | boolean reschedule = false; |
| 6940 | BroadcastRecord r = app.curReceiver; |
| 6941 | if (r != null) { |
| 6942 | // The current broadcast is waiting for this app's receiver |
| 6943 | // to be finished. Looks like that's not going to happen, so |
| 6944 | // let the broadcast continue. |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 6945 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6946 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 6947 | r.resultExtras, r.resultAbort, true); |
| 6948 | reschedule = true; |
| 6949 | } |
| 6950 | r = mPendingBroadcast; |
| 6951 | if (r != null && r.curApp == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6952 | if (DEBUG_BROADCAST) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6953 | "skip & discard pending app " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 6954 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6955 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 6956 | r.resultExtras, r.resultAbort, true); |
| 6957 | reschedule = true; |
| 6958 | } |
| 6959 | if (reschedule) { |
| 6960 | scheduleBroadcastsLocked(); |
| 6961 | } |
| 6962 | } |
| 6963 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6964 | /** |
| 6965 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 6966 | * The application process will exit immediately after this call returns. |
| 6967 | * @param app object of the crashing app, null for the system server |
| 6968 | * @param crashInfo describing the exception |
| 6969 | */ |
| 6970 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 6971 | ProcessRecord r = findAppProcess(app, "Crash"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6972 | |
| 6973 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
| 6974 | app == null ? "system" : (r == null ? "unknown" : r.processName), |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 6975 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6976 | crashInfo.exceptionClassName, |
| 6977 | crashInfo.exceptionMessage, |
| 6978 | crashInfo.throwFileName, |
| 6979 | crashInfo.throwLineNumber); |
| 6980 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 6981 | addErrorToDropBox("crash", r, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6982 | |
| 6983 | crashApplication(r, crashInfo); |
| 6984 | } |
| 6985 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 6986 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 6987 | IBinder app, |
| 6988 | int violationMask, |
| 6989 | StrictMode.ViolationInfo info) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 6990 | ProcessRecord r = findAppProcess(app, "StrictMode"); |
| 6991 | if (r == null) { |
| 6992 | return; |
| 6993 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 6994 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 6995 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 6996 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 6997 | boolean logIt = true; |
| 6998 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 6999 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 7000 | logIt = false; |
| 7001 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7002 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7003 | // the relative pain numbers, without logging all |
| 7004 | // the stack traces repeatedly. We'd want to do |
| 7005 | // likewise in the client code, which also does |
| 7006 | // dup suppression, before the Binder call. |
| 7007 | } else { |
| 7008 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 7009 | mAlreadyLoggedViolatedStacks.clear(); |
| 7010 | } |
| 7011 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 7012 | } |
| 7013 | } |
| 7014 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7015 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7016 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7017 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7018 | |
| 7019 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 7020 | AppErrorResult result = new AppErrorResult(); |
| 7021 | synchronized (this) { |
| 7022 | final long origId = Binder.clearCallingIdentity(); |
| 7023 | |
| 7024 | Message msg = Message.obtain(); |
| 7025 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 7026 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 7027 | data.put("result", result); |
| 7028 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7029 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7030 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7031 | msg.obj = data; |
| 7032 | mHandler.sendMessage(msg); |
| 7033 | |
| 7034 | Binder.restoreCallingIdentity(origId); |
| 7035 | } |
| 7036 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 7037 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7038 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7039 | } |
| 7040 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7041 | // Depending on the policy in effect, there could be a bunch of |
| 7042 | // these in quick succession so we try to batch these together to |
| 7043 | // minimize disk writes, number of dropbox entries, and maximize |
| 7044 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7045 | private void logStrictModeViolationToDropBox( |
| 7046 | ProcessRecord process, |
| 7047 | StrictMode.ViolationInfo info) { |
| 7048 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7049 | return; |
| 7050 | } |
| 7051 | final boolean isSystemApp = process == null || |
| 7052 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 7053 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
| 7054 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 7055 | final DropBoxManager dbox = (DropBoxManager) |
| 7056 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7057 | |
| 7058 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7059 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7060 | |
| 7061 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7062 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7063 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 7064 | synchronized (sb) { |
| 7065 | bufferWasEmpty = sb.length() == 0; |
| 7066 | appendDropBoxProcessHeaders(process, sb); |
| 7067 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 7068 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7069 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 7070 | if (info.violationNumThisLoop != 0) { |
| 7071 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 7072 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 7073 | if (info.numAnimationsRunning != 0) { |
| 7074 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 7075 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 7076 | if (info.broadcastIntentAction != null) { |
| 7077 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 7078 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7079 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7080 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7081 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 7082 | if (info.numInstances != -1) { |
| 7083 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 7084 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7085 | if (info.tags != null) { |
| 7086 | for (String tag : info.tags) { |
| 7087 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 7088 | } |
| 7089 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7090 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7091 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 7092 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7093 | } |
| 7094 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7095 | |
| 7096 | // Only buffer up to ~64k. Various logging bits truncate |
| 7097 | // things at 128k. |
| 7098 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7099 | } |
| 7100 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7101 | // Flush immediately if the buffer's grown too large, or this |
| 7102 | // is a non-system app. Non-system apps are isolated with a |
| 7103 | // different tag & policy and not batched. |
| 7104 | // |
| 7105 | // Batching is useful during internal testing with |
| 7106 | // StrictMode settings turned up high. Without batching, |
| 7107 | // thousands of separate files could be created on boot. |
| 7108 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7109 | new Thread("Error dump: " + dropboxTag) { |
| 7110 | @Override |
| 7111 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7112 | String report; |
| 7113 | synchronized (sb) { |
| 7114 | report = sb.toString(); |
| 7115 | sb.delete(0, sb.length()); |
| 7116 | sb.trimToSize(); |
| 7117 | } |
| 7118 | if (report.length() != 0) { |
| 7119 | dbox.addText(dropboxTag, report); |
| 7120 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7121 | } |
| 7122 | }.start(); |
| 7123 | return; |
| 7124 | } |
| 7125 | |
| 7126 | // System app batching: |
| 7127 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7128 | // An existing dropbox-writing thread is outstanding, so |
| 7129 | // we don't need to start it up. The existing thread will |
| 7130 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7131 | return; |
| 7132 | } |
| 7133 | |
| 7134 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 7135 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7136 | new Thread("Error dump: " + dropboxTag) { |
| 7137 | @Override |
| 7138 | public void run() { |
| 7139 | // 5 second sleep to let stacks arrive and be batched together |
| 7140 | try { |
| 7141 | Thread.sleep(5000); // 5 seconds |
| 7142 | } catch (InterruptedException e) {} |
| 7143 | |
| 7144 | String errorReport; |
| 7145 | synchronized (mStrictModeBuffer) { |
| 7146 | errorReport = mStrictModeBuffer.toString(); |
| 7147 | if (errorReport.length() == 0) { |
| 7148 | return; |
| 7149 | } |
| 7150 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 7151 | mStrictModeBuffer.trimToSize(); |
| 7152 | } |
| 7153 | dbox.addText(dropboxTag, errorReport); |
| 7154 | } |
| 7155 | }.start(); |
| 7156 | } |
| 7157 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7158 | /** |
| 7159 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 7160 | * @param app object of the crashing app, null for the system server |
| 7161 | * @param tag reported by the caller |
| 7162 | * @param crashInfo describing the context of the error |
| 7163 | * @return true if the process should exit immediately (WTF is fatal) |
| 7164 | */ |
| 7165 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7166 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7167 | ProcessRecord r = findAppProcess(app, "WTF"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7168 | |
| 7169 | EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(), |
| 7170 | app == null ? "system" : (r == null ? "unknown" : r.processName), |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7171 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7172 | tag, crashInfo.exceptionMessage); |
| 7173 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7174 | addErrorToDropBox("wtf", r, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7175 | |
Dianne Hackborn | 1ab4377 | 2011-03-15 14:38:02 -0700 | [diff] [blame] | 7176 | if (r != null && r.pid != Process.myPid() && |
| 7177 | Settings.Secure.getInt(mContext.getContentResolver(), |
| 7178 | Settings.Secure.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7179 | crashApplication(r, crashInfo); |
| 7180 | return true; |
| 7181 | } else { |
| 7182 | return false; |
| 7183 | } |
| 7184 | } |
| 7185 | |
| 7186 | /** |
| 7187 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 7188 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 7189 | */ |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7190 | private ProcessRecord findAppProcess(IBinder app, String reason) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7191 | if (app == null) { |
| 7192 | return null; |
| 7193 | } |
| 7194 | |
| 7195 | synchronized (this) { |
| 7196 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 7197 | final int NA = apps.size(); |
| 7198 | for (int ia=0; ia<NA; ia++) { |
| 7199 | ProcessRecord p = apps.valueAt(ia); |
| 7200 | if (p.thread != null && p.thread.asBinder() == app) { |
| 7201 | return p; |
| 7202 | } |
| 7203 | } |
| 7204 | } |
| 7205 | |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7206 | Slog.w(TAG, "Can't find mystery application for " + reason |
| 7207 | + " from pid=" + Binder.getCallingPid() |
| 7208 | + " uid=" + Binder.getCallingUid() + ": " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7209 | return null; |
| 7210 | } |
| 7211 | } |
| 7212 | |
| 7213 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7214 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 7215 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7216 | */ |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 7217 | private void appendDropBoxProcessHeaders(ProcessRecord process, StringBuilder sb) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 7218 | // Watchdog thread ends up invoking this function (with |
| 7219 | // a null ProcessRecord) to add the stack file to dropbox. |
| 7220 | // Do not acquire a lock on this (am) in such cases, as it |
| 7221 | // could cause a potential deadlock, if and when watchdog |
| 7222 | // is invoked due to unavailability of lock on am and it |
| 7223 | // would prevent watchdog from killing system_server. |
| 7224 | if (process == null) { |
| 7225 | sb.append("Process: system_server\n"); |
| 7226 | return; |
| 7227 | } |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 7228 | // Note: ProcessRecord 'process' is guarded by the service |
| 7229 | // instance. (notably process.pkgList, which could otherwise change |
| 7230 | // concurrently during execution of this method) |
| 7231 | synchronized (this) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 7232 | if (process.pid == MY_PID) { |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 7233 | sb.append("Process: system_server\n"); |
| 7234 | } else { |
| 7235 | sb.append("Process: ").append(process.processName).append("\n"); |
| 7236 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7237 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7238 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7239 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 7240 | for (String pkg : process.pkgList) { |
| 7241 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7242 | try { |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7243 | PackageInfo pi = pm.getPackageInfo(pkg, 0); |
| 7244 | if (pi != null) { |
| 7245 | sb.append(" v").append(pi.versionCode); |
| 7246 | if (pi.versionName != null) { |
| 7247 | sb.append(" (").append(pi.versionName).append(")"); |
| 7248 | } |
| 7249 | } |
| 7250 | } catch (RemoteException e) { |
| 7251 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7252 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7253 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7254 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7255 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7256 | } |
| 7257 | |
| 7258 | private static String processClass(ProcessRecord process) { |
| 7259 | if (process == null || process.pid == MY_PID) { |
| 7260 | return "system_server"; |
| 7261 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7262 | return "system_app"; |
| 7263 | } else { |
| 7264 | return "data_app"; |
| 7265 | } |
| 7266 | } |
| 7267 | |
| 7268 | /** |
| 7269 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 7270 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 7271 | * @param process which caused the error, null means the system server |
| 7272 | * @param activity which triggered the error, null if unknown |
| 7273 | * @param parent activity related to the error, null if unknown |
| 7274 | * @param subject line related to the error, null if absent |
| 7275 | * @param report in long form describing the error, null if absent |
| 7276 | * @param logFile to include in the report, null if none |
| 7277 | * @param crashInfo giving an application stack trace, null if absent |
| 7278 | */ |
| 7279 | public void addErrorToDropBox(String eventType, |
| 7280 | ProcessRecord process, ActivityRecord activity, ActivityRecord parent, String subject, |
| 7281 | final String report, final File logFile, |
| 7282 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 7283 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 7284 | // otherwise the watchdog may be prevented from resetting the system. |
| 7285 | |
| 7286 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 7287 | final DropBoxManager dbox = (DropBoxManager) |
| 7288 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7289 | |
| 7290 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7291 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7292 | |
| 7293 | final StringBuilder sb = new StringBuilder(1024); |
| 7294 | appendDropBoxProcessHeaders(process, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7295 | if (activity != null) { |
| 7296 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 7297 | } |
| 7298 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 7299 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 7300 | } |
| 7301 | if (parent != null && parent != activity) { |
| 7302 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 7303 | } |
| 7304 | if (subject != null) { |
| 7305 | sb.append("Subject: ").append(subject).append("\n"); |
| 7306 | } |
| 7307 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 7308 | if (Debug.isDebuggerConnected()) { |
| 7309 | sb.append("Debugger: Connected\n"); |
| 7310 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7311 | sb.append("\n"); |
| 7312 | |
| 7313 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 7314 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7315 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 7316 | @Override |
| 7317 | public void run() { |
| 7318 | if (report != null) { |
| 7319 | sb.append(report); |
| 7320 | } |
| 7321 | if (logFile != null) { |
| 7322 | try { |
| 7323 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 7324 | } catch (IOException e) { |
| 7325 | Slog.e(TAG, "Error reading " + logFile, e); |
| 7326 | } |
| 7327 | } |
| 7328 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 7329 | sb.append(crashInfo.stackTrace); |
| 7330 | } |
| 7331 | |
| 7332 | String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 7333 | int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0); |
| 7334 | if (lines > 0) { |
| 7335 | sb.append("\n"); |
| 7336 | |
| 7337 | // Merge several logcat streams, and take the last N lines |
| 7338 | InputStreamReader input = null; |
| 7339 | try { |
| 7340 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 7341 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 7342 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 7343 | |
| 7344 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 7345 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 7346 | input = new InputStreamReader(logcat.getInputStream()); |
| 7347 | |
| 7348 | int num; |
| 7349 | char[] buf = new char[8192]; |
| 7350 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 7351 | } catch (IOException e) { |
| 7352 | Slog.e(TAG, "Error running logcat", e); |
| 7353 | } finally { |
| 7354 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 7355 | } |
| 7356 | } |
| 7357 | |
| 7358 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7359 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7360 | }; |
| 7361 | |
| 7362 | if (process == null || process.pid == MY_PID) { |
| 7363 | worker.run(); // We may be about to die -- need to run this synchronously |
| 7364 | } else { |
| 7365 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7366 | } |
| 7367 | } |
| 7368 | |
| 7369 | /** |
| 7370 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 7371 | * Deal with edge cases (intercepts from instrumented applications, |
| 7372 | * ActivityController, error intent receivers, that sort of thing). |
| 7373 | * @param r the application crashing |
| 7374 | * @param crashInfo describing the failure |
| 7375 | */ |
| 7376 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7377 | long timeMillis = System.currentTimeMillis(); |
| 7378 | String shortMsg = crashInfo.exceptionClassName; |
| 7379 | String longMsg = crashInfo.exceptionMessage; |
| 7380 | String stackTrace = crashInfo.stackTrace; |
| 7381 | if (shortMsg != null && longMsg != null) { |
| 7382 | longMsg = shortMsg + ": " + longMsg; |
| 7383 | } else if (shortMsg != null) { |
| 7384 | longMsg = shortMsg; |
| 7385 | } |
| 7386 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7387 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7388 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7389 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7390 | try { |
| 7391 | String name = r != null ? r.processName : null; |
| 7392 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7393 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7394 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7395 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7396 | + " at watcher's request"); |
| 7397 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7398 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7399 | } |
| 7400 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7401 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7402 | } |
| 7403 | } |
| 7404 | |
| 7405 | final long origId = Binder.clearCallingIdentity(); |
| 7406 | |
| 7407 | // If this process is running instrumentation, finish it. |
| 7408 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7409 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7410 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7411 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 7412 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7413 | Bundle info = new Bundle(); |
| 7414 | info.putString("shortMsg", shortMsg); |
| 7415 | info.putString("longMsg", longMsg); |
| 7416 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 7417 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7418 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7419 | } |
| 7420 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7421 | // If we can't identify the process or it's already exceeded its crash quota, |
| 7422 | // quit right away without showing a crash dialog. |
| 7423 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7424 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7425 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7426 | } |
| 7427 | |
| 7428 | Message msg = Message.obtain(); |
| 7429 | msg.what = SHOW_ERROR_MSG; |
| 7430 | HashMap data = new HashMap(); |
| 7431 | data.put("result", result); |
| 7432 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7433 | msg.obj = data; |
| 7434 | mHandler.sendMessage(msg); |
| 7435 | |
| 7436 | Binder.restoreCallingIdentity(origId); |
| 7437 | } |
| 7438 | |
| 7439 | int res = result.get(); |
| 7440 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7441 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7442 | synchronized (this) { |
| 7443 | if (r != null) { |
| 7444 | mProcessCrashTimes.put(r.info.processName, r.info.uid, |
| 7445 | SystemClock.uptimeMillis()); |
| 7446 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7447 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7448 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7449 | } |
| 7450 | } |
| 7451 | |
| 7452 | if (appErrorIntent != null) { |
| 7453 | try { |
| 7454 | mContext.startActivity(appErrorIntent); |
| 7455 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7456 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7457 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7458 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7459 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7460 | |
| 7461 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 7462 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 7463 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7464 | if (report == null) { |
| 7465 | return null; |
| 7466 | } |
| 7467 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 7468 | result.setComponent(r.errorReportReceiver); |
| 7469 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 7470 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 7471 | return result; |
| 7472 | } |
| 7473 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7474 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 7475 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7476 | if (r.errorReportReceiver == null) { |
| 7477 | return null; |
| 7478 | } |
| 7479 | |
| 7480 | if (!r.crashing && !r.notResponding) { |
| 7481 | return null; |
| 7482 | } |
| 7483 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7484 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 7485 | report.packageName = r.info.packageName; |
| 7486 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 7487 | report.processName = r.processName; |
| 7488 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 7489 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7490 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7491 | if (r.crashing) { |
| 7492 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 7493 | report.crashInfo = crashInfo; |
| 7494 | } else if (r.notResponding) { |
| 7495 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 7496 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7497 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7498 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 7499 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 7500 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7501 | } |
| 7502 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7503 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7504 | } |
| 7505 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7506 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
| 7507 | // assume our apps are happy - lazy create the list |
| 7508 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 7509 | |
| 7510 | synchronized (this) { |
| 7511 | |
| 7512 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7513 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 7514 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7515 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 7516 | // This one's in trouble, so we'll generate a report for it |
| 7517 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 7518 | ActivityManager.ProcessErrorStateInfo report = null; |
| 7519 | if (app.crashing) { |
| 7520 | report = app.crashingReport; |
| 7521 | } else if (app.notResponding) { |
| 7522 | report = app.notRespondingReport; |
| 7523 | } |
| 7524 | |
| 7525 | if (report != null) { |
| 7526 | if (errList == null) { |
| 7527 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 7528 | } |
| 7529 | errList.add(report); |
| 7530 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7531 | 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] | 7532 | " crashing = " + app.crashing + |
| 7533 | " notResponding = " + app.notResponding); |
| 7534 | } |
| 7535 | } |
| 7536 | } |
| 7537 | } |
| 7538 | |
| 7539 | return errList; |
| 7540 | } |
| 7541 | |
| 7542 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
| 7543 | // Lazy instantiation of list |
| 7544 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
| 7545 | synchronized (this) { |
| 7546 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7547 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 7548 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7549 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 7550 | // Generate process state info for running application |
| 7551 | ActivityManager.RunningAppProcessInfo currApp = |
| 7552 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 7553 | app.pid, app.getPackageList()); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 7554 | currApp.uid = app.info.uid; |
Dianne Hackborn | baf42c6 | 2010-06-24 11:23:39 -0700 | [diff] [blame] | 7555 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 482566e | 2010-09-03 12:51:28 -0700 | [diff] [blame] | 7556 | currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
Dianne Hackborn | baf42c6 | 2010-06-24 11:23:39 -0700 | [diff] [blame] | 7557 | } |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 7558 | if (app.persistent) { |
| 7559 | currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 7560 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7561 | int adj = app.curAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7562 | if (adj >= EMPTY_APP_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7563 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_EMPTY; |
| 7564 | } else if (adj >= HIDDEN_APP_MIN_ADJ) { |
| 7565 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 7566 | currApp.lru = adj - HIDDEN_APP_MIN_ADJ + 1; |
| 7567 | } else if (adj >= HOME_APP_ADJ) { |
| 7568 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 7569 | currApp.lru = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7570 | } else if (adj >= SECONDARY_SERVER_ADJ) { |
| 7571 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 7572 | } else if (adj >= HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | 5383f50 | 2010-10-22 12:59:20 -0700 | [diff] [blame] | 7573 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 7574 | } else if (adj >= PERCEPTIBLE_APP_ADJ) { |
| 7575 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7576 | } else if (adj >= VISIBLE_APP_ADJ) { |
| 7577 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 7578 | } else { |
| 7579 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 7580 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 7581 | currApp.importanceReasonCode = app.adjTypeCode; |
| 7582 | if (app.adjSource instanceof ProcessRecord) { |
| 7583 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7584 | } else if (app.adjSource instanceof ActivityRecord) { |
| 7585 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 7586 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 7587 | } |
| 7588 | if (app.adjTarget instanceof ComponentName) { |
| 7589 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 7590 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7591 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7592 | // + " lru=" + currApp.lru); |
| 7593 | if (runList == null) { |
| 7594 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 7595 | } |
| 7596 | runList.add(currApp); |
| 7597 | } |
| 7598 | } |
| 7599 | } |
| 7600 | return runList; |
| 7601 | } |
| 7602 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7603 | public List<ApplicationInfo> getRunningExternalApplications() { |
| 7604 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 7605 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 7606 | if (runningApps != null && runningApps.size() > 0) { |
| 7607 | Set<String> extList = new HashSet<String>(); |
| 7608 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 7609 | if (app.pkgList != null) { |
| 7610 | for (String pkg : app.pkgList) { |
| 7611 | extList.add(pkg); |
| 7612 | } |
| 7613 | } |
| 7614 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7615 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7616 | for (String pkg : extList) { |
| 7617 | try { |
| 7618 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0); |
| 7619 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 7620 | retList.add(info); |
| 7621 | } |
| 7622 | } catch (RemoteException e) { |
| 7623 | } |
| 7624 | } |
| 7625 | } |
| 7626 | return retList; |
| 7627 | } |
| 7628 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7629 | @Override |
| 7630 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7631 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 7632 | != PackageManager.PERMISSION_GRANTED) { |
| 7633 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 7634 | + Binder.getCallingPid() |
| 7635 | + ", uid=" + Binder.getCallingUid() |
| 7636 | + " without permission " |
| 7637 | + android.Manifest.permission.DUMP); |
| 7638 | return; |
| 7639 | } |
| 7640 | |
| 7641 | boolean dumpAll = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7642 | boolean dumpClient = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7643 | |
| 7644 | int opti = 0; |
| 7645 | while (opti < args.length) { |
| 7646 | String opt = args[opti]; |
| 7647 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 7648 | break; |
| 7649 | } |
| 7650 | opti++; |
| 7651 | if ("-a".equals(opt)) { |
| 7652 | dumpAll = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7653 | } else if ("-c".equals(opt)) { |
| 7654 | dumpClient = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7655 | } else if ("-h".equals(opt)) { |
| 7656 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7657 | pw.println(" [-a] [-c] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7658 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7659 | pw.println(" a[ctivities]: activity stack state"); |
| 7660 | pw.println(" b[roadcasts]: broadcast state"); |
| 7661 | pw.println(" i[ntents]: pending intent state"); |
| 7662 | pw.println(" p[rocesses]: process state"); |
| 7663 | pw.println(" o[om]: out of memory management"); |
| 7664 | pw.println(" prov[iders]: content provider state"); |
| 7665 | pw.println(" s[ervices]: service state"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7666 | pw.println(" service [COMP_SPEC]: service client-side state"); |
| 7667 | pw.println(" cmd may also be a COMP_SPEC to dump activities."); |
| 7668 | pw.println(" COMP_SPEC may also be a component name (com.foo/.myApp),"); |
| 7669 | pw.println(" a partial substring in a component name, an"); |
| 7670 | pw.println(" ActivityRecord hex object identifier, or"); |
| 7671 | pw.println(" \"all\" for all objects"); |
| 7672 | pw.println(" -a: include all available server state."); |
| 7673 | pw.println(" -c: include client state."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7674 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7675 | } else { |
| 7676 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7677 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7678 | } |
| 7679 | |
| 7680 | // Is the caller requesting to dump a particular piece of data? |
| 7681 | if (opti < args.length) { |
| 7682 | String cmd = args[opti]; |
| 7683 | opti++; |
| 7684 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 7685 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7686 | dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7687 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7688 | return; |
| 7689 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
| 7690 | synchronized (this) { |
| 7691 | dumpBroadcastsLocked(fd, pw, args, opti, true); |
| 7692 | } |
| 7693 | return; |
| 7694 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
| 7695 | synchronized (this) { |
| 7696 | dumpPendingIntentsLocked(fd, pw, args, opti, true); |
| 7697 | } |
| 7698 | return; |
| 7699 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
| 7700 | synchronized (this) { |
| 7701 | dumpProcessesLocked(fd, pw, args, opti, true); |
| 7702 | } |
| 7703 | return; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7704 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 7705 | synchronized (this) { |
| 7706 | dumpOomLocked(fd, pw, args, opti, true); |
| 7707 | } |
| 7708 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7709 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 7710 | synchronized (this) { |
| 7711 | dumpProvidersLocked(fd, pw, args, opti, true); |
| 7712 | } |
| 7713 | return; |
| 7714 | } else if ("service".equals(cmd)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7715 | String[] newArgs; |
| 7716 | String name; |
| 7717 | if (opti >= args.length) { |
| 7718 | name = null; |
| 7719 | newArgs = EMPTY_STRING_ARRAY; |
| 7720 | } else { |
| 7721 | name = args[opti]; |
| 7722 | opti++; |
| 7723 | newArgs = new String[args.length - opti]; |
| 7724 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 7725 | } |
| 7726 | if (!dumpService(fd, pw, name, newArgs, 0, dumpAll)) { |
| 7727 | pw.println("No services match: " + name); |
| 7728 | pw.println("Use -h for help."); |
| 7729 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7730 | return; |
| 7731 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 7732 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7733 | dumpServicesLocked(fd, pw, args, opti, true, dumpClient); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7734 | } |
| 7735 | return; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 7736 | } else { |
| 7737 | // Dumping a single activity? |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7738 | if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 7739 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 7740 | pw.println("Use -h for help."); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 7741 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 7742 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7743 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7744 | } |
| 7745 | |
| 7746 | // No piece of data specified, dump everything. |
| 7747 | synchronized (this) { |
| 7748 | boolean needSep; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7749 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7750 | if (needSep) { |
| 7751 | pw.println(" "); |
| 7752 | } |
| 7753 | if (dumpAll) { |
| 7754 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7755 | } |
| 7756 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll); |
| 7757 | if (needSep) { |
| 7758 | pw.println(" "); |
| 7759 | } |
| 7760 | if (dumpAll) { |
| 7761 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7762 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7763 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7764 | if (needSep) { |
| 7765 | pw.println(" "); |
| 7766 | } |
| 7767 | if (dumpAll) { |
| 7768 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7769 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7770 | needSep = dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7771 | if (needSep) { |
| 7772 | pw.println(" "); |
| 7773 | } |
| 7774 | if (dumpAll) { |
| 7775 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7776 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7777 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7778 | if (needSep) { |
| 7779 | pw.println(" "); |
| 7780 | } |
| 7781 | if (dumpAll) { |
| 7782 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7783 | } |
| 7784 | dumpProcessesLocked(fd, pw, args, opti, dumpAll); |
| 7785 | } |
| 7786 | } |
| 7787 | |
| 7788 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7789 | int opti, boolean dumpAll, boolean dumpClient) { |
| 7790 | pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)"); |
| 7791 | pw.println(" Main stack:"); |
| 7792 | dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7793 | pw.println(" "); |
| 7794 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7795 | dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7796 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7797 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7798 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7799 | dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false, |
| 7800 | !dumpAll, false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7801 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7802 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7803 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7804 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7805 | dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false, |
| 7806 | !dumpAll, false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7807 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7808 | if (mMainStack.mGoingToSleepActivities.size() > 0) { |
| 7809 | pw.println(" "); |
| 7810 | pw.println(" Activities waiting to sleep:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7811 | dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false, |
| 7812 | !dumpAll, false); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7813 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7814 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7815 | pw.println(" "); |
| 7816 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7817 | dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false, |
| 7818 | !dumpAll, false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7819 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7820 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7821 | pw.println(" "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7822 | if (mMainStack.mPausingActivity != null) { |
| 7823 | pw.println(" mPausingActivity: " + mMainStack.mPausingActivity); |
| 7824 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7825 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7826 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7827 | if (dumpAll) { |
| 7828 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
| 7829 | pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout); |
| 7830 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7831 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7832 | if (mRecentTasks.size() > 0) { |
| 7833 | pw.println(); |
| 7834 | pw.println(" Recent tasks:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7835 | |
| 7836 | final int N = mRecentTasks.size(); |
| 7837 | for (int i=0; i<N; i++) { |
| 7838 | TaskRecord tr = mRecentTasks.get(i); |
| 7839 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 7840 | pw.println(tr); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7841 | if (dumpAll) { |
| 7842 | mRecentTasks.get(i).dump(pw, " "); |
| 7843 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7844 | } |
| 7845 | } |
| 7846 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7847 | if (dumpAll) { |
| 7848 | pw.println(" "); |
| 7849 | pw.println(" mCurTask: " + mCurTask); |
| 7850 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7851 | |
| 7852 | return true; |
| 7853 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7854 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7855 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 7856 | int opti, boolean dumpAll) { |
| 7857 | boolean needSep = false; |
| 7858 | int numPers = 0; |
| 7859 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7860 | pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)"); |
| 7861 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7862 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7863 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 7864 | final int NA = procs.size(); |
| 7865 | for (int ia=0; ia<NA; ia++) { |
| 7866 | if (!needSep) { |
| 7867 | pw.println(" All known processes:"); |
| 7868 | needSep = true; |
| 7869 | } |
| 7870 | ProcessRecord r = procs.valueAt(ia); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 7871 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 7872 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 7873 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7874 | r.dump(pw, " "); |
| 7875 | if (r.persistent) { |
| 7876 | numPers++; |
| 7877 | } |
| 7878 | } |
| 7879 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7880 | } |
| 7881 | |
| 7882 | if (mLruProcesses.size() > 0) { |
| 7883 | if (needSep) pw.println(" "); |
| 7884 | needSep = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7885 | pw.println(" Process LRU list (most recent first):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7886 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
| 7887 | "Proc", "PERS", false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7888 | needSep = true; |
| 7889 | } |
| 7890 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7891 | if (dumpAll) { |
| 7892 | synchronized (mPidsSelfLocked) { |
| 7893 | if (mPidsSelfLocked.size() > 0) { |
| 7894 | if (needSep) pw.println(" "); |
| 7895 | needSep = true; |
| 7896 | pw.println(" PID mappings:"); |
| 7897 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 7898 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 7899 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
| 7900 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7901 | } |
| 7902 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7903 | } |
| 7904 | |
| 7905 | if (mForegroundProcesses.size() > 0) { |
| 7906 | if (needSep) pw.println(" "); |
| 7907 | needSep = true; |
| 7908 | pw.println(" Foreground Processes:"); |
| 7909 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 7910 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 7911 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7912 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7913 | } |
| 7914 | |
| 7915 | if (mPersistentStartingProcesses.size() > 0) { |
| 7916 | if (needSep) pw.println(" "); |
| 7917 | needSep = true; |
| 7918 | pw.println(" Persisent processes that are starting:"); |
| 7919 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7920 | "Starting Norm", "Restarting PERS"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7921 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7922 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7923 | if (mStartingProcesses.size() > 0) { |
| 7924 | if (needSep) pw.println(" "); |
| 7925 | needSep = true; |
| 7926 | pw.println(" Processes that are starting:"); |
| 7927 | dumpProcessList(pw, this, mStartingProcesses, " ", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7928 | "Starting Norm", "Starting PERS"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7929 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7930 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7931 | if (mRemovedProcesses.size() > 0) { |
| 7932 | if (needSep) pw.println(" "); |
| 7933 | needSep = true; |
| 7934 | pw.println(" Processes that are being removed:"); |
| 7935 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7936 | "Removed Norm", "Removed PERS"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7937 | } |
| 7938 | |
| 7939 | if (mProcessesOnHold.size() > 0) { |
| 7940 | if (needSep) pw.println(" "); |
| 7941 | needSep = true; |
| 7942 | pw.println(" Processes that are on old until the system is ready:"); |
| 7943 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7944 | "OnHold Norm", "OnHold PERS"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7945 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7946 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7947 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7948 | |
| 7949 | if (mProcessCrashTimes.getMap().size() > 0) { |
| 7950 | if (needSep) pw.println(" "); |
| 7951 | needSep = true; |
| 7952 | pw.println(" Time since processes crashed:"); |
| 7953 | long now = SystemClock.uptimeMillis(); |
| 7954 | for (Map.Entry<String, SparseArray<Long>> procs |
| 7955 | : mProcessCrashTimes.getMap().entrySet()) { |
| 7956 | SparseArray<Long> uids = procs.getValue(); |
| 7957 | final int N = uids.size(); |
| 7958 | for (int i=0; i<N; i++) { |
| 7959 | pw.print(" Process "); pw.print(procs.getKey()); |
| 7960 | pw.print(" uid "); pw.print(uids.keyAt(i)); |
| 7961 | pw.print(": last crashed "); |
| 7962 | pw.print((now-uids.valueAt(i))); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 7963 | pw.println(" ms ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 7964 | } |
| 7965 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7966 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7967 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7968 | if (mBadProcesses.getMap().size() > 0) { |
| 7969 | if (needSep) pw.println(" "); |
| 7970 | needSep = true; |
| 7971 | pw.println(" Bad processes:"); |
| 7972 | for (Map.Entry<String, SparseArray<Long>> procs |
| 7973 | : mBadProcesses.getMap().entrySet()) { |
| 7974 | SparseArray<Long> uids = procs.getValue(); |
| 7975 | final int N = uids.size(); |
| 7976 | for (int i=0; i<N; i++) { |
| 7977 | pw.print(" Bad process "); pw.print(procs.getKey()); |
| 7978 | pw.print(" uid "); pw.print(uids.keyAt(i)); |
| 7979 | pw.print(": crashed at time "); |
| 7980 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7981 | } |
| 7982 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7983 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7984 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7985 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7986 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7987 | if (mHeavyWeightProcess != null) { |
| 7988 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 7989 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7990 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7991 | if (dumpAll) { |
| 7992 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | 3d0724d | 2011-05-12 15:39:41 -0700 | [diff] [blame] | 7993 | if (mCompatModePackages.getPackages().size() > 0) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 7994 | pw.println(" mScreenCompatPackages:"); |
| 7995 | for (Map.Entry<String, Integer> entry |
| 7996 | : mCompatModePackages.getPackages().entrySet()) { |
| 7997 | String pkg = entry.getKey(); |
| 7998 | int mode = entry.getValue(); |
| 7999 | pw.print(" "); pw.print(pkg); pw.print(": "); |
| 8000 | pw.print(mode); pw.println(); |
| 8001 | } |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 8002 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8003 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8004 | pw.println(" mSleeping=" + mSleeping + " mShuttingDown=" + mShuttingDown); |
| 8005 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 8006 | || mOrigWaitForDebugger) { |
| 8007 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 8008 | + " mDebugTransient=" + mDebugTransient |
| 8009 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 8010 | } |
| 8011 | if (mAlwaysFinishActivities || mController != null) { |
| 8012 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 8013 | + " mController=" + mController); |
| 8014 | } |
| 8015 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8016 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8017 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 8018 | + " mProcessesReady=" + mProcessesReady |
| 8019 | + " mSystemReady=" + mSystemReady); |
| 8020 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8021 | + " mBooted=" + mBooted |
| 8022 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8023 | pw.print(" mLastPowerCheckRealtime="); |
| 8024 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 8025 | pw.println(""); |
| 8026 | pw.print(" mLastPowerCheckUptime="); |
| 8027 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 8028 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8029 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 8030 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 8031 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8032 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8033 | |
| 8034 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8035 | } |
| 8036 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8037 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8038 | int opti, boolean needSep, boolean dumpAll) { |
| 8039 | if (mProcessesToGc.size() > 0) { |
| 8040 | if (needSep) pw.println(" "); |
| 8041 | needSep = true; |
| 8042 | pw.println(" Processes that are waiting to GC:"); |
| 8043 | long now = SystemClock.uptimeMillis(); |
| 8044 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 8045 | ProcessRecord proc = mProcessesToGc.get(i); |
| 8046 | pw.print(" Process "); pw.println(proc); |
| 8047 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 8048 | pw.print(", last gced="); |
| 8049 | pw.print(now-proc.lastRequestedGc); |
| 8050 | pw.print(" ms ago, last lowMem="); |
| 8051 | pw.print(now-proc.lastLowMemory); |
| 8052 | pw.println(" ms ago"); |
| 8053 | |
| 8054 | } |
| 8055 | } |
| 8056 | return needSep; |
| 8057 | } |
| 8058 | |
| 8059 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8060 | int opti, boolean dumpAll) { |
| 8061 | boolean needSep = false; |
| 8062 | |
| 8063 | if (mLruProcesses.size() > 0) { |
| 8064 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 8065 | |
| 8066 | Comparator<ProcessRecord> comparator = new Comparator<ProcessRecord>() { |
| 8067 | @Override |
| 8068 | public int compare(ProcessRecord object1, ProcessRecord object2) { |
| 8069 | if (object1.setAdj != object2.setAdj) { |
| 8070 | return object1.setAdj > object2.setAdj ? -1 : 1; |
| 8071 | } |
| 8072 | if (object1.setSchedGroup != object2.setSchedGroup) { |
| 8073 | return object1.setSchedGroup > object2.setSchedGroup ? -1 : 1; |
| 8074 | } |
| 8075 | if (object1.keeping != object2.keeping) { |
| 8076 | return object1.keeping ? -1 : 1; |
| 8077 | } |
| 8078 | if (object1.pid != object2.pid) { |
| 8079 | return object1.pid > object2.pid ? -1 : 1; |
| 8080 | } |
| 8081 | return 0; |
| 8082 | } |
| 8083 | }; |
| 8084 | |
| 8085 | Collections.sort(procs, comparator); |
| 8086 | |
| 8087 | if (needSep) pw.println(" "); |
| 8088 | needSep = true; |
| 8089 | pw.println(" Process OOM control:"); |
| 8090 | dumpProcessOomList(pw, this, procs, " ", |
| 8091 | "Proc", "PERS", true); |
| 8092 | needSep = true; |
| 8093 | } |
| 8094 | |
| 8095 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll); |
| 8096 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8097 | pw.println(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8098 | pw.println(" mHomeProcess: " + mHomeProcess); |
| 8099 | if (mHeavyWeightProcess != null) { |
| 8100 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 8101 | } |
| 8102 | |
| 8103 | return true; |
| 8104 | } |
| 8105 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8106 | /** |
| 8107 | * There are three ways to call this: |
| 8108 | * - no service specified: dump all the services |
| 8109 | * - a flattened component name that matched an existing service was specified as the |
| 8110 | * first arg: dump that one service |
| 8111 | * - the first arg isn't the flattened component name of an existing service: |
| 8112 | * dump all services whose component contains the first arg as a substring |
| 8113 | */ |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8114 | protected boolean dumpService(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 8115 | int opti, boolean dumpAll) { |
| 8116 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8117 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8118 | if ("all".equals(name)) { |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 8119 | synchronized (this) { |
| 8120 | for (ServiceRecord r1 : mServices.values()) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8121 | services.add(r1); |
| 8122 | } |
| 8123 | } |
| 8124 | } else { |
| 8125 | ComponentName componentName = name != null |
| 8126 | ? ComponentName.unflattenFromString(name) : null; |
| 8127 | int objectId = 0; |
| 8128 | if (componentName == null) { |
| 8129 | // Not a '/' separated full component name; maybe an object ID? |
| 8130 | try { |
| 8131 | objectId = Integer.parseInt(name, 16); |
| 8132 | name = null; |
| 8133 | componentName = null; |
| 8134 | } catch (RuntimeException e) { |
| 8135 | } |
| 8136 | } |
| 8137 | |
| 8138 | synchronized (this) { |
| 8139 | for (ServiceRecord r1 : mServices.values()) { |
| 8140 | if (componentName != null) { |
| 8141 | if (r1.name.equals(componentName)) { |
| 8142 | services.add(r1); |
| 8143 | } |
| 8144 | } else if (name != null) { |
| 8145 | if (r1.name.flattenToString().contains(name)) { |
| 8146 | services.add(r1); |
| 8147 | } |
| 8148 | } else if (System.identityHashCode(r1) == objectId) { |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 8149 | services.add(r1); |
| 8150 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8151 | } |
| 8152 | } |
| 8153 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8154 | |
| 8155 | if (services.size() <= 0) { |
| 8156 | return false; |
| 8157 | } |
| 8158 | |
| 8159 | boolean needSep = false; |
| 8160 | for (int i=0; i<services.size(); i++) { |
| 8161 | if (needSep) { |
| 8162 | pw.println(); |
| 8163 | } |
| 8164 | needSep = true; |
| 8165 | dumpService("", fd, pw, services.get(i), args, dumpAll); |
| 8166 | } |
| 8167 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8168 | } |
| 8169 | |
| 8170 | /** |
| 8171 | * Invokes IApplicationThread.dumpService() on the thread of the specified service if |
| 8172 | * there is a thread associated with the service. |
| 8173 | */ |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8174 | private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw, |
| 8175 | final ServiceRecord r, String[] args, boolean dumpAll) { |
| 8176 | String innerPrefix = prefix + " "; |
| 8177 | synchronized (this) { |
| 8178 | pw.print(prefix); pw.print("SERVICE "); |
| 8179 | pw.print(r.shortName); pw.print(" "); |
| 8180 | pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 8181 | pw.print(" pid="); |
| 8182 | if (r.app != null) pw.println(r.app.pid); |
| 8183 | else pw.println("(not running)"); |
| 8184 | if (dumpAll) { |
| 8185 | r.dump(pw, innerPrefix); |
| 8186 | } |
| 8187 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8188 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8189 | pw.print(prefix); pw.println(" Client:"); |
| 8190 | pw.flush(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8191 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8192 | TransferPipe tp = new TransferPipe(); |
| 8193 | try { |
| 8194 | r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), r, args); |
| 8195 | tp.setBufferPrefix(prefix + " "); |
| 8196 | tp.go(fd); |
| 8197 | } finally { |
| 8198 | tp.kill(); |
| 8199 | } |
| 8200 | } catch (IOException e) { |
| 8201 | pw.println(prefix + " Failure while dumping the service: " + e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8202 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8203 | 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] | 8204 | } |
| 8205 | } |
| 8206 | } |
| 8207 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8208 | /** |
| 8209 | * There are three things that cmd can be: |
| 8210 | * - a flattened component name that matched an existing activity |
| 8211 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 8212 | * dump all activity whose component contains the cmd as a substring |
| 8213 | * - A hex number of the ActivityRecord object instance. |
| 8214 | */ |
| 8215 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 8216 | int opti, boolean dumpAll) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8217 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8218 | |
| 8219 | if ("all".equals(name)) { |
| 8220 | synchronized (this) { |
| 8221 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8222 | activities.add(r1); |
| 8223 | } |
| 8224 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8225 | } else { |
| 8226 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 8227 | int objectId = 0; |
| 8228 | if (componentName == null) { |
| 8229 | // Not a '/' separated full component name; maybe an object ID? |
| 8230 | try { |
| 8231 | objectId = Integer.parseInt(name, 16); |
| 8232 | name = null; |
| 8233 | componentName = null; |
| 8234 | } catch (RuntimeException e) { |
| 8235 | } |
| 8236 | } |
| 8237 | |
| 8238 | synchronized (this) { |
| 8239 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
| 8240 | if (componentName != null) { |
| 8241 | if (r1.intent.getComponent().equals(componentName)) { |
| 8242 | activities.add(r1); |
| 8243 | } |
| 8244 | } else if (name != null) { |
| 8245 | if (r1.intent.getComponent().flattenToString().contains(name)) { |
| 8246 | activities.add(r1); |
| 8247 | } |
| 8248 | } else if (System.identityHashCode(r1) == objectId) { |
| 8249 | activities.add(r1); |
| 8250 | } |
| 8251 | } |
| 8252 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8253 | } |
| 8254 | |
| 8255 | if (activities.size() <= 0) { |
| 8256 | return false; |
| 8257 | } |
| 8258 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8259 | String[] newArgs = new String[args.length - opti]; |
| 8260 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 8261 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8262 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8263 | boolean needSep = false; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8264 | for (int i=activities.size()-1; i>=0; i--) { |
| 8265 | ActivityRecord r = (ActivityRecord)activities.get(i); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8266 | if (needSep) { |
| 8267 | pw.println(); |
| 8268 | } |
| 8269 | needSep = true; |
| 8270 | synchronized (this) { |
| 8271 | if (lastTask != r.task) { |
| 8272 | lastTask = r.task; |
| 8273 | pw.print("TASK "); pw.print(lastTask.affinity); |
| 8274 | pw.print(" id="); pw.println(lastTask.taskId); |
| 8275 | if (dumpAll) { |
| 8276 | lastTask.dump(pw, " "); |
| 8277 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8278 | } |
| 8279 | } |
| 8280 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8281 | } |
| 8282 | return true; |
| 8283 | } |
| 8284 | |
| 8285 | /** |
| 8286 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 8287 | * there is a thread associated with the activity. |
| 8288 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8289 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8290 | final ActivityRecord r, String[] args, boolean dumpAll) { |
| 8291 | String innerPrefix = prefix + " "; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8292 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8293 | pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName); |
| 8294 | pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 8295 | pw.print(" pid="); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8296 | if (r.app != null) pw.println(r.app.pid); |
| 8297 | else pw.println("(not running)"); |
| 8298 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8299 | r.dump(pw, innerPrefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8300 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8301 | } |
| 8302 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8303 | // flush anything that is already in the PrintWriter since the thread is going |
| 8304 | // to write to the file descriptor directly |
| 8305 | pw.flush(); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8306 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8307 | TransferPipe tp = new TransferPipe(); |
| 8308 | try { |
| 8309 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), r, |
| 8310 | innerPrefix, args); |
| 8311 | tp.go(fd); |
| 8312 | } finally { |
| 8313 | tp.kill(); |
| 8314 | } |
| 8315 | } catch (IOException e) { |
| 8316 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8317 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8318 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8319 | } |
| 8320 | } |
| 8321 | } |
| 8322 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8323 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8324 | int opti, boolean dumpAll) { |
| 8325 | boolean needSep = false; |
| 8326 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8327 | pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8328 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8329 | if (mRegisteredReceivers.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8330 | pw.println(" Registered Receivers:"); |
| 8331 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 8332 | while (it.hasNext()) { |
| 8333 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8334 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8335 | r.dump(pw, " "); |
| 8336 | } |
| 8337 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8338 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8339 | pw.println(); |
| 8340 | pw.println(" Receiver Resolver Table:"); |
| 8341 | mReceiverResolver.dump(pw, null, " ", null, false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8342 | needSep = true; |
| 8343 | } |
| 8344 | |
| 8345 | if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0 |
| 8346 | || mPendingBroadcast != null) { |
| 8347 | if (mParallelBroadcasts.size() > 0) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8348 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8349 | pw.println(" Active broadcasts:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8350 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8351 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 8352 | pw.println(" Broadcast #" + i + ":"); |
| 8353 | mParallelBroadcasts.get(i).dump(pw, " "); |
| 8354 | } |
| 8355 | if (mOrderedBroadcasts.size() > 0) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8356 | pw.println(); |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 8357 | pw.println(" Active ordered broadcasts:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8358 | } |
| 8359 | for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) { |
| 8360 | pw.println(" Serialized Broadcast #" + i + ":"); |
| 8361 | mOrderedBroadcasts.get(i).dump(pw, " "); |
| 8362 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8363 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8364 | pw.println(" Pending broadcast:"); |
| 8365 | if (mPendingBroadcast != null) { |
| 8366 | mPendingBroadcast.dump(pw, " "); |
| 8367 | } else { |
| 8368 | pw.println(" (null)"); |
| 8369 | } |
| 8370 | needSep = true; |
| 8371 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8372 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8373 | if (needSep) { |
| 8374 | pw.println(); |
| 8375 | } |
| 8376 | pw.println(" Historical broadcasts:"); |
| 8377 | for (int i=0; i<MAX_BROADCAST_HISTORY; i++) { |
| 8378 | BroadcastRecord r = mBroadcastHistory[i]; |
| 8379 | if (r == null) { |
| 8380 | break; |
| 8381 | } |
| 8382 | if (dumpAll) { |
| 8383 | pw.print(" Historical Broadcast #"); pw.print(i); pw.println(":"); |
| 8384 | r.dump(pw, " "); |
| 8385 | } else { |
| 8386 | if (i >= 50) { |
| 8387 | pw.println(" ..."); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 8388 | break; |
| 8389 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8390 | pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 8391 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8392 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8393 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8394 | |
| 8395 | if (mStickyBroadcasts != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8396 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8397 | pw.println(" Sticky broadcasts:"); |
| 8398 | StringBuilder sb = new StringBuilder(128); |
| 8399 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 8400 | : mStickyBroadcasts.entrySet()) { |
| 8401 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8402 | if (dumpAll) { |
| 8403 | pw.println(":"); |
| 8404 | ArrayList<Intent> intents = ent.getValue(); |
| 8405 | final int N = intents.size(); |
| 8406 | for (int i=0; i<N; i++) { |
| 8407 | sb.setLength(0); |
| 8408 | sb.append(" Intent: "); |
| 8409 | intents.get(i).toShortString(sb, true, false); |
| 8410 | pw.println(sb.toString()); |
| 8411 | Bundle bundle = intents.get(i).getExtras(); |
| 8412 | if (bundle != null) { |
| 8413 | pw.print(" "); |
| 8414 | pw.println(bundle.toString()); |
| 8415 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8416 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8417 | } else { |
| 8418 | pw.println(""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8419 | } |
| 8420 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8421 | needSep = true; |
| 8422 | } |
| 8423 | |
| 8424 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8425 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8426 | pw.println(" mBroadcastsScheduled=" + mBroadcastsScheduled); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8427 | pw.println(" mHandler:"); |
| 8428 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8429 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8430 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8431 | |
| 8432 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8433 | } |
| 8434 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8435 | boolean dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8436 | int opti, boolean dumpAll, boolean dumpClient) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8437 | boolean needSep = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8438 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8439 | pw.println("ACTIVITY MANAGER SERVICES (dumpsys activity services)"); |
| 8440 | if (mServices.size() > 0) { |
| 8441 | pw.println(" Active services:"); |
| 8442 | long nowReal = SystemClock.elapsedRealtime(); |
| 8443 | Iterator<ServiceRecord> it = mServices.values().iterator(); |
| 8444 | needSep = false; |
| 8445 | while (it.hasNext()) { |
| 8446 | ServiceRecord r = it.next(); |
| 8447 | if (needSep) { |
| 8448 | pw.println(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8449 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8450 | pw.print(" * "); pw.println(r); |
| 8451 | if (dumpAll) { |
| 8452 | r.dump(pw, " "); |
| 8453 | needSep = true; |
| 8454 | } else { |
| 8455 | pw.print(" app="); pw.println(r.app); |
| 8456 | pw.print(" created="); |
| 8457 | TimeUtils.formatDuration(r.createTime, nowReal, pw); |
| 8458 | pw.print(" started="); pw.print(r.startRequested); |
| 8459 | pw.print(" connections="); pw.println(r.connections.size()); |
| 8460 | } |
| 8461 | if (dumpClient && r.app != null && r.app.thread != null) { |
| 8462 | pw.println(" Client:"); |
| 8463 | pw.flush(); |
| 8464 | try { |
| 8465 | TransferPipe tp = new TransferPipe(); |
| 8466 | try { |
| 8467 | r.app.thread.dumpService( |
| 8468 | tp.getWriteFd().getFileDescriptor(), r, args); |
| 8469 | tp.setBufferPrefix(" "); |
| 8470 | // Short timeout, since blocking here can |
| 8471 | // deadlock with the application. |
| 8472 | tp.go(fd, 2000); |
| 8473 | } finally { |
| 8474 | tp.kill(); |
| 8475 | } |
| 8476 | } catch (IOException e) { |
| 8477 | pw.println(" Failure while dumping the service: " + e); |
| 8478 | } catch (RemoteException e) { |
| 8479 | pw.println(" Got a RemoteException while dumping the service"); |
| 8480 | } |
| 8481 | needSep = true; |
| 8482 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8483 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8484 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8485 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8486 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8487 | if (mPendingServices.size() > 0) { |
| 8488 | if (needSep) pw.println(" "); |
| 8489 | pw.println(" Pending services:"); |
| 8490 | for (int i=0; i<mPendingServices.size(); i++) { |
| 8491 | ServiceRecord r = mPendingServices.get(i); |
| 8492 | pw.print(" * Pending "); pw.println(r); |
| 8493 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8494 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8495 | needSep = true; |
| 8496 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8497 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8498 | if (mRestartingServices.size() > 0) { |
| 8499 | if (needSep) pw.println(" "); |
| 8500 | pw.println(" Restarting services:"); |
| 8501 | for (int i=0; i<mRestartingServices.size(); i++) { |
| 8502 | ServiceRecord r = mRestartingServices.get(i); |
| 8503 | pw.print(" * Restarting "); pw.println(r); |
| 8504 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8505 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8506 | needSep = true; |
| 8507 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8508 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8509 | if (mStoppingServices.size() > 0) { |
| 8510 | if (needSep) pw.println(" "); |
| 8511 | pw.println(" Stopping services:"); |
| 8512 | for (int i=0; i<mStoppingServices.size(); i++) { |
| 8513 | ServiceRecord r = mStoppingServices.get(i); |
| 8514 | pw.print(" * Stopping "); pw.println(r); |
| 8515 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8516 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8517 | needSep = true; |
| 8518 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8519 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8520 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8521 | if (mServiceConnections.size() > 0) { |
| 8522 | if (needSep) pw.println(" "); |
| 8523 | pw.println(" Connection bindings to services:"); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8524 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8525 | = mServiceConnections.values().iterator(); |
| 8526 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8527 | ArrayList<ConnectionRecord> r = it.next(); |
| 8528 | for (int i=0; i<r.size(); i++) { |
| 8529 | pw.print(" * "); pw.println(r.get(i)); |
| 8530 | r.get(i).dump(pw, " "); |
| 8531 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8532 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8533 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8534 | } |
| 8535 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8536 | |
| 8537 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8538 | } |
| 8539 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8540 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8541 | int opti, boolean dumpAll) { |
| 8542 | boolean needSep = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8543 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8544 | pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)"); |
| 8545 | if (mProvidersByClass.size() > 0) { |
| 8546 | if (needSep) pw.println(" "); |
| 8547 | pw.println(" Published content providers (by class):"); |
| 8548 | Iterator<Map.Entry<String, ContentProviderRecord>> it |
| 8549 | = mProvidersByClass.entrySet().iterator(); |
| 8550 | while (it.hasNext()) { |
| 8551 | Map.Entry<String, ContentProviderRecord> e = it.next(); |
| 8552 | ContentProviderRecord r = e.getValue(); |
| 8553 | if (dumpAll) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8554 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8555 | r.dump(pw, " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8556 | } else { |
| 8557 | pw.print(" * "); pw.print(r.name.toShortString()); |
| 8558 | if (r.app != null) { |
| 8559 | pw.println(":"); |
| 8560 | pw.print(" "); pw.println(r.app); |
| 8561 | } else { |
| 8562 | pw.println(); |
| 8563 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8564 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8565 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8566 | needSep = true; |
| 8567 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8568 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8569 | if (dumpAll) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8570 | if (mProvidersByName.size() > 0) { |
| 8571 | pw.println(" "); |
| 8572 | pw.println(" Authority to provider mappings:"); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8573 | Iterator<Map.Entry<String, ContentProviderRecord>> it |
| 8574 | = mProvidersByName.entrySet().iterator(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8575 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8576 | Map.Entry<String, ContentProviderRecord> e = it.next(); |
| 8577 | ContentProviderRecord r = e.getValue(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8578 | pw.print(" "); pw.print(e.getKey()); pw.print(": "); |
| 8579 | pw.println(r); |
| 8580 | } |
| 8581 | needSep = true; |
| 8582 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8583 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8584 | |
| 8585 | if (mLaunchingProviders.size() > 0) { |
| 8586 | if (needSep) pw.println(" "); |
| 8587 | pw.println(" Launching content providers:"); |
| 8588 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
| 8589 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
| 8590 | pw.println(mLaunchingProviders.get(i)); |
| 8591 | } |
| 8592 | needSep = true; |
| 8593 | } |
| 8594 | |
| 8595 | if (mGrantedUriPermissions.size() > 0) { |
| 8596 | pw.println(); |
| 8597 | pw.println("Granted Uri Permissions:"); |
| 8598 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 8599 | int uid = mGrantedUriPermissions.keyAt(i); |
| 8600 | HashMap<Uri, UriPermission> perms |
| 8601 | = mGrantedUriPermissions.valueAt(i); |
| 8602 | pw.print(" * UID "); pw.print(uid); |
| 8603 | pw.println(" holds:"); |
| 8604 | for (UriPermission perm : perms.values()) { |
| 8605 | pw.print(" "); pw.println(perm); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8606 | if (dumpAll) { |
| 8607 | perm.dump(pw, " "); |
| 8608 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8609 | } |
| 8610 | } |
| 8611 | needSep = true; |
| 8612 | } |
| 8613 | |
| 8614 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8615 | } |
| 8616 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8617 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8618 | int opti, boolean dumpAll) { |
| 8619 | boolean needSep = false; |
| 8620 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8621 | if (this.mIntentSenderRecords.size() > 0) { |
| 8622 | pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)"); |
| 8623 | Iterator<WeakReference<PendingIntentRecord>> it |
| 8624 | = mIntentSenderRecords.values().iterator(); |
| 8625 | while (it.hasNext()) { |
| 8626 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 8627 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
| 8628 | needSep = true; |
| 8629 | if (rec != null) { |
| 8630 | pw.print(" * "); pw.println(rec); |
| 8631 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8632 | rec.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8633 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8634 | } else { |
| 8635 | pw.print(" * "); pw.println(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8636 | } |
| 8637 | } |
| 8638 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8639 | |
| 8640 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8641 | } |
| 8642 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8643 | private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list, |
| 8644 | String prefix, String label, boolean complete, boolean brief, boolean client) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8645 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8646 | boolean needNL = false; |
| 8647 | final String innerPrefix = prefix + " "; |
| 8648 | final String[] args = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8649 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8650 | final ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 8651 | final boolean full = !brief && (complete || !r.isInHistory()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8652 | if (needNL) { |
| 8653 | pw.println(" "); |
| 8654 | needNL = false; |
| 8655 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8656 | if (lastTask != r.task) { |
| 8657 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8658 | pw.print(prefix); |
| 8659 | pw.print(full ? "* " : " "); |
| 8660 | pw.println(lastTask); |
| 8661 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 8662 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8663 | } else if (complete) { |
| 8664 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 8665 | if (lastTask.intent != null) { |
| 8666 | pw.print(prefix); pw.print(" "); pw.println(lastTask.intent); |
| 8667 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 8668 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8669 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8670 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 8671 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 8672 | pw.println(r); |
| 8673 | if (full) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8674 | r.dump(pw, innerPrefix); |
| 8675 | } else if (complete) { |
| 8676 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 8677 | pw.print(innerPrefix); pw.println(r.intent); |
| 8678 | if (r.app != null) { |
| 8679 | pw.print(innerPrefix); pw.println(r.app); |
| 8680 | } |
| 8681 | } |
| 8682 | if (client && r.app != null && r.app.thread != null) { |
| 8683 | // flush anything that is already in the PrintWriter since the thread is going |
| 8684 | // to write to the file descriptor directly |
| 8685 | pw.flush(); |
| 8686 | try { |
| 8687 | TransferPipe tp = new TransferPipe(); |
| 8688 | try { |
| 8689 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), r, |
| 8690 | innerPrefix, args); |
| 8691 | // Short timeout, since blocking here can |
| 8692 | // deadlock with the application. |
| 8693 | tp.go(fd, 2000); |
| 8694 | } finally { |
| 8695 | tp.kill(); |
| 8696 | } |
| 8697 | } catch (IOException e) { |
| 8698 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
| 8699 | } catch (RemoteException e) { |
| 8700 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
| 8701 | } |
| 8702 | needNL = true; |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 8703 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8704 | } |
| 8705 | } |
| 8706 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 8707 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 8708 | if (val == base) { |
| 8709 | if (space == null) return prefix; |
| 8710 | return prefix + " "; |
| 8711 | } |
| 8712 | return prefix + "+" + Integer.toString(val-base); |
| 8713 | } |
| 8714 | |
| 8715 | private static final int dumpProcessList(PrintWriter pw, |
| 8716 | ActivityManagerService service, List list, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8717 | String prefix, String normalLabel, String persistentLabel) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8718 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 8719 | final int N = list.size()-1; |
| 8720 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8721 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8722 | pw.println(String.format("%s%s #%2d: %s", |
| 8723 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 8724 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8725 | if (r.persistent) { |
| 8726 | numPers++; |
| 8727 | } |
| 8728 | } |
| 8729 | return numPers; |
| 8730 | } |
| 8731 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8732 | private static final void dumpProcessOomList(PrintWriter pw, |
| 8733 | ActivityManagerService service, List<ProcessRecord> list, |
| 8734 | String prefix, String normalLabel, String persistentLabel, |
| 8735 | boolean inclDetails) { |
| 8736 | |
| 8737 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 8738 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 8739 | final long curUptime = SystemClock.uptimeMillis(); |
| 8740 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 8741 | |
| 8742 | final int N = list.size()-1; |
| 8743 | for (int i=N; i>=0; i--) { |
| 8744 | ProcessRecord r = list.get(i); |
| 8745 | String oomAdj; |
| 8746 | if (r.setAdj >= EMPTY_APP_ADJ) { |
| 8747 | oomAdj = buildOomTag("empty", null, r.setAdj, EMPTY_APP_ADJ); |
| 8748 | } else if (r.setAdj >= HIDDEN_APP_MIN_ADJ) { |
| 8749 | oomAdj = buildOomTag("bak", " ", r.setAdj, HIDDEN_APP_MIN_ADJ); |
| 8750 | } else if (r.setAdj >= HOME_APP_ADJ) { |
| 8751 | oomAdj = buildOomTag("home ", null, r.setAdj, HOME_APP_ADJ); |
| 8752 | } else if (r.setAdj >= SECONDARY_SERVER_ADJ) { |
| 8753 | oomAdj = buildOomTag("svc", " ", r.setAdj, SECONDARY_SERVER_ADJ); |
| 8754 | } else if (r.setAdj >= BACKUP_APP_ADJ) { |
| 8755 | oomAdj = buildOomTag("bckup", null, r.setAdj, BACKUP_APP_ADJ); |
| 8756 | } else if (r.setAdj >= HEAVY_WEIGHT_APP_ADJ) { |
| 8757 | oomAdj = buildOomTag("hvy ", null, r.setAdj, HEAVY_WEIGHT_APP_ADJ); |
| 8758 | } else if (r.setAdj >= PERCEPTIBLE_APP_ADJ) { |
| 8759 | oomAdj = buildOomTag("prcp ", null, r.setAdj, PERCEPTIBLE_APP_ADJ); |
| 8760 | } else if (r.setAdj >= VISIBLE_APP_ADJ) { |
| 8761 | oomAdj = buildOomTag("vis ", null, r.setAdj, VISIBLE_APP_ADJ); |
| 8762 | } else if (r.setAdj >= FOREGROUND_APP_ADJ) { |
| 8763 | oomAdj = buildOomTag("fore ", null, r.setAdj, FOREGROUND_APP_ADJ); |
| 8764 | } else if (r.setAdj >= CORE_SERVER_ADJ) { |
| 8765 | oomAdj = buildOomTag("core ", null, r.setAdj, CORE_SERVER_ADJ); |
| 8766 | } else if (r.setAdj >= SYSTEM_ADJ) { |
| 8767 | oomAdj = buildOomTag("sys ", null, r.setAdj, SYSTEM_ADJ); |
| 8768 | } else { |
| 8769 | oomAdj = Integer.toString(r.setAdj); |
| 8770 | } |
| 8771 | String schedGroup; |
| 8772 | switch (r.setSchedGroup) { |
| 8773 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 8774 | schedGroup = "B"; |
| 8775 | break; |
| 8776 | case Process.THREAD_GROUP_DEFAULT: |
| 8777 | schedGroup = "F"; |
| 8778 | break; |
| 8779 | default: |
| 8780 | schedGroup = Integer.toString(r.setSchedGroup); |
| 8781 | break; |
| 8782 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 8783 | String foreground; |
| 8784 | if (r.foregroundActivities) { |
| 8785 | foreground = "A"; |
| 8786 | } else if (r.foregroundServices) { |
| 8787 | foreground = "S"; |
| 8788 | } else { |
| 8789 | foreground = " "; |
| 8790 | } |
| 8791 | pw.println(String.format("%s%s #%2d: adj=%s/%s%s %s (%s)", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8792 | prefix, (r.persistent ? persistentLabel : normalLabel), |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 8793 | N-i, oomAdj, schedGroup, foreground, r.toShortString(), r.adjType)); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8794 | if (r.adjSource != null || r.adjTarget != null) { |
| 8795 | pw.print(prefix); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8796 | pw.print(" "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8797 | if (r.adjTarget instanceof ComponentName) { |
| 8798 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 8799 | } else if (r.adjTarget != null) { |
| 8800 | pw.print(r.adjTarget.toString()); |
| 8801 | } else { |
| 8802 | pw.print("{null}"); |
| 8803 | } |
| 8804 | pw.print("<="); |
| 8805 | if (r.adjSource instanceof ProcessRecord) { |
| 8806 | pw.print("Proc{"); |
| 8807 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 8808 | pw.println("}"); |
| 8809 | } else if (r.adjSource != null) { |
| 8810 | pw.println(r.adjSource.toString()); |
| 8811 | } else { |
| 8812 | pw.println("{null}"); |
| 8813 | } |
| 8814 | } |
| 8815 | if (inclDetails) { |
| 8816 | pw.print(prefix); |
| 8817 | pw.print(" "); |
| 8818 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 8819 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
| 8820 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 8821 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 8822 | pw.print(" cur="); pw.print(r.curAdj); |
| 8823 | pw.print(" set="); pw.println(r.setAdj); |
| 8824 | pw.print(prefix); |
| 8825 | pw.print(" "); |
| 8826 | pw.print("keeping="); pw.print(r.keeping); |
| 8827 | pw.print(" hidden="); pw.print(r.hidden); |
| 8828 | pw.print(" empty="); pw.println(r.empty); |
| 8829 | |
| 8830 | if (!r.keeping) { |
| 8831 | if (r.lastWakeTime != 0) { |
| 8832 | long wtime; |
| 8833 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 8834 | synchronized (stats) { |
| 8835 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 8836 | r.pid, curRealtime); |
| 8837 | } |
| 8838 | long timeUsed = wtime - r.lastWakeTime; |
| 8839 | pw.print(prefix); |
| 8840 | pw.print(" "); |
| 8841 | pw.print("keep awake over "); |
| 8842 | TimeUtils.formatDuration(realtimeSince, pw); |
| 8843 | pw.print(" used "); |
| 8844 | TimeUtils.formatDuration(timeUsed, pw); |
| 8845 | pw.print(" ("); |
| 8846 | pw.print((timeUsed*100)/realtimeSince); |
| 8847 | pw.println("%)"); |
| 8848 | } |
| 8849 | if (r.lastCpuTime != 0) { |
| 8850 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 8851 | pw.print(prefix); |
| 8852 | pw.print(" "); |
| 8853 | pw.print("run cpu over "); |
| 8854 | TimeUtils.formatDuration(uptimeSince, pw); |
| 8855 | pw.print(" used "); |
| 8856 | TimeUtils.formatDuration(timeUsed, pw); |
| 8857 | pw.print(" ("); |
| 8858 | pw.print((timeUsed*100)/uptimeSince); |
| 8859 | pw.println("%)"); |
| 8860 | } |
| 8861 | } |
| 8862 | } |
| 8863 | } |
| 8864 | } |
| 8865 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8866 | ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, String[] args) { |
| 8867 | ArrayList<ProcessRecord> procs; |
| 8868 | synchronized (this) { |
| 8869 | if (args != null && args.length > 0 |
| 8870 | && args[0].charAt(0) != '-') { |
| 8871 | procs = new ArrayList<ProcessRecord>(); |
| 8872 | int pid = -1; |
| 8873 | try { |
| 8874 | pid = Integer.parseInt(args[0]); |
| 8875 | } catch (NumberFormatException e) { |
| 8876 | |
| 8877 | } |
| 8878 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8879 | ProcessRecord proc = mLruProcesses.get(i); |
| 8880 | if (proc.pid == pid) { |
| 8881 | procs.add(proc); |
| 8882 | } else if (proc.processName.equals(args[0])) { |
| 8883 | procs.add(proc); |
| 8884 | } |
| 8885 | } |
| 8886 | if (procs.size() <= 0) { |
| 8887 | pw.println("No process found for: " + args[0]); |
| 8888 | return null; |
| 8889 | } |
| 8890 | } else { |
| 8891 | procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 8892 | } |
| 8893 | } |
| 8894 | return procs; |
| 8895 | } |
| 8896 | |
| 8897 | final void dumpGraphicsHardwareUsage(FileDescriptor fd, |
| 8898 | PrintWriter pw, String[] args) { |
| 8899 | ArrayList<ProcessRecord> procs = collectProcesses(pw, args); |
| 8900 | if (procs == null) { |
| 8901 | return; |
| 8902 | } |
| 8903 | |
| 8904 | long uptime = SystemClock.uptimeMillis(); |
| 8905 | long realtime = SystemClock.elapsedRealtime(); |
| 8906 | pw.println("Applications Graphics Acceleration Info:"); |
| 8907 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 8908 | |
| 8909 | String callArgs[] = {"graphics"}; |
| 8910 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 8911 | ProcessRecord r = procs.get(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 8912 | if (r.thread != null) { |
| 8913 | pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **"); |
| 8914 | pw.flush(); |
| 8915 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8916 | TransferPipe.goDump(r.thread.asBinder(), fd, callArgs); |
| 8917 | } catch (IOException e) { |
| 8918 | pw.println("Failure: " + e); |
| 8919 | pw.flush(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 8920 | } catch (RemoteException e) { |
| 8921 | pw.println("Got RemoteException!"); |
| 8922 | pw.flush(); |
| 8923 | } |
| 8924 | } |
| 8925 | } |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 8926 | } |
| 8927 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8928 | final void dumpApplicationMemoryUsage(FileDescriptor fd, |
| 8929 | PrintWriter pw, String prefix, String[] args) { |
| 8930 | ArrayList<ProcessRecord> procs = collectProcesses(pw, args); |
| 8931 | if (procs == null) { |
| 8932 | return; |
| 8933 | } |
| 8934 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 8935 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8936 | long uptime = SystemClock.uptimeMillis(); |
| 8937 | long realtime = SystemClock.elapsedRealtime(); |
| 8938 | |
| 8939 | if (isCheckinRequest) { |
| 8940 | // short checkin version |
| 8941 | pw.println(uptime + "," + realtime); |
| 8942 | pw.flush(); |
| 8943 | } else { |
| 8944 | pw.println("Applications Memory Usage (kB):"); |
| 8945 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 8946 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8947 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 8948 | ProcessRecord r = procs.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8949 | if (r.thread != null) { |
| 8950 | if (!isCheckinRequest) { |
| 8951 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 8952 | pw.flush(); |
| 8953 | } |
| 8954 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8955 | TransferPipe.goDump(r.thread.asBinder(), fd, args); |
| 8956 | } catch (IOException e) { |
| 8957 | pw.println("Failure: " + e); |
| 8958 | pw.flush(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8959 | } catch (RemoteException e) { |
| 8960 | if (!isCheckinRequest) { |
| 8961 | pw.println("Got RemoteException!"); |
| 8962 | pw.flush(); |
| 8963 | } |
| 8964 | } |
| 8965 | } |
| 8966 | } |
| 8967 | } |
| 8968 | |
| 8969 | /** |
| 8970 | * Searches array of arguments for the specified string |
| 8971 | * @param args array of argument strings |
| 8972 | * @param value value to search for |
| 8973 | * @return true if the value is contained in the array |
| 8974 | */ |
| 8975 | private static boolean scanArgs(String[] args, String value) { |
| 8976 | if (args != null) { |
| 8977 | for (String arg : args) { |
| 8978 | if (value.equals(arg)) { |
| 8979 | return true; |
| 8980 | } |
| 8981 | } |
| 8982 | } |
| 8983 | return false; |
| 8984 | } |
| 8985 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8986 | private final void killServicesLocked(ProcessRecord app, |
| 8987 | boolean allowRestart) { |
| 8988 | // Report disconnected services. |
| 8989 | if (false) { |
| 8990 | // XXX we are letting the client link to the service for |
| 8991 | // death notifications. |
| 8992 | if (app.services.size() > 0) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8993 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8994 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8995 | ServiceRecord r = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8996 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8997 | Iterator<ArrayList<ConnectionRecord>> jt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8998 | = r.connections.values().iterator(); |
| 8999 | while (jt.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9000 | ArrayList<ConnectionRecord> cl = jt.next(); |
| 9001 | for (int i=0; i<cl.size(); i++) { |
| 9002 | ConnectionRecord c = cl.get(i); |
| 9003 | if (c.binding.client != app) { |
| 9004 | try { |
| 9005 | //c.conn.connected(r.className, null); |
| 9006 | } catch (Exception e) { |
| 9007 | // todo: this should be asynchronous! |
| 9008 | Slog.w(TAG, "Exception thrown disconnected servce " |
| 9009 | + r.shortName |
| 9010 | + " from app " + app.processName, e); |
| 9011 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9012 | } |
| 9013 | } |
| 9014 | } |
| 9015 | } |
| 9016 | } |
| 9017 | } |
| 9018 | } |
| 9019 | |
| 9020 | // Clean up any connections this application has to other services. |
| 9021 | if (app.connections.size() > 0) { |
| 9022 | Iterator<ConnectionRecord> it = app.connections.iterator(); |
| 9023 | while (it.hasNext()) { |
| 9024 | ConnectionRecord r = it.next(); |
| 9025 | removeConnectionLocked(r, app, null); |
| 9026 | } |
| 9027 | } |
| 9028 | app.connections.clear(); |
| 9029 | |
| 9030 | if (app.services.size() != 0) { |
| 9031 | // Any services running in the application need to be placed |
| 9032 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9033 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9034 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9035 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9036 | synchronized (sr.stats.getBatteryStats()) { |
| 9037 | sr.stats.stopLaunchedLocked(); |
| 9038 | } |
| 9039 | sr.app = null; |
| 9040 | sr.executeNesting = 0; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9041 | if (mStoppingServices.remove(sr)) { |
| 9042 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
| 9043 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9044 | |
| 9045 | boolean hasClients = sr.bindings.size() > 0; |
| 9046 | if (hasClients) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9047 | Iterator<IntentBindRecord> bindings |
| 9048 | = sr.bindings.values().iterator(); |
| 9049 | while (bindings.hasNext()) { |
| 9050 | IntentBindRecord b = bindings.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9051 | if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9052 | + ": shouldUnbind=" + b.hasBound); |
| 9053 | b.binder = null; |
| 9054 | b.requested = b.received = b.hasBound = false; |
| 9055 | } |
| 9056 | } |
| 9057 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 9058 | if (sr.crashCount >= 2 && (sr.serviceInfo.applicationInfo.flags |
| 9059 | &ApplicationInfo.FLAG_PERSISTENT) == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9060 | Slog.w(TAG, "Service crashed " + sr.crashCount |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9061 | + " times, stopping: " + sr); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 9062 | EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9063 | sr.crashCount, sr.shortName, app.pid); |
| 9064 | bringDownServiceLocked(sr, true); |
| 9065 | } else if (!allowRestart) { |
| 9066 | bringDownServiceLocked(sr, true); |
| 9067 | } else { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9068 | boolean canceled = scheduleServiceRestartLocked(sr, true); |
| 9069 | |
| 9070 | // Should the service remain running? Note that in the |
| 9071 | // extreme case of so many attempts to deliver a command |
| 9072 | // that it failed, that we also will stop it here. |
| 9073 | if (sr.startRequested && (sr.stopIfKilled || canceled)) { |
| 9074 | if (sr.pendingStarts.size() == 0) { |
| 9075 | sr.startRequested = false; |
| 9076 | if (!hasClients) { |
| 9077 | // Whoops, no reason to restart! |
| 9078 | bringDownServiceLocked(sr, true); |
| 9079 | } |
| 9080 | } |
| 9081 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9082 | } |
| 9083 | } |
| 9084 | |
| 9085 | if (!allowRestart) { |
| 9086 | app.services.clear(); |
| 9087 | } |
| 9088 | } |
| 9089 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 9090 | // Make sure we have no more records on the stopping list. |
| 9091 | int i = mStoppingServices.size(); |
| 9092 | while (i > 0) { |
| 9093 | i--; |
| 9094 | ServiceRecord sr = mStoppingServices.get(i); |
| 9095 | if (sr.app == app) { |
| 9096 | mStoppingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9097 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 9098 | } |
| 9099 | } |
| 9100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9101 | app.executingServices.clear(); |
| 9102 | } |
| 9103 | |
| 9104 | private final void removeDyingProviderLocked(ProcessRecord proc, |
| 9105 | ContentProviderRecord cpr) { |
| 9106 | synchronized (cpr) { |
| 9107 | cpr.launchingApp = null; |
| 9108 | cpr.notifyAll(); |
| 9109 | } |
| 9110 | |
| 9111 | mProvidersByClass.remove(cpr.info.name); |
| 9112 | String names[] = cpr.info.authority.split(";"); |
| 9113 | for (int j = 0; j < names.length; j++) { |
| 9114 | mProvidersByName.remove(names[j]); |
| 9115 | } |
| 9116 | |
| 9117 | Iterator<ProcessRecord> cit = cpr.clients.iterator(); |
| 9118 | while (cit.hasNext()) { |
| 9119 | ProcessRecord capp = cit.next(); |
| 9120 | if (!capp.persistent && capp.thread != null |
| 9121 | && capp.pid != 0 |
| 9122 | && capp.pid != MY_PID) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 9123 | Slog.i(TAG, "Kill " + capp.processName |
| 9124 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
| 9125 | + " in dying process " + proc.processName); |
| 9126 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid, |
| 9127 | capp.processName, capp.setAdj, "dying provider " + proc.processName); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 9128 | Process.killProcessQuiet(capp.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9129 | } |
| 9130 | } |
| 9131 | |
| 9132 | mLaunchingProviders.remove(cpr); |
| 9133 | } |
| 9134 | |
| 9135 | /** |
| 9136 | * Main code for cleaning up a process when it has gone away. This is |
| 9137 | * called both as a result of the process dying, or directly when stopping |
| 9138 | * a process when running in single process mode. |
| 9139 | */ |
| 9140 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
| 9141 | boolean restarting, int index) { |
| 9142 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 9143 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9144 | } |
| 9145 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 9146 | mProcessesToGc.remove(app); |
| 9147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9148 | // Dismiss any open dialogs. |
| 9149 | if (app.crashDialog != null) { |
| 9150 | app.crashDialog.dismiss(); |
| 9151 | app.crashDialog = null; |
| 9152 | } |
| 9153 | if (app.anrDialog != null) { |
| 9154 | app.anrDialog.dismiss(); |
| 9155 | app.anrDialog = null; |
| 9156 | } |
| 9157 | if (app.waitDialog != null) { |
| 9158 | app.waitDialog.dismiss(); |
| 9159 | app.waitDialog = null; |
| 9160 | } |
| 9161 | |
| 9162 | app.crashing = false; |
| 9163 | app.notResponding = false; |
| 9164 | |
| 9165 | app.resetPackageList(); |
| 9166 | app.thread = null; |
| 9167 | app.forcingToForeground = null; |
| 9168 | app.foregroundServices = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 9169 | app.foregroundActivities = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9170 | |
| 9171 | killServicesLocked(app, true); |
| 9172 | |
| 9173 | boolean restart = false; |
| 9174 | |
| 9175 | int NL = mLaunchingProviders.size(); |
| 9176 | |
| 9177 | // Remove published content providers. |
| 9178 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9179 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9180 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9181 | ContentProviderRecord cpr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9182 | cpr.provider = null; |
| 9183 | cpr.app = null; |
| 9184 | |
| 9185 | // See if someone is waiting for this provider... in which |
| 9186 | // case we don't remove it, but just let it restart. |
| 9187 | int i = 0; |
| 9188 | if (!app.bad) { |
| 9189 | for (; i<NL; i++) { |
| 9190 | if (mLaunchingProviders.get(i) == cpr) { |
| 9191 | restart = true; |
| 9192 | break; |
| 9193 | } |
| 9194 | } |
| 9195 | } else { |
| 9196 | i = NL; |
| 9197 | } |
| 9198 | |
| 9199 | if (i >= NL) { |
| 9200 | removeDyingProviderLocked(app, cpr); |
| 9201 | NL = mLaunchingProviders.size(); |
| 9202 | } |
| 9203 | } |
| 9204 | app.pubProviders.clear(); |
| 9205 | } |
| 9206 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 9207 | // Take care of any launching providers waiting for this process. |
| 9208 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 9209 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9210 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 9211 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9212 | // Unregister from connected content providers. |
| 9213 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 9214 | Iterator it = app.conProviders.keySet().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9215 | while (it.hasNext()) { |
| 9216 | ContentProviderRecord cpr = (ContentProviderRecord)it.next(); |
| 9217 | cpr.clients.remove(app); |
| 9218 | } |
| 9219 | app.conProviders.clear(); |
| 9220 | } |
| 9221 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 9222 | // At this point there may be remaining entries in mLaunchingProviders |
| 9223 | // where we were the only one waiting, so they are no longer of use. |
| 9224 | // Look for these and clean up if found. |
| 9225 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 9226 | // the actual situation to identify what is actually going on. |
| 9227 | if (false) { |
| 9228 | for (int i=0; i<NL; i++) { |
| 9229 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 9230 | mLaunchingProviders.get(i); |
| 9231 | if (cpr.clients.size() <= 0 && cpr.externals <= 0) { |
| 9232 | synchronized (cpr) { |
| 9233 | cpr.launchingApp = null; |
| 9234 | cpr.notifyAll(); |
| 9235 | } |
| 9236 | } |
| 9237 | } |
| 9238 | } |
| 9239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9240 | skipCurrentReceiverLocked(app); |
| 9241 | |
| 9242 | // Unregister any receivers. |
| 9243 | if (app.receivers.size() > 0) { |
| 9244 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 9245 | while (it.hasNext()) { |
| 9246 | removeReceiverLocked(it.next()); |
| 9247 | } |
| 9248 | app.receivers.clear(); |
| 9249 | } |
| 9250 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 9251 | // If the app is undergoing backup, tell the backup manager about it |
| 9252 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9253 | if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 9254 | try { |
| 9255 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 9256 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 9257 | bm.agentDisconnected(app.info.packageName); |
| 9258 | } catch (RemoteException e) { |
| 9259 | // can't happen; backup manager is local |
| 9260 | } |
| 9261 | } |
| 9262 | |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame^] | 9263 | mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 9264 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9265 | // If the caller is restarting this app, then leave it in its |
| 9266 | // current lists and let the caller take care of it. |
| 9267 | if (restarting) { |
| 9268 | return; |
| 9269 | } |
| 9270 | |
| 9271 | if (!app.persistent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9272 | if (DEBUG_PROCESSES) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9273 | "Removing non-persistent process during cleanup: " + app); |
| 9274 | mProcessNames.remove(app.processName, app.info.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9275 | if (mHeavyWeightProcess == app) { |
| 9276 | mHeavyWeightProcess = null; |
| 9277 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 9278 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9279 | } else if (!app.removed) { |
| 9280 | // This app is persistent, so we need to keep its record around. |
| 9281 | // If it is not already on the pending app list, add it there |
| 9282 | // and start a new process for it. |
| 9283 | app.thread = null; |
| 9284 | app.forcingToForeground = null; |
| 9285 | app.foregroundServices = false; |
| 9286 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 9287 | mPersistentStartingProcesses.add(app); |
| 9288 | restart = true; |
| 9289 | } |
| 9290 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 9291 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 9292 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9293 | mProcessesOnHold.remove(app); |
| 9294 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 9295 | if (app == mHomeProcess) { |
| 9296 | mHomeProcess = null; |
| 9297 | } |
| 9298 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9299 | if (restart) { |
| 9300 | // We have components that still need to be running in the |
| 9301 | // process, so re-launch it. |
| 9302 | mProcessNames.put(app.processName, app.info.uid, app); |
| 9303 | startProcessLocked(app, "restart", app.processName); |
| 9304 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 9305 | // Goodbye! |
| 9306 | synchronized (mPidsSelfLocked) { |
| 9307 | mPidsSelfLocked.remove(app.pid); |
| 9308 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 9309 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9310 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9311 | } |
| 9312 | } |
| 9313 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 9314 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 9315 | // Look through the content providers we are waiting to have launched, |
| 9316 | // and if any run in this process then either schedule a restart of |
| 9317 | // the process or kill the client waiting for it if this process has |
| 9318 | // gone bad. |
| 9319 | int NL = mLaunchingProviders.size(); |
| 9320 | boolean restart = false; |
| 9321 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9322 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 9323 | if (cpr.launchingApp == app) { |
| 9324 | if (!alwaysBad && !app.bad) { |
| 9325 | restart = true; |
| 9326 | } else { |
| 9327 | removeDyingProviderLocked(app, cpr); |
| 9328 | NL = mLaunchingProviders.size(); |
| 9329 | } |
| 9330 | } |
| 9331 | } |
| 9332 | return restart; |
| 9333 | } |
| 9334 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9335 | // ========================================================= |
| 9336 | // SERVICES |
| 9337 | // ========================================================= |
| 9338 | |
| 9339 | ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) { |
| 9340 | ActivityManager.RunningServiceInfo info = |
| 9341 | new ActivityManager.RunningServiceInfo(); |
| 9342 | info.service = r.name; |
| 9343 | if (r.app != null) { |
| 9344 | info.pid = r.app.pid; |
| 9345 | } |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 9346 | info.uid = r.appInfo.uid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9347 | info.process = r.processName; |
| 9348 | info.foreground = r.isForeground; |
| 9349 | info.activeSince = r.createTime; |
| 9350 | info.started = r.startRequested; |
| 9351 | info.clientCount = r.connections.size(); |
| 9352 | info.crashCount = r.crashCount; |
| 9353 | info.lastActivityTime = r.lastActivity; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 9354 | if (r.isForeground) { |
| 9355 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND; |
| 9356 | } |
| 9357 | if (r.startRequested) { |
| 9358 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED; |
| 9359 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 9360 | if (r.app != null && r.app.pid == MY_PID) { |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 9361 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS; |
| 9362 | } |
| 9363 | if (r.app != null && r.app.persistent) { |
| 9364 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS; |
| 9365 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9366 | |
| 9367 | for (ArrayList<ConnectionRecord> connl : r.connections.values()) { |
| 9368 | for (int i=0; i<connl.size(); i++) { |
| 9369 | ConnectionRecord conn = connl.get(i); |
| 9370 | if (conn.clientLabel != 0) { |
| 9371 | info.clientPackage = conn.binding.client.info.packageName; |
| 9372 | info.clientLabel = conn.clientLabel; |
| 9373 | return info; |
| 9374 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 9375 | } |
| 9376 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9377 | return info; |
| 9378 | } |
| 9379 | |
| 9380 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 9381 | int flags) { |
| 9382 | synchronized (this) { |
| 9383 | ArrayList<ActivityManager.RunningServiceInfo> res |
| 9384 | = new ArrayList<ActivityManager.RunningServiceInfo>(); |
| 9385 | |
| 9386 | if (mServices.size() > 0) { |
| 9387 | Iterator<ServiceRecord> it = mServices.values().iterator(); |
| 9388 | while (it.hasNext() && res.size() < maxNum) { |
| 9389 | res.add(makeRunningServiceInfoLocked(it.next())); |
| 9390 | } |
| 9391 | } |
| 9392 | |
| 9393 | for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { |
| 9394 | ServiceRecord r = mRestartingServices.get(i); |
| 9395 | ActivityManager.RunningServiceInfo info = |
| 9396 | makeRunningServiceInfoLocked(r); |
| 9397 | info.restarting = r.nextRestartTime; |
| 9398 | res.add(info); |
| 9399 | } |
| 9400 | |
| 9401 | return res; |
| 9402 | } |
| 9403 | } |
| 9404 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 9405 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
| 9406 | synchronized (this) { |
| 9407 | ServiceRecord r = mServices.get(name); |
| 9408 | if (r != null) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9409 | for (ArrayList<ConnectionRecord> conn : r.connections.values()) { |
| 9410 | for (int i=0; i<conn.size(); i++) { |
| 9411 | if (conn.get(i).clientIntent != null) { |
| 9412 | return conn.get(i).clientIntent; |
| 9413 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 9414 | } |
| 9415 | } |
| 9416 | } |
| 9417 | } |
| 9418 | return null; |
| 9419 | } |
| 9420 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9421 | private final ServiceRecord findServiceLocked(ComponentName name, |
| 9422 | IBinder token) { |
| 9423 | ServiceRecord r = mServices.get(name); |
| 9424 | return r == token ? r : null; |
| 9425 | } |
| 9426 | |
| 9427 | private final class ServiceLookupResult { |
| 9428 | final ServiceRecord record; |
| 9429 | final String permission; |
| 9430 | |
| 9431 | ServiceLookupResult(ServiceRecord _record, String _permission) { |
| 9432 | record = _record; |
| 9433 | permission = _permission; |
| 9434 | } |
| 9435 | }; |
| 9436 | |
| 9437 | private ServiceLookupResult findServiceLocked(Intent service, |
| 9438 | String resolvedType) { |
| 9439 | ServiceRecord r = null; |
| 9440 | if (service.getComponent() != null) { |
| 9441 | r = mServices.get(service.getComponent()); |
| 9442 | } |
| 9443 | if (r == null) { |
| 9444 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
| 9445 | r = mServicesByIntent.get(filter); |
| 9446 | } |
| 9447 | |
| 9448 | if (r == null) { |
| 9449 | try { |
| 9450 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 9451 | AppGlobals.getPackageManager().resolveService( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9452 | service, resolvedType, 0); |
| 9453 | ServiceInfo sInfo = |
| 9454 | rInfo != null ? rInfo.serviceInfo : null; |
| 9455 | if (sInfo == null) { |
| 9456 | return null; |
| 9457 | } |
| 9458 | |
| 9459 | ComponentName name = new ComponentName( |
| 9460 | sInfo.applicationInfo.packageName, sInfo.name); |
| 9461 | r = mServices.get(name); |
| 9462 | } catch (RemoteException ex) { |
| 9463 | // pm is in same process, this will never happen. |
| 9464 | } |
| 9465 | } |
| 9466 | if (r != null) { |
| 9467 | int callingPid = Binder.getCallingPid(); |
| 9468 | int callingUid = Binder.getCallingUid(); |
| 9469 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 9470 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9471 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 9472 | if (!r.exported) { |
| 9473 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 9474 | + " from pid=" + callingPid |
| 9475 | + ", uid=" + callingUid |
| 9476 | + " that is not exported from uid " + r.appInfo.uid); |
| 9477 | return new ServiceLookupResult(null, "not exported from uid " |
| 9478 | + r.appInfo.uid); |
| 9479 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9480 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9481 | + " from pid=" + callingPid |
| 9482 | + ", uid=" + callingUid |
| 9483 | + " requires " + r.permission); |
| 9484 | return new ServiceLookupResult(null, r.permission); |
| 9485 | } |
| 9486 | return new ServiceLookupResult(r, null); |
| 9487 | } |
| 9488 | return null; |
| 9489 | } |
| 9490 | |
| 9491 | private class ServiceRestarter implements Runnable { |
| 9492 | private ServiceRecord mService; |
| 9493 | |
| 9494 | void setService(ServiceRecord service) { |
| 9495 | mService = service; |
| 9496 | } |
| 9497 | |
| 9498 | public void run() { |
| 9499 | synchronized(ActivityManagerService.this) { |
| 9500 | performServiceRestartLocked(mService); |
| 9501 | } |
| 9502 | } |
| 9503 | } |
| 9504 | |
| 9505 | private ServiceLookupResult retrieveServiceLocked(Intent service, |
| 9506 | String resolvedType, int callingPid, int callingUid) { |
| 9507 | ServiceRecord r = null; |
| 9508 | if (service.getComponent() != null) { |
| 9509 | r = mServices.get(service.getComponent()); |
| 9510 | } |
| 9511 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
| 9512 | r = mServicesByIntent.get(filter); |
| 9513 | if (r == null) { |
| 9514 | try { |
| 9515 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 9516 | AppGlobals.getPackageManager().resolveService( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 9517 | service, resolvedType, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9518 | ServiceInfo sInfo = |
| 9519 | rInfo != null ? rInfo.serviceInfo : null; |
| 9520 | if (sInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9521 | Slog.w(TAG, "Unable to start service " + service + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9522 | ": not found"); |
| 9523 | return null; |
| 9524 | } |
| 9525 | |
| 9526 | ComponentName name = new ComponentName( |
| 9527 | sInfo.applicationInfo.packageName, sInfo.name); |
| 9528 | r = mServices.get(name); |
| 9529 | if (r == null) { |
| 9530 | filter = new Intent.FilterComparison(service.cloneFilter()); |
| 9531 | ServiceRestarter res = new ServiceRestarter(); |
| 9532 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 9533 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 9534 | synchronized (stats) { |
| 9535 | ss = stats.getServiceStatsLocked( |
| 9536 | sInfo.applicationInfo.uid, sInfo.packageName, |
| 9537 | sInfo.name); |
| 9538 | } |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 9539 | r = new ServiceRecord(this, ss, name, filter, sInfo, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9540 | res.setService(r); |
| 9541 | mServices.put(name, r); |
| 9542 | mServicesByIntent.put(filter, r); |
| 9543 | |
| 9544 | // Make sure this component isn't in the pending list. |
| 9545 | int N = mPendingServices.size(); |
| 9546 | for (int i=0; i<N; i++) { |
| 9547 | ServiceRecord pr = mPendingServices.get(i); |
| 9548 | if (pr.name.equals(name)) { |
| 9549 | mPendingServices.remove(i); |
| 9550 | i--; |
| 9551 | N--; |
| 9552 | } |
| 9553 | } |
| 9554 | } |
| 9555 | } catch (RemoteException ex) { |
| 9556 | // pm is in same process, this will never happen. |
| 9557 | } |
| 9558 | } |
| 9559 | if (r != null) { |
| 9560 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 9561 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9562 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 9563 | if (!r.exported) { |
| 9564 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 9565 | + " from pid=" + callingPid |
| 9566 | + ", uid=" + callingUid |
| 9567 | + " that is not exported from uid " + r.appInfo.uid); |
| 9568 | return new ServiceLookupResult(null, "not exported from uid " |
| 9569 | + r.appInfo.uid); |
| 9570 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9571 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 9572 | + " from pid=" + callingPid |
| 9573 | + ", uid=" + callingUid |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9574 | + " requires " + r.permission); |
| 9575 | return new ServiceLookupResult(null, r.permission); |
| 9576 | } |
| 9577 | return new ServiceLookupResult(r, null); |
| 9578 | } |
| 9579 | return null; |
| 9580 | } |
| 9581 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9582 | private final void bumpServiceExecutingLocked(ServiceRecord r, String why) { |
| 9583 | if (DEBUG_SERVICE) Log.v(TAG, ">>> EXECUTING " |
| 9584 | + why + " of " + r + " in app " + r.app); |
| 9585 | else if (DEBUG_SERVICE_EXECUTING) Log.v(TAG, ">>> EXECUTING " |
| 9586 | + why + " of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9587 | long now = SystemClock.uptimeMillis(); |
| 9588 | if (r.executeNesting == 0 && r.app != null) { |
| 9589 | if (r.app.executingServices.size() == 0) { |
| 9590 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 9591 | msg.obj = r.app; |
| 9592 | mHandler.sendMessageAtTime(msg, now+SERVICE_TIMEOUT); |
| 9593 | } |
| 9594 | r.app.executingServices.add(r); |
| 9595 | } |
| 9596 | r.executeNesting++; |
| 9597 | r.executingStart = now; |
| 9598 | } |
| 9599 | |
| 9600 | private final void sendServiceArgsLocked(ServiceRecord r, |
| 9601 | boolean oomAdjusted) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9602 | final int N = r.pendingStarts.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9603 | if (N == 0) { |
| 9604 | return; |
| 9605 | } |
| 9606 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9607 | while (r.pendingStarts.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9608 | try { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9609 | ServiceRecord.StartItem si = r.pendingStarts.remove(0); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9610 | if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to: " |
| 9611 | + r + " " + r.intent + " args=" + si.intent); |
Dianne Hackborn | 3a28f22 | 2011-03-01 12:25:54 -0800 | [diff] [blame] | 9612 | if (si.intent == null && N > 1) { |
| 9613 | // If somehow we got a dummy null intent in the middle, |
| 9614 | // then skip it. DO NOT skip a null intent when it is |
| 9615 | // the only one in the list -- this is to support the |
| 9616 | // onStartCommand(null) case. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9617 | continue; |
| 9618 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9619 | si.deliveredTime = SystemClock.uptimeMillis(); |
| 9620 | r.deliveredStarts.add(si); |
| 9621 | si.deliveryCount++; |
Dianne Hackborn | 3a28f22 | 2011-03-01 12:25:54 -0800 | [diff] [blame] | 9622 | if (si.targetPermissionUid >= 0 && si.intent != null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9623 | grantUriPermissionUncheckedFromIntentLocked(si.targetPermissionUid, |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 9624 | r.packageName, si.intent, si.getUriPermissionsLocked()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9625 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9626 | bumpServiceExecutingLocked(r, "start"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9627 | if (!oomAdjusted) { |
| 9628 | oomAdjusted = true; |
| 9629 | updateOomAdjLocked(r.app); |
| 9630 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9631 | int flags = 0; |
| 9632 | if (si.deliveryCount > 0) { |
| 9633 | flags |= Service.START_FLAG_RETRY; |
| 9634 | } |
| 9635 | if (si.doneExecutingCount > 0) { |
| 9636 | flags |= Service.START_FLAG_REDELIVERY; |
| 9637 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 9638 | r.app.thread.scheduleServiceArgs(r, si.taskRemoved, si.id, flags, si.intent); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9639 | } catch (RemoteException e) { |
| 9640 | // Remote process gone... we'll let the normal cleanup take |
| 9641 | // care of this. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9642 | if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while scheduling start: " + r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9643 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9644 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9645 | Slog.w(TAG, "Unexpected exception", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9646 | break; |
| 9647 | } |
| 9648 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9649 | } |
| 9650 | |
| 9651 | private final boolean requestServiceBindingLocked(ServiceRecord r, |
| 9652 | IntentBindRecord i, boolean rebind) { |
| 9653 | if (r.app == null || r.app.thread == null) { |
| 9654 | // If service is not currently running, can't yet bind. |
| 9655 | return false; |
| 9656 | } |
| 9657 | if ((!i.requested || rebind) && i.apps.size() > 0) { |
| 9658 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9659 | bumpServiceExecutingLocked(r, "bind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9660 | r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind); |
| 9661 | if (!rebind) { |
| 9662 | i.requested = true; |
| 9663 | } |
| 9664 | i.hasBound = true; |
| 9665 | i.doRebind = false; |
| 9666 | } catch (RemoteException e) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9667 | 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] | 9668 | return false; |
| 9669 | } |
| 9670 | } |
| 9671 | return true; |
| 9672 | } |
| 9673 | |
| 9674 | private final void requestServiceBindingsLocked(ServiceRecord r) { |
| 9675 | Iterator<IntentBindRecord> bindings = r.bindings.values().iterator(); |
| 9676 | while (bindings.hasNext()) { |
| 9677 | IntentBindRecord i = bindings.next(); |
| 9678 | if (!requestServiceBindingLocked(r, i, false)) { |
| 9679 | break; |
| 9680 | } |
| 9681 | } |
| 9682 | } |
| 9683 | |
| 9684 | private final void realStartServiceLocked(ServiceRecord r, |
| 9685 | ProcessRecord app) throws RemoteException { |
| 9686 | if (app.thread == null) { |
| 9687 | throw new RemoteException(); |
| 9688 | } |
| 9689 | |
| 9690 | r.app = app; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 9691 | r.restartTime = r.lastActivity = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9692 | |
| 9693 | app.services.add(r); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9694 | bumpServiceExecutingLocked(r, "create"); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 9695 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9696 | |
| 9697 | boolean created = false; |
| 9698 | try { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 9699 | mStringBuilder.setLength(0); |
| 9700 | r.intent.getIntent().toShortString(mStringBuilder, false, true); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 9701 | EventLog.writeEvent(EventLogTags.AM_CREATE_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9702 | System.identityHashCode(r), r.shortName, |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 9703 | mStringBuilder.toString(), r.app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9704 | synchronized (r.stats.getBatteryStats()) { |
| 9705 | r.stats.startLaunchedLocked(); |
| 9706 | } |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 9707 | ensurePackageDexOpt(r.serviceInfo.packageName); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 9708 | app.thread.scheduleCreateService(r, r.serviceInfo, |
| 9709 | compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo)); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 9710 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9711 | created = true; |
| 9712 | } finally { |
| 9713 | if (!created) { |
| 9714 | app.services.remove(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9715 | scheduleServiceRestartLocked(r, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9716 | } |
| 9717 | } |
| 9718 | |
| 9719 | requestServiceBindingsLocked(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9720 | |
| 9721 | // If the service is in the started state, and there are no |
| 9722 | // pending arguments, then fake up one so its onStartCommand() will |
| 9723 | // be called. |
| 9724 | if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 9725 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
| 9726 | null, -1)); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9727 | } |
| 9728 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9729 | sendServiceArgsLocked(r, true); |
| 9730 | } |
| 9731 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9732 | private final boolean scheduleServiceRestartLocked(ServiceRecord r, |
| 9733 | boolean allowCancel) { |
| 9734 | boolean canceled = false; |
| 9735 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9736 | final long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9737 | long minDuration = SERVICE_RESTART_DURATION; |
Dianne Hackborn | 6ccd2af | 2009-08-27 12:26:44 -0700 | [diff] [blame] | 9738 | long resetTime = SERVICE_RESET_RUN_DURATION; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9739 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 9740 | if ((r.serviceInfo.applicationInfo.flags |
| 9741 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 9742 | minDuration /= 4; |
| 9743 | } |
| 9744 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9745 | // Any delivered but not yet finished starts should be put back |
| 9746 | // on the pending list. |
| 9747 | final int N = r.deliveredStarts.size(); |
| 9748 | if (N > 0) { |
| 9749 | for (int i=N-1; i>=0; i--) { |
| 9750 | ServiceRecord.StartItem si = r.deliveredStarts.get(i); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9751 | si.removeUriPermissionsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9752 | if (si.intent == null) { |
| 9753 | // We'll generate this again if needed. |
| 9754 | } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT |
| 9755 | && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) { |
| 9756 | r.pendingStarts.add(0, si); |
| 9757 | long dur = SystemClock.uptimeMillis() - si.deliveredTime; |
| 9758 | dur *= 2; |
| 9759 | if (minDuration < dur) minDuration = dur; |
| 9760 | if (resetTime < dur) resetTime = dur; |
| 9761 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9762 | Slog.w(TAG, "Canceling start item " + si.intent + " in service " |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9763 | + r.name); |
| 9764 | canceled = true; |
| 9765 | } |
| 9766 | } |
| 9767 | r.deliveredStarts.clear(); |
| 9768 | } |
| 9769 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9770 | r.totalRestartCount++; |
| 9771 | if (r.restartDelay == 0) { |
| 9772 | r.restartCount++; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9773 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9774 | } else { |
| 9775 | // If it has been a "reasonably long time" since the service |
| 9776 | // was started, then reset our restart duration back to |
| 9777 | // the beginning, so we don't infinitely increase the duration |
| 9778 | // on a service that just occasionally gets killed (which is |
| 9779 | // a normal case, due to process being killed to reclaim memory). |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9780 | if (now > (r.restartTime+resetTime)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9781 | r.restartCount = 1; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9782 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9783 | } else { |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 9784 | if ((r.serviceInfo.applicationInfo.flags |
| 9785 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 9786 | // Services in peristent processes will restart much more |
| 9787 | // quickly, since they are pretty important. (Think SystemUI). |
| 9788 | r.restartDelay += minDuration/2; |
| 9789 | } else { |
| 9790 | r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR; |
| 9791 | if (r.restartDelay < minDuration) { |
| 9792 | r.restartDelay = minDuration; |
| 9793 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9794 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9795 | } |
| 9796 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9797 | |
| 9798 | r.nextRestartTime = now + r.restartDelay; |
| 9799 | |
| 9800 | // Make sure that we don't end up restarting a bunch of services |
| 9801 | // all at the same time. |
| 9802 | boolean repeat; |
| 9803 | do { |
| 9804 | repeat = false; |
| 9805 | for (int i=mRestartingServices.size()-1; i>=0; i--) { |
| 9806 | ServiceRecord r2 = mRestartingServices.get(i); |
| 9807 | if (r2 != r && r.nextRestartTime |
| 9808 | >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN) |
| 9809 | && r.nextRestartTime |
| 9810 | < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) { |
| 9811 | r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN; |
| 9812 | r.restartDelay = r.nextRestartTime - now; |
| 9813 | repeat = true; |
| 9814 | break; |
| 9815 | } |
| 9816 | } |
| 9817 | } while (repeat); |
| 9818 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9819 | if (!mRestartingServices.contains(r)) { |
| 9820 | mRestartingServices.add(r); |
| 9821 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9822 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 9823 | r.cancelNotification(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9824 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9825 | mHandler.removeCallbacks(r.restarter); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9826 | mHandler.postAtTime(r.restarter, r.nextRestartTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9827 | r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9828 | Slog.w(TAG, "Scheduling restart of crashed service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9829 | + r.shortName + " in " + r.restartDelay + "ms"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 9830 | EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9831 | r.shortName, r.restartDelay); |
| 9832 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9833 | return canceled; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9834 | } |
| 9835 | |
| 9836 | final void performServiceRestartLocked(ServiceRecord r) { |
| 9837 | if (!mRestartingServices.contains(r)) { |
| 9838 | return; |
| 9839 | } |
| 9840 | bringUpServiceLocked(r, r.intent.getIntent().getFlags(), true); |
| 9841 | } |
| 9842 | |
| 9843 | private final boolean unscheduleServiceRestartLocked(ServiceRecord r) { |
| 9844 | if (r.restartDelay == 0) { |
| 9845 | return false; |
| 9846 | } |
| 9847 | r.resetRestartCounter(); |
| 9848 | mRestartingServices.remove(r); |
| 9849 | mHandler.removeCallbacks(r.restarter); |
| 9850 | return true; |
| 9851 | } |
| 9852 | |
| 9853 | private final boolean bringUpServiceLocked(ServiceRecord r, |
| 9854 | int intentFlags, boolean whileRestarting) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9855 | //Slog.i(TAG, "Bring up service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9856 | //r.dump(" "); |
| 9857 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 9858 | if (r.app != null && r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9859 | sendServiceArgsLocked(r, false); |
| 9860 | return true; |
| 9861 | } |
| 9862 | |
| 9863 | if (!whileRestarting && r.restartDelay > 0) { |
| 9864 | // If waiting for a restart, then do nothing. |
| 9865 | return true; |
| 9866 | } |
| 9867 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9868 | 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] | 9869 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 9870 | // We are now bringing the service up, so no longer in the |
| 9871 | // restarting state. |
| 9872 | mRestartingServices.remove(r); |
| 9873 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 9874 | // Service is now being launched, its package can't be stopped. |
| 9875 | try { |
| 9876 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 9877 | r.packageName, false); |
| 9878 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 9879 | } catch (IllegalArgumentException e) { |
| 9880 | Slog.w(TAG, "Failed trying to unstop package " |
| 9881 | + r.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 9882 | } |
| 9883 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9884 | final String appName = r.processName; |
| 9885 | ProcessRecord app = getProcessRecordLocked(appName, r.appInfo.uid); |
| 9886 | if (app != null && app.thread != null) { |
| 9887 | try { |
| 9888 | realStartServiceLocked(r, app); |
| 9889 | return true; |
| 9890 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9891 | 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] | 9892 | } |
| 9893 | |
| 9894 | // If a dead object exception was thrown -- fall through to |
| 9895 | // restart the application. |
| 9896 | } |
| 9897 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 9898 | // Not running -- get it started, and enqueue this service record |
| 9899 | // to be executed when the app comes up. |
| 9900 | if (startProcessLocked(appName, r.appInfo, true, intentFlags, |
| 9901 | "service", r.name, false) == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9902 | Slog.w(TAG, "Unable to launch app " |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 9903 | + r.appInfo.packageName + "/" |
| 9904 | + r.appInfo.uid + " for service " |
| 9905 | + r.intent.getIntent() + ": process is bad"); |
| 9906 | bringDownServiceLocked(r, true); |
| 9907 | return false; |
| 9908 | } |
| 9909 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9910 | if (!mPendingServices.contains(r)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9911 | mPendingServices.add(r); |
| 9912 | } |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 9913 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9914 | return true; |
| 9915 | } |
| 9916 | |
| 9917 | private final void bringDownServiceLocked(ServiceRecord r, boolean force) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9918 | //Slog.i(TAG, "Bring down service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9919 | //r.dump(" "); |
| 9920 | |
| 9921 | // Does it still need to run? |
| 9922 | if (!force && r.startRequested) { |
| 9923 | return; |
| 9924 | } |
| 9925 | if (r.connections.size() > 0) { |
| 9926 | if (!force) { |
| 9927 | // XXX should probably keep a count of the number of auto-create |
| 9928 | // connections directly in the service. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9929 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9930 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9931 | ArrayList<ConnectionRecord> cr = it.next(); |
| 9932 | for (int i=0; i<cr.size(); i++) { |
| 9933 | if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) { |
| 9934 | return; |
| 9935 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9936 | } |
| 9937 | } |
| 9938 | } |
| 9939 | |
| 9940 | // Report to all of the connections that the service is no longer |
| 9941 | // available. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9942 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9943 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9944 | ArrayList<ConnectionRecord> c = it.next(); |
| 9945 | for (int i=0; i<c.size(); i++) { |
| 9946 | try { |
| 9947 | c.get(i).conn.connected(r.name, null); |
| 9948 | } catch (Exception e) { |
| 9949 | Slog.w(TAG, "Failure disconnecting service " + r.name + |
| 9950 | " to connection " + c.get(i).conn.asBinder() + |
| 9951 | " (in " + c.get(i).binding.client.processName + ")", e); |
| 9952 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9953 | } |
| 9954 | } |
| 9955 | } |
| 9956 | |
| 9957 | // Tell the service that it has been unbound. |
| 9958 | if (r.bindings.size() > 0 && r.app != null && r.app.thread != null) { |
| 9959 | Iterator<IntentBindRecord> it = r.bindings.values().iterator(); |
| 9960 | while (it.hasNext()) { |
| 9961 | IntentBindRecord ibr = it.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9962 | 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] | 9963 | + ": hasBound=" + ibr.hasBound); |
| 9964 | if (r.app != null && r.app.thread != null && ibr.hasBound) { |
| 9965 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9966 | bumpServiceExecutingLocked(r, "bring down unbind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9967 | updateOomAdjLocked(r.app); |
| 9968 | ibr.hasBound = false; |
| 9969 | r.app.thread.scheduleUnbindService(r, |
| 9970 | ibr.intent.getIntent()); |
| 9971 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9972 | Slog.w(TAG, "Exception when unbinding service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9973 | + r.shortName, e); |
| 9974 | serviceDoneExecutingLocked(r, true); |
| 9975 | } |
| 9976 | } |
| 9977 | } |
| 9978 | } |
| 9979 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9980 | if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down " + r + " " + r.intent); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 9981 | EventLog.writeEvent(EventLogTags.AM_DESTROY_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9982 | System.identityHashCode(r), r.shortName, |
| 9983 | (r.app != null) ? r.app.pid : -1); |
| 9984 | |
| 9985 | mServices.remove(r.name); |
| 9986 | mServicesByIntent.remove(r.intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9987 | r.totalRestartCount = 0; |
| 9988 | unscheduleServiceRestartLocked(r); |
| 9989 | |
| 9990 | // Also make sure it is not on the pending list. |
| 9991 | int N = mPendingServices.size(); |
| 9992 | for (int i=0; i<N; i++) { |
| 9993 | if (mPendingServices.get(i) == r) { |
| 9994 | mPendingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9995 | if (DEBUG_SERVICE) Slog.v(TAG, "Removed pending: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9996 | i--; |
| 9997 | N--; |
| 9998 | } |
| 9999 | } |
| 10000 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10001 | r.cancelNotification(); |
| 10002 | r.isForeground = false; |
| 10003 | r.foregroundId = 0; |
| 10004 | r.foregroundNoti = null; |
| 10005 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10006 | // Clear start entries. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10007 | r.clearDeliveredStartsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10008 | r.pendingStarts.clear(); |
| 10009 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10010 | if (r.app != null) { |
| 10011 | synchronized (r.stats.getBatteryStats()) { |
| 10012 | r.stats.stopLaunchedLocked(); |
| 10013 | } |
| 10014 | r.app.services.remove(r); |
| 10015 | if (r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10016 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10017 | bumpServiceExecutingLocked(r, "stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10018 | mStoppingServices.add(r); |
| 10019 | updateOomAdjLocked(r.app); |
| 10020 | r.app.thread.scheduleStopService(r); |
| 10021 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10022 | Slog.w(TAG, "Exception when stopping service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10023 | + r.shortName, e); |
| 10024 | serviceDoneExecutingLocked(r, true); |
| 10025 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10026 | updateServiceForegroundLocked(r.app, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10027 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10028 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10029 | TAG, "Removed service that has no process: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10030 | } |
| 10031 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10032 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10033 | TAG, "Removed service that is not running: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10034 | } |
Vairavan Srinivasan | a207ce2 | 2010-12-23 13:51:48 -0800 | [diff] [blame] | 10035 | |
| 10036 | if (r.bindings.size() > 0) { |
| 10037 | r.bindings.clear(); |
| 10038 | } |
| 10039 | |
| 10040 | if (r.restarter instanceof ServiceRestarter) { |
| 10041 | ((ServiceRestarter)r.restarter).setService(null); |
| 10042 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10043 | } |
| 10044 | |
| 10045 | ComponentName startServiceLocked(IApplicationThread caller, |
| 10046 | Intent service, String resolvedType, |
| 10047 | int callingPid, int callingUid) { |
| 10048 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10049 | if (DEBUG_SERVICE) Slog.v(TAG, "startService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10050 | + " type=" + resolvedType + " args=" + service.getExtras()); |
| 10051 | |
| 10052 | if (caller != null) { |
| 10053 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 10054 | if (callerApp == null) { |
| 10055 | throw new SecurityException( |
| 10056 | "Unable to find app for caller " + caller |
| 10057 | + " (pid=" + Binder.getCallingPid() |
| 10058 | + ") when starting service " + service); |
| 10059 | } |
| 10060 | } |
| 10061 | |
| 10062 | ServiceLookupResult res = |
| 10063 | retrieveServiceLocked(service, resolvedType, |
| 10064 | callingPid, callingUid); |
| 10065 | if (res == null) { |
| 10066 | return null; |
| 10067 | } |
| 10068 | if (res.record == null) { |
| 10069 | return new ComponentName("!", res.permission != null |
| 10070 | ? res.permission : "private to package"); |
| 10071 | } |
| 10072 | ServiceRecord r = res.record; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10073 | int targetPermissionUid = checkGrantUriPermissionFromIntentLocked( |
| 10074 | callingUid, r.packageName, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10075 | if (unscheduleServiceRestartLocked(r)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10076 | 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] | 10077 | } |
| 10078 | r.startRequested = true; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10079 | r.callStart = false; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10080 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10081 | service, targetPermissionUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10082 | r.lastActivity = SystemClock.uptimeMillis(); |
| 10083 | synchronized (r.stats.getBatteryStats()) { |
| 10084 | r.stats.startRunningLocked(); |
| 10085 | } |
| 10086 | if (!bringUpServiceLocked(r, service.getFlags(), false)) { |
| 10087 | return new ComponentName("!", "Service process is bad"); |
| 10088 | } |
| 10089 | return r.name; |
| 10090 | } |
| 10091 | } |
| 10092 | |
| 10093 | public ComponentName startService(IApplicationThread caller, Intent service, |
| 10094 | String resolvedType) { |
| 10095 | // Refuse possible leaked file descriptors |
| 10096 | if (service != null && service.hasFileDescriptors() == true) { |
| 10097 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10098 | } |
| 10099 | |
| 10100 | synchronized(this) { |
| 10101 | final int callingPid = Binder.getCallingPid(); |
| 10102 | final int callingUid = Binder.getCallingUid(); |
| 10103 | final long origId = Binder.clearCallingIdentity(); |
| 10104 | ComponentName res = startServiceLocked(caller, service, |
| 10105 | resolvedType, callingPid, callingUid); |
| 10106 | Binder.restoreCallingIdentity(origId); |
| 10107 | return res; |
| 10108 | } |
| 10109 | } |
| 10110 | |
| 10111 | ComponentName startServiceInPackage(int uid, |
| 10112 | Intent service, String resolvedType) { |
| 10113 | synchronized(this) { |
| 10114 | final long origId = Binder.clearCallingIdentity(); |
| 10115 | ComponentName res = startServiceLocked(null, service, |
| 10116 | resolvedType, -1, uid); |
| 10117 | Binder.restoreCallingIdentity(origId); |
| 10118 | return res; |
| 10119 | } |
| 10120 | } |
| 10121 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10122 | private void stopServiceLocked(ServiceRecord service) { |
| 10123 | synchronized (service.stats.getBatteryStats()) { |
| 10124 | service.stats.stopRunningLocked(); |
| 10125 | } |
| 10126 | service.startRequested = false; |
| 10127 | service.callStart = false; |
| 10128 | bringDownServiceLocked(service, false); |
| 10129 | } |
| 10130 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10131 | public int stopService(IApplicationThread caller, Intent service, |
| 10132 | String resolvedType) { |
| 10133 | // Refuse possible leaked file descriptors |
| 10134 | if (service != null && service.hasFileDescriptors() == true) { |
| 10135 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10136 | } |
| 10137 | |
| 10138 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10139 | if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10140 | + " type=" + resolvedType); |
| 10141 | |
| 10142 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 10143 | if (caller != null && callerApp == null) { |
| 10144 | throw new SecurityException( |
| 10145 | "Unable to find app for caller " + caller |
| 10146 | + " (pid=" + Binder.getCallingPid() |
| 10147 | + ") when stopping service " + service); |
| 10148 | } |
| 10149 | |
| 10150 | // If this service is active, make sure it is stopped. |
| 10151 | ServiceLookupResult r = findServiceLocked(service, resolvedType); |
| 10152 | if (r != null) { |
| 10153 | if (r.record != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10154 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10155 | try { |
| 10156 | stopServiceLocked(r.record); |
| 10157 | } finally { |
| 10158 | Binder.restoreCallingIdentity(origId); |
| 10159 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10160 | return 1; |
| 10161 | } |
| 10162 | return -1; |
| 10163 | } |
| 10164 | } |
| 10165 | |
| 10166 | return 0; |
| 10167 | } |
| 10168 | |
| 10169 | public IBinder peekService(Intent service, String resolvedType) { |
| 10170 | // Refuse possible leaked file descriptors |
| 10171 | if (service != null && service.hasFileDescriptors() == true) { |
| 10172 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10173 | } |
| 10174 | |
| 10175 | IBinder ret = null; |
| 10176 | |
| 10177 | synchronized(this) { |
| 10178 | ServiceLookupResult r = findServiceLocked(service, resolvedType); |
| 10179 | |
| 10180 | if (r != null) { |
| 10181 | // r.record is null if findServiceLocked() failed the caller permission check |
| 10182 | if (r.record == null) { |
| 10183 | throw new SecurityException( |
| 10184 | "Permission Denial: Accessing service " + r.record.name |
| 10185 | + " from pid=" + Binder.getCallingPid() |
| 10186 | + ", uid=" + Binder.getCallingUid() |
| 10187 | + " requires " + r.permission); |
| 10188 | } |
| 10189 | IntentBindRecord ib = r.record.bindings.get(r.record.intent); |
| 10190 | if (ib != null) { |
| 10191 | ret = ib.binder; |
| 10192 | } |
| 10193 | } |
| 10194 | } |
| 10195 | |
| 10196 | return ret; |
| 10197 | } |
| 10198 | |
| 10199 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 10200 | int startId) { |
| 10201 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10202 | if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10203 | + " " + token + " startId=" + startId); |
| 10204 | ServiceRecord r = findServiceLocked(className, token); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10205 | if (r != null) { |
| 10206 | if (startId >= 0) { |
| 10207 | // Asked to only stop if done with all work. Note that |
| 10208 | // to avoid leaks, we will take this as dropping all |
| 10209 | // start items up to and including this one. |
| 10210 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 10211 | if (si != null) { |
| 10212 | while (r.deliveredStarts.size() > 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10213 | ServiceRecord.StartItem cur = r.deliveredStarts.remove(0); |
| 10214 | cur.removeUriPermissionsLocked(); |
| 10215 | if (cur == si) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10216 | break; |
| 10217 | } |
| 10218 | } |
| 10219 | } |
| 10220 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10221 | if (r.getLastStartId() != startId) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10222 | return false; |
| 10223 | } |
| 10224 | |
| 10225 | if (r.deliveredStarts.size() > 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10226 | Slog.w(TAG, "stopServiceToken startId " + startId |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10227 | + " is last, but have " + r.deliveredStarts.size() |
| 10228 | + " remaining args"); |
| 10229 | } |
| 10230 | } |
| 10231 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10232 | synchronized (r.stats.getBatteryStats()) { |
| 10233 | r.stats.stopRunningLocked(); |
| 10234 | r.startRequested = false; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10235 | r.callStart = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10236 | } |
| 10237 | final long origId = Binder.clearCallingIdentity(); |
| 10238 | bringDownServiceLocked(r, false); |
| 10239 | Binder.restoreCallingIdentity(origId); |
| 10240 | return true; |
| 10241 | } |
| 10242 | } |
| 10243 | return false; |
| 10244 | } |
| 10245 | |
| 10246 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10247 | int id, Notification notification, boolean removeNotification) { |
| 10248 | final long origId = Binder.clearCallingIdentity(); |
| 10249 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10250 | synchronized(this) { |
| 10251 | ServiceRecord r = findServiceLocked(className, token); |
| 10252 | if (r != null) { |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10253 | if (id != 0) { |
| 10254 | if (notification == null) { |
| 10255 | throw new IllegalArgumentException("null notification"); |
| 10256 | } |
| 10257 | if (r.foregroundId != id) { |
| 10258 | r.cancelNotification(); |
| 10259 | r.foregroundId = id; |
| 10260 | } |
| 10261 | notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 10262 | r.foregroundNoti = notification; |
| 10263 | r.isForeground = true; |
| 10264 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10265 | if (r.app != null) { |
| 10266 | updateServiceForegroundLocked(r.app, true); |
| 10267 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10268 | } else { |
| 10269 | if (r.isForeground) { |
| 10270 | r.isForeground = false; |
| 10271 | if (r.app != null) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 10272 | updateLruProcessLocked(r.app, false, true); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10273 | updateServiceForegroundLocked(r.app, true); |
| 10274 | } |
| 10275 | } |
| 10276 | if (removeNotification) { |
| 10277 | r.cancelNotification(); |
| 10278 | r.foregroundId = 0; |
| 10279 | r.foregroundNoti = null; |
| 10280 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10281 | } |
| 10282 | } |
| 10283 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10284 | } finally { |
| 10285 | Binder.restoreCallingIdentity(origId); |
| 10286 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10287 | } |
| 10288 | |
| 10289 | public void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) { |
| 10290 | boolean anyForeground = false; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10291 | for (ServiceRecord sr : proc.services) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10292 | if (sr.isForeground) { |
| 10293 | anyForeground = true; |
| 10294 | break; |
| 10295 | } |
| 10296 | } |
| 10297 | if (anyForeground != proc.foregroundServices) { |
| 10298 | proc.foregroundServices = anyForeground; |
| 10299 | if (oomAdj) { |
| 10300 | updateOomAdjLocked(); |
| 10301 | } |
| 10302 | } |
| 10303 | } |
| 10304 | |
| 10305 | public int bindService(IApplicationThread caller, IBinder token, |
| 10306 | Intent service, String resolvedType, |
| 10307 | IServiceConnection connection, int flags) { |
| 10308 | // Refuse possible leaked file descriptors |
| 10309 | if (service != null && service.hasFileDescriptors() == true) { |
| 10310 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10311 | } |
| 10312 | |
| 10313 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10314 | if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10315 | + " type=" + resolvedType + " conn=" + connection.asBinder() |
| 10316 | + " flags=0x" + Integer.toHexString(flags)); |
| 10317 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 10318 | if (callerApp == null) { |
| 10319 | throw new SecurityException( |
| 10320 | "Unable to find app for caller " + caller |
| 10321 | + " (pid=" + Binder.getCallingPid() |
| 10322 | + ") when binding service " + service); |
| 10323 | } |
| 10324 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10325 | ActivityRecord activity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10326 | if (token != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 10327 | int aindex = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10328 | if (aindex < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10329 | Slog.w(TAG, "Binding with unknown activity: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10330 | return 0; |
| 10331 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 10332 | activity = (ActivityRecord)mMainStack.mHistory.get(aindex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10333 | } |
| 10334 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10335 | int clientLabel = 0; |
| 10336 | PendingIntent clientIntent = null; |
| 10337 | |
| 10338 | if (callerApp.info.uid == Process.SYSTEM_UID) { |
| 10339 | // Hacky kind of thing -- allow system stuff to tell us |
| 10340 | // what they are, so we can report this elsewhere for |
| 10341 | // others to know why certain services are running. |
| 10342 | try { |
| 10343 | clientIntent = (PendingIntent)service.getParcelableExtra( |
| 10344 | Intent.EXTRA_CLIENT_INTENT); |
| 10345 | } catch (RuntimeException e) { |
| 10346 | } |
| 10347 | if (clientIntent != null) { |
| 10348 | clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0); |
| 10349 | if (clientLabel != 0) { |
| 10350 | // There are no useful extras in the intent, trash them. |
| 10351 | // System code calling with this stuff just needs to know |
| 10352 | // this will happen. |
| 10353 | service = service.cloneFilter(); |
| 10354 | } |
| 10355 | } |
| 10356 | } |
| 10357 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10358 | ServiceLookupResult res = |
| 10359 | retrieveServiceLocked(service, resolvedType, |
| 10360 | Binder.getCallingPid(), Binder.getCallingUid()); |
| 10361 | if (res == null) { |
| 10362 | return 0; |
| 10363 | } |
| 10364 | if (res.record == null) { |
| 10365 | return -1; |
| 10366 | } |
| 10367 | ServiceRecord s = res.record; |
| 10368 | |
| 10369 | final long origId = Binder.clearCallingIdentity(); |
| 10370 | |
| 10371 | if (unscheduleServiceRestartLocked(s)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10372 | if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: " |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10373 | + s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10374 | } |
| 10375 | |
| 10376 | AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp); |
| 10377 | ConnectionRecord c = new ConnectionRecord(b, activity, |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10378 | connection, flags, clientLabel, clientIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10379 | |
| 10380 | IBinder binder = connection.asBinder(); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10381 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 10382 | if (clist == null) { |
| 10383 | clist = new ArrayList<ConnectionRecord>(); |
| 10384 | s.connections.put(binder, clist); |
| 10385 | } |
| 10386 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10387 | b.connections.add(c); |
| 10388 | if (activity != null) { |
| 10389 | if (activity.connections == null) { |
| 10390 | activity.connections = new HashSet<ConnectionRecord>(); |
| 10391 | } |
| 10392 | activity.connections.add(c); |
| 10393 | } |
| 10394 | b.client.connections.add(c); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10395 | clist = mServiceConnections.get(binder); |
| 10396 | if (clist == null) { |
| 10397 | clist = new ArrayList<ConnectionRecord>(); |
| 10398 | mServiceConnections.put(binder, clist); |
| 10399 | } |
| 10400 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10401 | |
| 10402 | if ((flags&Context.BIND_AUTO_CREATE) != 0) { |
| 10403 | s.lastActivity = SystemClock.uptimeMillis(); |
| 10404 | if (!bringUpServiceLocked(s, service.getFlags(), false)) { |
| 10405 | return 0; |
| 10406 | } |
| 10407 | } |
| 10408 | |
| 10409 | if (s.app != null) { |
| 10410 | // This could have made the service more important. |
| 10411 | updateOomAdjLocked(s.app); |
| 10412 | } |
| 10413 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10414 | 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] | 10415 | + ": received=" + b.intent.received |
| 10416 | + " apps=" + b.intent.apps.size() |
| 10417 | + " doRebind=" + b.intent.doRebind); |
| 10418 | |
| 10419 | if (s.app != null && b.intent.received) { |
| 10420 | // Service is already running, so we can immediately |
| 10421 | // publish the connection. |
| 10422 | try { |
| 10423 | c.conn.connected(s.name, b.intent.binder); |
| 10424 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10425 | Slog.w(TAG, "Failure sending service " + s.shortName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10426 | + " to connection " + c.conn.asBinder() |
| 10427 | + " (in " + c.binding.client.processName + ")", e); |
| 10428 | } |
| 10429 | |
| 10430 | // If this is the first app connected back to this binding, |
| 10431 | // and the service had previously asked to be told when |
| 10432 | // rebound, then do so. |
| 10433 | if (b.intent.apps.size() == 1 && b.intent.doRebind) { |
| 10434 | requestServiceBindingLocked(s, b.intent, true); |
| 10435 | } |
| 10436 | } else if (!b.intent.requested) { |
| 10437 | requestServiceBindingLocked(s, b.intent, false); |
| 10438 | } |
| 10439 | |
| 10440 | Binder.restoreCallingIdentity(origId); |
| 10441 | } |
| 10442 | |
| 10443 | return 1; |
| 10444 | } |
| 10445 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 10446 | void removeConnectionLocked( |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10447 | ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10448 | IBinder binder = c.conn.asBinder(); |
| 10449 | AppBindRecord b = c.binding; |
| 10450 | ServiceRecord s = b.service; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10451 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 10452 | if (clist != null) { |
| 10453 | clist.remove(c); |
| 10454 | if (clist.size() == 0) { |
| 10455 | s.connections.remove(binder); |
| 10456 | } |
| 10457 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10458 | b.connections.remove(c); |
| 10459 | if (c.activity != null && c.activity != skipAct) { |
| 10460 | if (c.activity.connections != null) { |
| 10461 | c.activity.connections.remove(c); |
| 10462 | } |
| 10463 | } |
| 10464 | if (b.client != skipApp) { |
| 10465 | b.client.connections.remove(c); |
| 10466 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10467 | clist = mServiceConnections.get(binder); |
| 10468 | if (clist != null) { |
| 10469 | clist.remove(c); |
| 10470 | if (clist.size() == 0) { |
| 10471 | mServiceConnections.remove(binder); |
| 10472 | } |
| 10473 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10474 | |
| 10475 | if (b.connections.size() == 0) { |
| 10476 | b.intent.apps.remove(b.client); |
| 10477 | } |
| 10478 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10479 | if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10480 | + ": shouldUnbind=" + b.intent.hasBound); |
| 10481 | if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0 |
| 10482 | && b.intent.hasBound) { |
| 10483 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10484 | bumpServiceExecutingLocked(s, "unbind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10485 | updateOomAdjLocked(s.app); |
| 10486 | b.intent.hasBound = false; |
| 10487 | // Assume the client doesn't want to know about a rebind; |
| 10488 | // we will deal with that later if it asks for one. |
| 10489 | b.intent.doRebind = false; |
| 10490 | s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent()); |
| 10491 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10492 | Slog.w(TAG, "Exception when unbinding service " + s.shortName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10493 | serviceDoneExecutingLocked(s, true); |
| 10494 | } |
| 10495 | } |
| 10496 | |
| 10497 | if ((c.flags&Context.BIND_AUTO_CREATE) != 0) { |
| 10498 | bringDownServiceLocked(s, false); |
| 10499 | } |
| 10500 | } |
| 10501 | |
| 10502 | public boolean unbindService(IServiceConnection connection) { |
| 10503 | synchronized (this) { |
| 10504 | IBinder binder = connection.asBinder(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10505 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10506 | ArrayList<ConnectionRecord> clist = mServiceConnections.get(binder); |
| 10507 | if (clist == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10508 | 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] | 10509 | + connection.asBinder()); |
| 10510 | return false; |
| 10511 | } |
| 10512 | |
| 10513 | final long origId = Binder.clearCallingIdentity(); |
| 10514 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10515 | while (clist.size() > 0) { |
| 10516 | ConnectionRecord r = clist.get(0); |
| 10517 | removeConnectionLocked(r, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10518 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10519 | if (r.binding.service.app != null) { |
| 10520 | // This could have made the service less important. |
| 10521 | updateOomAdjLocked(r.binding.service.app); |
| 10522 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10523 | } |
| 10524 | |
| 10525 | Binder.restoreCallingIdentity(origId); |
| 10526 | } |
| 10527 | |
| 10528 | return true; |
| 10529 | } |
| 10530 | |
| 10531 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 10532 | // Refuse possible leaked file descriptors |
| 10533 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 10534 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10535 | } |
| 10536 | |
| 10537 | synchronized(this) { |
| 10538 | if (!(token instanceof ServiceRecord)) { |
| 10539 | throw new IllegalArgumentException("Invalid service token"); |
| 10540 | } |
| 10541 | ServiceRecord r = (ServiceRecord)token; |
| 10542 | |
| 10543 | final long origId = Binder.clearCallingIdentity(); |
| 10544 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10545 | if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10546 | + " " + intent + ": " + service); |
| 10547 | if (r != null) { |
| 10548 | Intent.FilterComparison filter |
| 10549 | = new Intent.FilterComparison(intent); |
| 10550 | IntentBindRecord b = r.bindings.get(filter); |
| 10551 | if (b != null && !b.received) { |
| 10552 | b.binder = service; |
| 10553 | b.requested = true; |
| 10554 | b.received = true; |
| 10555 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10556 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10557 | = r.connections.values().iterator(); |
| 10558 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10559 | ArrayList<ConnectionRecord> clist = it.next(); |
| 10560 | for (int i=0; i<clist.size(); i++) { |
| 10561 | ConnectionRecord c = clist.get(i); |
| 10562 | if (!filter.equals(c.binding.intent.intent)) { |
| 10563 | if (DEBUG_SERVICE) Slog.v( |
| 10564 | TAG, "Not publishing to: " + c); |
| 10565 | if (DEBUG_SERVICE) Slog.v( |
| 10566 | TAG, "Bound intent: " + c.binding.intent.intent); |
| 10567 | if (DEBUG_SERVICE) Slog.v( |
| 10568 | TAG, "Published intent: " + intent); |
| 10569 | continue; |
| 10570 | } |
| 10571 | if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c); |
| 10572 | try { |
| 10573 | c.conn.connected(r.name, service); |
| 10574 | } catch (Exception e) { |
| 10575 | Slog.w(TAG, "Failure sending service " + r.name + |
| 10576 | " to connection " + c.conn.asBinder() + |
| 10577 | " (in " + c.binding.client.processName + ")", e); |
| 10578 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10579 | } |
| 10580 | } |
| 10581 | } |
| 10582 | } |
| 10583 | |
| 10584 | serviceDoneExecutingLocked(r, mStoppingServices.contains(r)); |
| 10585 | |
| 10586 | Binder.restoreCallingIdentity(origId); |
| 10587 | } |
| 10588 | } |
| 10589 | } |
| 10590 | |
| 10591 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 10592 | // Refuse possible leaked file descriptors |
| 10593 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 10594 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10595 | } |
| 10596 | |
| 10597 | synchronized(this) { |
| 10598 | if (!(token instanceof ServiceRecord)) { |
| 10599 | throw new IllegalArgumentException("Invalid service token"); |
| 10600 | } |
| 10601 | ServiceRecord r = (ServiceRecord)token; |
| 10602 | |
| 10603 | final long origId = Binder.clearCallingIdentity(); |
| 10604 | |
| 10605 | if (r != null) { |
| 10606 | Intent.FilterComparison filter |
| 10607 | = new Intent.FilterComparison(intent); |
| 10608 | IntentBindRecord b = r.bindings.get(filter); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10609 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10610 | + " at " + b + ": apps=" |
| 10611 | + (b != null ? b.apps.size() : 0)); |
| 10612 | if (b != null) { |
| 10613 | if (b.apps.size() > 0) { |
| 10614 | // Applications have already bound since the last |
| 10615 | // unbind, so just rebind right here. |
| 10616 | requestServiceBindingLocked(r, b, true); |
| 10617 | } else { |
| 10618 | // Note to tell the service the next time there is |
| 10619 | // a new client. |
| 10620 | b.doRebind = true; |
| 10621 | } |
| 10622 | } |
| 10623 | |
| 10624 | serviceDoneExecutingLocked(r, mStoppingServices.contains(r)); |
| 10625 | |
| 10626 | Binder.restoreCallingIdentity(origId); |
| 10627 | } |
| 10628 | } |
| 10629 | } |
| 10630 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10631 | 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] | 10632 | synchronized(this) { |
| 10633 | if (!(token instanceof ServiceRecord)) { |
| 10634 | throw new IllegalArgumentException("Invalid service token"); |
| 10635 | } |
| 10636 | ServiceRecord r = (ServiceRecord)token; |
| 10637 | boolean inStopping = mStoppingServices.contains(token); |
| 10638 | if (r != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10639 | if (r != token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10640 | Slog.w(TAG, "Done executing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10641 | + " with incorrect token: given " + token |
| 10642 | + ", expected " + r); |
| 10643 | return; |
| 10644 | } |
| 10645 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10646 | if (type == 1) { |
| 10647 | // This is a call from a service start... take care of |
| 10648 | // book-keeping. |
| 10649 | r.callStart = true; |
| 10650 | switch (res) { |
| 10651 | case Service.START_STICKY_COMPATIBILITY: |
| 10652 | case Service.START_STICKY: { |
| 10653 | // We are done with the associated start arguments. |
| 10654 | r.findDeliveredStart(startId, true); |
| 10655 | // Don't stop if killed. |
| 10656 | r.stopIfKilled = false; |
| 10657 | break; |
| 10658 | } |
| 10659 | case Service.START_NOT_STICKY: { |
| 10660 | // We are done with the associated start arguments. |
| 10661 | r.findDeliveredStart(startId, true); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10662 | if (r.getLastStartId() == startId) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10663 | // There is no more work, and this service |
| 10664 | // doesn't want to hang around if killed. |
| 10665 | r.stopIfKilled = true; |
| 10666 | } |
| 10667 | break; |
| 10668 | } |
| 10669 | case Service.START_REDELIVER_INTENT: { |
| 10670 | // We'll keep this item until they explicitly |
| 10671 | // call stop for it, but keep track of the fact |
| 10672 | // that it was delivered. |
| 10673 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 10674 | if (si != null) { |
| 10675 | si.deliveryCount = 0; |
| 10676 | si.doneExecutingCount++; |
| 10677 | // Don't stop if killed. |
| 10678 | r.stopIfKilled = true; |
| 10679 | } |
| 10680 | break; |
| 10681 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10682 | case Service.START_TASK_REMOVED_COMPLETE: { |
| 10683 | // Special processing for onTaskRemoved(). Don't |
| 10684 | // impact normal onStartCommand() processing. |
| 10685 | r.findDeliveredStart(startId, true); |
| 10686 | break; |
| 10687 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10688 | default: |
| 10689 | throw new IllegalArgumentException( |
| 10690 | "Unknown service start result: " + res); |
| 10691 | } |
| 10692 | if (res == Service.START_STICKY_COMPATIBILITY) { |
| 10693 | r.callStart = false; |
| 10694 | } |
| 10695 | } |
| 10696 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10697 | final long origId = Binder.clearCallingIdentity(); |
| 10698 | serviceDoneExecutingLocked(r, inStopping); |
| 10699 | Binder.restoreCallingIdentity(origId); |
| 10700 | } else { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10701 | Slog.w(TAG, "Done executing unknown service from pid " |
| 10702 | + Binder.getCallingPid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10703 | } |
| 10704 | } |
| 10705 | } |
| 10706 | |
| 10707 | public void serviceDoneExecutingLocked(ServiceRecord r, boolean inStopping) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10708 | if (DEBUG_SERVICE) Slog.v(TAG, "<<< DONE EXECUTING " + r |
| 10709 | + ": nesting=" + r.executeNesting |
| 10710 | + ", inStopping=" + inStopping + ", app=" + r.app); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10711 | 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] | 10712 | r.executeNesting--; |
| 10713 | if (r.executeNesting <= 0 && r.app != null) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10714 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 10715 | "Nesting at 0 of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10716 | r.app.executingServices.remove(r); |
| 10717 | if (r.app.executingServices.size() == 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10718 | if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG, |
| 10719 | "No more executingServices of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10720 | mHandler.removeMessages(SERVICE_TIMEOUT_MSG, r.app); |
| 10721 | } |
| 10722 | if (inStopping) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10723 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 10724 | "doneExecuting remove stopping " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10725 | mStoppingServices.remove(r); |
Mattias Petersson | 3996b41 | 2010-10-27 09:32:51 +0200 | [diff] [blame] | 10726 | r.bindings.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10727 | } |
| 10728 | updateOomAdjLocked(r.app); |
| 10729 | } |
| 10730 | } |
| 10731 | |
| 10732 | void serviceTimeout(ProcessRecord proc) { |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 10733 | String anrMessage = null; |
| 10734 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10735 | synchronized(this) { |
| 10736 | if (proc.executingServices.size() == 0 || proc.thread == null) { |
| 10737 | return; |
| 10738 | } |
| 10739 | long maxTime = SystemClock.uptimeMillis() - SERVICE_TIMEOUT; |
| 10740 | Iterator<ServiceRecord> it = proc.executingServices.iterator(); |
| 10741 | ServiceRecord timeout = null; |
| 10742 | long nextTime = 0; |
| 10743 | while (it.hasNext()) { |
| 10744 | ServiceRecord sr = it.next(); |
| 10745 | if (sr.executingStart < maxTime) { |
| 10746 | timeout = sr; |
| 10747 | break; |
| 10748 | } |
| 10749 | if (sr.executingStart > nextTime) { |
| 10750 | nextTime = sr.executingStart; |
| 10751 | } |
| 10752 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10753 | if (timeout != null && mLruProcesses.contains(proc)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10754 | Slog.w(TAG, "Timeout executing service: " + timeout); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 10755 | anrMessage = "Executing service " + timeout.shortName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10756 | } else { |
| 10757 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 10758 | msg.obj = proc; |
| 10759 | mHandler.sendMessageAtTime(msg, nextTime+SERVICE_TIMEOUT); |
| 10760 | } |
| 10761 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 10762 | |
| 10763 | if (anrMessage != null) { |
| 10764 | appNotResponding(proc, null, null, anrMessage); |
| 10765 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10766 | } |
| 10767 | |
| 10768 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10769 | // BACKUP AND RESTORE |
| 10770 | // ========================================================= |
| 10771 | |
| 10772 | // Cause the target app to be launched if necessary and its backup agent |
| 10773 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 10774 | // activity manager to announce its creation. |
| 10775 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10776 | if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10777 | enforceCallingPermission("android.permission.BACKUP", "startBackupAgent"); |
| 10778 | |
| 10779 | synchronized(this) { |
| 10780 | // !!! TODO: currently no check here that we're already bound |
| 10781 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 10782 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 10783 | synchronized (stats) { |
| 10784 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 10785 | } |
| 10786 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10787 | // Backup agent is now in use, its package can't be stopped. |
| 10788 | try { |
| 10789 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 10790 | app.packageName, false); |
| 10791 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 10792 | } catch (IllegalArgumentException e) { |
| 10793 | Slog.w(TAG, "Failed trying to unstop package " |
| 10794 | + app.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10795 | } |
| 10796 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10797 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 10798 | ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) |
| 10799 | ? new ComponentName(app.packageName, app.backupAgentName) |
| 10800 | : new ComponentName("android", "FullBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10801 | // startProcessLocked() returns existing proc's record if it's already running |
| 10802 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 10803 | false, 0, "backup", hostingName, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10804 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10805 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10806 | return false; |
| 10807 | } |
| 10808 | |
| 10809 | r.app = proc; |
| 10810 | mBackupTarget = r; |
| 10811 | mBackupAppName = app.packageName; |
| 10812 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 10813 | // Try not to kill the process during backup |
| 10814 | updateOomAdjLocked(proc); |
| 10815 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10816 | // If the process is already attached, schedule the creation of the backup agent now. |
| 10817 | // If it is not yet live, this will be done when it attaches to the framework. |
| 10818 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10819 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10820 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 10821 | proc.thread.scheduleCreateBackupAgent(app, |
| 10822 | compatibilityInfoForPackageLocked(app), backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10823 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 10824 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10825 | } |
| 10826 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10827 | 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] | 10828 | } |
| 10829 | // Invariants: at this point, the target app process exists and the application |
| 10830 | // is either already running or in the process of coming up. mBackupTarget and |
| 10831 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 10832 | // know that it's scheduled for a backup-agent operation. |
| 10833 | } |
| 10834 | |
| 10835 | return true; |
| 10836 | } |
| 10837 | |
| 10838 | // A backup agent has just come up |
| 10839 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10840 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10841 | + " = " + agent); |
| 10842 | |
| 10843 | synchronized(this) { |
| 10844 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10845 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10846 | return; |
| 10847 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 10848 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10849 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 10850 | long oldIdent = Binder.clearCallingIdentity(); |
| 10851 | try { |
| 10852 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10853 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10854 | bm.agentConnected(agentPackageName, agent); |
| 10855 | } catch (RemoteException e) { |
| 10856 | // can't happen; the backup manager service is local |
| 10857 | } catch (Exception e) { |
| 10858 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 10859 | e.printStackTrace(); |
| 10860 | } finally { |
| 10861 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10862 | } |
| 10863 | } |
| 10864 | |
| 10865 | // done with this agent |
| 10866 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10867 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10868 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10869 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10870 | return; |
| 10871 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10872 | |
| 10873 | synchronized(this) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10874 | if (mBackupAppName == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10875 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10876 | return; |
| 10877 | } |
| 10878 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10879 | if (!mBackupAppName.equals(appInfo.packageName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10880 | 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] | 10881 | return; |
| 10882 | } |
| 10883 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 10884 | ProcessRecord proc = mBackupTarget.app; |
| 10885 | mBackupTarget = null; |
| 10886 | mBackupAppName = null; |
| 10887 | |
| 10888 | // Not backing this app up any more; reset its OOM adjustment |
| 10889 | updateOomAdjLocked(proc); |
| 10890 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 10891 | // If the app crashed during backup, 'thread' will be null here |
| 10892 | if (proc.thread != null) { |
| 10893 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 10894 | proc.thread.scheduleDestroyBackupAgent(appInfo, |
| 10895 | compatibilityInfoForPackageLocked(appInfo)); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 10896 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10897 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 10898 | e.printStackTrace(); |
| 10899 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10900 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10901 | } |
| 10902 | } |
| 10903 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10904 | // BROADCASTS |
| 10905 | // ========================================================= |
| 10906 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 10907 | private final List getStickiesLocked(String action, IntentFilter filter, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10908 | List cur) { |
| 10909 | final ContentResolver resolver = mContext.getContentResolver(); |
| 10910 | final ArrayList<Intent> list = mStickyBroadcasts.get(action); |
| 10911 | if (list == null) { |
| 10912 | return cur; |
| 10913 | } |
| 10914 | int N = list.size(); |
| 10915 | for (int i=0; i<N; i++) { |
| 10916 | Intent intent = list.get(i); |
| 10917 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 10918 | if (cur == null) { |
| 10919 | cur = new ArrayList<Intent>(); |
| 10920 | } |
| 10921 | cur.add(intent); |
| 10922 | } |
| 10923 | } |
| 10924 | return cur; |
| 10925 | } |
| 10926 | |
| 10927 | private final void scheduleBroadcastsLocked() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10928 | if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts: current=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10929 | + mBroadcastsScheduled); |
| 10930 | |
| 10931 | if (mBroadcastsScheduled) { |
| 10932 | return; |
| 10933 | } |
| 10934 | mHandler.sendEmptyMessage(BROADCAST_INTENT_MSG); |
| 10935 | mBroadcastsScheduled = true; |
| 10936 | } |
| 10937 | |
| 10938 | public Intent registerReceiver(IApplicationThread caller, |
| 10939 | IIntentReceiver receiver, IntentFilter filter, String permission) { |
| 10940 | synchronized(this) { |
| 10941 | ProcessRecord callerApp = null; |
| 10942 | if (caller != null) { |
| 10943 | callerApp = getRecordForAppLocked(caller); |
| 10944 | if (callerApp == null) { |
| 10945 | throw new SecurityException( |
| 10946 | "Unable to find app for caller " + caller |
| 10947 | + " (pid=" + Binder.getCallingPid() |
| 10948 | + ") when registering receiver " + receiver); |
| 10949 | } |
| 10950 | } |
| 10951 | |
| 10952 | List allSticky = null; |
| 10953 | |
| 10954 | // Look for any matching sticky broadcasts... |
| 10955 | Iterator actions = filter.actionsIterator(); |
| 10956 | if (actions != null) { |
| 10957 | while (actions.hasNext()) { |
| 10958 | String action = (String)actions.next(); |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 10959 | allSticky = getStickiesLocked(action, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10960 | } |
| 10961 | } else { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 10962 | allSticky = getStickiesLocked(null, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10963 | } |
| 10964 | |
| 10965 | // The first sticky in the list is returned directly back to |
| 10966 | // the client. |
| 10967 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 10968 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10969 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10970 | + ": " + sticky); |
| 10971 | |
| 10972 | if (receiver == null) { |
| 10973 | return sticky; |
| 10974 | } |
| 10975 | |
| 10976 | ReceiverList rl |
| 10977 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 10978 | if (rl == null) { |
| 10979 | rl = new ReceiverList(this, callerApp, |
| 10980 | Binder.getCallingPid(), |
| 10981 | Binder.getCallingUid(), receiver); |
| 10982 | if (rl.app != null) { |
| 10983 | rl.app.receivers.add(rl); |
| 10984 | } else { |
| 10985 | try { |
| 10986 | receiver.asBinder().linkToDeath(rl, 0); |
| 10987 | } catch (RemoteException e) { |
| 10988 | return sticky; |
| 10989 | } |
| 10990 | rl.linkedToDeath = true; |
| 10991 | } |
| 10992 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
| 10993 | } |
| 10994 | BroadcastFilter bf = new BroadcastFilter(filter, rl, permission); |
| 10995 | rl.add(bf); |
| 10996 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10997 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10998 | } |
| 10999 | mReceiverResolver.addFilter(bf); |
| 11000 | |
| 11001 | // Enqueue broadcasts for all existing stickies that match |
| 11002 | // this filter. |
| 11003 | if (allSticky != null) { |
| 11004 | ArrayList receivers = new ArrayList(); |
| 11005 | receivers.add(bf); |
| 11006 | |
| 11007 | int N = allSticky.size(); |
| 11008 | for (int i=0; i<N; i++) { |
| 11009 | Intent intent = (Intent)allSticky.get(i); |
| 11010 | BroadcastRecord r = new BroadcastRecord(intent, null, |
| 11011 | null, -1, -1, null, receivers, null, 0, null, null, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11012 | false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11013 | if (mParallelBroadcasts.size() == 0) { |
| 11014 | scheduleBroadcastsLocked(); |
| 11015 | } |
| 11016 | mParallelBroadcasts.add(r); |
| 11017 | } |
| 11018 | } |
| 11019 | |
| 11020 | return sticky; |
| 11021 | } |
| 11022 | } |
| 11023 | |
| 11024 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11025 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11026 | |
| 11027 | boolean doNext = false; |
| 11028 | |
| 11029 | synchronized(this) { |
| 11030 | ReceiverList rl |
| 11031 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 11032 | if (rl != null) { |
| 11033 | if (rl.curBroadcast != null) { |
| 11034 | BroadcastRecord r = rl.curBroadcast; |
| 11035 | doNext = finishReceiverLocked( |
| 11036 | receiver.asBinder(), r.resultCode, r.resultData, |
| 11037 | r.resultExtras, r.resultAbort, true); |
| 11038 | } |
| 11039 | |
| 11040 | if (rl.app != null) { |
| 11041 | rl.app.receivers.remove(rl); |
| 11042 | } |
| 11043 | removeReceiverLocked(rl); |
| 11044 | if (rl.linkedToDeath) { |
| 11045 | rl.linkedToDeath = false; |
| 11046 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 11047 | } |
| 11048 | } |
| 11049 | } |
| 11050 | |
| 11051 | if (!doNext) { |
| 11052 | return; |
| 11053 | } |
| 11054 | |
| 11055 | final long origId = Binder.clearCallingIdentity(); |
| 11056 | processNextBroadcast(false); |
| 11057 | trimApplications(); |
| 11058 | Binder.restoreCallingIdentity(origId); |
| 11059 | } |
| 11060 | |
| 11061 | void removeReceiverLocked(ReceiverList rl) { |
| 11062 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 11063 | int N = rl.size(); |
| 11064 | for (int i=0; i<N; i++) { |
| 11065 | mReceiverResolver.removeFilter(rl.get(i)); |
| 11066 | } |
| 11067 | } |
| 11068 | |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11069 | private final void sendPackageBroadcastLocked(int cmd, String[] packages) { |
| 11070 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 11071 | ProcessRecord r = mLruProcesses.get(i); |
| 11072 | if (r.thread != null) { |
| 11073 | try { |
| 11074 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 11075 | } catch (RemoteException ex) { |
| 11076 | } |
| 11077 | } |
| 11078 | } |
| 11079 | } |
| 11080 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11081 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 11082 | String callerPackage, Intent intent, String resolvedType, |
| 11083 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 11084 | Bundle map, String requiredPermission, |
| 11085 | boolean ordered, boolean sticky, int callingPid, int callingUid) { |
| 11086 | intent = new Intent(intent); |
| 11087 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11088 | // By default broadcasts do not go to stopped apps. |
| 11089 | intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES); |
| 11090 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11091 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11092 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
| 11093 | + " ordered=" + ordered); |
| 11094 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11095 | 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] | 11096 | } |
| 11097 | |
| 11098 | // Handle special intents: if this broadcast is from the package |
| 11099 | // manager about a package being removed, we need to remove all of |
| 11100 | // its activities from the history stack. |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11101 | final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11102 | intent.getAction()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11103 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 11104 | || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11105 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11106 | || uidRemoved) { |
| 11107 | if (checkComponentPermission( |
| 11108 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11109 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11110 | == PackageManager.PERMISSION_GRANTED) { |
| 11111 | if (uidRemoved) { |
| 11112 | final Bundle intentExtras = intent.getExtras(); |
| 11113 | final int uid = intentExtras != null |
| 11114 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 11115 | if (uid >= 0) { |
| 11116 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 11117 | synchronized (bs) { |
| 11118 | bs.removeUidStatsLocked(uid); |
| 11119 | } |
| 11120 | } |
| 11121 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11122 | // If resources are unvailble just force stop all |
| 11123 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11124 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11125 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 11126 | if (list != null && (list.length > 0)) { |
| 11127 | for (String pkg : list) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 11128 | forceStopPackageLocked(pkg, -1, false, true, true); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11129 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11130 | sendPackageBroadcastLocked( |
| 11131 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11132 | } |
| 11133 | } else { |
| 11134 | Uri data = intent.getData(); |
| 11135 | String ssp; |
| 11136 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11137 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 11138 | forceStopPackageLocked(ssp, |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 11139 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 11140 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11141 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11142 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
| 11143 | new String[] {ssp}); |
| 11144 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11145 | } |
| 11146 | } |
| 11147 | } |
| 11148 | } else { |
| 11149 | String msg = "Permission Denial: " + intent.getAction() |
| 11150 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 11151 | + ", uid=" + callingUid + ")" |
| 11152 | + " requires " |
| 11153 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11154 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11155 | throw new SecurityException(msg); |
| 11156 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11157 | |
| 11158 | // Special case for adding a package: by default turn on compatibility |
| 11159 | // mode. |
| 11160 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { |
| 11161 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 11162 | Uri data = intent.getData(); |
| 11163 | String ssp; |
| 11164 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11165 | mCompatModePackages.setPackageScreenCompatModeLocked(ssp, |
| 11166 | ActivityManager.COMPAT_MODE_ENABLED); |
| 11167 | } |
| 11168 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11169 | } |
| 11170 | |
| 11171 | /* |
| 11172 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 11173 | * of all currently running processes. This message will get queued up before the broadcast |
| 11174 | * happens. |
| 11175 | */ |
| 11176 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 11177 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 11178 | } |
| 11179 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 11180 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 11181 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 11182 | } |
| 11183 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 11184 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 11185 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 11186 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 11187 | } |
| 11188 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 11189 | /* |
| 11190 | * Prevent non-system code (defined here to be non-persistent |
| 11191 | * processes) from sending protected broadcasts. |
| 11192 | */ |
| 11193 | if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID |
| 11194 | || callingUid == Process.SHELL_UID || callingUid == 0) { |
| 11195 | // Always okay. |
| 11196 | } else if (callerApp == null || !callerApp.persistent) { |
| 11197 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11198 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 11199 | intent.getAction())) { |
| 11200 | String msg = "Permission Denial: not allowed to send broadcast " |
| 11201 | + intent.getAction() + " from pid=" |
| 11202 | + callingPid + ", uid=" + callingUid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11203 | Slog.w(TAG, msg); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 11204 | throw new SecurityException(msg); |
| 11205 | } |
| 11206 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11207 | Slog.w(TAG, "Remote exception", e); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 11208 | return BROADCAST_SUCCESS; |
| 11209 | } |
| 11210 | } |
| 11211 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11212 | // Add to the sticky list if requested. |
| 11213 | if (sticky) { |
| 11214 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 11215 | callingPid, callingUid) |
| 11216 | != PackageManager.PERMISSION_GRANTED) { |
| 11217 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 11218 | + callingPid + ", uid=" + callingUid |
| 11219 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11220 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11221 | throw new SecurityException(msg); |
| 11222 | } |
| 11223 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11224 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11225 | + " and enforce permission " + requiredPermission); |
| 11226 | return BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
| 11227 | } |
| 11228 | if (intent.getComponent() != null) { |
| 11229 | throw new SecurityException( |
| 11230 | "Sticky broadcasts can't target a specific component"); |
| 11231 | } |
| 11232 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 11233 | if (list == null) { |
| 11234 | list = new ArrayList<Intent>(); |
| 11235 | mStickyBroadcasts.put(intent.getAction(), list); |
| 11236 | } |
| 11237 | int N = list.size(); |
| 11238 | int i; |
| 11239 | for (i=0; i<N; i++) { |
| 11240 | if (intent.filterEquals(list.get(i))) { |
| 11241 | // This sticky already exists, replace it. |
| 11242 | list.set(i, new Intent(intent)); |
| 11243 | break; |
| 11244 | } |
| 11245 | } |
| 11246 | if (i >= N) { |
| 11247 | list.add(new Intent(intent)); |
| 11248 | } |
| 11249 | } |
| 11250 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11251 | // Figure out who all will receive this broadcast. |
| 11252 | List receivers = null; |
| 11253 | List<BroadcastFilter> registeredReceivers = null; |
| 11254 | try { |
| 11255 | if (intent.getComponent() != null) { |
| 11256 | // Broadcast is going to one specific receiver class... |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11257 | ActivityInfo ai = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11258 | getReceiverInfo(intent.getComponent(), STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11259 | if (ai != null) { |
| 11260 | receivers = new ArrayList(); |
| 11261 | ResolveInfo ri = new ResolveInfo(); |
| 11262 | ri.activityInfo = ai; |
| 11263 | receivers.add(ri); |
| 11264 | } |
| 11265 | } else { |
| 11266 | // Need to resolve the intent to interested receivers... |
| 11267 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 11268 | == 0) { |
| 11269 | receivers = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11270 | AppGlobals.getPackageManager().queryIntentReceivers( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11271 | intent, resolvedType, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11272 | } |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 11273 | registeredReceivers = mReceiverResolver.queryIntent(intent, resolvedType, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11274 | } |
| 11275 | } catch (RemoteException ex) { |
| 11276 | // pm is in same process, this will never happen. |
| 11277 | } |
| 11278 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11279 | final boolean replacePending = |
| 11280 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 11281 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11282 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11283 | + " replacePending=" + replacePending); |
| 11284 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11285 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 11286 | if (!ordered && NR > 0) { |
| 11287 | // If we are not serializing this broadcast, then send the |
| 11288 | // registered receivers separately so they don't wait for the |
| 11289 | // components to be launched. |
| 11290 | BroadcastRecord r = new BroadcastRecord(intent, callerApp, |
| 11291 | callerPackage, callingPid, callingUid, requiredPermission, |
| 11292 | registeredReceivers, resultTo, resultCode, resultData, map, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11293 | ordered, sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11294 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11295 | TAG, "Enqueueing parallel broadcast " + r |
| 11296 | + ": prev had " + mParallelBroadcasts.size()); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11297 | boolean replaced = false; |
| 11298 | if (replacePending) { |
| 11299 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 11300 | if (intent.filterEquals(mParallelBroadcasts.get(i).intent)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11301 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11302 | "***** DROPPING PARALLEL: " + intent); |
| 11303 | mParallelBroadcasts.set(i, r); |
| 11304 | replaced = true; |
| 11305 | break; |
| 11306 | } |
| 11307 | } |
| 11308 | } |
| 11309 | if (!replaced) { |
| 11310 | mParallelBroadcasts.add(r); |
| 11311 | scheduleBroadcastsLocked(); |
| 11312 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11313 | registeredReceivers = null; |
| 11314 | NR = 0; |
| 11315 | } |
| 11316 | |
| 11317 | // Merge into one list. |
| 11318 | int ir = 0; |
| 11319 | if (receivers != null) { |
| 11320 | // A special case for PACKAGE_ADDED: do not allow the package |
| 11321 | // being added to see this broadcast. This prevents them from |
| 11322 | // using this as a back door to get run as soon as they are |
| 11323 | // installed. Maybe in the future we want to have a special install |
| 11324 | // broadcast or such for apps, but we'd like to deliberately make |
| 11325 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11326 | String skipPackages[] = null; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11327 | if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 11328 | || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 11329 | || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11330 | Uri data = intent.getData(); |
| 11331 | if (data != null) { |
| 11332 | String pkgName = data.getSchemeSpecificPart(); |
| 11333 | if (pkgName != null) { |
| 11334 | skipPackages = new String[] { pkgName }; |
| 11335 | } |
| 11336 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11337 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11338 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 11339 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11340 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 11341 | for (String skipPackage : skipPackages) { |
| 11342 | if (skipPackage != null) { |
| 11343 | int NT = receivers.size(); |
| 11344 | for (int it=0; it<NT; it++) { |
| 11345 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 11346 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 11347 | receivers.remove(it); |
| 11348 | it--; |
| 11349 | NT--; |
| 11350 | } |
| 11351 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11352 | } |
| 11353 | } |
| 11354 | } |
| 11355 | |
| 11356 | int NT = receivers != null ? receivers.size() : 0; |
| 11357 | int it = 0; |
| 11358 | ResolveInfo curt = null; |
| 11359 | BroadcastFilter curr = null; |
| 11360 | while (it < NT && ir < NR) { |
| 11361 | if (curt == null) { |
| 11362 | curt = (ResolveInfo)receivers.get(it); |
| 11363 | } |
| 11364 | if (curr == null) { |
| 11365 | curr = registeredReceivers.get(ir); |
| 11366 | } |
| 11367 | if (curr.getPriority() >= curt.priority) { |
| 11368 | // Insert this broadcast record into the final list. |
| 11369 | receivers.add(it, curr); |
| 11370 | ir++; |
| 11371 | curr = null; |
| 11372 | it++; |
| 11373 | NT++; |
| 11374 | } else { |
| 11375 | // Skip to the next ResolveInfo in the final list. |
| 11376 | it++; |
| 11377 | curt = null; |
| 11378 | } |
| 11379 | } |
| 11380 | } |
| 11381 | while (ir < NR) { |
| 11382 | if (receivers == null) { |
| 11383 | receivers = new ArrayList(); |
| 11384 | } |
| 11385 | receivers.add(registeredReceivers.get(ir)); |
| 11386 | ir++; |
| 11387 | } |
| 11388 | |
| 11389 | if ((receivers != null && receivers.size() > 0) |
| 11390 | || resultTo != null) { |
| 11391 | BroadcastRecord r = new BroadcastRecord(intent, callerApp, |
| 11392 | callerPackage, callingPid, callingUid, requiredPermission, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11393 | receivers, resultTo, resultCode, resultData, map, ordered, |
| 11394 | sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11395 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11396 | TAG, "Enqueueing ordered broadcast " + r |
| 11397 | + ": prev had " + mOrderedBroadcasts.size()); |
| 11398 | if (DEBUG_BROADCAST) { |
| 11399 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11400 | 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] | 11401 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11402 | boolean replaced = false; |
| 11403 | if (replacePending) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11404 | for (int i=mOrderedBroadcasts.size()-1; i>0; i--) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11405 | if (intent.filterEquals(mOrderedBroadcasts.get(i).intent)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11406 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11407 | "***** DROPPING ORDERED: " + intent); |
| 11408 | mOrderedBroadcasts.set(i, r); |
| 11409 | replaced = true; |
| 11410 | break; |
| 11411 | } |
| 11412 | } |
| 11413 | } |
| 11414 | if (!replaced) { |
| 11415 | mOrderedBroadcasts.add(r); |
| 11416 | scheduleBroadcastsLocked(); |
| 11417 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11418 | } |
| 11419 | |
| 11420 | return BROADCAST_SUCCESS; |
| 11421 | } |
| 11422 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11423 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11424 | // Refuse possible leaked file descriptors |
| 11425 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11426 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11427 | } |
| 11428 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11429 | int flags = intent.getFlags(); |
| 11430 | |
| 11431 | if (!mProcessesReady) { |
| 11432 | // if the caller really truly claims to know what they're doing, go |
| 11433 | // ahead and allow the broadcast without launching any receivers |
| 11434 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 11435 | intent = new Intent(intent); |
| 11436 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 11437 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 11438 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 11439 | + " before boot completion"); |
| 11440 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 11441 | } |
| 11442 | } |
| 11443 | |
| 11444 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 11445 | throw new IllegalArgumentException( |
| 11446 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 11447 | } |
| 11448 | |
| 11449 | return intent; |
| 11450 | } |
| 11451 | |
| 11452 | public final int broadcastIntent(IApplicationThread caller, |
| 11453 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11454 | int resultCode, String resultData, Bundle map, |
| 11455 | String requiredPermission, boolean serialized, boolean sticky) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11456 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11457 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 11458 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11459 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11460 | final int callingPid = Binder.getCallingPid(); |
| 11461 | final int callingUid = Binder.getCallingUid(); |
| 11462 | final long origId = Binder.clearCallingIdentity(); |
| 11463 | int res = broadcastIntentLocked(callerApp, |
| 11464 | callerApp != null ? callerApp.info.packageName : null, |
| 11465 | intent, resolvedType, resultTo, |
| 11466 | resultCode, resultData, map, requiredPermission, serialized, |
| 11467 | sticky, callingPid, callingUid); |
| 11468 | Binder.restoreCallingIdentity(origId); |
| 11469 | return res; |
| 11470 | } |
| 11471 | } |
| 11472 | |
| 11473 | int broadcastIntentInPackage(String packageName, int uid, |
| 11474 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11475 | int resultCode, String resultData, Bundle map, |
| 11476 | String requiredPermission, boolean serialized, boolean sticky) { |
| 11477 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11478 | intent = verifyBroadcastLocked(intent); |
| 11479 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11480 | final long origId = Binder.clearCallingIdentity(); |
| 11481 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 11482 | resultTo, resultCode, resultData, map, requiredPermission, |
| 11483 | serialized, sticky, -1, uid); |
| 11484 | Binder.restoreCallingIdentity(origId); |
| 11485 | return res; |
| 11486 | } |
| 11487 | } |
| 11488 | |
| 11489 | public final void unbroadcastIntent(IApplicationThread caller, |
| 11490 | Intent intent) { |
| 11491 | // Refuse possible leaked file descriptors |
| 11492 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11493 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11494 | } |
| 11495 | |
| 11496 | synchronized(this) { |
| 11497 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 11498 | != PackageManager.PERMISSION_GRANTED) { |
| 11499 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 11500 | + Binder.getCallingPid() |
| 11501 | + ", uid=" + Binder.getCallingUid() |
| 11502 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11503 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11504 | throw new SecurityException(msg); |
| 11505 | } |
| 11506 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 11507 | if (list != null) { |
| 11508 | int N = list.size(); |
| 11509 | int i; |
| 11510 | for (i=0; i<N; i++) { |
| 11511 | if (intent.filterEquals(list.get(i))) { |
| 11512 | list.remove(i); |
| 11513 | break; |
| 11514 | } |
| 11515 | } |
| 11516 | } |
| 11517 | } |
| 11518 | } |
| 11519 | |
| 11520 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 11521 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 11522 | boolean explicit) { |
| 11523 | if (mOrderedBroadcasts.size() == 0) { |
| 11524 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11525 | Slog.w(TAG, "finishReceiver called but no pending broadcasts"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11526 | } |
| 11527 | return false; |
| 11528 | } |
| 11529 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 11530 | if (r.receiver == null) { |
| 11531 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11532 | Slog.w(TAG, "finishReceiver called but none active"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11533 | } |
| 11534 | return false; |
| 11535 | } |
| 11536 | if (r.receiver != receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11537 | 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] | 11538 | return false; |
| 11539 | } |
| 11540 | int state = r.state; |
| 11541 | r.state = r.IDLE; |
| 11542 | if (state == r.IDLE) { |
| 11543 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11544 | Slog.w(TAG, "finishReceiver called but state is IDLE"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11545 | } |
| 11546 | } |
| 11547 | r.receiver = null; |
| 11548 | r.intent.setComponent(null); |
| 11549 | if (r.curApp != null) { |
| 11550 | r.curApp.curReceiver = null; |
| 11551 | } |
| 11552 | if (r.curFilter != null) { |
| 11553 | r.curFilter.receiverList.curBroadcast = null; |
| 11554 | } |
| 11555 | r.curFilter = null; |
| 11556 | r.curApp = null; |
| 11557 | r.curComponent = null; |
| 11558 | r.curReceiver = null; |
| 11559 | mPendingBroadcast = null; |
| 11560 | |
| 11561 | r.resultCode = resultCode; |
| 11562 | r.resultData = resultData; |
| 11563 | r.resultExtras = resultExtras; |
| 11564 | r.resultAbort = resultAbort; |
| 11565 | |
| 11566 | // We will process the next receiver right now if this is finishing |
| 11567 | // an app receiver (which is always asynchronous) or after we have |
| 11568 | // come back from calling a receiver. |
| 11569 | return state == BroadcastRecord.APP_RECEIVE |
| 11570 | || state == BroadcastRecord.CALL_DONE_RECEIVE; |
| 11571 | } |
| 11572 | |
| 11573 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 11574 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11575 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11576 | |
| 11577 | // Refuse possible leaked file descriptors |
| 11578 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 11579 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 11580 | } |
| 11581 | |
| 11582 | boolean doNext; |
| 11583 | |
| 11584 | final long origId = Binder.clearCallingIdentity(); |
| 11585 | |
| 11586 | synchronized(this) { |
| 11587 | doNext = finishReceiverLocked( |
| 11588 | who, resultCode, resultData, resultExtras, resultAbort, true); |
| 11589 | } |
| 11590 | |
| 11591 | if (doNext) { |
| 11592 | processNextBroadcast(false); |
| 11593 | } |
| 11594 | trimApplications(); |
| 11595 | |
| 11596 | Binder.restoreCallingIdentity(origId); |
| 11597 | } |
| 11598 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11599 | private final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11600 | if (r.nextReceiver > 0) { |
| 11601 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 11602 | if (curReceiver instanceof BroadcastFilter) { |
| 11603 | BroadcastFilter bf = (BroadcastFilter) curReceiver; |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 11604 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11605 | System.identityHashCode(r), |
| 11606 | r.intent.getAction(), |
| 11607 | r.nextReceiver - 1, |
| 11608 | System.identityHashCode(bf)); |
| 11609 | } else { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 11610 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11611 | System.identityHashCode(r), |
| 11612 | r.intent.getAction(), |
| 11613 | r.nextReceiver - 1, |
| 11614 | ((ResolveInfo)curReceiver).toString()); |
| 11615 | } |
| 11616 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11617 | 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] | 11618 | + r); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 11619 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11620 | System.identityHashCode(r), |
| 11621 | r.intent.getAction(), |
| 11622 | r.nextReceiver, |
| 11623 | "NONE"); |
| 11624 | } |
| 11625 | } |
| 11626 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11627 | private final void setBroadcastTimeoutLocked(long timeoutTime) { |
| 11628 | if (! mPendingBroadcastTimeoutMessage) { |
| 11629 | Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG); |
| 11630 | mHandler.sendMessageAtTime(msg, timeoutTime); |
| 11631 | mPendingBroadcastTimeoutMessage = true; |
| 11632 | } |
| 11633 | } |
| 11634 | |
| 11635 | private final void cancelBroadcastTimeoutLocked() { |
| 11636 | if (mPendingBroadcastTimeoutMessage) { |
| 11637 | mHandler.removeMessages(BROADCAST_TIMEOUT_MSG); |
| 11638 | mPendingBroadcastTimeoutMessage = false; |
| 11639 | } |
| 11640 | } |
| 11641 | |
| 11642 | private final void broadcastTimeoutLocked(boolean fromMsg) { |
| 11643 | if (fromMsg) { |
| 11644 | mPendingBroadcastTimeoutMessage = false; |
| 11645 | } |
| 11646 | |
| 11647 | if (mOrderedBroadcasts.size() == 0) { |
| 11648 | return; |
| 11649 | } |
| 11650 | |
| 11651 | long now = SystemClock.uptimeMillis(); |
| 11652 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 11653 | if (fromMsg) { |
| 11654 | if (mDidDexOpt) { |
| 11655 | // Delay timeouts until dexopt finishes. |
| 11656 | mDidDexOpt = false; |
| 11657 | long timeoutTime = SystemClock.uptimeMillis() + BROADCAST_TIMEOUT; |
| 11658 | setBroadcastTimeoutLocked(timeoutTime); |
| 11659 | return; |
| 11660 | } |
| 11661 | if (! mProcessesReady) { |
| 11662 | // Only process broadcast timeouts if the system is ready. That way |
| 11663 | // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended |
| 11664 | // to do heavy lifting for system up. |
| 11665 | return; |
| 11666 | } |
| 11667 | |
| 11668 | long timeoutTime = r.receiverTime + BROADCAST_TIMEOUT; |
| 11669 | if (timeoutTime > now) { |
| 11670 | // We can observe premature timeouts because we do not cancel and reset the |
| 11671 | // broadcast timeout message after each receiver finishes. Instead, we set up |
| 11672 | // an initial timeout then kick it down the road a little further as needed |
| 11673 | // when it expires. |
| 11674 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 11675 | "Premature timeout @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for " |
| 11676 | + timeoutTime); |
| 11677 | setBroadcastTimeoutLocked(timeoutTime); |
| 11678 | return; |
| 11679 | } |
| 11680 | } |
| 11681 | |
| 11682 | Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r.receiver |
| 11683 | + ", started " + (now - r.receiverTime) + "ms ago"); |
| 11684 | r.receiverTime = now; |
| 11685 | r.anrCount++; |
| 11686 | |
| 11687 | // Current receiver has passed its expiration date. |
| 11688 | if (r.nextReceiver <= 0) { |
| 11689 | Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0"); |
| 11690 | return; |
| 11691 | } |
| 11692 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11693 | ProcessRecord app = null; |
| 11694 | String anrMessage = null; |
| 11695 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11696 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 11697 | Slog.w(TAG, "Receiver during timeout: " + curReceiver); |
| 11698 | logBroadcastReceiverDiscardLocked(r); |
| 11699 | if (curReceiver instanceof BroadcastFilter) { |
| 11700 | BroadcastFilter bf = (BroadcastFilter)curReceiver; |
| 11701 | if (bf.receiverList.pid != 0 |
| 11702 | && bf.receiverList.pid != MY_PID) { |
| 11703 | synchronized (this.mPidsSelfLocked) { |
| 11704 | app = this.mPidsSelfLocked.get( |
| 11705 | bf.receiverList.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11706 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11707 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11708 | } else { |
| 11709 | app = r.curApp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11710 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11711 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11712 | if (app != null) { |
| 11713 | anrMessage = "Broadcast of " + r.intent.toString(); |
| 11714 | } |
| 11715 | |
| 11716 | if (mPendingBroadcast == r) { |
| 11717 | mPendingBroadcast = null; |
| 11718 | } |
| 11719 | |
| 11720 | // Move on to the next receiver. |
| 11721 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 11722 | r.resultExtras, r.resultAbort, true); |
| 11723 | scheduleBroadcastsLocked(); |
| 11724 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11725 | if (anrMessage != null) { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11726 | // Post the ANR to the handler since we do not want to process ANRs while |
| 11727 | // potentially holding our lock. |
| 11728 | mHandler.post(new AppNotResponding(app, anrMessage)); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11729 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11730 | } |
| 11731 | |
| 11732 | private final void processCurBroadcastLocked(BroadcastRecord r, |
| 11733 | ProcessRecord app) throws RemoteException { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11734 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 11735 | "Process cur broadcast " + r + " for app " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11736 | if (app.thread == null) { |
| 11737 | throw new RemoteException(); |
| 11738 | } |
| 11739 | r.receiver = app.thread.asBinder(); |
| 11740 | r.curApp = app; |
| 11741 | app.curReceiver = r; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11742 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11743 | |
| 11744 | // Tell the application to launch this receiver. |
| 11745 | r.intent.setComponent(r.curComponent); |
| 11746 | |
| 11747 | boolean started = false; |
| 11748 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11749 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11750 | "Delivering to component " + r.curComponent |
| 11751 | + ": " + r); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 11752 | ensurePackageDexOpt(r.intent.getComponent().getPackageName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11753 | app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver, |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11754 | compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11755 | r.resultCode, r.resultData, r.resultExtras, r.ordered); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11756 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 11757 | "Process cur broadcast " + r + " DELIVERED for app " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11758 | started = true; |
| 11759 | } finally { |
| 11760 | if (!started) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11761 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 11762 | "Process cur broadcast " + r + ": NOT STARTED!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11763 | r.receiver = null; |
| 11764 | r.curApp = null; |
| 11765 | app.curReceiver = null; |
| 11766 | } |
| 11767 | } |
| 11768 | |
| 11769 | } |
| 11770 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11771 | static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 11772 | Intent intent, int resultCode, String data, Bundle extras, |
| 11773 | boolean ordered, boolean sticky) throws RemoteException { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11774 | // 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] | 11775 | if (app != null && app.thread != null) { |
| 11776 | // If we have an app thread, do the call through that so it is |
| 11777 | // correctly ordered with other one-way calls. |
| 11778 | app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 11779 | data, extras, ordered, sticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11780 | } else { |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 11781 | receiver.performReceive(intent, resultCode, data, extras, ordered, sticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11782 | } |
| 11783 | } |
| 11784 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11785 | private final void deliverToRegisteredReceiverLocked(BroadcastRecord r, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11786 | BroadcastFilter filter, boolean ordered) { |
| 11787 | boolean skip = false; |
| 11788 | if (filter.requiredPermission != null) { |
| 11789 | int perm = checkComponentPermission(filter.requiredPermission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11790 | r.callingPid, r.callingUid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11791 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11792 | Slog.w(TAG, "Permission Denial: broadcasting " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11793 | + r.intent.toString() |
| 11794 | + " from " + r.callerPackage + " (pid=" |
| 11795 | + r.callingPid + ", uid=" + r.callingUid + ")" |
| 11796 | + " requires " + filter.requiredPermission |
| 11797 | + " due to registered receiver " + filter); |
| 11798 | skip = true; |
| 11799 | } |
| 11800 | } |
| 11801 | if (r.requiredPermission != null) { |
| 11802 | int perm = checkComponentPermission(r.requiredPermission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11803 | filter.receiverList.pid, filter.receiverList.uid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11804 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11805 | Slog.w(TAG, "Permission Denial: receiving " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11806 | + r.intent.toString() |
| 11807 | + " to " + filter.receiverList.app |
| 11808 | + " (pid=" + filter.receiverList.pid |
| 11809 | + ", uid=" + filter.receiverList.uid + ")" |
| 11810 | + " requires " + r.requiredPermission |
| 11811 | + " due to sender " + r.callerPackage |
| 11812 | + " (uid " + r.callingUid + ")"); |
| 11813 | skip = true; |
| 11814 | } |
| 11815 | } |
| 11816 | |
| 11817 | if (!skip) { |
| 11818 | // If this is not being sent as an ordered broadcast, then we |
| 11819 | // don't want to touch the fields that keep track of the current |
| 11820 | // state of ordered broadcasts. |
| 11821 | if (ordered) { |
| 11822 | r.receiver = filter.receiverList.receiver.asBinder(); |
| 11823 | r.curFilter = filter; |
| 11824 | filter.receiverList.curBroadcast = r; |
| 11825 | r.state = BroadcastRecord.CALL_IN_RECEIVE; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11826 | if (filter.receiverList.app != null) { |
| 11827 | // Bump hosting application to no longer be in background |
| 11828 | // scheduling class. Note that we can't do that if there |
| 11829 | // isn't an app... but we can only be in that case for |
| 11830 | // things that directly call the IActivityManager API, which |
| 11831 | // are already core system stuff so don't matter for this. |
| 11832 | r.curApp = filter.receiverList.app; |
| 11833 | filter.receiverList.app.curReceiver = r; |
| 11834 | updateOomAdjLocked(); |
| 11835 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11836 | } |
| 11837 | try { |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11838 | if (DEBUG_BROADCAST_LIGHT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11839 | int seq = r.intent.getIntExtra("seq", -1); |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 11840 | Slog.i(TAG, "Delivering to " + filter |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11841 | + " (seq=" + seq + "): " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11842 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11843 | performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11844 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11845 | r.resultData, r.resultExtras, r.ordered, r.initialSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11846 | if (ordered) { |
| 11847 | r.state = BroadcastRecord.CALL_DONE_RECEIVE; |
| 11848 | } |
| 11849 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11850 | Slog.w(TAG, "Failure sending broadcast " + r.intent, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11851 | if (ordered) { |
| 11852 | r.receiver = null; |
| 11853 | r.curFilter = null; |
| 11854 | filter.receiverList.curBroadcast = null; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11855 | if (filter.receiverList.app != null) { |
| 11856 | filter.receiverList.app.curReceiver = null; |
| 11857 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11858 | } |
| 11859 | } |
| 11860 | } |
| 11861 | } |
| 11862 | |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11863 | private final void addBroadcastToHistoryLocked(BroadcastRecord r) { |
| 11864 | if (r.callingUid < 0) { |
| 11865 | // This was from a registerReceiver() call; ignore it. |
| 11866 | return; |
| 11867 | } |
| 11868 | System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1, |
| 11869 | MAX_BROADCAST_HISTORY-1); |
| 11870 | r.finishTime = SystemClock.uptimeMillis(); |
| 11871 | mBroadcastHistory[0] = r; |
| 11872 | } |
| 11873 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11874 | private final void processNextBroadcast(boolean fromMsg) { |
| 11875 | synchronized(this) { |
| 11876 | BroadcastRecord r; |
| 11877 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11878 | if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11879 | + mParallelBroadcasts.size() + " broadcasts, " |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 11880 | + mOrderedBroadcasts.size() + " ordered broadcasts"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11881 | |
| 11882 | updateCpuStats(); |
| 11883 | |
| 11884 | if (fromMsg) { |
| 11885 | mBroadcastsScheduled = false; |
| 11886 | } |
| 11887 | |
| 11888 | // First, deliver any non-serialized broadcasts right away. |
| 11889 | while (mParallelBroadcasts.size() > 0) { |
| 11890 | r = mParallelBroadcasts.remove(0); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11891 | r.dispatchTime = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11892 | final int N = r.receivers.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11893 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11894 | + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11895 | for (int i=0; i<N; i++) { |
| 11896 | Object target = r.receivers.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11897 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 11898 | "Delivering non-ordered to registered " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11899 | + target + ": " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11900 | deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11901 | } |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11902 | addBroadcastToHistoryLocked(r); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11903 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11904 | + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11905 | } |
| 11906 | |
| 11907 | // Now take care of the next serialized one... |
| 11908 | |
| 11909 | // If we are waiting for a process to come up to handle the next |
| 11910 | // broadcast, then do nothing at this point. Just in case, we |
| 11911 | // check that the process we're waiting for still exists. |
| 11912 | if (mPendingBroadcast != null) { |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 11913 | if (DEBUG_BROADCAST_LIGHT) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11914 | Slog.v(TAG, "processNextBroadcast: waiting for " |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 11915 | + mPendingBroadcast.curApp); |
| 11916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11917 | |
| 11918 | boolean isDead; |
| 11919 | synchronized (mPidsSelfLocked) { |
| 11920 | isDead = (mPidsSelfLocked.get(mPendingBroadcast.curApp.pid) == null); |
| 11921 | } |
| 11922 | if (!isDead) { |
| 11923 | // It's still alive, so keep waiting |
| 11924 | return; |
| 11925 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11926 | Slog.w(TAG, "pending app " + mPendingBroadcast.curApp |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11927 | + " died before responding to broadcast"); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11928 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 11929 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11930 | mPendingBroadcast = null; |
| 11931 | } |
| 11932 | } |
| 11933 | |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11934 | boolean looped = false; |
| 11935 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11936 | do { |
| 11937 | if (mOrderedBroadcasts.size() == 0) { |
| 11938 | // No more broadcasts pending, so all done! |
| 11939 | scheduleAppGcsLocked(); |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11940 | if (looped) { |
| 11941 | // If we had finished the last ordered broadcast, then |
| 11942 | // make sure all processes have correct oom and sched |
| 11943 | // adjustments. |
| 11944 | updateOomAdjLocked(); |
| 11945 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11946 | return; |
| 11947 | } |
| 11948 | r = mOrderedBroadcasts.get(0); |
| 11949 | boolean forceReceive = false; |
| 11950 | |
| 11951 | // Ensure that even if something goes awry with the timeout |
| 11952 | // detection, we catch "hung" broadcasts here, discard them, |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 11953 | // and continue to make progress. |
| 11954 | // |
| 11955 | // 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] | 11956 | // receivers don't get executed with timeouts. They're intended for |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 11957 | // one time heavy lifting after system upgrades and can take |
| 11958 | // significant amounts of time. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11959 | int numReceivers = (r.receivers != null) ? r.receivers.size() : 0; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11960 | if (mProcessesReady && r.dispatchTime > 0) { |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 11961 | long now = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11962 | if ((numReceivers > 0) && |
| 11963 | (now > r.dispatchTime + (2*BROADCAST_TIMEOUT*numReceivers))) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11964 | Slog.w(TAG, "Hung broadcast discarded after timeout failure:" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11965 | + " now=" + now |
| 11966 | + " dispatchTime=" + r.dispatchTime |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11967 | + " startTime=" + r.receiverTime |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11968 | + " intent=" + r.intent |
| 11969 | + " numReceivers=" + numReceivers |
| 11970 | + " nextReceiver=" + r.nextReceiver |
| 11971 | + " state=" + r.state); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11972 | broadcastTimeoutLocked(false); // forcibly finish this broadcast |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11973 | forceReceive = true; |
| 11974 | r.state = BroadcastRecord.IDLE; |
| 11975 | } |
| 11976 | } |
| 11977 | |
| 11978 | if (r.state != BroadcastRecord.IDLE) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11979 | if (DEBUG_BROADCAST) Slog.d(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11980 | "processNextBroadcast() called when not idle (state=" |
| 11981 | + r.state + ")"); |
| 11982 | return; |
| 11983 | } |
| 11984 | |
| 11985 | if (r.receivers == null || r.nextReceiver >= numReceivers |
| 11986 | || r.resultAbort || forceReceive) { |
| 11987 | // No more receivers for this broadcast! Send the final |
| 11988 | // result if requested... |
| 11989 | if (r.resultTo != null) { |
| 11990 | try { |
| 11991 | if (DEBUG_BROADCAST) { |
| 11992 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11993 | Slog.i(TAG, "Finishing broadcast " + r.intent.getAction() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11994 | + " seq=" + seq + " app=" + r.callerApp); |
| 11995 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11996 | performReceiveLocked(r.callerApp, r.resultTo, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11997 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 11998 | r.resultData, r.resultExtras, false, false); |
Johannes Carlsson | b5a8654 | 2010-10-27 10:08:10 +0200 | [diff] [blame] | 11999 | // Set this to null so that the reference |
| 12000 | // (local and remote) isnt kept in the mBroadcastHistory. |
| 12001 | r.resultTo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12002 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12003 | 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] | 12004 | } |
| 12005 | } |
| 12006 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12007 | if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG"); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12008 | cancelBroadcastTimeoutLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12009 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12010 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12011 | + r); |
| 12012 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12013 | // ... and on to the next... |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12014 | addBroadcastToHistoryLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12015 | mOrderedBroadcasts.remove(0); |
| 12016 | r = null; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12017 | looped = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12018 | continue; |
| 12019 | } |
| 12020 | } while (r == null); |
| 12021 | |
| 12022 | // Get the next receiver... |
| 12023 | int recIdx = r.nextReceiver++; |
| 12024 | |
| 12025 | // Keep track of when this receiver started, and make sure there |
| 12026 | // is a timeout message pending to kill it if need be. |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12027 | r.receiverTime = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12028 | if (recIdx == 0) { |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12029 | r.dispatchTime = r.receiverTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12030 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12031 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12032 | + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12033 | } |
| 12034 | if (! mPendingBroadcastTimeoutMessage) { |
| 12035 | long timeoutTime = r.receiverTime + BROADCAST_TIMEOUT; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12036 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12037 | "Submitting BROADCAST_TIMEOUT_MSG for " + r + " at " + timeoutTime); |
| 12038 | setBroadcastTimeoutLocked(timeoutTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12039 | } |
| 12040 | |
| 12041 | Object nextReceiver = r.receivers.get(recIdx); |
| 12042 | if (nextReceiver instanceof BroadcastFilter) { |
| 12043 | // Simple case: this is a registered receiver who gets |
| 12044 | // a direct call. |
| 12045 | BroadcastFilter filter = (BroadcastFilter)nextReceiver; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12046 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 12047 | "Delivering ordered to registered " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12048 | + filter + ": " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12049 | deliverToRegisteredReceiverLocked(r, filter, r.ordered); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12050 | if (r.receiver == null || !r.ordered) { |
| 12051 | // The receiver has already finished, so schedule to |
| 12052 | // process the next one. |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 12053 | if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing: ordered=" |
| 12054 | + r.ordered + " receiver=" + r.receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12055 | r.state = BroadcastRecord.IDLE; |
| 12056 | scheduleBroadcastsLocked(); |
| 12057 | } |
| 12058 | return; |
| 12059 | } |
| 12060 | |
| 12061 | // Hard case: need to instantiate the receiver, possibly |
| 12062 | // starting its application process to host it. |
| 12063 | |
| 12064 | ResolveInfo info = |
| 12065 | (ResolveInfo)nextReceiver; |
| 12066 | |
| 12067 | boolean skip = false; |
| 12068 | int perm = checkComponentPermission(info.activityInfo.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12069 | r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid, |
| 12070 | info.activityInfo.exported); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12071 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12072 | if (!info.activityInfo.exported) { |
| 12073 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 12074 | + r.intent.toString() |
| 12075 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 12076 | + ", uid=" + r.callingUid + ")" |
| 12077 | + " is not exported from uid " + info.activityInfo.applicationInfo.uid |
| 12078 | + " due to receiver " + info.activityInfo.packageName |
| 12079 | + "/" + info.activityInfo.name); |
| 12080 | } else { |
| 12081 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 12082 | + r.intent.toString() |
| 12083 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 12084 | + ", uid=" + r.callingUid + ")" |
| 12085 | + " requires " + info.activityInfo.permission |
| 12086 | + " due to receiver " + info.activityInfo.packageName |
| 12087 | + "/" + info.activityInfo.name); |
| 12088 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12089 | skip = true; |
| 12090 | } |
| 12091 | if (r.callingUid != Process.SYSTEM_UID && |
| 12092 | r.requiredPermission != null) { |
| 12093 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12094 | perm = AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12095 | checkPermission(r.requiredPermission, |
| 12096 | info.activityInfo.applicationInfo.packageName); |
| 12097 | } catch (RemoteException e) { |
| 12098 | perm = PackageManager.PERMISSION_DENIED; |
| 12099 | } |
| 12100 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12101 | Slog.w(TAG, "Permission Denial: receiving " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12102 | + r.intent + " to " |
| 12103 | + info.activityInfo.applicationInfo.packageName |
| 12104 | + " requires " + r.requiredPermission |
| 12105 | + " due to sender " + r.callerPackage |
| 12106 | + " (uid " + r.callingUid + ")"); |
| 12107 | skip = true; |
| 12108 | } |
| 12109 | } |
| 12110 | if (r.curApp != null && r.curApp.crashing) { |
| 12111 | // If the target process is crashing, just skip it. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12112 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12113 | "Skipping deliver ordered " + r + " to " + r.curApp |
| 12114 | + ": process crashing"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12115 | skip = true; |
| 12116 | } |
| 12117 | |
| 12118 | if (skip) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12119 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12120 | "Skipping delivery of ordered " + r + " for whatever reason"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12121 | r.receiver = null; |
| 12122 | r.curFilter = null; |
| 12123 | r.state = BroadcastRecord.IDLE; |
| 12124 | scheduleBroadcastsLocked(); |
| 12125 | return; |
| 12126 | } |
| 12127 | |
| 12128 | r.state = BroadcastRecord.APP_RECEIVE; |
| 12129 | String targetProcess = info.activityInfo.processName; |
| 12130 | r.curComponent = new ComponentName( |
| 12131 | info.activityInfo.applicationInfo.packageName, |
| 12132 | info.activityInfo.name); |
| 12133 | r.curReceiver = info.activityInfo; |
| 12134 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 12135 | // Broadcast is being executed, its package can't be stopped. |
| 12136 | try { |
| 12137 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 12138 | r.curComponent.getPackageName(), false); |
| 12139 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 12140 | } catch (IllegalArgumentException e) { |
| 12141 | Slog.w(TAG, "Failed trying to unstop package " |
| 12142 | + r.curComponent.getPackageName() + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 12143 | } |
| 12144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12145 | // Is this receiver's application already running? |
| 12146 | ProcessRecord app = getProcessRecordLocked(targetProcess, |
| 12147 | info.activityInfo.applicationInfo.uid); |
| 12148 | if (app != null && app.thread != null) { |
| 12149 | try { |
| 12150 | processCurBroadcastLocked(r, app); |
| 12151 | return; |
| 12152 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12153 | Slog.w(TAG, "Exception when sending broadcast to " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12154 | + r.curComponent, e); |
| 12155 | } |
| 12156 | |
| 12157 | // If a dead object exception was thrown -- fall through to |
| 12158 | // restart the application. |
| 12159 | } |
| 12160 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 12161 | // 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] | 12162 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12163 | "Need to start app " + targetProcess + " for broadcast " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12164 | if ((r.curApp=startProcessLocked(targetProcess, |
| 12165 | info.activityInfo.applicationInfo, true, |
| 12166 | r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 12167 | "broadcast", r.curComponent, |
| 12168 | (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0)) |
| 12169 | == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12170 | // Ah, this recipient is unavailable. Finish it if necessary, |
| 12171 | // and mark the broadcast record as ready for the next. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12172 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12173 | + info.activityInfo.applicationInfo.packageName + "/" |
| 12174 | + info.activityInfo.applicationInfo.uid + " for broadcast " |
| 12175 | + r.intent + ": process is bad"); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12176 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12177 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 12178 | r.resultExtras, r.resultAbort, true); |
| 12179 | scheduleBroadcastsLocked(); |
| 12180 | r.state = BroadcastRecord.IDLE; |
| 12181 | return; |
| 12182 | } |
| 12183 | |
| 12184 | mPendingBroadcast = r; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12185 | mPendingBroadcastRecvIndex = recIdx; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12186 | } |
| 12187 | } |
| 12188 | |
| 12189 | // ========================================================= |
| 12190 | // INSTRUMENTATION |
| 12191 | // ========================================================= |
| 12192 | |
| 12193 | public boolean startInstrumentation(ComponentName className, |
| 12194 | String profileFile, int flags, Bundle arguments, |
| 12195 | IInstrumentationWatcher watcher) { |
| 12196 | // Refuse possible leaked file descriptors |
| 12197 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 12198 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12199 | } |
| 12200 | |
| 12201 | synchronized(this) { |
| 12202 | InstrumentationInfo ii = null; |
| 12203 | ApplicationInfo ai = null; |
| 12204 | try { |
| 12205 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12206 | className, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12207 | ai = mContext.getPackageManager().getApplicationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12208 | ii.targetPackage, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12209 | } catch (PackageManager.NameNotFoundException e) { |
| 12210 | } |
| 12211 | if (ii == null) { |
| 12212 | reportStartInstrumentationFailure(watcher, className, |
| 12213 | "Unable to find instrumentation info for: " + className); |
| 12214 | return false; |
| 12215 | } |
| 12216 | if (ai == null) { |
| 12217 | reportStartInstrumentationFailure(watcher, className, |
| 12218 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 12219 | return false; |
| 12220 | } |
| 12221 | |
| 12222 | int match = mContext.getPackageManager().checkSignatures( |
| 12223 | ii.targetPackage, ii.packageName); |
| 12224 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 12225 | String msg = "Permission Denial: starting instrumentation " |
| 12226 | + className + " from pid=" |
| 12227 | + Binder.getCallingPid() |
| 12228 | + ", uid=" + Binder.getCallingPid() |
| 12229 | + " not allowed because package " + ii.packageName |
| 12230 | + " does not have a signature matching the target " |
| 12231 | + ii.targetPackage; |
| 12232 | reportStartInstrumentationFailure(watcher, className, msg); |
| 12233 | throw new SecurityException(msg); |
| 12234 | } |
| 12235 | |
| 12236 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 12237 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12238 | ProcessRecord app = addAppLocked(ai); |
| 12239 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12240 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12241 | app.instrumentationProfileFile = profileFile; |
| 12242 | app.instrumentationArguments = arguments; |
| 12243 | app.instrumentationWatcher = watcher; |
| 12244 | app.instrumentationResultClass = className; |
| 12245 | Binder.restoreCallingIdentity(origId); |
| 12246 | } |
| 12247 | |
| 12248 | return true; |
| 12249 | } |
| 12250 | |
| 12251 | /** |
| 12252 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 12253 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 12254 | * the "am" command to report errors with more information. |
| 12255 | * |
| 12256 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 12257 | * @param cn The component name of the instrumentation. |
| 12258 | * @param report The error report. |
| 12259 | */ |
| 12260 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 12261 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12262 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12263 | try { |
| 12264 | if (watcher != null) { |
| 12265 | Bundle results = new Bundle(); |
| 12266 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 12267 | results.putString("Error", report); |
| 12268 | watcher.instrumentationStatus(cn, -1, results); |
| 12269 | } |
| 12270 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12271 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12272 | } |
| 12273 | } |
| 12274 | |
| 12275 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 12276 | if (app.instrumentationWatcher != null) { |
| 12277 | try { |
| 12278 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 12279 | app.instrumentationWatcher.instrumentationFinished( |
| 12280 | app.instrumentationClass, |
| 12281 | resultCode, |
| 12282 | results); |
| 12283 | } catch (RemoteException e) { |
| 12284 | } |
| 12285 | } |
| 12286 | app.instrumentationWatcher = null; |
| 12287 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12288 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12289 | app.instrumentationProfileFile = null; |
| 12290 | app.instrumentationArguments = null; |
| 12291 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 12292 | forceStopPackageLocked(app.processName, -1, false, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12293 | } |
| 12294 | |
| 12295 | public void finishInstrumentation(IApplicationThread target, |
| 12296 | int resultCode, Bundle results) { |
| 12297 | // Refuse possible leaked file descriptors |
| 12298 | if (results != null && results.hasFileDescriptors()) { |
| 12299 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12300 | } |
| 12301 | |
| 12302 | synchronized(this) { |
| 12303 | ProcessRecord app = getRecordForAppLocked(target); |
| 12304 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12305 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12306 | return; |
| 12307 | } |
| 12308 | final long origId = Binder.clearCallingIdentity(); |
| 12309 | finishInstrumentationLocked(app, resultCode, results); |
| 12310 | Binder.restoreCallingIdentity(origId); |
| 12311 | } |
| 12312 | } |
| 12313 | |
| 12314 | // ========================================================= |
| 12315 | // CONFIGURATION |
| 12316 | // ========================================================= |
| 12317 | |
| 12318 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 12319 | ConfigurationInfo config = new ConfigurationInfo(); |
| 12320 | synchronized (this) { |
| 12321 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 12322 | config.reqKeyboardType = mConfiguration.keyboard; |
| 12323 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12324 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 12325 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12326 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 12327 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12328 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 12329 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12330 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 12331 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 12332 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12333 | } |
| 12334 | return config; |
| 12335 | } |
| 12336 | |
| 12337 | public Configuration getConfiguration() { |
| 12338 | Configuration ci; |
| 12339 | synchronized(this) { |
| 12340 | ci = new Configuration(mConfiguration); |
| 12341 | } |
| 12342 | return ci; |
| 12343 | } |
| 12344 | |
| 12345 | public void updateConfiguration(Configuration values) { |
| 12346 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12347 | "updateConfiguration()"); |
| 12348 | |
| 12349 | synchronized(this) { |
| 12350 | if (values == null && mWindowManager != null) { |
| 12351 | // sentinel: fetch the current configuration from the window manager |
| 12352 | values = mWindowManager.computeNewConfiguration(); |
| 12353 | } |
| 12354 | |
| 12355 | final long origId = Binder.clearCallingIdentity(); |
| 12356 | updateConfigurationLocked(values, null); |
| 12357 | Binder.restoreCallingIdentity(origId); |
| 12358 | } |
| 12359 | } |
| 12360 | |
| 12361 | /** |
| 12362 | * Do either or both things: (1) change the current configuration, and (2) |
| 12363 | * make sure the given activity is running with the (now) current |
| 12364 | * configuration. Returns true if the activity has been left running, or |
| 12365 | * false if <var>starting</var> is being destroyed to match the new |
| 12366 | * configuration. |
| 12367 | */ |
| 12368 | public boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12369 | ActivityRecord starting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12370 | int changes = 0; |
| 12371 | |
| 12372 | boolean kept = true; |
| 12373 | |
| 12374 | if (values != null) { |
| 12375 | Configuration newConfig = new Configuration(mConfiguration); |
| 12376 | changes = newConfig.updateFrom(values); |
| 12377 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12378 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12379 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12380 | } |
| 12381 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12382 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12383 | |
| 12384 | if (values.locale != null) { |
| 12385 | saveLocaleLocked(values.locale, |
| 12386 | !values.locale.equals(mConfiguration.locale), |
| 12387 | values.userSetLocale); |
| 12388 | } |
| 12389 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12390 | mConfigurationSeq++; |
| 12391 | if (mConfigurationSeq <= 0) { |
| 12392 | mConfigurationSeq = 1; |
| 12393 | } |
| 12394 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12395 | mConfiguration = newConfig; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12396 | Slog.i(TAG, "Config changed: " + newConfig); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12397 | |
| 12398 | AttributeCache ac = AttributeCache.instance(); |
| 12399 | if (ac != null) { |
| 12400 | ac.updateConfiguration(mConfiguration); |
| 12401 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12402 | |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12403 | if (Settings.System.hasInterestingConfigurationChanges(changes)) { |
| 12404 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
| 12405 | msg.obj = new Configuration(mConfiguration); |
| 12406 | mHandler.sendMessage(msg); |
| 12407 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12408 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12409 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 12410 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12411 | try { |
| 12412 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12413 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12414 | + app.processName + " new config " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12415 | app.thread.scheduleConfigurationChanged(mConfiguration); |
| 12416 | } |
| 12417 | } catch (Exception e) { |
| 12418 | } |
| 12419 | } |
| 12420 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12421 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 12422 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12423 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
| 12424 | null, false, false, MY_PID, Process.SYSTEM_UID); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12425 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
| 12426 | broadcastIntentLocked(null, null, |
| 12427 | new Intent(Intent.ACTION_LOCALE_CHANGED), |
| 12428 | null, null, 0, null, null, |
| 12429 | null, false, false, MY_PID, Process.SYSTEM_UID); |
| 12430 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12431 | } |
| 12432 | } |
| 12433 | |
| 12434 | if (changes != 0 && starting == null) { |
| 12435 | // If the configuration changed, and the caller is not already |
| 12436 | // in the process of starting an activity, then find the top |
| 12437 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12438 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12439 | } |
| 12440 | |
| 12441 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12442 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 12443 | // And we need to make sure at this point that all other activities |
| 12444 | // are made visible with the correct configuration. |
| 12445 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12446 | } |
| 12447 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12448 | if (values != null && mWindowManager != null) { |
| 12449 | mWindowManager.setNewConfiguration(mConfiguration); |
| 12450 | } |
| 12451 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12452 | return kept; |
| 12453 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12454 | |
| 12455 | /** |
| 12456 | * Save the locale. You must be inside a synchronized (this) block. |
| 12457 | */ |
| 12458 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 12459 | if(isDiff) { |
| 12460 | SystemProperties.set("user.language", l.getLanguage()); |
| 12461 | SystemProperties.set("user.region", l.getCountry()); |
| 12462 | } |
| 12463 | |
| 12464 | if(isPersist) { |
| 12465 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 12466 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 12467 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 12468 | } |
| 12469 | } |
| 12470 | |
| 12471 | // ========================================================= |
| 12472 | // LIFETIME MANAGEMENT |
| 12473 | // ========================================================= |
| 12474 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12475 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, |
| 12476 | ProcessRecord TOP_APP, boolean recursed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12477 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12478 | // This adjustment has already been computed. If we are calling |
| 12479 | // from the top, we may have already computed our adjustment with |
| 12480 | // an earlier hidden adjustment that isn't really for us... if |
| 12481 | // so, use the new hidden adjustment. |
| 12482 | if (!recursed && app.hidden) { |
| 12483 | app.curAdj = hiddenAdj; |
| 12484 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12485 | return app.curAdj; |
| 12486 | } |
| 12487 | |
| 12488 | if (app.thread == null) { |
| 12489 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12490 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12491 | return (app.curAdj=EMPTY_APP_ADJ); |
| 12492 | } |
| 12493 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12494 | if (app.maxAdj <= FOREGROUND_APP_ADJ) { |
| 12495 | // The max adjustment doesn't allow this app to be anything |
| 12496 | // below foreground, so it is not worth doing work for it. |
| 12497 | app.adjType = "fixed"; |
| 12498 | app.adjSeq = mAdjSeq; |
| 12499 | app.curRawAdj = app.maxAdj; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12500 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12501 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12502 | return (app.curAdj=app.maxAdj); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12503 | } |
| 12504 | |
| 12505 | final boolean hadForegroundActivities = app.foregroundActivities; |
| 12506 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 12507 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12508 | app.adjSource = null; |
| 12509 | app.adjTarget = null; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12510 | app.keeping = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12511 | app.empty = false; |
| 12512 | app.hidden = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12513 | app.foregroundActivities = false; |
| 12514 | |
| 12515 | final int activitiesSize = app.activities.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12516 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12517 | // Determine the importance of the process, starting with most |
| 12518 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12519 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12520 | int schedGroup; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12521 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12522 | // The last app on the list is the foreground app. |
| 12523 | adj = FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12524 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12525 | app.adjType = "top-activity"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12526 | app.foregroundActivities = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12527 | } else if (app.instrumentationClass != null) { |
| 12528 | // Don't want to kill running instrumentation. |
| 12529 | adj = FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12530 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12531 | app.adjType = "instrumentation"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12532 | } else if (app.curReceiver != null || |
| 12533 | (mPendingBroadcast != null && mPendingBroadcast.curApp == app)) { |
| 12534 | // An app that is currently receiving a broadcast also |
| 12535 | // counts as being in the foreground. |
| 12536 | adj = FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12537 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12538 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12539 | } else if (app.executingServices.size() > 0) { |
| 12540 | // An app that is currently executing a service callback also |
| 12541 | // counts as being in the foreground. |
| 12542 | adj = FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12543 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12544 | app.adjType = "exec-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12545 | } else if (activitiesSize > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12546 | // This app is in the background with paused activities. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12547 | // We inspect activities to potentially upgrade adjustment further below. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12548 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12549 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12550 | app.hidden = true; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12551 | app.adjType = "bg-activities"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12552 | } else { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12553 | // A very not-needed process. If this is lower in the lru list, |
| 12554 | // we will push it in to the empty bucket. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12555 | adj = hiddenAdj; |
| 12556 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12557 | app.hidden = true; |
| 12558 | app.empty = true; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12559 | app.adjType = "bg-empty"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12560 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12561 | |
| 12562 | // Examine all activities if not already foreground. |
| 12563 | if (!app.foregroundActivities && activitiesSize > 0) { |
| 12564 | for (int j = 0; j < activitiesSize; j++) { |
| 12565 | final ActivityRecord r = app.activities.get(j); |
| 12566 | if (r.visible) { |
| 12567 | // App has a visible activity; only upgrade adjustment. |
| 12568 | if (adj > VISIBLE_APP_ADJ) { |
| 12569 | adj = VISIBLE_APP_ADJ; |
| 12570 | app.adjType = "visible"; |
| 12571 | } |
| 12572 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12573 | app.hidden = false; |
| 12574 | app.foregroundActivities = true; |
| 12575 | break; |
| 12576 | } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED |
| 12577 | || r.state == ActivityState.STOPPING) { |
| 12578 | // Only upgrade adjustment. |
| 12579 | if (adj > PERCEPTIBLE_APP_ADJ) { |
| 12580 | adj = PERCEPTIBLE_APP_ADJ; |
| 12581 | app.adjType = "stopping"; |
| 12582 | } |
| 12583 | app.foregroundActivities = true; |
| 12584 | } |
| 12585 | } |
| 12586 | } |
| 12587 | |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12588 | if (adj > PERCEPTIBLE_APP_ADJ) { |
| 12589 | if (app.foregroundServices) { |
| 12590 | // The user is aware of this app, so make it visible. |
| 12591 | adj = PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12592 | app.adjType = "foreground-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12593 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12594 | } else if (app.forcingToForeground != null) { |
| 12595 | // The user is aware of this app, so make it visible. |
| 12596 | adj = PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12597 | app.adjType = "force-foreground"; |
| 12598 | app.adjSource = app.forcingToForeground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12599 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12600 | } |
| 12601 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12602 | |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12603 | if (adj > HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) { |
| 12604 | // We don't want to kill the current heavy-weight process. |
| 12605 | adj = HEAVY_WEIGHT_APP_ADJ; |
| 12606 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 12607 | app.adjType = "heavy"; |
| 12608 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12609 | |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12610 | if (adj > HOME_APP_ADJ && app == mHomeProcess) { |
| 12611 | // This process is hosting what we currently consider to be the |
| 12612 | // home app, so we don't want to let it go into the background. |
| 12613 | adj = HOME_APP_ADJ; |
| 12614 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 12615 | app.adjType = "home"; |
| 12616 | } |
| 12617 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12618 | //Slog.i(TAG, "OOM " + app + ": initial adj=" + adj); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12619 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12620 | // 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] | 12621 | // there are applications dependent on our services or providers, but |
| 12622 | // this gives us a baseline and makes sure we don't get into an |
| 12623 | // infinite recursion. |
| 12624 | app.adjSeq = mAdjSeq; |
| 12625 | app.curRawAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12626 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12627 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 12628 | // If possible we want to avoid killing apps while they're being backed up |
| 12629 | if (adj > BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12630 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12631 | adj = BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12632 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12633 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12634 | } |
| 12635 | } |
| 12636 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12637 | if (app.services.size() != 0 && (adj > FOREGROUND_APP_ADJ |
| 12638 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12639 | final long now = SystemClock.uptimeMillis(); |
| 12640 | // This process is more important if the top activity is |
| 12641 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12642 | Iterator<ServiceRecord> jt = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12643 | while (jt.hasNext() && adj > FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12644 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12645 | if (s.startRequested) { |
| 12646 | if (now < (s.lastActivity+MAX_SERVICE_INACTIVITY)) { |
| 12647 | // This service has seen some activity within |
| 12648 | // recent memory, so we will keep its process ahead |
| 12649 | // of the background processes. |
| 12650 | if (adj > SECONDARY_SERVER_ADJ) { |
| 12651 | adj = SECONDARY_SERVER_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12652 | app.adjType = "started-services"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12653 | app.hidden = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12654 | } |
| 12655 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12656 | // If we have let the service slide into the background |
| 12657 | // state, still have some text describing what it is doing |
| 12658 | // even though the service no longer has an impact. |
| 12659 | if (adj > SECONDARY_SERVER_ADJ) { |
| 12660 | app.adjType = "started-bg-services"; |
| 12661 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12662 | // Don't kill this process because it is doing work; it |
| 12663 | // has said it is doing work. |
| 12664 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12665 | } |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12666 | if (s.connections.size() > 0 && (adj > FOREGROUND_APP_ADJ |
| 12667 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12668 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12669 | = s.connections.values().iterator(); |
| 12670 | while (kt.hasNext() && adj > FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12671 | ArrayList<ConnectionRecord> clist = kt.next(); |
| 12672 | for (int i=0; i<clist.size() && adj > FOREGROUND_APP_ADJ; i++) { |
| 12673 | // XXX should compute this based on the max of |
| 12674 | // all connected clients. |
| 12675 | ConnectionRecord cr = clist.get(i); |
| 12676 | if (cr.binding.client == app) { |
| 12677 | // Binding to ourself is not interesting. |
| 12678 | continue; |
| 12679 | } |
| 12680 | if ((cr.flags&Context.BIND_AUTO_CREATE) != 0) { |
| 12681 | ProcessRecord client = cr.binding.client; |
| 12682 | int myHiddenAdj = hiddenAdj; |
| 12683 | if (myHiddenAdj > client.hiddenAdj) { |
| 12684 | if (client.hiddenAdj >= VISIBLE_APP_ADJ) { |
| 12685 | myHiddenAdj = client.hiddenAdj; |
| 12686 | } else { |
| 12687 | myHiddenAdj = VISIBLE_APP_ADJ; |
| 12688 | } |
| 12689 | } |
| 12690 | int clientAdj = computeOomAdjLocked( |
| 12691 | client, myHiddenAdj, TOP_APP, true); |
| 12692 | if (adj > clientAdj) { |
| 12693 | adj = clientAdj >= VISIBLE_APP_ADJ |
| 12694 | ? clientAdj : VISIBLE_APP_ADJ; |
| 12695 | if (!client.hidden) { |
| 12696 | app.hidden = false; |
| 12697 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12698 | if (client.keeping) { |
| 12699 | app.keeping = true; |
| 12700 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12701 | app.adjType = "service"; |
| 12702 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12703 | .REASON_SERVICE_IN_USE; |
| 12704 | app.adjSource = cr.binding.client; |
| 12705 | app.adjTarget = s.name; |
| 12706 | } |
| 12707 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 12708 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 12709 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12710 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12711 | } |
| 12712 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12713 | ActivityRecord a = cr.activity; |
| 12714 | //if (a != null) { |
| 12715 | // Slog.i(TAG, "Connection to " + a ": state=" + a.state); |
| 12716 | //} |
| 12717 | if (a != null && adj > FOREGROUND_APP_ADJ && |
| 12718 | (a.state == ActivityState.RESUMED |
| 12719 | || a.state == ActivityState.PAUSING)) { |
| 12720 | adj = FOREGROUND_APP_ADJ; |
| 12721 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12722 | app.hidden = false; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12723 | app.adjType = "service"; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 12724 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12725 | .REASON_SERVICE_IN_USE; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12726 | app.adjSource = a; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 12727 | app.adjTarget = s.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12728 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12729 | } |
| 12730 | } |
| 12731 | } |
| 12732 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12733 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12734 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12735 | // would like to avoid killing it unless it would prevent the current |
| 12736 | // application from running. By default we put the process in |
| 12737 | // with the rest of the background processes; as we scan through |
| 12738 | // its services we may bump it up from there. |
| 12739 | if (adj > hiddenAdj) { |
| 12740 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12741 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12742 | app.adjType = "bg-services"; |
| 12743 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12744 | } |
| 12745 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12746 | if (app.pubProviders.size() != 0 && (adj > FOREGROUND_APP_ADJ |
| 12747 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12748 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12749 | while (jt.hasNext() && (adj > FOREGROUND_APP_ADJ |
| 12750 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12751 | ContentProviderRecord cpr = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12752 | if (cpr.clients.size() != 0) { |
| 12753 | Iterator<ProcessRecord> kt = cpr.clients.iterator(); |
| 12754 | while (kt.hasNext() && adj > FOREGROUND_APP_ADJ) { |
| 12755 | ProcessRecord client = kt.next(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 12756 | if (client == app) { |
| 12757 | // Being our own client is not interesting. |
| 12758 | continue; |
| 12759 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12760 | int myHiddenAdj = hiddenAdj; |
| 12761 | if (myHiddenAdj > client.hiddenAdj) { |
| 12762 | if (client.hiddenAdj > FOREGROUND_APP_ADJ) { |
| 12763 | myHiddenAdj = client.hiddenAdj; |
| 12764 | } else { |
| 12765 | myHiddenAdj = FOREGROUND_APP_ADJ; |
| 12766 | } |
| 12767 | } |
| 12768 | int clientAdj = computeOomAdjLocked( |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12769 | client, myHiddenAdj, TOP_APP, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12770 | if (adj > clientAdj) { |
| 12771 | adj = clientAdj > FOREGROUND_APP_ADJ |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12772 | ? clientAdj : FOREGROUND_APP_ADJ; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12773 | if (!client.hidden) { |
| 12774 | app.hidden = false; |
| 12775 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12776 | if (client.keeping) { |
| 12777 | app.keeping = true; |
| 12778 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12779 | app.adjType = "provider"; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 12780 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12781 | .REASON_PROVIDER_IN_USE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12782 | app.adjSource = client; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 12783 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12784 | } |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12785 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 12786 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12787 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12788 | } |
| 12789 | } |
| 12790 | // If the provider has external (non-framework) process |
| 12791 | // dependencies, ensure that its adjustment is at least |
| 12792 | // FOREGROUND_APP_ADJ. |
| 12793 | if (cpr.externals != 0) { |
| 12794 | if (adj > FOREGROUND_APP_ADJ) { |
| 12795 | adj = FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12796 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12797 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12798 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12799 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 12800 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12801 | } |
| 12802 | } |
| 12803 | } |
| 12804 | } |
| 12805 | |
| 12806 | app.curRawAdj = adj; |
| 12807 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12808 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12809 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 12810 | if (adj > app.maxAdj) { |
| 12811 | adj = app.maxAdj; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 12812 | if (app.maxAdj <= PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12813 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12814 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12815 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12816 | if (adj < HIDDEN_APP_MIN_ADJ) { |
| 12817 | app.keeping = true; |
| 12818 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12819 | |
| 12820 | app.curAdj = adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12821 | app.curSchedGroup = schedGroup; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12822 | |
| 12823 | if (hadForegroundActivities != app.foregroundActivities) { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame^] | 12824 | mHandler.obtainMessage(DISPATCH_FOREGROUND_ACTIVITIES_CHANGED, app.pid, app.info.uid, |
| 12825 | app.foregroundActivities).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12826 | } |
| 12827 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12828 | return adj; |
| 12829 | } |
| 12830 | |
| 12831 | /** |
| 12832 | * Ask a given process to GC right now. |
| 12833 | */ |
| 12834 | final void performAppGcLocked(ProcessRecord app) { |
| 12835 | try { |
| 12836 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 12837 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12838 | if (app.reportLowMemory) { |
| 12839 | app.reportLowMemory = false; |
| 12840 | app.thread.scheduleLowMemory(); |
| 12841 | } else { |
| 12842 | app.thread.processInBackground(); |
| 12843 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12844 | } |
| 12845 | } catch (Exception e) { |
| 12846 | // whatever. |
| 12847 | } |
| 12848 | } |
| 12849 | |
| 12850 | /** |
| 12851 | * Returns true if things are idle enough to perform GCs. |
| 12852 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12853 | private final boolean canGcNowLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12854 | return mParallelBroadcasts.size() == 0 |
| 12855 | && mOrderedBroadcasts.size() == 0 |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12856 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 12857 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12858 | } |
| 12859 | |
| 12860 | /** |
| 12861 | * Perform GCs on all processes that are waiting for it, but only |
| 12862 | * if things are idle. |
| 12863 | */ |
| 12864 | final void performAppGcsLocked() { |
| 12865 | final int N = mProcessesToGc.size(); |
| 12866 | if (N <= 0) { |
| 12867 | return; |
| 12868 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12869 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12870 | while (mProcessesToGc.size() > 0) { |
| 12871 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 12872 | if (proc.curRawAdj > PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12873 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 12874 | <= SystemClock.uptimeMillis()) { |
| 12875 | // To avoid spamming the system, we will GC processes one |
| 12876 | // at a time, waiting a few seconds between each. |
| 12877 | performAppGcLocked(proc); |
| 12878 | scheduleAppGcsLocked(); |
| 12879 | return; |
| 12880 | } else { |
| 12881 | // It hasn't been long enough since we last GCed this |
| 12882 | // process... put it in the list to wait for its time. |
| 12883 | addProcessToGcListLocked(proc); |
| 12884 | break; |
| 12885 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12886 | } |
| 12887 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12888 | |
| 12889 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12890 | } |
| 12891 | } |
| 12892 | |
| 12893 | /** |
| 12894 | * If all looks good, perform GCs on all processes waiting for them. |
| 12895 | */ |
| 12896 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12897 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12898 | performAppGcsLocked(); |
| 12899 | return; |
| 12900 | } |
| 12901 | // Still not idle, wait some more. |
| 12902 | scheduleAppGcsLocked(); |
| 12903 | } |
| 12904 | |
| 12905 | /** |
| 12906 | * Schedule the execution of all pending app GCs. |
| 12907 | */ |
| 12908 | final void scheduleAppGcsLocked() { |
| 12909 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12910 | |
| 12911 | if (mProcessesToGc.size() > 0) { |
| 12912 | // Schedule a GC for the time to the next process. |
| 12913 | ProcessRecord proc = mProcessesToGc.get(0); |
| 12914 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 12915 | |
| 12916 | long when = mProcessesToGc.get(0).lastRequestedGc + GC_MIN_INTERVAL; |
| 12917 | long now = SystemClock.uptimeMillis(); |
| 12918 | if (when < (now+GC_TIMEOUT)) { |
| 12919 | when = now + GC_TIMEOUT; |
| 12920 | } |
| 12921 | mHandler.sendMessageAtTime(msg, when); |
| 12922 | } |
| 12923 | } |
| 12924 | |
| 12925 | /** |
| 12926 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 12927 | * list in sorted order by the last GC time. The process can't already be |
| 12928 | * on the list. |
| 12929 | */ |
| 12930 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 12931 | boolean added = false; |
| 12932 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 12933 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 12934 | proc.lastRequestedGc) { |
| 12935 | added = true; |
| 12936 | mProcessesToGc.add(i+1, proc); |
| 12937 | break; |
| 12938 | } |
| 12939 | } |
| 12940 | if (!added) { |
| 12941 | mProcessesToGc.add(0, proc); |
| 12942 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12943 | } |
| 12944 | |
| 12945 | /** |
| 12946 | * Set up to ask a process to GC itself. This will either do it |
| 12947 | * immediately, or put it on the list of processes to gc the next |
| 12948 | * time things are idle. |
| 12949 | */ |
| 12950 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 12951 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12952 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12953 | return; |
| 12954 | } |
| 12955 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12956 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12957 | scheduleAppGcsLocked(); |
| 12958 | } |
| 12959 | } |
| 12960 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12961 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 12962 | updateCpuStatsNow(); |
| 12963 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12964 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12965 | boolean doWakeKills = doKills; |
| 12966 | boolean doCpuKills = doKills; |
| 12967 | if (mLastPowerCheckRealtime == 0) { |
| 12968 | doWakeKills = false; |
| 12969 | } |
| 12970 | if (mLastPowerCheckUptime == 0) { |
| 12971 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12972 | } |
| 12973 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12974 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12975 | } |
| 12976 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12977 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 12978 | final long curUptime = SystemClock.uptimeMillis(); |
| 12979 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 12980 | mLastPowerCheckRealtime = curRealtime; |
| 12981 | mLastPowerCheckUptime = curUptime; |
| 12982 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 12983 | doWakeKills = false; |
| 12984 | } |
| 12985 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 12986 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12987 | } |
| 12988 | int i = mLruProcesses.size(); |
| 12989 | while (i > 0) { |
| 12990 | i--; |
| 12991 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12992 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12993 | long wtime; |
| 12994 | synchronized (stats) { |
| 12995 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 12996 | app.pid, curRealtime); |
| 12997 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12998 | long wtimeUsed = wtime - app.lastWakeTime; |
| 12999 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 13000 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13001 | StringBuilder sb = new StringBuilder(128); |
| 13002 | sb.append("Wake for "); |
| 13003 | app.toShortString(sb); |
| 13004 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13005 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13006 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13007 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13008 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13009 | sb.append((wtimeUsed*100)/realtimeSince); |
| 13010 | sb.append("%)"); |
| 13011 | Slog.i(TAG, sb.toString()); |
| 13012 | sb.setLength(0); |
| 13013 | sb.append("CPU for "); |
| 13014 | app.toShortString(sb); |
| 13015 | sb.append(": over "); |
| 13016 | TimeUtils.formatDuration(uptimeSince, sb); |
| 13017 | sb.append(" used "); |
| 13018 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 13019 | sb.append(" ("); |
| 13020 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13021 | sb.append("%)"); |
| 13022 | Slog.i(TAG, sb.toString()); |
| 13023 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13024 | // If a process has held a wake lock for more |
| 13025 | // than 50% of the time during this period, |
| 13026 | // that sounds pad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13027 | if (doWakeKills && realtimeSince > 0 |
| 13028 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 13029 | synchronized (stats) { |
| 13030 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 13031 | realtimeSince, wtimeUsed); |
| 13032 | } |
| 13033 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 13034 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 13035 | + " during " + realtimeSince); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13036 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13037 | app.processName, app.setAdj, "excessive wake lock"); |
| 13038 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13039 | } else if (doCpuKills && uptimeSince > 0 |
| 13040 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 13041 | synchronized (stats) { |
| 13042 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 13043 | uptimeSince, cputimeUsed); |
| 13044 | } |
| 13045 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 13046 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 13047 | + " during " + uptimeSince); |
| 13048 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13049 | app.processName, app.setAdj, "excessive cpu"); |
| 13050 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13051 | } else { |
| 13052 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13053 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13054 | } |
| 13055 | } |
| 13056 | } |
| 13057 | } |
| 13058 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13059 | private final boolean updateOomAdjLocked( |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13060 | ProcessRecord app, int hiddenAdj, ProcessRecord TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13061 | app.hiddenAdj = hiddenAdj; |
| 13062 | |
| 13063 | if (app.thread == null) { |
| 13064 | return true; |
| 13065 | } |
| 13066 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13067 | boolean success = true; |
| 13068 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13069 | final boolean wasKeeping = app.keeping; |
| 13070 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13071 | int adj = computeOomAdjLocked(app, hiddenAdj, TOP_APP, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13072 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13073 | if ((app.pid != 0 && app.pid != MY_PID) || Process.supportsProcesses()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13074 | if (app.curRawAdj != app.setRawAdj) { |
| 13075 | if (app.curRawAdj > FOREGROUND_APP_ADJ |
| 13076 | && app.setRawAdj <= FOREGROUND_APP_ADJ) { |
| 13077 | // If this app is transitioning from foreground to |
| 13078 | // non-foreground, have it do a gc. |
| 13079 | scheduleAppGcLocked(app); |
| 13080 | } else if (app.curRawAdj >= HIDDEN_APP_MIN_ADJ |
| 13081 | && app.setRawAdj < HIDDEN_APP_MIN_ADJ) { |
| 13082 | // Likewise do a gc when an app is moving in to the |
| 13083 | // background (such as a service stopping). |
| 13084 | scheduleAppGcLocked(app); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13085 | } |
| 13086 | |
| 13087 | if (wasKeeping && !app.keeping) { |
| 13088 | // This app is no longer something we want to keep. Note |
| 13089 | // its current wake lock time to later know to kill it if |
| 13090 | // it is not behaving well. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13091 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 13092 | synchronized (stats) { |
| 13093 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 13094 | app.pid, SystemClock.elapsedRealtime()); |
| 13095 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13096 | app.lastCpuTime = app.curCpuTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13097 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13098 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13099 | app.setRawAdj = app.curRawAdj; |
| 13100 | } |
| 13101 | if (adj != app.setAdj) { |
| 13102 | if (Process.setOomAdj(app.pid, adj)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13103 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13104 | TAG, "Set app " + app.processName + |
| 13105 | " oom adj to " + adj); |
| 13106 | app.setAdj = adj; |
| 13107 | } else { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13108 | success = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13109 | } |
| 13110 | } |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13111 | if (app.setSchedGroup != app.curSchedGroup) { |
| 13112 | app.setSchedGroup = app.curSchedGroup; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13113 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13114 | "Setting process group of " + app.processName |
| 13115 | + " to " + app.curSchedGroup); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 13116 | if (app.waitingToKill != null && |
| 13117 | app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 13118 | Slog.i(TAG, "Killing " + app + ": " + app.waitingToKill); |
| 13119 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13120 | app.processName, app.setAdj, app.waitingToKill); |
| 13121 | Process.killProcessQuiet(app.pid); |
| 13122 | } else { |
| 13123 | if (true) { |
| 13124 | long oldId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13125 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 13126 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 13127 | } catch (Exception e) { |
| 13128 | Slog.w(TAG, "Failed setting process group of " + app.pid |
| 13129 | + " to " + app.curSchedGroup); |
| 13130 | e.printStackTrace(); |
| 13131 | } finally { |
| 13132 | Binder.restoreCallingIdentity(oldId); |
| 13133 | } |
| 13134 | } |
| 13135 | if (false) { |
| 13136 | if (app.thread != null) { |
| 13137 | try { |
| 13138 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 13139 | } catch (RemoteException e) { |
| 13140 | } |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13141 | } |
| 13142 | } |
| 13143 | } |
| 13144 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13145 | } |
| 13146 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13147 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13148 | } |
| 13149 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13150 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13151 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13152 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13153 | resumedActivity = mMainStack.mPausingActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13154 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13155 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13156 | } |
| 13157 | } |
| 13158 | return resumedActivity; |
| 13159 | } |
| 13160 | |
| 13161 | private final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13162 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13163 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13164 | int curAdj = app.curAdj; |
| 13165 | final boolean wasHidden = app.curAdj >= HIDDEN_APP_MIN_ADJ |
| 13166 | && app.curAdj <= HIDDEN_APP_MAX_ADJ; |
| 13167 | |
| 13168 | mAdjSeq++; |
| 13169 | |
| 13170 | final boolean res = updateOomAdjLocked(app, app.hiddenAdj, TOP_APP); |
| 13171 | if (res) { |
| 13172 | final boolean nowHidden = app.curAdj >= HIDDEN_APP_MIN_ADJ |
| 13173 | && app.curAdj <= HIDDEN_APP_MAX_ADJ; |
| 13174 | if (nowHidden != wasHidden) { |
| 13175 | // Changed to/from hidden state, so apps after it in the LRU |
| 13176 | // list may also be changed. |
| 13177 | updateOomAdjLocked(); |
| 13178 | } |
| 13179 | } |
| 13180 | return res; |
| 13181 | } |
| 13182 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13183 | final boolean updateOomAdjLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13184 | boolean didOomAdj = true; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13185 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13186 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13187 | |
| 13188 | if (false) { |
| 13189 | RuntimeException e = new RuntimeException(); |
| 13190 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13191 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13192 | } |
| 13193 | |
| 13194 | mAdjSeq++; |
| 13195 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13196 | // Let's determine how many processes we have running vs. |
| 13197 | // how many slots we have for background processes; we may want |
| 13198 | // to put multiple processes in a slot of there are enough of |
| 13199 | // them. |
| 13200 | int numSlots = HIDDEN_APP_MAX_ADJ - HIDDEN_APP_MIN_ADJ + 1; |
| 13201 | int factor = (mLruProcesses.size()-4)/numSlots; |
| 13202 | if (factor < 1) factor = 1; |
| 13203 | int step = 0; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13204 | int numHidden = 0; |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13205 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13206 | // First try updating the OOM adjustment for each of the |
| 13207 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13208 | int i = mLruProcesses.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13209 | int curHiddenAdj = HIDDEN_APP_MIN_ADJ; |
| 13210 | while (i > 0) { |
| 13211 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13212 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13213 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13214 | if (updateOomAdjLocked(app, curHiddenAdj, TOP_APP)) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13215 | if (curHiddenAdj < EMPTY_APP_ADJ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13216 | && app.curAdj == curHiddenAdj) { |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13217 | step++; |
| 13218 | if (step >= factor) { |
| 13219 | step = 0; |
| 13220 | curHiddenAdj++; |
| 13221 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13222 | } |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13223 | if (app.curAdj >= HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 149427c | 2010-04-23 14:20:03 -0700 | [diff] [blame] | 13224 | if (!app.killedBackground) { |
| 13225 | numHidden++; |
| 13226 | if (numHidden > MAX_HIDDEN_APPS) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 13227 | Slog.i(TAG, "No longer want " + app.processName |
| 13228 | + " (pid " + app.pid + "): hidden #" + numHidden); |
Dianne Hackborn | 149427c | 2010-04-23 14:20:03 -0700 | [diff] [blame] | 13229 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13230 | app.processName, app.setAdj, "too many background"); |
| 13231 | app.killedBackground = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 13232 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 149427c | 2010-04-23 14:20:03 -0700 | [diff] [blame] | 13233 | } |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13234 | } |
| 13235 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13236 | } else { |
| 13237 | didOomAdj = false; |
| 13238 | } |
| 13239 | } |
| 13240 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13241 | // If we return false, we will fall back on killing processes to |
| 13242 | // have a fixed limit. Do this if a limit has been requested; else |
| 13243 | // only return false if one of the adjustments failed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13244 | return ENFORCE_PROCESS_LIMIT || mProcessLimit > 0 ? false : didOomAdj; |
| 13245 | } |
| 13246 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13247 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13248 | synchronized (this) { |
| 13249 | int i; |
| 13250 | |
| 13251 | // First remove any unused application processes whose package |
| 13252 | // has been removed. |
| 13253 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 13254 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 13255 | if (app.activities.size() == 0 |
| 13256 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13257 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13258 | TAG, "Exiting empty application process " |
| 13259 | + app.processName + " (" |
| 13260 | + (app.thread != null ? app.thread.asBinder() : null) |
| 13261 | + ")\n"); |
| 13262 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 13263 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13264 | app.processName, app.setAdj, "empty"); |
| 13265 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13266 | } else { |
| 13267 | try { |
| 13268 | app.thread.scheduleExit(); |
| 13269 | } catch (Exception e) { |
| 13270 | // Ignore exceptions. |
| 13271 | } |
| 13272 | } |
| 13273 | cleanUpApplicationRecordLocked(app, false, -1); |
| 13274 | mRemovedProcesses.remove(i); |
| 13275 | |
| 13276 | if (app.persistent) { |
| 13277 | if (app.persistent) { |
| 13278 | addAppLocked(app.info); |
| 13279 | } |
| 13280 | } |
| 13281 | } |
| 13282 | } |
| 13283 | |
| 13284 | // Now try updating the OOM adjustment for each of the |
| 13285 | // application processes based on their current state. |
| 13286 | // If the setOomAdj() API is not supported, then go with our |
| 13287 | // back-up plan... |
| 13288 | if (!updateOomAdjLocked()) { |
| 13289 | |
| 13290 | // Count how many processes are running services. |
| 13291 | int numServiceProcs = 0; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13292 | for (i=mLruProcesses.size()-1; i>=0; i--) { |
| 13293 | final ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13294 | |
| 13295 | if (app.persistent || app.services.size() != 0 |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 13296 | || app.curReceiver != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13297 | // Don't count processes holding services against our |
| 13298 | // maximum process count. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13299 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13300 | TAG, "Not trimming app " + app + " with services: " |
| 13301 | + app.services); |
| 13302 | numServiceProcs++; |
| 13303 | } |
| 13304 | } |
| 13305 | |
| 13306 | int curMaxProcs = mProcessLimit; |
| 13307 | if (curMaxProcs <= 0) curMaxProcs = MAX_PROCESSES; |
| 13308 | if (mAlwaysFinishActivities) { |
| 13309 | curMaxProcs = 1; |
| 13310 | } |
| 13311 | curMaxProcs += numServiceProcs; |
| 13312 | |
| 13313 | // Quit as many processes as we can to get down to the desired |
| 13314 | // process count. First remove any processes that no longer |
| 13315 | // have activites running in them. |
| 13316 | for ( i=0; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13317 | i<mLruProcesses.size() |
| 13318 | && mLruProcesses.size() > curMaxProcs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13319 | i++) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13320 | final ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13321 | // Quit an application only if it is not currently |
| 13322 | // running any activities. |
| 13323 | if (!app.persistent && app.activities.size() == 0 |
| 13324 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13325 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13326 | TAG, "Exiting empty application process " |
| 13327 | + app.processName + " (" |
| 13328 | + (app.thread != null ? app.thread.asBinder() : null) |
| 13329 | + ")\n"); |
| 13330 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 13331 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13332 | app.processName, app.setAdj, "empty"); |
| 13333 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13334 | } else { |
| 13335 | try { |
| 13336 | app.thread.scheduleExit(); |
| 13337 | } catch (Exception e) { |
| 13338 | // Ignore exceptions. |
| 13339 | } |
| 13340 | } |
| 13341 | // todo: For now we assume the application is not buggy |
| 13342 | // or evil, and will quit as a result of our request. |
| 13343 | // Eventually we need to drive this off of the death |
| 13344 | // notification, and kill the process if it takes too long. |
| 13345 | cleanUpApplicationRecordLocked(app, false, i); |
| 13346 | i--; |
| 13347 | } |
| 13348 | } |
| 13349 | |
| 13350 | // If we still have too many processes, now from the least |
| 13351 | // recently used process we start finishing activities. |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 13352 | if (false) Slog.v( |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13353 | TAG, "*** NOW HAVE " + mLruProcesses.size() + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13354 | " of " + curMaxProcs + " processes"); |
| 13355 | for ( i=0; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13356 | i<mLruProcesses.size() |
| 13357 | && mLruProcesses.size() > curMaxProcs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13358 | i++) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13359 | final ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13360 | // Quit the application only if we have a state saved for |
| 13361 | // all of its activities. |
| 13362 | boolean canQuit = !app.persistent && app.curReceiver == null |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 13363 | && app.services.size() == 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13364 | int NUMA = app.activities.size(); |
| 13365 | int j; |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 13366 | if (false) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13367 | TAG, "Looking to quit " + app.processName); |
| 13368 | for (j=0; j<NUMA && canQuit; j++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13369 | ActivityRecord r = app.activities.get(j); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 13370 | if (false) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13371 | TAG, " " + r.intent.getComponent().flattenToShortString() |
| 13372 | + ": frozen=" + r.haveState + ", visible=" + r.visible); |
| 13373 | canQuit = (r.haveState || !r.stateNotNeeded) |
| 13374 | && !r.visible && r.stopped; |
| 13375 | } |
| 13376 | if (canQuit) { |
| 13377 | // Finish all of the activities, and then the app itself. |
| 13378 | for (j=0; j<NUMA; j++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13379 | ActivityRecord r = app.activities.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13380 | if (!r.finishing) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13381 | r.stack.destroyActivityLocked(r, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13382 | } |
| 13383 | r.resultTo = null; |
| 13384 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13385 | Slog.i(TAG, "Exiting application process " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13386 | + app.processName + " (" |
| 13387 | + (app.thread != null ? app.thread.asBinder() : null) |
| 13388 | + ")\n"); |
| 13389 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 13390 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13391 | app.processName, app.setAdj, "old background"); |
| 13392 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13393 | } else { |
| 13394 | try { |
| 13395 | app.thread.scheduleExit(); |
| 13396 | } catch (Exception e) { |
| 13397 | // Ignore exceptions. |
| 13398 | } |
| 13399 | } |
| 13400 | // todo: For now we assume the application is not buggy |
| 13401 | // or evil, and will quit as a result of our request. |
| 13402 | // Eventually we need to drive this off of the death |
| 13403 | // notification, and kill the process if it takes too long. |
| 13404 | cleanUpApplicationRecordLocked(app, false, i); |
| 13405 | i--; |
| 13406 | //dump(); |
| 13407 | } |
| 13408 | } |
| 13409 | |
| 13410 | } |
| 13411 | |
| 13412 | int curMaxActivities = MAX_ACTIVITIES; |
| 13413 | if (mAlwaysFinishActivities) { |
| 13414 | curMaxActivities = 1; |
| 13415 | } |
| 13416 | |
| 13417 | // Finally, if there are too many activities now running, try to |
| 13418 | // finish as many as we can to get back down to the limit. |
| 13419 | for ( i=0; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13420 | i<mMainStack.mLRUActivities.size() |
| 13421 | && mMainStack.mLRUActivities.size() > curMaxActivities; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13422 | i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13423 | final ActivityRecord r |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13424 | = (ActivityRecord)mMainStack.mLRUActivities.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13425 | |
| 13426 | // We can finish this one if we have its icicle saved and |
| 13427 | // it is not persistent. |
| 13428 | if ((r.haveState || !r.stateNotNeeded) && !r.visible |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 13429 | && r.stopped && !r.finishing) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13430 | final int origSize = mMainStack.mLRUActivities.size(); |
| 13431 | r.stack.destroyActivityLocked(r, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13432 | |
| 13433 | // This will remove it from the LRU list, so keep |
| 13434 | // our index at the same value. Note that this check to |
| 13435 | // see if the size changes is just paranoia -- if |
| 13436 | // something unexpected happens, we don't want to end up |
| 13437 | // in an infinite loop. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13438 | if (origSize > mMainStack.mLRUActivities.size()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13439 | i--; |
| 13440 | } |
| 13441 | } |
| 13442 | } |
| 13443 | } |
| 13444 | } |
| 13445 | |
| 13446 | /** This method sends the specified signal to each of the persistent apps */ |
| 13447 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 13448 | if (sig != Process.SIGNAL_USR1) { |
| 13449 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 13450 | } |
| 13451 | |
| 13452 | synchronized (this) { |
| 13453 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 13454 | != PackageManager.PERMISSION_GRANTED) { |
| 13455 | throw new SecurityException("Requires permission " |
| 13456 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 13457 | } |
| 13458 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13459 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 13460 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13461 | if (r.thread != null && r.persistent) { |
| 13462 | Process.sendSignal(r.pid, sig); |
| 13463 | } |
| 13464 | } |
| 13465 | } |
| 13466 | } |
| 13467 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13468 | public boolean profileControl(String process, boolean start, |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13469 | String path, ParcelFileDescriptor fd) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13470 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13471 | try { |
| 13472 | synchronized (this) { |
| 13473 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 13474 | // its own permission. |
| 13475 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 13476 | != PackageManager.PERMISSION_GRANTED) { |
| 13477 | throw new SecurityException("Requires permission " |
| 13478 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13479 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13480 | |
| 13481 | if (start && fd == null) { |
| 13482 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13483 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13484 | |
| 13485 | ProcessRecord proc = null; |
| 13486 | try { |
| 13487 | int pid = Integer.parseInt(process); |
| 13488 | synchronized (mPidsSelfLocked) { |
| 13489 | proc = mPidsSelfLocked.get(pid); |
| 13490 | } |
| 13491 | } catch (NumberFormatException e) { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13492 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13493 | |
| 13494 | if (proc == null) { |
| 13495 | HashMap<String, SparseArray<ProcessRecord>> all |
| 13496 | = mProcessNames.getMap(); |
| 13497 | SparseArray<ProcessRecord> procs = all.get(process); |
| 13498 | if (procs != null && procs.size() > 0) { |
| 13499 | proc = procs.valueAt(0); |
| 13500 | } |
| 13501 | } |
| 13502 | |
| 13503 | if (proc == null || proc.thread == null) { |
| 13504 | throw new IllegalArgumentException("Unknown process: " + process); |
| 13505 | } |
| 13506 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 13507 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 13508 | if (!isDebuggable) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13509 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 13510 | throw new SecurityException("Process not debuggable: " + proc); |
| 13511 | } |
| 13512 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13513 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13514 | proc.thread.profilerControl(start, path, fd); |
| 13515 | fd = null; |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13516 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13517 | } |
| 13518 | } catch (RemoteException e) { |
| 13519 | throw new IllegalStateException("Process disappeared"); |
| 13520 | } finally { |
| 13521 | if (fd != null) { |
| 13522 | try { |
| 13523 | fd.close(); |
| 13524 | } catch (IOException e) { |
| 13525 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13526 | } |
| 13527 | } |
| 13528 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13529 | |
| 13530 | public boolean dumpHeap(String process, boolean managed, |
| 13531 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 13532 | |
| 13533 | try { |
| 13534 | synchronized (this) { |
| 13535 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 13536 | // its own permission (same as profileControl). |
| 13537 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 13538 | != PackageManager.PERMISSION_GRANTED) { |
| 13539 | throw new SecurityException("Requires permission " |
| 13540 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 13541 | } |
| 13542 | |
| 13543 | if (fd == null) { |
| 13544 | throw new IllegalArgumentException("null fd"); |
| 13545 | } |
| 13546 | |
| 13547 | ProcessRecord proc = null; |
| 13548 | try { |
| 13549 | int pid = Integer.parseInt(process); |
| 13550 | synchronized (mPidsSelfLocked) { |
| 13551 | proc = mPidsSelfLocked.get(pid); |
| 13552 | } |
| 13553 | } catch (NumberFormatException e) { |
| 13554 | } |
| 13555 | |
| 13556 | if (proc == null) { |
| 13557 | HashMap<String, SparseArray<ProcessRecord>> all |
| 13558 | = mProcessNames.getMap(); |
| 13559 | SparseArray<ProcessRecord> procs = all.get(process); |
| 13560 | if (procs != null && procs.size() > 0) { |
| 13561 | proc = procs.valueAt(0); |
| 13562 | } |
| 13563 | } |
| 13564 | |
| 13565 | if (proc == null || proc.thread == null) { |
| 13566 | throw new IllegalArgumentException("Unknown process: " + process); |
| 13567 | } |
| 13568 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 13569 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 13570 | if (!isDebuggable) { |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13571 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 13572 | throw new SecurityException("Process not debuggable: " + proc); |
| 13573 | } |
| 13574 | } |
| 13575 | |
| 13576 | proc.thread.dumpHeap(managed, path, fd); |
| 13577 | fd = null; |
| 13578 | return true; |
| 13579 | } |
| 13580 | } catch (RemoteException e) { |
| 13581 | throw new IllegalStateException("Process disappeared"); |
| 13582 | } finally { |
| 13583 | if (fd != null) { |
| 13584 | try { |
| 13585 | fd.close(); |
| 13586 | } catch (IOException e) { |
| 13587 | } |
| 13588 | } |
| 13589 | } |
| 13590 | } |
| 13591 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13592 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 13593 | public void monitor() { |
| 13594 | synchronized (this) { } |
| 13595 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 13596 | |
| 13597 | public void onCoreSettingsChange(Bundle settings) { |
| 13598 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 13599 | ProcessRecord processRecord = mLruProcesses.get(i); |
| 13600 | try { |
| 13601 | if (processRecord.thread != null) { |
| 13602 | processRecord.thread.setCoreSettings(settings); |
| 13603 | } |
| 13604 | } catch (RemoteException re) { |
| 13605 | /* ignore */ |
| 13606 | } |
| 13607 | } |
| 13608 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 13609 | |
| 13610 | // Multi-user methods |
| 13611 | |
| 13612 | public boolean switchUser(int userid) { |
| 13613 | // TODO |
| 13614 | return true; |
| 13615 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13616 | } |