The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.am; |
| 18 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import com.android.internal.os.BatteryStatsImpl; |
Dianne Hackborn | 45ce864 | 2011-07-14 16:10:16 -0700 | [diff] [blame] | 21 | import com.android.internal.os.ProcessStats; |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 22 | import com.android.server.AttributeCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import com.android.server.IntentResolver; |
| 24 | import com.android.server.ProcessMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import com.android.server.SystemServer; |
| 26 | import com.android.server.Watchdog; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 27 | import com.android.server.am.ActivityStack.ActivityState; |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 28 | import com.android.server.wm.WindowManagerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 30 | import dalvik.system.Zygote; |
| 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.app.Activity; |
| 33 | import android.app.ActivityManager; |
| 34 | import android.app.ActivityManagerNative; |
| 35 | import android.app.ActivityThread; |
| 36 | import android.app.AlertDialog; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 37 | import android.app.AppGlobals; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 38 | import android.app.ApplicationErrorReport; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.app.Dialog; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 40 | import android.app.IActivityController; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.app.IActivityWatcher; |
| 42 | import android.app.IApplicationThread; |
| 43 | import android.app.IInstrumentationWatcher; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 44 | import android.app.INotificationManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 45 | import android.app.IProcessObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.app.IServiceConnection; |
| 47 | import android.app.IThumbnailReceiver; |
| 48 | import android.app.Instrumentation; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 49 | import android.app.Notification; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 50 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.app.PendingIntent; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 52 | import android.app.Service; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 53 | import android.app.backup.IBackupManager; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 54 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 55 | import android.content.BroadcastReceiver; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 56 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.content.ComponentName; |
| 58 | import android.content.ContentResolver; |
| 59 | import android.content.Context; |
Christian Mehlmauer | 7664e20 | 2010-07-20 08:46:17 +0200 | [diff] [blame] | 60 | import android.content.DialogInterface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.content.Intent; |
| 62 | import android.content.IntentFilter; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 63 | import android.content.IIntentReceiver; |
| 64 | import android.content.IIntentSender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 65 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.content.pm.ActivityInfo; |
| 67 | import android.content.pm.ApplicationInfo; |
| 68 | import android.content.pm.ConfigurationInfo; |
| 69 | import android.content.pm.IPackageDataObserver; |
| 70 | import android.content.pm.IPackageManager; |
| 71 | import android.content.pm.InstrumentationInfo; |
Dan Egnor | 66c40e7 | 2010-01-26 16:23:11 -0800 | [diff] [blame] | 72 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.content.pm.PackageManager; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 74 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import android.content.pm.ProviderInfo; |
| 76 | import android.content.pm.ResolveInfo; |
| 77 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 78 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 79 | import android.content.res.CompatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.content.res.Configuration; |
| 81 | import android.graphics.Bitmap; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 82 | import android.net.Proxy; |
| 83 | import android.net.ProxyProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.net.Uri; |
| 85 | import android.os.Binder; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 86 | import android.os.Build; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 87 | import android.os.Bundle; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 88 | import android.os.Debug; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 89 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | import android.os.Environment; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 91 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.os.FileUtils; |
| 93 | import android.os.Handler; |
| 94 | import android.os.IBinder; |
| 95 | import android.os.IPermissionController; |
| 96 | import android.os.Looper; |
| 97 | import android.os.Message; |
| 98 | import android.os.Parcel; |
| 99 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import android.os.Process; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 101 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | import android.os.RemoteException; |
| 103 | import android.os.ServiceManager; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 104 | import android.os.StrictMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | import android.os.SystemClock; |
| 106 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.util.EventLog; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 109 | import android.util.Pair; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 110 | import android.util.Slog; |
Joe Onorato | 5d3bea6 | 2010-03-01 13:44:29 -0800 | [diff] [blame] | 111 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | import android.util.PrintWriterPrinter; |
| 113 | import android.util.SparseArray; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 114 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import android.view.Gravity; |
| 116 | import android.view.LayoutInflater; |
| 117 | import android.view.View; |
| 118 | import android.view.WindowManager; |
| 119 | import android.view.WindowManagerPolicy; |
| 120 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 121 | import java.io.BufferedInputStream; |
| 122 | import java.io.BufferedOutputStream; |
| 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 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 185 | private static final String SYSTEM_DEBUGGABLE = "ro.debuggable"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | // Maximum number of recent tasks that we can remember. |
| 188 | static final int MAX_RECENT_TASKS = 20; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 189 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 190 | // Amount of time after a call to stopAppSwitches() during which we will |
| 191 | // prevent further untrusted switches from happening. |
| 192 | static final long APP_SWITCH_DELAY_TIME = 5*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | |
| 194 | // How long we wait for a launched process to attach to the activity manager |
| 195 | // before we decide it's never going to come up for real. |
| 196 | static final int PROC_START_TIMEOUT = 10*1000; |
| 197 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 198 | // How long we wait for a launched process to attach to the activity manager |
| 199 | // before we decide it's never going to come up for real, when the process was |
| 200 | // started with a wrapper for instrumentation (such as Valgrind) because it |
| 201 | // could take much longer than usual. |
| 202 | static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000; |
| 203 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | // How long to wait after going idle before forcing apps to GC. |
| 205 | static final int GC_TIMEOUT = 5*1000; |
| 206 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 207 | // The minimum amount of time between successive GC requests for a process. |
| 208 | static final int GC_MIN_INTERVAL = 60*1000; |
| 209 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 210 | // The rate at which we check for apps using excessive power -- 15 mins. |
| 211 | static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000; |
| 212 | |
| 213 | // The minimum sample duration we will allow before deciding we have |
| 214 | // enough data on wake locks to start killing things. |
| 215 | static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
| 216 | |
| 217 | // The minimum sample duration we will allow before deciding we have |
| 218 | // enough data on CPU usage to start killing things. |
| 219 | 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] | 220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | // How long we allow a receiver to run before giving up on it. |
| 222 | static final int BROADCAST_TIMEOUT = 10*1000; |
| 223 | |
| 224 | // How long we wait for a service to finish executing. |
| 225 | static final int SERVICE_TIMEOUT = 20*1000; |
| 226 | |
| 227 | // How long a service needs to be running until restarting its process |
| 228 | // is no longer considered to be a relaunch of the service. |
| 229 | static final int SERVICE_RESTART_DURATION = 5*1000; |
| 230 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 231 | // How long a service needs to be running until it will start back at |
| 232 | // SERVICE_RESTART_DURATION after being killed. |
| 233 | static final int SERVICE_RESET_RUN_DURATION = 60*1000; |
| 234 | |
| 235 | // Multiplying factor to increase restart duration time by, for each time |
| 236 | // a service is killed before it has run for SERVICE_RESET_RUN_DURATION. |
| 237 | static final int SERVICE_RESTART_DURATION_FACTOR = 4; |
| 238 | |
| 239 | // The minimum amount of time between restarting services that we allow. |
| 240 | // That is, when multiple services are restarting, we won't allow each |
| 241 | // to restart less than this amount of time from the last one. |
| 242 | static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000; |
| 243 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | // Maximum amount of time for there to be no activity on a service before |
| 245 | // we consider it non-essential and allow its process to go on the |
| 246 | // LRU background list. |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 247 | static final int MAX_SERVICE_INACTIVITY = 30*60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | |
| 249 | // How long we wait until we timeout on key dispatching. |
| 250 | static final int KEY_DISPATCHING_TIMEOUT = 5*1000; |
| 251 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | // How long we wait until we timeout on key dispatching during instrumentation. |
| 253 | static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000; |
| 254 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 255 | static final int MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | |
| 257 | static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 258 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 259 | public ActivityStack mMainStack; |
| 260 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 262 | * Description of a request to start a new activity, which has been held |
| 263 | * due to app switches being disabled. |
| 264 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 265 | static class PendingActivityLaunch { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 266 | ActivityRecord r; |
| 267 | ActivityRecord sourceRecord; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 268 | Uri[] grantedUriPermissions; |
| 269 | int grantedMode; |
| 270 | boolean onlyIfNeeded; |
| 271 | } |
| 272 | |
| 273 | final ArrayList<PendingActivityLaunch> mPendingActivityLaunches |
| 274 | = new ArrayList<PendingActivityLaunch>(); |
| 275 | |
| 276 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | * List of all active broadcasts that are to be executed immediately |
| 278 | * (without waiting for another broadcast to finish). Currently this only |
| 279 | * contains broadcasts to registered receivers, to avoid spinning up |
| 280 | * a bunch of processes to execute IntentReceiver components. |
| 281 | */ |
| 282 | final ArrayList<BroadcastRecord> mParallelBroadcasts |
| 283 | = new ArrayList<BroadcastRecord>(); |
| 284 | |
| 285 | /** |
| 286 | * List of all active broadcasts that are to be executed one at a time. |
| 287 | * The object at the top of the list is the currently activity broadcasts; |
| 288 | * those after it are waiting for the top to finish.. |
| 289 | */ |
| 290 | final ArrayList<BroadcastRecord> mOrderedBroadcasts |
| 291 | = new ArrayList<BroadcastRecord>(); |
| 292 | |
| 293 | /** |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 294 | * Historical data of past broadcasts, for debugging. |
| 295 | */ |
| 296 | static final int MAX_BROADCAST_HISTORY = 100; |
| 297 | final BroadcastRecord[] mBroadcastHistory |
| 298 | = new BroadcastRecord[MAX_BROADCAST_HISTORY]; |
| 299 | |
| 300 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | * Set when we current have a BROADCAST_INTENT_MSG in flight. |
| 302 | */ |
| 303 | boolean mBroadcastsScheduled = false; |
| 304 | |
| 305 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | * Activity we have told the window manager to have key focus. |
| 307 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 308 | ActivityRecord mFocusedActivity = null; |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 309 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | * List of intents that were used to start the most recent tasks. |
| 311 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 312 | final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | |
| 314 | /** |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 315 | * Process management. |
| 316 | */ |
| 317 | final ProcessList mProcessList = new ProcessList(); |
| 318 | |
| 319 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | * All of the applications we currently have running organized by name. |
| 321 | * The keys are strings of the application package name (as |
| 322 | * returned by the package manager), and the keys are ApplicationRecord |
| 323 | * objects. |
| 324 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 325 | final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | |
| 327 | /** |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 328 | * The currently running heavy-weight process, if any. |
| 329 | */ |
| 330 | ProcessRecord mHeavyWeightProcess = null; |
| 331 | |
| 332 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | * The last time that various processes have crashed. |
| 334 | */ |
| 335 | final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>(); |
| 336 | |
| 337 | /** |
| 338 | * Set of applications that we consider to be bad, and will reject |
| 339 | * incoming broadcasts from (which the user has no control over). |
| 340 | * Processes are added to this set when they have crashed twice within |
| 341 | * a minimum amount of time; they are removed from it when they are |
| 342 | * later restarted (hopefully due to some user action). The value is the |
| 343 | * time it was added to the list. |
| 344 | */ |
| 345 | final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>(); |
| 346 | |
| 347 | /** |
| 348 | * All of the processes we currently have running organized by pid. |
| 349 | * The keys are the pid running the application. |
| 350 | * |
| 351 | * <p>NOTE: This object is protected by its own lock, NOT the global |
| 352 | * activity manager lock! |
| 353 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 354 | final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | |
| 356 | /** |
| 357 | * All of the processes that have been forced to be foreground. The key |
| 358 | * is the pid of the caller who requested it (we hold a death |
| 359 | * link on it). |
| 360 | */ |
| 361 | abstract class ForegroundToken implements IBinder.DeathRecipient { |
| 362 | int pid; |
| 363 | IBinder token; |
| 364 | } |
| 365 | final SparseArray<ForegroundToken> mForegroundProcesses |
| 366 | = new SparseArray<ForegroundToken>(); |
| 367 | |
| 368 | /** |
| 369 | * List of records for processes that someone had tried to start before the |
| 370 | * system was ready. We don't start them at that point, but ensure they |
| 371 | * are started by the time booting is complete. |
| 372 | */ |
| 373 | final ArrayList<ProcessRecord> mProcessesOnHold |
| 374 | = new ArrayList<ProcessRecord>(); |
| 375 | |
| 376 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | * List of persistent applications that are in the process |
| 378 | * of being started. |
| 379 | */ |
| 380 | final ArrayList<ProcessRecord> mPersistentStartingProcesses |
| 381 | = new ArrayList<ProcessRecord>(); |
| 382 | |
| 383 | /** |
| 384 | * Processes that are being forcibly torn down. |
| 385 | */ |
| 386 | final ArrayList<ProcessRecord> mRemovedProcesses |
| 387 | = new ArrayList<ProcessRecord>(); |
| 388 | |
| 389 | /** |
| 390 | * List of running applications, sorted by recent usage. |
| 391 | * The first entry in the list is the least recently used. |
| 392 | * It contains ApplicationRecord objects. This list does NOT include |
| 393 | * any persistent application records (since we never want to exit them). |
| 394 | */ |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 395 | final ArrayList<ProcessRecord> mLruProcesses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | = new ArrayList<ProcessRecord>(); |
| 397 | |
| 398 | /** |
| 399 | * List of processes that should gc as soon as things are idle. |
| 400 | */ |
| 401 | final ArrayList<ProcessRecord> mProcessesToGc |
| 402 | = new ArrayList<ProcessRecord>(); |
| 403 | |
| 404 | /** |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 405 | * This is the process holding what we currently consider to be |
| 406 | * the "home" activity. |
| 407 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 408 | ProcessRecord mHomeProcess; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 409 | |
| 410 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 411 | * Packages that the user has asked to have run in screen size |
| 412 | * compatibility mode instead of filling the screen. |
| 413 | */ |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 414 | final CompatModePackages mCompatModePackages; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 415 | |
| 416 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | * Set of PendingResultRecord objects that are currently active. |
| 418 | */ |
| 419 | final HashSet mPendingResultRecords = new HashSet(); |
| 420 | |
| 421 | /** |
| 422 | * Set of IntentSenderRecord objects that are currently active. |
| 423 | */ |
| 424 | final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords |
| 425 | = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>(); |
| 426 | |
| 427 | /** |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 428 | * Fingerprints (hashCode()) of stack traces that we've |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 429 | * already logged DropBox entries for. Guarded by itself. If |
| 430 | * something (rogue user app) forces this over |
| 431 | * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared. |
| 432 | */ |
| 433 | private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>(); |
| 434 | private static final int MAX_DUP_SUPPRESSED_STACKS = 5000; |
| 435 | |
| 436 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 437 | * Strict Mode background batched logging state. |
| 438 | * |
| 439 | * The string buffer is guarded by itself, and its lock is also |
| 440 | * used to determine if another batched write is already |
| 441 | * in-flight. |
| 442 | */ |
| 443 | private final StringBuilder mStrictModeBuffer = new StringBuilder(); |
| 444 | |
| 445 | /** |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 446 | * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler. |
| 447 | */ |
| 448 | private boolean mPendingBroadcastTimeoutMessage; |
| 449 | |
| 450 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | * Intent broadcast that we have tried to start, but are |
| 452 | * waiting for its application's process to be created. We only |
| 453 | * need one (instead of a list) because we always process broadcasts |
| 454 | * one at a time, so no others can be started while waiting for this |
| 455 | * one. |
| 456 | */ |
| 457 | BroadcastRecord mPendingBroadcast = null; |
| 458 | |
| 459 | /** |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 460 | * The receiver index that is pending, to restart the broadcast if needed. |
| 461 | */ |
| 462 | int mPendingBroadcastRecvIndex; |
| 463 | |
| 464 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | * Keeps track of all IIntentReceivers that have been registered for |
| 466 | * broadcasts. Hash keys are the receiver IBinder, hash value is |
| 467 | * a ReceiverList. |
| 468 | */ |
| 469 | final HashMap mRegisteredReceivers = new HashMap(); |
| 470 | |
| 471 | /** |
| 472 | * Resolver for broadcast intents to registered receivers. |
| 473 | * Holds BroadcastFilter (subclass of IntentFilter). |
| 474 | */ |
| 475 | final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver |
| 476 | = new IntentResolver<BroadcastFilter, BroadcastFilter>() { |
| 477 | @Override |
| 478 | protected boolean allowFilterResult( |
| 479 | BroadcastFilter filter, List<BroadcastFilter> dest) { |
| 480 | IBinder target = filter.receiverList.receiver.asBinder(); |
| 481 | for (int i=dest.size()-1; i>=0; i--) { |
| 482 | if (dest.get(i).receiverList.receiver.asBinder() == target) { |
| 483 | return false; |
| 484 | } |
| 485 | } |
| 486 | return true; |
| 487 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 488 | |
| 489 | @Override |
| 490 | protected String packageForFilter(BroadcastFilter filter) { |
| 491 | return filter.packageName; |
| 492 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | }; |
| 494 | |
| 495 | /** |
| 496 | * State of all active sticky broadcasts. Keys are the action of the |
| 497 | * sticky Intent, values are an ArrayList of all broadcasted intents with |
| 498 | * that action (which should usually be one). |
| 499 | */ |
| 500 | final HashMap<String, ArrayList<Intent>> mStickyBroadcasts = |
| 501 | new HashMap<String, ArrayList<Intent>>(); |
| 502 | |
| 503 | /** |
| 504 | * All currently running services. |
| 505 | */ |
| 506 | final HashMap<ComponentName, ServiceRecord> mServices = |
| 507 | new HashMap<ComponentName, ServiceRecord>(); |
| 508 | |
| 509 | /** |
| 510 | * All currently running services indexed by the Intent used to start them. |
| 511 | */ |
| 512 | final HashMap<Intent.FilterComparison, ServiceRecord> mServicesByIntent = |
| 513 | new HashMap<Intent.FilterComparison, ServiceRecord>(); |
| 514 | |
| 515 | /** |
| 516 | * All currently bound service connections. Keys are the IBinder of |
| 517 | * the client's IServiceConnection. |
| 518 | */ |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 519 | final HashMap<IBinder, ArrayList<ConnectionRecord>> mServiceConnections |
| 520 | = new HashMap<IBinder, ArrayList<ConnectionRecord>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | |
| 522 | /** |
| 523 | * List of services that we have been asked to start, |
| 524 | * but haven't yet been able to. It is used to hold start requests |
| 525 | * while waiting for their corresponding application thread to get |
| 526 | * going. |
| 527 | */ |
| 528 | final ArrayList<ServiceRecord> mPendingServices |
| 529 | = new ArrayList<ServiceRecord>(); |
| 530 | |
| 531 | /** |
| 532 | * List of services that are scheduled to restart following a crash. |
| 533 | */ |
| 534 | final ArrayList<ServiceRecord> mRestartingServices |
| 535 | = new ArrayList<ServiceRecord>(); |
| 536 | |
| 537 | /** |
| 538 | * List of services that are in the process of being stopped. |
| 539 | */ |
| 540 | final ArrayList<ServiceRecord> mStoppingServices |
| 541 | = new ArrayList<ServiceRecord>(); |
| 542 | |
| 543 | /** |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 544 | * Backup/restore process management |
| 545 | */ |
| 546 | String mBackupAppName = null; |
| 547 | BackupRecord mBackupTarget = null; |
| 548 | |
| 549 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | * List of PendingThumbnailsRecord objects of clients who are still |
| 551 | * waiting to receive all of the thumbnails for a task. |
| 552 | */ |
| 553 | final ArrayList mPendingThumbnails = new ArrayList(); |
| 554 | |
| 555 | /** |
| 556 | * List of HistoryRecord objects that have been finished and must |
| 557 | * still report back to a pending thumbnail receiver. |
| 558 | */ |
| 559 | final ArrayList mCancelledThumbnails = new ArrayList(); |
| 560 | |
| 561 | /** |
| 562 | * All of the currently running global content providers. Keys are a |
| 563 | * string containing the provider name and values are a |
| 564 | * ContentProviderRecord object containing the data about it. Note |
| 565 | * that a single provider may be published under multiple names, so |
| 566 | * there may be multiple entries here for a single one in mProvidersByClass. |
| 567 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 568 | final HashMap<String, ContentProviderRecord> mProvidersByName |
| 569 | = new HashMap<String, ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | |
| 571 | /** |
| 572 | * All of the currently running global content providers. Keys are a |
| 573 | * string containing the provider's implementation class and values are a |
| 574 | * ContentProviderRecord object containing the data about it. |
| 575 | */ |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 576 | final HashMap<ComponentName, ContentProviderRecord> mProvidersByClass |
| 577 | = new HashMap<ComponentName, ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | |
| 579 | /** |
| 580 | * List of content providers who have clients waiting for them. The |
| 581 | * application is currently being launched and the provider will be |
| 582 | * removed from this list once it is published. |
| 583 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 584 | final ArrayList<ContentProviderRecord> mLaunchingProviders |
| 585 | = new ArrayList<ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | |
| 587 | /** |
| 588 | * Global set of specific Uri permissions that have been granted. |
| 589 | */ |
| 590 | final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions |
| 591 | = new SparseArray<HashMap<Uri, UriPermission>>(); |
| 592 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 593 | CoreSettingsObserver mCoreSettingsObserver; |
| 594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | /** |
| 596 | * Thread-local storage used to carry caller permissions over through |
| 597 | * indirect content-provider access. |
| 598 | * @see #ActivityManagerService.openContentUri() |
| 599 | */ |
| 600 | private class Identity { |
| 601 | public int pid; |
| 602 | public int uid; |
| 603 | |
| 604 | Identity(int _pid, int _uid) { |
| 605 | pid = _pid; |
| 606 | uid = _uid; |
| 607 | } |
| 608 | } |
| 609 | private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>(); |
| 610 | |
| 611 | /** |
| 612 | * All information we have collected about the runtime performance of |
| 613 | * any user id that can impact battery performance. |
| 614 | */ |
| 615 | final BatteryStatsService mBatteryStatsService; |
| 616 | |
| 617 | /** |
| 618 | * information about component usage |
| 619 | */ |
| 620 | final UsageStatsService mUsageStatsService; |
| 621 | |
| 622 | /** |
| 623 | * Current configuration information. HistoryRecord objects are given |
| 624 | * a reference to this object to indicate which configuration they are |
| 625 | * currently running in, so this object must be kept immutable. |
| 626 | */ |
| 627 | Configuration mConfiguration = new Configuration(); |
| 628 | |
| 629 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 630 | * Current sequencing integer of the configuration, for skipping old |
| 631 | * configurations. |
| 632 | */ |
| 633 | int mConfigurationSeq = 0; |
| 634 | |
| 635 | /** |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 636 | * Hardware-reported OpenGLES version. |
| 637 | */ |
| 638 | final int GL_ES_VERSION; |
| 639 | |
| 640 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | * List of initialization arguments to pass to all processes when binding applications to them. |
| 642 | * For example, references to the commonly used services. |
| 643 | */ |
| 644 | HashMap<String, IBinder> mAppBindArgs; |
| 645 | |
| 646 | /** |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 647 | * Temporary to avoid allocations. Protected by main lock. |
| 648 | */ |
| 649 | final StringBuilder mStringBuilder = new StringBuilder(256); |
| 650 | |
| 651 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 652 | * Used to control how we initialize the service. |
| 653 | */ |
| 654 | boolean mStartRunning = false; |
| 655 | ComponentName mTopComponent; |
| 656 | String mTopAction; |
| 657 | String mTopData; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 658 | boolean mProcessesReady = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | boolean mSystemReady = false; |
| 660 | boolean mBooting = false; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 661 | boolean mWaitingUpdate = false; |
| 662 | boolean mDidUpdate = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 663 | boolean mOnBattery = false; |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 664 | boolean mLaunchWarningShown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | |
| 666 | Context mContext; |
| 667 | |
| 668 | int mFactoryTest; |
| 669 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 670 | boolean mCheckedForSetup; |
| 671 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 673 | * The time at which we will allow normal application switches again, |
| 674 | * after a call to {@link #stopAppSwitches()}. |
| 675 | */ |
| 676 | long mAppSwitchesAllowedTime; |
| 677 | |
| 678 | /** |
| 679 | * This is set to true after the first switch after mAppSwitchesAllowedTime |
| 680 | * is set; any switches after that will clear the time. |
| 681 | */ |
| 682 | boolean mDidAppSwitch; |
| 683 | |
| 684 | /** |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 685 | * Last time (in realtime) at which we checked for power usage. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 686 | */ |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 687 | long mLastPowerCheckRealtime; |
| 688 | |
| 689 | /** |
| 690 | * Last time (in uptime) at which we checked for power usage. |
| 691 | */ |
| 692 | long mLastPowerCheckUptime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 693 | |
| 694 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | * Set while we are wanting to sleep, to prevent any |
| 696 | * activities from being started/resumed. |
| 697 | */ |
| 698 | boolean mSleeping = false; |
| 699 | |
| 700 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 701 | * Set if we are shutting down the system, similar to sleeping. |
| 702 | */ |
| 703 | boolean mShuttingDown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | |
| 705 | /** |
| 706 | * Task identifier that activities are currently being started |
| 707 | * in. Incremented each time a new task is created. |
| 708 | * todo: Replace this with a TokenSpace class that generates non-repeating |
| 709 | * integers that won't wrap. |
| 710 | */ |
| 711 | int mCurTask = 1; |
| 712 | |
| 713 | /** |
| 714 | * Current sequence id for oom_adj computation traversal. |
| 715 | */ |
| 716 | int mAdjSeq = 0; |
| 717 | |
| 718 | /** |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 719 | * Current sequence id for process LRU updating. |
| 720 | */ |
| 721 | int mLruSeq = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | |
| 723 | /** |
| 724 | * System monitoring: number of processes that died since the last |
| 725 | * N procs were started. |
| 726 | */ |
| 727 | int[] mProcDeaths = new int[20]; |
| 728 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 729 | /** |
| 730 | * This is set if we had to do a delayed dexopt of an app before launching |
| 731 | * it, to increasing the ANR timeouts in that case. |
| 732 | */ |
| 733 | boolean mDidDexOpt; |
| 734 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | String mDebugApp = null; |
| 736 | boolean mWaitForDebugger = false; |
| 737 | boolean mDebugTransient = false; |
| 738 | String mOrigDebugApp = null; |
| 739 | boolean mOrigWaitForDebugger = false; |
| 740 | boolean mAlwaysFinishActivities = false; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 741 | IActivityController mController = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 742 | String mProfileApp = null; |
| 743 | ProcessRecord mProfileProc = null; |
| 744 | String mProfileFile; |
| 745 | ParcelFileDescriptor mProfileFd; |
| 746 | int mProfileType = 0; |
| 747 | boolean mAutoStopProfiler = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 749 | final RemoteCallbackList<IActivityWatcher> mWatchers |
| 750 | = new RemoteCallbackList<IActivityWatcher>(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 751 | |
| 752 | final RemoteCallbackList<IProcessObserver> mProcessObservers |
| 753 | = new RemoteCallbackList<IProcessObserver>(); |
| 754 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | /** |
| 756 | * Callback of last caller to {@link #requestPss}. |
| 757 | */ |
| 758 | Runnable mRequestPssCallback; |
| 759 | |
| 760 | /** |
| 761 | * Remaining processes for which we are waiting results from the last |
| 762 | * call to {@link #requestPss}. |
| 763 | */ |
| 764 | final ArrayList<ProcessRecord> mRequestPssList |
| 765 | = new ArrayList<ProcessRecord>(); |
| 766 | |
| 767 | /** |
| 768 | * Runtime statistics collection thread. This object's lock is used to |
| 769 | * protect all related state. |
| 770 | */ |
| 771 | final Thread mProcessStatsThread; |
| 772 | |
| 773 | /** |
| 774 | * Used to collect process stats when showing not responding dialog. |
| 775 | * Protected by mProcessStatsThread. |
| 776 | */ |
| 777 | final ProcessStats mProcessStats = new ProcessStats( |
| 778 | MONITOR_THREAD_CPU_USAGE); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 779 | final AtomicLong mLastCpuTime = new AtomicLong(0); |
| 780 | final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true); |
| 781 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 782 | long mLastWriteTime = 0; |
| 783 | |
| 784 | /** |
| 785 | * Set to true after the system has finished booting. |
| 786 | */ |
| 787 | boolean mBooted = false; |
| 788 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 789 | int mProcessLimit = ProcessList.MAX_HIDDEN_APPS; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 790 | int mProcessLimitOverride = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | |
| 792 | WindowManagerService mWindowManager; |
| 793 | |
| 794 | static ActivityManagerService mSelf; |
| 795 | static ActivityThread mSystemThread; |
| 796 | |
| 797 | private final class AppDeathRecipient implements IBinder.DeathRecipient { |
| 798 | final ProcessRecord mApp; |
| 799 | final int mPid; |
| 800 | final IApplicationThread mAppThread; |
| 801 | |
| 802 | AppDeathRecipient(ProcessRecord app, int pid, |
| 803 | IApplicationThread thread) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 804 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | TAG, "New death recipient " + this |
| 806 | + " for thread " + thread.asBinder()); |
| 807 | mApp = app; |
| 808 | mPid = pid; |
| 809 | mAppThread = thread; |
| 810 | } |
| 811 | |
| 812 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 813 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | TAG, "Death received in " + this |
| 815 | + " for thread " + mAppThread.asBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | synchronized(ActivityManagerService.this) { |
| 817 | appDiedLocked(mApp, mPid, mAppThread); |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | static final int SHOW_ERROR_MSG = 1; |
| 823 | static final int SHOW_NOT_RESPONDING_MSG = 2; |
| 824 | static final int SHOW_FACTORY_ERROR_MSG = 3; |
| 825 | static final int UPDATE_CONFIGURATION_MSG = 4; |
| 826 | static final int GC_BACKGROUND_PROCESSES_MSG = 5; |
| 827 | static final int WAIT_FOR_DEBUGGER_MSG = 6; |
| 828 | static final int BROADCAST_INTENT_MSG = 7; |
| 829 | static final int BROADCAST_TIMEOUT_MSG = 8; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 830 | static final int SERVICE_TIMEOUT_MSG = 12; |
| 831 | static final int UPDATE_TIME_ZONE = 13; |
| 832 | static final int SHOW_UID_ERROR_MSG = 14; |
| 833 | static final int IM_FEELING_LUCKY_MSG = 15; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | static final int PROC_START_TIMEOUT_MSG = 20; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 835 | static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 836 | static final int KILL_APPLICATION_MSG = 22; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 837 | static final int FINALIZE_PENDING_INTENT_MSG = 23; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 838 | static final int POST_HEAVY_NOTIFICATION_MSG = 24; |
| 839 | static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 840 | static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 841 | static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 842 | static final int CLEAR_DNS_CACHE = 28; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 843 | static final int UPDATE_HTTP_PROXY = 29; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 844 | static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30; |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 845 | static final int DISPATCH_FOREGROUND_ACTIVITIES_CHANGED = 31; |
| 846 | static final int DISPATCH_PROCESS_DIED = 32; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | |
| 848 | AlertDialog mUidAlert; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 849 | CompatModeDialog mCompatModeDialog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | |
| 851 | final Handler mHandler = new Handler() { |
| 852 | //public Handler() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 853 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | //} |
| 855 | |
| 856 | public void handleMessage(Message msg) { |
| 857 | switch (msg.what) { |
| 858 | case SHOW_ERROR_MSG: { |
| 859 | HashMap data = (HashMap) msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 860 | synchronized (ActivityManagerService.this) { |
| 861 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 862 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 863 | Slog.e(TAG, "App already has crash dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | return; |
| 865 | } |
| 866 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 867 | if (!mSleeping && !mShuttingDown) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 868 | Dialog d = new AppErrorDialog(mContext, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | d.show(); |
| 870 | proc.crashDialog = d; |
| 871 | } else { |
| 872 | // The device is asleep, so just pretend that the user |
| 873 | // saw a crash dialog and hit "force quit". |
| 874 | res.set(0); |
| 875 | } |
| 876 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 877 | |
| 878 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | } break; |
| 880 | case SHOW_NOT_RESPONDING_MSG: { |
| 881 | synchronized (ActivityManagerService.this) { |
| 882 | HashMap data = (HashMap) msg.obj; |
| 883 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 884 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 885 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | return; |
| 887 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 888 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 889 | Intent intent = new Intent("android.intent.action.ANR"); |
| 890 | if (!mProcessesReady) { |
| 891 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 892 | } |
| 893 | broadcastIntentLocked(null, null, intent, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 894 | null, null, 0, null, null, null, |
| 895 | false, false, MY_PID, Process.SYSTEM_UID); |
| 896 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 898 | mContext, proc, (ActivityRecord)data.get("activity")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | d.show(); |
| 900 | proc.anrDialog = d; |
| 901 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 902 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 903 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 905 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 906 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 907 | synchronized (ActivityManagerService.this) { |
| 908 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 909 | if (proc == null) { |
| 910 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 911 | break; |
| 912 | } |
| 913 | if (proc.crashDialog != null) { |
| 914 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 915 | return; |
| 916 | } |
| 917 | AppErrorResult res = (AppErrorResult) data.get("result"); |
| 918 | if (!mSleeping && !mShuttingDown) { |
| 919 | Dialog d = new StrictModeViolationDialog(mContext, res, proc); |
| 920 | d.show(); |
| 921 | proc.crashDialog = d; |
| 922 | } else { |
| 923 | // The device is asleep, so just pretend that the user |
| 924 | // saw a crash dialog and hit "force quit". |
| 925 | res.set(0); |
| 926 | } |
| 927 | } |
| 928 | ensureBootCompleted(); |
| 929 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | case SHOW_FACTORY_ERROR_MSG: { |
| 931 | Dialog d = new FactoryErrorDialog( |
| 932 | mContext, msg.getData().getCharSequence("msg")); |
| 933 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 934 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 935 | } break; |
| 936 | case UPDATE_CONFIGURATION_MSG: { |
| 937 | final ContentResolver resolver = mContext.getContentResolver(); |
| 938 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 939 | } break; |
| 940 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 941 | synchronized (ActivityManagerService.this) { |
| 942 | performAppGcsIfAppropriateLocked(); |
| 943 | } |
| 944 | } break; |
| 945 | case WAIT_FOR_DEBUGGER_MSG: { |
| 946 | synchronized (ActivityManagerService.this) { |
| 947 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 948 | if (msg.arg1 != 0) { |
| 949 | if (!app.waitedForDebugger) { |
| 950 | Dialog d = new AppWaitingForDebuggerDialog( |
| 951 | ActivityManagerService.this, |
| 952 | mContext, app); |
| 953 | app.waitDialog = d; |
| 954 | app.waitedForDebugger = true; |
| 955 | d.show(); |
| 956 | } |
| 957 | } else { |
| 958 | if (app.waitDialog != null) { |
| 959 | app.waitDialog.dismiss(); |
| 960 | app.waitDialog = null; |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | } break; |
| 965 | case BROADCAST_INTENT_MSG: { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 966 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | TAG, "Received BROADCAST_INTENT_MSG"); |
| 968 | processNextBroadcast(true); |
| 969 | } break; |
| 970 | case BROADCAST_TIMEOUT_MSG: { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 971 | synchronized (ActivityManagerService.this) { |
| 972 | broadcastTimeoutLocked(true); |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 973 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 974 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 976 | if (mDidDexOpt) { |
| 977 | mDidDexOpt = false; |
| 978 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 979 | nmsg.obj = msg.obj; |
| 980 | mHandler.sendMessageDelayed(nmsg, SERVICE_TIMEOUT); |
| 981 | return; |
| 982 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | serviceTimeout((ProcessRecord)msg.obj); |
| 984 | } break; |
| 985 | case UPDATE_TIME_ZONE: { |
| 986 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 987 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 988 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | if (r.thread != null) { |
| 990 | try { |
| 991 | r.thread.updateTimeZone(); |
| 992 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 993 | 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] | 994 | } |
| 995 | } |
| 996 | } |
| 997 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 998 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 999 | case CLEAR_DNS_CACHE: { |
| 1000 | synchronized (ActivityManagerService.this) { |
| 1001 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1002 | ProcessRecord r = mLruProcesses.get(i); |
| 1003 | if (r.thread != null) { |
| 1004 | try { |
| 1005 | r.thread.clearDnsCache(); |
| 1006 | } catch (RemoteException ex) { |
| 1007 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1013 | case UPDATE_HTTP_PROXY: { |
| 1014 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 1015 | String host = ""; |
| 1016 | String port = ""; |
| 1017 | String exclList = ""; |
| 1018 | if (proxy != null) { |
| 1019 | host = proxy.getHost(); |
| 1020 | port = Integer.toString(proxy.getPort()); |
| 1021 | exclList = proxy.getExclusionList(); |
| 1022 | } |
| 1023 | synchronized (ActivityManagerService.this) { |
| 1024 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1025 | ProcessRecord r = mLruProcesses.get(i); |
| 1026 | if (r.thread != null) { |
| 1027 | try { |
| 1028 | r.thread.setHttpProxy(host, port, exclList); |
| 1029 | } catch (RemoteException ex) { |
| 1030 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1031 | r.info.processName); |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1037 | case SHOW_UID_ERROR_MSG: { |
| 1038 | // XXX This is a temporary dialog, no need to localize. |
| 1039 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1040 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1041 | d.setCancelable(false); |
| 1042 | d.setTitle("System UIDs Inconsistent"); |
| 1043 | 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] | 1044 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1046 | mUidAlert = d; |
| 1047 | d.show(); |
| 1048 | } break; |
| 1049 | case IM_FEELING_LUCKY_MSG: { |
| 1050 | if (mUidAlert != null) { |
| 1051 | mUidAlert.dismiss(); |
| 1052 | mUidAlert = null; |
| 1053 | } |
| 1054 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1056 | if (mDidDexOpt) { |
| 1057 | mDidDexOpt = false; |
| 1058 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1059 | nmsg.obj = msg.obj; |
| 1060 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1061 | return; |
| 1062 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1064 | synchronized (ActivityManagerService.this) { |
| 1065 | processStartTimedOutLocked(app); |
| 1066 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1067 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1068 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1069 | synchronized (ActivityManagerService.this) { |
| 1070 | doPendingActivityLaunchesLocked(true); |
| 1071 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1072 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1073 | case KILL_APPLICATION_MSG: { |
| 1074 | synchronized (ActivityManagerService.this) { |
| 1075 | int uid = msg.arg1; |
| 1076 | boolean restart = (msg.arg2 == 1); |
| 1077 | String pkg = (String) msg.obj; |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 1078 | forceStopPackageLocked(pkg, uid, restart, false, true, false); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1081 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1082 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1083 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1084 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1085 | INotificationManager inm = NotificationManager.getService(); |
| 1086 | if (inm == null) { |
| 1087 | return; |
| 1088 | } |
| 1089 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1090 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1091 | ProcessRecord process = root.app; |
| 1092 | if (process == null) { |
| 1093 | return; |
| 1094 | } |
| 1095 | |
| 1096 | try { |
| 1097 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1098 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1099 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1100 | Notification notification = new Notification(); |
| 1101 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1102 | notification.when = 0; |
| 1103 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1104 | notification.tickerText = text; |
| 1105 | notification.defaults = 0; // please be quiet |
| 1106 | notification.sound = null; |
| 1107 | notification.vibrate = null; |
| 1108 | notification.setLatestEventInfo(context, text, |
| 1109 | mContext.getText(R.string.heavy_weight_notification_detail), |
| 1110 | PendingIntent.getActivity(mContext, 0, root.intent, |
| 1111 | PendingIntent.FLAG_CANCEL_CURRENT)); |
| 1112 | |
| 1113 | try { |
| 1114 | int[] outId = new int[1]; |
| 1115 | inm.enqueueNotification("android", R.string.heavy_weight_notification, |
| 1116 | notification, outId); |
| 1117 | } catch (RuntimeException e) { |
| 1118 | Slog.w(ActivityManagerService.TAG, |
| 1119 | "Error showing notification for heavy-weight app", e); |
| 1120 | } catch (RemoteException e) { |
| 1121 | } |
| 1122 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1123 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1124 | } |
| 1125 | } break; |
| 1126 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1127 | INotificationManager inm = NotificationManager.getService(); |
| 1128 | if (inm == null) { |
| 1129 | return; |
| 1130 | } |
| 1131 | try { |
| 1132 | inm.cancelNotification("android", |
| 1133 | R.string.heavy_weight_notification); |
| 1134 | } catch (RuntimeException e) { |
| 1135 | Slog.w(ActivityManagerService.TAG, |
| 1136 | "Error canceling notification for service", e); |
| 1137 | } catch (RemoteException e) { |
| 1138 | } |
| 1139 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1140 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1141 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1142 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1143 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1144 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1145 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1146 | } |
| 1147 | } break; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1148 | case SHOW_COMPAT_MODE_DIALOG_MSG: { |
| 1149 | synchronized (ActivityManagerService.this) { |
| 1150 | ActivityRecord ar = (ActivityRecord)msg.obj; |
| 1151 | if (mCompatModeDialog != null) { |
| 1152 | if (mCompatModeDialog.mAppInfo.packageName.equals( |
| 1153 | ar.info.applicationInfo.packageName)) { |
| 1154 | return; |
| 1155 | } |
| 1156 | mCompatModeDialog.dismiss(); |
| 1157 | mCompatModeDialog = null; |
| 1158 | } |
Dianne Hackborn | 2947826 | 2011-06-07 15:44:22 -0700 | [diff] [blame] | 1159 | if (ar != null && false) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1160 | if (mCompatModePackages.getPackageAskCompatModeLocked( |
| 1161 | ar.packageName)) { |
| 1162 | int mode = mCompatModePackages.computeCompatModeLocked( |
| 1163 | ar.info.applicationInfo); |
| 1164 | if (mode == ActivityManager.COMPAT_MODE_DISABLED |
| 1165 | || mode == ActivityManager.COMPAT_MODE_ENABLED) { |
| 1166 | mCompatModeDialog = new CompatModeDialog( |
| 1167 | ActivityManagerService.this, mContext, |
| 1168 | ar.info.applicationInfo); |
| 1169 | mCompatModeDialog.show(); |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | } |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 1174 | break; |
| 1175 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1176 | case DISPATCH_FOREGROUND_ACTIVITIES_CHANGED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1177 | final int pid = msg.arg1; |
| 1178 | final int uid = msg.arg2; |
| 1179 | final boolean foregroundActivities = (Boolean) msg.obj; |
| 1180 | dispatchForegroundActivitiesChanged(pid, uid, foregroundActivities); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1181 | break; |
| 1182 | } |
| 1183 | case DISPATCH_PROCESS_DIED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1184 | final int pid = msg.arg1; |
| 1185 | final int uid = msg.arg2; |
| 1186 | dispatchProcessDied(pid, uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1187 | break; |
| 1188 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | }; |
| 1192 | |
| 1193 | public static void setSystemProcess() { |
| 1194 | try { |
| 1195 | ActivityManagerService m = mSelf; |
| 1196 | |
| 1197 | ServiceManager.addService("activity", m); |
| 1198 | ServiceManager.addService("meminfo", new MemBinder(m)); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1199 | ServiceManager.addService("gfxinfo", new GraphicsBinder(m)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | if (MONITOR_CPU_USAGE) { |
| 1201 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1202 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1203 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1204 | |
| 1205 | ApplicationInfo info = |
| 1206 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 1207 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1208 | mSystemThread.installSystemApplicationInfo(info); |
| 1209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | synchronized (mSelf) { |
| 1211 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1212 | mSystemThread.getApplicationThread(), info, |
| 1213 | info.processName); |
| 1214 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1215 | app.pid = MY_PID; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1216 | app.maxAdj = ProcessList.SYSTEM_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | mSelf.mProcessNames.put(app.processName, app.info.uid, app); |
| 1218 | synchronized (mSelf.mPidsSelfLocked) { |
| 1219 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1220 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1221 | mSelf.updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1222 | } |
| 1223 | } catch (PackageManager.NameNotFoundException e) { |
| 1224 | throw new RuntimeException( |
| 1225 | "Unable to find android system package", e); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | public void setWindowManager(WindowManagerService wm) { |
| 1230 | mWindowManager = wm; |
| 1231 | } |
| 1232 | |
| 1233 | public static final Context main(int factoryTest) { |
| 1234 | AThread thr = new AThread(); |
| 1235 | thr.start(); |
| 1236 | |
| 1237 | synchronized (thr) { |
| 1238 | while (thr.mService == null) { |
| 1239 | try { |
| 1240 | thr.wait(); |
| 1241 | } catch (InterruptedException e) { |
| 1242 | } |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | ActivityManagerService m = thr.mService; |
| 1247 | mSelf = m; |
| 1248 | ActivityThread at = ActivityThread.systemMain(); |
| 1249 | mSystemThread = at; |
| 1250 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1251 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1252 | m.mContext = context; |
| 1253 | m.mFactoryTest = factoryTest; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1254 | m.mMainStack = new ActivityStack(m, context, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | |
| 1256 | m.mBatteryStatsService.publish(context); |
| 1257 | m.mUsageStatsService.publish(context); |
| 1258 | |
| 1259 | synchronized (thr) { |
| 1260 | thr.mReady = true; |
| 1261 | thr.notifyAll(); |
| 1262 | } |
| 1263 | |
| 1264 | m.startRunning(null, null, null, null); |
| 1265 | |
| 1266 | return context; |
| 1267 | } |
| 1268 | |
| 1269 | public static ActivityManagerService self() { |
| 1270 | return mSelf; |
| 1271 | } |
| 1272 | |
| 1273 | static class AThread extends Thread { |
| 1274 | ActivityManagerService mService; |
| 1275 | boolean mReady = false; |
| 1276 | |
| 1277 | public AThread() { |
| 1278 | super("ActivityManager"); |
| 1279 | } |
| 1280 | |
| 1281 | public void run() { |
| 1282 | Looper.prepare(); |
| 1283 | |
| 1284 | android.os.Process.setThreadPriority( |
| 1285 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1286 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | |
| 1288 | ActivityManagerService m = new ActivityManagerService(); |
| 1289 | |
| 1290 | synchronized (this) { |
| 1291 | mService = m; |
| 1292 | notifyAll(); |
| 1293 | } |
| 1294 | |
| 1295 | synchronized (this) { |
| 1296 | while (!mReady) { |
| 1297 | try { |
| 1298 | wait(); |
| 1299 | } catch (InterruptedException e) { |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1304 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1305 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1306 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1307 | } |
| 1308 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1309 | Looper.loop(); |
| 1310 | } |
| 1311 | } |
| 1312 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1313 | static class MemBinder extends Binder { |
| 1314 | ActivityManagerService mActivityManagerService; |
| 1315 | MemBinder(ActivityManagerService activityManagerService) { |
| 1316 | mActivityManagerService = activityManagerService; |
| 1317 | } |
| 1318 | |
| 1319 | @Override |
| 1320 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1321 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1322 | != PackageManager.PERMISSION_GRANTED) { |
| 1323 | pw.println("Permission Denial: can't dump meminfo from from pid=" |
| 1324 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1325 | + " without permission " + android.Manifest.permission.DUMP); |
| 1326 | return; |
| 1327 | } |
| 1328 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1329 | mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1330 | } |
| 1331 | } |
| 1332 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1333 | static class GraphicsBinder extends Binder { |
| 1334 | ActivityManagerService mActivityManagerService; |
| 1335 | GraphicsBinder(ActivityManagerService activityManagerService) { |
| 1336 | mActivityManagerService = activityManagerService; |
| 1337 | } |
| 1338 | |
| 1339 | @Override |
| 1340 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1341 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1342 | != PackageManager.PERMISSION_GRANTED) { |
| 1343 | pw.println("Permission Denial: can't dump gfxinfo from from pid=" |
| 1344 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1345 | + " without permission " + android.Manifest.permission.DUMP); |
| 1346 | return; |
| 1347 | } |
| 1348 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1349 | mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | static class CpuBinder extends Binder { |
| 1354 | ActivityManagerService mActivityManagerService; |
| 1355 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1356 | mActivityManagerService = activityManagerService; |
| 1357 | } |
| 1358 | |
| 1359 | @Override |
| 1360 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1361 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1362 | != PackageManager.PERMISSION_GRANTED) { |
| 1363 | pw.println("Permission Denial: can't dump cpuinfo from from pid=" |
| 1364 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1365 | + " without permission " + android.Manifest.permission.DUMP); |
| 1366 | return; |
| 1367 | } |
| 1368 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1369 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1370 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1371 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1372 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | private ActivityManagerService() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1378 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1379 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1380 | File dataDir = Environment.getDataDirectory(); |
| 1381 | File systemDir = new File(dataDir, "system"); |
| 1382 | systemDir.mkdirs(); |
| 1383 | mBatteryStatsService = new BatteryStatsService(new File( |
| 1384 | systemDir, "batterystats.bin").toString()); |
| 1385 | mBatteryStatsService.getActiveStatistics().readLocked(); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1386 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1387 | mOnBattery = DEBUG_POWER ? true |
| 1388 | : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1389 | mBatteryStatsService.getActiveStatistics().setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1390 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1391 | mUsageStatsService = new UsageStatsService(new File( |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1392 | systemDir, "usagestats").toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1393 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1394 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1395 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1396 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1397 | mConfiguration.setToDefaults(); |
| 1398 | mConfiguration.locale = Locale.getDefault(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1399 | mProcessStats.init(); |
| 1400 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 1401 | mCompatModePackages = new CompatModePackages(this, systemDir); |
| 1402 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1403 | // Add ourself to the Watchdog monitors. |
| 1404 | Watchdog.getInstance().addMonitor(this); |
| 1405 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1407 | public void run() { |
| 1408 | while (true) { |
| 1409 | try { |
| 1410 | try { |
| 1411 | synchronized(this) { |
| 1412 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1413 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1414 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1415 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1416 | // + ", write delay=" + nextWriteDelay); |
| 1417 | if (nextWriteDelay < nextCpuDelay) { |
| 1418 | nextCpuDelay = nextWriteDelay; |
| 1419 | } |
| 1420 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1421 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1422 | this.wait(nextCpuDelay); |
| 1423 | } |
| 1424 | } |
| 1425 | } catch (InterruptedException e) { |
| 1426 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1427 | updateCpuStatsNow(); |
| 1428 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1429 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | }; |
| 1434 | mProcessStatsThread.start(); |
| 1435 | } |
| 1436 | |
| 1437 | @Override |
| 1438 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1439 | throws RemoteException { |
| 1440 | try { |
| 1441 | return super.onTransact(code, data, reply, flags); |
| 1442 | } catch (RuntimeException e) { |
| 1443 | // The activity manager only throws security exceptions, so let's |
| 1444 | // log all others. |
| 1445 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1446 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1447 | } |
| 1448 | throw e; |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1453 | final long now = SystemClock.uptimeMillis(); |
| 1454 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1455 | return; |
| 1456 | } |
| 1457 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1458 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1459 | mProcessStatsThread.notify(); |
| 1460 | } |
| 1461 | } |
| 1462 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1463 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1464 | void updateCpuStatsNow() { |
| 1465 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1466 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1467 | final long now = SystemClock.uptimeMillis(); |
| 1468 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1469 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1470 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1471 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1472 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1473 | haveNewCpuStats = true; |
| 1474 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1475 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1476 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1477 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1478 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1479 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1480 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1481 | int user = mProcessStats.getLastUserTime(); |
| 1482 | int system = mProcessStats.getLastSystemTime(); |
| 1483 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1484 | int irq = mProcessStats.getLastIrqTime(); |
| 1485 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1486 | int idle = mProcessStats.getLastIdleTime(); |
| 1487 | |
| 1488 | int total = user + system + iowait + irq + softIrq + idle; |
| 1489 | if (total == 0) total = 1; |
| 1490 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1491 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1492 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1493 | (user * 100) / total, |
| 1494 | (system * 100) / total, |
| 1495 | (iowait * 100) / total, |
| 1496 | (irq * 100) / total, |
| 1497 | (softIrq * 100) / total); |
| 1498 | } |
| 1499 | } |
| 1500 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1501 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1502 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1503 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1504 | synchronized(mPidsSelfLocked) { |
| 1505 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1506 | if (mOnBattery) { |
| 1507 | int perc = bstats.startAddingCpuLocked(); |
| 1508 | int totalUTime = 0; |
| 1509 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1510 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1511 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1512 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1513 | if (!st.working) { |
| 1514 | continue; |
| 1515 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1516 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1517 | int otherUTime = (st.rel_utime*perc)/100; |
| 1518 | int otherSTime = (st.rel_stime*perc)/100; |
| 1519 | totalUTime += otherUTime; |
| 1520 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1521 | if (pr != null) { |
| 1522 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1523 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1524 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1525 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1526 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1527 | } else { |
| 1528 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1529 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1530 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1531 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1532 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1533 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1534 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1535 | } |
| 1536 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1537 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1538 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1539 | } |
| 1540 | } |
| 1541 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1542 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1543 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1544 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1545 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1546 | } |
| 1547 | } |
| 1548 | } |
| 1549 | } |
| 1550 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1551 | @Override |
| 1552 | public void batteryNeedsCpuUpdate() { |
| 1553 | updateCpuStatsNow(); |
| 1554 | } |
| 1555 | |
| 1556 | @Override |
| 1557 | public void batteryPowerChanged(boolean onBattery) { |
| 1558 | // When plugging in, update the CPU stats first before changing |
| 1559 | // the plug state. |
| 1560 | updateCpuStatsNow(); |
| 1561 | synchronized (this) { |
| 1562 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1563 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1564 | } |
| 1565 | } |
| 1566 | } |
| 1567 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1568 | /** |
| 1569 | * Initialize the application bind args. These are passed to each |
| 1570 | * process when the bindApplication() IPC is sent to the process. They're |
| 1571 | * lazily setup to make sure the services are running when they're asked for. |
| 1572 | */ |
| 1573 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1574 | if (mAppBindArgs == null) { |
| 1575 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1576 | |
| 1577 | // Setup the application init args |
| 1578 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1579 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1580 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1581 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1582 | } |
| 1583 | return mAppBindArgs; |
| 1584 | } |
| 1585 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1586 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | if (mFocusedActivity != r) { |
| 1588 | mFocusedActivity = r; |
| 1589 | mWindowManager.setFocusedApp(r, true); |
| 1590 | } |
| 1591 | } |
| 1592 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1593 | private final void updateLruProcessInternalLocked(ProcessRecord app, |
| 1594 | boolean oomAdj, boolean updateActivityTime, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1595 | // 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] | 1596 | int lrui = mLruProcesses.indexOf(app); |
| 1597 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1598 | |
| 1599 | int i = mLruProcesses.size()-1; |
| 1600 | int skipTop = 0; |
| 1601 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1602 | app.lruSeq = mLruSeq; |
| 1603 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1604 | // compute the new weight for this process. |
| 1605 | if (updateActivityTime) { |
| 1606 | app.lastActivityTime = SystemClock.uptimeMillis(); |
| 1607 | } |
| 1608 | if (app.activities.size() > 0) { |
| 1609 | // If this process has activities, we more strongly want to keep |
| 1610 | // it around. |
| 1611 | app.lruWeight = app.lastActivityTime; |
| 1612 | } else if (app.pubProviders.size() > 0) { |
| 1613 | // If this process contains content providers, we want to keep |
| 1614 | // it a little more strongly. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1615 | app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1616 | // Also don't let it kick out the first few "real" hidden processes. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1617 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1618 | } else { |
| 1619 | // If this process doesn't have activities, we less strongly |
| 1620 | // want to keep it around, and generally want to avoid getting |
| 1621 | // in front of any very recently used activities. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1622 | app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1623 | // Also don't let it kick out the first few "real" hidden processes. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1624 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1625 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1626 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1627 | while (i >= 0) { |
| 1628 | ProcessRecord p = mLruProcesses.get(i); |
| 1629 | // If this app shouldn't be in front of the first N background |
| 1630 | // apps, then skip over that many that are currently hidden. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1631 | if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1632 | skipTop--; |
| 1633 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1634 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1635 | mLruProcesses.add(i+1, app); |
| 1636 | break; |
| 1637 | } |
| 1638 | i--; |
| 1639 | } |
| 1640 | if (i < 0) { |
| 1641 | mLruProcesses.add(0, app); |
| 1642 | } |
| 1643 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1644 | // If the app is currently using a content provider or service, |
| 1645 | // bump those processes as well. |
| 1646 | if (app.connections.size() > 0) { |
| 1647 | for (ConnectionRecord cr : app.connections) { |
| 1648 | if (cr.binding != null && cr.binding.service != null |
| 1649 | && cr.binding.service.app != null |
| 1650 | && cr.binding.service.app.lruSeq != mLruSeq) { |
| 1651 | updateLruProcessInternalLocked(cr.binding.service.app, oomAdj, |
| 1652 | updateActivityTime, i+1); |
| 1653 | } |
| 1654 | } |
| 1655 | } |
| 1656 | if (app.conProviders.size() > 0) { |
| 1657 | for (ContentProviderRecord cpr : app.conProviders.keySet()) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 1658 | if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) { |
| 1659 | updateLruProcessInternalLocked(cpr.proc, oomAdj, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1660 | updateActivityTime, i+1); |
| 1661 | } |
| 1662 | } |
| 1663 | } |
| 1664 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1665 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1666 | if (oomAdj) { |
| 1667 | updateOomAdjLocked(); |
| 1668 | } |
| 1669 | } |
| 1670 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1671 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1672 | boolean oomAdj, boolean updateActivityTime) { |
| 1673 | mLruSeq++; |
| 1674 | updateLruProcessInternalLocked(app, oomAdj, updateActivityTime, 0); |
| 1675 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1676 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1677 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1678 | String processName, int uid) { |
| 1679 | if (uid == Process.SYSTEM_UID) { |
| 1680 | // The system gets to run in any process. If there are multiple |
| 1681 | // processes with the same uid, just pick the first (this |
| 1682 | // should never happen). |
| 1683 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1684 | processName); |
| 1685 | return procs != null ? procs.valueAt(0) : null; |
| 1686 | } |
| 1687 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1688 | return proc; |
| 1689 | } |
| 1690 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1691 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1692 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1693 | try { |
| 1694 | if (pm.performDexOpt(packageName)) { |
| 1695 | mDidDexOpt = true; |
| 1696 | } |
| 1697 | } catch (RemoteException e) { |
| 1698 | } |
| 1699 | } |
| 1700 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1701 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1702 | int transit = mWindowManager.getPendingAppTransition(); |
| 1703 | return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN |
| 1704 | || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN |
| 1705 | || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT; |
| 1706 | } |
| 1707 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1708 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1709 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1710 | String hostingType, ComponentName hostingName, boolean allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1711 | ProcessRecord app = getProcessRecordLocked(processName, info.uid); |
| 1712 | // We don't have to do anything more if: |
| 1713 | // (1) There is an existing application record; and |
| 1714 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 1715 | // object attached to it so we know it couldn't have crashed; and |
| 1716 | // (3) There is a pid assigned to it, so it is either starting or |
| 1717 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1718 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1719 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 1720 | + " thread=" + (app != null ? app.thread : null) |
| 1721 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1722 | if (app != null && app.pid > 0) { |
| 1723 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1724 | // We already have the app running, or are waiting for it to |
| 1725 | // 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] | 1726 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1727 | // If this is a new package in the process, add the package to the list |
| 1728 | app.addPackage(info.packageName); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1729 | return app; |
| 1730 | } else { |
| 1731 | // An application record is attached to a previous process, |
| 1732 | // clean it up now. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1733 | if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 1734 | handleAppDiedLocked(app, true, true); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1735 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1736 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1737 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | String hostingNameStr = hostingName != null |
| 1739 | ? hostingName.flattenToShortString() : null; |
| 1740 | |
| 1741 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 1742 | // If we are in the background, then check to see if this process |
| 1743 | // is bad. If so, we will just silently fail. |
| 1744 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1745 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 1746 | + "/" + info.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1747 | return null; |
| 1748 | } |
| 1749 | } else { |
| 1750 | // When the user is explicitly starting a process, then clear its |
| 1751 | // crash count so that we won't make it bad until they see at |
| 1752 | // least one crash dialog again, and make the process good again |
| 1753 | // if it had been bad. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1754 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
| 1755 | + "/" + info.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1756 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 1757 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1758 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1759 | info.processName); |
| 1760 | mBadProcesses.remove(info.processName, info.uid); |
| 1761 | if (app != null) { |
| 1762 | app.bad = false; |
| 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | if (app == null) { |
| 1768 | app = newProcessRecordLocked(null, info, processName); |
| 1769 | mProcessNames.put(processName, info.uid, app); |
| 1770 | } else { |
| 1771 | // If this is a new package in the process, add the package to the list |
| 1772 | app.addPackage(info.packageName); |
| 1773 | } |
| 1774 | |
| 1775 | // If the system is not ready yet, then hold off on starting this |
| 1776 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1777 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1778 | && !isAllowedWhileBooting(info) |
| 1779 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1780 | if (!mProcessesOnHold.contains(app)) { |
| 1781 | mProcessesOnHold.add(app); |
| 1782 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1783 | 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] | 1784 | return app; |
| 1785 | } |
| 1786 | |
| 1787 | startProcessLocked(app, hostingType, hostingNameStr); |
| 1788 | return (app.pid != 0) ? app : null; |
| 1789 | } |
| 1790 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1791 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 1792 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 1793 | } |
| 1794 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1795 | private final void startProcessLocked(ProcessRecord app, |
| 1796 | String hostingType, String hostingNameStr) { |
| 1797 | if (app.pid > 0 && app.pid != MY_PID) { |
| 1798 | synchronized (mPidsSelfLocked) { |
| 1799 | mPidsSelfLocked.remove(app.pid); |
| 1800 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 1801 | } |
| 1802 | app.pid = 0; |
| 1803 | } |
| 1804 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1805 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 1806 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1807 | mProcessesOnHold.remove(app); |
| 1808 | |
| 1809 | updateCpuStats(); |
| 1810 | |
| 1811 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 1812 | mProcDeaths[0] = 0; |
| 1813 | |
| 1814 | try { |
| 1815 | int uid = app.info.uid; |
| 1816 | int[] gids = null; |
| 1817 | try { |
| 1818 | gids = mContext.getPackageManager().getPackageGids( |
| 1819 | app.info.packageName); |
| 1820 | } catch (PackageManager.NameNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1821 | Slog.w(TAG, "Unable to retrieve gids", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1822 | } |
| 1823 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 1824 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 1825 | && mTopComponent != null |
| 1826 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 1827 | uid = 0; |
| 1828 | } |
| 1829 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 1830 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 1831 | uid = 0; |
| 1832 | } |
| 1833 | } |
| 1834 | int debugFlags = 0; |
| 1835 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 1836 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
Elliott Hughes | fa36aee | 2011-06-17 14:39:41 -0700 | [diff] [blame] | 1837 | // Also turn on CheckJNI for debuggable apps. It's quite |
| 1838 | // awkward to turn on otherwise. |
| 1839 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1840 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 1841 | // Run the app in safe mode if its manifest requests so or the |
| 1842 | // system is booted in safe mode. |
| 1843 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 1844 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 1845 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 1846 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1847 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 1848 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 1849 | } |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 1850 | if ("1".equals(SystemProperties.get("debug.jni.logging"))) { |
| 1851 | debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING; |
| 1852 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1853 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 1854 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 1855 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1856 | |
| 1857 | // Start the process. It will either succeed and return a result containing |
| 1858 | // the PID of the new process, or else throw a RuntimeException. |
| 1859 | Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread", |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 1860 | app.processName, uid, uid, gids, debugFlags, |
| 1861 | app.info.targetSdkVersion, null); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1862 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1863 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 1864 | synchronized (bs) { |
| 1865 | if (bs.isOnBattery()) { |
| 1866 | app.batteryStats.incStartsLocked(); |
| 1867 | } |
| 1868 | } |
| 1869 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1870 | EventLog.writeEvent(EventLogTags.AM_PROC_START, startResult.pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1871 | app.processName, hostingType, |
| 1872 | hostingNameStr != null ? hostingNameStr : ""); |
| 1873 | |
| 1874 | if (app.persistent) { |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1875 | Watchdog.getInstance().processStarted(app.processName, startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1876 | } |
| 1877 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 1878 | StringBuilder buf = mStringBuilder; |
| 1879 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1880 | buf.append("Start proc "); |
| 1881 | buf.append(app.processName); |
| 1882 | buf.append(" for "); |
| 1883 | buf.append(hostingType); |
| 1884 | if (hostingNameStr != null) { |
| 1885 | buf.append(" "); |
| 1886 | buf.append(hostingNameStr); |
| 1887 | } |
| 1888 | buf.append(": pid="); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1889 | buf.append(startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1890 | buf.append(" uid="); |
| 1891 | buf.append(uid); |
| 1892 | buf.append(" gids={"); |
| 1893 | if (gids != null) { |
| 1894 | for (int gi=0; gi<gids.length; gi++) { |
| 1895 | if (gi != 0) buf.append(", "); |
| 1896 | buf.append(gids[gi]); |
| 1897 | |
| 1898 | } |
| 1899 | } |
| 1900 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1901 | Slog.i(TAG, buf.toString()); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1902 | app.pid = startResult.pid; |
| 1903 | app.usingWrapper = startResult.usingWrapper; |
| 1904 | app.removed = false; |
| 1905 | synchronized (mPidsSelfLocked) { |
| 1906 | this.mPidsSelfLocked.put(startResult.pid, app); |
| 1907 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1908 | msg.obj = app; |
| 1909 | mHandler.sendMessageDelayed(msg, startResult.usingWrapper |
| 1910 | ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1911 | } |
| 1912 | } catch (RuntimeException e) { |
| 1913 | // XXX do better error recovery. |
| 1914 | app.pid = 0; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1915 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1919 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1920 | if (resumed) { |
| 1921 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 1922 | } else { |
| 1923 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 1924 | } |
| 1925 | } |
| 1926 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1927 | boolean startHomeActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 1928 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 1929 | && mTopAction == null) { |
| 1930 | // We are running in factory test mode, but unable to find |
| 1931 | // the factory test app, so just sit around displaying the |
| 1932 | // error message and don't try to start anything. |
| 1933 | return false; |
| 1934 | } |
| 1935 | Intent intent = new Intent( |
| 1936 | mTopAction, |
| 1937 | mTopData != null ? Uri.parse(mTopData) : null); |
| 1938 | intent.setComponent(mTopComponent); |
| 1939 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 1940 | intent.addCategory(Intent.CATEGORY_HOME); |
| 1941 | } |
| 1942 | ActivityInfo aInfo = |
| 1943 | intent.resolveActivityInfo(mContext.getPackageManager(), |
| 1944 | STOCK_PM_FLAGS); |
| 1945 | if (aInfo != null) { |
| 1946 | intent.setComponent(new ComponentName( |
| 1947 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 1948 | // Don't do this if the home app is currently being |
| 1949 | // instrumented. |
| 1950 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 1951 | aInfo.applicationInfo.uid); |
| 1952 | if (app == null || app.instrumentationClass == null) { |
| 1953 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1954 | mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 1955 | null, null, 0, 0, 0, false, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | |
| 1960 | return true; |
| 1961 | } |
| 1962 | |
| 1963 | /** |
| 1964 | * Starts the "new version setup screen" if appropriate. |
| 1965 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1966 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 1967 | // Only do this once per boot. |
| 1968 | if (mCheckedForSetup) { |
| 1969 | return; |
| 1970 | } |
| 1971 | |
| 1972 | // We will show this screen if the current one is a different |
| 1973 | // version than the last one shown, and we are not running in |
| 1974 | // low-level factory test mode. |
| 1975 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1976 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
| 1977 | Settings.Secure.getInt(resolver, |
| 1978 | Settings.Secure.DEVICE_PROVISIONED, 0) != 0) { |
| 1979 | mCheckedForSetup = true; |
| 1980 | |
| 1981 | // See if we should be showing the platform update setup UI. |
| 1982 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 1983 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 1984 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 1985 | |
| 1986 | // We don't allow third party apps to replace this. |
| 1987 | ResolveInfo ri = null; |
| 1988 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 1989 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 1990 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 1991 | ri = ris.get(i); |
| 1992 | break; |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | if (ri != null) { |
| 1997 | String vers = ri.activityInfo.metaData != null |
| 1998 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 1999 | : null; |
| 2000 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2001 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2002 | Intent.METADATA_SETUP_VERSION); |
| 2003 | } |
| 2004 | String lastVers = Settings.Secure.getString( |
| 2005 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2006 | if (vers != null && !vers.equals(lastVers)) { |
| 2007 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2008 | intent.setComponent(new ComponentName( |
| 2009 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2010 | mMainStack.startActivityLocked(null, intent, null, null, 0, ri.activityInfo, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2011 | null, null, 0, 0, 0, false, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2012 | } |
| 2013 | } |
| 2014 | } |
| 2015 | } |
| 2016 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2017 | CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2018 | return mCompatModePackages.compatibilityInfoForPackageLocked(ai); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2019 | } |
| 2020 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2021 | public int getFrontActivityScreenCompatMode() { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2022 | synchronized (this) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2023 | return mCompatModePackages.getFrontActivityScreenCompatModeLocked(); |
| 2024 | } |
| 2025 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2026 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2027 | public void setFrontActivityScreenCompatMode(int mode) { |
| 2028 | synchronized (this) { |
| 2029 | mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode); |
| 2030 | } |
| 2031 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2032 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2033 | public int getPackageScreenCompatMode(String packageName) { |
| 2034 | synchronized (this) { |
| 2035 | return mCompatModePackages.getPackageScreenCompatModeLocked(packageName); |
| 2036 | } |
| 2037 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2038 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2039 | public void setPackageScreenCompatMode(String packageName, int mode) { |
| 2040 | synchronized (this) { |
| 2041 | mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2042 | } |
| 2043 | } |
| 2044 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2045 | public boolean getPackageAskScreenCompat(String packageName) { |
| 2046 | synchronized (this) { |
| 2047 | return mCompatModePackages.getPackageAskCompatModeLocked(packageName); |
| 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | public void setPackageAskScreenCompat(String packageName, boolean ask) { |
| 2052 | synchronized (this) { |
| 2053 | mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask); |
| 2054 | } |
| 2055 | } |
| 2056 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2057 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2058 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2059 | |
| 2060 | final int identHash = System.identityHashCode(r); |
| 2061 | updateUsageStats(r, true); |
| 2062 | |
| 2063 | int i = mWatchers.beginBroadcast(); |
| 2064 | while (i > 0) { |
| 2065 | i--; |
| 2066 | IActivityWatcher w = mWatchers.getBroadcastItem(i); |
| 2067 | if (w != null) { |
| 2068 | try { |
| 2069 | w.activityResuming(identHash); |
| 2070 | } catch (RemoteException e) { |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | mWatchers.finishBroadcast(); |
| 2075 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2076 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2077 | private void dispatchForegroundActivitiesChanged(int pid, int uid, boolean foregroundActivities) { |
| 2078 | int i = mProcessObservers.beginBroadcast(); |
| 2079 | while (i > 0) { |
| 2080 | i--; |
| 2081 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2082 | if (observer != null) { |
| 2083 | try { |
| 2084 | observer.onForegroundActivitiesChanged(pid, uid, foregroundActivities); |
| 2085 | } catch (RemoteException e) { |
| 2086 | } |
| 2087 | } |
| 2088 | } |
| 2089 | mProcessObservers.finishBroadcast(); |
| 2090 | } |
| 2091 | |
| 2092 | private void dispatchProcessDied(int pid, int uid) { |
| 2093 | int i = mProcessObservers.beginBroadcast(); |
| 2094 | while (i > 0) { |
| 2095 | i--; |
| 2096 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2097 | if (observer != null) { |
| 2098 | try { |
| 2099 | observer.onProcessDied(pid, uid); |
| 2100 | } catch (RemoteException e) { |
| 2101 | } |
| 2102 | } |
| 2103 | } |
| 2104 | mProcessObservers.finishBroadcast(); |
| 2105 | } |
| 2106 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2107 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2108 | final int N = mPendingActivityLaunches.size(); |
| 2109 | if (N <= 0) { |
| 2110 | return; |
| 2111 | } |
| 2112 | for (int i=0; i<N; i++) { |
| 2113 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2114 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2115 | pal.grantedUriPermissions, pal.grantedMode, pal.onlyIfNeeded, |
| 2116 | doResume && i == (N-1)); |
| 2117 | } |
| 2118 | mPendingActivityLaunches.clear(); |
| 2119 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2120 | |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2121 | public final int startActivity(IApplicationThread caller, |
| 2122 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2123 | int grantedMode, IBinder resultTo, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2124 | String resultWho, int requestCode, boolean onlyIfNeeded, boolean debug, |
| 2125 | String profileFile, ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2126 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2127 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2128 | requestCode, onlyIfNeeded, debug, profileFile, profileFd, autoStopProfiler, |
| 2129 | null, null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | public final WaitResult startActivityAndWait(IApplicationThread caller, |
| 2133 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2134 | int grantedMode, IBinder resultTo, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2135 | String resultWho, int requestCode, boolean onlyIfNeeded, boolean debug, |
| 2136 | String profileFile, ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2137 | WaitResult res = new WaitResult(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2138 | mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2139 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2140 | requestCode, onlyIfNeeded, debug, profileFile, profileFd, autoStopProfiler, |
| 2141 | res, null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2142 | return res; |
| 2143 | } |
| 2144 | |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2145 | public final int startActivityWithConfig(IApplicationThread caller, |
| 2146 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2147 | int grantedMode, IBinder resultTo, |
| 2148 | String resultWho, int requestCode, boolean onlyIfNeeded, |
| 2149 | boolean debug, Configuration config) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2150 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2151 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2152 | requestCode, onlyIfNeeded, debug, null, null, false, null, config); |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2155 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2156 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2157 | IBinder resultTo, String resultWho, int requestCode, |
| 2158 | int flagsMask, int flagsValues) { |
| 2159 | // Refuse possible leaked file descriptors |
| 2160 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2161 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2162 | } |
| 2163 | |
| 2164 | IIntentSender sender = intent.getTarget(); |
| 2165 | if (!(sender instanceof PendingIntentRecord)) { |
| 2166 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2167 | } |
| 2168 | |
| 2169 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2170 | |
| 2171 | synchronized (this) { |
| 2172 | // If this is coming from the currently resumed activity, it is |
| 2173 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2174 | if (mMainStack.mResumedActivity != null |
| 2175 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2176 | Binder.getCallingUid()) { |
| 2177 | mAppSwitchesAllowedTime = 0; |
| 2178 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 2181 | return pir.sendInner(0, fillInIntent, resolvedType, null, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2182 | null, resultTo, resultWho, requestCode, flagsMask, flagsValues); |
| 2183 | } |
| 2184 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2185 | public boolean startNextMatchingActivity(IBinder callingActivity, |
| 2186 | Intent intent) { |
| 2187 | // Refuse possible leaked file descriptors |
| 2188 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2189 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2190 | } |
| 2191 | |
| 2192 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2193 | ActivityRecord r = mMainStack.isInStackLocked(callingActivity); |
| 2194 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2195 | return false; |
| 2196 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2197 | if (r.app == null || r.app.thread == null) { |
| 2198 | // The caller is not running... d'oh! |
| 2199 | return false; |
| 2200 | } |
| 2201 | intent = new Intent(intent); |
| 2202 | // The caller is not allowed to change the data. |
| 2203 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2204 | // And we are resetting to find the next component... |
| 2205 | intent.setComponent(null); |
| 2206 | |
| 2207 | ActivityInfo aInfo = null; |
| 2208 | try { |
| 2209 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2210 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2211 | intent, r.resolvedType, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 2212 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2213 | |
| 2214 | // Look for the original activity in the list... |
| 2215 | final int N = resolves != null ? resolves.size() : 0; |
| 2216 | for (int i=0; i<N; i++) { |
| 2217 | ResolveInfo rInfo = resolves.get(i); |
| 2218 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2219 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2220 | // We found the current one... the next matching is |
| 2221 | // after it. |
| 2222 | i++; |
| 2223 | if (i<N) { |
| 2224 | aInfo = resolves.get(i).activityInfo; |
| 2225 | } |
| 2226 | break; |
| 2227 | } |
| 2228 | } |
| 2229 | } catch (RemoteException e) { |
| 2230 | } |
| 2231 | |
| 2232 | if (aInfo == null) { |
| 2233 | // Nobody who is next! |
| 2234 | return false; |
| 2235 | } |
| 2236 | |
| 2237 | intent.setComponent(new ComponentName( |
| 2238 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2239 | intent.setFlags(intent.getFlags()&~( |
| 2240 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2241 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2242 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2243 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2244 | |
| 2245 | // Okay now we need to start the new activity, replacing the |
| 2246 | // currently running activity. This is a little tricky because |
| 2247 | // we want to start the new one as if the current one is finished, |
| 2248 | // but not finish the current one first so that there is no flicker. |
| 2249 | // And thus... |
| 2250 | final boolean wasFinishing = r.finishing; |
| 2251 | r.finishing = true; |
| 2252 | |
| 2253 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2254 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2255 | final String resultWho = r.resultWho; |
| 2256 | final int requestCode = r.requestCode; |
| 2257 | r.resultTo = null; |
| 2258 | if (resultTo != null) { |
| 2259 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2260 | } |
| 2261 | |
| 2262 | final long origId = Binder.clearCallingIdentity(); |
| 2263 | // XXX we are not dealing with propagating grantedUriPermissions... |
| 2264 | // 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] | 2265 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2266 | r.resolvedType, null, 0, aInfo, resultTo, resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2267 | requestCode, -1, r.launchedFromUid, false, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2268 | Binder.restoreCallingIdentity(origId); |
| 2269 | |
| 2270 | r.finishing = wasFinishing; |
| 2271 | if (res != START_SUCCESS) { |
| 2272 | return false; |
| 2273 | } |
| 2274 | return true; |
| 2275 | } |
| 2276 | } |
| 2277 | |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2278 | public final int startActivityInPackage(int uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2279 | Intent intent, String resolvedType, IBinder resultTo, |
| 2280 | String resultWho, int requestCode, boolean onlyIfNeeded) { |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2281 | |
| 2282 | // This is so super not safe, that only the system (or okay root) |
| 2283 | // can do it. |
| 2284 | final int callingUid = Binder.getCallingUid(); |
| 2285 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2286 | throw new SecurityException( |
| 2287 | "startActivityInPackage only available to the system"); |
| 2288 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2289 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2290 | return mMainStack.startActivityMayWait(null, uid, intent, resolvedType, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2291 | null, 0, resultTo, resultWho, requestCode, onlyIfNeeded, false, |
| 2292 | null, null, false, null, null); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | public final int startActivities(IApplicationThread caller, |
| 2296 | Intent[] intents, String[] resolvedTypes, IBinder resultTo) { |
| 2297 | return mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo); |
| 2298 | } |
| 2299 | |
| 2300 | public final int startActivitiesInPackage(int uid, |
| 2301 | Intent[] intents, String[] resolvedTypes, IBinder resultTo) { |
| 2302 | |
| 2303 | // This is so super not safe, that only the system (or okay root) |
| 2304 | // can do it. |
| 2305 | final int callingUid = Binder.getCallingUid(); |
| 2306 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2307 | throw new SecurityException( |
| 2308 | "startActivityInPackage only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2309 | } |
| 2310 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2311 | return mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2312 | } |
| 2313 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2314 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2315 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2316 | // Quick case: check if the top-most recent task is the same. |
| 2317 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2318 | return; |
| 2319 | } |
| 2320 | // 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] | 2321 | for (int i=0; i<N; i++) { |
| 2322 | TaskRecord tr = mRecentTasks.get(i); |
| 2323 | if ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2324 | || (task.intent != null && task.intent.filterEquals(tr.intent))) { |
| 2325 | mRecentTasks.remove(i); |
| 2326 | i--; |
| 2327 | N--; |
| 2328 | if (task.intent == null) { |
| 2329 | // If the new recent task we are adding is not fully |
| 2330 | // specified, then replace it with the existing recent task. |
| 2331 | task = tr; |
| 2332 | } |
| 2333 | } |
| 2334 | } |
| 2335 | if (N >= MAX_RECENT_TASKS) { |
| 2336 | mRecentTasks.remove(N-1); |
| 2337 | } |
| 2338 | mRecentTasks.add(0, task); |
| 2339 | } |
| 2340 | |
| 2341 | public void setRequestedOrientation(IBinder token, |
| 2342 | int requestedOrientation) { |
| 2343 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2344 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2345 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2346 | return; |
| 2347 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2348 | final long origId = Binder.clearCallingIdentity(); |
| 2349 | mWindowManager.setAppOrientation(r, requestedOrientation); |
| 2350 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2351 | mConfiguration, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2352 | r.mayFreezeScreenLocked(r.app) ? r : null); |
| 2353 | if (config != null) { |
| 2354 | r.frozenBeforeDestroy = true; |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 2355 | if (!updateConfigurationLocked(config, r, false)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2356 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2357 | } |
| 2358 | } |
| 2359 | Binder.restoreCallingIdentity(origId); |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | public int getRequestedOrientation(IBinder token) { |
| 2364 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2365 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2366 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2367 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2368 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2369 | return mWindowManager.getAppOrientation(r); |
| 2370 | } |
| 2371 | } |
| 2372 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2373 | /** |
| 2374 | * This is the internal entry point for handling Activity.finish(). |
| 2375 | * |
| 2376 | * @param token The Binder token referencing the Activity we want to finish. |
| 2377 | * @param resultCode Result code, if any, from this Activity. |
| 2378 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2379 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2380 | * @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] | 2381 | */ |
| 2382 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2383 | // Refuse possible leaked file descriptors |
| 2384 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2385 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2386 | } |
| 2387 | |
| 2388 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2389 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2390 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2391 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2392 | if (next != null) { |
| 2393 | // ask watcher if this is allowed |
| 2394 | boolean resumeOK = true; |
| 2395 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2396 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2397 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2398 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | if (!resumeOK) { |
| 2402 | return false; |
| 2403 | } |
| 2404 | } |
| 2405 | } |
| 2406 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2407 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2408 | resultData, "app-request"); |
| 2409 | Binder.restoreCallingIdentity(origId); |
| 2410 | return res; |
| 2411 | } |
| 2412 | } |
| 2413 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2414 | public final void finishHeavyWeightApp() { |
| 2415 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2416 | != PackageManager.PERMISSION_GRANTED) { |
| 2417 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2418 | + Binder.getCallingPid() |
| 2419 | + ", uid=" + Binder.getCallingUid() |
| 2420 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2421 | Slog.w(TAG, msg); |
| 2422 | throw new SecurityException(msg); |
| 2423 | } |
| 2424 | |
| 2425 | synchronized(this) { |
| 2426 | if (mHeavyWeightProcess == null) { |
| 2427 | return; |
| 2428 | } |
| 2429 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2430 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2431 | mHeavyWeightProcess.activities); |
| 2432 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2433 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2434 | if (!r.finishing) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2435 | int index = mMainStack.indexOfTokenLocked(r); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2436 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2437 | mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED, |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2438 | null, "finish-heavy"); |
| 2439 | } |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | mHeavyWeightProcess = null; |
| 2444 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 2445 | } |
| 2446 | } |
| 2447 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2448 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2449 | String message) { |
| 2450 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2451 | != PackageManager.PERMISSION_GRANTED) { |
| 2452 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2453 | + Binder.getCallingPid() |
| 2454 | + ", uid=" + Binder.getCallingUid() |
| 2455 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2456 | Slog.w(TAG, msg); |
| 2457 | throw new SecurityException(msg); |
| 2458 | } |
| 2459 | |
| 2460 | synchronized(this) { |
| 2461 | ProcessRecord proc = null; |
| 2462 | |
| 2463 | // Figure out which process to kill. We don't trust that initialPid |
| 2464 | // still has any relation to current pids, so must scan through the |
| 2465 | // list. |
| 2466 | synchronized (mPidsSelfLocked) { |
| 2467 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2468 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
| 2469 | if (p.info.uid != uid) { |
| 2470 | continue; |
| 2471 | } |
| 2472 | if (p.pid == initialPid) { |
| 2473 | proc = p; |
| 2474 | break; |
| 2475 | } |
| 2476 | for (String str : p.pkgList) { |
| 2477 | if (str.equals(packageName)) { |
| 2478 | proc = p; |
| 2479 | } |
| 2480 | } |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2485 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2486 | + " initialPid=" + initialPid |
| 2487 | + " packageName=" + packageName); |
| 2488 | return; |
| 2489 | } |
| 2490 | |
| 2491 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2492 | if (proc.pid == Process.myPid()) { |
| 2493 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2494 | return; |
| 2495 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2496 | long ident = Binder.clearCallingIdentity(); |
| 2497 | try { |
| 2498 | proc.thread.scheduleCrash(message); |
| 2499 | } catch (RemoteException e) { |
| 2500 | } |
| 2501 | Binder.restoreCallingIdentity(ident); |
| 2502 | } |
| 2503 | } |
| 2504 | } |
| 2505 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2506 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2507 | int requestCode) { |
| 2508 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2509 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2510 | if (self == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2511 | return; |
| 2512 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2513 | |
| 2514 | final long origId = Binder.clearCallingIdentity(); |
| 2515 | |
| 2516 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2517 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2518 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2519 | if (r.resultTo == self && r.requestCode == requestCode) { |
| 2520 | if ((r.resultWho == null && resultWho == null) || |
| 2521 | (r.resultWho != null && r.resultWho.equals(resultWho))) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2522 | mMainStack.finishActivityLocked(r, i, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2523 | Activity.RESULT_CANCELED, null, "request-sub"); |
| 2524 | } |
| 2525 | } |
| 2526 | } |
| 2527 | |
| 2528 | Binder.restoreCallingIdentity(origId); |
| 2529 | } |
| 2530 | } |
| 2531 | |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2532 | public boolean willActivityBeVisible(IBinder token) { |
| 2533 | synchronized(this) { |
| 2534 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2535 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2536 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2537 | if (r == token) { |
| 2538 | return true; |
| 2539 | } |
| 2540 | if (r.fullscreen && !r.finishing) { |
| 2541 | return false; |
| 2542 | } |
| 2543 | } |
| 2544 | return true; |
| 2545 | } |
| 2546 | } |
| 2547 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2548 | public void overridePendingTransition(IBinder token, String packageName, |
| 2549 | int enterAnim, int exitAnim) { |
| 2550 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2551 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2552 | if (self == null) { |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2553 | return; |
| 2554 | } |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2555 | |
| 2556 | final long origId = Binder.clearCallingIdentity(); |
| 2557 | |
| 2558 | if (self.state == ActivityState.RESUMED |
| 2559 | || self.state == ActivityState.PAUSING) { |
| 2560 | mWindowManager.overridePendingAppTransition(packageName, |
| 2561 | enterAnim, exitAnim); |
| 2562 | } |
| 2563 | |
| 2564 | Binder.restoreCallingIdentity(origId); |
| 2565 | } |
| 2566 | } |
| 2567 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2568 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2569 | * Main function for removing an existing process from the activity manager |
| 2570 | * as a result of that process going away. Clears out all connections |
| 2571 | * to the process. |
| 2572 | */ |
| 2573 | private final void handleAppDiedLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2574 | boolean restarting, boolean allowRestart) { |
| 2575 | cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2576 | if (!restarting) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2577 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2578 | } |
| 2579 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2580 | if (mProfileProc == app) { |
| 2581 | clearProfilerLocked(); |
| 2582 | } |
| 2583 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2584 | // Just in case... |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2585 | if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) { |
| 2586 | if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity); |
| 2587 | mMainStack.mPausingActivity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2588 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2589 | if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) { |
| 2590 | mMainStack.mLastPausedActivity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | // Remove this application's activities from active lists. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2594 | mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2595 | |
| 2596 | boolean atTop = true; |
| 2597 | boolean hasVisibleActivities = false; |
| 2598 | |
| 2599 | // Clean out the history list. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2600 | int i = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2601 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2602 | TAG, "Removing app " + app + " from history with " + i + " entries"); |
| 2603 | while (i > 0) { |
| 2604 | i--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2605 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2606 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2607 | TAG, "Record #" + i + " " + r + ": app=" + r.app); |
| 2608 | if (r.app == app) { |
| 2609 | if ((!r.haveState && !r.stateNotNeeded) || r.finishing) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2610 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2611 | TAG, "Removing this entry! frozen=" + r.haveState |
| 2612 | + " finishing=" + r.finishing); |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 2613 | r.makeFinishing(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2614 | mMainStack.mHistory.remove(i); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 2615 | r.takeFromHistory(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2616 | mWindowManager.removeAppToken(r); |
| 2617 | if (VALIDATE_TOKENS) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2618 | mWindowManager.validateAppTokens(mMainStack.mHistory); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2619 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2620 | r.removeUriPermissionsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2621 | |
| 2622 | } else { |
| 2623 | // We have the current state for this activity, so |
| 2624 | // it can be restarted later when needed. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2625 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2626 | TAG, "Keeping entry, setting app to null"); |
| 2627 | if (r.visible) { |
| 2628 | hasVisibleActivities = true; |
| 2629 | } |
| 2630 | r.app = null; |
| 2631 | r.nowVisible = false; |
| 2632 | if (!r.haveState) { |
| 2633 | r.icicle = null; |
| 2634 | } |
| 2635 | } |
| 2636 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2637 | r.stack.cleanUpActivityLocked(r, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2638 | } |
| 2639 | atTop = false; |
| 2640 | } |
| 2641 | |
| 2642 | app.activities.clear(); |
| 2643 | |
| 2644 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2645 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2646 | + " running instrumentation " + app.instrumentationClass); |
| 2647 | Bundle info = new Bundle(); |
| 2648 | info.putString("shortMsg", "Process crashed."); |
| 2649 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 2650 | } |
| 2651 | |
| 2652 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2653 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2654 | // If there was nothing to resume, and we are not already |
| 2655 | // restarting this process, but there is a visible activity that |
| 2656 | // is hosted by the process... then make sure all visible |
| 2657 | // activities are running, taking care of restarting this |
| 2658 | // process. |
| 2659 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2660 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2661 | } |
| 2662 | } |
| 2663 | } |
| 2664 | } |
| 2665 | |
| 2666 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 2667 | IBinder threadBinder = thread.asBinder(); |
| 2668 | |
| 2669 | // Find the application record. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2670 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2671 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2672 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 2673 | return i; |
| 2674 | } |
| 2675 | } |
| 2676 | return -1; |
| 2677 | } |
| 2678 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2679 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2680 | IApplicationThread thread) { |
| 2681 | if (thread == null) { |
| 2682 | return null; |
| 2683 | } |
| 2684 | |
| 2685 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2686 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2687 | } |
| 2688 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2689 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2690 | IApplicationThread thread) { |
| 2691 | |
| 2692 | mProcDeaths[0]++; |
| 2693 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2694 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 2695 | synchronized (stats) { |
| 2696 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 2697 | } |
| 2698 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2699 | // Clean up already done if the process has been re-started. |
| 2700 | if (app.pid == pid && app.thread != null && |
| 2701 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 2702 | if (!app.killedBackground) { |
| 2703 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 2704 | + ") has died."); |
| 2705 | } |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 2706 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2707 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2708 | TAG, "Dying app: " + app + ", pid: " + pid |
| 2709 | + ", thread: " + thread.asBinder()); |
| 2710 | boolean doLowMem = app.instrumentationClass == null; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2711 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2712 | |
| 2713 | if (doLowMem) { |
| 2714 | // If there are no longer any background processes running, |
| 2715 | // and the app that died was not running instrumentation, |
| 2716 | // then tell everyone we are now low on memory. |
| 2717 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2718 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2719 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2720 | if (rec.thread != null && rec.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2721 | haveBg = true; |
| 2722 | break; |
| 2723 | } |
| 2724 | } |
| 2725 | |
| 2726 | if (!haveBg) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2727 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2728 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2729 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2730 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 2731 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2732 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 2733 | // The low memory report is overriding any current |
| 2734 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2735 | // heavy/important/visible/foreground processes first. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2736 | if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2737 | rec.lastRequestedGc = 0; |
| 2738 | } else { |
| 2739 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2740 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2741 | rec.reportLowMemory = true; |
| 2742 | rec.lastLowMemory = now; |
| 2743 | mProcessesToGc.remove(rec); |
| 2744 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2745 | } |
| 2746 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2747 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2748 | } |
| 2749 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2750 | } else if (app.pid != pid) { |
| 2751 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2752 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2753 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | 28a8c2b | 2010-03-01 11:30:02 -0800 | [diff] [blame] | 2754 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 2755 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2756 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2757 | + thread.asBinder()); |
| 2758 | } |
| 2759 | } |
| 2760 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2761 | /** |
| 2762 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2763 | * @param clearTraces causes the dump file to be erased prior to the new |
| 2764 | * traces being written, if true; when false, the new traces will be |
| 2765 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2766 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 2767 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2768 | * @return file containing stack traces, or null if no dump file is configured |
| 2769 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2770 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
| 2771 | ProcessStats processStats, SparseArray<Boolean> lastPids) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2772 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 2773 | if (tracesPath == null || tracesPath.length() == 0) { |
| 2774 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2775 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2776 | |
| 2777 | File tracesFile = new File(tracesPath); |
| 2778 | try { |
| 2779 | File tracesDir = tracesFile.getParentFile(); |
| 2780 | if (!tracesDir.exists()) tracesFile.mkdirs(); |
| 2781 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 2782 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2783 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2784 | tracesFile.createNewFile(); |
| 2785 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 2786 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2787 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2788 | return null; |
| 2789 | } |
| 2790 | |
| 2791 | // Use a FileObserver to detect when traces finish writing. |
| 2792 | // The order of traces is considered important to maintain for legibility. |
| 2793 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 2794 | public synchronized void onEvent(int event, String path) { notify(); } |
| 2795 | }; |
| 2796 | |
| 2797 | try { |
| 2798 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2799 | |
| 2800 | // First collect all of the stacks of the most important pids. |
| 2801 | try { |
| 2802 | int num = firstPids.size(); |
| 2803 | for (int i = 0; i < num; i++) { |
| 2804 | synchronized (observer) { |
| 2805 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 2806 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 2807 | } |
| 2808 | } |
| 2809 | } catch (InterruptedException e) { |
| 2810 | Log.wtf(TAG, e); |
| 2811 | } |
| 2812 | |
| 2813 | // Next measure CPU usage. |
| 2814 | if (processStats != null) { |
| 2815 | processStats.init(); |
| 2816 | System.gc(); |
| 2817 | processStats.update(); |
| 2818 | try { |
| 2819 | synchronized (processStats) { |
| 2820 | processStats.wait(500); // measure over 1/2 second. |
| 2821 | } |
| 2822 | } catch (InterruptedException e) { |
| 2823 | } |
| 2824 | processStats.update(); |
| 2825 | |
| 2826 | // We'll take the stack crawls of just the top apps using CPU. |
| 2827 | final int N = processStats.countWorkingStats(); |
| 2828 | int numProcs = 0; |
| 2829 | for (int i=0; i<N && numProcs<5; i++) { |
| 2830 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 2831 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 2832 | numProcs++; |
| 2833 | try { |
| 2834 | synchronized (observer) { |
| 2835 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 2836 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 2837 | } |
| 2838 | } catch (InterruptedException e) { |
| 2839 | Log.wtf(TAG, e); |
| 2840 | } |
| 2841 | |
| 2842 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2843 | } |
| 2844 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2845 | |
| 2846 | return tracesFile; |
| 2847 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2848 | } finally { |
| 2849 | observer.stopWatching(); |
| 2850 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2851 | } |
| 2852 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 2853 | private final class AppNotResponding implements Runnable { |
| 2854 | private final ProcessRecord mApp; |
| 2855 | private final String mAnnotation; |
| 2856 | |
| 2857 | public AppNotResponding(ProcessRecord app, String annotation) { |
| 2858 | mApp = app; |
| 2859 | mAnnotation = annotation; |
| 2860 | } |
| 2861 | |
| 2862 | @Override |
| 2863 | public void run() { |
| 2864 | appNotResponding(mApp, null, null, mAnnotation); |
| 2865 | } |
| 2866 | } |
| 2867 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2868 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
| 2869 | ActivityRecord parent, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2870 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 2871 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 2872 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 2873 | if (mController != null) { |
| 2874 | try { |
| 2875 | // 0 == continue, -1 = kill process immediately |
| 2876 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 2877 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 2878 | } catch (RemoteException e) { |
| 2879 | mController = null; |
| 2880 | } |
| 2881 | } |
| 2882 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2883 | long anrTime = SystemClock.uptimeMillis(); |
| 2884 | if (MONITOR_CPU_USAGE) { |
| 2885 | updateCpuStatsNow(); |
| 2886 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2887 | |
| 2888 | synchronized (this) { |
| 2889 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 2890 | if (mShuttingDown) { |
| 2891 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 2892 | return; |
| 2893 | } else if (app.notResponding) { |
| 2894 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 2895 | return; |
| 2896 | } else if (app.crashing) { |
| 2897 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 2898 | return; |
| 2899 | } |
| 2900 | |
| 2901 | // In case we come through here for the same app before completing |
| 2902 | // this one, mark as anring now so we will bail out. |
| 2903 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2904 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2905 | // Log the ANR to the event log. |
| 2906 | EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags, |
| 2907 | annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2908 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2909 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2910 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2911 | |
| 2912 | int parentPid = app.pid; |
| 2913 | 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] | 2914 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2915 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2916 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2917 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2918 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 2919 | ProcessRecord r = mLruProcesses.get(i); |
| 2920 | if (r != null && r.thread != null) { |
| 2921 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2922 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 2923 | if (r.persistent) { |
| 2924 | firstPids.add(pid); |
| 2925 | } else { |
| 2926 | lastPids.put(pid, Boolean.TRUE); |
| 2927 | } |
| 2928 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2929 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2930 | } |
| 2931 | } |
| 2932 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2933 | // Log the ANR to the main log. |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 2934 | StringBuilder info = mStringBuilder; |
| 2935 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2936 | info.append("ANR in ").append(app.processName); |
| 2937 | if (activity != null && activity.shortComponentName != null) { |
| 2938 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 2939 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 2940 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2941 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 2942 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2943 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2944 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 2945 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2946 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2947 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 2948 | final ProcessStats processStats = new ProcessStats(true); |
| 2949 | |
| 2950 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids); |
| 2951 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2952 | String cpuInfo = null; |
| 2953 | if (MONITOR_CPU_USAGE) { |
| 2954 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2955 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2956 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2957 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2958 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2959 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2960 | } |
| 2961 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2962 | info.append(processStats.printCurrentState(anrTime)); |
| 2963 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2964 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2965 | if (tracesFile == null) { |
| 2966 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 2967 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 2968 | } |
| 2969 | |
| 2970 | addErrorToDropBox("anr", app, activity, parent, annotation, cpuInfo, tracesFile, null); |
| 2971 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2972 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2973 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2974 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 2975 | 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] | 2976 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2977 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 2978 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2979 | } |
| 2980 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2981 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2982 | } |
| 2983 | } |
| 2984 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2985 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 2986 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 2987 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2988 | |
| 2989 | synchronized (this) { |
| 2990 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 2991 | Slog.w(TAG, "Killing " + app + ": background ANR"); |
| 2992 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 2993 | app.processName, app.setAdj, "background ANR"); |
| 2994 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2995 | return; |
| 2996 | } |
| 2997 | |
| 2998 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 2999 | makeAppNotRespondingLocked(app, |
| 3000 | activity != null ? activity.shortComponentName : null, |
| 3001 | annotation != null ? "ANR " + annotation : "ANR", |
| 3002 | info.toString()); |
| 3003 | |
| 3004 | // Bring up the infamous App Not Responding dialog |
| 3005 | Message msg = Message.obtain(); |
| 3006 | HashMap map = new HashMap(); |
| 3007 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3008 | msg.obj = map; |
| 3009 | map.put("app", app); |
| 3010 | if (activity != null) { |
| 3011 | map.put("activity", activity); |
| 3012 | } |
| 3013 | |
| 3014 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3015 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3016 | } |
| 3017 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3018 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3019 | if (!mLaunchWarningShown) { |
| 3020 | mLaunchWarningShown = true; |
| 3021 | mHandler.post(new Runnable() { |
| 3022 | @Override |
| 3023 | public void run() { |
| 3024 | synchronized (ActivityManagerService.this) { |
| 3025 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3026 | d.show(); |
| 3027 | mHandler.postDelayed(new Runnable() { |
| 3028 | @Override |
| 3029 | public void run() { |
| 3030 | synchronized (ActivityManagerService.this) { |
| 3031 | d.dismiss(); |
| 3032 | mLaunchWarningShown = false; |
| 3033 | } |
| 3034 | } |
| 3035 | }, 4000); |
| 3036 | } |
| 3037 | } |
| 3038 | }); |
| 3039 | } |
| 3040 | } |
| 3041 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3042 | public boolean clearApplicationUserData(final String packageName, |
| 3043 | final IPackageDataObserver observer) { |
| 3044 | int uid = Binder.getCallingUid(); |
| 3045 | int pid = Binder.getCallingPid(); |
| 3046 | long callingId = Binder.clearCallingIdentity(); |
| 3047 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3048 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3049 | int pkgUid = -1; |
| 3050 | synchronized(this) { |
| 3051 | try { |
| 3052 | pkgUid = pm.getPackageUid(packageName); |
| 3053 | } catch (RemoteException e) { |
| 3054 | } |
| 3055 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3056 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3057 | return false; |
| 3058 | } |
| 3059 | if (uid == pkgUid || checkComponentPermission( |
| 3060 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3061 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3062 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3063 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3064 | } else { |
| 3065 | throw new SecurityException(pid+" does not have permission:"+ |
| 3066 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3067 | "for process:"+packageName); |
| 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | try { |
| 3072 | //clear application user data |
| 3073 | pm.clearApplicationUserData(packageName, observer); |
| 3074 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3075 | Uri.fromParts("package", packageName, null)); |
| 3076 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3077 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
| 3078 | null, null, 0, null, null, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3079 | } catch (RemoteException e) { |
| 3080 | } |
| 3081 | } finally { |
| 3082 | Binder.restoreCallingIdentity(callingId); |
| 3083 | } |
| 3084 | return true; |
| 3085 | } |
| 3086 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3087 | public void killBackgroundProcesses(final String packageName) { |
| 3088 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3089 | != PackageManager.PERMISSION_GRANTED && |
| 3090 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3091 | != PackageManager.PERMISSION_GRANTED) { |
| 3092 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3093 | + Binder.getCallingPid() |
| 3094 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3095 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3096 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3097 | throw new SecurityException(msg); |
| 3098 | } |
| 3099 | |
| 3100 | long callingId = Binder.clearCallingIdentity(); |
| 3101 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3102 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3103 | int pkgUid = -1; |
| 3104 | synchronized(this) { |
| 3105 | try { |
| 3106 | pkgUid = pm.getPackageUid(packageName); |
| 3107 | } catch (RemoteException e) { |
| 3108 | } |
| 3109 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3110 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3111 | return; |
| 3112 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3113 | killPackageProcessesLocked(packageName, pkgUid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3114 | ProcessList.SECONDARY_SERVER_ADJ, false, true, true, false); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3115 | } |
| 3116 | } finally { |
| 3117 | Binder.restoreCallingIdentity(callingId); |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | public void forceStopPackage(final String packageName) { |
| 3122 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3123 | != PackageManager.PERMISSION_GRANTED) { |
| 3124 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3125 | + Binder.getCallingPid() |
| 3126 | + ", uid=" + Binder.getCallingUid() |
| 3127 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3128 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3129 | throw new SecurityException(msg); |
| 3130 | } |
| 3131 | |
| 3132 | long callingId = Binder.clearCallingIdentity(); |
| 3133 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3134 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3135 | int pkgUid = -1; |
| 3136 | synchronized(this) { |
| 3137 | try { |
| 3138 | pkgUid = pm.getPackageUid(packageName); |
| 3139 | } catch (RemoteException e) { |
| 3140 | } |
| 3141 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3142 | Slog.w(TAG, "Invalid packageName: " + packageName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3143 | return; |
| 3144 | } |
| 3145 | forceStopPackageLocked(packageName, pkgUid); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3146 | try { |
| 3147 | pm.setPackageStoppedState(packageName, true); |
| 3148 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 3149 | } catch (IllegalArgumentException e) { |
| 3150 | Slog.w(TAG, "Failed trying to unstop package " |
| 3151 | + packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3152 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3153 | } |
| 3154 | } finally { |
| 3155 | Binder.restoreCallingIdentity(callingId); |
| 3156 | } |
| 3157 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3158 | |
| 3159 | /* |
| 3160 | * The pkg name and uid have to be specified. |
| 3161 | * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int) |
| 3162 | */ |
| 3163 | public void killApplicationWithUid(String pkg, int uid) { |
| 3164 | if (pkg == null) { |
| 3165 | return; |
| 3166 | } |
| 3167 | // Make sure the uid is valid. |
| 3168 | if (uid < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3169 | Slog.w(TAG, "Invalid uid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3170 | return; |
| 3171 | } |
| 3172 | int callerUid = Binder.getCallingUid(); |
| 3173 | // Only the system server can kill an application |
| 3174 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3175 | // Post an aysnc message to kill the application |
| 3176 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
| 3177 | msg.arg1 = uid; |
| 3178 | msg.arg2 = 0; |
| 3179 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3180 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3181 | } else { |
| 3182 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3183 | pkg); |
| 3184 | } |
| 3185 | } |
| 3186 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3187 | public void closeSystemDialogs(String reason) { |
| 3188 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3189 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3190 | if (reason != null) { |
| 3191 | intent.putExtra("reason", reason); |
| 3192 | } |
| 3193 | |
| 3194 | final int uid = Binder.getCallingUid(); |
| 3195 | final long origId = Binder.clearCallingIdentity(); |
| 3196 | synchronized (this) { |
| 3197 | int i = mWatchers.beginBroadcast(); |
| 3198 | while (i > 0) { |
| 3199 | i--; |
| 3200 | IActivityWatcher w = mWatchers.getBroadcastItem(i); |
| 3201 | if (w != null) { |
| 3202 | try { |
| 3203 | w.closingSystemDialogs(reason); |
| 3204 | } catch (RemoteException e) { |
| 3205 | } |
| 3206 | } |
| 3207 | } |
| 3208 | mWatchers.finishBroadcast(); |
| 3209 | |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3210 | mWindowManager.closeSystemDialogs(reason); |
| 3211 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3212 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 3213 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3214 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3215 | r.stack.finishActivityLocked(r, i, |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3216 | Activity.RESULT_CANCELED, null, "close-sys"); |
| 3217 | } |
| 3218 | } |
| 3219 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3220 | broadcastIntentLocked(null, null, intent, null, |
| 3221 | null, 0, null, null, null, false, false, -1, uid); |
| 3222 | } |
| 3223 | Binder.restoreCallingIdentity(origId); |
| 3224 | } |
| 3225 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3226 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3227 | throws RemoteException { |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3228 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3229 | for (int i=pids.length-1; i>=0; i--) { |
| 3230 | infos[i] = new Debug.MemoryInfo(); |
| 3231 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3232 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3233 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3234 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3235 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3236 | public long[] getProcessPss(int[] pids) throws RemoteException { |
| 3237 | long[] pss = new long[pids.length]; |
| 3238 | for (int i=pids.length-1; i>=0; i--) { |
| 3239 | pss[i] = Debug.getPss(pids[i]); |
| 3240 | } |
| 3241 | return pss; |
| 3242 | } |
| 3243 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3244 | public void killApplicationProcess(String processName, int uid) { |
| 3245 | if (processName == null) { |
| 3246 | return; |
| 3247 | } |
| 3248 | |
| 3249 | int callerUid = Binder.getCallingUid(); |
| 3250 | // Only the system server can kill an application |
| 3251 | if (callerUid == Process.SYSTEM_UID) { |
| 3252 | synchronized (this) { |
| 3253 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3254 | if (app != null && app.thread != null) { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3255 | try { |
| 3256 | app.thread.scheduleSuicide(); |
| 3257 | } catch (RemoteException e) { |
| 3258 | // If the other end already died, then our work here is done. |
| 3259 | } |
| 3260 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3261 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3262 | + processName + " / " + uid); |
| 3263 | } |
| 3264 | } |
| 3265 | } else { |
| 3266 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3267 | processName); |
| 3268 | } |
| 3269 | } |
| 3270 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3271 | private void forceStopPackageLocked(final String packageName, int uid) { |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3272 | forceStopPackageLocked(packageName, uid, false, false, true, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3273 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3274 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3275 | if (!mProcessesReady) { |
| 3276 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3277 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3278 | intent.putExtra(Intent.EXTRA_UID, uid); |
| 3279 | broadcastIntentLocked(null, null, intent, |
| 3280 | null, null, 0, null, null, null, |
| 3281 | false, false, MY_PID, Process.SYSTEM_UID); |
| 3282 | } |
| 3283 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3284 | private final boolean killPackageProcessesLocked(String packageName, int uid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3285 | int minOomAdj, boolean callerWillRestart, boolean allowRestart, boolean doit, |
| 3286 | boolean evenPersistent) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3287 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3288 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3289 | // Remove all processes this package may have touched: all with the |
| 3290 | // same UID (except for the system or root user), and all whose name |
| 3291 | // matches the package name. |
| 3292 | final String procNamePrefix = packageName + ":"; |
| 3293 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3294 | final int NA = apps.size(); |
| 3295 | for (int ia=0; ia<NA; ia++) { |
| 3296 | ProcessRecord app = apps.valueAt(ia); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3297 | if (app.persistent && !evenPersistent) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3298 | // we don't kill persistent processes |
| 3299 | continue; |
| 3300 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3301 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3302 | if (doit) { |
| 3303 | procs.add(app); |
| 3304 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3305 | } else if ((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid) |
| 3306 | || app.processName.equals(packageName) |
| 3307 | || app.processName.startsWith(procNamePrefix)) { |
| 3308 | if (app.setAdj >= minOomAdj) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3309 | if (!doit) { |
| 3310 | return true; |
| 3311 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3312 | app.removed = true; |
| 3313 | procs.add(app); |
| 3314 | } |
| 3315 | } |
| 3316 | } |
| 3317 | } |
| 3318 | |
| 3319 | int N = procs.size(); |
| 3320 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3321 | removeProcessLocked(procs.get(i), callerWillRestart, allowRestart); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3322 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3323 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3324 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3325 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3326 | private final boolean forceStopPackageLocked(String name, int uid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3327 | boolean callerWillRestart, boolean purgeCache, boolean doit, |
| 3328 | boolean evenPersistent) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3329 | int i; |
| 3330 | int N; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3331 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3332 | if (uid < 0) { |
| 3333 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3334 | uid = AppGlobals.getPackageManager().getPackageUid(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3335 | } catch (RemoteException e) { |
| 3336 | } |
| 3337 | } |
| 3338 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3339 | if (doit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3340 | Slog.i(TAG, "Force stopping package " + name + " uid=" + uid); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3341 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3342 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3343 | while (badApps.hasNext()) { |
| 3344 | SparseArray<Long> ba = badApps.next(); |
| 3345 | if (ba.get(uid) != null) { |
| 3346 | badApps.remove(); |
| 3347 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3348 | } |
| 3349 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3350 | |
| 3351 | boolean didSomething = killPackageProcessesLocked(name, uid, -100, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3352 | callerWillRestart, false, doit, evenPersistent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3353 | |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3354 | TaskRecord lastTask = null; |
| 3355 | for (i=0; i<mMainStack.mHistory.size(); i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3356 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3357 | final boolean samePackage = r.packageName.equals(name); |
| 3358 | if ((samePackage || r.task == lastTask) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3359 | && (r.app == null || evenPersistent || !r.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3360 | if (!doit) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 3361 | if (r.finishing) { |
| 3362 | // If this activity is just finishing, then it is not |
| 3363 | // interesting as far as something to stop. |
| 3364 | continue; |
| 3365 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3366 | return true; |
| 3367 | } |
| 3368 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3369 | Slog.i(TAG, " Force finishing activity " + r); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3370 | if (samePackage) { |
| 3371 | if (r.app != null) { |
| 3372 | r.app.removed = true; |
| 3373 | } |
| 3374 | r.app = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3375 | } |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3376 | lastTask = r.task; |
| 3377 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
| 3378 | null, "force-stop")) { |
| 3379 | i--; |
| 3380 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3381 | } |
| 3382 | } |
| 3383 | |
| 3384 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
| 3385 | for (ServiceRecord service : mServices.values()) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3386 | if (service.packageName.equals(name) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3387 | && (service.app == null || evenPersistent || !service.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3388 | if (!doit) { |
| 3389 | return true; |
| 3390 | } |
| 3391 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3392 | Slog.i(TAG, " Force stopping service " + service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3393 | if (service.app != null) { |
| 3394 | service.app.removed = true; |
| 3395 | } |
| 3396 | service.app = null; |
| 3397 | services.add(service); |
| 3398 | } |
| 3399 | } |
| 3400 | |
| 3401 | N = services.size(); |
| 3402 | for (i=0; i<N; i++) { |
| 3403 | bringDownServiceLocked(services.get(i), true); |
| 3404 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3405 | |
| 3406 | ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); |
| 3407 | for (ContentProviderRecord provider : mProvidersByClass.values()) { |
| 3408 | if (provider.info.packageName.equals(name) |
| 3409 | && (provider.proc == null || evenPersistent || !provider.proc.persistent)) { |
| 3410 | if (!doit) { |
| 3411 | return true; |
| 3412 | } |
| 3413 | didSomething = true; |
| 3414 | providers.add(provider); |
| 3415 | } |
| 3416 | } |
| 3417 | |
| 3418 | N = providers.size(); |
| 3419 | for (i=0; i<N; i++) { |
| 3420 | removeDyingProviderLocked(null, providers.get(i)); |
| 3421 | } |
| 3422 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3423 | if (doit) { |
| 3424 | if (purgeCache) { |
| 3425 | AttributeCache ac = AttributeCache.instance(); |
| 3426 | if (ac != null) { |
| 3427 | ac.removePackage(name); |
| 3428 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3429 | } |
Dianne Hackborn | 38cc896 | 2011-10-13 11:33:55 -0700 | [diff] [blame^] | 3430 | if (mBooted) { |
| 3431 | mMainStack.resumeTopActivityLocked(null); |
| 3432 | mMainStack.scheduleIdleLocked(); |
| 3433 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3434 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3435 | |
| 3436 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3437 | } |
| 3438 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3439 | private final boolean removeProcessLocked(ProcessRecord app, |
| 3440 | boolean callerWillRestart, boolean allowRestart) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3441 | final String name = app.processName; |
| 3442 | final int uid = app.info.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3443 | if (DEBUG_PROCESSES) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3444 | TAG, "Force removing process " + app + " (" + name |
| 3445 | + "/" + uid + ")"); |
| 3446 | |
| 3447 | mProcessNames.remove(name, uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3448 | if (mHeavyWeightProcess == app) { |
| 3449 | mHeavyWeightProcess = null; |
| 3450 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3451 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3452 | boolean needRestart = false; |
| 3453 | if (app.pid > 0 && app.pid != MY_PID) { |
| 3454 | int pid = app.pid; |
| 3455 | synchronized (mPidsSelfLocked) { |
| 3456 | mPidsSelfLocked.remove(pid); |
| 3457 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3458 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3459 | handleAppDiedLocked(app, true, allowRestart); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3460 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3461 | Process.killProcess(pid); |
| 3462 | |
| 3463 | if (app.persistent) { |
| 3464 | if (!callerWillRestart) { |
| 3465 | addAppLocked(app.info); |
| 3466 | } else { |
| 3467 | needRestart = true; |
| 3468 | } |
| 3469 | } |
| 3470 | } else { |
| 3471 | mRemovedProcesses.add(app); |
| 3472 | } |
| 3473 | |
| 3474 | return needRestart; |
| 3475 | } |
| 3476 | |
| 3477 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 3478 | final int pid = app.pid; |
| 3479 | boolean gone = false; |
| 3480 | synchronized (mPidsSelfLocked) { |
| 3481 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 3482 | if (knownApp != null && knownApp.thread == null) { |
| 3483 | mPidsSelfLocked.remove(pid); |
| 3484 | gone = true; |
| 3485 | } |
| 3486 | } |
| 3487 | |
| 3488 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3489 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3490 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.info.uid, |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3491 | app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3492 | mProcessNames.remove(app.processName, app.info.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3493 | if (mHeavyWeightProcess == app) { |
| 3494 | mHeavyWeightProcess = null; |
| 3495 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3496 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3497 | // Take care of any launching providers waiting for this process. |
| 3498 | checkAppInLaunchingProvidersLocked(app, true); |
| 3499 | // Take care of any services that are waiting for the process. |
| 3500 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3501 | ServiceRecord sr = mPendingServices.get(i); |
| 3502 | if (app.info.uid == sr.appInfo.uid |
| 3503 | && app.processName.equals(sr.processName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3504 | Slog.w(TAG, "Forcing bringing down service: " + sr); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3505 | mPendingServices.remove(i); |
| 3506 | i--; |
| 3507 | bringDownServiceLocked(sr, true); |
| 3508 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3509 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3510 | EventLog.writeEvent(EventLogTags.AM_KILL, pid, |
| 3511 | app.processName, app.setAdj, "start timeout"); |
| 3512 | Process.killProcessQuiet(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3513 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3514 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3515 | try { |
| 3516 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 3517 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 3518 | bm.agentDisconnected(app.info.packageName); |
| 3519 | } catch (RemoteException e) { |
| 3520 | // Can't happen; the backup manager is local |
| 3521 | } |
| 3522 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3523 | if (mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3524 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3525 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 3526 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3527 | mPendingBroadcast = null; |
| 3528 | scheduleBroadcastsLocked(); |
| 3529 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3530 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3531 | 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] | 3532 | } |
| 3533 | } |
| 3534 | |
| 3535 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 3536 | int pid) { |
| 3537 | |
| 3538 | // Find the application record that is being attached... either via |
| 3539 | // the pid if we are running in multiple processes, or just pull the |
| 3540 | // next app record if we are emulating process with anonymous threads. |
| 3541 | ProcessRecord app; |
| 3542 | if (pid != MY_PID && pid >= 0) { |
| 3543 | synchronized (mPidsSelfLocked) { |
| 3544 | app = mPidsSelfLocked.get(pid); |
| 3545 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3546 | } else { |
| 3547 | app = null; |
| 3548 | } |
| 3549 | |
| 3550 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3551 | 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] | 3552 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3553 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3554 | if (pid > 0 && pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3555 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3556 | } else { |
| 3557 | try { |
| 3558 | thread.scheduleExit(); |
| 3559 | } catch (Exception e) { |
| 3560 | // Ignore exceptions. |
| 3561 | } |
| 3562 | } |
| 3563 | return false; |
| 3564 | } |
| 3565 | |
| 3566 | // If this application record is still attached to a previous |
| 3567 | // process, clean it up now. |
| 3568 | if (app.thread != null) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3569 | handleAppDiedLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | // Tell the process all about itself. |
| 3573 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3574 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3575 | TAG, "Binding process pid " + pid + " to record " + app); |
| 3576 | |
| 3577 | String processName = app.processName; |
| 3578 | try { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 3579 | AppDeathRecipient adr = new AppDeathRecipient( |
| 3580 | app, pid, thread); |
| 3581 | thread.asBinder().linkToDeath(adr, 0); |
| 3582 | app.deathRecipient = adr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3583 | } catch (RemoteException e) { |
| 3584 | app.resetPackageList(); |
| 3585 | startProcessLocked(app, "link fail", processName); |
| 3586 | return false; |
| 3587 | } |
| 3588 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3589 | 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] | 3590 | |
| 3591 | app.thread = thread; |
| 3592 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 3593 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 3594 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3595 | app.forcingToForeground = null; |
| 3596 | app.foregroundServices = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 3597 | app.hasShownUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3598 | app.debugging = false; |
| 3599 | |
| 3600 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3601 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3602 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3603 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3604 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3605 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3606 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3607 | } |
| 3608 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3609 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3610 | TAG, "New app record " + app |
| 3611 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 3612 | try { |
| 3613 | int testMode = IApplicationThread.DEBUG_OFF; |
| 3614 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 3615 | testMode = mWaitForDebugger |
| 3616 | ? IApplicationThread.DEBUG_WAIT |
| 3617 | : IApplicationThread.DEBUG_ON; |
| 3618 | app.debugging = true; |
| 3619 | if (mDebugTransient) { |
| 3620 | mDebugApp = mOrigDebugApp; |
| 3621 | mWaitForDebugger = mOrigWaitForDebugger; |
| 3622 | } |
| 3623 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3624 | String profileFile = app.instrumentationProfileFile; |
| 3625 | ParcelFileDescriptor profileFd = null; |
| 3626 | boolean profileAutoStop = false; |
| 3627 | if (mProfileApp != null && mProfileApp.equals(processName)) { |
| 3628 | mProfileProc = app; |
| 3629 | profileFile = mProfileFile; |
| 3630 | profileFd = mProfileFd; |
| 3631 | profileAutoStop = mAutoStopProfiler; |
| 3632 | } |
| 3633 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3634 | // If the app is being launched for restore or full backup, set it up specially |
| 3635 | boolean isRestrictedBackupMode = false; |
| 3636 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 3637 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3638 | || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3639 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 3640 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3641 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 3642 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 3643 | ? app.instrumentationInfo.packageName |
| 3644 | : app.info.packageName); |
| 3645 | if (app.instrumentationClass != null) { |
| 3646 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3647 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3648 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 3649 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3650 | ApplicationInfo appInfo = app.instrumentationInfo != null |
| 3651 | ? app.instrumentationInfo : app.info; |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 3652 | app.compat = compatibilityInfoForPackageLocked(appInfo); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3653 | if (profileFd != null) { |
| 3654 | profileFd = profileFd.dup(); |
| 3655 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3656 | thread.bindApplication(processName, appInfo, providers, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3657 | app.instrumentationClass, profileFile, profileFd, profileAutoStop, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3658 | app.instrumentationArguments, app.instrumentationWatcher, testMode, |
Dianne Hackborn | 5d927c2 | 2011-09-02 12:22:18 -0700 | [diff] [blame] | 3659 | isRestrictedBackupMode || !normalMode, app.persistent, |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 3660 | mConfiguration, app.compat, getCommonServicesLocked(), |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 3661 | mCoreSettingsObserver.getCoreSettingsLocked()); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3662 | updateLruProcessLocked(app, false, true); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3663 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3664 | } catch (Exception e) { |
| 3665 | // todo: Yikes! What should we do? For now we will try to |
| 3666 | // start another process, but that could easily get us in |
| 3667 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3668 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3669 | |
| 3670 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 3671 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3672 | startProcessLocked(app, "bind fail", processName); |
| 3673 | return false; |
| 3674 | } |
| 3675 | |
| 3676 | // Remove this record from the list of starting applications. |
| 3677 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3678 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 3679 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3680 | mProcessesOnHold.remove(app); |
| 3681 | |
| 3682 | boolean badApp = false; |
| 3683 | boolean didSomething = false; |
| 3684 | |
| 3685 | // 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] | 3686 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 3687 | if (hr != null && normalMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3688 | if (hr.app == null && app.info.uid == hr.info.applicationInfo.uid |
| 3689 | && processName.equals(hr.processName)) { |
| 3690 | try { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3691 | if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3692 | didSomething = true; |
| 3693 | } |
| 3694 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3695 | 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] | 3696 | + hr.intent.getComponent().flattenToShortString(), e); |
| 3697 | badApp = true; |
| 3698 | } |
| 3699 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3700 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3701 | } |
| 3702 | } |
| 3703 | |
| 3704 | // Find any services that should be running in this process... |
| 3705 | if (!badApp && mPendingServices.size() > 0) { |
| 3706 | ServiceRecord sr = null; |
| 3707 | try { |
| 3708 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3709 | sr = mPendingServices.get(i); |
| 3710 | if (app.info.uid != sr.appInfo.uid |
| 3711 | || !processName.equals(sr.processName)) { |
| 3712 | continue; |
| 3713 | } |
| 3714 | |
| 3715 | mPendingServices.remove(i); |
| 3716 | i--; |
| 3717 | realStartServiceLocked(sr, app); |
| 3718 | didSomething = true; |
| 3719 | } |
| 3720 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3721 | 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] | 3722 | + sr.shortName, e); |
| 3723 | badApp = true; |
| 3724 | } |
| 3725 | } |
| 3726 | |
| 3727 | // Check if the next broadcast receiver is in this process... |
| 3728 | BroadcastRecord br = mPendingBroadcast; |
| 3729 | if (!badApp && br != null && br.curApp == app) { |
| 3730 | try { |
| 3731 | mPendingBroadcast = null; |
| 3732 | processCurBroadcastLocked(br, app); |
| 3733 | didSomething = true; |
| 3734 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3735 | 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] | 3736 | + br.curComponent.flattenToShortString(), e); |
| 3737 | badApp = true; |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 3738 | logBroadcastReceiverDiscardLocked(br); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3739 | finishReceiverLocked(br.receiver, br.resultCode, br.resultData, |
| 3740 | br.resultExtras, br.resultAbort, true); |
| 3741 | scheduleBroadcastsLocked(); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3742 | // We need to reset the state if we fails to start the receiver. |
| 3743 | br.state = BroadcastRecord.IDLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3744 | } |
| 3745 | } |
| 3746 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3747 | // Check whether the next backup agent is in this process... |
| 3748 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.info.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3749 | 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] | 3750 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3751 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3752 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, |
| 3753 | compatibilityInfoForPackageLocked(mBackupTarget.appInfo), |
| 3754 | mBackupTarget.backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3755 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3756 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3757 | e.printStackTrace(); |
| 3758 | } |
| 3759 | } |
| 3760 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3761 | if (badApp) { |
| 3762 | // todo: Also need to kill application to deal with all |
| 3763 | // kinds of exceptions. |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3764 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3765 | return false; |
| 3766 | } |
| 3767 | |
| 3768 | if (!didSomething) { |
| 3769 | updateOomAdjLocked(); |
| 3770 | } |
| 3771 | |
| 3772 | return true; |
| 3773 | } |
| 3774 | |
| 3775 | public final void attachApplication(IApplicationThread thread) { |
| 3776 | synchronized (this) { |
| 3777 | int callingPid = Binder.getCallingPid(); |
| 3778 | final long origId = Binder.clearCallingIdentity(); |
| 3779 | attachApplicationLocked(thread, callingPid); |
| 3780 | Binder.restoreCallingIdentity(origId); |
| 3781 | } |
| 3782 | } |
| 3783 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3784 | public final void activityIdle(IBinder token, Configuration config, boolean stopProfiling) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3785 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3786 | ActivityRecord r = mMainStack.activityIdleInternal(token, false, config); |
| 3787 | if (stopProfiling) { |
| 3788 | synchronized (this) { |
| 3789 | if (mProfileProc == r.app) { |
| 3790 | if (mProfileFd != null) { |
| 3791 | try { |
| 3792 | mProfileFd.close(); |
| 3793 | } catch (IOException e) { |
| 3794 | } |
| 3795 | clearProfilerLocked(); |
| 3796 | } |
| 3797 | } |
| 3798 | } |
| 3799 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3800 | Binder.restoreCallingIdentity(origId); |
| 3801 | } |
| 3802 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3803 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3804 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3805 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3806 | mWindowManager.enableScreenAfterBoot(); |
| 3807 | } |
| 3808 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3809 | public void showBootMessage(final CharSequence msg, final boolean always) { |
| 3810 | mWindowManager.showBootMessage(msg, always); |
| 3811 | } |
| 3812 | |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 3813 | public void dismissKeyguardOnNextActivity() { |
| 3814 | synchronized (this) { |
| 3815 | mMainStack.dismissKeyguardOnNextActivityLocked(); |
| 3816 | } |
| 3817 | } |
| 3818 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3819 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3820 | IntentFilter pkgFilter = new IntentFilter(); |
| 3821 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 3822 | pkgFilter.addDataScheme("package"); |
| 3823 | mContext.registerReceiver(new BroadcastReceiver() { |
| 3824 | @Override |
| 3825 | public void onReceive(Context context, Intent intent) { |
| 3826 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 3827 | if (pkgs != null) { |
| 3828 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 3829 | synchronized (ActivityManagerService.this) { |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3830 | if (forceStopPackageLocked(pkg, -1, false, false, false, false)) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 3831 | setResultCode(Activity.RESULT_OK); |
| 3832 | return; |
| 3833 | } |
| 3834 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3835 | } |
| 3836 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3837 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3838 | }, pkgFilter); |
| 3839 | |
| 3840 | synchronized (this) { |
| 3841 | // Ensure that any processes we had put on hold are now started |
| 3842 | // up. |
| 3843 | final int NP = mProcessesOnHold.size(); |
| 3844 | if (NP > 0) { |
| 3845 | ArrayList<ProcessRecord> procs = |
| 3846 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 3847 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3848 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 3849 | + procs.get(ip)); |
| 3850 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3851 | } |
| 3852 | } |
| 3853 | |
| 3854 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3855 | // Start looking for apps that are abusing wake locks. |
| 3856 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3857 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3858 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 3859 | SystemProperties.set("sys.boot_completed", "1"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3860 | broadcastIntentLocked(null, null, |
| 3861 | new Intent(Intent.ACTION_BOOT_COMPLETED, null), |
| 3862 | null, null, 0, null, null, |
| 3863 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
| 3864 | false, false, MY_PID, Process.SYSTEM_UID); |
| 3865 | } |
| 3866 | } |
| 3867 | } |
| 3868 | |
| 3869 | final void ensureBootCompleted() { |
| 3870 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 3871 | boolean enableScreen; |
| 3872 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3873 | booting = mBooting; |
| 3874 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 3875 | enableScreen = !mBooted; |
| 3876 | mBooted = true; |
| 3877 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3878 | |
| 3879 | if (booting) { |
| 3880 | finishBooting(); |
| 3881 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 3882 | |
| 3883 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 3884 | enableScreenAfterBoot(); |
| 3885 | } |
| 3886 | } |
| 3887 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 3888 | public final void activityPaused(IBinder token) { |
| 3889 | final long origId = Binder.clearCallingIdentity(); |
| 3890 | mMainStack.activityPaused(token, false); |
| 3891 | Binder.restoreCallingIdentity(origId); |
| 3892 | } |
| 3893 | |
| 3894 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 3895 | CharSequence description) { |
| 3896 | if (localLOGV) Slog.v( |
| 3897 | TAG, "Activity stopped: token=" + token); |
| 3898 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3899 | // Refuse possible leaked file descriptors |
| 3900 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 3901 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 3902 | } |
| 3903 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3904 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3905 | |
| 3906 | final long origId = Binder.clearCallingIdentity(); |
| 3907 | |
| 3908 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3909 | r = mMainStack.isInStackLocked(token); |
| 3910 | if (r != null) { |
| 3911 | r.stack.activityStoppedLocked(r, icicle, thumbnail, description); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3912 | } |
| 3913 | } |
| 3914 | |
| 3915 | if (r != null) { |
| 3916 | sendPendingThumbnail(r, null, null, null, false); |
| 3917 | } |
| 3918 | |
| 3919 | trimApplications(); |
| 3920 | |
| 3921 | Binder.restoreCallingIdentity(origId); |
| 3922 | } |
| 3923 | |
| 3924 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3925 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3926 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | public String getCallingPackage(IBinder token) { |
| 3930 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3931 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 3932 | 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] | 3933 | } |
| 3934 | } |
| 3935 | |
| 3936 | public ComponentName getCallingActivity(IBinder token) { |
| 3937 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3938 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3939 | return r != null ? r.intent.getComponent() : null; |
| 3940 | } |
| 3941 | } |
| 3942 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3943 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3944 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 3945 | if (r == null) { |
| 3946 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3947 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3948 | return r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3949 | } |
| 3950 | |
| 3951 | public ComponentName getActivityClassForToken(IBinder token) { |
| 3952 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3953 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 3954 | if (r == null) { |
| 3955 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3956 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3957 | return r.intent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | public String getPackageForToken(IBinder token) { |
| 3962 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3963 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 3964 | if (r == null) { |
| 3965 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3966 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 3967 | return r.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | public IIntentSender getIntentSender(int type, |
| 3972 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3973 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3974 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3975 | if (intents != null) { |
| 3976 | if (intents.length < 1) { |
| 3977 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 3978 | } |
| 3979 | for (int i=0; i<intents.length; i++) { |
| 3980 | Intent intent = intents[i]; |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 3981 | if (intent != null) { |
| 3982 | if (intent.hasFileDescriptors()) { |
| 3983 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 3984 | } |
| 3985 | if (type == INTENT_SENDER_BROADCAST && |
| 3986 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 3987 | throw new IllegalArgumentException( |
| 3988 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 3989 | } |
| 3990 | intents[i] = new Intent(intent); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3991 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3992 | } |
| 3993 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3994 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3995 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3996 | } |
| 3997 | } |
| 3998 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3999 | synchronized(this) { |
| 4000 | int callingUid = Binder.getCallingUid(); |
| 4001 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4002 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4003 | int uid = AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4004 | .getPackageUid(packageName); |
| 4005 | if (uid != Binder.getCallingUid()) { |
| 4006 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 4007 | + Binder.getCallingPid() |
| 4008 | + ", uid=" + Binder.getCallingUid() |
| 4009 | + ", (need uid=" + uid + ")" |
| 4010 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4011 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4012 | throw new SecurityException(msg); |
| 4013 | } |
| 4014 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4015 | |
| 4016 | return getIntentSenderLocked(type, packageName, callingUid, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4017 | token, resultWho, requestCode, intents, resolvedTypes, flags); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4018 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4019 | } catch (RemoteException e) { |
| 4020 | throw new SecurityException(e); |
| 4021 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4022 | } |
| 4023 | } |
| 4024 | |
| 4025 | IIntentSender getIntentSenderLocked(int type, |
| 4026 | String packageName, int callingUid, IBinder token, String resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4027 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4028 | ActivityRecord activity = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4029 | if (type == INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4030 | activity = mMainStack.isInStackLocked(token); |
| 4031 | if (activity == null) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4032 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4033 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4034 | if (activity.finishing) { |
| 4035 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4036 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 4040 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 4041 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 4042 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 4043 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 4044 | |
| 4045 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 4046 | type, packageName, activity, resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4047 | requestCode, intents, resolvedTypes, flags); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4048 | WeakReference<PendingIntentRecord> ref; |
| 4049 | ref = mIntentSenderRecords.get(key); |
| 4050 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 4051 | if (rec != null) { |
| 4052 | if (!cancelCurrent) { |
| 4053 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4054 | if (rec.key.requestIntent != null) { |
| 4055 | rec.key.requestIntent.replaceExtras(intents != null ? intents[0] : null); |
| 4056 | } |
| 4057 | if (intents != null) { |
| 4058 | intents[intents.length-1] = rec.key.requestIntent; |
| 4059 | rec.key.allIntents = intents; |
| 4060 | rec.key.allResolvedTypes = resolvedTypes; |
| 4061 | } else { |
| 4062 | rec.key.allIntents = null; |
| 4063 | rec.key.allResolvedTypes = null; |
| 4064 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4065 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4066 | return rec; |
| 4067 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4068 | rec.canceled = true; |
| 4069 | mIntentSenderRecords.remove(key); |
| 4070 | } |
| 4071 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4072 | return rec; |
| 4073 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4074 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4075 | mIntentSenderRecords.put(key, rec.ref); |
| 4076 | if (type == INTENT_SENDER_ACTIVITY_RESULT) { |
| 4077 | if (activity.pendingResults == null) { |
| 4078 | activity.pendingResults |
| 4079 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4080 | } |
| 4081 | activity.pendingResults.add(rec.ref); |
| 4082 | } |
| 4083 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | public void cancelIntentSender(IIntentSender sender) { |
| 4087 | if (!(sender instanceof PendingIntentRecord)) { |
| 4088 | return; |
| 4089 | } |
| 4090 | synchronized(this) { |
| 4091 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4092 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4093 | int uid = AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4094 | .getPackageUid(rec.key.packageName); |
| 4095 | if (uid != Binder.getCallingUid()) { |
| 4096 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4097 | + Binder.getCallingPid() |
| 4098 | + ", uid=" + Binder.getCallingUid() |
| 4099 | + " is not allowed to cancel packges " |
| 4100 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4101 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4102 | throw new SecurityException(msg); |
| 4103 | } |
| 4104 | } catch (RemoteException e) { |
| 4105 | throw new SecurityException(e); |
| 4106 | } |
| 4107 | cancelIntentSenderLocked(rec, true); |
| 4108 | } |
| 4109 | } |
| 4110 | |
| 4111 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4112 | rec.canceled = true; |
| 4113 | mIntentSenderRecords.remove(rec.key); |
| 4114 | if (cleanActivity && rec.key.activity != null) { |
| 4115 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4116 | } |
| 4117 | } |
| 4118 | |
| 4119 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4120 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4121 | return null; |
| 4122 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4123 | try { |
| 4124 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4125 | return res.key.packageName; |
| 4126 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4127 | } |
| 4128 | return null; |
| 4129 | } |
| 4130 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 4131 | public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) { |
| 4132 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4133 | return false; |
| 4134 | } |
| 4135 | try { |
| 4136 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4137 | if (res.key.allIntents == null) { |
| 4138 | return false; |
| 4139 | } |
| 4140 | for (int i=0; i<res.key.allIntents.length; i++) { |
| 4141 | Intent intent = res.key.allIntents[i]; |
| 4142 | if (intent.getPackage() != null && intent.getComponent() != null) { |
| 4143 | return false; |
| 4144 | } |
| 4145 | } |
| 4146 | return true; |
| 4147 | } catch (ClassCastException e) { |
| 4148 | } |
| 4149 | return false; |
| 4150 | } |
| 4151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4152 | public void setProcessLimit(int max) { |
| 4153 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4154 | "setProcessLimit()"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4155 | synchronized (this) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4156 | mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4157 | mProcessLimitOverride = max; |
| 4158 | } |
| 4159 | trimApplications(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | public int getProcessLimit() { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4163 | synchronized (this) { |
| 4164 | return mProcessLimitOverride; |
| 4165 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4166 | } |
| 4167 | |
| 4168 | void foregroundTokenDied(ForegroundToken token) { |
| 4169 | synchronized (ActivityManagerService.this) { |
| 4170 | synchronized (mPidsSelfLocked) { |
| 4171 | ForegroundToken cur |
| 4172 | = mForegroundProcesses.get(token.pid); |
| 4173 | if (cur != token) { |
| 4174 | return; |
| 4175 | } |
| 4176 | mForegroundProcesses.remove(token.pid); |
| 4177 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4178 | if (pr == null) { |
| 4179 | return; |
| 4180 | } |
| 4181 | pr.forcingToForeground = null; |
| 4182 | pr.foregroundServices = false; |
| 4183 | } |
| 4184 | updateOomAdjLocked(); |
| 4185 | } |
| 4186 | } |
| 4187 | |
| 4188 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4189 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4190 | "setProcessForeground()"); |
| 4191 | synchronized(this) { |
| 4192 | boolean changed = false; |
| 4193 | |
| 4194 | synchronized (mPidsSelfLocked) { |
| 4195 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
| 4196 | if (pr == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4197 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4198 | return; |
| 4199 | } |
| 4200 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4201 | if (oldToken != null) { |
| 4202 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4203 | mForegroundProcesses.remove(pid); |
| 4204 | pr.forcingToForeground = null; |
| 4205 | changed = true; |
| 4206 | } |
| 4207 | if (isForeground && token != null) { |
| 4208 | ForegroundToken newToken = new ForegroundToken() { |
| 4209 | public void binderDied() { |
| 4210 | foregroundTokenDied(this); |
| 4211 | } |
| 4212 | }; |
| 4213 | newToken.pid = pid; |
| 4214 | newToken.token = token; |
| 4215 | try { |
| 4216 | token.linkToDeath(newToken, 0); |
| 4217 | mForegroundProcesses.put(pid, newToken); |
| 4218 | pr.forcingToForeground = token; |
| 4219 | changed = true; |
| 4220 | } catch (RemoteException e) { |
| 4221 | // If the process died while doing this, we will later |
| 4222 | // do the cleanup with the process death link. |
| 4223 | } |
| 4224 | } |
| 4225 | } |
| 4226 | |
| 4227 | if (changed) { |
| 4228 | updateOomAdjLocked(); |
| 4229 | } |
| 4230 | } |
| 4231 | } |
| 4232 | |
| 4233 | // ========================================================= |
| 4234 | // PERMISSIONS |
| 4235 | // ========================================================= |
| 4236 | |
| 4237 | static class PermissionController extends IPermissionController.Stub { |
| 4238 | ActivityManagerService mActivityManagerService; |
| 4239 | PermissionController(ActivityManagerService activityManagerService) { |
| 4240 | mActivityManagerService = activityManagerService; |
| 4241 | } |
| 4242 | |
| 4243 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4244 | return mActivityManagerService.checkPermission(permission, pid, |
| 4245 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4246 | } |
| 4247 | } |
| 4248 | |
| 4249 | /** |
| 4250 | * This can be called with or without the global lock held. |
| 4251 | */ |
| 4252 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4253 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4254 | // We might be performing an operation on behalf of an indirect binder |
| 4255 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4256 | // client identity accordingly before proceeding. |
| 4257 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4258 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4259 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4260 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4261 | uid = tlsIdentity.uid; |
| 4262 | pid = tlsIdentity.pid; |
| 4263 | } |
| 4264 | |
| 4265 | // Root, system server and our own process get to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4266 | if (uid == 0 || uid == Process.SYSTEM_UID || pid == MY_PID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4267 | return PackageManager.PERMISSION_GRANTED; |
| 4268 | } |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4269 | // If there is a uid that owns whatever is being accessed, it has |
| 4270 | // blanket access to it regardless of the permissions it requires. |
| 4271 | if (owningUid >= 0 && uid == owningUid) { |
| 4272 | return PackageManager.PERMISSION_GRANTED; |
| 4273 | } |
| 4274 | // If the target is not exported, then nobody else can get to it. |
| 4275 | if (!exported) { |
| 4276 | Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4277 | return PackageManager.PERMISSION_DENIED; |
| 4278 | } |
| 4279 | if (permission == null) { |
| 4280 | return PackageManager.PERMISSION_GRANTED; |
| 4281 | } |
| 4282 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4283 | return AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4284 | .checkUidPermission(permission, uid); |
| 4285 | } catch (RemoteException e) { |
| 4286 | // Should never happen, but if it does... deny! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4287 | Slog.e(TAG, "PackageManager is dead?!?", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4288 | } |
| 4289 | return PackageManager.PERMISSION_DENIED; |
| 4290 | } |
| 4291 | |
| 4292 | /** |
| 4293 | * As the only public entry point for permissions checking, this method |
| 4294 | * can enforce the semantic that requesting a check on a null global |
| 4295 | * permission is automatically denied. (Internally a null permission |
| 4296 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4297 | * when only uid-based security is needed.) |
| 4298 | * |
| 4299 | * This can be called with or without the global lock held. |
| 4300 | */ |
| 4301 | public int checkPermission(String permission, int pid, int uid) { |
| 4302 | if (permission == null) { |
| 4303 | return PackageManager.PERMISSION_DENIED; |
| 4304 | } |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4305 | return checkComponentPermission(permission, pid, uid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | /** |
| 4309 | * Binder IPC calls go through the public entry point. |
| 4310 | * This can be called with or without the global lock held. |
| 4311 | */ |
| 4312 | int checkCallingPermission(String permission) { |
| 4313 | return checkPermission(permission, |
| 4314 | Binder.getCallingPid(), |
| 4315 | Binder.getCallingUid()); |
| 4316 | } |
| 4317 | |
| 4318 | /** |
| 4319 | * This can be called with or without the global lock held. |
| 4320 | */ |
| 4321 | void enforceCallingPermission(String permission, String func) { |
| 4322 | if (checkCallingPermission(permission) |
| 4323 | == PackageManager.PERMISSION_GRANTED) { |
| 4324 | return; |
| 4325 | } |
| 4326 | |
| 4327 | String msg = "Permission Denial: " + func + " from pid=" |
| 4328 | + Binder.getCallingPid() |
| 4329 | + ", uid=" + Binder.getCallingUid() |
| 4330 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4331 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4332 | throw new SecurityException(msg); |
| 4333 | } |
| 4334 | |
| 4335 | private final boolean checkHoldingPermissionsLocked(IPackageManager pm, |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4336 | ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
| 4337 | boolean readPerm = (modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 4338 | boolean writePerm = (modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0; |
| 4339 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4340 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4341 | try { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4342 | // Is the component private from the target uid? |
| 4343 | final boolean prv = !pi.exported && pi.applicationInfo.uid != uid; |
| 4344 | |
| 4345 | // Acceptable if the there is no read permission needed from the |
| 4346 | // target or the target is holding the read permission. |
| 4347 | if (!readPerm) { |
| 4348 | if ((!prv && pi.readPermission == null) || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4349 | (pm.checkUidPermission(pi.readPermission, uid) |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4350 | == PackageManager.PERMISSION_GRANTED)) { |
| 4351 | readPerm = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4352 | } |
| 4353 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4354 | |
| 4355 | // Acceptable if the there is no write permission needed from the |
| 4356 | // target or the target is holding the read permission. |
| 4357 | if (!writePerm) { |
| 4358 | if (!prv && (pi.writePermission == null) || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4359 | (pm.checkUidPermission(pi.writePermission, uid) |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4360 | == PackageManager.PERMISSION_GRANTED)) { |
| 4361 | writePerm = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4362 | } |
| 4363 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4364 | |
| 4365 | // Acceptable if there is a path permission matching the URI that |
| 4366 | // the target holds the permission on. |
| 4367 | PathPermission[] pps = pi.pathPermissions; |
| 4368 | if (pps != null && (!readPerm || !writePerm)) { |
| 4369 | final String path = uri.getPath(); |
| 4370 | int i = pps.length; |
| 4371 | while (i > 0 && (!readPerm || !writePerm)) { |
| 4372 | i--; |
| 4373 | PathPermission pp = pps[i]; |
| 4374 | if (!readPerm) { |
| 4375 | final String pprperm = pp.getReadPermission(); |
| 4376 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 4377 | + pprperm + " for " + pp.getPath() |
| 4378 | + ": match=" + pp.match(path) |
| 4379 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 4380 | if (pprperm != null && pp.match(path) && |
| 4381 | (pm.checkUidPermission(pprperm, uid) |
| 4382 | == PackageManager.PERMISSION_GRANTED)) { |
| 4383 | readPerm = true; |
| 4384 | } |
| 4385 | } |
| 4386 | if (!writePerm) { |
| 4387 | final String ppwperm = pp.getWritePermission(); |
| 4388 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 4389 | + ppwperm + " for " + pp.getPath() |
| 4390 | + ": match=" + pp.match(path) |
| 4391 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 4392 | if (ppwperm != null && pp.match(path) && |
| 4393 | (pm.checkUidPermission(ppwperm, uid) |
| 4394 | == PackageManager.PERMISSION_GRANTED)) { |
| 4395 | writePerm = true; |
| 4396 | } |
| 4397 | } |
| 4398 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4399 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4400 | } catch (RemoteException e) { |
| 4401 | return false; |
| 4402 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4403 | |
| 4404 | return readPerm && writePerm; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4405 | } |
| 4406 | |
| 4407 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 4408 | int modeFlags) { |
| 4409 | // Root gets to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4410 | if (uid == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4411 | return true; |
| 4412 | } |
| 4413 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 4414 | if (perms == null) return false; |
| 4415 | UriPermission perm = perms.get(uri); |
| 4416 | if (perm == null) return false; |
| 4417 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 4418 | } |
| 4419 | |
| 4420 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
| 4421 | // Another redirected-binder-call permissions check as in |
| 4422 | // {@link checkComponentPermission}. |
| 4423 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4424 | if (tlsIdentity != null) { |
| 4425 | uid = tlsIdentity.uid; |
| 4426 | pid = tlsIdentity.pid; |
| 4427 | } |
| 4428 | |
| 4429 | // Our own process gets to do everything. |
| 4430 | if (pid == MY_PID) { |
| 4431 | return PackageManager.PERMISSION_GRANTED; |
| 4432 | } |
| 4433 | synchronized(this) { |
| 4434 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 4435 | ? PackageManager.PERMISSION_GRANTED |
| 4436 | : PackageManager.PERMISSION_DENIED; |
| 4437 | } |
| 4438 | } |
| 4439 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4440 | /** |
| 4441 | * Check if the targetPkg can be granted permission to access uri by |
| 4442 | * the callingUid using the given modeFlags. Throws a security exception |
| 4443 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 4444 | * if the URI permission grant should be performed; returns -1 if it is not |
| 4445 | * needed (for example targetPkg already has permission to access the URI). |
| 4446 | */ |
| 4447 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
| 4448 | Uri uri, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4449 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4450 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4451 | if (modeFlags == 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4452 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4453 | } |
| 4454 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4455 | if (targetPkg != null) { |
| 4456 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4457 | "Checking grant " + targetPkg + " permission to " + uri); |
| 4458 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4459 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4460 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4461 | |
| 4462 | // If this is not a content: uri, we can't do anything with it. |
| 4463 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4464 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4465 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4466 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4467 | } |
| 4468 | |
| 4469 | String name = uri.getAuthority(); |
| 4470 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4471 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4472 | if (cpr != null) { |
| 4473 | pi = cpr.info; |
| 4474 | } else { |
| 4475 | try { |
| 4476 | pi = pm.resolveContentProvider(name, |
| 4477 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4478 | } catch (RemoteException ex) { |
| 4479 | } |
| 4480 | } |
| 4481 | if (pi == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4482 | Slog.w(TAG, "No content provider found for: " + name); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4483 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4484 | } |
| 4485 | |
| 4486 | int targetUid; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4487 | if (targetPkg != null) { |
| 4488 | try { |
| 4489 | targetUid = pm.getPackageUid(targetPkg); |
| 4490 | if (targetUid < 0) { |
| 4491 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4492 | "Can't grant URI permission no uid for: " + targetPkg); |
| 4493 | return -1; |
| 4494 | } |
| 4495 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4496 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4497 | } |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4498 | } else { |
| 4499 | targetUid = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4500 | } |
| 4501 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4502 | if (targetUid >= 0) { |
| 4503 | // First... does the target actually need this permission? |
| 4504 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 4505 | // No need to grant the target this permission. |
| 4506 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4507 | "Target " + targetPkg + " already has full permission to " + uri); |
| 4508 | return -1; |
| 4509 | } |
| 4510 | } else { |
| 4511 | // First... there is no target package, so can anyone access it? |
| 4512 | boolean allowed = pi.exported; |
| 4513 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4514 | if (pi.readPermission != null) { |
| 4515 | allowed = false; |
| 4516 | } |
| 4517 | } |
| 4518 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4519 | if (pi.writePermission != null) { |
| 4520 | allowed = false; |
| 4521 | } |
| 4522 | } |
| 4523 | if (allowed) { |
| 4524 | return -1; |
| 4525 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4526 | } |
| 4527 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4528 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4529 | if (!pi.grantUriPermissions) { |
| 4530 | throw new SecurityException("Provider " + pi.packageName |
| 4531 | + "/" + pi.name |
| 4532 | + " does not allow granting of Uri permissions (uri " |
| 4533 | + uri + ")"); |
| 4534 | } |
| 4535 | if (pi.uriPermissionPatterns != null) { |
| 4536 | final int N = pi.uriPermissionPatterns.length; |
| 4537 | boolean allowed = false; |
| 4538 | for (int i=0; i<N; i++) { |
| 4539 | if (pi.uriPermissionPatterns[i] != null |
| 4540 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 4541 | allowed = true; |
| 4542 | break; |
| 4543 | } |
| 4544 | } |
| 4545 | if (!allowed) { |
| 4546 | throw new SecurityException("Provider " + pi.packageName |
| 4547 | + "/" + pi.name |
| 4548 | + " does not allow granting of permission to path of Uri " |
| 4549 | + uri); |
| 4550 | } |
| 4551 | } |
| 4552 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4553 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4554 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4555 | if (callingUid != Process.myUid()) { |
| 4556 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 4557 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4558 | throw new SecurityException("Uid " + callingUid |
| 4559 | + " does not have permission to uri " + uri); |
| 4560 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4561 | } |
| 4562 | } |
| 4563 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4564 | return targetUid; |
| 4565 | } |
| 4566 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4567 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 4568 | Uri uri, int modeFlags) { |
| 4569 | synchronized(this) { |
| 4570 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags); |
| 4571 | } |
| 4572 | } |
| 4573 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4574 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 4575 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 4576 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4577 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4578 | if (modeFlags == 0) { |
| 4579 | return; |
| 4580 | } |
| 4581 | |
| 4582 | // 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] | 4583 | // to the uri, and the target doesn't. Let's now give this to |
| 4584 | // the target. |
| 4585 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4586 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4587 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4588 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4589 | HashMap<Uri, UriPermission> targetUris |
| 4590 | = mGrantedUriPermissions.get(targetUid); |
| 4591 | if (targetUris == null) { |
| 4592 | targetUris = new HashMap<Uri, UriPermission>(); |
| 4593 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 4594 | } |
| 4595 | |
| 4596 | UriPermission perm = targetUris.get(uri); |
| 4597 | if (perm == null) { |
| 4598 | perm = new UriPermission(targetUid, uri); |
| 4599 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4600 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4601 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4602 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4603 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4604 | perm.globalModeFlags |= modeFlags; |
Vairavan Srinivasan | 91c12c2 | 2011-01-21 18:26:06 -0800 | [diff] [blame] | 4605 | } else { |
| 4606 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4607 | perm.readOwners.add(owner); |
| 4608 | owner.addReadPermission(perm); |
| 4609 | } |
| 4610 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4611 | perm.writeOwners.add(owner); |
| 4612 | owner.addWritePermission(perm); |
| 4613 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4614 | } |
| 4615 | } |
| 4616 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4617 | void grantUriPermissionLocked(int callingUid, |
| 4618 | String targetPkg, Uri uri, int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4619 | if (targetPkg == null) { |
| 4620 | throw new NullPointerException("targetPkg"); |
| 4621 | } |
| 4622 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4623 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags); |
| 4624 | if (targetUid < 0) { |
| 4625 | return; |
| 4626 | } |
| 4627 | |
| 4628 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 4629 | } |
| 4630 | |
| 4631 | /** |
| 4632 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 4633 | */ |
| 4634 | int checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 4635 | String targetPkg, Intent intent) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4636 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4637 | "Checking URI perm to " + (intent != null ? intent.getData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4638 | + " from " + intent + "; flags=0x" |
| 4639 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 4640 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4641 | if (targetPkg == null) { |
| 4642 | throw new NullPointerException("targetPkg"); |
| 4643 | } |
| 4644 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4645 | if (intent == null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4646 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4647 | } |
| 4648 | Uri data = intent.getData(); |
| 4649 | if (data == null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4650 | return -1; |
| 4651 | } |
| 4652 | return checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 4653 | intent.getFlags()); |
| 4654 | } |
| 4655 | |
| 4656 | /** |
| 4657 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 4658 | */ |
| 4659 | void grantUriPermissionUncheckedFromIntentLocked(int targetUid, |
| 4660 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
| 4661 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, intent.getData(), |
| 4662 | intent.getFlags(), owner); |
| 4663 | } |
| 4664 | |
| 4665 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 4666 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
| 4667 | int targetUid = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, intent); |
| 4668 | if (targetUid < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4669 | return; |
| 4670 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4671 | |
| 4672 | grantUriPermissionUncheckedFromIntentLocked(targetUid, targetPkg, intent, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4673 | } |
| 4674 | |
| 4675 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 4676 | Uri uri, int modeFlags) { |
| 4677 | synchronized(this) { |
| 4678 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 4679 | if (r == null) { |
| 4680 | throw new SecurityException("Unable to find app for caller " |
| 4681 | + caller |
| 4682 | + " when granting permission to uri " + uri); |
| 4683 | } |
| 4684 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4685 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4686 | } |
| 4687 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4688 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | grantUriPermissionLocked(r.info.uid, targetPkg, uri, modeFlags, |
| 4692 | null); |
| 4693 | } |
| 4694 | } |
| 4695 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4696 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4697 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4698 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 4699 | HashMap<Uri, UriPermission> perms |
| 4700 | = mGrantedUriPermissions.get(perm.uid); |
| 4701 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4702 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4703 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4704 | perms.remove(perm.uri); |
| 4705 | if (perms.size() == 0) { |
| 4706 | mGrantedUriPermissions.remove(perm.uid); |
| 4707 | } |
| 4708 | } |
| 4709 | } |
| 4710 | } |
| 4711 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4712 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 4713 | int modeFlags) { |
| 4714 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4715 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4716 | if (modeFlags == 0) { |
| 4717 | return; |
| 4718 | } |
| 4719 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4720 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4721 | "Revoking all granted permissions to " + uri); |
| 4722 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4723 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4724 | |
| 4725 | final String authority = uri.getAuthority(); |
| 4726 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4727 | ContentProviderRecord cpr = mProvidersByName.get(authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4728 | if (cpr != null) { |
| 4729 | pi = cpr.info; |
| 4730 | } else { |
| 4731 | try { |
| 4732 | pi = pm.resolveContentProvider(authority, |
| 4733 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4734 | } catch (RemoteException ex) { |
| 4735 | } |
| 4736 | } |
| 4737 | if (pi == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4738 | Slog.w(TAG, "No content provider found for: " + authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4739 | return; |
| 4740 | } |
| 4741 | |
| 4742 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4743 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4744 | // Right now, if you are not the original owner of the permission, |
| 4745 | // you are not allowed to revoke it. |
| 4746 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4747 | throw new SecurityException("Uid " + callingUid |
| 4748 | + " does not have permission to uri " + uri); |
| 4749 | //} |
| 4750 | } |
| 4751 | |
| 4752 | // Go through all of the permissions and remove any that match. |
| 4753 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 4754 | if (SEGMENTS != null) { |
| 4755 | final int NS = SEGMENTS.size(); |
| 4756 | int N = mGrantedUriPermissions.size(); |
| 4757 | for (int i=0; i<N; i++) { |
| 4758 | HashMap<Uri, UriPermission> perms |
| 4759 | = mGrantedUriPermissions.valueAt(i); |
| 4760 | Iterator<UriPermission> it = perms.values().iterator(); |
| 4761 | toploop: |
| 4762 | while (it.hasNext()) { |
| 4763 | UriPermission perm = it.next(); |
| 4764 | Uri targetUri = perm.uri; |
| 4765 | if (!authority.equals(targetUri.getAuthority())) { |
| 4766 | continue; |
| 4767 | } |
| 4768 | List<String> targetSegments = targetUri.getPathSegments(); |
| 4769 | if (targetSegments == null) { |
| 4770 | continue; |
| 4771 | } |
| 4772 | if (targetSegments.size() < NS) { |
| 4773 | continue; |
| 4774 | } |
| 4775 | for (int j=0; j<NS; j++) { |
| 4776 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 4777 | continue toploop; |
| 4778 | } |
| 4779 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4780 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4781 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4782 | perm.clearModes(modeFlags); |
| 4783 | if (perm.modeFlags == 0) { |
| 4784 | it.remove(); |
| 4785 | } |
| 4786 | } |
| 4787 | if (perms.size() == 0) { |
| 4788 | mGrantedUriPermissions.remove( |
| 4789 | mGrantedUriPermissions.keyAt(i)); |
| 4790 | N--; |
| 4791 | i--; |
| 4792 | } |
| 4793 | } |
| 4794 | } |
| 4795 | } |
| 4796 | |
| 4797 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 4798 | int modeFlags) { |
| 4799 | synchronized(this) { |
| 4800 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 4801 | if (r == null) { |
| 4802 | throw new SecurityException("Unable to find app for caller " |
| 4803 | + caller |
| 4804 | + " when revoking permission to uri " + uri); |
| 4805 | } |
| 4806 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4807 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4808 | return; |
| 4809 | } |
| 4810 | |
| 4811 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4812 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4813 | if (modeFlags == 0) { |
| 4814 | return; |
| 4815 | } |
| 4816 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4817 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4818 | |
| 4819 | final String authority = uri.getAuthority(); |
| 4820 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4821 | ContentProviderRecord cpr = mProvidersByName.get(authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4822 | if (cpr != null) { |
| 4823 | pi = cpr.info; |
| 4824 | } else { |
| 4825 | try { |
| 4826 | pi = pm.resolveContentProvider(authority, |
| 4827 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4828 | } catch (RemoteException ex) { |
| 4829 | } |
| 4830 | } |
| 4831 | if (pi == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4832 | Slog.w(TAG, "No content provider found for: " + authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4833 | return; |
| 4834 | } |
| 4835 | |
| 4836 | revokeUriPermissionLocked(r.info.uid, uri, modeFlags); |
| 4837 | } |
| 4838 | } |
| 4839 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4840 | @Override |
| 4841 | public IBinder newUriPermissionOwner(String name) { |
| 4842 | synchronized(this) { |
| 4843 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 4844 | return owner.getExternalTokenLocked(); |
| 4845 | } |
| 4846 | } |
| 4847 | |
| 4848 | @Override |
| 4849 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 4850 | Uri uri, int modeFlags) { |
| 4851 | synchronized(this) { |
| 4852 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 4853 | if (owner == null) { |
| 4854 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 4855 | } |
| 4856 | if (fromUid != Binder.getCallingUid()) { |
| 4857 | if (Binder.getCallingUid() != Process.myUid()) { |
| 4858 | // Only system code can grant URI permissions on behalf |
| 4859 | // of other users. |
| 4860 | throw new SecurityException("nice try"); |
| 4861 | } |
| 4862 | } |
| 4863 | if (targetPkg == null) { |
| 4864 | throw new IllegalArgumentException("null target"); |
| 4865 | } |
| 4866 | if (uri == null) { |
| 4867 | throw new IllegalArgumentException("null uri"); |
| 4868 | } |
| 4869 | |
| 4870 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 4871 | } |
| 4872 | } |
| 4873 | |
| 4874 | @Override |
| 4875 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 4876 | synchronized(this) { |
| 4877 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 4878 | if (owner == null) { |
| 4879 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 4880 | } |
| 4881 | |
| 4882 | if (uri == null) { |
| 4883 | owner.removeUriPermissionsLocked(mode); |
| 4884 | } else { |
| 4885 | owner.removeUriPermissionLocked(uri, mode); |
| 4886 | } |
| 4887 | } |
| 4888 | } |
| 4889 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4890 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 4891 | synchronized (this) { |
| 4892 | ProcessRecord app = |
| 4893 | who != null ? getRecordForAppLocked(who) : null; |
| 4894 | if (app == null) return; |
| 4895 | |
| 4896 | Message msg = Message.obtain(); |
| 4897 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 4898 | msg.obj = app; |
| 4899 | msg.arg1 = waiting ? 1 : 0; |
| 4900 | mHandler.sendMessage(msg); |
| 4901 | } |
| 4902 | } |
| 4903 | |
| 4904 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4905 | final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ); |
| 4906 | final long hiddenAppMem = mProcessList.getMemLevel(ProcessList.HIDDEN_APP_MIN_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4907 | outInfo.availMem = Process.getFreeMemory(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4908 | outInfo.threshold = homeAppMem; |
| 4909 | outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2)); |
| 4910 | outInfo.hiddenAppThreshold = hiddenAppMem; |
| 4911 | outInfo.secondaryServerThreshold = mProcessList.getMemLevel( |
| 4912 | ProcessList.SECONDARY_SERVER_ADJ); |
| 4913 | outInfo.visibleAppThreshold = mProcessList.getMemLevel( |
| 4914 | ProcessList.VISIBLE_APP_ADJ); |
| 4915 | outInfo.foregroundAppThreshold = mProcessList.getMemLevel( |
| 4916 | ProcessList.FOREGROUND_APP_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4917 | } |
| 4918 | |
| 4919 | // ========================================================= |
| 4920 | // TASK MANAGEMENT |
| 4921 | // ========================================================= |
| 4922 | |
| 4923 | public List getTasks(int maxNum, int flags, |
| 4924 | IThumbnailReceiver receiver) { |
| 4925 | ArrayList list = new ArrayList(); |
| 4926 | |
| 4927 | PendingThumbnailsRecord pending = null; |
| 4928 | IApplicationThread topThumbnail = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4929 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4930 | |
| 4931 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4932 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4933 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 4934 | + ", receiver=" + receiver); |
| 4935 | |
| 4936 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 4937 | != PackageManager.PERMISSION_GRANTED) { |
| 4938 | if (receiver != null) { |
| 4939 | // If the caller wants to wait for pending thumbnails, |
| 4940 | // it ain't gonna get them. |
| 4941 | try { |
| 4942 | receiver.finished(); |
| 4943 | } catch (RemoteException ex) { |
| 4944 | } |
| 4945 | } |
| 4946 | String msg = "Permission Denial: getTasks() from pid=" |
| 4947 | + Binder.getCallingPid() |
| 4948 | + ", uid=" + Binder.getCallingUid() |
| 4949 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4950 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4951 | throw new SecurityException(msg); |
| 4952 | } |
| 4953 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4954 | int pos = mMainStack.mHistory.size()-1; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4955 | ActivityRecord next = |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4956 | pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4957 | ActivityRecord top = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4958 | TaskRecord curTask = null; |
| 4959 | int numActivities = 0; |
| 4960 | int numRunning = 0; |
| 4961 | while (pos >= 0 && maxNum > 0) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4962 | final ActivityRecord r = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4963 | pos--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4964 | next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4965 | |
| 4966 | // Initialize state for next task if needed. |
| 4967 | if (top == null || |
| 4968 | (top.state == ActivityState.INITIALIZING |
| 4969 | && top.task == r.task)) { |
| 4970 | top = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4971 | curTask = r.task; |
| 4972 | numActivities = numRunning = 0; |
| 4973 | } |
| 4974 | |
| 4975 | // Add 'r' into the current task. |
| 4976 | numActivities++; |
| 4977 | if (r.app != null && r.app.thread != null) { |
| 4978 | numRunning++; |
| 4979 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4980 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4981 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4982 | TAG, r.intent.getComponent().flattenToShortString() |
| 4983 | + ": task=" + r.task); |
| 4984 | |
| 4985 | // If the next one is a different task, generate a new |
| 4986 | // TaskInfo entry for what we have. |
| 4987 | if (next == null || next.task != curTask) { |
| 4988 | ActivityManager.RunningTaskInfo ci |
| 4989 | = new ActivityManager.RunningTaskInfo(); |
| 4990 | ci.id = curTask.taskId; |
| 4991 | ci.baseActivity = r.intent.getComponent(); |
| 4992 | ci.topActivity = top.intent.getComponent(); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 4993 | if (top.thumbHolder != null) { |
| 4994 | ci.description = top.thumbHolder.lastDescription; |
| 4995 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4996 | ci.numActivities = numActivities; |
| 4997 | ci.numRunning = numRunning; |
| 4998 | //System.out.println( |
| 4999 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 5000 | if (ci.thumbnail == null && receiver != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5001 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5002 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 5003 | + " app=" + top.app |
| 5004 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 5005 | if (top.state == ActivityState.RESUMED |
| 5006 | || top.state == ActivityState.PAUSING) { |
| 5007 | if (top.idle && top.app != null |
| 5008 | && top.app.thread != null) { |
| 5009 | topRecord = top; |
| 5010 | topThumbnail = top.app.thread; |
| 5011 | } else { |
| 5012 | top.thumbnailNeeded = true; |
| 5013 | } |
| 5014 | } |
| 5015 | if (pending == null) { |
| 5016 | pending = new PendingThumbnailsRecord(receiver); |
| 5017 | } |
| 5018 | pending.pendingRecords.add(top); |
| 5019 | } |
| 5020 | list.add(ci); |
| 5021 | maxNum--; |
| 5022 | top = null; |
| 5023 | } |
| 5024 | } |
| 5025 | |
| 5026 | if (pending != null) { |
| 5027 | mPendingThumbnails.add(pending); |
| 5028 | } |
| 5029 | } |
| 5030 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5031 | 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] | 5032 | |
| 5033 | if (topThumbnail != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5034 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5035 | try { |
| 5036 | topThumbnail.requestThumbnail(topRecord); |
| 5037 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5038 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5039 | sendPendingThumbnail(null, topRecord, null, null, true); |
| 5040 | } |
| 5041 | } |
| 5042 | |
| 5043 | if (pending == null && receiver != null) { |
| 5044 | // In this case all thumbnails were available and the client |
| 5045 | // is being asked to be told when the remaining ones come in... |
| 5046 | // which is unusually, since the top-most currently running |
| 5047 | // activity should never have a canned thumbnail! Oh well. |
| 5048 | try { |
| 5049 | receiver.finished(); |
| 5050 | } catch (RemoteException ex) { |
| 5051 | } |
| 5052 | } |
| 5053 | |
| 5054 | return list; |
| 5055 | } |
| 5056 | |
| 5057 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
| 5058 | int flags) { |
| 5059 | synchronized (this) { |
| 5060 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 5061 | "getRecentTasks()"); |
| 5062 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5063 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5064 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5065 | final int N = mRecentTasks.size(); |
| 5066 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 5067 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 5068 | maxNum < N ? maxNum : N); |
| 5069 | for (int i=0; i<N && maxNum > 0; i++) { |
| 5070 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5071 | // Return the entry if desired by the caller. We always return |
| 5072 | // the first entry, because callers always expect this to be the |
| 5073 | // forground app. We may filter others if the caller has |
| 5074 | // not supplied RECENT_WITH_EXCLUDED and there is some reason |
| 5075 | // we should exclude the entry. |
| 5076 | if (i == 0 |
| 5077 | || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5078 | || (tr.intent == null) |
| 5079 | || ((tr.intent.getFlags() |
| 5080 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 5081 | ActivityManager.RecentTaskInfo rti |
| 5082 | = new ActivityManager.RecentTaskInfo(); |
| 5083 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5084 | rti.persistentId = tr.taskId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5085 | rti.baseIntent = new Intent( |
| 5086 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5087 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 5088 | rti.description = tr.lastDescription; |
| 5089 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5090 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5091 | // Check whether this activity is currently available. |
| 5092 | try { |
| 5093 | if (rti.origActivity != null) { |
| 5094 | if (pm.getActivityInfo(rti.origActivity, 0) == null) { |
| 5095 | continue; |
| 5096 | } |
| 5097 | } else if (rti.baseIntent != null) { |
| 5098 | if (pm.queryIntentActivities(rti.baseIntent, |
| 5099 | null, 0) == null) { |
| 5100 | continue; |
| 5101 | } |
| 5102 | } |
| 5103 | } catch (RemoteException e) { |
| 5104 | // Will never happen. |
| 5105 | } |
| 5106 | } |
| 5107 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5108 | res.add(rti); |
| 5109 | maxNum--; |
| 5110 | } |
| 5111 | } |
| 5112 | return res; |
| 5113 | } |
| 5114 | } |
| 5115 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5116 | private TaskRecord taskForIdLocked(int id) { |
| 5117 | final int N = mRecentTasks.size(); |
| 5118 | for (int i=0; i<N; i++) { |
| 5119 | TaskRecord tr = mRecentTasks.get(i); |
| 5120 | if (tr.taskId == id) { |
| 5121 | return tr; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5122 | } |
| 5123 | } |
| 5124 | return null; |
| 5125 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5126 | |
| 5127 | public ActivityManager.TaskThumbnails getTaskThumbnails(int id) { |
| 5128 | synchronized (this) { |
| 5129 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5130 | "getTaskThumbnails()"); |
| 5131 | TaskRecord tr = taskForIdLocked(id); |
| 5132 | if (tr != null) { |
| 5133 | return mMainStack.getTaskThumbnailsLocked(tr); |
| 5134 | } |
| 5135 | } |
| 5136 | return null; |
| 5137 | } |
| 5138 | |
| 5139 | public boolean removeSubTask(int taskId, int subTaskIndex) { |
| 5140 | synchronized (this) { |
| 5141 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5142 | "removeSubTask()"); |
| 5143 | long ident = Binder.clearCallingIdentity(); |
| 5144 | try { |
| 5145 | return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex) != null; |
| 5146 | } finally { |
| 5147 | Binder.restoreCallingIdentity(ident); |
| 5148 | } |
| 5149 | } |
| 5150 | } |
| 5151 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5152 | private void cleanUpRemovedTaskLocked(ActivityRecord root, boolean killProcesses) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5153 | TaskRecord tr = root.task; |
| 5154 | Intent baseIntent = new Intent( |
| 5155 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5156 | ComponentName component = baseIntent.getComponent(); |
| 5157 | if (component == null) { |
| 5158 | Slog.w(TAG, "Now component for base intent of task: " + tr); |
| 5159 | return; |
| 5160 | } |
| 5161 | |
| 5162 | // Find any running services associated with this app. |
| 5163 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
| 5164 | for (ServiceRecord sr : mServices.values()) { |
| 5165 | if (sr.packageName.equals(component.getPackageName())) { |
| 5166 | services.add(sr); |
| 5167 | } |
| 5168 | } |
| 5169 | |
| 5170 | // Take care of any running services associated with the app. |
| 5171 | for (int i=0; i<services.size(); i++) { |
| 5172 | ServiceRecord sr = services.get(i); |
| 5173 | if (sr.startRequested) { |
| 5174 | if ((sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0) { |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5175 | Slog.i(TAG, "Stopping service " + sr.shortName + ": remove task"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5176 | stopServiceLocked(sr); |
| 5177 | } else { |
| 5178 | sr.pendingStarts.add(new ServiceRecord.StartItem(sr, true, |
| 5179 | sr.makeNextStartId(), baseIntent, -1)); |
| 5180 | if (sr.app != null && sr.app.thread != null) { |
| 5181 | sendServiceArgsLocked(sr, false); |
| 5182 | } |
| 5183 | } |
| 5184 | } |
| 5185 | } |
| 5186 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5187 | if (killProcesses) { |
| 5188 | // Find any running processes associated with this app. |
| 5189 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 5190 | SparseArray<ProcessRecord> appProcs |
| 5191 | = mProcessNames.getMap().get(component.getPackageName()); |
| 5192 | if (appProcs != null) { |
| 5193 | for (int i=0; i<appProcs.size(); i++) { |
| 5194 | procs.add(appProcs.valueAt(i)); |
| 5195 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5196 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5197 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5198 | // Kill the running processes. |
| 5199 | for (int i=0; i<procs.size(); i++) { |
| 5200 | ProcessRecord pr = procs.get(i); |
| 5201 | if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 5202 | Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task"); |
| 5203 | EventLog.writeEvent(EventLogTags.AM_KILL, pr.pid, |
| 5204 | pr.processName, pr.setAdj, "remove task"); |
| 5205 | Process.killProcessQuiet(pr.pid); |
| 5206 | } else { |
| 5207 | pr.waitingToKill = "remove task"; |
| 5208 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5209 | } |
| 5210 | } |
| 5211 | } |
| 5212 | |
| 5213 | public boolean removeTask(int taskId, int flags) { |
| 5214 | synchronized (this) { |
| 5215 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5216 | "removeTask()"); |
| 5217 | long ident = Binder.clearCallingIdentity(); |
| 5218 | try { |
| 5219 | ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1); |
| 5220 | if (r != null) { |
| 5221 | mRecentTasks.remove(r.task); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5222 | cleanUpRemovedTaskLocked(r, |
| 5223 | (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5224 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5225 | } else { |
| 5226 | TaskRecord tr = null; |
| 5227 | int i=0; |
| 5228 | while (i < mRecentTasks.size()) { |
| 5229 | TaskRecord t = mRecentTasks.get(i); |
| 5230 | if (t.taskId == taskId) { |
| 5231 | tr = t; |
| 5232 | break; |
| 5233 | } |
| 5234 | i++; |
| 5235 | } |
| 5236 | if (tr != null) { |
| 5237 | if (tr.numActivities <= 0) { |
| 5238 | // Caller is just removing a recent task that is |
| 5239 | // not actively running. That is easy! |
| 5240 | mRecentTasks.remove(i); |
| 5241 | } else { |
| 5242 | Slog.w(TAG, "removeTask: task " + taskId |
| 5243 | + " does not have activities to remove, " |
| 5244 | + " but numActivities=" + tr.numActivities |
| 5245 | + ": " + tr); |
| 5246 | } |
| 5247 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5248 | } |
| 5249 | } finally { |
| 5250 | Binder.restoreCallingIdentity(ident); |
| 5251 | } |
| 5252 | } |
| 5253 | return false; |
| 5254 | } |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5256 | private final int findAffinityTaskTopLocked(int startIndex, String affinity) { |
| 5257 | int j; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5258 | TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5259 | TaskRecord jt = startTask; |
| 5260 | |
| 5261 | // First look backwards |
| 5262 | for (j=startIndex-1; j>=0; j--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5263 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5264 | if (r.task != jt) { |
| 5265 | jt = r.task; |
| 5266 | if (affinity.equals(jt.affinity)) { |
| 5267 | return j; |
| 5268 | } |
| 5269 | } |
| 5270 | } |
| 5271 | |
| 5272 | // Now look forwards |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5273 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5274 | jt = startTask; |
| 5275 | for (j=startIndex+1; j<N; j++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5276 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5277 | if (r.task != jt) { |
| 5278 | if (affinity.equals(jt.affinity)) { |
| 5279 | return j; |
| 5280 | } |
| 5281 | jt = r.task; |
| 5282 | } |
| 5283 | } |
| 5284 | |
| 5285 | // Might it be at the top? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5286 | 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] | 5287 | return N-1; |
| 5288 | } |
| 5289 | |
| 5290 | return -1; |
| 5291 | } |
| 5292 | |
| 5293 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5294 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5295 | */ |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5296 | public void moveTaskToFront(int task, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5297 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5298 | "moveTaskToFront()"); |
| 5299 | |
| 5300 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5301 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5302 | Binder.getCallingUid(), "Task to front")) { |
| 5303 | return; |
| 5304 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5305 | final long origId = Binder.clearCallingIdentity(); |
| 5306 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5307 | TaskRecord tr = taskForIdLocked(task); |
| 5308 | if (tr != null) { |
| 5309 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5310 | mMainStack.mUserLeaving = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5311 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5312 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5313 | // Caller wants the home activity moved with it. To accomplish this, |
| 5314 | // we'll just move the home task to the top first. |
| 5315 | mMainStack.moveHomeToFrontLocked(); |
| 5316 | } |
| 5317 | mMainStack.moveTaskToFrontLocked(tr, null); |
| 5318 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5319 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5320 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 5321 | ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5322 | if (hr.task.taskId == task) { |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5323 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5324 | mMainStack.mUserLeaving = true; |
| 5325 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5326 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5327 | // Caller wants the home activity moved with it. To accomplish this, |
| 5328 | // we'll just move the home task to the top first. |
| 5329 | mMainStack.moveHomeToFrontLocked(); |
| 5330 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5331 | mMainStack.moveTaskToFrontLocked(hr.task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5332 | return; |
| 5333 | } |
| 5334 | } |
| 5335 | } finally { |
| 5336 | Binder.restoreCallingIdentity(origId); |
| 5337 | } |
| 5338 | } |
| 5339 | } |
| 5340 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5341 | public void moveTaskToBack(int task) { |
| 5342 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5343 | "moveTaskToBack()"); |
| 5344 | |
| 5345 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5346 | if (mMainStack.mResumedActivity != null |
| 5347 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5348 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5349 | Binder.getCallingUid(), "Task to back")) { |
| 5350 | return; |
| 5351 | } |
| 5352 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5353 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5354 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5355 | Binder.restoreCallingIdentity(origId); |
| 5356 | } |
| 5357 | } |
| 5358 | |
| 5359 | /** |
| 5360 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 5361 | * of the history stack. The activity's order within the task is unchanged. |
| 5362 | * |
| 5363 | * @param token A reference to the activity we wish to move |
| 5364 | * @param nonRoot If false then this only works if the activity is the root |
| 5365 | * of a task; if true it will work for any activity in a task. |
| 5366 | * @return Returns true if the move completed, false if not. |
| 5367 | */ |
| 5368 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
| 5369 | synchronized(this) { |
| 5370 | final long origId = Binder.clearCallingIdentity(); |
| 5371 | int taskId = getTaskForActivityLocked(token, !nonRoot); |
| 5372 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5373 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5374 | } |
| 5375 | Binder.restoreCallingIdentity(origId); |
| 5376 | } |
| 5377 | return false; |
| 5378 | } |
| 5379 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5380 | public void moveTaskBackwards(int task) { |
| 5381 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5382 | "moveTaskBackwards()"); |
| 5383 | |
| 5384 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5385 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5386 | Binder.getCallingUid(), "Task backwards")) { |
| 5387 | return; |
| 5388 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5389 | final long origId = Binder.clearCallingIdentity(); |
| 5390 | moveTaskBackwardsLocked(task); |
| 5391 | Binder.restoreCallingIdentity(origId); |
| 5392 | } |
| 5393 | } |
| 5394 | |
| 5395 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5396 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5397 | } |
| 5398 | |
| 5399 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 5400 | synchronized(this) { |
| 5401 | return getTaskForActivityLocked(token, onlyRoot); |
| 5402 | } |
| 5403 | } |
| 5404 | |
| 5405 | int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5406 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5407 | TaskRecord lastTask = null; |
| 5408 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5409 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5410 | if (r == token) { |
| 5411 | if (!onlyRoot || lastTask != r.task) { |
| 5412 | return r.task.taskId; |
| 5413 | } |
| 5414 | return -1; |
| 5415 | } |
| 5416 | lastTask = r.task; |
| 5417 | } |
| 5418 | |
| 5419 | return -1; |
| 5420 | } |
| 5421 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5422 | public void finishOtherInstances(IBinder token, ComponentName className) { |
| 5423 | synchronized(this) { |
| 5424 | final long origId = Binder.clearCallingIdentity(); |
| 5425 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5426 | int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5427 | TaskRecord lastTask = null; |
| 5428 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5429 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5430 | if (r.realActivity.equals(className) |
| 5431 | && r != token && lastTask != r.task) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5432 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5433 | null, "others")) { |
| 5434 | i--; |
| 5435 | N--; |
| 5436 | } |
| 5437 | } |
| 5438 | lastTask = r.task; |
| 5439 | } |
| 5440 | |
| 5441 | Binder.restoreCallingIdentity(origId); |
| 5442 | } |
| 5443 | } |
| 5444 | |
| 5445 | // ========================================================= |
| 5446 | // THUMBNAILS |
| 5447 | // ========================================================= |
| 5448 | |
| 5449 | public void reportThumbnail(IBinder token, |
| 5450 | Bitmap thumbnail, CharSequence description) { |
| 5451 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 5452 | final long origId = Binder.clearCallingIdentity(); |
| 5453 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 5454 | Binder.restoreCallingIdentity(origId); |
| 5455 | } |
| 5456 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5457 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5458 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 5459 | TaskRecord task = null; |
| 5460 | ArrayList receivers = null; |
| 5461 | |
| 5462 | //System.out.println("Send pending thumbnail: " + r); |
| 5463 | |
| 5464 | synchronized(this) { |
| 5465 | if (r == null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 5466 | r = mMainStack.isInStackLocked(token); |
| 5467 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5468 | return; |
| 5469 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5470 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5471 | if (thumbnail == null && r.thumbHolder != null) { |
| 5472 | thumbnail = r.thumbHolder.lastThumbnail; |
| 5473 | description = r.thumbHolder.lastDescription; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5474 | } |
| 5475 | if (thumbnail == null && !always) { |
| 5476 | // If there is no thumbnail, and this entry is not actually |
| 5477 | // going away, then abort for now and pick up the next |
| 5478 | // thumbnail we get. |
| 5479 | return; |
| 5480 | } |
| 5481 | task = r.task; |
| 5482 | |
| 5483 | int N = mPendingThumbnails.size(); |
| 5484 | int i=0; |
| 5485 | while (i<N) { |
| 5486 | PendingThumbnailsRecord pr = |
| 5487 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 5488 | //System.out.println("Looking in " + pr.pendingRecords); |
| 5489 | if (pr.pendingRecords.remove(r)) { |
| 5490 | if (receivers == null) { |
| 5491 | receivers = new ArrayList(); |
| 5492 | } |
| 5493 | receivers.add(pr); |
| 5494 | if (pr.pendingRecords.size() == 0) { |
| 5495 | pr.finished = true; |
| 5496 | mPendingThumbnails.remove(i); |
| 5497 | N--; |
| 5498 | continue; |
| 5499 | } |
| 5500 | } |
| 5501 | i++; |
| 5502 | } |
| 5503 | } |
| 5504 | |
| 5505 | if (receivers != null) { |
| 5506 | final int N = receivers.size(); |
| 5507 | for (int i=0; i<N; i++) { |
| 5508 | try { |
| 5509 | PendingThumbnailsRecord pr = |
| 5510 | (PendingThumbnailsRecord)receivers.get(i); |
| 5511 | pr.receiver.newThumbnail( |
| 5512 | task != null ? task.taskId : -1, thumbnail, description); |
| 5513 | if (pr.finished) { |
| 5514 | pr.receiver.finished(); |
| 5515 | } |
| 5516 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5517 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5518 | } |
| 5519 | } |
| 5520 | } |
| 5521 | } |
| 5522 | |
| 5523 | // ========================================================= |
| 5524 | // CONTENT PROVIDERS |
| 5525 | // ========================================================= |
| 5526 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 5527 | private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) { |
| 5528 | List<ProviderInfo> providers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5529 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5530 | providers = AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5531 | queryContentProviders(app.processName, app.info.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5532 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5533 | } catch (RemoteException ex) { |
| 5534 | } |
| 5535 | if (providers != null) { |
| 5536 | final int N = providers.size(); |
| 5537 | for (int i=0; i<N; i++) { |
| 5538 | ProviderInfo cpi = |
| 5539 | (ProviderInfo)providers.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5540 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
| 5541 | ContentProviderRecord cpr = mProvidersByClass.get(comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5542 | if (cpr == null) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5543 | cpr = new ContentProviderRecord(cpi, app.info, comp); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5544 | mProvidersByClass.put(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5545 | } |
| 5546 | app.pubProviders.put(cpi.name, cpr); |
| 5547 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 5548 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5549 | } |
| 5550 | } |
| 5551 | return providers; |
| 5552 | } |
| 5553 | |
| 5554 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5555 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5556 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
| 5557 | final int callingUid = (r != null) ? r.info.uid : Binder.getCallingUid(); |
| 5558 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5559 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5560 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5561 | return null; |
| 5562 | } |
| 5563 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5564 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5565 | == PackageManager.PERMISSION_GRANTED) { |
| 5566 | return null; |
| 5567 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5568 | |
| 5569 | PathPermission[] pps = cpi.pathPermissions; |
| 5570 | if (pps != null) { |
| 5571 | int i = pps.length; |
| 5572 | while (i > 0) { |
| 5573 | i--; |
| 5574 | PathPermission pp = pps[i]; |
| 5575 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5576 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5577 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5578 | return null; |
| 5579 | } |
| 5580 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5581 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5582 | == PackageManager.PERMISSION_GRANTED) { |
| 5583 | return null; |
| 5584 | } |
| 5585 | } |
| 5586 | } |
| 5587 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5588 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 5589 | if (perms != null) { |
| 5590 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 5591 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 5592 | return null; |
| 5593 | } |
| 5594 | } |
| 5595 | } |
| 5596 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5597 | String msg; |
| 5598 | if (!cpi.exported) { |
| 5599 | msg = "Permission Denial: opening provider " + cpi.name |
| 5600 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 5601 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 5602 | + cpi.applicationInfo.uid; |
| 5603 | } else { |
| 5604 | msg = "Permission Denial: opening provider " + cpi.name |
| 5605 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 5606 | + ", uid=" + callingUid + ") requires " |
| 5607 | + cpi.readPermission + " or " + cpi.writePermission; |
| 5608 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5609 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5610 | return msg; |
| 5611 | } |
| 5612 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5613 | boolean incProviderCount(ProcessRecord r, ContentProviderRecord cpr) { |
| 5614 | if (r != null) { |
| 5615 | Integer cnt = r.conProviders.get(cpr); |
| 5616 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 5617 | "Adding provider requested by " |
| 5618 | + r.processName + " from process " |
| 5619 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 5620 | + " cnt=" + (cnt == null ? 1 : cnt)); |
| 5621 | if (cnt == null) { |
| 5622 | cpr.clients.add(r); |
| 5623 | r.conProviders.put(cpr, new Integer(1)); |
| 5624 | return true; |
| 5625 | } else { |
| 5626 | r.conProviders.put(cpr, new Integer(cnt.intValue()+1)); |
| 5627 | } |
| 5628 | } else { |
| 5629 | cpr.externals++; |
| 5630 | } |
| 5631 | return false; |
| 5632 | } |
| 5633 | |
| 5634 | boolean decProviderCount(ProcessRecord r, ContentProviderRecord cpr) { |
| 5635 | if (r != null) { |
| 5636 | Integer cnt = r.conProviders.get(cpr); |
| 5637 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 5638 | "Removing provider requested by " |
| 5639 | + r.processName + " from process " |
| 5640 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 5641 | + " cnt=" + cnt); |
| 5642 | if (cnt == null || cnt.intValue() <= 1) { |
| 5643 | cpr.clients.remove(r); |
| 5644 | r.conProviders.remove(cpr); |
| 5645 | return true; |
| 5646 | } else { |
| 5647 | r.conProviders.put(cpr, new Integer(cnt.intValue()-1)); |
| 5648 | } |
| 5649 | } else { |
| 5650 | cpr.externals++; |
| 5651 | } |
| 5652 | return false; |
| 5653 | } |
| 5654 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5655 | private final ContentProviderHolder getContentProviderImpl( |
| 5656 | IApplicationThread caller, String name) { |
| 5657 | ContentProviderRecord cpr; |
| 5658 | ProviderInfo cpi = null; |
| 5659 | |
| 5660 | synchronized(this) { |
| 5661 | ProcessRecord r = null; |
| 5662 | if (caller != null) { |
| 5663 | r = getRecordForAppLocked(caller); |
| 5664 | if (r == null) { |
| 5665 | throw new SecurityException( |
| 5666 | "Unable to find app for caller " + caller |
| 5667 | + " (pid=" + Binder.getCallingPid() |
| 5668 | + ") when getting content provider " + name); |
| 5669 | } |
| 5670 | } |
| 5671 | |
| 5672 | // First check if this content provider has been published... |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5673 | cpr = mProvidersByName.get(name); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5674 | boolean providerRunning = cpr != null; |
| 5675 | if (providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5676 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5677 | String msg; |
| 5678 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 5679 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5680 | } |
| 5681 | |
| 5682 | if (r != null && cpr.canRunHere(r)) { |
| 5683 | // This provider has been published or is in the process |
| 5684 | // of being published... but it is also allowed to run |
| 5685 | // in the caller's process, so don't make a connection |
| 5686 | // and just let the caller instantiate its own instance. |
| 5687 | if (cpr.provider != null) { |
| 5688 | // don't give caller the provider object, it needs |
| 5689 | // to make its own. |
| 5690 | cpr = new ContentProviderRecord(cpr); |
| 5691 | } |
| 5692 | return cpr; |
| 5693 | } |
| 5694 | |
| 5695 | final long origId = Binder.clearCallingIdentity(); |
| 5696 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5697 | // 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] | 5698 | // return it right away. |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5699 | final boolean countChanged = incProviderCount(r, cpr); |
| 5700 | if (countChanged) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5701 | if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 5702 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 5703 | // make sure to count it as being accessed and thus |
| 5704 | // back up on the LRU list. This is good because |
| 5705 | // content providers are often expensive to start. |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5706 | updateLruProcessLocked(cpr.proc, false, true); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 5707 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 5708 | } |
| 5709 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5710 | if (cpr.proc != null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5711 | if (false) { |
| 5712 | if (cpr.name.flattenToShortString().equals( |
| 5713 | "com.android.providers.calendar/.CalendarProvider2")) { |
| 5714 | Slog.v(TAG, "****************** KILLING " |
| 5715 | + cpr.name.flattenToShortString()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5716 | Process.killProcess(cpr.proc.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5717 | } |
| 5718 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5719 | boolean success = updateOomAdjLocked(cpr.proc); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5720 | if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success); |
| 5721 | // NOTE: there is still a race here where a signal could be |
| 5722 | // pending on the process even though we managed to update its |
| 5723 | // adj level. Not sure what to do about this, but at least |
| 5724 | // the race is now smaller. |
| 5725 | if (!success) { |
| 5726 | // Uh oh... it looks like the provider's process |
| 5727 | // has been killed on us. We need to wait for a new |
| 5728 | // process to be started, and make sure its death |
| 5729 | // doesn't kill our process. |
| 5730 | Slog.i(TAG, |
| 5731 | "Existing provider " + cpr.name.flattenToShortString() |
| 5732 | + " is crashing; detaching " + r); |
| 5733 | boolean lastRef = decProviderCount(r, cpr); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5734 | appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5735 | if (!lastRef) { |
| 5736 | // This wasn't the last ref our process had on |
| 5737 | // the provider... we have now been killed, bail. |
| 5738 | return null; |
| 5739 | } |
| 5740 | providerRunning = false; |
| 5741 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5742 | } |
| 5743 | |
| 5744 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5745 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5746 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5747 | if (!providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5748 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5749 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5750 | resolveContentProvider(name, |
| 5751 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5752 | } catch (RemoteException ex) { |
| 5753 | } |
| 5754 | if (cpi == null) { |
| 5755 | return null; |
| 5756 | } |
| 5757 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5758 | String msg; |
| 5759 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 5760 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5761 | } |
| 5762 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 5763 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 5764 | && !cpi.processName.equals("system")) { |
| 5765 | // If this content provider does not run in the system |
| 5766 | // process, and the system is not yet ready to run other |
| 5767 | // processes, then fail fast instead of hanging. |
| 5768 | throw new IllegalArgumentException( |
| 5769 | "Attempt to launch content provider before system ready"); |
| 5770 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5771 | |
| 5772 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
| 5773 | cpr = mProvidersByClass.get(comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5774 | final boolean firstClass = cpr == null; |
| 5775 | if (firstClass) { |
| 5776 | try { |
| 5777 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5778 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5779 | getApplicationInfo( |
| 5780 | cpi.applicationInfo.packageName, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5781 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5782 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5783 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5784 | + cpi.name); |
| 5785 | return null; |
| 5786 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5787 | cpr = new ContentProviderRecord(cpi, ai, comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5788 | } catch (RemoteException ex) { |
| 5789 | // pm is in same process, this will never happen. |
| 5790 | } |
| 5791 | } |
| 5792 | |
| 5793 | if (r != null && cpr.canRunHere(r)) { |
| 5794 | // If this is a multiprocess provider, then just return its |
| 5795 | // info and allow the caller to instantiate it. Only do |
| 5796 | // this if the provider is the same user as the caller's |
| 5797 | // process, or can run as root (so can be in any process). |
| 5798 | return cpr; |
| 5799 | } |
| 5800 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5801 | if (DEBUG_PROVIDER) { |
| 5802 | RuntimeException e = new RuntimeException("here"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5803 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.info.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5804 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5805 | } |
| 5806 | |
| 5807 | // This is single process, and our app is now connecting to it. |
| 5808 | // See if we are already in the process of launching this |
| 5809 | // provider. |
| 5810 | final int N = mLaunchingProviders.size(); |
| 5811 | int i; |
| 5812 | for (i=0; i<N; i++) { |
| 5813 | if (mLaunchingProviders.get(i) == cpr) { |
| 5814 | break; |
| 5815 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5816 | } |
| 5817 | |
| 5818 | // If the provider is not already being launched, then get it |
| 5819 | // started. |
| 5820 | if (i >= N) { |
| 5821 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 5822 | |
| 5823 | try { |
| 5824 | // Content provider is now in use, its package can't be stopped. |
| 5825 | try { |
| 5826 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 5827 | cpr.appInfo.packageName, false); |
| 5828 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 5829 | } catch (IllegalArgumentException e) { |
| 5830 | Slog.w(TAG, "Failed trying to unstop package " |
| 5831 | + cpr.appInfo.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 5832 | } |
| 5833 | |
| 5834 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 5835 | cpr.appInfo, false, 0, "content provider", |
| 5836 | new ComponentName(cpi.applicationInfo.packageName, |
| 5837 | cpi.name), false); |
| 5838 | if (proc == null) { |
| 5839 | Slog.w(TAG, "Unable to launch app " |
| 5840 | + cpi.applicationInfo.packageName + "/" |
| 5841 | + cpi.applicationInfo.uid + " for provider " |
| 5842 | + name + ": process is bad"); |
| 5843 | return null; |
| 5844 | } |
| 5845 | cpr.launchingApp = proc; |
| 5846 | mLaunchingProviders.add(cpr); |
| 5847 | } finally { |
| 5848 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5849 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5850 | } |
| 5851 | |
| 5852 | // Make sure the provider is published (the same provider class |
| 5853 | // may be published under multiple names). |
| 5854 | if (firstClass) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5855 | mProvidersByClass.put(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5856 | } |
| 5857 | mProvidersByName.put(name, cpr); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5858 | incProviderCount(r, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5859 | } |
| 5860 | } |
| 5861 | |
| 5862 | // Wait for the provider to be published... |
| 5863 | synchronized (cpr) { |
| 5864 | while (cpr.provider == null) { |
| 5865 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5866 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5867 | + cpi.applicationInfo.packageName + "/" |
| 5868 | + cpi.applicationInfo.uid + " for provider " |
| 5869 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 5870 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5871 | cpi.applicationInfo.packageName, |
| 5872 | cpi.applicationInfo.uid, name); |
| 5873 | return null; |
| 5874 | } |
| 5875 | try { |
| 5876 | cpr.wait(); |
| 5877 | } catch (InterruptedException ex) { |
| 5878 | } |
| 5879 | } |
| 5880 | } |
| 5881 | return cpr; |
| 5882 | } |
| 5883 | |
| 5884 | public final ContentProviderHolder getContentProvider( |
| 5885 | IApplicationThread caller, String name) { |
| 5886 | if (caller == null) { |
| 5887 | String msg = "null IApplicationThread when getting content provider " |
| 5888 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5889 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5890 | throw new SecurityException(msg); |
| 5891 | } |
| 5892 | |
| 5893 | return getContentProviderImpl(caller, name); |
| 5894 | } |
| 5895 | |
| 5896 | private ContentProviderHolder getContentProviderExternal(String name) { |
| 5897 | return getContentProviderImpl(null, name); |
| 5898 | } |
| 5899 | |
| 5900 | /** |
| 5901 | * Drop a content provider from a ProcessRecord's bookkeeping |
| 5902 | * @param cpr |
| 5903 | */ |
| 5904 | public void removeContentProvider(IApplicationThread caller, String name) { |
| 5905 | synchronized (this) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5906 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5907 | if(cpr == null) { |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5908 | // remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5909 | if (DEBUG_PROVIDER) Slog.v(TAG, name + |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5910 | " provider not found in providers list"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5911 | return; |
| 5912 | } |
| 5913 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5914 | if (r == null) { |
| 5915 | throw new SecurityException( |
| 5916 | "Unable to find app for caller " + caller + |
| 5917 | " when removing content provider " + name); |
| 5918 | } |
| 5919 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5920 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
| 5921 | ContentProviderRecord localCpr = mProvidersByClass.get(comp); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5922 | if (localCpr.proc == r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5923 | //should not happen. taken care of as a local provider |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5924 | Slog.w(TAG, "removeContentProvider called on local provider: " |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5925 | + cpr.info.name + " in process " + r.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5926 | return; |
| 5927 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5928 | if (decProviderCount(r, localCpr)) { |
| 5929 | updateOomAdjLocked(); |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 5930 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5931 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5932 | } |
| 5933 | } |
| 5934 | |
| 5935 | private void removeContentProviderExternal(String name) { |
| 5936 | synchronized (this) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5937 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5938 | if(cpr == null) { |
| 5939 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5940 | 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] | 5941 | return; |
| 5942 | } |
| 5943 | |
| 5944 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5945 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
| 5946 | ContentProviderRecord localCpr = mProvidersByClass.get(comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5947 | localCpr.externals--; |
| 5948 | if (localCpr.externals < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5949 | Slog.e(TAG, "Externals < 0 for content provider " + localCpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5950 | } |
| 5951 | updateOomAdjLocked(); |
| 5952 | } |
| 5953 | } |
| 5954 | |
| 5955 | public final void publishContentProviders(IApplicationThread caller, |
| 5956 | List<ContentProviderHolder> providers) { |
| 5957 | if (providers == null) { |
| 5958 | return; |
| 5959 | } |
| 5960 | |
| 5961 | synchronized(this) { |
| 5962 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5963 | if (r == null) { |
| 5964 | throw new SecurityException( |
| 5965 | "Unable to find app for caller " + caller |
| 5966 | + " (pid=" + Binder.getCallingPid() |
| 5967 | + ") when publishing content providers"); |
| 5968 | } |
| 5969 | |
| 5970 | final long origId = Binder.clearCallingIdentity(); |
| 5971 | |
| 5972 | final int N = providers.size(); |
| 5973 | for (int i=0; i<N; i++) { |
| 5974 | ContentProviderHolder src = providers.get(i); |
| 5975 | if (src == null || src.info == null || src.provider == null) { |
| 5976 | continue; |
| 5977 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5978 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5979 | if (dst != null) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5980 | ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name); |
| 5981 | mProvidersByClass.put(comp, dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5982 | String names[] = dst.info.authority.split(";"); |
| 5983 | for (int j = 0; j < names.length; j++) { |
| 5984 | mProvidersByName.put(names[j], dst); |
| 5985 | } |
| 5986 | |
| 5987 | int NL = mLaunchingProviders.size(); |
| 5988 | int j; |
| 5989 | for (j=0; j<NL; j++) { |
| 5990 | if (mLaunchingProviders.get(j) == dst) { |
| 5991 | mLaunchingProviders.remove(j); |
| 5992 | j--; |
| 5993 | NL--; |
| 5994 | } |
| 5995 | } |
| 5996 | synchronized (dst) { |
| 5997 | dst.provider = src.provider; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5998 | dst.proc = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5999 | dst.notifyAll(); |
| 6000 | } |
| 6001 | updateOomAdjLocked(r); |
| 6002 | } |
| 6003 | } |
| 6004 | |
| 6005 | Binder.restoreCallingIdentity(origId); |
| 6006 | } |
| 6007 | } |
| 6008 | |
| 6009 | public static final void installSystemProviders() { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6010 | List<ProviderInfo> providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6011 | synchronized (mSelf) { |
| 6012 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 6013 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 6014 | if (providers != null) { |
| 6015 | for (int i=providers.size()-1; i>=0; i--) { |
| 6016 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 6017 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6018 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 6019 | + ": not system .apk"); |
| 6020 | providers.remove(i); |
| 6021 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6022 | } |
| 6023 | } |
| 6024 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6025 | if (providers != null) { |
| 6026 | mSystemThread.installSystemProviders(providers); |
| 6027 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 6028 | |
| 6029 | mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 6030 | |
| 6031 | mSelf.mUsageStatsService.monitorPackages(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6032 | } |
| 6033 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6034 | /** |
| 6035 | * Allows app to retrieve the MIME type of a URI without having permission |
| 6036 | * to access its content provider. |
| 6037 | * |
| 6038 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 6039 | * |
| 6040 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 6041 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 6042 | */ |
| 6043 | public String getProviderMimeType(Uri uri) { |
| 6044 | final String name = uri.getAuthority(); |
| 6045 | final long ident = Binder.clearCallingIdentity(); |
| 6046 | ContentProviderHolder holder = null; |
| 6047 | |
| 6048 | try { |
| 6049 | holder = getContentProviderExternal(name); |
| 6050 | if (holder != null) { |
| 6051 | return holder.provider.getType(uri); |
| 6052 | } |
| 6053 | } catch (RemoteException e) { |
| 6054 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 6055 | return null; |
| 6056 | } finally { |
| 6057 | if (holder != null) { |
| 6058 | removeContentProviderExternal(name); |
| 6059 | } |
| 6060 | Binder.restoreCallingIdentity(ident); |
| 6061 | } |
| 6062 | |
| 6063 | return null; |
| 6064 | } |
| 6065 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6066 | // ========================================================= |
| 6067 | // GLOBAL MANAGEMENT |
| 6068 | // ========================================================= |
| 6069 | |
| 6070 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
| 6071 | ApplicationInfo info, String customProcess) { |
| 6072 | String proc = customProcess != null ? customProcess : info.processName; |
| 6073 | BatteryStatsImpl.Uid.Proc ps = null; |
| 6074 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 6075 | synchronized (stats) { |
| 6076 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 6077 | } |
| 6078 | return new ProcessRecord(ps, thread, info, proc); |
| 6079 | } |
| 6080 | |
| 6081 | final ProcessRecord addAppLocked(ApplicationInfo info) { |
| 6082 | ProcessRecord app = getProcessRecordLocked(info.processName, info.uid); |
| 6083 | |
| 6084 | if (app == null) { |
| 6085 | app = newProcessRecordLocked(null, info, null); |
| 6086 | mProcessNames.put(info.processName, info.uid, app); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 6087 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6088 | } |
| 6089 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6090 | // This package really, really can not be stopped. |
| 6091 | try { |
| 6092 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 6093 | info.packageName, false); |
| 6094 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6095 | } catch (IllegalArgumentException e) { |
| 6096 | Slog.w(TAG, "Failed trying to unstop package " |
| 6097 | + info.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6098 | } |
| 6099 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6100 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 6101 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 6102 | app.persistent = true; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 6103 | app.maxAdj = ProcessList.CORE_SERVER_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6104 | } |
| 6105 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 6106 | mPersistentStartingProcesses.add(app); |
| 6107 | startProcessLocked(app, "added application", app.processName); |
| 6108 | } |
| 6109 | |
| 6110 | return app; |
| 6111 | } |
| 6112 | |
| 6113 | public void unhandledBack() { |
| 6114 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 6115 | "unhandledBack()"); |
| 6116 | |
| 6117 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6118 | int count = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6119 | if (DEBUG_SWITCH) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6120 | TAG, "Performing unhandledBack(): stack size = " + count); |
| 6121 | if (count > 1) { |
| 6122 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6123 | mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6124 | count-1, Activity.RESULT_CANCELED, null, "unhandled-back"); |
| 6125 | Binder.restoreCallingIdentity(origId); |
| 6126 | } |
| 6127 | } |
| 6128 | } |
| 6129 | |
| 6130 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
| 6131 | String name = uri.getAuthority(); |
| 6132 | ContentProviderHolder cph = getContentProviderExternal(name); |
| 6133 | ParcelFileDescriptor pfd = null; |
| 6134 | if (cph != null) { |
| 6135 | // We record the binder invoker's uid in thread-local storage before |
| 6136 | // going to the content provider to open the file. Later, in the code |
| 6137 | // that handles all permissions checks, we look for this uid and use |
| 6138 | // that rather than the Activity Manager's own uid. The effect is that |
| 6139 | // we do the check against the caller's permissions even though it looks |
| 6140 | // to the content provider like the Activity Manager itself is making |
| 6141 | // the request. |
| 6142 | sCallerIdentity.set(new Identity( |
| 6143 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 6144 | try { |
| 6145 | pfd = cph.provider.openFile(uri, "r"); |
| 6146 | } catch (FileNotFoundException e) { |
| 6147 | // do nothing; pfd will be returned null |
| 6148 | } finally { |
| 6149 | // Ensure that whatever happens, we clean up the identity state |
| 6150 | sCallerIdentity.remove(); |
| 6151 | } |
| 6152 | |
| 6153 | // We've got the fd now, so we're done with the provider. |
| 6154 | removeContentProviderExternal(name); |
| 6155 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6156 | 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] | 6157 | } |
| 6158 | return pfd; |
| 6159 | } |
| 6160 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6161 | // Actually is sleeping or shutting down or whatever else in the future |
| 6162 | // is an inactive state. |
| 6163 | public boolean isSleeping() { |
| 6164 | return mSleeping || mShuttingDown; |
| 6165 | } |
| 6166 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6167 | public void goingToSleep() { |
| 6168 | synchronized(this) { |
| 6169 | mSleeping = true; |
| 6170 | mWindowManager.setEventDispatching(false); |
| 6171 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6172 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6173 | |
| 6174 | // Initialize the wake times of all processes. |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 6175 | checkExcessivePowerUsageLocked(false); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6176 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 6177 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 6178 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6179 | } |
| 6180 | } |
| 6181 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6182 | public boolean shutdown(int timeout) { |
| 6183 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 6184 | != PackageManager.PERMISSION_GRANTED) { |
| 6185 | throw new SecurityException("Requires permission " |
| 6186 | + android.Manifest.permission.SHUTDOWN); |
| 6187 | } |
| 6188 | |
| 6189 | boolean timedout = false; |
| 6190 | |
| 6191 | synchronized(this) { |
| 6192 | mShuttingDown = true; |
| 6193 | mWindowManager.setEventDispatching(false); |
| 6194 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6195 | if (mMainStack.mResumedActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6196 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6197 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6198 | while (mMainStack.mResumedActivity != null |
| 6199 | || mMainStack.mPausingActivity != null) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6200 | long delay = endTime - System.currentTimeMillis(); |
| 6201 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6202 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6203 | timedout = true; |
| 6204 | break; |
| 6205 | } |
| 6206 | try { |
| 6207 | this.wait(); |
| 6208 | } catch (InterruptedException e) { |
| 6209 | } |
| 6210 | } |
| 6211 | } |
| 6212 | } |
| 6213 | |
| 6214 | mUsageStatsService.shutdown(); |
| 6215 | mBatteryStatsService.shutdown(); |
| 6216 | |
| 6217 | return timedout; |
| 6218 | } |
| 6219 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6220 | public final void activitySlept(IBinder token) { |
| 6221 | if (localLOGV) Slog.v( |
| 6222 | TAG, "Activity slept: token=" + token); |
| 6223 | |
| 6224 | ActivityRecord r = null; |
| 6225 | |
| 6226 | final long origId = Binder.clearCallingIdentity(); |
| 6227 | |
| 6228 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6229 | r = mMainStack.isInStackLocked(token); |
| 6230 | if (r != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6231 | mMainStack.activitySleptLocked(r); |
| 6232 | } |
| 6233 | } |
| 6234 | |
| 6235 | Binder.restoreCallingIdentity(origId); |
| 6236 | } |
| 6237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6238 | public void wakingUp() { |
| 6239 | synchronized(this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6240 | mWindowManager.setEventDispatching(true); |
| 6241 | mSleeping = false; |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6242 | mMainStack.awakeFromSleepingLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6243 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6244 | } |
| 6245 | } |
| 6246 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6247 | public void stopAppSwitches() { |
| 6248 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6249 | != PackageManager.PERMISSION_GRANTED) { |
| 6250 | throw new SecurityException("Requires permission " |
| 6251 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6252 | } |
| 6253 | |
| 6254 | synchronized(this) { |
| 6255 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 6256 | + APP_SWITCH_DELAY_TIME; |
| 6257 | mDidAppSwitch = false; |
| 6258 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6259 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6260 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 6261 | } |
| 6262 | } |
| 6263 | |
| 6264 | public void resumeAppSwitches() { |
| 6265 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6266 | != PackageManager.PERMISSION_GRANTED) { |
| 6267 | throw new SecurityException("Requires permission " |
| 6268 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6269 | } |
| 6270 | |
| 6271 | synchronized(this) { |
| 6272 | // Note that we don't execute any pending app switches... we will |
| 6273 | // let those wait until either the timeout, or the next start |
| 6274 | // activity request. |
| 6275 | mAppSwitchesAllowedTime = 0; |
| 6276 | } |
| 6277 | } |
| 6278 | |
| 6279 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 6280 | String name) { |
| 6281 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 6282 | return true; |
| 6283 | } |
| 6284 | |
| 6285 | final int perm = checkComponentPermission( |
| 6286 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6287 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6288 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 6289 | return true; |
| 6290 | } |
| 6291 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6292 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6293 | return false; |
| 6294 | } |
| 6295 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6296 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 6297 | boolean persistent) { |
| 6298 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 6299 | "setDebugApp()"); |
| 6300 | |
| 6301 | // Note that this is not really thread safe if there are multiple |
| 6302 | // callers into it at the same time, but that's not a situation we |
| 6303 | // care about. |
| 6304 | if (persistent) { |
| 6305 | final ContentResolver resolver = mContext.getContentResolver(); |
| 6306 | Settings.System.putString( |
| 6307 | resolver, Settings.System.DEBUG_APP, |
| 6308 | packageName); |
| 6309 | Settings.System.putInt( |
| 6310 | resolver, Settings.System.WAIT_FOR_DEBUGGER, |
| 6311 | waitForDebugger ? 1 : 0); |
| 6312 | } |
| 6313 | |
| 6314 | synchronized (this) { |
| 6315 | if (!persistent) { |
| 6316 | mOrigDebugApp = mDebugApp; |
| 6317 | mOrigWaitForDebugger = mWaitForDebugger; |
| 6318 | } |
| 6319 | mDebugApp = packageName; |
| 6320 | mWaitForDebugger = waitForDebugger; |
| 6321 | mDebugTransient = !persistent; |
| 6322 | if (packageName != null) { |
| 6323 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 6324 | forceStopPackageLocked(packageName, -1, false, false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6325 | Binder.restoreCallingIdentity(origId); |
| 6326 | } |
| 6327 | } |
| 6328 | } |
| 6329 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 6330 | void setProfileApp(ApplicationInfo app, String processName, String profileFile, |
| 6331 | ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
| 6332 | synchronized (this) { |
| 6333 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 6334 | if (!isDebuggable) { |
| 6335 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 6336 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 6337 | } |
| 6338 | } |
| 6339 | mProfileApp = processName; |
| 6340 | mProfileFile = profileFile; |
| 6341 | if (mProfileFd != null) { |
| 6342 | try { |
| 6343 | mProfileFd.close(); |
| 6344 | } catch (IOException e) { |
| 6345 | } |
| 6346 | mProfileFd = null; |
| 6347 | } |
| 6348 | mProfileFd = profileFd; |
| 6349 | mProfileType = 0; |
| 6350 | mAutoStopProfiler = autoStopProfiler; |
| 6351 | } |
| 6352 | } |
| 6353 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6354 | public void setAlwaysFinish(boolean enabled) { |
| 6355 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 6356 | "setAlwaysFinish()"); |
| 6357 | |
| 6358 | Settings.System.putInt( |
| 6359 | mContext.getContentResolver(), |
| 6360 | Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
| 6361 | |
| 6362 | synchronized (this) { |
| 6363 | mAlwaysFinishActivities = enabled; |
| 6364 | } |
| 6365 | } |
| 6366 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6367 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6368 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6369 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6370 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6371 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6372 | } |
| 6373 | } |
| 6374 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 6375 | public boolean isUserAMonkey() { |
| 6376 | // For now the fact that there is a controller implies |
| 6377 | // we have a monkey. |
| 6378 | synchronized (this) { |
| 6379 | return mController != null; |
| 6380 | } |
| 6381 | } |
| 6382 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6383 | public void registerActivityWatcher(IActivityWatcher watcher) { |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6384 | synchronized (this) { |
| 6385 | mWatchers.register(watcher); |
| 6386 | } |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6387 | } |
| 6388 | |
| 6389 | public void unregisterActivityWatcher(IActivityWatcher watcher) { |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6390 | synchronized (this) { |
| 6391 | mWatchers.unregister(watcher); |
| 6392 | } |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6393 | } |
| 6394 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 6395 | public void registerProcessObserver(IProcessObserver observer) { |
| 6396 | mProcessObservers.register(observer); |
| 6397 | } |
| 6398 | |
| 6399 | public void unregisterProcessObserver(IProcessObserver observer) { |
| 6400 | mProcessObservers.unregister(observer); |
| 6401 | } |
| 6402 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6403 | public void setImmersive(IBinder token, boolean immersive) { |
| 6404 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6405 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 6406 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6407 | throw new IllegalArgumentException(); |
| 6408 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6409 | r.immersive = immersive; |
| 6410 | } |
| 6411 | } |
| 6412 | |
| 6413 | public boolean isImmersive(IBinder token) { |
| 6414 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6415 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 6416 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6417 | throw new IllegalArgumentException(); |
| 6418 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6419 | return r.immersive; |
| 6420 | } |
| 6421 | } |
| 6422 | |
| 6423 | public boolean isTopActivityImmersive() { |
| 6424 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6425 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6426 | return (r != null) ? r.immersive : false; |
| 6427 | } |
| 6428 | } |
| 6429 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6430 | public final void enterSafeMode() { |
| 6431 | synchronized(this) { |
| 6432 | // It only makes sense to do this before the system is ready |
| 6433 | // and started launching other packages. |
| 6434 | if (!mSystemReady) { |
| 6435 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6436 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6437 | } catch (RemoteException e) { |
| 6438 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6439 | } |
| 6440 | } |
| 6441 | } |
| 6442 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 6443 | public final void showSafeModeOverlay() { |
| 6444 | View v = LayoutInflater.from(mContext).inflate( |
| 6445 | com.android.internal.R.layout.safe_mode, null); |
| 6446 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 6447 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 6448 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6449 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6450 | lp.gravity = Gravity.BOTTOM | Gravity.LEFT; |
| 6451 | lp.format = v.getBackground().getOpacity(); |
| 6452 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 6453 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 6454 | ((WindowManager)mContext.getSystemService( |
| 6455 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 6456 | } |
| 6457 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6458 | public void noteWakeupAlarm(IIntentSender sender) { |
| 6459 | if (!(sender instanceof PendingIntentRecord)) { |
| 6460 | return; |
| 6461 | } |
| 6462 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 6463 | synchronized (stats) { |
| 6464 | if (mBatteryStatsService.isOnBattery()) { |
| 6465 | mBatteryStatsService.enforceCallingPermission(); |
| 6466 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 6467 | int MY_UID = Binder.getCallingUid(); |
| 6468 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 6469 | BatteryStatsImpl.Uid.Pkg pkg = |
| 6470 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 6471 | pkg.incWakeupsLocked(); |
| 6472 | } |
| 6473 | } |
| 6474 | } |
| 6475 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6476 | public boolean killPids(int[] pids, String pReason, boolean secure) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6477 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6478 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6479 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6480 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6481 | // XXX Note: don't acquire main activity lock here, because the window |
| 6482 | // manager calls in with its locks held. |
| 6483 | |
| 6484 | boolean killed = false; |
| 6485 | synchronized (mPidsSelfLocked) { |
| 6486 | int[] types = new int[pids.length]; |
| 6487 | int worstType = 0; |
| 6488 | for (int i=0; i<pids.length; i++) { |
| 6489 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 6490 | if (proc != null) { |
| 6491 | int type = proc.setAdj; |
| 6492 | types[i] = type; |
| 6493 | if (type > worstType) { |
| 6494 | worstType = type; |
| 6495 | } |
| 6496 | } |
| 6497 | } |
| 6498 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6499 | // 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] | 6500 | // then constrain it so we will kill all hidden procs. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 6501 | if (worstType < ProcessList.EMPTY_APP_ADJ && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 6502 | worstType = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6503 | } |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6504 | |
| 6505 | // If this is not a secure call, don't let it kill processes that |
| 6506 | // are important. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 6507 | if (!secure && worstType < ProcessList.SECONDARY_SERVER_ADJ) { |
| 6508 | worstType = ProcessList.SECONDARY_SERVER_ADJ; |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6509 | } |
| 6510 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6511 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6512 | for (int i=0; i<pids.length; i++) { |
| 6513 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 6514 | if (proc == null) { |
| 6515 | continue; |
| 6516 | } |
| 6517 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6518 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 6519 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6520 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid, |
| 6521 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6522 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6523 | proc.killedBackground = true; |
| 6524 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6525 | } |
| 6526 | } |
| 6527 | } |
| 6528 | return killed; |
| 6529 | } |
| 6530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6531 | public final void startRunning(String pkg, String cls, String action, |
| 6532 | String data) { |
| 6533 | synchronized(this) { |
| 6534 | if (mStartRunning) { |
| 6535 | return; |
| 6536 | } |
| 6537 | mStartRunning = true; |
| 6538 | mTopComponent = pkg != null && cls != null |
| 6539 | ? new ComponentName(pkg, cls) : null; |
| 6540 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 6541 | mTopData = data; |
| 6542 | if (!mSystemReady) { |
| 6543 | return; |
| 6544 | } |
| 6545 | } |
| 6546 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6547 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6548 | } |
| 6549 | |
| 6550 | private void retrieveSettings() { |
| 6551 | final ContentResolver resolver = mContext.getContentResolver(); |
| 6552 | String debugApp = Settings.System.getString( |
| 6553 | resolver, Settings.System.DEBUG_APP); |
| 6554 | boolean waitForDebugger = Settings.System.getInt( |
| 6555 | resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0; |
| 6556 | boolean alwaysFinishActivities = Settings.System.getInt( |
| 6557 | resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
| 6558 | |
| 6559 | Configuration configuration = new Configuration(); |
| 6560 | Settings.System.getConfiguration(resolver, configuration); |
| 6561 | |
| 6562 | synchronized (this) { |
| 6563 | mDebugApp = mOrigDebugApp = debugApp; |
| 6564 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 6565 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 6566 | // This happens before any activities are started, so we can |
| 6567 | // change mConfiguration in-place. |
| 6568 | mConfiguration.updateFrom(configuration); |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 6569 | mConfigurationSeq = mConfiguration.seq = 1; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6570 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6571 | } |
| 6572 | } |
| 6573 | |
| 6574 | public boolean testIsSystemReady() { |
| 6575 | // no need to synchronize(this) just to read & return the value |
| 6576 | return mSystemReady; |
| 6577 | } |
| 6578 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6579 | private static File getCalledPreBootReceiversFile() { |
| 6580 | File dataDir = Environment.getDataDirectory(); |
| 6581 | File systemDir = new File(dataDir, "system"); |
| 6582 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 6583 | return fname; |
| 6584 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6585 | |
| 6586 | static final int LAST_DONE_VERSION = 10000; |
| 6587 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6588 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 6589 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 6590 | File file = getCalledPreBootReceiversFile(); |
| 6591 | FileInputStream fis = null; |
| 6592 | try { |
| 6593 | fis = new FileInputStream(file); |
| 6594 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6595 | int fvers = dis.readInt(); |
| 6596 | if (fvers == LAST_DONE_VERSION) { |
| 6597 | String vers = dis.readUTF(); |
| 6598 | String codename = dis.readUTF(); |
| 6599 | String build = dis.readUTF(); |
| 6600 | if (android.os.Build.VERSION.RELEASE.equals(vers) |
| 6601 | && android.os.Build.VERSION.CODENAME.equals(codename) |
| 6602 | && android.os.Build.VERSION.INCREMENTAL.equals(build)) { |
| 6603 | int num = dis.readInt(); |
| 6604 | while (num > 0) { |
| 6605 | num--; |
| 6606 | String pkg = dis.readUTF(); |
| 6607 | String cls = dis.readUTF(); |
| 6608 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 6609 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6610 | } |
| 6611 | } |
| 6612 | } catch (FileNotFoundException e) { |
| 6613 | } catch (IOException e) { |
| 6614 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 6615 | } finally { |
| 6616 | if (fis != null) { |
| 6617 | try { |
| 6618 | fis.close(); |
| 6619 | } catch (IOException e) { |
| 6620 | } |
| 6621 | } |
| 6622 | } |
| 6623 | return lastDoneReceivers; |
| 6624 | } |
| 6625 | |
| 6626 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 6627 | File file = getCalledPreBootReceiversFile(); |
| 6628 | FileOutputStream fos = null; |
| 6629 | DataOutputStream dos = null; |
| 6630 | try { |
| 6631 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 6632 | fos = new FileOutputStream(file); |
| 6633 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6634 | dos.writeInt(LAST_DONE_VERSION); |
| 6635 | dos.writeUTF(android.os.Build.VERSION.RELEASE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6636 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6637 | dos.writeUTF(android.os.Build.VERSION.INCREMENTAL); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6638 | dos.writeInt(list.size()); |
| 6639 | for (int i=0; i<list.size(); i++) { |
| 6640 | dos.writeUTF(list.get(i).getPackageName()); |
| 6641 | dos.writeUTF(list.get(i).getClassName()); |
| 6642 | } |
| 6643 | } catch (IOException e) { |
| 6644 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 6645 | file.delete(); |
| 6646 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 6647 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6648 | if (dos != null) { |
| 6649 | try { |
| 6650 | dos.close(); |
| 6651 | } catch (IOException e) { |
| 6652 | // TODO Auto-generated catch block |
| 6653 | e.printStackTrace(); |
| 6654 | } |
| 6655 | } |
| 6656 | } |
| 6657 | } |
| 6658 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6659 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6660 | synchronized(this) { |
| 6661 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6662 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6663 | return; |
| 6664 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6665 | |
| 6666 | // Check to see if there are any update receivers to run. |
| 6667 | if (!mDidUpdate) { |
| 6668 | if (mWaitingUpdate) { |
| 6669 | return; |
| 6670 | } |
| 6671 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 6672 | List<ResolveInfo> ris = null; |
| 6673 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6674 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6675 | intent, null, 0); |
| 6676 | } catch (RemoteException e) { |
| 6677 | } |
| 6678 | if (ris != null) { |
| 6679 | for (int i=ris.size()-1; i>=0; i--) { |
| 6680 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 6681 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6682 | ris.remove(i); |
| 6683 | } |
| 6684 | } |
| 6685 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6686 | |
| 6687 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
| 6688 | |
| 6689 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6690 | for (int i=0; i<ris.size(); i++) { |
| 6691 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6692 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 6693 | if (lastDoneReceivers.contains(comp)) { |
| 6694 | ris.remove(i); |
| 6695 | i--; |
| 6696 | } |
| 6697 | } |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 6698 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6699 | for (int i=0; i<ris.size(); i++) { |
| 6700 | ActivityInfo ai = ris.get(i).activityInfo; |
| 6701 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 6702 | doneReceivers.add(comp); |
| 6703 | intent.setComponent(comp); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6704 | IIntentReceiver finisher = null; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 6705 | if (i == ris.size()-1) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6706 | finisher = new IIntentReceiver.Stub() { |
| 6707 | public void performReceive(Intent intent, int resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 6708 | String data, Bundle extras, boolean ordered, |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 6709 | boolean sticky) { |
| 6710 | // The raw IIntentReceiver interface is called |
| 6711 | // with the AM lock held, so redispatch to |
| 6712 | // execute our code without the lock. |
| 6713 | mHandler.post(new Runnable() { |
| 6714 | public void run() { |
| 6715 | synchronized (ActivityManagerService.this) { |
| 6716 | mDidUpdate = true; |
| 6717 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6718 | writeLastDonePreBootReceivers(doneReceivers); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6719 | showBootMessage(mContext.getText( |
| 6720 | R.string.android_upgrading_complete), |
| 6721 | false); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 6722 | systemReady(goingCallback); |
| 6723 | } |
| 6724 | }); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6725 | } |
| 6726 | }; |
| 6727 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6728 | Slog.i(TAG, "Sending system update to: " + intent.getComponent()); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6729 | broadcastIntentLocked(null, null, intent, null, finisher, |
| 6730 | 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 6731 | if (finisher != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6732 | mWaitingUpdate = true; |
| 6733 | } |
| 6734 | } |
| 6735 | } |
| 6736 | if (mWaitingUpdate) { |
| 6737 | return; |
| 6738 | } |
| 6739 | mDidUpdate = true; |
| 6740 | } |
| 6741 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6742 | mSystemReady = true; |
| 6743 | if (!mStartRunning) { |
| 6744 | return; |
| 6745 | } |
| 6746 | } |
| 6747 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6748 | ArrayList<ProcessRecord> procsToKill = null; |
| 6749 | synchronized(mPidsSelfLocked) { |
| 6750 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 6751 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 6752 | if (!isAllowedWhileBooting(proc.info)){ |
| 6753 | if (procsToKill == null) { |
| 6754 | procsToKill = new ArrayList<ProcessRecord>(); |
| 6755 | } |
| 6756 | procsToKill.add(proc); |
| 6757 | } |
| 6758 | } |
| 6759 | } |
| 6760 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6761 | synchronized(this) { |
| 6762 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6763 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 6764 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6765 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 6766 | removeProcessLocked(proc, true, false); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6767 | } |
| 6768 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6769 | |
| 6770 | // Now that we have cleaned up any update processes, we |
| 6771 | // are ready to start launching real processes and know that |
| 6772 | // we won't trample on them any more. |
| 6773 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6774 | } |
| 6775 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6776 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6777 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6778 | SystemClock.uptimeMillis()); |
| 6779 | |
| 6780 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6781 | // Make sure we have no pre-ready processes sitting around. |
| 6782 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6783 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 6784 | ResolveInfo ri = mContext.getPackageManager() |
| 6785 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6786 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6787 | CharSequence errorMsg = null; |
| 6788 | if (ri != null) { |
| 6789 | ActivityInfo ai = ri.activityInfo; |
| 6790 | ApplicationInfo app = ai.applicationInfo; |
| 6791 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 6792 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 6793 | mTopData = null; |
| 6794 | mTopComponent = new ComponentName(app.packageName, |
| 6795 | ai.name); |
| 6796 | } else { |
| 6797 | errorMsg = mContext.getResources().getText( |
| 6798 | com.android.internal.R.string.factorytest_not_system); |
| 6799 | } |
| 6800 | } else { |
| 6801 | errorMsg = mContext.getResources().getText( |
| 6802 | com.android.internal.R.string.factorytest_no_action); |
| 6803 | } |
| 6804 | if (errorMsg != null) { |
| 6805 | mTopAction = null; |
| 6806 | mTopData = null; |
| 6807 | mTopComponent = null; |
| 6808 | Message msg = Message.obtain(); |
| 6809 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 6810 | msg.getData().putCharSequence("msg", errorMsg); |
| 6811 | mHandler.sendMessage(msg); |
| 6812 | } |
| 6813 | } |
| 6814 | } |
| 6815 | |
| 6816 | retrieveSettings(); |
| 6817 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6818 | if (goingCallback != null) goingCallback.run(); |
| 6819 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6820 | synchronized (this) { |
| 6821 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 6822 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6823 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6824 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6825 | if (apps != null) { |
| 6826 | int N = apps.size(); |
| 6827 | int i; |
| 6828 | for (i=0; i<N; i++) { |
| 6829 | ApplicationInfo info |
| 6830 | = (ApplicationInfo)apps.get(i); |
| 6831 | if (info != null && |
| 6832 | !info.packageName.equals("android")) { |
| 6833 | addAppLocked(info); |
| 6834 | } |
| 6835 | } |
| 6836 | } |
| 6837 | } catch (RemoteException ex) { |
| 6838 | // pm is in same process, this will never happen. |
| 6839 | } |
| 6840 | } |
| 6841 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6842 | // Start up initial activity. |
| 6843 | mBooting = true; |
| 6844 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6845 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6846 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6847 | Message msg = Message.obtain(); |
| 6848 | msg.what = SHOW_UID_ERROR_MSG; |
| 6849 | mHandler.sendMessage(msg); |
| 6850 | } |
| 6851 | } catch (RemoteException e) { |
| 6852 | } |
| 6853 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6854 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6855 | } |
| 6856 | } |
| 6857 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6858 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6859 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6860 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6861 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6862 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6863 | startAppProblemLocked(app); |
| 6864 | app.stopFreezingAllLocked(); |
| 6865 | return handleAppCrashLocked(app); |
| 6866 | } |
| 6867 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6868 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6869 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6870 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6871 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6872 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 6873 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6874 | startAppProblemLocked(app); |
| 6875 | app.stopFreezingAllLocked(); |
| 6876 | } |
| 6877 | |
| 6878 | /** |
| 6879 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 6880 | * |
| 6881 | * @param app The ProcessRecord in which the error occurred. |
| 6882 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 6883 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6884 | * @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] | 6885 | * @param shortMsg Short message describing the crash. |
| 6886 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6887 | * @param stackTrace Full crash stack trace, may be null. |
| 6888 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6889 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 6890 | */ |
| 6891 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6892 | 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] | 6893 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6894 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6895 | report.condition = condition; |
| 6896 | report.processName = app.processName; |
| 6897 | report.pid = app.pid; |
| 6898 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6899 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6900 | report.shortMsg = shortMsg; |
| 6901 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6902 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6903 | |
| 6904 | return report; |
| 6905 | } |
| 6906 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 6907 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6908 | synchronized (this) { |
| 6909 | app.crashing = false; |
| 6910 | app.crashingReport = null; |
| 6911 | app.notResponding = false; |
| 6912 | app.notRespondingReport = null; |
| 6913 | if (app.anrDialog == fromDialog) { |
| 6914 | app.anrDialog = null; |
| 6915 | } |
| 6916 | if (app.waitDialog == fromDialog) { |
| 6917 | app.waitDialog = null; |
| 6918 | } |
| 6919 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 6920 | handleAppCrashLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6921 | Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request"); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 6922 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 6923 | app.processName, app.setAdj, "user's request after error"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6924 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6925 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6926 | } |
| 6927 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 6928 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6929 | private boolean handleAppCrashLocked(ProcessRecord app) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6930 | long now = SystemClock.uptimeMillis(); |
| 6931 | |
| 6932 | Long crashTime = mProcessCrashTimes.get(app.info.processName, |
| 6933 | app.info.uid); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 6934 | if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6935 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6936 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6937 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6938 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6939 | app.info.processName, app.info.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6940 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 6941 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6942 | if (r.app == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6943 | Slog.w(TAG, " Force finishing activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6944 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6945 | 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] | 6946 | } |
| 6947 | } |
| 6948 | if (!app.persistent) { |
| 6949 | // We don't want to start this process again until the user |
| 6950 | // explicitly does so... but for persistent process, we really |
| 6951 | // need to keep it running. If a persistent process is actually |
| 6952 | // repeatedly crashing, then badness for everyone. |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6953 | EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.info.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6954 | app.info.processName); |
| 6955 | mBadProcesses.put(app.info.processName, app.info.uid, now); |
| 6956 | app.bad = true; |
| 6957 | mProcessCrashTimes.remove(app.info.processName, app.info.uid); |
| 6958 | app.removed = true; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 6959 | // Don't let services in this process be restarted and potentially |
| 6960 | // annoy the user repeatedly. Unless it is persistent, since those |
| 6961 | // processes run critical code. |
| 6962 | removeProcessLocked(app, false, false); |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 6963 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6964 | return false; |
| 6965 | } |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 6966 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6967 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6968 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6969 | if (r.app == app) { |
| 6970 | // If the top running activity is from this crashing |
| 6971 | // process, then terminate it to avoid getting in a loop. |
| 6972 | Slog.w(TAG, " Force finishing activity " |
| 6973 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6974 | int index = mMainStack.indexOfTokenLocked(r); |
| 6975 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6976 | Activity.RESULT_CANCELED, null, "crashed"); |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 6977 | // Also terminate any activities below it that aren't yet |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6978 | // stopped, to avoid a situation where one will get |
| 6979 | // re-start our crashing activity once it gets resumed again. |
| 6980 | index--; |
| 6981 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6982 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6983 | if (r.state == ActivityState.RESUMED |
| 6984 | || r.state == ActivityState.PAUSING |
| 6985 | || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 6986 | if (!r.isHomeActivity || mHomeProcess != r.app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6987 | Slog.w(TAG, " Force finishing activity " |
| 6988 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6989 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6990 | Activity.RESULT_CANCELED, null, "crashed"); |
| 6991 | } |
| 6992 | } |
| 6993 | } |
| 6994 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6995 | } |
| 6996 | |
| 6997 | // Bump up the crash count of any services currently running in the proc. |
| 6998 | if (app.services.size() != 0) { |
| 6999 | // Any services running in the application need to be placed |
| 7000 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7001 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7002 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7003 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7004 | sr.crashCount++; |
| 7005 | } |
| 7006 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7007 | |
| 7008 | // If the crashing process is what we consider to be the "home process" and it has been |
| 7009 | // replaced by a third-party app, clear the package preferred activities from packages |
| 7010 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 7011 | // from blocking the user to manually clear the list. |
| 7012 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 7013 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7014 | Iterator it = mHomeProcess.activities.iterator(); |
| 7015 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 7016 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7017 | if (r.isHomeActivity) { |
| 7018 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 7019 | try { |
| 7020 | ActivityThread.getPackageManager() |
| 7021 | .clearPackagePreferredActivities(r.packageName); |
| 7022 | } catch (RemoteException c) { |
| 7023 | // pm is in same process, this will never happen. |
| 7024 | } |
| 7025 | } |
| 7026 | } |
| 7027 | } |
| 7028 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7029 | mProcessCrashTimes.put(app.info.processName, app.info.uid, now); |
| 7030 | return true; |
| 7031 | } |
| 7032 | |
| 7033 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 7034 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 7035 | mContext, app.info.packageName, app.info.flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7036 | skipCurrentReceiverLocked(app); |
| 7037 | } |
| 7038 | |
| 7039 | void skipCurrentReceiverLocked(ProcessRecord app) { |
| 7040 | boolean reschedule = false; |
| 7041 | BroadcastRecord r = app.curReceiver; |
| 7042 | if (r != null) { |
| 7043 | // The current broadcast is waiting for this app's receiver |
| 7044 | // to be finished. Looks like that's not going to happen, so |
| 7045 | // let the broadcast continue. |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 7046 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7047 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 7048 | r.resultExtras, r.resultAbort, true); |
| 7049 | reschedule = true; |
| 7050 | } |
| 7051 | r = mPendingBroadcast; |
| 7052 | if (r != null && r.curApp == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7053 | if (DEBUG_BROADCAST) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7054 | "skip & discard pending app " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 7055 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7056 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 7057 | r.resultExtras, r.resultAbort, true); |
| 7058 | reschedule = true; |
| 7059 | } |
| 7060 | if (reschedule) { |
| 7061 | scheduleBroadcastsLocked(); |
| 7062 | } |
| 7063 | } |
| 7064 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7065 | /** |
| 7066 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 7067 | * The application process will exit immediately after this call returns. |
| 7068 | * @param app object of the crashing app, null for the system server |
| 7069 | * @param crashInfo describing the exception |
| 7070 | */ |
| 7071 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7072 | ProcessRecord r = findAppProcess(app, "Crash"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7073 | |
| 7074 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
| 7075 | app == null ? "system" : (r == null ? "unknown" : r.processName), |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7076 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7077 | crashInfo.exceptionClassName, |
| 7078 | crashInfo.exceptionMessage, |
| 7079 | crashInfo.throwFileName, |
| 7080 | crashInfo.throwLineNumber); |
| 7081 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7082 | addErrorToDropBox("crash", r, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7083 | |
| 7084 | crashApplication(r, crashInfo); |
| 7085 | } |
| 7086 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7087 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7088 | IBinder app, |
| 7089 | int violationMask, |
| 7090 | StrictMode.ViolationInfo info) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7091 | ProcessRecord r = findAppProcess(app, "StrictMode"); |
| 7092 | if (r == null) { |
| 7093 | return; |
| 7094 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7095 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7096 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 7097 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7098 | boolean logIt = true; |
| 7099 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 7100 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 7101 | logIt = false; |
| 7102 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7103 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7104 | // the relative pain numbers, without logging all |
| 7105 | // the stack traces repeatedly. We'd want to do |
| 7106 | // likewise in the client code, which also does |
| 7107 | // dup suppression, before the Binder call. |
| 7108 | } else { |
| 7109 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 7110 | mAlreadyLoggedViolatedStacks.clear(); |
| 7111 | } |
| 7112 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 7113 | } |
| 7114 | } |
| 7115 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7116 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7117 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7118 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7119 | |
| 7120 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 7121 | AppErrorResult result = new AppErrorResult(); |
| 7122 | synchronized (this) { |
| 7123 | final long origId = Binder.clearCallingIdentity(); |
| 7124 | |
| 7125 | Message msg = Message.obtain(); |
| 7126 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 7127 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 7128 | data.put("result", result); |
| 7129 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7130 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7131 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7132 | msg.obj = data; |
| 7133 | mHandler.sendMessage(msg); |
| 7134 | |
| 7135 | Binder.restoreCallingIdentity(origId); |
| 7136 | } |
| 7137 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 7138 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7139 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7140 | } |
| 7141 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7142 | // Depending on the policy in effect, there could be a bunch of |
| 7143 | // these in quick succession so we try to batch these together to |
| 7144 | // minimize disk writes, number of dropbox entries, and maximize |
| 7145 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7146 | private void logStrictModeViolationToDropBox( |
| 7147 | ProcessRecord process, |
| 7148 | StrictMode.ViolationInfo info) { |
| 7149 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7150 | return; |
| 7151 | } |
| 7152 | final boolean isSystemApp = process == null || |
| 7153 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 7154 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
| 7155 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 7156 | final DropBoxManager dbox = (DropBoxManager) |
| 7157 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7158 | |
| 7159 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7160 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7161 | |
| 7162 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7163 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7164 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 7165 | synchronized (sb) { |
| 7166 | bufferWasEmpty = sb.length() == 0; |
| 7167 | appendDropBoxProcessHeaders(process, sb); |
| 7168 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 7169 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7170 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 7171 | if (info.violationNumThisLoop != 0) { |
| 7172 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 7173 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 7174 | if (info.numAnimationsRunning != 0) { |
| 7175 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 7176 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 7177 | if (info.broadcastIntentAction != null) { |
| 7178 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 7179 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7180 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7181 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7182 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 7183 | if (info.numInstances != -1) { |
| 7184 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 7185 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7186 | if (info.tags != null) { |
| 7187 | for (String tag : info.tags) { |
| 7188 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 7189 | } |
| 7190 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7191 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7192 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 7193 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7194 | } |
| 7195 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7196 | |
| 7197 | // Only buffer up to ~64k. Various logging bits truncate |
| 7198 | // things at 128k. |
| 7199 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7200 | } |
| 7201 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7202 | // Flush immediately if the buffer's grown too large, or this |
| 7203 | // is a non-system app. Non-system apps are isolated with a |
| 7204 | // different tag & policy and not batched. |
| 7205 | // |
| 7206 | // Batching is useful during internal testing with |
| 7207 | // StrictMode settings turned up high. Without batching, |
| 7208 | // thousands of separate files could be created on boot. |
| 7209 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7210 | new Thread("Error dump: " + dropboxTag) { |
| 7211 | @Override |
| 7212 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7213 | String report; |
| 7214 | synchronized (sb) { |
| 7215 | report = sb.toString(); |
| 7216 | sb.delete(0, sb.length()); |
| 7217 | sb.trimToSize(); |
| 7218 | } |
| 7219 | if (report.length() != 0) { |
| 7220 | dbox.addText(dropboxTag, report); |
| 7221 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7222 | } |
| 7223 | }.start(); |
| 7224 | return; |
| 7225 | } |
| 7226 | |
| 7227 | // System app batching: |
| 7228 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7229 | // An existing dropbox-writing thread is outstanding, so |
| 7230 | // we don't need to start it up. The existing thread will |
| 7231 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7232 | return; |
| 7233 | } |
| 7234 | |
| 7235 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 7236 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7237 | new Thread("Error dump: " + dropboxTag) { |
| 7238 | @Override |
| 7239 | public void run() { |
| 7240 | // 5 second sleep to let stacks arrive and be batched together |
| 7241 | try { |
| 7242 | Thread.sleep(5000); // 5 seconds |
| 7243 | } catch (InterruptedException e) {} |
| 7244 | |
| 7245 | String errorReport; |
| 7246 | synchronized (mStrictModeBuffer) { |
| 7247 | errorReport = mStrictModeBuffer.toString(); |
| 7248 | if (errorReport.length() == 0) { |
| 7249 | return; |
| 7250 | } |
| 7251 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 7252 | mStrictModeBuffer.trimToSize(); |
| 7253 | } |
| 7254 | dbox.addText(dropboxTag, errorReport); |
| 7255 | } |
| 7256 | }.start(); |
| 7257 | } |
| 7258 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7259 | /** |
| 7260 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 7261 | * @param app object of the crashing app, null for the system server |
| 7262 | * @param tag reported by the caller |
| 7263 | * @param crashInfo describing the context of the error |
| 7264 | * @return true if the process should exit immediately (WTF is fatal) |
| 7265 | */ |
| 7266 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7267 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7268 | ProcessRecord r = findAppProcess(app, "WTF"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7269 | |
| 7270 | EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(), |
| 7271 | app == null ? "system" : (r == null ? "unknown" : r.processName), |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7272 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7273 | tag, crashInfo.exceptionMessage); |
| 7274 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7275 | addErrorToDropBox("wtf", r, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7276 | |
Dianne Hackborn | 1ab4377 | 2011-03-15 14:38:02 -0700 | [diff] [blame] | 7277 | if (r != null && r.pid != Process.myPid() && |
| 7278 | Settings.Secure.getInt(mContext.getContentResolver(), |
| 7279 | Settings.Secure.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7280 | crashApplication(r, crashInfo); |
| 7281 | return true; |
| 7282 | } else { |
| 7283 | return false; |
| 7284 | } |
| 7285 | } |
| 7286 | |
| 7287 | /** |
| 7288 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 7289 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 7290 | */ |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7291 | private ProcessRecord findAppProcess(IBinder app, String reason) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7292 | if (app == null) { |
| 7293 | return null; |
| 7294 | } |
| 7295 | |
| 7296 | synchronized (this) { |
| 7297 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 7298 | final int NA = apps.size(); |
| 7299 | for (int ia=0; ia<NA; ia++) { |
| 7300 | ProcessRecord p = apps.valueAt(ia); |
| 7301 | if (p.thread != null && p.thread.asBinder() == app) { |
| 7302 | return p; |
| 7303 | } |
| 7304 | } |
| 7305 | } |
| 7306 | |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7307 | Slog.w(TAG, "Can't find mystery application for " + reason |
| 7308 | + " from pid=" + Binder.getCallingPid() |
| 7309 | + " uid=" + Binder.getCallingUid() + ": " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7310 | return null; |
| 7311 | } |
| 7312 | } |
| 7313 | |
| 7314 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7315 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 7316 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7317 | */ |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 7318 | private void appendDropBoxProcessHeaders(ProcessRecord process, StringBuilder sb) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 7319 | // Watchdog thread ends up invoking this function (with |
| 7320 | // a null ProcessRecord) to add the stack file to dropbox. |
| 7321 | // Do not acquire a lock on this (am) in such cases, as it |
| 7322 | // could cause a potential deadlock, if and when watchdog |
| 7323 | // is invoked due to unavailability of lock on am and it |
| 7324 | // would prevent watchdog from killing system_server. |
| 7325 | if (process == null) { |
| 7326 | sb.append("Process: system_server\n"); |
| 7327 | return; |
| 7328 | } |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 7329 | // Note: ProcessRecord 'process' is guarded by the service |
| 7330 | // instance. (notably process.pkgList, which could otherwise change |
| 7331 | // concurrently during execution of this method) |
| 7332 | synchronized (this) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 7333 | if (process.pid == MY_PID) { |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 7334 | sb.append("Process: system_server\n"); |
| 7335 | } else { |
| 7336 | sb.append("Process: ").append(process.processName).append("\n"); |
| 7337 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7338 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7339 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7340 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 7341 | for (String pkg : process.pkgList) { |
| 7342 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7343 | try { |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7344 | PackageInfo pi = pm.getPackageInfo(pkg, 0); |
| 7345 | if (pi != null) { |
| 7346 | sb.append(" v").append(pi.versionCode); |
| 7347 | if (pi.versionName != null) { |
| 7348 | sb.append(" (").append(pi.versionName).append(")"); |
| 7349 | } |
| 7350 | } |
| 7351 | } catch (RemoteException e) { |
| 7352 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7353 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7354 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7355 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7356 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7357 | } |
| 7358 | |
| 7359 | private static String processClass(ProcessRecord process) { |
| 7360 | if (process == null || process.pid == MY_PID) { |
| 7361 | return "system_server"; |
| 7362 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7363 | return "system_app"; |
| 7364 | } else { |
| 7365 | return "data_app"; |
| 7366 | } |
| 7367 | } |
| 7368 | |
| 7369 | /** |
| 7370 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 7371 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 7372 | * @param process which caused the error, null means the system server |
| 7373 | * @param activity which triggered the error, null if unknown |
| 7374 | * @param parent activity related to the error, null if unknown |
| 7375 | * @param subject line related to the error, null if absent |
| 7376 | * @param report in long form describing the error, null if absent |
| 7377 | * @param logFile to include in the report, null if none |
| 7378 | * @param crashInfo giving an application stack trace, null if absent |
| 7379 | */ |
| 7380 | public void addErrorToDropBox(String eventType, |
| 7381 | ProcessRecord process, ActivityRecord activity, ActivityRecord parent, String subject, |
| 7382 | final String report, final File logFile, |
| 7383 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 7384 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 7385 | // otherwise the watchdog may be prevented from resetting the system. |
| 7386 | |
| 7387 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 7388 | final DropBoxManager dbox = (DropBoxManager) |
| 7389 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7390 | |
| 7391 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7392 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7393 | |
| 7394 | final StringBuilder sb = new StringBuilder(1024); |
| 7395 | appendDropBoxProcessHeaders(process, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7396 | if (activity != null) { |
| 7397 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 7398 | } |
| 7399 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 7400 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 7401 | } |
| 7402 | if (parent != null && parent != activity) { |
| 7403 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 7404 | } |
| 7405 | if (subject != null) { |
| 7406 | sb.append("Subject: ").append(subject).append("\n"); |
| 7407 | } |
| 7408 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 7409 | if (Debug.isDebuggerConnected()) { |
| 7410 | sb.append("Debugger: Connected\n"); |
| 7411 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7412 | sb.append("\n"); |
| 7413 | |
| 7414 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 7415 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7416 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 7417 | @Override |
| 7418 | public void run() { |
| 7419 | if (report != null) { |
| 7420 | sb.append(report); |
| 7421 | } |
| 7422 | if (logFile != null) { |
| 7423 | try { |
| 7424 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 7425 | } catch (IOException e) { |
| 7426 | Slog.e(TAG, "Error reading " + logFile, e); |
| 7427 | } |
| 7428 | } |
| 7429 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 7430 | sb.append(crashInfo.stackTrace); |
| 7431 | } |
| 7432 | |
| 7433 | String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 7434 | int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0); |
| 7435 | if (lines > 0) { |
| 7436 | sb.append("\n"); |
| 7437 | |
| 7438 | // Merge several logcat streams, and take the last N lines |
| 7439 | InputStreamReader input = null; |
| 7440 | try { |
| 7441 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 7442 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 7443 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 7444 | |
| 7445 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 7446 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 7447 | input = new InputStreamReader(logcat.getInputStream()); |
| 7448 | |
| 7449 | int num; |
| 7450 | char[] buf = new char[8192]; |
| 7451 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 7452 | } catch (IOException e) { |
| 7453 | Slog.e(TAG, "Error running logcat", e); |
| 7454 | } finally { |
| 7455 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 7456 | } |
| 7457 | } |
| 7458 | |
| 7459 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7460 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7461 | }; |
| 7462 | |
| 7463 | if (process == null || process.pid == MY_PID) { |
| 7464 | worker.run(); // We may be about to die -- need to run this synchronously |
| 7465 | } else { |
| 7466 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7467 | } |
| 7468 | } |
| 7469 | |
| 7470 | /** |
| 7471 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 7472 | * Deal with edge cases (intercepts from instrumented applications, |
| 7473 | * ActivityController, error intent receivers, that sort of thing). |
| 7474 | * @param r the application crashing |
| 7475 | * @param crashInfo describing the failure |
| 7476 | */ |
| 7477 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7478 | long timeMillis = System.currentTimeMillis(); |
| 7479 | String shortMsg = crashInfo.exceptionClassName; |
| 7480 | String longMsg = crashInfo.exceptionMessage; |
| 7481 | String stackTrace = crashInfo.stackTrace; |
| 7482 | if (shortMsg != null && longMsg != null) { |
| 7483 | longMsg = shortMsg + ": " + longMsg; |
| 7484 | } else if (shortMsg != null) { |
| 7485 | longMsg = shortMsg; |
| 7486 | } |
| 7487 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7488 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7489 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7490 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7491 | try { |
| 7492 | String name = r != null ? r.processName : null; |
| 7493 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7494 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7495 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7496 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7497 | + " at watcher's request"); |
| 7498 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7499 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7500 | } |
| 7501 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7502 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7503 | } |
| 7504 | } |
| 7505 | |
| 7506 | final long origId = Binder.clearCallingIdentity(); |
| 7507 | |
| 7508 | // If this process is running instrumentation, finish it. |
| 7509 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7510 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7511 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7512 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 7513 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7514 | Bundle info = new Bundle(); |
| 7515 | info.putString("shortMsg", shortMsg); |
| 7516 | info.putString("longMsg", longMsg); |
| 7517 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 7518 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7519 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7520 | } |
| 7521 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7522 | // If we can't identify the process or it's already exceeded its crash quota, |
| 7523 | // quit right away without showing a crash dialog. |
| 7524 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7525 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7526 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7527 | } |
| 7528 | |
| 7529 | Message msg = Message.obtain(); |
| 7530 | msg.what = SHOW_ERROR_MSG; |
| 7531 | HashMap data = new HashMap(); |
| 7532 | data.put("result", result); |
| 7533 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7534 | msg.obj = data; |
| 7535 | mHandler.sendMessage(msg); |
| 7536 | |
| 7537 | Binder.restoreCallingIdentity(origId); |
| 7538 | } |
| 7539 | |
| 7540 | int res = result.get(); |
| 7541 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7542 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7543 | synchronized (this) { |
| 7544 | if (r != null) { |
| 7545 | mProcessCrashTimes.put(r.info.processName, r.info.uid, |
| 7546 | SystemClock.uptimeMillis()); |
| 7547 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7548 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7549 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7550 | } |
| 7551 | } |
| 7552 | |
| 7553 | if (appErrorIntent != null) { |
| 7554 | try { |
| 7555 | mContext.startActivity(appErrorIntent); |
| 7556 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7557 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7558 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7559 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7560 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7561 | |
| 7562 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 7563 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 7564 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7565 | if (report == null) { |
| 7566 | return null; |
| 7567 | } |
| 7568 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 7569 | result.setComponent(r.errorReportReceiver); |
| 7570 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 7571 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 7572 | return result; |
| 7573 | } |
| 7574 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7575 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 7576 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7577 | if (r.errorReportReceiver == null) { |
| 7578 | return null; |
| 7579 | } |
| 7580 | |
| 7581 | if (!r.crashing && !r.notResponding) { |
| 7582 | return null; |
| 7583 | } |
| 7584 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7585 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 7586 | report.packageName = r.info.packageName; |
| 7587 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 7588 | report.processName = r.processName; |
| 7589 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 7590 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7591 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7592 | if (r.crashing) { |
| 7593 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 7594 | report.crashInfo = crashInfo; |
| 7595 | } else if (r.notResponding) { |
| 7596 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 7597 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7598 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7599 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 7600 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 7601 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7602 | } |
| 7603 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7604 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7605 | } |
| 7606 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7607 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
| 7608 | // assume our apps are happy - lazy create the list |
| 7609 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 7610 | |
| 7611 | synchronized (this) { |
| 7612 | |
| 7613 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7614 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 7615 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7616 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 7617 | // This one's in trouble, so we'll generate a report for it |
| 7618 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 7619 | ActivityManager.ProcessErrorStateInfo report = null; |
| 7620 | if (app.crashing) { |
| 7621 | report = app.crashingReport; |
| 7622 | } else if (app.notResponding) { |
| 7623 | report = app.notRespondingReport; |
| 7624 | } |
| 7625 | |
| 7626 | if (report != null) { |
| 7627 | if (errList == null) { |
| 7628 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 7629 | } |
| 7630 | errList.add(report); |
| 7631 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7632 | 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] | 7633 | " crashing = " + app.crashing + |
| 7634 | " notResponding = " + app.notResponding); |
| 7635 | } |
| 7636 | } |
| 7637 | } |
| 7638 | } |
| 7639 | |
| 7640 | return errList; |
| 7641 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7642 | |
| 7643 | static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) { |
| 7644 | if (adj >= ProcessList.EMPTY_APP_ADJ) { |
| 7645 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_EMPTY; |
| 7646 | } else if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 7647 | if (currApp != null) { |
| 7648 | currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
| 7649 | } |
| 7650 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 7651 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 7652 | if (currApp != null) { |
| 7653 | currApp.lru = 0; |
| 7654 | } |
| 7655 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 7656 | } else if (adj >= ProcessList.SECONDARY_SERVER_ADJ) { |
| 7657 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 7658 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 7659 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 7660 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 7661 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 7662 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 7663 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 7664 | } else { |
| 7665 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 7666 | } |
| 7667 | } |
| 7668 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7669 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
| 7670 | // Lazy instantiation of list |
| 7671 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
| 7672 | synchronized (this) { |
| 7673 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7674 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 7675 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7676 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 7677 | // Generate process state info for running application |
| 7678 | ActivityManager.RunningAppProcessInfo currApp = |
| 7679 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 7680 | app.pid, app.getPackageList()); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 7681 | currApp.uid = app.info.uid; |
Dianne Hackborn | baf42c6 | 2010-06-24 11:23:39 -0700 | [diff] [blame] | 7682 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 482566e | 2010-09-03 12:51:28 -0700 | [diff] [blame] | 7683 | currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
Dianne Hackborn | baf42c6 | 2010-06-24 11:23:39 -0700 | [diff] [blame] | 7684 | } |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 7685 | if (app.persistent) { |
| 7686 | currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 7687 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7688 | int adj = app.curAdj; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7689 | currApp.importance = oomAdjToImportance(adj, currApp); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 7690 | currApp.importanceReasonCode = app.adjTypeCode; |
| 7691 | if (app.adjSource instanceof ProcessRecord) { |
| 7692 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7693 | currApp.importanceReasonImportance = oomAdjToImportance( |
| 7694 | app.adjSourceOom, null); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7695 | } else if (app.adjSource instanceof ActivityRecord) { |
| 7696 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 7697 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 7698 | } |
| 7699 | if (app.adjTarget instanceof ComponentName) { |
| 7700 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 7701 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7702 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7703 | // + " lru=" + currApp.lru); |
| 7704 | if (runList == null) { |
| 7705 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 7706 | } |
| 7707 | runList.add(currApp); |
| 7708 | } |
| 7709 | } |
| 7710 | } |
| 7711 | return runList; |
| 7712 | } |
| 7713 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7714 | public List<ApplicationInfo> getRunningExternalApplications() { |
| 7715 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 7716 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 7717 | if (runningApps != null && runningApps.size() > 0) { |
| 7718 | Set<String> extList = new HashSet<String>(); |
| 7719 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 7720 | if (app.pkgList != null) { |
| 7721 | for (String pkg : app.pkgList) { |
| 7722 | extList.add(pkg); |
| 7723 | } |
| 7724 | } |
| 7725 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7726 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7727 | for (String pkg : extList) { |
| 7728 | try { |
| 7729 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0); |
| 7730 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 7731 | retList.add(info); |
| 7732 | } |
| 7733 | } catch (RemoteException e) { |
| 7734 | } |
| 7735 | } |
| 7736 | } |
| 7737 | return retList; |
| 7738 | } |
| 7739 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7740 | @Override |
| 7741 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7742 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 7743 | != PackageManager.PERMISSION_GRANTED) { |
| 7744 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 7745 | + Binder.getCallingPid() |
| 7746 | + ", uid=" + Binder.getCallingUid() |
| 7747 | + " without permission " |
| 7748 | + android.Manifest.permission.DUMP); |
| 7749 | return; |
| 7750 | } |
| 7751 | |
| 7752 | boolean dumpAll = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7753 | boolean dumpClient = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7754 | |
| 7755 | int opti = 0; |
| 7756 | while (opti < args.length) { |
| 7757 | String opt = args[opti]; |
| 7758 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 7759 | break; |
| 7760 | } |
| 7761 | opti++; |
| 7762 | if ("-a".equals(opt)) { |
| 7763 | dumpAll = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7764 | } else if ("-c".equals(opt)) { |
| 7765 | dumpClient = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7766 | } else if ("-h".equals(opt)) { |
| 7767 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7768 | pw.println(" [-a] [-c] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7769 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7770 | pw.println(" a[ctivities]: activity stack state"); |
| 7771 | pw.println(" b[roadcasts]: broadcast state"); |
| 7772 | pw.println(" i[ntents]: pending intent state"); |
| 7773 | pw.println(" p[rocesses]: process state"); |
| 7774 | pw.println(" o[om]: out of memory management"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 7775 | pw.println(" prov[iders] [COMP_SPEC ...]: content provider state"); |
| 7776 | pw.println(" s[ervices] [COMP_SPEC ...]: service state"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7777 | pw.println(" service [COMP_SPEC]: service client-side state"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 7778 | pw.println(" all: dump all activities"); |
| 7779 | pw.println(" top: dump the top activity"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7780 | pw.println(" cmd may also be a COMP_SPEC to dump activities."); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 7781 | pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),"); |
| 7782 | pw.println(" a partial substring in a component name, a"); |
| 7783 | pw.println(" hex object identifier."); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7784 | pw.println(" -a: include all available server state."); |
| 7785 | pw.println(" -c: include client state."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7786 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7787 | } else { |
| 7788 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7789 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7790 | } |
| 7791 | |
| 7792 | // Is the caller requesting to dump a particular piece of data? |
| 7793 | if (opti < args.length) { |
| 7794 | String cmd = args[opti]; |
| 7795 | opti++; |
| 7796 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 7797 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7798 | dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7799 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7800 | return; |
| 7801 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
| 7802 | synchronized (this) { |
| 7803 | dumpBroadcastsLocked(fd, pw, args, opti, true); |
| 7804 | } |
| 7805 | return; |
| 7806 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
| 7807 | synchronized (this) { |
| 7808 | dumpPendingIntentsLocked(fd, pw, args, opti, true); |
| 7809 | } |
| 7810 | return; |
| 7811 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
| 7812 | synchronized (this) { |
| 7813 | dumpProcessesLocked(fd, pw, args, opti, true); |
| 7814 | } |
| 7815 | return; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7816 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 7817 | synchronized (this) { |
| 7818 | dumpOomLocked(fd, pw, args, opti, true); |
| 7819 | } |
| 7820 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7821 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 7822 | synchronized (this) { |
| 7823 | dumpProvidersLocked(fd, pw, args, opti, true); |
| 7824 | } |
| 7825 | return; |
| 7826 | } else if ("service".equals(cmd)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7827 | String[] newArgs; |
| 7828 | String name; |
| 7829 | if (opti >= args.length) { |
| 7830 | name = null; |
| 7831 | newArgs = EMPTY_STRING_ARRAY; |
| 7832 | } else { |
| 7833 | name = args[opti]; |
| 7834 | opti++; |
| 7835 | newArgs = new String[args.length - opti]; |
| 7836 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 7837 | } |
| 7838 | if (!dumpService(fd, pw, name, newArgs, 0, dumpAll)) { |
| 7839 | pw.println("No services match: " + name); |
| 7840 | pw.println("Use -h for help."); |
| 7841 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7842 | return; |
| 7843 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 7844 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7845 | dumpServicesLocked(fd, pw, args, opti, true, dumpClient); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7846 | } |
| 7847 | return; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 7848 | } else { |
| 7849 | // Dumping a single activity? |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7850 | if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 7851 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 7852 | pw.println("Use -h for help."); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 7853 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 7854 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7855 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7856 | } |
| 7857 | |
| 7858 | // No piece of data specified, dump everything. |
| 7859 | synchronized (this) { |
| 7860 | boolean needSep; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7861 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7862 | if (needSep) { |
| 7863 | pw.println(" "); |
| 7864 | } |
| 7865 | if (dumpAll) { |
| 7866 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7867 | } |
| 7868 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll); |
| 7869 | if (needSep) { |
| 7870 | pw.println(" "); |
| 7871 | } |
| 7872 | if (dumpAll) { |
| 7873 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7874 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7875 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7876 | if (needSep) { |
| 7877 | pw.println(" "); |
| 7878 | } |
| 7879 | if (dumpAll) { |
| 7880 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7881 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7882 | needSep = dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7883 | if (needSep) { |
| 7884 | pw.println(" "); |
| 7885 | } |
| 7886 | if (dumpAll) { |
| 7887 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7888 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7889 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7890 | if (needSep) { |
| 7891 | pw.println(" "); |
| 7892 | } |
| 7893 | if (dumpAll) { |
| 7894 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7895 | } |
| 7896 | dumpProcessesLocked(fd, pw, args, opti, dumpAll); |
| 7897 | } |
| 7898 | } |
| 7899 | |
| 7900 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7901 | int opti, boolean dumpAll, boolean dumpClient) { |
| 7902 | pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)"); |
| 7903 | pw.println(" Main stack:"); |
| 7904 | dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7905 | pw.println(" "); |
| 7906 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7907 | dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7908 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7909 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7910 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7911 | dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false, |
| 7912 | !dumpAll, false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7913 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7914 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7915 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7916 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7917 | dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false, |
| 7918 | !dumpAll, false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7919 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7920 | if (mMainStack.mGoingToSleepActivities.size() > 0) { |
| 7921 | pw.println(" "); |
| 7922 | pw.println(" Activities waiting to sleep:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7923 | dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false, |
| 7924 | !dumpAll, false); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7925 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7926 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7927 | pw.println(" "); |
| 7928 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7929 | dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false, |
| 7930 | !dumpAll, false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7931 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7932 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7933 | pw.println(" "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7934 | if (mMainStack.mPausingActivity != null) { |
| 7935 | pw.println(" mPausingActivity: " + mMainStack.mPausingActivity); |
| 7936 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7937 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7938 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7939 | if (dumpAll) { |
| 7940 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
| 7941 | pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 7942 | pw.println(" mDismissKeyguardOnNextActivity: " |
| 7943 | + mMainStack.mDismissKeyguardOnNextActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7944 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7945 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7946 | if (mRecentTasks.size() > 0) { |
| 7947 | pw.println(); |
| 7948 | pw.println(" Recent tasks:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7949 | |
| 7950 | final int N = mRecentTasks.size(); |
| 7951 | for (int i=0; i<N; i++) { |
| 7952 | TaskRecord tr = mRecentTasks.get(i); |
| 7953 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 7954 | pw.println(tr); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7955 | if (dumpAll) { |
| 7956 | mRecentTasks.get(i).dump(pw, " "); |
| 7957 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7958 | } |
| 7959 | } |
| 7960 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7961 | if (dumpAll) { |
| 7962 | pw.println(" "); |
| 7963 | pw.println(" mCurTask: " + mCurTask); |
| 7964 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7965 | |
| 7966 | return true; |
| 7967 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7968 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7969 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 7970 | int opti, boolean dumpAll) { |
| 7971 | boolean needSep = false; |
| 7972 | int numPers = 0; |
| 7973 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7974 | pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)"); |
| 7975 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7976 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7977 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 7978 | final int NA = procs.size(); |
| 7979 | for (int ia=0; ia<NA; ia++) { |
| 7980 | if (!needSep) { |
| 7981 | pw.println(" All known processes:"); |
| 7982 | needSep = true; |
| 7983 | } |
| 7984 | ProcessRecord r = procs.valueAt(ia); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 7985 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 7986 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 7987 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7988 | r.dump(pw, " "); |
| 7989 | if (r.persistent) { |
| 7990 | numPers++; |
| 7991 | } |
| 7992 | } |
| 7993 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7994 | } |
| 7995 | |
| 7996 | if (mLruProcesses.size() > 0) { |
| 7997 | if (needSep) pw.println(" "); |
| 7998 | needSep = true; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7999 | pw.println(" Process LRU list (sorted by oom_adj):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8000 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
| 8001 | "Proc", "PERS", false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8002 | needSep = true; |
| 8003 | } |
| 8004 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8005 | if (dumpAll) { |
| 8006 | synchronized (mPidsSelfLocked) { |
| 8007 | if (mPidsSelfLocked.size() > 0) { |
| 8008 | if (needSep) pw.println(" "); |
| 8009 | needSep = true; |
| 8010 | pw.println(" PID mappings:"); |
| 8011 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 8012 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 8013 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
| 8014 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8015 | } |
| 8016 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8017 | } |
| 8018 | |
| 8019 | if (mForegroundProcesses.size() > 0) { |
| 8020 | if (needSep) pw.println(" "); |
| 8021 | needSep = true; |
| 8022 | pw.println(" Foreground Processes:"); |
| 8023 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 8024 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 8025 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8026 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8027 | } |
| 8028 | |
| 8029 | if (mPersistentStartingProcesses.size() > 0) { |
| 8030 | if (needSep) pw.println(" "); |
| 8031 | needSep = true; |
| 8032 | pw.println(" Persisent processes that are starting:"); |
| 8033 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8034 | "Starting Norm", "Restarting PERS"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8035 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8036 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8037 | if (mRemovedProcesses.size() > 0) { |
| 8038 | if (needSep) pw.println(" "); |
| 8039 | needSep = true; |
| 8040 | pw.println(" Processes that are being removed:"); |
| 8041 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8042 | "Removed Norm", "Removed PERS"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8043 | } |
| 8044 | |
| 8045 | if (mProcessesOnHold.size() > 0) { |
| 8046 | if (needSep) pw.println(" "); |
| 8047 | needSep = true; |
| 8048 | pw.println(" Processes that are on old until the system is ready:"); |
| 8049 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8050 | "OnHold Norm", "OnHold PERS"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8051 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8052 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8053 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8054 | |
| 8055 | if (mProcessCrashTimes.getMap().size() > 0) { |
| 8056 | if (needSep) pw.println(" "); |
| 8057 | needSep = true; |
| 8058 | pw.println(" Time since processes crashed:"); |
| 8059 | long now = SystemClock.uptimeMillis(); |
| 8060 | for (Map.Entry<String, SparseArray<Long>> procs |
| 8061 | : mProcessCrashTimes.getMap().entrySet()) { |
| 8062 | SparseArray<Long> uids = procs.getValue(); |
| 8063 | final int N = uids.size(); |
| 8064 | for (int i=0; i<N; i++) { |
| 8065 | pw.print(" Process "); pw.print(procs.getKey()); |
| 8066 | pw.print(" uid "); pw.print(uids.keyAt(i)); |
| 8067 | pw.print(": last crashed "); |
| 8068 | pw.print((now-uids.valueAt(i))); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 8069 | pw.println(" ms ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 8070 | } |
| 8071 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8072 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8073 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8074 | if (mBadProcesses.getMap().size() > 0) { |
| 8075 | if (needSep) pw.println(" "); |
| 8076 | needSep = true; |
| 8077 | pw.println(" Bad processes:"); |
| 8078 | for (Map.Entry<String, SparseArray<Long>> procs |
| 8079 | : mBadProcesses.getMap().entrySet()) { |
| 8080 | SparseArray<Long> uids = procs.getValue(); |
| 8081 | final int N = uids.size(); |
| 8082 | for (int i=0; i<N; i++) { |
| 8083 | pw.print(" Bad process "); pw.print(procs.getKey()); |
| 8084 | pw.print(" uid "); pw.print(uids.keyAt(i)); |
| 8085 | pw.print(": crashed at time "); |
| 8086 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8087 | } |
| 8088 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8089 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8090 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8091 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8092 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8093 | if (mHeavyWeightProcess != null) { |
| 8094 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 8095 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8096 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8097 | if (dumpAll) { |
| 8098 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | 3d0724d | 2011-05-12 15:39:41 -0700 | [diff] [blame] | 8099 | if (mCompatModePackages.getPackages().size() > 0) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 8100 | pw.println(" mScreenCompatPackages:"); |
| 8101 | for (Map.Entry<String, Integer> entry |
| 8102 | : mCompatModePackages.getPackages().entrySet()) { |
| 8103 | String pkg = entry.getKey(); |
| 8104 | int mode = entry.getValue(); |
| 8105 | pw.print(" "); pw.print(pkg); pw.print(": "); |
| 8106 | pw.print(mode); pw.println(); |
| 8107 | } |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 8108 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8109 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8110 | pw.println(" mSleeping=" + mSleeping + " mShuttingDown=" + mShuttingDown); |
| 8111 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 8112 | || mOrigWaitForDebugger) { |
| 8113 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 8114 | + " mDebugTransient=" + mDebugTransient |
| 8115 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 8116 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 8117 | if (mProfileApp != null || mProfileProc != null || mProfileFile != null |
| 8118 | || mProfileFd != null) { |
| 8119 | pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc); |
| 8120 | pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd); |
| 8121 | pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler=" |
| 8122 | + mAutoStopProfiler); |
| 8123 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8124 | if (mAlwaysFinishActivities || mController != null) { |
| 8125 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 8126 | + " mController=" + mController); |
| 8127 | } |
| 8128 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8129 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8130 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 8131 | + " mProcessesReady=" + mProcessesReady |
| 8132 | + " mSystemReady=" + mSystemReady); |
| 8133 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8134 | + " mBooted=" + mBooted |
| 8135 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8136 | pw.print(" mLastPowerCheckRealtime="); |
| 8137 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 8138 | pw.println(""); |
| 8139 | pw.print(" mLastPowerCheckUptime="); |
| 8140 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 8141 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8142 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 8143 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 8144 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8145 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8146 | |
| 8147 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8148 | } |
| 8149 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8150 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8151 | int opti, boolean needSep, boolean dumpAll) { |
| 8152 | if (mProcessesToGc.size() > 0) { |
| 8153 | if (needSep) pw.println(" "); |
| 8154 | needSep = true; |
| 8155 | pw.println(" Processes that are waiting to GC:"); |
| 8156 | long now = SystemClock.uptimeMillis(); |
| 8157 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 8158 | ProcessRecord proc = mProcessesToGc.get(i); |
| 8159 | pw.print(" Process "); pw.println(proc); |
| 8160 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 8161 | pw.print(", last gced="); |
| 8162 | pw.print(now-proc.lastRequestedGc); |
| 8163 | pw.print(" ms ago, last lowMem="); |
| 8164 | pw.print(now-proc.lastLowMemory); |
| 8165 | pw.println(" ms ago"); |
| 8166 | |
| 8167 | } |
| 8168 | } |
| 8169 | return needSep; |
| 8170 | } |
| 8171 | |
| 8172 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8173 | int opti, boolean dumpAll) { |
| 8174 | boolean needSep = false; |
| 8175 | |
| 8176 | if (mLruProcesses.size() > 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8177 | if (needSep) pw.println(" "); |
| 8178 | needSep = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 8179 | pw.println(" OOM levels:"); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8180 | pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ); |
| 8181 | pw.print(" CORE_SERVER_ADJ: "); pw.println(ProcessList.CORE_SERVER_ADJ); |
| 8182 | pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ); |
| 8183 | pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ); |
| 8184 | pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ); |
| 8185 | pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 8186 | pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ); |
| 8187 | pw.print(" SECONDARY_SERVER_ADJ: "); pw.println(ProcessList.SECONDARY_SERVER_ADJ); |
| 8188 | pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ); |
| 8189 | pw.print(" HIDDEN_APP_MIN_ADJ: "); pw.println(ProcessList.HIDDEN_APP_MIN_ADJ); |
| 8190 | pw.print(" EMPTY_APP_ADJ: "); pw.println(ProcessList.EMPTY_APP_ADJ); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 8191 | |
| 8192 | if (needSep) pw.println(" "); |
| 8193 | needSep = true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8194 | pw.println(" Process OOM control:"); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8195 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8196 | "Proc", "PERS", true); |
| 8197 | needSep = true; |
| 8198 | } |
| 8199 | |
| 8200 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll); |
| 8201 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8202 | pw.println(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8203 | pw.println(" mHomeProcess: " + mHomeProcess); |
| 8204 | if (mHeavyWeightProcess != null) { |
| 8205 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 8206 | } |
| 8207 | |
| 8208 | return true; |
| 8209 | } |
| 8210 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8211 | /** |
| 8212 | * There are three ways to call this: |
| 8213 | * - no service specified: dump all the services |
| 8214 | * - a flattened component name that matched an existing service was specified as the |
| 8215 | * first arg: dump that one service |
| 8216 | * - the first arg isn't the flattened component name of an existing service: |
| 8217 | * dump all services whose component contains the first arg as a substring |
| 8218 | */ |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8219 | protected boolean dumpService(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 8220 | int opti, boolean dumpAll) { |
| 8221 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8222 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8223 | if ("all".equals(name)) { |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 8224 | synchronized (this) { |
| 8225 | for (ServiceRecord r1 : mServices.values()) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8226 | services.add(r1); |
| 8227 | } |
| 8228 | } |
| 8229 | } else { |
| 8230 | ComponentName componentName = name != null |
| 8231 | ? ComponentName.unflattenFromString(name) : null; |
| 8232 | int objectId = 0; |
| 8233 | if (componentName == null) { |
| 8234 | // Not a '/' separated full component name; maybe an object ID? |
| 8235 | try { |
| 8236 | objectId = Integer.parseInt(name, 16); |
| 8237 | name = null; |
| 8238 | componentName = null; |
| 8239 | } catch (RuntimeException e) { |
| 8240 | } |
| 8241 | } |
| 8242 | |
| 8243 | synchronized (this) { |
| 8244 | for (ServiceRecord r1 : mServices.values()) { |
| 8245 | if (componentName != null) { |
| 8246 | if (r1.name.equals(componentName)) { |
| 8247 | services.add(r1); |
| 8248 | } |
| 8249 | } else if (name != null) { |
| 8250 | if (r1.name.flattenToString().contains(name)) { |
| 8251 | services.add(r1); |
| 8252 | } |
| 8253 | } else if (System.identityHashCode(r1) == objectId) { |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 8254 | services.add(r1); |
| 8255 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8256 | } |
| 8257 | } |
| 8258 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8259 | |
| 8260 | if (services.size() <= 0) { |
| 8261 | return false; |
| 8262 | } |
| 8263 | |
| 8264 | boolean needSep = false; |
| 8265 | for (int i=0; i<services.size(); i++) { |
| 8266 | if (needSep) { |
| 8267 | pw.println(); |
| 8268 | } |
| 8269 | needSep = true; |
| 8270 | dumpService("", fd, pw, services.get(i), args, dumpAll); |
| 8271 | } |
| 8272 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8273 | } |
| 8274 | |
| 8275 | /** |
| 8276 | * Invokes IApplicationThread.dumpService() on the thread of the specified service if |
| 8277 | * there is a thread associated with the service. |
| 8278 | */ |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8279 | private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw, |
| 8280 | final ServiceRecord r, String[] args, boolean dumpAll) { |
| 8281 | String innerPrefix = prefix + " "; |
| 8282 | synchronized (this) { |
| 8283 | pw.print(prefix); pw.print("SERVICE "); |
| 8284 | pw.print(r.shortName); pw.print(" "); |
| 8285 | pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 8286 | pw.print(" pid="); |
| 8287 | if (r.app != null) pw.println(r.app.pid); |
| 8288 | else pw.println("(not running)"); |
| 8289 | if (dumpAll) { |
| 8290 | r.dump(pw, innerPrefix); |
| 8291 | } |
| 8292 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8293 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8294 | pw.print(prefix); pw.println(" Client:"); |
| 8295 | pw.flush(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8296 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8297 | TransferPipe tp = new TransferPipe(); |
| 8298 | try { |
| 8299 | r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), r, args); |
| 8300 | tp.setBufferPrefix(prefix + " "); |
| 8301 | tp.go(fd); |
| 8302 | } finally { |
| 8303 | tp.kill(); |
| 8304 | } |
| 8305 | } catch (IOException e) { |
| 8306 | pw.println(prefix + " Failure while dumping the service: " + e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8307 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8308 | 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] | 8309 | } |
| 8310 | } |
| 8311 | } |
| 8312 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8313 | static class ItemMatcher { |
| 8314 | ArrayList<ComponentName> components; |
| 8315 | ArrayList<String> strings; |
| 8316 | ArrayList<Integer> objects; |
| 8317 | boolean all; |
| 8318 | |
| 8319 | ItemMatcher() { |
| 8320 | all = true; |
| 8321 | } |
| 8322 | |
| 8323 | void build(String name) { |
| 8324 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 8325 | if (componentName != null) { |
| 8326 | if (components == null) { |
| 8327 | components = new ArrayList<ComponentName>(); |
| 8328 | } |
| 8329 | components.add(componentName); |
| 8330 | all = false; |
| 8331 | } else { |
| 8332 | int objectId = 0; |
| 8333 | // Not a '/' separated full component name; maybe an object ID? |
| 8334 | try { |
| 8335 | objectId = Integer.parseInt(name, 16); |
| 8336 | if (objects == null) { |
| 8337 | objects = new ArrayList<Integer>(); |
| 8338 | } |
| 8339 | objects.add(objectId); |
| 8340 | all = false; |
| 8341 | } catch (RuntimeException e) { |
| 8342 | // Not an integer; just do string match. |
| 8343 | if (strings == null) { |
| 8344 | strings = new ArrayList<String>(); |
| 8345 | } |
| 8346 | strings.add(name); |
| 8347 | all = false; |
| 8348 | } |
| 8349 | } |
| 8350 | } |
| 8351 | |
| 8352 | int build(String[] args, int opti) { |
| 8353 | for (; opti<args.length; opti++) { |
| 8354 | String name = args[opti]; |
| 8355 | if ("--".equals(name)) { |
| 8356 | return opti+1; |
| 8357 | } |
| 8358 | build(name); |
| 8359 | } |
| 8360 | return opti; |
| 8361 | } |
| 8362 | |
| 8363 | boolean match(Object object, ComponentName comp) { |
| 8364 | if (all) { |
| 8365 | return true; |
| 8366 | } |
| 8367 | if (components != null) { |
| 8368 | for (int i=0; i<components.size(); i++) { |
| 8369 | if (components.get(i).equals(comp)) { |
| 8370 | return true; |
| 8371 | } |
| 8372 | } |
| 8373 | } |
| 8374 | if (objects != null) { |
| 8375 | for (int i=0; i<objects.size(); i++) { |
| 8376 | if (System.identityHashCode(object) == objects.get(i)) { |
| 8377 | return true; |
| 8378 | } |
| 8379 | } |
| 8380 | } |
| 8381 | if (strings != null) { |
| 8382 | String flat = comp.flattenToString(); |
| 8383 | for (int i=0; i<strings.size(); i++) { |
| 8384 | if (flat.contains(strings.get(i))) { |
| 8385 | return true; |
| 8386 | } |
| 8387 | } |
| 8388 | } |
| 8389 | return false; |
| 8390 | } |
| 8391 | } |
| 8392 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8393 | /** |
| 8394 | * There are three things that cmd can be: |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8395 | * - a flattened component name that matches an existing activity |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8396 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 8397 | * dump all activity whose component contains the cmd as a substring |
| 8398 | * - A hex number of the ActivityRecord object instance. |
| 8399 | */ |
| 8400 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 8401 | int opti, boolean dumpAll) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8402 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8403 | |
| 8404 | if ("all".equals(name)) { |
| 8405 | synchronized (this) { |
| 8406 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8407 | activities.add(r1); |
| 8408 | } |
| 8409 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 8410 | } else if ("top".equals(name)) { |
| 8411 | synchronized (this) { |
| 8412 | final int N = mMainStack.mHistory.size(); |
| 8413 | if (N > 0) { |
| 8414 | activities.add((ActivityRecord)mMainStack.mHistory.get(N-1)); |
| 8415 | } |
| 8416 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8417 | } else { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8418 | ItemMatcher matcher = new ItemMatcher(); |
| 8419 | matcher.build(name); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8420 | |
| 8421 | synchronized (this) { |
| 8422 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8423 | if (matcher.match(r1, r1.intent.getComponent())) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8424 | activities.add(r1); |
| 8425 | } |
| 8426 | } |
| 8427 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8428 | } |
| 8429 | |
| 8430 | if (activities.size() <= 0) { |
| 8431 | return false; |
| 8432 | } |
| 8433 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8434 | String[] newArgs = new String[args.length - opti]; |
| 8435 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 8436 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8437 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8438 | boolean needSep = false; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8439 | for (int i=activities.size()-1; i>=0; i--) { |
| 8440 | ActivityRecord r = (ActivityRecord)activities.get(i); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8441 | if (needSep) { |
| 8442 | pw.println(); |
| 8443 | } |
| 8444 | needSep = true; |
| 8445 | synchronized (this) { |
| 8446 | if (lastTask != r.task) { |
| 8447 | lastTask = r.task; |
| 8448 | pw.print("TASK "); pw.print(lastTask.affinity); |
| 8449 | pw.print(" id="); pw.println(lastTask.taskId); |
| 8450 | if (dumpAll) { |
| 8451 | lastTask.dump(pw, " "); |
| 8452 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8453 | } |
| 8454 | } |
| 8455 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8456 | } |
| 8457 | return true; |
| 8458 | } |
| 8459 | |
| 8460 | /** |
| 8461 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 8462 | * there is a thread associated with the activity. |
| 8463 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8464 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8465 | final ActivityRecord r, String[] args, boolean dumpAll) { |
| 8466 | String innerPrefix = prefix + " "; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8467 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8468 | pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName); |
| 8469 | pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 8470 | pw.print(" pid="); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8471 | if (r.app != null) pw.println(r.app.pid); |
| 8472 | else pw.println("(not running)"); |
| 8473 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8474 | r.dump(pw, innerPrefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8475 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8476 | } |
| 8477 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8478 | // flush anything that is already in the PrintWriter since the thread is going |
| 8479 | // to write to the file descriptor directly |
| 8480 | pw.flush(); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8481 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8482 | TransferPipe tp = new TransferPipe(); |
| 8483 | try { |
| 8484 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), r, |
| 8485 | innerPrefix, args); |
| 8486 | tp.go(fd); |
| 8487 | } finally { |
| 8488 | tp.kill(); |
| 8489 | } |
| 8490 | } catch (IOException e) { |
| 8491 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8492 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8493 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8494 | } |
| 8495 | } |
| 8496 | } |
| 8497 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8498 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8499 | int opti, boolean dumpAll) { |
| 8500 | boolean needSep = false; |
| 8501 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8502 | pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8503 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8504 | if (mRegisteredReceivers.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8505 | pw.println(" Registered Receivers:"); |
| 8506 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 8507 | while (it.hasNext()) { |
| 8508 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8509 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8510 | r.dump(pw, " "); |
| 8511 | } |
| 8512 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8513 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8514 | pw.println(); |
| 8515 | pw.println(" Receiver Resolver Table:"); |
| 8516 | mReceiverResolver.dump(pw, null, " ", null, false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8517 | needSep = true; |
| 8518 | } |
| 8519 | |
| 8520 | if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0 |
| 8521 | || mPendingBroadcast != null) { |
| 8522 | if (mParallelBroadcasts.size() > 0) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8523 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8524 | pw.println(" Active broadcasts:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8525 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8526 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 8527 | pw.println(" Broadcast #" + i + ":"); |
| 8528 | mParallelBroadcasts.get(i).dump(pw, " "); |
| 8529 | } |
| 8530 | if (mOrderedBroadcasts.size() > 0) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8531 | pw.println(); |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 8532 | pw.println(" Active ordered broadcasts:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8533 | } |
| 8534 | for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) { |
| 8535 | pw.println(" Serialized Broadcast #" + i + ":"); |
| 8536 | mOrderedBroadcasts.get(i).dump(pw, " "); |
| 8537 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8538 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8539 | pw.println(" Pending broadcast:"); |
| 8540 | if (mPendingBroadcast != null) { |
| 8541 | mPendingBroadcast.dump(pw, " "); |
| 8542 | } else { |
| 8543 | pw.println(" (null)"); |
| 8544 | } |
| 8545 | needSep = true; |
| 8546 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8547 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8548 | if (needSep) { |
| 8549 | pw.println(); |
| 8550 | } |
| 8551 | pw.println(" Historical broadcasts:"); |
| 8552 | for (int i=0; i<MAX_BROADCAST_HISTORY; i++) { |
| 8553 | BroadcastRecord r = mBroadcastHistory[i]; |
| 8554 | if (r == null) { |
| 8555 | break; |
| 8556 | } |
| 8557 | if (dumpAll) { |
| 8558 | pw.print(" Historical Broadcast #"); pw.print(i); pw.println(":"); |
| 8559 | r.dump(pw, " "); |
| 8560 | } else { |
| 8561 | if (i >= 50) { |
| 8562 | pw.println(" ..."); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 8563 | break; |
| 8564 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8565 | pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 8566 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8567 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8568 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8569 | |
| 8570 | if (mStickyBroadcasts != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8571 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8572 | pw.println(" Sticky broadcasts:"); |
| 8573 | StringBuilder sb = new StringBuilder(128); |
| 8574 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 8575 | : mStickyBroadcasts.entrySet()) { |
| 8576 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8577 | if (dumpAll) { |
| 8578 | pw.println(":"); |
| 8579 | ArrayList<Intent> intents = ent.getValue(); |
| 8580 | final int N = intents.size(); |
| 8581 | for (int i=0; i<N; i++) { |
| 8582 | sb.setLength(0); |
| 8583 | sb.append(" Intent: "); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 8584 | intents.get(i).toShortString(sb, false, true, false); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8585 | pw.println(sb.toString()); |
| 8586 | Bundle bundle = intents.get(i).getExtras(); |
| 8587 | if (bundle != null) { |
| 8588 | pw.print(" "); |
| 8589 | pw.println(bundle.toString()); |
| 8590 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8591 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8592 | } else { |
| 8593 | pw.println(""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8594 | } |
| 8595 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8596 | needSep = true; |
| 8597 | } |
| 8598 | |
| 8599 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8600 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8601 | pw.println(" mBroadcastsScheduled=" + mBroadcastsScheduled); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8602 | pw.println(" mHandler:"); |
| 8603 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8604 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8605 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8606 | |
| 8607 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8608 | } |
| 8609 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8610 | boolean dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8611 | int opti, boolean dumpAll, boolean dumpClient) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8612 | boolean needSep = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8613 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8614 | ItemMatcher matcher = new ItemMatcher(); |
| 8615 | matcher.build(args, opti); |
| 8616 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8617 | pw.println("ACTIVITY MANAGER SERVICES (dumpsys activity services)"); |
| 8618 | if (mServices.size() > 0) { |
| 8619 | pw.println(" Active services:"); |
| 8620 | long nowReal = SystemClock.elapsedRealtime(); |
| 8621 | Iterator<ServiceRecord> it = mServices.values().iterator(); |
| 8622 | needSep = false; |
| 8623 | while (it.hasNext()) { |
| 8624 | ServiceRecord r = it.next(); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8625 | if (!matcher.match(r, r.name)) { |
| 8626 | continue; |
| 8627 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8628 | if (needSep) { |
| 8629 | pw.println(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8630 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8631 | pw.print(" * "); pw.println(r); |
| 8632 | if (dumpAll) { |
| 8633 | r.dump(pw, " "); |
| 8634 | needSep = true; |
| 8635 | } else { |
| 8636 | pw.print(" app="); pw.println(r.app); |
| 8637 | pw.print(" created="); |
| 8638 | TimeUtils.formatDuration(r.createTime, nowReal, pw); |
| 8639 | pw.print(" started="); pw.print(r.startRequested); |
| 8640 | pw.print(" connections="); pw.println(r.connections.size()); |
| 8641 | } |
| 8642 | if (dumpClient && r.app != null && r.app.thread != null) { |
| 8643 | pw.println(" Client:"); |
| 8644 | pw.flush(); |
| 8645 | try { |
| 8646 | TransferPipe tp = new TransferPipe(); |
| 8647 | try { |
| 8648 | r.app.thread.dumpService( |
| 8649 | tp.getWriteFd().getFileDescriptor(), r, args); |
| 8650 | tp.setBufferPrefix(" "); |
| 8651 | // Short timeout, since blocking here can |
| 8652 | // deadlock with the application. |
| 8653 | tp.go(fd, 2000); |
| 8654 | } finally { |
| 8655 | tp.kill(); |
| 8656 | } |
| 8657 | } catch (IOException e) { |
| 8658 | pw.println(" Failure while dumping the service: " + e); |
| 8659 | } catch (RemoteException e) { |
| 8660 | pw.println(" Got a RemoteException while dumping the service"); |
| 8661 | } |
| 8662 | needSep = true; |
| 8663 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8664 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8665 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8666 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8667 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8668 | if (mPendingServices.size() > 0) { |
| 8669 | if (needSep) pw.println(" "); |
| 8670 | pw.println(" Pending services:"); |
| 8671 | for (int i=0; i<mPendingServices.size(); i++) { |
| 8672 | ServiceRecord r = mPendingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8673 | if (!matcher.match(r, r.name)) { |
| 8674 | continue; |
| 8675 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8676 | pw.print(" * Pending "); pw.println(r); |
| 8677 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8678 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8679 | needSep = true; |
| 8680 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8681 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8682 | if (mRestartingServices.size() > 0) { |
| 8683 | if (needSep) pw.println(" "); |
| 8684 | pw.println(" Restarting services:"); |
| 8685 | for (int i=0; i<mRestartingServices.size(); i++) { |
| 8686 | ServiceRecord r = mRestartingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8687 | if (!matcher.match(r, r.name)) { |
| 8688 | continue; |
| 8689 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8690 | pw.print(" * Restarting "); pw.println(r); |
| 8691 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8692 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8693 | needSep = true; |
| 8694 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8695 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8696 | if (mStoppingServices.size() > 0) { |
| 8697 | if (needSep) pw.println(" "); |
| 8698 | pw.println(" Stopping services:"); |
| 8699 | for (int i=0; i<mStoppingServices.size(); i++) { |
| 8700 | ServiceRecord r = mStoppingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8701 | if (!matcher.match(r, r.name)) { |
| 8702 | continue; |
| 8703 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8704 | pw.print(" * Stopping "); pw.println(r); |
| 8705 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8706 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8707 | needSep = true; |
| 8708 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8709 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8710 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8711 | if (mServiceConnections.size() > 0) { |
| 8712 | if (needSep) pw.println(" "); |
| 8713 | pw.println(" Connection bindings to services:"); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8714 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8715 | = mServiceConnections.values().iterator(); |
| 8716 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8717 | ArrayList<ConnectionRecord> r = it.next(); |
| 8718 | for (int i=0; i<r.size(); i++) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8719 | ConnectionRecord cr = r.get(i); |
| 8720 | if (!matcher.match(cr.binding.service, cr.binding.service.name)) { |
| 8721 | continue; |
| 8722 | } |
| 8723 | pw.print(" * "); pw.println(cr); |
| 8724 | cr.dump(pw, " "); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8725 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8726 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8727 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8728 | } |
| 8729 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8730 | |
| 8731 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8732 | } |
| 8733 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8734 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8735 | int opti, boolean dumpAll) { |
| 8736 | boolean needSep = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8737 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8738 | ItemMatcher matcher = new ItemMatcher(); |
| 8739 | matcher.build(args, opti); |
| 8740 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8741 | pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)"); |
| 8742 | if (mProvidersByClass.size() > 0) { |
| 8743 | if (needSep) pw.println(" "); |
| 8744 | pw.println(" Published content providers (by class):"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8745 | Iterator<Map.Entry<ComponentName, ContentProviderRecord>> it |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8746 | = mProvidersByClass.entrySet().iterator(); |
| 8747 | while (it.hasNext()) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8748 | Map.Entry<ComponentName, ContentProviderRecord> e = it.next(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8749 | ContentProviderRecord r = e.getValue(); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8750 | ComponentName comp = e.getKey(); |
| 8751 | String cls = comp.getClassName(); |
| 8752 | int end = cls.lastIndexOf('.'); |
| 8753 | if (end > 0 && end < (cls.length()-2)) { |
| 8754 | cls = cls.substring(end+1); |
| 8755 | } |
| 8756 | if (!matcher.match(r, comp)) { |
| 8757 | continue; |
| 8758 | } |
| 8759 | pw.print(" * "); pw.print(cls); pw.print(" ("); |
| 8760 | pw.print(comp.flattenToShortString()); pw.print(")"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8761 | if (dumpAll) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8762 | pw.println(); |
| 8763 | r.dump(pw, " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8764 | } else { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8765 | pw.print(" * "); pw.print(e.getKey().flattenToShortString()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 8766 | if (r.proc != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8767 | pw.println(":"); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 8768 | pw.print(" "); pw.println(r.proc); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8769 | } else { |
| 8770 | pw.println(); |
| 8771 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8772 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8773 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8774 | needSep = true; |
| 8775 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8776 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8777 | if (dumpAll) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8778 | if (mProvidersByName.size() > 0) { |
| 8779 | pw.println(" "); |
| 8780 | pw.println(" Authority to provider mappings:"); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8781 | Iterator<Map.Entry<String, ContentProviderRecord>> it |
| 8782 | = mProvidersByName.entrySet().iterator(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8783 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8784 | Map.Entry<String, ContentProviderRecord> e = it.next(); |
| 8785 | ContentProviderRecord r = e.getValue(); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8786 | if (!matcher.match(r, r.name)) { |
| 8787 | continue; |
| 8788 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8789 | pw.print(" "); pw.print(e.getKey()); pw.print(": "); |
| 8790 | pw.println(r); |
| 8791 | } |
| 8792 | needSep = true; |
| 8793 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8794 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8795 | |
| 8796 | if (mLaunchingProviders.size() > 0) { |
| 8797 | if (needSep) pw.println(" "); |
| 8798 | pw.println(" Launching content providers:"); |
| 8799 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
| 8800 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
| 8801 | pw.println(mLaunchingProviders.get(i)); |
| 8802 | } |
| 8803 | needSep = true; |
| 8804 | } |
| 8805 | |
| 8806 | if (mGrantedUriPermissions.size() > 0) { |
| 8807 | pw.println(); |
| 8808 | pw.println("Granted Uri Permissions:"); |
| 8809 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 8810 | int uid = mGrantedUriPermissions.keyAt(i); |
| 8811 | HashMap<Uri, UriPermission> perms |
| 8812 | = mGrantedUriPermissions.valueAt(i); |
| 8813 | pw.print(" * UID "); pw.print(uid); |
| 8814 | pw.println(" holds:"); |
| 8815 | for (UriPermission perm : perms.values()) { |
| 8816 | pw.print(" "); pw.println(perm); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8817 | if (dumpAll) { |
| 8818 | perm.dump(pw, " "); |
| 8819 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8820 | } |
| 8821 | } |
| 8822 | needSep = true; |
| 8823 | } |
| 8824 | |
| 8825 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8826 | } |
| 8827 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8828 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8829 | int opti, boolean dumpAll) { |
| 8830 | boolean needSep = false; |
| 8831 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8832 | if (this.mIntentSenderRecords.size() > 0) { |
| 8833 | pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)"); |
| 8834 | Iterator<WeakReference<PendingIntentRecord>> it |
| 8835 | = mIntentSenderRecords.values().iterator(); |
| 8836 | while (it.hasNext()) { |
| 8837 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 8838 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
| 8839 | needSep = true; |
| 8840 | if (rec != null) { |
| 8841 | pw.print(" * "); pw.println(rec); |
| 8842 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8843 | rec.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8844 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8845 | } else { |
| 8846 | pw.print(" * "); pw.println(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8847 | } |
| 8848 | } |
| 8849 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8850 | |
| 8851 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8852 | } |
| 8853 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8854 | private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list, |
| 8855 | 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] | 8856 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8857 | boolean needNL = false; |
| 8858 | final String innerPrefix = prefix + " "; |
| 8859 | final String[] args = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8860 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8861 | final ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 8862 | final boolean full = !brief && (complete || !r.isInHistory()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8863 | if (needNL) { |
| 8864 | pw.println(" "); |
| 8865 | needNL = false; |
| 8866 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8867 | if (lastTask != r.task) { |
| 8868 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8869 | pw.print(prefix); |
| 8870 | pw.print(full ? "* " : " "); |
| 8871 | pw.println(lastTask); |
| 8872 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 8873 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8874 | } else if (complete) { |
| 8875 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 8876 | if (lastTask.intent != null) { |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 8877 | pw.print(prefix); pw.print(" "); |
| 8878 | pw.println(lastTask.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8879 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 8880 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8881 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8882 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 8883 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 8884 | pw.println(r); |
| 8885 | if (full) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8886 | r.dump(pw, innerPrefix); |
| 8887 | } else if (complete) { |
| 8888 | // Complete + brief == give a summary. Isn't that obvious?!? |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 8889 | pw.print(innerPrefix); pw.println(r.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8890 | if (r.app != null) { |
| 8891 | pw.print(innerPrefix); pw.println(r.app); |
| 8892 | } |
| 8893 | } |
| 8894 | if (client && r.app != null && r.app.thread != null) { |
| 8895 | // flush anything that is already in the PrintWriter since the thread is going |
| 8896 | // to write to the file descriptor directly |
| 8897 | pw.flush(); |
| 8898 | try { |
| 8899 | TransferPipe tp = new TransferPipe(); |
| 8900 | try { |
| 8901 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), r, |
| 8902 | innerPrefix, args); |
| 8903 | // Short timeout, since blocking here can |
| 8904 | // deadlock with the application. |
| 8905 | tp.go(fd, 2000); |
| 8906 | } finally { |
| 8907 | tp.kill(); |
| 8908 | } |
| 8909 | } catch (IOException e) { |
| 8910 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
| 8911 | } catch (RemoteException e) { |
| 8912 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
| 8913 | } |
| 8914 | needNL = true; |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 8915 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8916 | } |
| 8917 | } |
| 8918 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 8919 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 8920 | if (val == base) { |
| 8921 | if (space == null) return prefix; |
| 8922 | return prefix + " "; |
| 8923 | } |
| 8924 | return prefix + "+" + Integer.toString(val-base); |
| 8925 | } |
| 8926 | |
| 8927 | private static final int dumpProcessList(PrintWriter pw, |
| 8928 | ActivityManagerService service, List list, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8929 | String prefix, String normalLabel, String persistentLabel) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8930 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 8931 | final int N = list.size()-1; |
| 8932 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8933 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8934 | pw.println(String.format("%s%s #%2d: %s", |
| 8935 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 8936 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8937 | if (r.persistent) { |
| 8938 | numPers++; |
| 8939 | } |
| 8940 | } |
| 8941 | return numPers; |
| 8942 | } |
| 8943 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8944 | private static final void dumpProcessOomList(PrintWriter pw, |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8945 | ActivityManagerService service, List<ProcessRecord> origList, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8946 | String prefix, String normalLabel, String persistentLabel, |
| 8947 | boolean inclDetails) { |
| 8948 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8949 | ArrayList<Pair<ProcessRecord, Integer>> list |
| 8950 | = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size()); |
| 8951 | for (int i=0; i<origList.size(); i++) { |
| 8952 | list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i)); |
| 8953 | } |
| 8954 | |
| 8955 | Comparator<Pair<ProcessRecord, Integer>> comparator |
| 8956 | = new Comparator<Pair<ProcessRecord, Integer>>() { |
| 8957 | @Override |
| 8958 | public int compare(Pair<ProcessRecord, Integer> object1, |
| 8959 | Pair<ProcessRecord, Integer> object2) { |
| 8960 | if (object1.first.setAdj != object2.first.setAdj) { |
| 8961 | return object1.first.setAdj > object2.first.setAdj ? -1 : 1; |
| 8962 | } |
| 8963 | if (object1.second.intValue() != object2.second.intValue()) { |
| 8964 | return object1.second.intValue() > object2.second.intValue() ? -1 : 1; |
| 8965 | } |
| 8966 | return 0; |
| 8967 | } |
| 8968 | }; |
| 8969 | |
| 8970 | Collections.sort(list, comparator); |
| 8971 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8972 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 8973 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 8974 | final long curUptime = SystemClock.uptimeMillis(); |
| 8975 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 8976 | |
| 8977 | final int N = list.size()-1; |
| 8978 | for (int i=N; i>=0; i--) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8979 | ProcessRecord r = list.get(i).first; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8980 | String oomAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8981 | if (r.setAdj >= ProcessList.EMPTY_APP_ADJ) { |
| 8982 | oomAdj = buildOomTag("empty", null, r.setAdj, ProcessList.EMPTY_APP_ADJ); |
| 8983 | } else if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 8984 | oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ); |
| 8985 | } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) { |
| 8986 | oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ); |
| 8987 | } else if (r.setAdj >= ProcessList.SECONDARY_SERVER_ADJ) { |
| 8988 | oomAdj = buildOomTag("svc", " ", r.setAdj, ProcessList.SECONDARY_SERVER_ADJ); |
| 8989 | } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) { |
| 8990 | oomAdj = buildOomTag("bckup", null, r.setAdj, ProcessList.BACKUP_APP_ADJ); |
| 8991 | } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 8992 | oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 8993 | } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 8994 | oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ); |
| 8995 | } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 8996 | oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ); |
| 8997 | } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 8998 | oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ); |
| 8999 | } else if (r.setAdj >= ProcessList.CORE_SERVER_ADJ) { |
| 9000 | oomAdj = buildOomTag("core ", null, r.setAdj, ProcessList.CORE_SERVER_ADJ); |
| 9001 | } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) { |
| 9002 | oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9003 | } else { |
| 9004 | oomAdj = Integer.toString(r.setAdj); |
| 9005 | } |
| 9006 | String schedGroup; |
| 9007 | switch (r.setSchedGroup) { |
| 9008 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 9009 | schedGroup = "B"; |
| 9010 | break; |
| 9011 | case Process.THREAD_GROUP_DEFAULT: |
| 9012 | schedGroup = "F"; |
| 9013 | break; |
| 9014 | default: |
| 9015 | schedGroup = Integer.toString(r.setSchedGroup); |
| 9016 | break; |
| 9017 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 9018 | String foreground; |
| 9019 | if (r.foregroundActivities) { |
| 9020 | foreground = "A"; |
| 9021 | } else if (r.foregroundServices) { |
| 9022 | foreground = "S"; |
| 9023 | } else { |
| 9024 | foreground = " "; |
| 9025 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 9026 | pw.println(String.format("%s%s #%2d: adj=%s/%s%s trm=%2d %s (%s)", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9027 | prefix, (r.persistent ? persistentLabel : normalLabel), |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9028 | N-list.get(i).second, oomAdj, schedGroup, foreground, r.trimMemoryLevel, |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 9029 | r.toShortString(), r.adjType)); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9030 | if (r.adjSource != null || r.adjTarget != null) { |
| 9031 | pw.print(prefix); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9032 | pw.print(" "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9033 | if (r.adjTarget instanceof ComponentName) { |
| 9034 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 9035 | } else if (r.adjTarget != null) { |
| 9036 | pw.print(r.adjTarget.toString()); |
| 9037 | } else { |
| 9038 | pw.print("{null}"); |
| 9039 | } |
| 9040 | pw.print("<="); |
| 9041 | if (r.adjSource instanceof ProcessRecord) { |
| 9042 | pw.print("Proc{"); |
| 9043 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 9044 | pw.println("}"); |
| 9045 | } else if (r.adjSource != null) { |
| 9046 | pw.println(r.adjSource.toString()); |
| 9047 | } else { |
| 9048 | pw.println("{null}"); |
| 9049 | } |
| 9050 | } |
| 9051 | if (inclDetails) { |
| 9052 | pw.print(prefix); |
| 9053 | pw.print(" "); |
| 9054 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 9055 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
| 9056 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 9057 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 9058 | pw.print(" cur="); pw.print(r.curAdj); |
| 9059 | pw.print(" set="); pw.println(r.setAdj); |
| 9060 | pw.print(prefix); |
| 9061 | pw.print(" "); |
| 9062 | pw.print("keeping="); pw.print(r.keeping); |
| 9063 | pw.print(" hidden="); pw.print(r.hidden); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9064 | pw.print(" empty="); pw.print(r.empty); |
| 9065 | pw.print(" hasAboveClient="); pw.println(r.hasAboveClient); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9066 | |
| 9067 | if (!r.keeping) { |
| 9068 | if (r.lastWakeTime != 0) { |
| 9069 | long wtime; |
| 9070 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 9071 | synchronized (stats) { |
| 9072 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 9073 | r.pid, curRealtime); |
| 9074 | } |
| 9075 | long timeUsed = wtime - r.lastWakeTime; |
| 9076 | pw.print(prefix); |
| 9077 | pw.print(" "); |
| 9078 | pw.print("keep awake over "); |
| 9079 | TimeUtils.formatDuration(realtimeSince, pw); |
| 9080 | pw.print(" used "); |
| 9081 | TimeUtils.formatDuration(timeUsed, pw); |
| 9082 | pw.print(" ("); |
| 9083 | pw.print((timeUsed*100)/realtimeSince); |
| 9084 | pw.println("%)"); |
| 9085 | } |
| 9086 | if (r.lastCpuTime != 0) { |
| 9087 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 9088 | pw.print(prefix); |
| 9089 | pw.print(" "); |
| 9090 | pw.print("run cpu over "); |
| 9091 | TimeUtils.formatDuration(uptimeSince, pw); |
| 9092 | pw.print(" used "); |
| 9093 | TimeUtils.formatDuration(timeUsed, pw); |
| 9094 | pw.print(" ("); |
| 9095 | pw.print((timeUsed*100)/uptimeSince); |
| 9096 | pw.println("%)"); |
| 9097 | } |
| 9098 | } |
| 9099 | } |
| 9100 | } |
| 9101 | } |
| 9102 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9103 | ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9104 | ArrayList<ProcessRecord> procs; |
| 9105 | synchronized (this) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9106 | if (args != null && args.length > start |
| 9107 | && args[start].charAt(0) != '-') { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9108 | procs = new ArrayList<ProcessRecord>(); |
| 9109 | int pid = -1; |
| 9110 | try { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9111 | pid = Integer.parseInt(args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9112 | } catch (NumberFormatException e) { |
| 9113 | |
| 9114 | } |
| 9115 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 9116 | ProcessRecord proc = mLruProcesses.get(i); |
| 9117 | if (proc.pid == pid) { |
| 9118 | procs.add(proc); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9119 | } else if (proc.processName.equals(args[start])) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9120 | procs.add(proc); |
| 9121 | } |
| 9122 | } |
| 9123 | if (procs.size() <= 0) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9124 | pw.println("No process found for: " + args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9125 | return null; |
| 9126 | } |
| 9127 | } else { |
| 9128 | procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 9129 | } |
| 9130 | } |
| 9131 | return procs; |
| 9132 | } |
| 9133 | |
| 9134 | final void dumpGraphicsHardwareUsage(FileDescriptor fd, |
| 9135 | PrintWriter pw, String[] args) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9136 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9137 | if (procs == null) { |
| 9138 | return; |
| 9139 | } |
| 9140 | |
| 9141 | long uptime = SystemClock.uptimeMillis(); |
| 9142 | long realtime = SystemClock.elapsedRealtime(); |
| 9143 | pw.println("Applications Graphics Acceleration Info:"); |
| 9144 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 9145 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9146 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9147 | ProcessRecord r = procs.get(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9148 | if (r.thread != null) { |
| 9149 | pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **"); |
| 9150 | pw.flush(); |
| 9151 | try { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9152 | TransferPipe tp = new TransferPipe(); |
| 9153 | try { |
| 9154 | r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 9155 | tp.go(fd); |
| 9156 | } finally { |
| 9157 | tp.kill(); |
| 9158 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9159 | } catch (IOException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9160 | pw.println("Failure while dumping the app: " + r); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9161 | pw.flush(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9162 | } catch (RemoteException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9163 | pw.println("Got a RemoteException while dumping the app " + r); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9164 | pw.flush(); |
| 9165 | } |
| 9166 | } |
| 9167 | } |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9168 | } |
| 9169 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9170 | final static class MemItem { |
| 9171 | final String label; |
| 9172 | final long pss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9173 | ArrayList<MemItem> subitems; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9174 | |
| 9175 | public MemItem(String _label, long _pss) { |
| 9176 | label = _label; |
| 9177 | pss = _pss; |
| 9178 | } |
| 9179 | } |
| 9180 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9181 | final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items, |
| 9182 | boolean sort) { |
| 9183 | if (sort) { |
| 9184 | Collections.sort(items, new Comparator<MemItem>() { |
| 9185 | @Override |
| 9186 | public int compare(MemItem lhs, MemItem rhs) { |
| 9187 | if (lhs.pss < rhs.pss) { |
| 9188 | return 1; |
| 9189 | } else if (lhs.pss > rhs.pss) { |
| 9190 | return -1; |
| 9191 | } |
| 9192 | return 0; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9193 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9194 | }); |
| 9195 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9196 | |
| 9197 | for (int i=0; i<items.size(); i++) { |
| 9198 | MemItem mi = items.get(i); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9199 | pw.print(prefix); pw.printf("%7d Kb: ", mi.pss); pw.println(mi.label); |
| 9200 | if (mi.subitems != null) { |
| 9201 | dumpMemItems(pw, prefix + " ", mi.subitems, true); |
| 9202 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9203 | } |
| 9204 | } |
| 9205 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9206 | final void dumpApplicationMemoryUsage(FileDescriptor fd, |
| 9207 | PrintWriter pw, String prefix, String[] args) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9208 | boolean dumpAll = false; |
| 9209 | |
| 9210 | int opti = 0; |
| 9211 | while (opti < args.length) { |
| 9212 | String opt = args[opti]; |
| 9213 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 9214 | break; |
| 9215 | } |
| 9216 | opti++; |
| 9217 | if ("-a".equals(opt)) { |
| 9218 | dumpAll = true; |
| 9219 | } else if ("-h".equals(opt)) { |
| 9220 | pw.println("meminfo dump options: [-a] [process]"); |
| 9221 | pw.println(" -a: include all available information for each process."); |
| 9222 | pw.println("If [process] is specified it can be the name or "); |
| 9223 | pw.println("pid of a specific process to dump."); |
| 9224 | return; |
| 9225 | } else { |
| 9226 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 9227 | } |
| 9228 | } |
| 9229 | |
| 9230 | ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9231 | if (procs == null) { |
| 9232 | return; |
| 9233 | } |
| 9234 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 9235 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9236 | long uptime = SystemClock.uptimeMillis(); |
| 9237 | long realtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9238 | |
| 9239 | if (procs.size() == 1 || isCheckinRequest) { |
| 9240 | dumpAll = true; |
| 9241 | } |
| 9242 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9243 | if (isCheckinRequest) { |
| 9244 | // short checkin version |
| 9245 | pw.println(uptime + "," + realtime); |
| 9246 | pw.flush(); |
| 9247 | } else { |
| 9248 | pw.println("Applications Memory Usage (kB):"); |
| 9249 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 9250 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9251 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9252 | String[] innerArgs = new String[args.length-opti]; |
| 9253 | System.arraycopy(args, opti, innerArgs, 0, args.length-opti); |
| 9254 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9255 | ArrayList<MemItem> procMems = new ArrayList<MemItem>(); |
| 9256 | long nativePss=0, dalvikPss=0, otherPss=0; |
| 9257 | long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS]; |
| 9258 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9259 | final int[] oomAdj = new int[] { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9260 | ProcessList.SYSTEM_ADJ, ProcessList.CORE_SERVER_ADJ, ProcessList.FOREGROUND_APP_ADJ, |
| 9261 | ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ, |
| 9262 | ProcessList.BACKUP_APP_ADJ, ProcessList.SECONDARY_SERVER_ADJ, ProcessList.HOME_APP_ADJ, ProcessList.EMPTY_APP_ADJ |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9263 | }; |
| 9264 | final String[] oomLabel = new String[] { |
| 9265 | "System", "Persistent", "Foreground", |
| 9266 | "Visible", "Perceptible", "Heavy Weight", |
| 9267 | "Backup", "Services", "Home", "Background" |
| 9268 | }; |
| 9269 | long oomPss[] = new long[oomLabel.length]; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9270 | ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[])new ArrayList[oomLabel.length]; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9271 | |
| 9272 | long totalPss = 0; |
| 9273 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9274 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9275 | ProcessRecord r = procs.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9276 | if (r.thread != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9277 | if (!isCheckinRequest && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9278 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 9279 | pw.flush(); |
| 9280 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9281 | Debug.MemoryInfo mi = null; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9282 | if (dumpAll) { |
| 9283 | try { |
| 9284 | mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs); |
| 9285 | } catch (RemoteException e) { |
| 9286 | if (!isCheckinRequest) { |
| 9287 | pw.println("Got RemoteException!"); |
| 9288 | pw.flush(); |
| 9289 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9290 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9291 | } else { |
| 9292 | mi = new Debug.MemoryInfo(); |
| 9293 | Debug.getMemoryInfo(r.pid, mi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9294 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9295 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9296 | if (!isCheckinRequest && mi != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9297 | long myTotalPss = mi.getTotalPss(); |
| 9298 | totalPss += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9299 | MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")", |
| 9300 | myTotalPss); |
| 9301 | procMems.add(pssItem); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9302 | |
| 9303 | nativePss += mi.nativePss; |
| 9304 | dalvikPss += mi.dalvikPss; |
| 9305 | otherPss += mi.otherPss; |
| 9306 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
| 9307 | long mem = mi.getOtherPss(j); |
| 9308 | miscPss[j] += mem; |
| 9309 | otherPss -= mem; |
| 9310 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9311 | |
| 9312 | for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) { |
| 9313 | if (r.setAdj <= oomAdj[oomIndex] || oomIndex == (oomPss.length-1)) { |
| 9314 | oomPss[oomIndex] += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9315 | if (oomProcs[oomIndex] == null) { |
| 9316 | oomProcs[oomIndex] = new ArrayList<MemItem>(); |
| 9317 | } |
| 9318 | oomProcs[oomIndex].add(pssItem); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9319 | break; |
| 9320 | } |
| 9321 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9322 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9323 | } |
| 9324 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9325 | |
| 9326 | if (!isCheckinRequest && procs.size() > 1) { |
| 9327 | ArrayList<MemItem> catMems = new ArrayList<MemItem>(); |
| 9328 | |
| 9329 | catMems.add(new MemItem("Native", nativePss)); |
| 9330 | catMems.add(new MemItem("Dalvik", dalvikPss)); |
| 9331 | catMems.add(new MemItem("Unknown", otherPss)); |
| 9332 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
| 9333 | catMems.add(new MemItem(Debug.MemoryInfo.getOtherLabel(j), miscPss[j])); |
| 9334 | } |
| 9335 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9336 | ArrayList<MemItem> oomMems = new ArrayList<MemItem>(); |
| 9337 | for (int j=0; j<oomPss.length; j++) { |
| 9338 | if (oomPss[j] != 0) { |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9339 | MemItem item = new MemItem(oomLabel[j], oomPss[j]); |
| 9340 | item.subitems = oomProcs[j]; |
| 9341 | oomMems.add(item); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9342 | } |
| 9343 | } |
| 9344 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9345 | pw.println(); |
| 9346 | pw.println("Total PSS by process:"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9347 | dumpMemItems(pw, " ", procMems, true); |
| 9348 | pw.println(); |
| 9349 | pw.println("Total PSS by OOM adjustment:"); |
| 9350 | dumpMemItems(pw, " ", oomMems, false); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9351 | pw.println(); |
| 9352 | pw.println("Total PSS by category:"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9353 | dumpMemItems(pw, " ", catMems, true); |
| 9354 | pw.println(); |
| 9355 | pw.print("Total PSS: "); pw.print(totalPss); pw.println(" Kb"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9356 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9357 | } |
| 9358 | |
| 9359 | /** |
| 9360 | * Searches array of arguments for the specified string |
| 9361 | * @param args array of argument strings |
| 9362 | * @param value value to search for |
| 9363 | * @return true if the value is contained in the array |
| 9364 | */ |
| 9365 | private static boolean scanArgs(String[] args, String value) { |
| 9366 | if (args != null) { |
| 9367 | for (String arg : args) { |
| 9368 | if (value.equals(arg)) { |
| 9369 | return true; |
| 9370 | } |
| 9371 | } |
| 9372 | } |
| 9373 | return false; |
| 9374 | } |
| 9375 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9376 | private final void killServicesLocked(ProcessRecord app, |
| 9377 | boolean allowRestart) { |
| 9378 | // Report disconnected services. |
| 9379 | if (false) { |
| 9380 | // XXX we are letting the client link to the service for |
| 9381 | // death notifications. |
| 9382 | if (app.services.size() > 0) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9383 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9384 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9385 | ServiceRecord r = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9386 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9387 | Iterator<ArrayList<ConnectionRecord>> jt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9388 | = r.connections.values().iterator(); |
| 9389 | while (jt.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9390 | ArrayList<ConnectionRecord> cl = jt.next(); |
| 9391 | for (int i=0; i<cl.size(); i++) { |
| 9392 | ConnectionRecord c = cl.get(i); |
| 9393 | if (c.binding.client != app) { |
| 9394 | try { |
| 9395 | //c.conn.connected(r.className, null); |
| 9396 | } catch (Exception e) { |
| 9397 | // todo: this should be asynchronous! |
| 9398 | Slog.w(TAG, "Exception thrown disconnected servce " |
| 9399 | + r.shortName |
| 9400 | + " from app " + app.processName, e); |
| 9401 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9402 | } |
| 9403 | } |
| 9404 | } |
| 9405 | } |
| 9406 | } |
| 9407 | } |
| 9408 | } |
| 9409 | |
| 9410 | // Clean up any connections this application has to other services. |
| 9411 | if (app.connections.size() > 0) { |
| 9412 | Iterator<ConnectionRecord> it = app.connections.iterator(); |
| 9413 | while (it.hasNext()) { |
| 9414 | ConnectionRecord r = it.next(); |
| 9415 | removeConnectionLocked(r, app, null); |
| 9416 | } |
| 9417 | } |
| 9418 | app.connections.clear(); |
| 9419 | |
| 9420 | if (app.services.size() != 0) { |
| 9421 | // Any services running in the application need to be placed |
| 9422 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9423 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9424 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9425 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9426 | synchronized (sr.stats.getBatteryStats()) { |
| 9427 | sr.stats.stopLaunchedLocked(); |
| 9428 | } |
| 9429 | sr.app = null; |
| 9430 | sr.executeNesting = 0; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9431 | if (mStoppingServices.remove(sr)) { |
| 9432 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
| 9433 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9434 | |
| 9435 | boolean hasClients = sr.bindings.size() > 0; |
| 9436 | if (hasClients) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9437 | Iterator<IntentBindRecord> bindings |
| 9438 | = sr.bindings.values().iterator(); |
| 9439 | while (bindings.hasNext()) { |
| 9440 | IntentBindRecord b = bindings.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9441 | if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9442 | + ": shouldUnbind=" + b.hasBound); |
| 9443 | b.binder = null; |
| 9444 | b.requested = b.received = b.hasBound = false; |
| 9445 | } |
| 9446 | } |
| 9447 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 9448 | if (sr.crashCount >= 2 && (sr.serviceInfo.applicationInfo.flags |
| 9449 | &ApplicationInfo.FLAG_PERSISTENT) == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9450 | Slog.w(TAG, "Service crashed " + sr.crashCount |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9451 | + " times, stopping: " + sr); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 9452 | EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9453 | sr.crashCount, sr.shortName, app.pid); |
| 9454 | bringDownServiceLocked(sr, true); |
| 9455 | } else if (!allowRestart) { |
| 9456 | bringDownServiceLocked(sr, true); |
| 9457 | } else { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9458 | boolean canceled = scheduleServiceRestartLocked(sr, true); |
| 9459 | |
| 9460 | // Should the service remain running? Note that in the |
| 9461 | // extreme case of so many attempts to deliver a command |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 9462 | // that it failed we also will stop it here. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9463 | if (sr.startRequested && (sr.stopIfKilled || canceled)) { |
| 9464 | if (sr.pendingStarts.size() == 0) { |
| 9465 | sr.startRequested = false; |
| 9466 | if (!hasClients) { |
| 9467 | // Whoops, no reason to restart! |
| 9468 | bringDownServiceLocked(sr, true); |
| 9469 | } |
| 9470 | } |
| 9471 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9472 | } |
| 9473 | } |
| 9474 | |
| 9475 | if (!allowRestart) { |
| 9476 | app.services.clear(); |
| 9477 | } |
| 9478 | } |
| 9479 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 9480 | // Make sure we have no more records on the stopping list. |
| 9481 | int i = mStoppingServices.size(); |
| 9482 | while (i > 0) { |
| 9483 | i--; |
| 9484 | ServiceRecord sr = mStoppingServices.get(i); |
| 9485 | if (sr.app == app) { |
| 9486 | mStoppingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9487 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 9488 | } |
| 9489 | } |
| 9490 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9491 | app.executingServices.clear(); |
| 9492 | } |
| 9493 | |
| 9494 | private final void removeDyingProviderLocked(ProcessRecord proc, |
| 9495 | ContentProviderRecord cpr) { |
| 9496 | synchronized (cpr) { |
| 9497 | cpr.launchingApp = null; |
| 9498 | cpr.notifyAll(); |
| 9499 | } |
| 9500 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 9501 | mProvidersByClass.remove(cpr.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9502 | String names[] = cpr.info.authority.split(";"); |
| 9503 | for (int j = 0; j < names.length; j++) { |
| 9504 | mProvidersByName.remove(names[j]); |
| 9505 | } |
| 9506 | |
| 9507 | Iterator<ProcessRecord> cit = cpr.clients.iterator(); |
| 9508 | while (cit.hasNext()) { |
| 9509 | ProcessRecord capp = cit.next(); |
| 9510 | if (!capp.persistent && capp.thread != null |
| 9511 | && capp.pid != 0 |
| 9512 | && capp.pid != MY_PID) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 9513 | Slog.i(TAG, "Kill " + capp.processName |
| 9514 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 9515 | + " in dying process " + (proc != null ? proc.processName : "??")); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 9516 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid, |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 9517 | capp.processName, capp.setAdj, "dying provider " |
| 9518 | + cpr.name.toShortString()); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 9519 | Process.killProcessQuiet(capp.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9520 | } |
| 9521 | } |
| 9522 | |
| 9523 | mLaunchingProviders.remove(cpr); |
| 9524 | } |
| 9525 | |
| 9526 | /** |
| 9527 | * Main code for cleaning up a process when it has gone away. This is |
| 9528 | * called both as a result of the process dying, or directly when stopping |
| 9529 | * a process when running in single process mode. |
| 9530 | */ |
| 9531 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 9532 | boolean restarting, boolean allowRestart, int index) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9533 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 9534 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9535 | } |
| 9536 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 9537 | mProcessesToGc.remove(app); |
| 9538 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9539 | // Dismiss any open dialogs. |
| 9540 | if (app.crashDialog != null) { |
| 9541 | app.crashDialog.dismiss(); |
| 9542 | app.crashDialog = null; |
| 9543 | } |
| 9544 | if (app.anrDialog != null) { |
| 9545 | app.anrDialog.dismiss(); |
| 9546 | app.anrDialog = null; |
| 9547 | } |
| 9548 | if (app.waitDialog != null) { |
| 9549 | app.waitDialog.dismiss(); |
| 9550 | app.waitDialog = null; |
| 9551 | } |
| 9552 | |
| 9553 | app.crashing = false; |
| 9554 | app.notResponding = false; |
| 9555 | |
| 9556 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 9557 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9558 | app.thread = null; |
| 9559 | app.forcingToForeground = null; |
| 9560 | app.foregroundServices = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 9561 | app.foregroundActivities = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 9562 | app.hasShownUi = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9563 | app.hasAboveClient = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9564 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 9565 | killServicesLocked(app, allowRestart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9566 | |
| 9567 | boolean restart = false; |
| 9568 | |
| 9569 | int NL = mLaunchingProviders.size(); |
| 9570 | |
| 9571 | // Remove published content providers. |
| 9572 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9573 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9574 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9575 | ContentProviderRecord cpr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9576 | cpr.provider = null; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 9577 | cpr.proc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9578 | |
| 9579 | // See if someone is waiting for this provider... in which |
| 9580 | // case we don't remove it, but just let it restart. |
| 9581 | int i = 0; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 9582 | if (!app.bad && allowRestart) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9583 | for (; i<NL; i++) { |
| 9584 | if (mLaunchingProviders.get(i) == cpr) { |
| 9585 | restart = true; |
| 9586 | break; |
| 9587 | } |
| 9588 | } |
| 9589 | } else { |
| 9590 | i = NL; |
| 9591 | } |
| 9592 | |
| 9593 | if (i >= NL) { |
| 9594 | removeDyingProviderLocked(app, cpr); |
| 9595 | NL = mLaunchingProviders.size(); |
| 9596 | } |
| 9597 | } |
| 9598 | app.pubProviders.clear(); |
| 9599 | } |
| 9600 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 9601 | // Take care of any launching providers waiting for this process. |
| 9602 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 9603 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9604 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 9605 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9606 | // Unregister from connected content providers. |
| 9607 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 9608 | Iterator it = app.conProviders.keySet().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9609 | while (it.hasNext()) { |
| 9610 | ContentProviderRecord cpr = (ContentProviderRecord)it.next(); |
| 9611 | cpr.clients.remove(app); |
| 9612 | } |
| 9613 | app.conProviders.clear(); |
| 9614 | } |
| 9615 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 9616 | // At this point there may be remaining entries in mLaunchingProviders |
| 9617 | // where we were the only one waiting, so they are no longer of use. |
| 9618 | // Look for these and clean up if found. |
| 9619 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 9620 | // the actual situation to identify what is actually going on. |
| 9621 | if (false) { |
| 9622 | for (int i=0; i<NL; i++) { |
| 9623 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 9624 | mLaunchingProviders.get(i); |
| 9625 | if (cpr.clients.size() <= 0 && cpr.externals <= 0) { |
| 9626 | synchronized (cpr) { |
| 9627 | cpr.launchingApp = null; |
| 9628 | cpr.notifyAll(); |
| 9629 | } |
| 9630 | } |
| 9631 | } |
| 9632 | } |
| 9633 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9634 | skipCurrentReceiverLocked(app); |
| 9635 | |
| 9636 | // Unregister any receivers. |
| 9637 | if (app.receivers.size() > 0) { |
| 9638 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 9639 | while (it.hasNext()) { |
| 9640 | removeReceiverLocked(it.next()); |
| 9641 | } |
| 9642 | app.receivers.clear(); |
| 9643 | } |
| 9644 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 9645 | // If the app is undergoing backup, tell the backup manager about it |
| 9646 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9647 | if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 9648 | try { |
| 9649 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 9650 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 9651 | bm.agentDisconnected(app.info.packageName); |
| 9652 | } catch (RemoteException e) { |
| 9653 | // can't happen; backup manager is local |
| 9654 | } |
| 9655 | } |
| 9656 | |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 9657 | mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 9658 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9659 | // If the caller is restarting this app, then leave it in its |
| 9660 | // current lists and let the caller take care of it. |
| 9661 | if (restarting) { |
| 9662 | return; |
| 9663 | } |
| 9664 | |
| 9665 | if (!app.persistent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9666 | if (DEBUG_PROCESSES) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9667 | "Removing non-persistent process during cleanup: " + app); |
| 9668 | mProcessNames.remove(app.processName, app.info.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9669 | if (mHeavyWeightProcess == app) { |
| 9670 | mHeavyWeightProcess = null; |
| 9671 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 9672 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9673 | } else if (!app.removed) { |
| 9674 | // This app is persistent, so we need to keep its record around. |
| 9675 | // If it is not already on the pending app list, add it there |
| 9676 | // and start a new process for it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9677 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 9678 | mPersistentStartingProcesses.add(app); |
| 9679 | restart = true; |
| 9680 | } |
| 9681 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 9682 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 9683 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9684 | mProcessesOnHold.remove(app); |
| 9685 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 9686 | if (app == mHomeProcess) { |
| 9687 | mHomeProcess = null; |
| 9688 | } |
| 9689 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9690 | if (restart) { |
| 9691 | // We have components that still need to be running in the |
| 9692 | // process, so re-launch it. |
| 9693 | mProcessNames.put(app.processName, app.info.uid, app); |
| 9694 | startProcessLocked(app, "restart", app.processName); |
| 9695 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 9696 | // Goodbye! |
| 9697 | synchronized (mPidsSelfLocked) { |
| 9698 | mPidsSelfLocked.remove(app.pid); |
| 9699 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 9700 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9701 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9702 | } |
| 9703 | } |
| 9704 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 9705 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 9706 | // Look through the content providers we are waiting to have launched, |
| 9707 | // and if any run in this process then either schedule a restart of |
| 9708 | // the process or kill the client waiting for it if this process has |
| 9709 | // gone bad. |
| 9710 | int NL = mLaunchingProviders.size(); |
| 9711 | boolean restart = false; |
| 9712 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9713 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 9714 | if (cpr.launchingApp == app) { |
| 9715 | if (!alwaysBad && !app.bad) { |
| 9716 | restart = true; |
| 9717 | } else { |
| 9718 | removeDyingProviderLocked(app, cpr); |
| 9719 | NL = mLaunchingProviders.size(); |
| 9720 | } |
| 9721 | } |
| 9722 | } |
| 9723 | return restart; |
| 9724 | } |
| 9725 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9726 | // ========================================================= |
| 9727 | // SERVICES |
| 9728 | // ========================================================= |
| 9729 | |
| 9730 | ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) { |
| 9731 | ActivityManager.RunningServiceInfo info = |
| 9732 | new ActivityManager.RunningServiceInfo(); |
| 9733 | info.service = r.name; |
| 9734 | if (r.app != null) { |
| 9735 | info.pid = r.app.pid; |
| 9736 | } |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 9737 | info.uid = r.appInfo.uid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9738 | info.process = r.processName; |
| 9739 | info.foreground = r.isForeground; |
| 9740 | info.activeSince = r.createTime; |
| 9741 | info.started = r.startRequested; |
| 9742 | info.clientCount = r.connections.size(); |
| 9743 | info.crashCount = r.crashCount; |
| 9744 | info.lastActivityTime = r.lastActivity; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 9745 | if (r.isForeground) { |
| 9746 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND; |
| 9747 | } |
| 9748 | if (r.startRequested) { |
| 9749 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED; |
| 9750 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 9751 | if (r.app != null && r.app.pid == MY_PID) { |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 9752 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS; |
| 9753 | } |
| 9754 | if (r.app != null && r.app.persistent) { |
| 9755 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS; |
| 9756 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9757 | |
| 9758 | for (ArrayList<ConnectionRecord> connl : r.connections.values()) { |
| 9759 | for (int i=0; i<connl.size(); i++) { |
| 9760 | ConnectionRecord conn = connl.get(i); |
| 9761 | if (conn.clientLabel != 0) { |
| 9762 | info.clientPackage = conn.binding.client.info.packageName; |
| 9763 | info.clientLabel = conn.clientLabel; |
| 9764 | return info; |
| 9765 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 9766 | } |
| 9767 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9768 | return info; |
| 9769 | } |
| 9770 | |
| 9771 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 9772 | int flags) { |
| 9773 | synchronized (this) { |
| 9774 | ArrayList<ActivityManager.RunningServiceInfo> res |
| 9775 | = new ArrayList<ActivityManager.RunningServiceInfo>(); |
| 9776 | |
| 9777 | if (mServices.size() > 0) { |
| 9778 | Iterator<ServiceRecord> it = mServices.values().iterator(); |
| 9779 | while (it.hasNext() && res.size() < maxNum) { |
| 9780 | res.add(makeRunningServiceInfoLocked(it.next())); |
| 9781 | } |
| 9782 | } |
| 9783 | |
| 9784 | for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { |
| 9785 | ServiceRecord r = mRestartingServices.get(i); |
| 9786 | ActivityManager.RunningServiceInfo info = |
| 9787 | makeRunningServiceInfoLocked(r); |
| 9788 | info.restarting = r.nextRestartTime; |
| 9789 | res.add(info); |
| 9790 | } |
| 9791 | |
| 9792 | return res; |
| 9793 | } |
| 9794 | } |
| 9795 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 9796 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
| 9797 | synchronized (this) { |
| 9798 | ServiceRecord r = mServices.get(name); |
| 9799 | if (r != null) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9800 | for (ArrayList<ConnectionRecord> conn : r.connections.values()) { |
| 9801 | for (int i=0; i<conn.size(); i++) { |
| 9802 | if (conn.get(i).clientIntent != null) { |
| 9803 | return conn.get(i).clientIntent; |
| 9804 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 9805 | } |
| 9806 | } |
| 9807 | } |
| 9808 | } |
| 9809 | return null; |
| 9810 | } |
| 9811 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9812 | private final ServiceRecord findServiceLocked(ComponentName name, |
| 9813 | IBinder token) { |
| 9814 | ServiceRecord r = mServices.get(name); |
| 9815 | return r == token ? r : null; |
| 9816 | } |
| 9817 | |
| 9818 | private final class ServiceLookupResult { |
| 9819 | final ServiceRecord record; |
| 9820 | final String permission; |
| 9821 | |
| 9822 | ServiceLookupResult(ServiceRecord _record, String _permission) { |
| 9823 | record = _record; |
| 9824 | permission = _permission; |
| 9825 | } |
| 9826 | }; |
| 9827 | |
| 9828 | private ServiceLookupResult findServiceLocked(Intent service, |
| 9829 | String resolvedType) { |
| 9830 | ServiceRecord r = null; |
| 9831 | if (service.getComponent() != null) { |
| 9832 | r = mServices.get(service.getComponent()); |
| 9833 | } |
| 9834 | if (r == null) { |
| 9835 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
| 9836 | r = mServicesByIntent.get(filter); |
| 9837 | } |
| 9838 | |
| 9839 | if (r == null) { |
| 9840 | try { |
| 9841 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 9842 | AppGlobals.getPackageManager().resolveService( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9843 | service, resolvedType, 0); |
| 9844 | ServiceInfo sInfo = |
| 9845 | rInfo != null ? rInfo.serviceInfo : null; |
| 9846 | if (sInfo == null) { |
| 9847 | return null; |
| 9848 | } |
| 9849 | |
| 9850 | ComponentName name = new ComponentName( |
| 9851 | sInfo.applicationInfo.packageName, sInfo.name); |
| 9852 | r = mServices.get(name); |
| 9853 | } catch (RemoteException ex) { |
| 9854 | // pm is in same process, this will never happen. |
| 9855 | } |
| 9856 | } |
| 9857 | if (r != null) { |
| 9858 | int callingPid = Binder.getCallingPid(); |
| 9859 | int callingUid = Binder.getCallingUid(); |
| 9860 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 9861 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9862 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 9863 | if (!r.exported) { |
| 9864 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 9865 | + " from pid=" + callingPid |
| 9866 | + ", uid=" + callingUid |
| 9867 | + " that is not exported from uid " + r.appInfo.uid); |
| 9868 | return new ServiceLookupResult(null, "not exported from uid " |
| 9869 | + r.appInfo.uid); |
| 9870 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9871 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9872 | + " from pid=" + callingPid |
| 9873 | + ", uid=" + callingUid |
| 9874 | + " requires " + r.permission); |
| 9875 | return new ServiceLookupResult(null, r.permission); |
| 9876 | } |
| 9877 | return new ServiceLookupResult(r, null); |
| 9878 | } |
| 9879 | return null; |
| 9880 | } |
| 9881 | |
| 9882 | private class ServiceRestarter implements Runnable { |
| 9883 | private ServiceRecord mService; |
| 9884 | |
| 9885 | void setService(ServiceRecord service) { |
| 9886 | mService = service; |
| 9887 | } |
| 9888 | |
| 9889 | public void run() { |
| 9890 | synchronized(ActivityManagerService.this) { |
| 9891 | performServiceRestartLocked(mService); |
| 9892 | } |
| 9893 | } |
| 9894 | } |
| 9895 | |
| 9896 | private ServiceLookupResult retrieveServiceLocked(Intent service, |
| 9897 | String resolvedType, int callingPid, int callingUid) { |
| 9898 | ServiceRecord r = null; |
| 9899 | if (service.getComponent() != null) { |
| 9900 | r = mServices.get(service.getComponent()); |
| 9901 | } |
| 9902 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
| 9903 | r = mServicesByIntent.get(filter); |
| 9904 | if (r == null) { |
| 9905 | try { |
| 9906 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 9907 | AppGlobals.getPackageManager().resolveService( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 9908 | service, resolvedType, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9909 | ServiceInfo sInfo = |
| 9910 | rInfo != null ? rInfo.serviceInfo : null; |
| 9911 | if (sInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9912 | Slog.w(TAG, "Unable to start service " + service + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9913 | ": not found"); |
| 9914 | return null; |
| 9915 | } |
| 9916 | |
| 9917 | ComponentName name = new ComponentName( |
| 9918 | sInfo.applicationInfo.packageName, sInfo.name); |
| 9919 | r = mServices.get(name); |
| 9920 | if (r == null) { |
| 9921 | filter = new Intent.FilterComparison(service.cloneFilter()); |
| 9922 | ServiceRestarter res = new ServiceRestarter(); |
| 9923 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 9924 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 9925 | synchronized (stats) { |
| 9926 | ss = stats.getServiceStatsLocked( |
| 9927 | sInfo.applicationInfo.uid, sInfo.packageName, |
| 9928 | sInfo.name); |
| 9929 | } |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 9930 | r = new ServiceRecord(this, ss, name, filter, sInfo, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9931 | res.setService(r); |
| 9932 | mServices.put(name, r); |
| 9933 | mServicesByIntent.put(filter, r); |
| 9934 | |
| 9935 | // Make sure this component isn't in the pending list. |
| 9936 | int N = mPendingServices.size(); |
| 9937 | for (int i=0; i<N; i++) { |
| 9938 | ServiceRecord pr = mPendingServices.get(i); |
| 9939 | if (pr.name.equals(name)) { |
| 9940 | mPendingServices.remove(i); |
| 9941 | i--; |
| 9942 | N--; |
| 9943 | } |
| 9944 | } |
| 9945 | } |
| 9946 | } catch (RemoteException ex) { |
| 9947 | // pm is in same process, this will never happen. |
| 9948 | } |
| 9949 | } |
| 9950 | if (r != null) { |
| 9951 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 9952 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9953 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 9954 | if (!r.exported) { |
| 9955 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 9956 | + " from pid=" + callingPid |
| 9957 | + ", uid=" + callingUid |
| 9958 | + " that is not exported from uid " + r.appInfo.uid); |
| 9959 | return new ServiceLookupResult(null, "not exported from uid " |
| 9960 | + r.appInfo.uid); |
| 9961 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9962 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 9963 | + " from pid=" + callingPid |
| 9964 | + ", uid=" + callingUid |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9965 | + " requires " + r.permission); |
| 9966 | return new ServiceLookupResult(null, r.permission); |
| 9967 | } |
| 9968 | return new ServiceLookupResult(r, null); |
| 9969 | } |
| 9970 | return null; |
| 9971 | } |
| 9972 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9973 | private final void bumpServiceExecutingLocked(ServiceRecord r, String why) { |
| 9974 | if (DEBUG_SERVICE) Log.v(TAG, ">>> EXECUTING " |
| 9975 | + why + " of " + r + " in app " + r.app); |
| 9976 | else if (DEBUG_SERVICE_EXECUTING) Log.v(TAG, ">>> EXECUTING " |
| 9977 | + why + " of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9978 | long now = SystemClock.uptimeMillis(); |
| 9979 | if (r.executeNesting == 0 && r.app != null) { |
| 9980 | if (r.app.executingServices.size() == 0) { |
| 9981 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 9982 | msg.obj = r.app; |
| 9983 | mHandler.sendMessageAtTime(msg, now+SERVICE_TIMEOUT); |
| 9984 | } |
| 9985 | r.app.executingServices.add(r); |
| 9986 | } |
| 9987 | r.executeNesting++; |
| 9988 | r.executingStart = now; |
| 9989 | } |
| 9990 | |
| 9991 | private final void sendServiceArgsLocked(ServiceRecord r, |
| 9992 | boolean oomAdjusted) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9993 | final int N = r.pendingStarts.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9994 | if (N == 0) { |
| 9995 | return; |
| 9996 | } |
| 9997 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9998 | while (r.pendingStarts.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9999 | try { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10000 | ServiceRecord.StartItem si = r.pendingStarts.remove(0); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10001 | if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to: " |
| 10002 | + r + " " + r.intent + " args=" + si.intent); |
Dianne Hackborn | 3a28f22 | 2011-03-01 12:25:54 -0800 | [diff] [blame] | 10003 | if (si.intent == null && N > 1) { |
| 10004 | // If somehow we got a dummy null intent in the middle, |
| 10005 | // then skip it. DO NOT skip a null intent when it is |
| 10006 | // the only one in the list -- this is to support the |
| 10007 | // onStartCommand(null) case. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10008 | continue; |
| 10009 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10010 | si.deliveredTime = SystemClock.uptimeMillis(); |
| 10011 | r.deliveredStarts.add(si); |
| 10012 | si.deliveryCount++; |
Dianne Hackborn | 3a28f22 | 2011-03-01 12:25:54 -0800 | [diff] [blame] | 10013 | if (si.targetPermissionUid >= 0 && si.intent != null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10014 | grantUriPermissionUncheckedFromIntentLocked(si.targetPermissionUid, |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 10015 | r.packageName, si.intent, si.getUriPermissionsLocked()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10016 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10017 | bumpServiceExecutingLocked(r, "start"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10018 | if (!oomAdjusted) { |
| 10019 | oomAdjusted = true; |
| 10020 | updateOomAdjLocked(r.app); |
| 10021 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10022 | int flags = 0; |
| 10023 | if (si.deliveryCount > 0) { |
| 10024 | flags |= Service.START_FLAG_RETRY; |
| 10025 | } |
| 10026 | if (si.doneExecutingCount > 0) { |
| 10027 | flags |= Service.START_FLAG_REDELIVERY; |
| 10028 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10029 | r.app.thread.scheduleServiceArgs(r, si.taskRemoved, si.id, flags, si.intent); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10030 | } catch (RemoteException e) { |
| 10031 | // Remote process gone... we'll let the normal cleanup take |
| 10032 | // care of this. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10033 | if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while scheduling start: " + r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10034 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10035 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10036 | Slog.w(TAG, "Unexpected exception", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10037 | break; |
| 10038 | } |
| 10039 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10040 | } |
| 10041 | |
| 10042 | private final boolean requestServiceBindingLocked(ServiceRecord r, |
| 10043 | IntentBindRecord i, boolean rebind) { |
| 10044 | if (r.app == null || r.app.thread == null) { |
| 10045 | // If service is not currently running, can't yet bind. |
| 10046 | return false; |
| 10047 | } |
| 10048 | if ((!i.requested || rebind) && i.apps.size() > 0) { |
| 10049 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10050 | bumpServiceExecutingLocked(r, "bind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10051 | r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind); |
| 10052 | if (!rebind) { |
| 10053 | i.requested = true; |
| 10054 | } |
| 10055 | i.hasBound = true; |
| 10056 | i.doRebind = false; |
| 10057 | } catch (RemoteException e) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10058 | 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] | 10059 | return false; |
| 10060 | } |
| 10061 | } |
| 10062 | return true; |
| 10063 | } |
| 10064 | |
| 10065 | private final void requestServiceBindingsLocked(ServiceRecord r) { |
| 10066 | Iterator<IntentBindRecord> bindings = r.bindings.values().iterator(); |
| 10067 | while (bindings.hasNext()) { |
| 10068 | IntentBindRecord i = bindings.next(); |
| 10069 | if (!requestServiceBindingLocked(r, i, false)) { |
| 10070 | break; |
| 10071 | } |
| 10072 | } |
| 10073 | } |
| 10074 | |
| 10075 | private final void realStartServiceLocked(ServiceRecord r, |
| 10076 | ProcessRecord app) throws RemoteException { |
| 10077 | if (app.thread == null) { |
| 10078 | throw new RemoteException(); |
| 10079 | } |
| 10080 | |
| 10081 | r.app = app; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 10082 | r.restartTime = r.lastActivity = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10083 | |
| 10084 | app.services.add(r); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10085 | bumpServiceExecutingLocked(r, "create"); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10086 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10087 | |
| 10088 | boolean created = false; |
| 10089 | try { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 10090 | mStringBuilder.setLength(0); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 10091 | r.intent.getIntent().toShortString(mStringBuilder, true, false, true); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10092 | EventLog.writeEvent(EventLogTags.AM_CREATE_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10093 | System.identityHashCode(r), r.shortName, |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 10094 | mStringBuilder.toString(), r.app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10095 | synchronized (r.stats.getBatteryStats()) { |
| 10096 | r.stats.startLaunchedLocked(); |
| 10097 | } |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 10098 | ensurePackageDexOpt(r.serviceInfo.packageName); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 10099 | app.thread.scheduleCreateService(r, r.serviceInfo, |
| 10100 | compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo)); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10101 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10102 | created = true; |
| 10103 | } finally { |
| 10104 | if (!created) { |
| 10105 | app.services.remove(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10106 | scheduleServiceRestartLocked(r, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10107 | } |
| 10108 | } |
| 10109 | |
| 10110 | requestServiceBindingsLocked(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10111 | |
| 10112 | // If the service is in the started state, and there are no |
| 10113 | // pending arguments, then fake up one so its onStartCommand() will |
| 10114 | // be called. |
| 10115 | if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10116 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
| 10117 | null, -1)); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10118 | } |
| 10119 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10120 | sendServiceArgsLocked(r, true); |
| 10121 | } |
| 10122 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10123 | private final boolean scheduleServiceRestartLocked(ServiceRecord r, |
| 10124 | boolean allowCancel) { |
| 10125 | boolean canceled = false; |
| 10126 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10127 | final long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10128 | long minDuration = SERVICE_RESTART_DURATION; |
Dianne Hackborn | 6ccd2af | 2009-08-27 12:26:44 -0700 | [diff] [blame] | 10129 | long resetTime = SERVICE_RESET_RUN_DURATION; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10130 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 10131 | if ((r.serviceInfo.applicationInfo.flags |
| 10132 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 10133 | minDuration /= 4; |
| 10134 | } |
| 10135 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10136 | // Any delivered but not yet finished starts should be put back |
| 10137 | // on the pending list. |
| 10138 | final int N = r.deliveredStarts.size(); |
| 10139 | if (N > 0) { |
| 10140 | for (int i=N-1; i>=0; i--) { |
| 10141 | ServiceRecord.StartItem si = r.deliveredStarts.get(i); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10142 | si.removeUriPermissionsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10143 | if (si.intent == null) { |
| 10144 | // We'll generate this again if needed. |
| 10145 | } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT |
| 10146 | && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) { |
| 10147 | r.pendingStarts.add(0, si); |
| 10148 | long dur = SystemClock.uptimeMillis() - si.deliveredTime; |
| 10149 | dur *= 2; |
| 10150 | if (minDuration < dur) minDuration = dur; |
| 10151 | if (resetTime < dur) resetTime = dur; |
| 10152 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10153 | Slog.w(TAG, "Canceling start item " + si.intent + " in service " |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10154 | + r.name); |
| 10155 | canceled = true; |
| 10156 | } |
| 10157 | } |
| 10158 | r.deliveredStarts.clear(); |
| 10159 | } |
| 10160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10161 | r.totalRestartCount++; |
| 10162 | if (r.restartDelay == 0) { |
| 10163 | r.restartCount++; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10164 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10165 | } else { |
| 10166 | // If it has been a "reasonably long time" since the service |
| 10167 | // was started, then reset our restart duration back to |
| 10168 | // the beginning, so we don't infinitely increase the duration |
| 10169 | // on a service that just occasionally gets killed (which is |
| 10170 | // a normal case, due to process being killed to reclaim memory). |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10171 | if (now > (r.restartTime+resetTime)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10172 | r.restartCount = 1; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10173 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10174 | } else { |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 10175 | if ((r.serviceInfo.applicationInfo.flags |
| 10176 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 10177 | // Services in peristent processes will restart much more |
| 10178 | // quickly, since they are pretty important. (Think SystemUI). |
| 10179 | r.restartDelay += minDuration/2; |
| 10180 | } else { |
| 10181 | r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR; |
| 10182 | if (r.restartDelay < minDuration) { |
| 10183 | r.restartDelay = minDuration; |
| 10184 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10185 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10186 | } |
| 10187 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10188 | |
| 10189 | r.nextRestartTime = now + r.restartDelay; |
| 10190 | |
| 10191 | // Make sure that we don't end up restarting a bunch of services |
| 10192 | // all at the same time. |
| 10193 | boolean repeat; |
| 10194 | do { |
| 10195 | repeat = false; |
| 10196 | for (int i=mRestartingServices.size()-1; i>=0; i--) { |
| 10197 | ServiceRecord r2 = mRestartingServices.get(i); |
| 10198 | if (r2 != r && r.nextRestartTime |
| 10199 | >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN) |
| 10200 | && r.nextRestartTime |
| 10201 | < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) { |
| 10202 | r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN; |
| 10203 | r.restartDelay = r.nextRestartTime - now; |
| 10204 | repeat = true; |
| 10205 | break; |
| 10206 | } |
| 10207 | } |
| 10208 | } while (repeat); |
| 10209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10210 | if (!mRestartingServices.contains(r)) { |
| 10211 | mRestartingServices.add(r); |
| 10212 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10213 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10214 | r.cancelNotification(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10215 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10216 | mHandler.removeCallbacks(r.restarter); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10217 | mHandler.postAtTime(r.restarter, r.nextRestartTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10218 | r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10219 | Slog.w(TAG, "Scheduling restart of crashed service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10220 | + r.shortName + " in " + r.restartDelay + "ms"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10221 | EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10222 | r.shortName, r.restartDelay); |
| 10223 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10224 | return canceled; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10225 | } |
| 10226 | |
| 10227 | final void performServiceRestartLocked(ServiceRecord r) { |
| 10228 | if (!mRestartingServices.contains(r)) { |
| 10229 | return; |
| 10230 | } |
| 10231 | bringUpServiceLocked(r, r.intent.getIntent().getFlags(), true); |
| 10232 | } |
| 10233 | |
| 10234 | private final boolean unscheduleServiceRestartLocked(ServiceRecord r) { |
| 10235 | if (r.restartDelay == 0) { |
| 10236 | return false; |
| 10237 | } |
| 10238 | r.resetRestartCounter(); |
| 10239 | mRestartingServices.remove(r); |
| 10240 | mHandler.removeCallbacks(r.restarter); |
| 10241 | return true; |
| 10242 | } |
| 10243 | |
| 10244 | private final boolean bringUpServiceLocked(ServiceRecord r, |
| 10245 | int intentFlags, boolean whileRestarting) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10246 | //Slog.i(TAG, "Bring up service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10247 | //r.dump(" "); |
| 10248 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10249 | if (r.app != null && r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10250 | sendServiceArgsLocked(r, false); |
| 10251 | return true; |
| 10252 | } |
| 10253 | |
| 10254 | if (!whileRestarting && r.restartDelay > 0) { |
| 10255 | // If waiting for a restart, then do nothing. |
| 10256 | return true; |
| 10257 | } |
| 10258 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10259 | 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] | 10260 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10261 | // We are now bringing the service up, so no longer in the |
| 10262 | // restarting state. |
| 10263 | mRestartingServices.remove(r); |
| 10264 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10265 | // Service is now being launched, its package can't be stopped. |
| 10266 | try { |
| 10267 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 10268 | r.packageName, false); |
| 10269 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 10270 | } catch (IllegalArgumentException e) { |
| 10271 | Slog.w(TAG, "Failed trying to unstop package " |
| 10272 | + r.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10273 | } |
| 10274 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10275 | final String appName = r.processName; |
| 10276 | ProcessRecord app = getProcessRecordLocked(appName, r.appInfo.uid); |
| 10277 | if (app != null && app.thread != null) { |
| 10278 | try { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 10279 | app.addPackage(r.appInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10280 | realStartServiceLocked(r, app); |
| 10281 | return true; |
| 10282 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10283 | 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] | 10284 | } |
| 10285 | |
| 10286 | // If a dead object exception was thrown -- fall through to |
| 10287 | // restart the application. |
| 10288 | } |
| 10289 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10290 | // Not running -- get it started, and enqueue this service record |
| 10291 | // to be executed when the app comes up. |
| 10292 | if (startProcessLocked(appName, r.appInfo, true, intentFlags, |
| 10293 | "service", r.name, false) == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10294 | Slog.w(TAG, "Unable to launch app " |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10295 | + r.appInfo.packageName + "/" |
| 10296 | + r.appInfo.uid + " for service " |
| 10297 | + r.intent.getIntent() + ": process is bad"); |
| 10298 | bringDownServiceLocked(r, true); |
| 10299 | return false; |
| 10300 | } |
| 10301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10302 | if (!mPendingServices.contains(r)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10303 | mPendingServices.add(r); |
| 10304 | } |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10305 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10306 | return true; |
| 10307 | } |
| 10308 | |
| 10309 | private final void bringDownServiceLocked(ServiceRecord r, boolean force) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10310 | //Slog.i(TAG, "Bring down service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10311 | //r.dump(" "); |
| 10312 | |
| 10313 | // Does it still need to run? |
| 10314 | if (!force && r.startRequested) { |
| 10315 | return; |
| 10316 | } |
| 10317 | if (r.connections.size() > 0) { |
| 10318 | if (!force) { |
| 10319 | // XXX should probably keep a count of the number of auto-create |
| 10320 | // connections directly in the service. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10321 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10322 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10323 | ArrayList<ConnectionRecord> cr = it.next(); |
| 10324 | for (int i=0; i<cr.size(); i++) { |
| 10325 | if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) { |
| 10326 | return; |
| 10327 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10328 | } |
| 10329 | } |
| 10330 | } |
| 10331 | |
| 10332 | // Report to all of the connections that the service is no longer |
| 10333 | // available. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10334 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10335 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10336 | ArrayList<ConnectionRecord> c = it.next(); |
| 10337 | for (int i=0; i<c.size(); i++) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10338 | ConnectionRecord cr = c.get(i); |
| 10339 | // There is still a connection to the service that is |
| 10340 | // being brought down. Mark it as dead. |
| 10341 | cr.serviceDead = true; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10342 | try { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10343 | cr.conn.connected(r.name, null); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10344 | } catch (Exception e) { |
| 10345 | Slog.w(TAG, "Failure disconnecting service " + r.name + |
| 10346 | " to connection " + c.get(i).conn.asBinder() + |
| 10347 | " (in " + c.get(i).binding.client.processName + ")", e); |
| 10348 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10349 | } |
| 10350 | } |
| 10351 | } |
| 10352 | |
| 10353 | // Tell the service that it has been unbound. |
| 10354 | if (r.bindings.size() > 0 && r.app != null && r.app.thread != null) { |
| 10355 | Iterator<IntentBindRecord> it = r.bindings.values().iterator(); |
| 10356 | while (it.hasNext()) { |
| 10357 | IntentBindRecord ibr = it.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10358 | 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] | 10359 | + ": hasBound=" + ibr.hasBound); |
| 10360 | if (r.app != null && r.app.thread != null && ibr.hasBound) { |
| 10361 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10362 | bumpServiceExecutingLocked(r, "bring down unbind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10363 | updateOomAdjLocked(r.app); |
| 10364 | ibr.hasBound = false; |
| 10365 | r.app.thread.scheduleUnbindService(r, |
| 10366 | ibr.intent.getIntent()); |
| 10367 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10368 | Slog.w(TAG, "Exception when unbinding service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10369 | + r.shortName, e); |
| 10370 | serviceDoneExecutingLocked(r, true); |
| 10371 | } |
| 10372 | } |
| 10373 | } |
| 10374 | } |
| 10375 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10376 | if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down " + r + " " + r.intent); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10377 | EventLog.writeEvent(EventLogTags.AM_DESTROY_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10378 | System.identityHashCode(r), r.shortName, |
| 10379 | (r.app != null) ? r.app.pid : -1); |
| 10380 | |
| 10381 | mServices.remove(r.name); |
| 10382 | mServicesByIntent.remove(r.intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10383 | r.totalRestartCount = 0; |
| 10384 | unscheduleServiceRestartLocked(r); |
| 10385 | |
| 10386 | // Also make sure it is not on the pending list. |
| 10387 | int N = mPendingServices.size(); |
| 10388 | for (int i=0; i<N; i++) { |
| 10389 | if (mPendingServices.get(i) == r) { |
| 10390 | mPendingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10391 | if (DEBUG_SERVICE) Slog.v(TAG, "Removed pending: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10392 | i--; |
| 10393 | N--; |
| 10394 | } |
| 10395 | } |
| 10396 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10397 | r.cancelNotification(); |
| 10398 | r.isForeground = false; |
| 10399 | r.foregroundId = 0; |
| 10400 | r.foregroundNoti = null; |
| 10401 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10402 | // Clear start entries. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10403 | r.clearDeliveredStartsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10404 | r.pendingStarts.clear(); |
| 10405 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10406 | if (r.app != null) { |
| 10407 | synchronized (r.stats.getBatteryStats()) { |
| 10408 | r.stats.stopLaunchedLocked(); |
| 10409 | } |
| 10410 | r.app.services.remove(r); |
| 10411 | if (r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10412 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10413 | bumpServiceExecutingLocked(r, "stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10414 | mStoppingServices.add(r); |
| 10415 | updateOomAdjLocked(r.app); |
| 10416 | r.app.thread.scheduleStopService(r); |
| 10417 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10418 | Slog.w(TAG, "Exception when stopping service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10419 | + r.shortName, e); |
| 10420 | serviceDoneExecutingLocked(r, true); |
| 10421 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10422 | updateServiceForegroundLocked(r.app, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10423 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10424 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10425 | TAG, "Removed service that has no process: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10426 | } |
| 10427 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10428 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10429 | TAG, "Removed service that is not running: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10430 | } |
Vairavan Srinivasan | a207ce2 | 2010-12-23 13:51:48 -0800 | [diff] [blame] | 10431 | |
| 10432 | if (r.bindings.size() > 0) { |
| 10433 | r.bindings.clear(); |
| 10434 | } |
| 10435 | |
| 10436 | if (r.restarter instanceof ServiceRestarter) { |
| 10437 | ((ServiceRestarter)r.restarter).setService(null); |
| 10438 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10439 | } |
| 10440 | |
| 10441 | ComponentName startServiceLocked(IApplicationThread caller, |
| 10442 | Intent service, String resolvedType, |
| 10443 | int callingPid, int callingUid) { |
| 10444 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10445 | if (DEBUG_SERVICE) Slog.v(TAG, "startService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10446 | + " type=" + resolvedType + " args=" + service.getExtras()); |
| 10447 | |
| 10448 | if (caller != null) { |
| 10449 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 10450 | if (callerApp == null) { |
| 10451 | throw new SecurityException( |
| 10452 | "Unable to find app for caller " + caller |
| 10453 | + " (pid=" + Binder.getCallingPid() |
| 10454 | + ") when starting service " + service); |
| 10455 | } |
| 10456 | } |
| 10457 | |
| 10458 | ServiceLookupResult res = |
| 10459 | retrieveServiceLocked(service, resolvedType, |
| 10460 | callingPid, callingUid); |
| 10461 | if (res == null) { |
| 10462 | return null; |
| 10463 | } |
| 10464 | if (res.record == null) { |
| 10465 | return new ComponentName("!", res.permission != null |
| 10466 | ? res.permission : "private to package"); |
| 10467 | } |
| 10468 | ServiceRecord r = res.record; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10469 | int targetPermissionUid = checkGrantUriPermissionFromIntentLocked( |
| 10470 | callingUid, r.packageName, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10471 | if (unscheduleServiceRestartLocked(r)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10472 | 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] | 10473 | } |
| 10474 | r.startRequested = true; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10475 | r.callStart = false; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10476 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10477 | service, targetPermissionUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10478 | r.lastActivity = SystemClock.uptimeMillis(); |
| 10479 | synchronized (r.stats.getBatteryStats()) { |
| 10480 | r.stats.startRunningLocked(); |
| 10481 | } |
| 10482 | if (!bringUpServiceLocked(r, service.getFlags(), false)) { |
| 10483 | return new ComponentName("!", "Service process is bad"); |
| 10484 | } |
| 10485 | return r.name; |
| 10486 | } |
| 10487 | } |
| 10488 | |
| 10489 | public ComponentName startService(IApplicationThread caller, Intent service, |
| 10490 | String resolvedType) { |
| 10491 | // Refuse possible leaked file descriptors |
| 10492 | if (service != null && service.hasFileDescriptors() == true) { |
| 10493 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10494 | } |
| 10495 | |
| 10496 | synchronized(this) { |
| 10497 | final int callingPid = Binder.getCallingPid(); |
| 10498 | final int callingUid = Binder.getCallingUid(); |
| 10499 | final long origId = Binder.clearCallingIdentity(); |
| 10500 | ComponentName res = startServiceLocked(caller, service, |
| 10501 | resolvedType, callingPid, callingUid); |
| 10502 | Binder.restoreCallingIdentity(origId); |
| 10503 | return res; |
| 10504 | } |
| 10505 | } |
| 10506 | |
| 10507 | ComponentName startServiceInPackage(int uid, |
| 10508 | Intent service, String resolvedType) { |
| 10509 | synchronized(this) { |
| 10510 | final long origId = Binder.clearCallingIdentity(); |
| 10511 | ComponentName res = startServiceLocked(null, service, |
| 10512 | resolvedType, -1, uid); |
| 10513 | Binder.restoreCallingIdentity(origId); |
| 10514 | return res; |
| 10515 | } |
| 10516 | } |
| 10517 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10518 | private void stopServiceLocked(ServiceRecord service) { |
| 10519 | synchronized (service.stats.getBatteryStats()) { |
| 10520 | service.stats.stopRunningLocked(); |
| 10521 | } |
| 10522 | service.startRequested = false; |
| 10523 | service.callStart = false; |
| 10524 | bringDownServiceLocked(service, false); |
| 10525 | } |
| 10526 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10527 | public int stopService(IApplicationThread caller, Intent service, |
| 10528 | String resolvedType) { |
| 10529 | // Refuse possible leaked file descriptors |
| 10530 | if (service != null && service.hasFileDescriptors() == true) { |
| 10531 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10532 | } |
| 10533 | |
| 10534 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10535 | if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10536 | + " type=" + resolvedType); |
| 10537 | |
| 10538 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 10539 | if (caller != null && callerApp == null) { |
| 10540 | throw new SecurityException( |
| 10541 | "Unable to find app for caller " + caller |
| 10542 | + " (pid=" + Binder.getCallingPid() |
| 10543 | + ") when stopping service " + service); |
| 10544 | } |
| 10545 | |
| 10546 | // If this service is active, make sure it is stopped. |
| 10547 | ServiceLookupResult r = findServiceLocked(service, resolvedType); |
| 10548 | if (r != null) { |
| 10549 | if (r.record != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10550 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10551 | try { |
| 10552 | stopServiceLocked(r.record); |
| 10553 | } finally { |
| 10554 | Binder.restoreCallingIdentity(origId); |
| 10555 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10556 | return 1; |
| 10557 | } |
| 10558 | return -1; |
| 10559 | } |
| 10560 | } |
| 10561 | |
| 10562 | return 0; |
| 10563 | } |
| 10564 | |
| 10565 | public IBinder peekService(Intent service, String resolvedType) { |
| 10566 | // Refuse possible leaked file descriptors |
| 10567 | if (service != null && service.hasFileDescriptors() == true) { |
| 10568 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10569 | } |
| 10570 | |
| 10571 | IBinder ret = null; |
| 10572 | |
| 10573 | synchronized(this) { |
| 10574 | ServiceLookupResult r = findServiceLocked(service, resolvedType); |
| 10575 | |
| 10576 | if (r != null) { |
| 10577 | // r.record is null if findServiceLocked() failed the caller permission check |
| 10578 | if (r.record == null) { |
| 10579 | throw new SecurityException( |
| 10580 | "Permission Denial: Accessing service " + r.record.name |
| 10581 | + " from pid=" + Binder.getCallingPid() |
| 10582 | + ", uid=" + Binder.getCallingUid() |
| 10583 | + " requires " + r.permission); |
| 10584 | } |
| 10585 | IntentBindRecord ib = r.record.bindings.get(r.record.intent); |
| 10586 | if (ib != null) { |
| 10587 | ret = ib.binder; |
| 10588 | } |
| 10589 | } |
| 10590 | } |
| 10591 | |
| 10592 | return ret; |
| 10593 | } |
| 10594 | |
| 10595 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 10596 | int startId) { |
| 10597 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10598 | if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10599 | + " " + token + " startId=" + startId); |
| 10600 | ServiceRecord r = findServiceLocked(className, token); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10601 | if (r != null) { |
| 10602 | if (startId >= 0) { |
| 10603 | // Asked to only stop if done with all work. Note that |
| 10604 | // to avoid leaks, we will take this as dropping all |
| 10605 | // start items up to and including this one. |
| 10606 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 10607 | if (si != null) { |
| 10608 | while (r.deliveredStarts.size() > 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10609 | ServiceRecord.StartItem cur = r.deliveredStarts.remove(0); |
| 10610 | cur.removeUriPermissionsLocked(); |
| 10611 | if (cur == si) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10612 | break; |
| 10613 | } |
| 10614 | } |
| 10615 | } |
| 10616 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10617 | if (r.getLastStartId() != startId) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10618 | return false; |
| 10619 | } |
| 10620 | |
| 10621 | if (r.deliveredStarts.size() > 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10622 | Slog.w(TAG, "stopServiceToken startId " + startId |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10623 | + " is last, but have " + r.deliveredStarts.size() |
| 10624 | + " remaining args"); |
| 10625 | } |
| 10626 | } |
| 10627 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10628 | synchronized (r.stats.getBatteryStats()) { |
| 10629 | r.stats.stopRunningLocked(); |
| 10630 | r.startRequested = false; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10631 | r.callStart = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10632 | } |
| 10633 | final long origId = Binder.clearCallingIdentity(); |
| 10634 | bringDownServiceLocked(r, false); |
| 10635 | Binder.restoreCallingIdentity(origId); |
| 10636 | return true; |
| 10637 | } |
| 10638 | } |
| 10639 | return false; |
| 10640 | } |
| 10641 | |
| 10642 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10643 | int id, Notification notification, boolean removeNotification) { |
| 10644 | final long origId = Binder.clearCallingIdentity(); |
| 10645 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10646 | synchronized(this) { |
| 10647 | ServiceRecord r = findServiceLocked(className, token); |
| 10648 | if (r != null) { |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10649 | if (id != 0) { |
| 10650 | if (notification == null) { |
| 10651 | throw new IllegalArgumentException("null notification"); |
| 10652 | } |
| 10653 | if (r.foregroundId != id) { |
| 10654 | r.cancelNotification(); |
| 10655 | r.foregroundId = id; |
| 10656 | } |
| 10657 | notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 10658 | r.foregroundNoti = notification; |
| 10659 | r.isForeground = true; |
| 10660 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10661 | if (r.app != null) { |
| 10662 | updateServiceForegroundLocked(r.app, true); |
| 10663 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10664 | } else { |
| 10665 | if (r.isForeground) { |
| 10666 | r.isForeground = false; |
| 10667 | if (r.app != null) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 10668 | updateLruProcessLocked(r.app, false, true); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10669 | updateServiceForegroundLocked(r.app, true); |
| 10670 | } |
| 10671 | } |
| 10672 | if (removeNotification) { |
| 10673 | r.cancelNotification(); |
| 10674 | r.foregroundId = 0; |
| 10675 | r.foregroundNoti = null; |
| 10676 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10677 | } |
| 10678 | } |
| 10679 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10680 | } finally { |
| 10681 | Binder.restoreCallingIdentity(origId); |
| 10682 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10683 | } |
| 10684 | |
| 10685 | public void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) { |
| 10686 | boolean anyForeground = false; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10687 | for (ServiceRecord sr : proc.services) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10688 | if (sr.isForeground) { |
| 10689 | anyForeground = true; |
| 10690 | break; |
| 10691 | } |
| 10692 | } |
| 10693 | if (anyForeground != proc.foregroundServices) { |
| 10694 | proc.foregroundServices = anyForeground; |
| 10695 | if (oomAdj) { |
| 10696 | updateOomAdjLocked(); |
| 10697 | } |
| 10698 | } |
| 10699 | } |
| 10700 | |
| 10701 | public int bindService(IApplicationThread caller, IBinder token, |
| 10702 | Intent service, String resolvedType, |
| 10703 | IServiceConnection connection, int flags) { |
| 10704 | // Refuse possible leaked file descriptors |
| 10705 | if (service != null && service.hasFileDescriptors() == true) { |
| 10706 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10707 | } |
| 10708 | |
| 10709 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10710 | if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10711 | + " type=" + resolvedType + " conn=" + connection.asBinder() |
| 10712 | + " flags=0x" + Integer.toHexString(flags)); |
| 10713 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 10714 | if (callerApp == null) { |
| 10715 | throw new SecurityException( |
| 10716 | "Unable to find app for caller " + caller |
| 10717 | + " (pid=" + Binder.getCallingPid() |
| 10718 | + ") when binding service " + service); |
| 10719 | } |
| 10720 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10721 | ActivityRecord activity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10722 | if (token != null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 10723 | activity = mMainStack.isInStackLocked(token); |
| 10724 | if (activity == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10725 | Slog.w(TAG, "Binding with unknown activity: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10726 | return 0; |
| 10727 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10728 | } |
| 10729 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10730 | int clientLabel = 0; |
| 10731 | PendingIntent clientIntent = null; |
| 10732 | |
| 10733 | if (callerApp.info.uid == Process.SYSTEM_UID) { |
| 10734 | // Hacky kind of thing -- allow system stuff to tell us |
| 10735 | // what they are, so we can report this elsewhere for |
| 10736 | // others to know why certain services are running. |
| 10737 | try { |
| 10738 | clientIntent = (PendingIntent)service.getParcelableExtra( |
| 10739 | Intent.EXTRA_CLIENT_INTENT); |
| 10740 | } catch (RuntimeException e) { |
| 10741 | } |
| 10742 | if (clientIntent != null) { |
| 10743 | clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0); |
| 10744 | if (clientLabel != 0) { |
| 10745 | // There are no useful extras in the intent, trash them. |
| 10746 | // System code calling with this stuff just needs to know |
| 10747 | // this will happen. |
| 10748 | service = service.cloneFilter(); |
| 10749 | } |
| 10750 | } |
| 10751 | } |
| 10752 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10753 | ServiceLookupResult res = |
| 10754 | retrieveServiceLocked(service, resolvedType, |
| 10755 | Binder.getCallingPid(), Binder.getCallingUid()); |
| 10756 | if (res == null) { |
| 10757 | return 0; |
| 10758 | } |
| 10759 | if (res.record == null) { |
| 10760 | return -1; |
| 10761 | } |
| 10762 | ServiceRecord s = res.record; |
| 10763 | |
| 10764 | final long origId = Binder.clearCallingIdentity(); |
| 10765 | |
| 10766 | if (unscheduleServiceRestartLocked(s)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10767 | if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: " |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10768 | + s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10769 | } |
| 10770 | |
| 10771 | AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp); |
| 10772 | ConnectionRecord c = new ConnectionRecord(b, activity, |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10773 | connection, flags, clientLabel, clientIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10774 | |
| 10775 | IBinder binder = connection.asBinder(); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10776 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 10777 | if (clist == null) { |
| 10778 | clist = new ArrayList<ConnectionRecord>(); |
| 10779 | s.connections.put(binder, clist); |
| 10780 | } |
| 10781 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10782 | b.connections.add(c); |
| 10783 | if (activity != null) { |
| 10784 | if (activity.connections == null) { |
| 10785 | activity.connections = new HashSet<ConnectionRecord>(); |
| 10786 | } |
| 10787 | activity.connections.add(c); |
| 10788 | } |
| 10789 | b.client.connections.add(c); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10790 | if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { |
| 10791 | b.client.hasAboveClient = true; |
| 10792 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10793 | clist = mServiceConnections.get(binder); |
| 10794 | if (clist == null) { |
| 10795 | clist = new ArrayList<ConnectionRecord>(); |
| 10796 | mServiceConnections.put(binder, clist); |
| 10797 | } |
| 10798 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10799 | |
| 10800 | if ((flags&Context.BIND_AUTO_CREATE) != 0) { |
| 10801 | s.lastActivity = SystemClock.uptimeMillis(); |
| 10802 | if (!bringUpServiceLocked(s, service.getFlags(), false)) { |
| 10803 | return 0; |
| 10804 | } |
| 10805 | } |
| 10806 | |
| 10807 | if (s.app != null) { |
| 10808 | // This could have made the service more important. |
| 10809 | updateOomAdjLocked(s.app); |
| 10810 | } |
| 10811 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10812 | 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] | 10813 | + ": received=" + b.intent.received |
| 10814 | + " apps=" + b.intent.apps.size() |
| 10815 | + " doRebind=" + b.intent.doRebind); |
| 10816 | |
| 10817 | if (s.app != null && b.intent.received) { |
| 10818 | // Service is already running, so we can immediately |
| 10819 | // publish the connection. |
| 10820 | try { |
| 10821 | c.conn.connected(s.name, b.intent.binder); |
| 10822 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10823 | Slog.w(TAG, "Failure sending service " + s.shortName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10824 | + " to connection " + c.conn.asBinder() |
| 10825 | + " (in " + c.binding.client.processName + ")", e); |
| 10826 | } |
| 10827 | |
| 10828 | // If this is the first app connected back to this binding, |
| 10829 | // and the service had previously asked to be told when |
| 10830 | // rebound, then do so. |
| 10831 | if (b.intent.apps.size() == 1 && b.intent.doRebind) { |
| 10832 | requestServiceBindingLocked(s, b.intent, true); |
| 10833 | } |
| 10834 | } else if (!b.intent.requested) { |
| 10835 | requestServiceBindingLocked(s, b.intent, false); |
| 10836 | } |
| 10837 | |
| 10838 | Binder.restoreCallingIdentity(origId); |
| 10839 | } |
| 10840 | |
| 10841 | return 1; |
| 10842 | } |
| 10843 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 10844 | void removeConnectionLocked( |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10845 | ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10846 | IBinder binder = c.conn.asBinder(); |
| 10847 | AppBindRecord b = c.binding; |
| 10848 | ServiceRecord s = b.service; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10849 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 10850 | if (clist != null) { |
| 10851 | clist.remove(c); |
| 10852 | if (clist.size() == 0) { |
| 10853 | s.connections.remove(binder); |
| 10854 | } |
| 10855 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10856 | b.connections.remove(c); |
| 10857 | if (c.activity != null && c.activity != skipAct) { |
| 10858 | if (c.activity.connections != null) { |
| 10859 | c.activity.connections.remove(c); |
| 10860 | } |
| 10861 | } |
| 10862 | if (b.client != skipApp) { |
| 10863 | b.client.connections.remove(c); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10864 | if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { |
| 10865 | b.client.updateHasAboveClientLocked(); |
| 10866 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10867 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10868 | clist = mServiceConnections.get(binder); |
| 10869 | if (clist != null) { |
| 10870 | clist.remove(c); |
| 10871 | if (clist.size() == 0) { |
| 10872 | mServiceConnections.remove(binder); |
| 10873 | } |
| 10874 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10875 | |
| 10876 | if (b.connections.size() == 0) { |
| 10877 | b.intent.apps.remove(b.client); |
| 10878 | } |
| 10879 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10880 | if (!c.serviceDead) { |
| 10881 | if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent |
| 10882 | + ": shouldUnbind=" + b.intent.hasBound); |
| 10883 | if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0 |
| 10884 | && b.intent.hasBound) { |
| 10885 | try { |
| 10886 | bumpServiceExecutingLocked(s, "unbind"); |
| 10887 | updateOomAdjLocked(s.app); |
| 10888 | b.intent.hasBound = false; |
| 10889 | // Assume the client doesn't want to know about a rebind; |
| 10890 | // we will deal with that later if it asks for one. |
| 10891 | b.intent.doRebind = false; |
| 10892 | s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent()); |
| 10893 | } catch (Exception e) { |
| 10894 | Slog.w(TAG, "Exception when unbinding service " + s.shortName, e); |
| 10895 | serviceDoneExecutingLocked(s, true); |
| 10896 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10897 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10898 | |
| 10899 | if ((c.flags&Context.BIND_AUTO_CREATE) != 0) { |
| 10900 | bringDownServiceLocked(s, false); |
| 10901 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10902 | } |
| 10903 | } |
| 10904 | |
| 10905 | public boolean unbindService(IServiceConnection connection) { |
| 10906 | synchronized (this) { |
| 10907 | IBinder binder = connection.asBinder(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10908 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10909 | ArrayList<ConnectionRecord> clist = mServiceConnections.get(binder); |
| 10910 | if (clist == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10911 | 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] | 10912 | + connection.asBinder()); |
| 10913 | return false; |
| 10914 | } |
| 10915 | |
| 10916 | final long origId = Binder.clearCallingIdentity(); |
| 10917 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10918 | while (clist.size() > 0) { |
| 10919 | ConnectionRecord r = clist.get(0); |
| 10920 | removeConnectionLocked(r, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10921 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10922 | if (r.binding.service.app != null) { |
| 10923 | // This could have made the service less important. |
| 10924 | updateOomAdjLocked(r.binding.service.app); |
| 10925 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10926 | } |
| 10927 | |
| 10928 | Binder.restoreCallingIdentity(origId); |
| 10929 | } |
| 10930 | |
| 10931 | return true; |
| 10932 | } |
| 10933 | |
| 10934 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 10935 | // Refuse possible leaked file descriptors |
| 10936 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 10937 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10938 | } |
| 10939 | |
| 10940 | synchronized(this) { |
| 10941 | if (!(token instanceof ServiceRecord)) { |
| 10942 | throw new IllegalArgumentException("Invalid service token"); |
| 10943 | } |
| 10944 | ServiceRecord r = (ServiceRecord)token; |
| 10945 | |
| 10946 | final long origId = Binder.clearCallingIdentity(); |
| 10947 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10948 | if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10949 | + " " + intent + ": " + service); |
| 10950 | if (r != null) { |
| 10951 | Intent.FilterComparison filter |
| 10952 | = new Intent.FilterComparison(intent); |
| 10953 | IntentBindRecord b = r.bindings.get(filter); |
| 10954 | if (b != null && !b.received) { |
| 10955 | b.binder = service; |
| 10956 | b.requested = true; |
| 10957 | b.received = true; |
| 10958 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10959 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10960 | = r.connections.values().iterator(); |
| 10961 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10962 | ArrayList<ConnectionRecord> clist = it.next(); |
| 10963 | for (int i=0; i<clist.size(); i++) { |
| 10964 | ConnectionRecord c = clist.get(i); |
| 10965 | if (!filter.equals(c.binding.intent.intent)) { |
| 10966 | if (DEBUG_SERVICE) Slog.v( |
| 10967 | TAG, "Not publishing to: " + c); |
| 10968 | if (DEBUG_SERVICE) Slog.v( |
| 10969 | TAG, "Bound intent: " + c.binding.intent.intent); |
| 10970 | if (DEBUG_SERVICE) Slog.v( |
| 10971 | TAG, "Published intent: " + intent); |
| 10972 | continue; |
| 10973 | } |
| 10974 | if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c); |
| 10975 | try { |
| 10976 | c.conn.connected(r.name, service); |
| 10977 | } catch (Exception e) { |
| 10978 | Slog.w(TAG, "Failure sending service " + r.name + |
| 10979 | " to connection " + c.conn.asBinder() + |
| 10980 | " (in " + c.binding.client.processName + ")", e); |
| 10981 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10982 | } |
| 10983 | } |
| 10984 | } |
| 10985 | } |
| 10986 | |
| 10987 | serviceDoneExecutingLocked(r, mStoppingServices.contains(r)); |
| 10988 | |
| 10989 | Binder.restoreCallingIdentity(origId); |
| 10990 | } |
| 10991 | } |
| 10992 | } |
| 10993 | |
| 10994 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 10995 | // Refuse possible leaked file descriptors |
| 10996 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 10997 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10998 | } |
| 10999 | |
| 11000 | synchronized(this) { |
| 11001 | if (!(token instanceof ServiceRecord)) { |
| 11002 | throw new IllegalArgumentException("Invalid service token"); |
| 11003 | } |
| 11004 | ServiceRecord r = (ServiceRecord)token; |
| 11005 | |
| 11006 | final long origId = Binder.clearCallingIdentity(); |
| 11007 | |
| 11008 | if (r != null) { |
| 11009 | Intent.FilterComparison filter |
| 11010 | = new Intent.FilterComparison(intent); |
| 11011 | IntentBindRecord b = r.bindings.get(filter); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11012 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11013 | + " at " + b + ": apps=" |
| 11014 | + (b != null ? b.apps.size() : 0)); |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11015 | |
| 11016 | boolean inStopping = mStoppingServices.contains(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11017 | if (b != null) { |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11018 | if (b.apps.size() > 0 && !inStopping) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11019 | // Applications have already bound since the last |
| 11020 | // unbind, so just rebind right here. |
| 11021 | requestServiceBindingLocked(r, b, true); |
| 11022 | } else { |
| 11023 | // Note to tell the service the next time there is |
| 11024 | // a new client. |
| 11025 | b.doRebind = true; |
| 11026 | } |
| 11027 | } |
| 11028 | |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11029 | serviceDoneExecutingLocked(r, inStopping); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11030 | |
| 11031 | Binder.restoreCallingIdentity(origId); |
| 11032 | } |
| 11033 | } |
| 11034 | } |
| 11035 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11036 | 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] | 11037 | synchronized(this) { |
| 11038 | if (!(token instanceof ServiceRecord)) { |
| 11039 | throw new IllegalArgumentException("Invalid service token"); |
| 11040 | } |
| 11041 | ServiceRecord r = (ServiceRecord)token; |
| 11042 | boolean inStopping = mStoppingServices.contains(token); |
| 11043 | if (r != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11044 | if (r != token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11045 | Slog.w(TAG, "Done executing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11046 | + " with incorrect token: given " + token |
| 11047 | + ", expected " + r); |
| 11048 | return; |
| 11049 | } |
| 11050 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11051 | if (type == 1) { |
| 11052 | // This is a call from a service start... take care of |
| 11053 | // book-keeping. |
| 11054 | r.callStart = true; |
| 11055 | switch (res) { |
| 11056 | case Service.START_STICKY_COMPATIBILITY: |
| 11057 | case Service.START_STICKY: { |
| 11058 | // We are done with the associated start arguments. |
| 11059 | r.findDeliveredStart(startId, true); |
| 11060 | // Don't stop if killed. |
| 11061 | r.stopIfKilled = false; |
| 11062 | break; |
| 11063 | } |
| 11064 | case Service.START_NOT_STICKY: { |
| 11065 | // We are done with the associated start arguments. |
| 11066 | r.findDeliveredStart(startId, true); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11067 | if (r.getLastStartId() == startId) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11068 | // There is no more work, and this service |
| 11069 | // doesn't want to hang around if killed. |
| 11070 | r.stopIfKilled = true; |
| 11071 | } |
| 11072 | break; |
| 11073 | } |
| 11074 | case Service.START_REDELIVER_INTENT: { |
| 11075 | // We'll keep this item until they explicitly |
| 11076 | // call stop for it, but keep track of the fact |
| 11077 | // that it was delivered. |
| 11078 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 11079 | if (si != null) { |
| 11080 | si.deliveryCount = 0; |
| 11081 | si.doneExecutingCount++; |
| 11082 | // Don't stop if killed. |
| 11083 | r.stopIfKilled = true; |
| 11084 | } |
| 11085 | break; |
| 11086 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11087 | case Service.START_TASK_REMOVED_COMPLETE: { |
| 11088 | // Special processing for onTaskRemoved(). Don't |
| 11089 | // impact normal onStartCommand() processing. |
| 11090 | r.findDeliveredStart(startId, true); |
| 11091 | break; |
| 11092 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11093 | default: |
| 11094 | throw new IllegalArgumentException( |
| 11095 | "Unknown service start result: " + res); |
| 11096 | } |
| 11097 | if (res == Service.START_STICKY_COMPATIBILITY) { |
| 11098 | r.callStart = false; |
| 11099 | } |
| 11100 | } |
| 11101 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11102 | final long origId = Binder.clearCallingIdentity(); |
| 11103 | serviceDoneExecutingLocked(r, inStopping); |
| 11104 | Binder.restoreCallingIdentity(origId); |
| 11105 | } else { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11106 | Slog.w(TAG, "Done executing unknown service from pid " |
| 11107 | + Binder.getCallingPid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11108 | } |
| 11109 | } |
| 11110 | } |
| 11111 | |
| 11112 | public void serviceDoneExecutingLocked(ServiceRecord r, boolean inStopping) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11113 | if (DEBUG_SERVICE) Slog.v(TAG, "<<< DONE EXECUTING " + r |
| 11114 | + ": nesting=" + r.executeNesting |
| 11115 | + ", inStopping=" + inStopping + ", app=" + r.app); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11116 | 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] | 11117 | r.executeNesting--; |
| 11118 | if (r.executeNesting <= 0 && r.app != null) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11119 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 11120 | "Nesting at 0 of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11121 | r.app.executingServices.remove(r); |
| 11122 | if (r.app.executingServices.size() == 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11123 | if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG, |
| 11124 | "No more executingServices of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11125 | mHandler.removeMessages(SERVICE_TIMEOUT_MSG, r.app); |
| 11126 | } |
| 11127 | if (inStopping) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11128 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 11129 | "doneExecuting remove stopping " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11130 | mStoppingServices.remove(r); |
Mattias Petersson | 3996b41 | 2010-10-27 09:32:51 +0200 | [diff] [blame] | 11131 | r.bindings.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11132 | } |
| 11133 | updateOomAdjLocked(r.app); |
| 11134 | } |
| 11135 | } |
| 11136 | |
| 11137 | void serviceTimeout(ProcessRecord proc) { |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11138 | String anrMessage = null; |
| 11139 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11140 | synchronized(this) { |
| 11141 | if (proc.executingServices.size() == 0 || proc.thread == null) { |
| 11142 | return; |
| 11143 | } |
| 11144 | long maxTime = SystemClock.uptimeMillis() - SERVICE_TIMEOUT; |
| 11145 | Iterator<ServiceRecord> it = proc.executingServices.iterator(); |
| 11146 | ServiceRecord timeout = null; |
| 11147 | long nextTime = 0; |
| 11148 | while (it.hasNext()) { |
| 11149 | ServiceRecord sr = it.next(); |
| 11150 | if (sr.executingStart < maxTime) { |
| 11151 | timeout = sr; |
| 11152 | break; |
| 11153 | } |
| 11154 | if (sr.executingStart > nextTime) { |
| 11155 | nextTime = sr.executingStart; |
| 11156 | } |
| 11157 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11158 | if (timeout != null && mLruProcesses.contains(proc)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11159 | Slog.w(TAG, "Timeout executing service: " + timeout); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11160 | anrMessage = "Executing service " + timeout.shortName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11161 | } else { |
| 11162 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 11163 | msg.obj = proc; |
| 11164 | mHandler.sendMessageAtTime(msg, nextTime+SERVICE_TIMEOUT); |
| 11165 | } |
| 11166 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11167 | |
| 11168 | if (anrMessage != null) { |
| 11169 | appNotResponding(proc, null, null, anrMessage); |
| 11170 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11171 | } |
| 11172 | |
| 11173 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11174 | // BACKUP AND RESTORE |
| 11175 | // ========================================================= |
| 11176 | |
| 11177 | // Cause the target app to be launched if necessary and its backup agent |
| 11178 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 11179 | // activity manager to announce its creation. |
| 11180 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11181 | if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11182 | enforceCallingPermission("android.permission.BACKUP", "startBackupAgent"); |
| 11183 | |
| 11184 | synchronized(this) { |
| 11185 | // !!! TODO: currently no check here that we're already bound |
| 11186 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 11187 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 11188 | synchronized (stats) { |
| 11189 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 11190 | } |
| 11191 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11192 | // Backup agent is now in use, its package can't be stopped. |
| 11193 | try { |
| 11194 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 11195 | app.packageName, false); |
| 11196 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 11197 | } catch (IllegalArgumentException e) { |
| 11198 | Slog.w(TAG, "Failed trying to unstop package " |
| 11199 | + app.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11200 | } |
| 11201 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11202 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 11203 | ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) |
| 11204 | ? new ComponentName(app.packageName, app.backupAgentName) |
| 11205 | : new ComponentName("android", "FullBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11206 | // startProcessLocked() returns existing proc's record if it's already running |
| 11207 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 11208 | false, 0, "backup", hostingName, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11209 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11210 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11211 | return false; |
| 11212 | } |
| 11213 | |
| 11214 | r.app = proc; |
| 11215 | mBackupTarget = r; |
| 11216 | mBackupAppName = app.packageName; |
| 11217 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11218 | // Try not to kill the process during backup |
| 11219 | updateOomAdjLocked(proc); |
| 11220 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11221 | // If the process is already attached, schedule the creation of the backup agent now. |
| 11222 | // If it is not yet live, this will be done when it attaches to the framework. |
| 11223 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11224 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11225 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11226 | proc.thread.scheduleCreateBackupAgent(app, |
| 11227 | compatibilityInfoForPackageLocked(app), backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11228 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 11229 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11230 | } |
| 11231 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11232 | 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] | 11233 | } |
| 11234 | // Invariants: at this point, the target app process exists and the application |
| 11235 | // is either already running or in the process of coming up. mBackupTarget and |
| 11236 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 11237 | // know that it's scheduled for a backup-agent operation. |
| 11238 | } |
| 11239 | |
| 11240 | return true; |
| 11241 | } |
| 11242 | |
| 11243 | // A backup agent has just come up |
| 11244 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11245 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11246 | + " = " + agent); |
| 11247 | |
| 11248 | synchronized(this) { |
| 11249 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11250 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11251 | return; |
| 11252 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11253 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11254 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11255 | long oldIdent = Binder.clearCallingIdentity(); |
| 11256 | try { |
| 11257 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 11258 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 11259 | bm.agentConnected(agentPackageName, agent); |
| 11260 | } catch (RemoteException e) { |
| 11261 | // can't happen; the backup manager service is local |
| 11262 | } catch (Exception e) { |
| 11263 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 11264 | e.printStackTrace(); |
| 11265 | } finally { |
| 11266 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11267 | } |
| 11268 | } |
| 11269 | |
| 11270 | // done with this agent |
| 11271 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11272 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11273 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11274 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11275 | return; |
| 11276 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11277 | |
| 11278 | synchronized(this) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11279 | if (mBackupAppName == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11280 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11281 | return; |
| 11282 | } |
| 11283 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11284 | if (!mBackupAppName.equals(appInfo.packageName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11285 | 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] | 11286 | return; |
| 11287 | } |
| 11288 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11289 | ProcessRecord proc = mBackupTarget.app; |
| 11290 | mBackupTarget = null; |
| 11291 | mBackupAppName = null; |
| 11292 | |
| 11293 | // Not backing this app up any more; reset its OOM adjustment |
| 11294 | updateOomAdjLocked(proc); |
| 11295 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11296 | // If the app crashed during backup, 'thread' will be null here |
| 11297 | if (proc.thread != null) { |
| 11298 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11299 | proc.thread.scheduleDestroyBackupAgent(appInfo, |
| 11300 | compatibilityInfoForPackageLocked(appInfo)); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11301 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11302 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11303 | e.printStackTrace(); |
| 11304 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11305 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11306 | } |
| 11307 | } |
| 11308 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11309 | // BROADCASTS |
| 11310 | // ========================================================= |
| 11311 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 11312 | private final List getStickiesLocked(String action, IntentFilter filter, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11313 | List cur) { |
| 11314 | final ContentResolver resolver = mContext.getContentResolver(); |
| 11315 | final ArrayList<Intent> list = mStickyBroadcasts.get(action); |
| 11316 | if (list == null) { |
| 11317 | return cur; |
| 11318 | } |
| 11319 | int N = list.size(); |
| 11320 | for (int i=0; i<N; i++) { |
| 11321 | Intent intent = list.get(i); |
| 11322 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 11323 | if (cur == null) { |
| 11324 | cur = new ArrayList<Intent>(); |
| 11325 | } |
| 11326 | cur.add(intent); |
| 11327 | } |
| 11328 | } |
| 11329 | return cur; |
| 11330 | } |
| 11331 | |
| 11332 | private final void scheduleBroadcastsLocked() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11333 | if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts: current=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11334 | + mBroadcastsScheduled); |
| 11335 | |
| 11336 | if (mBroadcastsScheduled) { |
| 11337 | return; |
| 11338 | } |
| 11339 | mHandler.sendEmptyMessage(BROADCAST_INTENT_MSG); |
| 11340 | mBroadcastsScheduled = true; |
| 11341 | } |
| 11342 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11343 | public Intent registerReceiver(IApplicationThread caller, String callerPackage, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11344 | IIntentReceiver receiver, IntentFilter filter, String permission) { |
| 11345 | synchronized(this) { |
| 11346 | ProcessRecord callerApp = null; |
| 11347 | if (caller != null) { |
| 11348 | callerApp = getRecordForAppLocked(caller); |
| 11349 | if (callerApp == null) { |
| 11350 | throw new SecurityException( |
| 11351 | "Unable to find app for caller " + caller |
| 11352 | + " (pid=" + Binder.getCallingPid() |
| 11353 | + ") when registering receiver " + receiver); |
| 11354 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11355 | if (callerApp.info.uid != Process.SYSTEM_UID && |
| 11356 | !callerApp.pkgList.contains(callerPackage)) { |
| 11357 | throw new SecurityException("Given caller package " + callerPackage |
| 11358 | + " is not running in process " + callerApp); |
| 11359 | } |
| 11360 | } else { |
| 11361 | callerPackage = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11362 | } |
| 11363 | |
| 11364 | List allSticky = null; |
| 11365 | |
| 11366 | // Look for any matching sticky broadcasts... |
| 11367 | Iterator actions = filter.actionsIterator(); |
| 11368 | if (actions != null) { |
| 11369 | while (actions.hasNext()) { |
| 11370 | String action = (String)actions.next(); |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 11371 | allSticky = getStickiesLocked(action, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11372 | } |
| 11373 | } else { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 11374 | allSticky = getStickiesLocked(null, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11375 | } |
| 11376 | |
| 11377 | // The first sticky in the list is returned directly back to |
| 11378 | // the client. |
| 11379 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 11380 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11381 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11382 | + ": " + sticky); |
| 11383 | |
| 11384 | if (receiver == null) { |
| 11385 | return sticky; |
| 11386 | } |
| 11387 | |
| 11388 | ReceiverList rl |
| 11389 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 11390 | if (rl == null) { |
| 11391 | rl = new ReceiverList(this, callerApp, |
| 11392 | Binder.getCallingPid(), |
| 11393 | Binder.getCallingUid(), receiver); |
| 11394 | if (rl.app != null) { |
| 11395 | rl.app.receivers.add(rl); |
| 11396 | } else { |
| 11397 | try { |
| 11398 | receiver.asBinder().linkToDeath(rl, 0); |
| 11399 | } catch (RemoteException e) { |
| 11400 | return sticky; |
| 11401 | } |
| 11402 | rl.linkedToDeath = true; |
| 11403 | } |
| 11404 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
| 11405 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11406 | BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, permission); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11407 | rl.add(bf); |
| 11408 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11409 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11410 | } |
| 11411 | mReceiverResolver.addFilter(bf); |
| 11412 | |
| 11413 | // Enqueue broadcasts for all existing stickies that match |
| 11414 | // this filter. |
| 11415 | if (allSticky != null) { |
| 11416 | ArrayList receivers = new ArrayList(); |
| 11417 | receivers.add(bf); |
| 11418 | |
| 11419 | int N = allSticky.size(); |
| 11420 | for (int i=0; i<N; i++) { |
| 11421 | Intent intent = (Intent)allSticky.get(i); |
| 11422 | BroadcastRecord r = new BroadcastRecord(intent, null, |
| 11423 | null, -1, -1, null, receivers, null, 0, null, null, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11424 | false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11425 | if (mParallelBroadcasts.size() == 0) { |
| 11426 | scheduleBroadcastsLocked(); |
| 11427 | } |
| 11428 | mParallelBroadcasts.add(r); |
| 11429 | } |
| 11430 | } |
| 11431 | |
| 11432 | return sticky; |
| 11433 | } |
| 11434 | } |
| 11435 | |
| 11436 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11437 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11438 | |
| 11439 | boolean doNext = false; |
| 11440 | |
| 11441 | synchronized(this) { |
| 11442 | ReceiverList rl |
| 11443 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 11444 | if (rl != null) { |
| 11445 | if (rl.curBroadcast != null) { |
| 11446 | BroadcastRecord r = rl.curBroadcast; |
| 11447 | doNext = finishReceiverLocked( |
| 11448 | receiver.asBinder(), r.resultCode, r.resultData, |
| 11449 | r.resultExtras, r.resultAbort, true); |
| 11450 | } |
| 11451 | |
| 11452 | if (rl.app != null) { |
| 11453 | rl.app.receivers.remove(rl); |
| 11454 | } |
| 11455 | removeReceiverLocked(rl); |
| 11456 | if (rl.linkedToDeath) { |
| 11457 | rl.linkedToDeath = false; |
| 11458 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 11459 | } |
| 11460 | } |
| 11461 | } |
| 11462 | |
| 11463 | if (!doNext) { |
| 11464 | return; |
| 11465 | } |
| 11466 | |
| 11467 | final long origId = Binder.clearCallingIdentity(); |
| 11468 | processNextBroadcast(false); |
| 11469 | trimApplications(); |
| 11470 | Binder.restoreCallingIdentity(origId); |
| 11471 | } |
| 11472 | |
| 11473 | void removeReceiverLocked(ReceiverList rl) { |
| 11474 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 11475 | int N = rl.size(); |
| 11476 | for (int i=0; i<N; i++) { |
| 11477 | mReceiverResolver.removeFilter(rl.get(i)); |
| 11478 | } |
| 11479 | } |
| 11480 | |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11481 | private final void sendPackageBroadcastLocked(int cmd, String[] packages) { |
| 11482 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 11483 | ProcessRecord r = mLruProcesses.get(i); |
| 11484 | if (r.thread != null) { |
| 11485 | try { |
| 11486 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 11487 | } catch (RemoteException ex) { |
| 11488 | } |
| 11489 | } |
| 11490 | } |
| 11491 | } |
| 11492 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11493 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 11494 | String callerPackage, Intent intent, String resolvedType, |
| 11495 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 11496 | Bundle map, String requiredPermission, |
| 11497 | boolean ordered, boolean sticky, int callingPid, int callingUid) { |
| 11498 | intent = new Intent(intent); |
| 11499 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11500 | // By default broadcasts do not go to stopped apps. |
| 11501 | intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES); |
| 11502 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11503 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11504 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
| 11505 | + " ordered=" + ordered); |
| 11506 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11507 | 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] | 11508 | } |
| 11509 | |
| 11510 | // Handle special intents: if this broadcast is from the package |
| 11511 | // manager about a package being removed, we need to remove all of |
| 11512 | // its activities from the history stack. |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11513 | final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11514 | intent.getAction()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11515 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 11516 | || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11517 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11518 | || uidRemoved) { |
| 11519 | if (checkComponentPermission( |
| 11520 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11521 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11522 | == PackageManager.PERMISSION_GRANTED) { |
| 11523 | if (uidRemoved) { |
| 11524 | final Bundle intentExtras = intent.getExtras(); |
| 11525 | final int uid = intentExtras != null |
| 11526 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 11527 | if (uid >= 0) { |
| 11528 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 11529 | synchronized (bs) { |
| 11530 | bs.removeUidStatsLocked(uid); |
| 11531 | } |
| 11532 | } |
| 11533 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11534 | // If resources are unvailble just force stop all |
| 11535 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11536 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11537 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 11538 | if (list != null && (list.length > 0)) { |
| 11539 | for (String pkg : list) { |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 11540 | forceStopPackageLocked(pkg, -1, false, true, true, false); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11541 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11542 | sendPackageBroadcastLocked( |
| 11543 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11544 | } |
| 11545 | } else { |
| 11546 | Uri data = intent.getData(); |
| 11547 | String ssp; |
| 11548 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11549 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 11550 | forceStopPackageLocked(ssp, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 11551 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true, false); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 11552 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11553 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11554 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
| 11555 | new String[] {ssp}); |
| 11556 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11557 | } |
| 11558 | } |
| 11559 | } |
| 11560 | } else { |
| 11561 | String msg = "Permission Denial: " + intent.getAction() |
| 11562 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 11563 | + ", uid=" + callingUid + ")" |
| 11564 | + " requires " |
| 11565 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11566 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11567 | throw new SecurityException(msg); |
| 11568 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11569 | |
| 11570 | // Special case for adding a package: by default turn on compatibility |
| 11571 | // mode. |
| 11572 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 11573 | Uri data = intent.getData(); |
| 11574 | String ssp; |
| 11575 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11576 | mCompatModePackages.handlePackageAddedLocked(ssp, |
| 11577 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11578 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11579 | } |
| 11580 | |
| 11581 | /* |
| 11582 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 11583 | * of all currently running processes. This message will get queued up before the broadcast |
| 11584 | * happens. |
| 11585 | */ |
| 11586 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 11587 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 11588 | } |
| 11589 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 11590 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 11591 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 11592 | } |
| 11593 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 11594 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 11595 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 11596 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 11597 | } |
| 11598 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 11599 | /* |
| 11600 | * Prevent non-system code (defined here to be non-persistent |
| 11601 | * processes) from sending protected broadcasts. |
| 11602 | */ |
| 11603 | if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID |
| 11604 | || callingUid == Process.SHELL_UID || callingUid == 0) { |
| 11605 | // Always okay. |
| 11606 | } else if (callerApp == null || !callerApp.persistent) { |
| 11607 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11608 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 11609 | intent.getAction())) { |
| 11610 | String msg = "Permission Denial: not allowed to send broadcast " |
| 11611 | + intent.getAction() + " from pid=" |
| 11612 | + callingPid + ", uid=" + callingUid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11613 | Slog.w(TAG, msg); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 11614 | throw new SecurityException(msg); |
| 11615 | } |
| 11616 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11617 | Slog.w(TAG, "Remote exception", e); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 11618 | return BROADCAST_SUCCESS; |
| 11619 | } |
| 11620 | } |
| 11621 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11622 | // Add to the sticky list if requested. |
| 11623 | if (sticky) { |
| 11624 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 11625 | callingPid, callingUid) |
| 11626 | != PackageManager.PERMISSION_GRANTED) { |
| 11627 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 11628 | + callingPid + ", uid=" + callingUid |
| 11629 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11630 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11631 | throw new SecurityException(msg); |
| 11632 | } |
| 11633 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11634 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11635 | + " and enforce permission " + requiredPermission); |
| 11636 | return BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
| 11637 | } |
| 11638 | if (intent.getComponent() != null) { |
| 11639 | throw new SecurityException( |
| 11640 | "Sticky broadcasts can't target a specific component"); |
| 11641 | } |
| 11642 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 11643 | if (list == null) { |
| 11644 | list = new ArrayList<Intent>(); |
| 11645 | mStickyBroadcasts.put(intent.getAction(), list); |
| 11646 | } |
| 11647 | int N = list.size(); |
| 11648 | int i; |
| 11649 | for (i=0; i<N; i++) { |
| 11650 | if (intent.filterEquals(list.get(i))) { |
| 11651 | // This sticky already exists, replace it. |
| 11652 | list.set(i, new Intent(intent)); |
| 11653 | break; |
| 11654 | } |
| 11655 | } |
| 11656 | if (i >= N) { |
| 11657 | list.add(new Intent(intent)); |
| 11658 | } |
| 11659 | } |
| 11660 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11661 | // Figure out who all will receive this broadcast. |
| 11662 | List receivers = null; |
| 11663 | List<BroadcastFilter> registeredReceivers = null; |
| 11664 | try { |
| 11665 | if (intent.getComponent() != null) { |
| 11666 | // Broadcast is going to one specific receiver class... |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11667 | ActivityInfo ai = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11668 | getReceiverInfo(intent.getComponent(), STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11669 | if (ai != null) { |
| 11670 | receivers = new ArrayList(); |
| 11671 | ResolveInfo ri = new ResolveInfo(); |
| 11672 | ri.activityInfo = ai; |
| 11673 | receivers.add(ri); |
| 11674 | } |
| 11675 | } else { |
| 11676 | // Need to resolve the intent to interested receivers... |
| 11677 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 11678 | == 0) { |
| 11679 | receivers = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11680 | AppGlobals.getPackageManager().queryIntentReceivers( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11681 | intent, resolvedType, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11682 | } |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 11683 | registeredReceivers = mReceiverResolver.queryIntent(intent, resolvedType, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11684 | } |
| 11685 | } catch (RemoteException ex) { |
| 11686 | // pm is in same process, this will never happen. |
| 11687 | } |
| 11688 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11689 | final boolean replacePending = |
| 11690 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 11691 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11692 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11693 | + " replacePending=" + replacePending); |
| 11694 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11695 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 11696 | if (!ordered && NR > 0) { |
| 11697 | // If we are not serializing this broadcast, then send the |
| 11698 | // registered receivers separately so they don't wait for the |
| 11699 | // components to be launched. |
| 11700 | BroadcastRecord r = new BroadcastRecord(intent, callerApp, |
| 11701 | callerPackage, callingPid, callingUid, requiredPermission, |
| 11702 | registeredReceivers, resultTo, resultCode, resultData, map, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11703 | ordered, sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11704 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11705 | TAG, "Enqueueing parallel broadcast " + r |
| 11706 | + ": prev had " + mParallelBroadcasts.size()); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11707 | boolean replaced = false; |
| 11708 | if (replacePending) { |
| 11709 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 11710 | if (intent.filterEquals(mParallelBroadcasts.get(i).intent)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11711 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11712 | "***** DROPPING PARALLEL: " + intent); |
| 11713 | mParallelBroadcasts.set(i, r); |
| 11714 | replaced = true; |
| 11715 | break; |
| 11716 | } |
| 11717 | } |
| 11718 | } |
| 11719 | if (!replaced) { |
| 11720 | mParallelBroadcasts.add(r); |
| 11721 | scheduleBroadcastsLocked(); |
| 11722 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11723 | registeredReceivers = null; |
| 11724 | NR = 0; |
| 11725 | } |
| 11726 | |
| 11727 | // Merge into one list. |
| 11728 | int ir = 0; |
| 11729 | if (receivers != null) { |
| 11730 | // A special case for PACKAGE_ADDED: do not allow the package |
| 11731 | // being added to see this broadcast. This prevents them from |
| 11732 | // using this as a back door to get run as soon as they are |
| 11733 | // installed. Maybe in the future we want to have a special install |
| 11734 | // broadcast or such for apps, but we'd like to deliberately make |
| 11735 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11736 | String skipPackages[] = null; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11737 | if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 11738 | || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 11739 | || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11740 | Uri data = intent.getData(); |
| 11741 | if (data != null) { |
| 11742 | String pkgName = data.getSchemeSpecificPart(); |
| 11743 | if (pkgName != null) { |
| 11744 | skipPackages = new String[] { pkgName }; |
| 11745 | } |
| 11746 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11747 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11748 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 11749 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11750 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 11751 | for (String skipPackage : skipPackages) { |
| 11752 | if (skipPackage != null) { |
| 11753 | int NT = receivers.size(); |
| 11754 | for (int it=0; it<NT; it++) { |
| 11755 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 11756 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 11757 | receivers.remove(it); |
| 11758 | it--; |
| 11759 | NT--; |
| 11760 | } |
| 11761 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11762 | } |
| 11763 | } |
| 11764 | } |
| 11765 | |
| 11766 | int NT = receivers != null ? receivers.size() : 0; |
| 11767 | int it = 0; |
| 11768 | ResolveInfo curt = null; |
| 11769 | BroadcastFilter curr = null; |
| 11770 | while (it < NT && ir < NR) { |
| 11771 | if (curt == null) { |
| 11772 | curt = (ResolveInfo)receivers.get(it); |
| 11773 | } |
| 11774 | if (curr == null) { |
| 11775 | curr = registeredReceivers.get(ir); |
| 11776 | } |
| 11777 | if (curr.getPriority() >= curt.priority) { |
| 11778 | // Insert this broadcast record into the final list. |
| 11779 | receivers.add(it, curr); |
| 11780 | ir++; |
| 11781 | curr = null; |
| 11782 | it++; |
| 11783 | NT++; |
| 11784 | } else { |
| 11785 | // Skip to the next ResolveInfo in the final list. |
| 11786 | it++; |
| 11787 | curt = null; |
| 11788 | } |
| 11789 | } |
| 11790 | } |
| 11791 | while (ir < NR) { |
| 11792 | if (receivers == null) { |
| 11793 | receivers = new ArrayList(); |
| 11794 | } |
| 11795 | receivers.add(registeredReceivers.get(ir)); |
| 11796 | ir++; |
| 11797 | } |
| 11798 | |
| 11799 | if ((receivers != null && receivers.size() > 0) |
| 11800 | || resultTo != null) { |
| 11801 | BroadcastRecord r = new BroadcastRecord(intent, callerApp, |
| 11802 | callerPackage, callingPid, callingUid, requiredPermission, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11803 | receivers, resultTo, resultCode, resultData, map, ordered, |
| 11804 | sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11805 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11806 | TAG, "Enqueueing ordered broadcast " + r |
| 11807 | + ": prev had " + mOrderedBroadcasts.size()); |
| 11808 | if (DEBUG_BROADCAST) { |
| 11809 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11810 | 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] | 11811 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11812 | boolean replaced = false; |
| 11813 | if (replacePending) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11814 | for (int i=mOrderedBroadcasts.size()-1; i>0; i--) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11815 | if (intent.filterEquals(mOrderedBroadcasts.get(i).intent)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11816 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11817 | "***** DROPPING ORDERED: " + intent); |
| 11818 | mOrderedBroadcasts.set(i, r); |
| 11819 | replaced = true; |
| 11820 | break; |
| 11821 | } |
| 11822 | } |
| 11823 | } |
| 11824 | if (!replaced) { |
| 11825 | mOrderedBroadcasts.add(r); |
| 11826 | scheduleBroadcastsLocked(); |
| 11827 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11828 | } |
| 11829 | |
| 11830 | return BROADCAST_SUCCESS; |
| 11831 | } |
| 11832 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11833 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11834 | // Refuse possible leaked file descriptors |
| 11835 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11836 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11837 | } |
| 11838 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11839 | int flags = intent.getFlags(); |
| 11840 | |
| 11841 | if (!mProcessesReady) { |
| 11842 | // if the caller really truly claims to know what they're doing, go |
| 11843 | // ahead and allow the broadcast without launching any receivers |
| 11844 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 11845 | intent = new Intent(intent); |
| 11846 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 11847 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 11848 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 11849 | + " before boot completion"); |
| 11850 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 11851 | } |
| 11852 | } |
| 11853 | |
| 11854 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 11855 | throw new IllegalArgumentException( |
| 11856 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 11857 | } |
| 11858 | |
| 11859 | return intent; |
| 11860 | } |
| 11861 | |
| 11862 | public final int broadcastIntent(IApplicationThread caller, |
| 11863 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11864 | int resultCode, String resultData, Bundle map, |
| 11865 | String requiredPermission, boolean serialized, boolean sticky) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11866 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11867 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 11868 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11869 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11870 | final int callingPid = Binder.getCallingPid(); |
| 11871 | final int callingUid = Binder.getCallingUid(); |
| 11872 | final long origId = Binder.clearCallingIdentity(); |
| 11873 | int res = broadcastIntentLocked(callerApp, |
| 11874 | callerApp != null ? callerApp.info.packageName : null, |
| 11875 | intent, resolvedType, resultTo, |
| 11876 | resultCode, resultData, map, requiredPermission, serialized, |
| 11877 | sticky, callingPid, callingUid); |
| 11878 | Binder.restoreCallingIdentity(origId); |
| 11879 | return res; |
| 11880 | } |
| 11881 | } |
| 11882 | |
| 11883 | int broadcastIntentInPackage(String packageName, int uid, |
| 11884 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11885 | int resultCode, String resultData, Bundle map, |
| 11886 | String requiredPermission, boolean serialized, boolean sticky) { |
| 11887 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11888 | intent = verifyBroadcastLocked(intent); |
| 11889 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11890 | final long origId = Binder.clearCallingIdentity(); |
| 11891 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 11892 | resultTo, resultCode, resultData, map, requiredPermission, |
| 11893 | serialized, sticky, -1, uid); |
| 11894 | Binder.restoreCallingIdentity(origId); |
| 11895 | return res; |
| 11896 | } |
| 11897 | } |
| 11898 | |
| 11899 | public final void unbroadcastIntent(IApplicationThread caller, |
| 11900 | Intent intent) { |
| 11901 | // Refuse possible leaked file descriptors |
| 11902 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11903 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11904 | } |
| 11905 | |
| 11906 | synchronized(this) { |
| 11907 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 11908 | != PackageManager.PERMISSION_GRANTED) { |
| 11909 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 11910 | + Binder.getCallingPid() |
| 11911 | + ", uid=" + Binder.getCallingUid() |
| 11912 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11913 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11914 | throw new SecurityException(msg); |
| 11915 | } |
| 11916 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 11917 | if (list != null) { |
| 11918 | int N = list.size(); |
| 11919 | int i; |
| 11920 | for (i=0; i<N; i++) { |
| 11921 | if (intent.filterEquals(list.get(i))) { |
| 11922 | list.remove(i); |
| 11923 | break; |
| 11924 | } |
| 11925 | } |
| 11926 | } |
| 11927 | } |
| 11928 | } |
| 11929 | |
| 11930 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 11931 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 11932 | boolean explicit) { |
| 11933 | if (mOrderedBroadcasts.size() == 0) { |
| 11934 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11935 | Slog.w(TAG, "finishReceiver called but no pending broadcasts"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11936 | } |
| 11937 | return false; |
| 11938 | } |
| 11939 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 11940 | if (r.receiver == null) { |
| 11941 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11942 | Slog.w(TAG, "finishReceiver called but none active"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11943 | } |
| 11944 | return false; |
| 11945 | } |
| 11946 | if (r.receiver != receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11947 | 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] | 11948 | return false; |
| 11949 | } |
| 11950 | int state = r.state; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 11951 | r.state = BroadcastRecord.IDLE; |
| 11952 | if (state == BroadcastRecord.IDLE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11953 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11954 | Slog.w(TAG, "finishReceiver called but state is IDLE"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11955 | } |
| 11956 | } |
| 11957 | r.receiver = null; |
| 11958 | r.intent.setComponent(null); |
| 11959 | if (r.curApp != null) { |
| 11960 | r.curApp.curReceiver = null; |
| 11961 | } |
| 11962 | if (r.curFilter != null) { |
| 11963 | r.curFilter.receiverList.curBroadcast = null; |
| 11964 | } |
| 11965 | r.curFilter = null; |
| 11966 | r.curApp = null; |
| 11967 | r.curComponent = null; |
| 11968 | r.curReceiver = null; |
| 11969 | mPendingBroadcast = null; |
| 11970 | |
| 11971 | r.resultCode = resultCode; |
| 11972 | r.resultData = resultData; |
| 11973 | r.resultExtras = resultExtras; |
| 11974 | r.resultAbort = resultAbort; |
| 11975 | |
| 11976 | // We will process the next receiver right now if this is finishing |
| 11977 | // an app receiver (which is always asynchronous) or after we have |
| 11978 | // come back from calling a receiver. |
| 11979 | return state == BroadcastRecord.APP_RECEIVE |
| 11980 | || state == BroadcastRecord.CALL_DONE_RECEIVE; |
| 11981 | } |
| 11982 | |
| 11983 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 11984 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11985 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11986 | |
| 11987 | // Refuse possible leaked file descriptors |
| 11988 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 11989 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 11990 | } |
| 11991 | |
| 11992 | boolean doNext; |
| 11993 | |
| 11994 | final long origId = Binder.clearCallingIdentity(); |
| 11995 | |
| 11996 | synchronized(this) { |
| 11997 | doNext = finishReceiverLocked( |
| 11998 | who, resultCode, resultData, resultExtras, resultAbort, true); |
| 11999 | } |
| 12000 | |
| 12001 | if (doNext) { |
| 12002 | processNextBroadcast(false); |
| 12003 | } |
| 12004 | trimApplications(); |
| 12005 | |
| 12006 | Binder.restoreCallingIdentity(origId); |
| 12007 | } |
| 12008 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12009 | private final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12010 | if (r.nextReceiver > 0) { |
| 12011 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 12012 | if (curReceiver instanceof BroadcastFilter) { |
| 12013 | BroadcastFilter bf = (BroadcastFilter) curReceiver; |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12014 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12015 | System.identityHashCode(r), |
| 12016 | r.intent.getAction(), |
| 12017 | r.nextReceiver - 1, |
| 12018 | System.identityHashCode(bf)); |
| 12019 | } else { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12020 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12021 | System.identityHashCode(r), |
| 12022 | r.intent.getAction(), |
| 12023 | r.nextReceiver - 1, |
| 12024 | ((ResolveInfo)curReceiver).toString()); |
| 12025 | } |
| 12026 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12027 | 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] | 12028 | + r); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12029 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12030 | System.identityHashCode(r), |
| 12031 | r.intent.getAction(), |
| 12032 | r.nextReceiver, |
| 12033 | "NONE"); |
| 12034 | } |
| 12035 | } |
| 12036 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12037 | private final void setBroadcastTimeoutLocked(long timeoutTime) { |
| 12038 | if (! mPendingBroadcastTimeoutMessage) { |
| 12039 | Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG); |
| 12040 | mHandler.sendMessageAtTime(msg, timeoutTime); |
| 12041 | mPendingBroadcastTimeoutMessage = true; |
| 12042 | } |
| 12043 | } |
| 12044 | |
| 12045 | private final void cancelBroadcastTimeoutLocked() { |
| 12046 | if (mPendingBroadcastTimeoutMessage) { |
| 12047 | mHandler.removeMessages(BROADCAST_TIMEOUT_MSG); |
| 12048 | mPendingBroadcastTimeoutMessage = false; |
| 12049 | } |
| 12050 | } |
| 12051 | |
| 12052 | private final void broadcastTimeoutLocked(boolean fromMsg) { |
| 12053 | if (fromMsg) { |
| 12054 | mPendingBroadcastTimeoutMessage = false; |
| 12055 | } |
| 12056 | |
| 12057 | if (mOrderedBroadcasts.size() == 0) { |
| 12058 | return; |
| 12059 | } |
| 12060 | |
| 12061 | long now = SystemClock.uptimeMillis(); |
| 12062 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 12063 | if (fromMsg) { |
| 12064 | if (mDidDexOpt) { |
| 12065 | // Delay timeouts until dexopt finishes. |
| 12066 | mDidDexOpt = false; |
| 12067 | long timeoutTime = SystemClock.uptimeMillis() + BROADCAST_TIMEOUT; |
| 12068 | setBroadcastTimeoutLocked(timeoutTime); |
| 12069 | return; |
| 12070 | } |
| 12071 | if (! mProcessesReady) { |
| 12072 | // Only process broadcast timeouts if the system is ready. That way |
| 12073 | // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended |
| 12074 | // to do heavy lifting for system up. |
| 12075 | return; |
| 12076 | } |
| 12077 | |
| 12078 | long timeoutTime = r.receiverTime + BROADCAST_TIMEOUT; |
| 12079 | if (timeoutTime > now) { |
| 12080 | // We can observe premature timeouts because we do not cancel and reset the |
| 12081 | // broadcast timeout message after each receiver finishes. Instead, we set up |
| 12082 | // an initial timeout then kick it down the road a little further as needed |
| 12083 | // when it expires. |
| 12084 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12085 | "Premature timeout @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for " |
| 12086 | + timeoutTime); |
| 12087 | setBroadcastTimeoutLocked(timeoutTime); |
| 12088 | return; |
| 12089 | } |
| 12090 | } |
| 12091 | |
| 12092 | Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r.receiver |
| 12093 | + ", started " + (now - r.receiverTime) + "ms ago"); |
| 12094 | r.receiverTime = now; |
| 12095 | r.anrCount++; |
| 12096 | |
| 12097 | // Current receiver has passed its expiration date. |
| 12098 | if (r.nextReceiver <= 0) { |
| 12099 | Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0"); |
| 12100 | return; |
| 12101 | } |
| 12102 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12103 | ProcessRecord app = null; |
| 12104 | String anrMessage = null; |
| 12105 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12106 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 12107 | Slog.w(TAG, "Receiver during timeout: " + curReceiver); |
| 12108 | logBroadcastReceiverDiscardLocked(r); |
| 12109 | if (curReceiver instanceof BroadcastFilter) { |
| 12110 | BroadcastFilter bf = (BroadcastFilter)curReceiver; |
| 12111 | if (bf.receiverList.pid != 0 |
| 12112 | && bf.receiverList.pid != MY_PID) { |
| 12113 | synchronized (this.mPidsSelfLocked) { |
| 12114 | app = this.mPidsSelfLocked.get( |
| 12115 | bf.receiverList.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12116 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12117 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12118 | } else { |
| 12119 | app = r.curApp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12120 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12121 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12122 | if (app != null) { |
| 12123 | anrMessage = "Broadcast of " + r.intent.toString(); |
| 12124 | } |
| 12125 | |
| 12126 | if (mPendingBroadcast == r) { |
| 12127 | mPendingBroadcast = null; |
| 12128 | } |
| 12129 | |
| 12130 | // Move on to the next receiver. |
| 12131 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 12132 | r.resultExtras, r.resultAbort, true); |
| 12133 | scheduleBroadcastsLocked(); |
| 12134 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12135 | if (anrMessage != null) { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12136 | // Post the ANR to the handler since we do not want to process ANRs while |
| 12137 | // potentially holding our lock. |
| 12138 | mHandler.post(new AppNotResponding(app, anrMessage)); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12139 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12140 | } |
| 12141 | |
| 12142 | private final void processCurBroadcastLocked(BroadcastRecord r, |
| 12143 | ProcessRecord app) throws RemoteException { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12144 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12145 | "Process cur broadcast " + r + " for app " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12146 | if (app.thread == null) { |
| 12147 | throw new RemoteException(); |
| 12148 | } |
| 12149 | r.receiver = app.thread.asBinder(); |
| 12150 | r.curApp = app; |
| 12151 | app.curReceiver = r; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12152 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12153 | |
| 12154 | // Tell the application to launch this receiver. |
| 12155 | r.intent.setComponent(r.curComponent); |
| 12156 | |
| 12157 | boolean started = false; |
| 12158 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12159 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12160 | "Delivering to component " + r.curComponent |
| 12161 | + ": " + r); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 12162 | ensurePackageDexOpt(r.intent.getComponent().getPackageName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12163 | app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver, |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 12164 | compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12165 | r.resultCode, r.resultData, r.resultExtras, r.ordered); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12166 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12167 | "Process cur broadcast " + r + " DELIVERED for app " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12168 | started = true; |
| 12169 | } finally { |
| 12170 | if (!started) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12171 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12172 | "Process cur broadcast " + r + ": NOT STARTED!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12173 | r.receiver = null; |
| 12174 | r.curApp = null; |
| 12175 | app.curReceiver = null; |
| 12176 | } |
| 12177 | } |
| 12178 | |
| 12179 | } |
| 12180 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12181 | static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 12182 | Intent intent, int resultCode, String data, Bundle extras, |
| 12183 | boolean ordered, boolean sticky) throws RemoteException { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12184 | // 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] | 12185 | if (app != null && app.thread != null) { |
| 12186 | // If we have an app thread, do the call through that so it is |
| 12187 | // correctly ordered with other one-way calls. |
| 12188 | app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 12189 | data, extras, ordered, sticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12190 | } else { |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 12191 | receiver.performReceive(intent, resultCode, data, extras, ordered, sticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12192 | } |
| 12193 | } |
| 12194 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12195 | private final void deliverToRegisteredReceiverLocked(BroadcastRecord r, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12196 | BroadcastFilter filter, boolean ordered) { |
| 12197 | boolean skip = false; |
| 12198 | if (filter.requiredPermission != null) { |
| 12199 | int perm = checkComponentPermission(filter.requiredPermission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12200 | r.callingPid, r.callingUid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12201 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12202 | Slog.w(TAG, "Permission Denial: broadcasting " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12203 | + r.intent.toString() |
| 12204 | + " from " + r.callerPackage + " (pid=" |
| 12205 | + r.callingPid + ", uid=" + r.callingUid + ")" |
| 12206 | + " requires " + filter.requiredPermission |
| 12207 | + " due to registered receiver " + filter); |
| 12208 | skip = true; |
| 12209 | } |
| 12210 | } |
| 12211 | if (r.requiredPermission != null) { |
| 12212 | int perm = checkComponentPermission(r.requiredPermission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12213 | filter.receiverList.pid, filter.receiverList.uid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12214 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12215 | Slog.w(TAG, "Permission Denial: receiving " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12216 | + r.intent.toString() |
| 12217 | + " to " + filter.receiverList.app |
| 12218 | + " (pid=" + filter.receiverList.pid |
| 12219 | + ", uid=" + filter.receiverList.uid + ")" |
| 12220 | + " requires " + r.requiredPermission |
| 12221 | + " due to sender " + r.callerPackage |
| 12222 | + " (uid " + r.callingUid + ")"); |
| 12223 | skip = true; |
| 12224 | } |
| 12225 | } |
| 12226 | |
| 12227 | if (!skip) { |
| 12228 | // If this is not being sent as an ordered broadcast, then we |
| 12229 | // don't want to touch the fields that keep track of the current |
| 12230 | // state of ordered broadcasts. |
| 12231 | if (ordered) { |
| 12232 | r.receiver = filter.receiverList.receiver.asBinder(); |
| 12233 | r.curFilter = filter; |
| 12234 | filter.receiverList.curBroadcast = r; |
| 12235 | r.state = BroadcastRecord.CALL_IN_RECEIVE; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12236 | if (filter.receiverList.app != null) { |
| 12237 | // Bump hosting application to no longer be in background |
| 12238 | // scheduling class. Note that we can't do that if there |
| 12239 | // isn't an app... but we can only be in that case for |
| 12240 | // things that directly call the IActivityManager API, which |
| 12241 | // are already core system stuff so don't matter for this. |
| 12242 | r.curApp = filter.receiverList.app; |
| 12243 | filter.receiverList.app.curReceiver = r; |
| 12244 | updateOomAdjLocked(); |
| 12245 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12246 | } |
| 12247 | try { |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12248 | if (DEBUG_BROADCAST_LIGHT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12249 | int seq = r.intent.getIntExtra("seq", -1); |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 12250 | Slog.i(TAG, "Delivering to " + filter |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12251 | + " (seq=" + seq + "): " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12252 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12253 | performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12254 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12255 | r.resultData, r.resultExtras, r.ordered, r.initialSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12256 | if (ordered) { |
| 12257 | r.state = BroadcastRecord.CALL_DONE_RECEIVE; |
| 12258 | } |
| 12259 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12260 | Slog.w(TAG, "Failure sending broadcast " + r.intent, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12261 | if (ordered) { |
| 12262 | r.receiver = null; |
| 12263 | r.curFilter = null; |
| 12264 | filter.receiverList.curBroadcast = null; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12265 | if (filter.receiverList.app != null) { |
| 12266 | filter.receiverList.app.curReceiver = null; |
| 12267 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12268 | } |
| 12269 | } |
| 12270 | } |
| 12271 | } |
| 12272 | |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12273 | private final void addBroadcastToHistoryLocked(BroadcastRecord r) { |
| 12274 | if (r.callingUid < 0) { |
| 12275 | // This was from a registerReceiver() call; ignore it. |
| 12276 | return; |
| 12277 | } |
| 12278 | System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1, |
| 12279 | MAX_BROADCAST_HISTORY-1); |
| 12280 | r.finishTime = SystemClock.uptimeMillis(); |
| 12281 | mBroadcastHistory[0] = r; |
| 12282 | } |
| 12283 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12284 | private final void processNextBroadcast(boolean fromMsg) { |
| 12285 | synchronized(this) { |
| 12286 | BroadcastRecord r; |
| 12287 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12288 | if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12289 | + mParallelBroadcasts.size() + " broadcasts, " |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 12290 | + mOrderedBroadcasts.size() + " ordered broadcasts"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12291 | |
| 12292 | updateCpuStats(); |
| 12293 | |
| 12294 | if (fromMsg) { |
| 12295 | mBroadcastsScheduled = false; |
| 12296 | } |
| 12297 | |
| 12298 | // First, deliver any non-serialized broadcasts right away. |
| 12299 | while (mParallelBroadcasts.size() > 0) { |
| 12300 | r = mParallelBroadcasts.remove(0); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12301 | r.dispatchTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 12302 | r.dispatchClockTime = System.currentTimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12303 | final int N = r.receivers.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12304 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12305 | + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12306 | for (int i=0; i<N; i++) { |
| 12307 | Object target = r.receivers.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12308 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 12309 | "Delivering non-ordered to registered " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12310 | + target + ": " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12311 | deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12312 | } |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12313 | addBroadcastToHistoryLocked(r); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12314 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12315 | + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12316 | } |
| 12317 | |
| 12318 | // Now take care of the next serialized one... |
| 12319 | |
| 12320 | // If we are waiting for a process to come up to handle the next |
| 12321 | // broadcast, then do nothing at this point. Just in case, we |
| 12322 | // check that the process we're waiting for still exists. |
| 12323 | if (mPendingBroadcast != null) { |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 12324 | if (DEBUG_BROADCAST_LIGHT) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12325 | Slog.v(TAG, "processNextBroadcast: waiting for " |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 12326 | + mPendingBroadcast.curApp); |
| 12327 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12328 | |
| 12329 | boolean isDead; |
| 12330 | synchronized (mPidsSelfLocked) { |
| 12331 | isDead = (mPidsSelfLocked.get(mPendingBroadcast.curApp.pid) == null); |
| 12332 | } |
| 12333 | if (!isDead) { |
| 12334 | // It's still alive, so keep waiting |
| 12335 | return; |
| 12336 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12337 | Slog.w(TAG, "pending app " + mPendingBroadcast.curApp |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12338 | + " died before responding to broadcast"); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12339 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 12340 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12341 | mPendingBroadcast = null; |
| 12342 | } |
| 12343 | } |
| 12344 | |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12345 | boolean looped = false; |
| 12346 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12347 | do { |
| 12348 | if (mOrderedBroadcasts.size() == 0) { |
| 12349 | // No more broadcasts pending, so all done! |
| 12350 | scheduleAppGcsLocked(); |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12351 | if (looped) { |
| 12352 | // If we had finished the last ordered broadcast, then |
| 12353 | // make sure all processes have correct oom and sched |
| 12354 | // adjustments. |
| 12355 | updateOomAdjLocked(); |
| 12356 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12357 | return; |
| 12358 | } |
| 12359 | r = mOrderedBroadcasts.get(0); |
| 12360 | boolean forceReceive = false; |
| 12361 | |
| 12362 | // Ensure that even if something goes awry with the timeout |
| 12363 | // detection, we catch "hung" broadcasts here, discard them, |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 12364 | // and continue to make progress. |
| 12365 | // |
| 12366 | // 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] | 12367 | // receivers don't get executed with timeouts. They're intended for |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 12368 | // one time heavy lifting after system upgrades and can take |
| 12369 | // significant amounts of time. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12370 | int numReceivers = (r.receivers != null) ? r.receivers.size() : 0; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12371 | if (mProcessesReady && r.dispatchTime > 0) { |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 12372 | long now = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12373 | if ((numReceivers > 0) && |
| 12374 | (now > r.dispatchTime + (2*BROADCAST_TIMEOUT*numReceivers))) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12375 | Slog.w(TAG, "Hung broadcast discarded after timeout failure:" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12376 | + " now=" + now |
| 12377 | + " dispatchTime=" + r.dispatchTime |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12378 | + " startTime=" + r.receiverTime |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12379 | + " intent=" + r.intent |
| 12380 | + " numReceivers=" + numReceivers |
| 12381 | + " nextReceiver=" + r.nextReceiver |
| 12382 | + " state=" + r.state); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12383 | broadcastTimeoutLocked(false); // forcibly finish this broadcast |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12384 | forceReceive = true; |
| 12385 | r.state = BroadcastRecord.IDLE; |
| 12386 | } |
| 12387 | } |
| 12388 | |
| 12389 | if (r.state != BroadcastRecord.IDLE) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12390 | if (DEBUG_BROADCAST) Slog.d(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12391 | "processNextBroadcast() called when not idle (state=" |
| 12392 | + r.state + ")"); |
| 12393 | return; |
| 12394 | } |
| 12395 | |
| 12396 | if (r.receivers == null || r.nextReceiver >= numReceivers |
| 12397 | || r.resultAbort || forceReceive) { |
| 12398 | // No more receivers for this broadcast! Send the final |
| 12399 | // result if requested... |
| 12400 | if (r.resultTo != null) { |
| 12401 | try { |
| 12402 | if (DEBUG_BROADCAST) { |
| 12403 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12404 | Slog.i(TAG, "Finishing broadcast " + r.intent.getAction() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12405 | + " seq=" + seq + " app=" + r.callerApp); |
| 12406 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12407 | performReceiveLocked(r.callerApp, r.resultTo, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12408 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 12409 | r.resultData, r.resultExtras, false, false); |
Johannes Carlsson | b5a8654 | 2010-10-27 10:08:10 +0200 | [diff] [blame] | 12410 | // Set this to null so that the reference |
| 12411 | // (local and remote) isnt kept in the mBroadcastHistory. |
| 12412 | r.resultTo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12413 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12414 | 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] | 12415 | } |
| 12416 | } |
| 12417 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12418 | if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG"); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12419 | cancelBroadcastTimeoutLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12420 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12421 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12422 | + r); |
| 12423 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12424 | // ... and on to the next... |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12425 | addBroadcastToHistoryLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12426 | mOrderedBroadcasts.remove(0); |
| 12427 | r = null; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12428 | looped = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12429 | continue; |
| 12430 | } |
| 12431 | } while (r == null); |
| 12432 | |
| 12433 | // Get the next receiver... |
| 12434 | int recIdx = r.nextReceiver++; |
| 12435 | |
| 12436 | // Keep track of when this receiver started, and make sure there |
| 12437 | // is a timeout message pending to kill it if need be. |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12438 | r.receiverTime = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12439 | if (recIdx == 0) { |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12440 | r.dispatchTime = r.receiverTime; |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 12441 | r.dispatchClockTime = System.currentTimeMillis(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12442 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12443 | + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12444 | } |
| 12445 | if (! mPendingBroadcastTimeoutMessage) { |
| 12446 | long timeoutTime = r.receiverTime + BROADCAST_TIMEOUT; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12447 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12448 | "Submitting BROADCAST_TIMEOUT_MSG for " + r + " at " + timeoutTime); |
| 12449 | setBroadcastTimeoutLocked(timeoutTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12450 | } |
| 12451 | |
| 12452 | Object nextReceiver = r.receivers.get(recIdx); |
| 12453 | if (nextReceiver instanceof BroadcastFilter) { |
| 12454 | // Simple case: this is a registered receiver who gets |
| 12455 | // a direct call. |
| 12456 | BroadcastFilter filter = (BroadcastFilter)nextReceiver; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12457 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 12458 | "Delivering ordered to registered " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12459 | + filter + ": " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12460 | deliverToRegisteredReceiverLocked(r, filter, r.ordered); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12461 | if (r.receiver == null || !r.ordered) { |
| 12462 | // The receiver has already finished, so schedule to |
| 12463 | // process the next one. |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 12464 | if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing: ordered=" |
| 12465 | + r.ordered + " receiver=" + r.receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12466 | r.state = BroadcastRecord.IDLE; |
| 12467 | scheduleBroadcastsLocked(); |
| 12468 | } |
| 12469 | return; |
| 12470 | } |
| 12471 | |
| 12472 | // Hard case: need to instantiate the receiver, possibly |
| 12473 | // starting its application process to host it. |
| 12474 | |
| 12475 | ResolveInfo info = |
| 12476 | (ResolveInfo)nextReceiver; |
| 12477 | |
| 12478 | boolean skip = false; |
| 12479 | int perm = checkComponentPermission(info.activityInfo.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12480 | r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid, |
| 12481 | info.activityInfo.exported); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12482 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12483 | if (!info.activityInfo.exported) { |
| 12484 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 12485 | + r.intent.toString() |
| 12486 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 12487 | + ", uid=" + r.callingUid + ")" |
| 12488 | + " is not exported from uid " + info.activityInfo.applicationInfo.uid |
| 12489 | + " due to receiver " + info.activityInfo.packageName |
| 12490 | + "/" + info.activityInfo.name); |
| 12491 | } else { |
| 12492 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 12493 | + r.intent.toString() |
| 12494 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 12495 | + ", uid=" + r.callingUid + ")" |
| 12496 | + " requires " + info.activityInfo.permission |
| 12497 | + " due to receiver " + info.activityInfo.packageName |
| 12498 | + "/" + info.activityInfo.name); |
| 12499 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12500 | skip = true; |
| 12501 | } |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 12502 | if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID && |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12503 | r.requiredPermission != null) { |
| 12504 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12505 | perm = AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12506 | checkPermission(r.requiredPermission, |
| 12507 | info.activityInfo.applicationInfo.packageName); |
| 12508 | } catch (RemoteException e) { |
| 12509 | perm = PackageManager.PERMISSION_DENIED; |
| 12510 | } |
| 12511 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12512 | Slog.w(TAG, "Permission Denial: receiving " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12513 | + r.intent + " to " |
| 12514 | + info.activityInfo.applicationInfo.packageName |
| 12515 | + " requires " + r.requiredPermission |
| 12516 | + " due to sender " + r.callerPackage |
| 12517 | + " (uid " + r.callingUid + ")"); |
| 12518 | skip = true; |
| 12519 | } |
| 12520 | } |
| 12521 | if (r.curApp != null && r.curApp.crashing) { |
| 12522 | // If the target process is crashing, just skip it. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12523 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12524 | "Skipping deliver ordered " + r + " to " + r.curApp |
| 12525 | + ": process crashing"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12526 | skip = true; |
| 12527 | } |
| 12528 | |
| 12529 | if (skip) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12530 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12531 | "Skipping delivery of ordered " + r + " for whatever reason"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12532 | r.receiver = null; |
| 12533 | r.curFilter = null; |
| 12534 | r.state = BroadcastRecord.IDLE; |
| 12535 | scheduleBroadcastsLocked(); |
| 12536 | return; |
| 12537 | } |
| 12538 | |
| 12539 | r.state = BroadcastRecord.APP_RECEIVE; |
| 12540 | String targetProcess = info.activityInfo.processName; |
| 12541 | r.curComponent = new ComponentName( |
| 12542 | info.activityInfo.applicationInfo.packageName, |
| 12543 | info.activityInfo.name); |
| 12544 | r.curReceiver = info.activityInfo; |
| 12545 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 12546 | // Broadcast is being executed, its package can't be stopped. |
| 12547 | try { |
| 12548 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 12549 | r.curComponent.getPackageName(), false); |
| 12550 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 12551 | } catch (IllegalArgumentException e) { |
| 12552 | Slog.w(TAG, "Failed trying to unstop package " |
| 12553 | + r.curComponent.getPackageName() + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 12554 | } |
| 12555 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12556 | // Is this receiver's application already running? |
| 12557 | ProcessRecord app = getProcessRecordLocked(targetProcess, |
| 12558 | info.activityInfo.applicationInfo.uid); |
| 12559 | if (app != null && app.thread != null) { |
| 12560 | try { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 12561 | app.addPackage(info.activityInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12562 | processCurBroadcastLocked(r, app); |
| 12563 | return; |
| 12564 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12565 | Slog.w(TAG, "Exception when sending broadcast to " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12566 | + r.curComponent, e); |
| 12567 | } |
| 12568 | |
| 12569 | // If a dead object exception was thrown -- fall through to |
| 12570 | // restart the application. |
| 12571 | } |
| 12572 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 12573 | // 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] | 12574 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12575 | "Need to start app " + targetProcess + " for broadcast " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12576 | if ((r.curApp=startProcessLocked(targetProcess, |
| 12577 | info.activityInfo.applicationInfo, true, |
| 12578 | r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 12579 | "broadcast", r.curComponent, |
| 12580 | (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0)) |
| 12581 | == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12582 | // Ah, this recipient is unavailable. Finish it if necessary, |
| 12583 | // and mark the broadcast record as ready for the next. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12584 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12585 | + info.activityInfo.applicationInfo.packageName + "/" |
| 12586 | + info.activityInfo.applicationInfo.uid + " for broadcast " |
| 12587 | + r.intent + ": process is bad"); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12588 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12589 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 12590 | r.resultExtras, r.resultAbort, true); |
| 12591 | scheduleBroadcastsLocked(); |
| 12592 | r.state = BroadcastRecord.IDLE; |
| 12593 | return; |
| 12594 | } |
| 12595 | |
| 12596 | mPendingBroadcast = r; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12597 | mPendingBroadcastRecvIndex = recIdx; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12598 | } |
| 12599 | } |
| 12600 | |
| 12601 | // ========================================================= |
| 12602 | // INSTRUMENTATION |
| 12603 | // ========================================================= |
| 12604 | |
| 12605 | public boolean startInstrumentation(ComponentName className, |
| 12606 | String profileFile, int flags, Bundle arguments, |
| 12607 | IInstrumentationWatcher watcher) { |
| 12608 | // Refuse possible leaked file descriptors |
| 12609 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 12610 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12611 | } |
| 12612 | |
| 12613 | synchronized(this) { |
| 12614 | InstrumentationInfo ii = null; |
| 12615 | ApplicationInfo ai = null; |
| 12616 | try { |
| 12617 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12618 | className, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12619 | ai = mContext.getPackageManager().getApplicationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12620 | ii.targetPackage, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12621 | } catch (PackageManager.NameNotFoundException e) { |
| 12622 | } |
| 12623 | if (ii == null) { |
| 12624 | reportStartInstrumentationFailure(watcher, className, |
| 12625 | "Unable to find instrumentation info for: " + className); |
| 12626 | return false; |
| 12627 | } |
| 12628 | if (ai == null) { |
| 12629 | reportStartInstrumentationFailure(watcher, className, |
| 12630 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 12631 | return false; |
| 12632 | } |
| 12633 | |
| 12634 | int match = mContext.getPackageManager().checkSignatures( |
| 12635 | ii.targetPackage, ii.packageName); |
| 12636 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 12637 | String msg = "Permission Denial: starting instrumentation " |
| 12638 | + className + " from pid=" |
| 12639 | + Binder.getCallingPid() |
| 12640 | + ", uid=" + Binder.getCallingPid() |
| 12641 | + " not allowed because package " + ii.packageName |
| 12642 | + " does not have a signature matching the target " |
| 12643 | + ii.targetPackage; |
| 12644 | reportStartInstrumentationFailure(watcher, className, msg); |
| 12645 | throw new SecurityException(msg); |
| 12646 | } |
| 12647 | |
| 12648 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12649 | // Instrumentation can kill and relaunch even persistent processes |
| 12650 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12651 | ProcessRecord app = addAppLocked(ai); |
| 12652 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12653 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12654 | app.instrumentationProfileFile = profileFile; |
| 12655 | app.instrumentationArguments = arguments; |
| 12656 | app.instrumentationWatcher = watcher; |
| 12657 | app.instrumentationResultClass = className; |
| 12658 | Binder.restoreCallingIdentity(origId); |
| 12659 | } |
| 12660 | |
| 12661 | return true; |
| 12662 | } |
| 12663 | |
| 12664 | /** |
| 12665 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 12666 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 12667 | * the "am" command to report errors with more information. |
| 12668 | * |
| 12669 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 12670 | * @param cn The component name of the instrumentation. |
| 12671 | * @param report The error report. |
| 12672 | */ |
| 12673 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 12674 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12675 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12676 | try { |
| 12677 | if (watcher != null) { |
| 12678 | Bundle results = new Bundle(); |
| 12679 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 12680 | results.putString("Error", report); |
| 12681 | watcher.instrumentationStatus(cn, -1, results); |
| 12682 | } |
| 12683 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12684 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12685 | } |
| 12686 | } |
| 12687 | |
| 12688 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 12689 | if (app.instrumentationWatcher != null) { |
| 12690 | try { |
| 12691 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 12692 | app.instrumentationWatcher.instrumentationFinished( |
| 12693 | app.instrumentationClass, |
| 12694 | resultCode, |
| 12695 | results); |
| 12696 | } catch (RemoteException e) { |
| 12697 | } |
| 12698 | } |
| 12699 | app.instrumentationWatcher = null; |
| 12700 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12701 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12702 | app.instrumentationProfileFile = null; |
| 12703 | app.instrumentationArguments = null; |
| 12704 | |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12705 | forceStopPackageLocked(app.processName, -1, false, false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12706 | } |
| 12707 | |
| 12708 | public void finishInstrumentation(IApplicationThread target, |
| 12709 | int resultCode, Bundle results) { |
| 12710 | // Refuse possible leaked file descriptors |
| 12711 | if (results != null && results.hasFileDescriptors()) { |
| 12712 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12713 | } |
| 12714 | |
| 12715 | synchronized(this) { |
| 12716 | ProcessRecord app = getRecordForAppLocked(target); |
| 12717 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12718 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12719 | return; |
| 12720 | } |
| 12721 | final long origId = Binder.clearCallingIdentity(); |
| 12722 | finishInstrumentationLocked(app, resultCode, results); |
| 12723 | Binder.restoreCallingIdentity(origId); |
| 12724 | } |
| 12725 | } |
| 12726 | |
| 12727 | // ========================================================= |
| 12728 | // CONFIGURATION |
| 12729 | // ========================================================= |
| 12730 | |
| 12731 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 12732 | ConfigurationInfo config = new ConfigurationInfo(); |
| 12733 | synchronized (this) { |
| 12734 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 12735 | config.reqKeyboardType = mConfiguration.keyboard; |
| 12736 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12737 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 12738 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12739 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 12740 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12741 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 12742 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12743 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 12744 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 12745 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12746 | } |
| 12747 | return config; |
| 12748 | } |
| 12749 | |
| 12750 | public Configuration getConfiguration() { |
| 12751 | Configuration ci; |
| 12752 | synchronized(this) { |
| 12753 | ci = new Configuration(mConfiguration); |
| 12754 | } |
| 12755 | return ci; |
| 12756 | } |
| 12757 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12758 | public void updatePersistentConfiguration(Configuration values) { |
| 12759 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12760 | "updateConfiguration()"); |
| 12761 | enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS, |
| 12762 | "updateConfiguration()"); |
| 12763 | if (values == null) { |
| 12764 | throw new NullPointerException("Configuration must not be null"); |
| 12765 | } |
| 12766 | |
| 12767 | synchronized(this) { |
| 12768 | final long origId = Binder.clearCallingIdentity(); |
| 12769 | updateConfigurationLocked(values, null, true); |
| 12770 | Binder.restoreCallingIdentity(origId); |
| 12771 | } |
| 12772 | } |
| 12773 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12774 | public void updateConfiguration(Configuration values) { |
| 12775 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12776 | "updateConfiguration()"); |
| 12777 | |
| 12778 | synchronized(this) { |
| 12779 | if (values == null && mWindowManager != null) { |
| 12780 | // sentinel: fetch the current configuration from the window manager |
| 12781 | values = mWindowManager.computeNewConfiguration(); |
| 12782 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12783 | |
| 12784 | if (mWindowManager != null) { |
| 12785 | mProcessList.applyDisplaySize(mWindowManager); |
| 12786 | } |
| 12787 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12788 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12789 | if (values != null) { |
| 12790 | Settings.System.clearConfiguration(values); |
| 12791 | } |
| 12792 | updateConfigurationLocked(values, null, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12793 | Binder.restoreCallingIdentity(origId); |
| 12794 | } |
| 12795 | } |
| 12796 | |
| 12797 | /** |
| 12798 | * Do either or both things: (1) change the current configuration, and (2) |
| 12799 | * make sure the given activity is running with the (now) current |
| 12800 | * configuration. Returns true if the activity has been left running, or |
| 12801 | * false if <var>starting</var> is being destroyed to match the new |
| 12802 | * configuration. |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12803 | * @param persistent TODO |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12804 | */ |
| 12805 | public boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12806 | ActivityRecord starting, boolean persistent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12807 | int changes = 0; |
| 12808 | |
| 12809 | boolean kept = true; |
| 12810 | |
| 12811 | if (values != null) { |
| 12812 | Configuration newConfig = new Configuration(mConfiguration); |
| 12813 | changes = newConfig.updateFrom(values); |
| 12814 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12815 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12816 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12817 | } |
| 12818 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12819 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12820 | |
| 12821 | if (values.locale != null) { |
| 12822 | saveLocaleLocked(values.locale, |
| 12823 | !values.locale.equals(mConfiguration.locale), |
| 12824 | values.userSetLocale); |
| 12825 | } |
| 12826 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12827 | mConfigurationSeq++; |
| 12828 | if (mConfigurationSeq <= 0) { |
| 12829 | mConfigurationSeq = 1; |
| 12830 | } |
| 12831 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12832 | mConfiguration = newConfig; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12833 | Slog.i(TAG, "Config changed: " + newConfig); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12834 | |
| 12835 | AttributeCache ac = AttributeCache.instance(); |
| 12836 | if (ac != null) { |
| 12837 | ac.updateConfiguration(mConfiguration); |
| 12838 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12839 | |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12840 | // Make sure all resources in our process are updated |
| 12841 | // right now, so that anyone who is going to retrieve |
| 12842 | // resource values after we return will be sure to get |
| 12843 | // the new ones. This is especially important during |
| 12844 | // boot, where the first config change needs to guarantee |
| 12845 | // all resources have that config before following boot |
| 12846 | // code is executed. |
| 12847 | mSystemThread.applyConfigurationToResources(newConfig); |
| 12848 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12849 | if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12850 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
| 12851 | msg.obj = new Configuration(mConfiguration); |
| 12852 | mHandler.sendMessage(msg); |
| 12853 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12854 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12855 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 12856 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12857 | try { |
| 12858 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12859 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12860 | + app.processName + " new config " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12861 | app.thread.scheduleConfigurationChanged(mConfiguration); |
| 12862 | } |
| 12863 | } catch (Exception e) { |
| 12864 | } |
| 12865 | } |
| 12866 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12867 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 12868 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12869 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
| 12870 | null, false, false, MY_PID, Process.SYSTEM_UID); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12871 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
| 12872 | broadcastIntentLocked(null, null, |
| 12873 | new Intent(Intent.ACTION_LOCALE_CHANGED), |
| 12874 | null, null, 0, null, null, |
| 12875 | null, false, false, MY_PID, Process.SYSTEM_UID); |
| 12876 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12877 | } |
| 12878 | } |
| 12879 | |
| 12880 | if (changes != 0 && starting == null) { |
| 12881 | // If the configuration changed, and the caller is not already |
| 12882 | // in the process of starting an activity, then find the top |
| 12883 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12884 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12885 | } |
| 12886 | |
| 12887 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12888 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 12889 | // And we need to make sure at this point that all other activities |
| 12890 | // are made visible with the correct configuration. |
| 12891 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12892 | } |
| 12893 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12894 | if (values != null && mWindowManager != null) { |
| 12895 | mWindowManager.setNewConfiguration(mConfiguration); |
| 12896 | } |
| 12897 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12898 | return kept; |
| 12899 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12900 | |
| 12901 | /** |
| 12902 | * Save the locale. You must be inside a synchronized (this) block. |
| 12903 | */ |
| 12904 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 12905 | if(isDiff) { |
| 12906 | SystemProperties.set("user.language", l.getLanguage()); |
| 12907 | SystemProperties.set("user.region", l.getCountry()); |
| 12908 | } |
| 12909 | |
| 12910 | if(isPersist) { |
| 12911 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 12912 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 12913 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 12914 | } |
| 12915 | } |
| 12916 | |
| 12917 | // ========================================================= |
| 12918 | // LIFETIME MANAGEMENT |
| 12919 | // ========================================================= |
| 12920 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12921 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, |
| 12922 | ProcessRecord TOP_APP, boolean recursed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12923 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12924 | // This adjustment has already been computed. If we are calling |
| 12925 | // from the top, we may have already computed our adjustment with |
| 12926 | // an earlier hidden adjustment that isn't really for us... if |
| 12927 | // so, use the new hidden adjustment. |
| 12928 | if (!recursed && app.hidden) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12929 | app.curAdj = app.curRawAdj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12930 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12931 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12932 | } |
| 12933 | |
| 12934 | if (app.thread == null) { |
| 12935 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12936 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12937 | return (app.curAdj=ProcessList.EMPTY_APP_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12938 | } |
| 12939 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12940 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
| 12941 | app.adjSource = null; |
| 12942 | app.adjTarget = null; |
| 12943 | app.empty = false; |
| 12944 | app.hidden = false; |
| 12945 | |
| 12946 | final int activitiesSize = app.activities.size(); |
| 12947 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12948 | if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12949 | // The max adjustment doesn't allow this app to be anything |
| 12950 | // below foreground, so it is not worth doing work for it. |
| 12951 | app.adjType = "fixed"; |
| 12952 | app.adjSeq = mAdjSeq; |
| 12953 | app.curRawAdj = app.maxAdj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12954 | app.foregroundActivities = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12955 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12956 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12957 | // System process can do UI, and when they do we want to have |
| 12958 | // them trim their memory after the user leaves the UI. To |
| 12959 | // facilitate this, here we need to determine whether or not it |
| 12960 | // is currently showing UI. |
| 12961 | app.systemNoUi = true; |
| 12962 | if (app == TOP_APP) { |
| 12963 | app.systemNoUi = false; |
| 12964 | } else if (activitiesSize > 0) { |
| 12965 | for (int j = 0; j < activitiesSize; j++) { |
| 12966 | final ActivityRecord r = app.activities.get(j); |
| 12967 | if (r.visible) { |
| 12968 | app.systemNoUi = false; |
| 12969 | break; |
| 12970 | } |
| 12971 | } |
| 12972 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12973 | return (app.curAdj=app.maxAdj); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12974 | } |
| 12975 | |
| 12976 | final boolean hadForegroundActivities = app.foregroundActivities; |
| 12977 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12978 | app.foregroundActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12979 | app.keeping = false; |
| 12980 | app.systemNoUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12981 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12982 | // Determine the importance of the process, starting with most |
| 12983 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12984 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12985 | int schedGroup; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12986 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12987 | // The last app on the list is the foreground app. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12988 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12989 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12990 | app.adjType = "top-activity"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12991 | app.foregroundActivities = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12992 | } else if (app.instrumentationClass != null) { |
| 12993 | // Don't want to kill running instrumentation. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12994 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12995 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12996 | app.adjType = "instrumentation"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12997 | } else if (app.curReceiver != null || |
| 12998 | (mPendingBroadcast != null && mPendingBroadcast.curApp == app)) { |
| 12999 | // An app that is currently receiving a broadcast also |
| 13000 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13001 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13002 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13003 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13004 | } else if (app.executingServices.size() > 0) { |
| 13005 | // An app that is currently executing a service callback also |
| 13006 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13007 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13008 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13009 | app.adjType = "exec-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13010 | } else if (activitiesSize > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13011 | // This app is in the background with paused activities. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13012 | // We inspect activities to potentially upgrade adjustment further below. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13013 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13014 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13015 | app.hidden = true; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13016 | app.adjType = "bg-activities"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13017 | } else { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13018 | // A very not-needed process. If this is lower in the lru list, |
| 13019 | // we will push it in to the empty bucket. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13020 | adj = hiddenAdj; |
| 13021 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13022 | app.hidden = true; |
| 13023 | app.empty = true; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13024 | app.adjType = "bg-empty"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13025 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13026 | |
| 13027 | // Examine all activities if not already foreground. |
| 13028 | if (!app.foregroundActivities && activitiesSize > 0) { |
| 13029 | for (int j = 0; j < activitiesSize; j++) { |
| 13030 | final ActivityRecord r = app.activities.get(j); |
| 13031 | if (r.visible) { |
| 13032 | // App has a visible activity; only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13033 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 13034 | adj = ProcessList.VISIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13035 | app.adjType = "visible"; |
| 13036 | } |
| 13037 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13038 | app.hidden = false; |
| 13039 | app.foregroundActivities = true; |
| 13040 | break; |
| 13041 | } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED |
| 13042 | || r.state == ActivityState.STOPPING) { |
| 13043 | // Only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13044 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13045 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13046 | app.adjType = "stopping"; |
| 13047 | } |
| 13048 | app.foregroundActivities = true; |
| 13049 | } |
| 13050 | } |
| 13051 | } |
| 13052 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13053 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13054 | if (app.foregroundServices) { |
| 13055 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13056 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13057 | app.adjType = "foreground-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13058 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13059 | } else if (app.forcingToForeground != null) { |
| 13060 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13061 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13062 | app.adjType = "force-foreground"; |
| 13063 | app.adjSource = app.forcingToForeground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13064 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13065 | } |
| 13066 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13067 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13068 | if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13069 | // We don't want to kill the current heavy-weight process. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13070 | adj = ProcessList.HEAVY_WEIGHT_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13071 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 13072 | app.adjType = "heavy"; |
| 13073 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13074 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13075 | if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13076 | // This process is hosting what we currently consider to be the |
| 13077 | // home app, so we don't want to let it go into the background. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13078 | adj = ProcessList.HOME_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13079 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 13080 | app.adjType = "home"; |
| 13081 | } |
| 13082 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13083 | //Slog.i(TAG, "OOM " + app + ": initial adj=" + adj); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13084 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 13085 | // 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] | 13086 | // there are applications dependent on our services or providers, but |
| 13087 | // this gives us a baseline and makes sure we don't get into an |
| 13088 | // infinite recursion. |
| 13089 | app.adjSeq = mAdjSeq; |
| 13090 | app.curRawAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13091 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 13092 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 13093 | // If possible we want to avoid killing apps while they're being backed up |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13094 | if (adj > ProcessList.BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13095 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13096 | adj = ProcessList.BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13097 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13098 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 13099 | } |
| 13100 | } |
| 13101 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13102 | if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13103 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13104 | final long now = SystemClock.uptimeMillis(); |
| 13105 | // This process is more important if the top activity is |
| 13106 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13107 | Iterator<ServiceRecord> jt = app.services.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13108 | while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13109 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13110 | if (s.startRequested) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13111 | if (app.hasShownUi) { |
| 13112 | // If this process has shown some UI, let it immediately |
| 13113 | // go to the LRU list because it may be pretty heavy with |
| 13114 | // UI stuff. We'll tag it with a label just to help |
| 13115 | // debug and understand what is going on. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13116 | if (adj > ProcessList.SECONDARY_SERVER_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13117 | app.adjType = "started-bg-ui-services"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13118 | } |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13119 | } else { |
| 13120 | if (now < (s.lastActivity+MAX_SERVICE_INACTIVITY)) { |
| 13121 | // This service has seen some activity within |
| 13122 | // recent memory, so we will keep its process ahead |
| 13123 | // of the background processes. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13124 | if (adj > ProcessList.SECONDARY_SERVER_ADJ) { |
| 13125 | adj = ProcessList.SECONDARY_SERVER_ADJ; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13126 | app.adjType = "started-services"; |
| 13127 | app.hidden = false; |
| 13128 | } |
| 13129 | } |
| 13130 | // If we have let the service slide into the background |
| 13131 | // state, still have some text describing what it is doing |
| 13132 | // even though the service no longer has an impact. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13133 | if (adj > ProcessList.SECONDARY_SERVER_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13134 | app.adjType = "started-bg-services"; |
| 13135 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13136 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13137 | // Don't kill this process because it is doing work; it |
| 13138 | // has said it is doing work. |
| 13139 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13140 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13141 | if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13142 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13143 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13144 | = s.connections.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13145 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13146 | ArrayList<ConnectionRecord> clist = kt.next(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13147 | for (int i=0; i<clist.size() && adj > ProcessList.FOREGROUND_APP_ADJ; i++) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13148 | // XXX should compute this based on the max of |
| 13149 | // all connected clients. |
| 13150 | ConnectionRecord cr = clist.get(i); |
| 13151 | if (cr.binding.client == app) { |
| 13152 | // Binding to ourself is not interesting. |
| 13153 | continue; |
| 13154 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13155 | if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13156 | ProcessRecord client = cr.binding.client; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13157 | int clientAdj = adj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13158 | int myHiddenAdj = hiddenAdj; |
| 13159 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13160 | if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13161 | myHiddenAdj = client.hiddenAdj; |
| 13162 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13163 | myHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13164 | } |
| 13165 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13166 | clientAdj = computeOomAdjLocked( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13167 | client, myHiddenAdj, TOP_APP, true); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13168 | String adjType = null; |
| 13169 | if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) { |
| 13170 | // Not doing bind OOM management, so treat |
| 13171 | // this guy more like a started service. |
| 13172 | if (app.hasShownUi) { |
| 13173 | // If this process has shown some UI, let it immediately |
| 13174 | // go to the LRU list because it may be pretty heavy with |
| 13175 | // UI stuff. We'll tag it with a label just to help |
| 13176 | // debug and understand what is going on. |
| 13177 | if (adj > clientAdj) { |
| 13178 | adjType = "bound-bg-ui-services"; |
| 13179 | } |
| 13180 | clientAdj = adj; |
| 13181 | } else { |
| 13182 | if (now >= (s.lastActivity+MAX_SERVICE_INACTIVITY)) { |
| 13183 | // This service has not seen activity within |
| 13184 | // recent memory, so allow it to drop to the |
| 13185 | // LRU list if there is no other reason to keep |
| 13186 | // it around. We'll also tag it with a label just |
| 13187 | // to help debug and undertand what is going on. |
| 13188 | if (adj > clientAdj) { |
| 13189 | adjType = "bound-bg-services"; |
| 13190 | } |
| 13191 | clientAdj = adj; |
| 13192 | } |
| 13193 | } |
| 13194 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13195 | if (adj > clientAdj) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13196 | // If this process has recently shown UI, and |
| 13197 | // the process that is binding to it is less |
| 13198 | // important than being visible, then we don't |
| 13199 | // care about the binding as much as we care |
| 13200 | // about letting this process get into the LRU |
| 13201 | // list to be killed and restarted if needed for |
| 13202 | // memory. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13203 | if (app.hasShownUi && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13204 | adjType = "bound-bg-ui-services"; |
| 13205 | } else { |
| 13206 | if ((cr.flags&(Context.BIND_ABOVE_CLIENT |
| 13207 | |Context.BIND_IMPORTANT)) != 0) { |
| 13208 | adj = clientAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13209 | } else if (clientAdj >= ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13210 | adj = clientAdj; |
| 13211 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13212 | adj = ProcessList.VISIBLE_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13213 | } |
| 13214 | if (!client.hidden) { |
| 13215 | app.hidden = false; |
| 13216 | } |
| 13217 | if (client.keeping) { |
| 13218 | app.keeping = true; |
| 13219 | } |
| 13220 | adjType = "service"; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13221 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13222 | } |
| 13223 | if (adjType != null) { |
| 13224 | app.adjType = adjType; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13225 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13226 | .REASON_SERVICE_IN_USE; |
| 13227 | app.adjSource = cr.binding.client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13228 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13229 | app.adjTarget = s.name; |
| 13230 | } |
| 13231 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 13232 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 13233 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13234 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13235 | } |
| 13236 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13237 | if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) { |
| 13238 | ActivityRecord a = cr.activity; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13239 | if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ && |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13240 | (a.visible || a.state == ActivityState.RESUMED |
| 13241 | || a.state == ActivityState.PAUSING)) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13242 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13243 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 13244 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13245 | } |
| 13246 | app.hidden = false; |
| 13247 | app.adjType = "service"; |
| 13248 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13249 | .REASON_SERVICE_IN_USE; |
| 13250 | app.adjSource = a; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13251 | app.adjSourceOom = adj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13252 | app.adjTarget = s.name; |
| 13253 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13254 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13255 | } |
| 13256 | } |
| 13257 | } |
| 13258 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13259 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13260 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13261 | // would like to avoid killing it unless it would prevent the current |
| 13262 | // application from running. By default we put the process in |
| 13263 | // with the rest of the background processes; as we scan through |
| 13264 | // its services we may bump it up from there. |
| 13265 | if (adj > hiddenAdj) { |
| 13266 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13267 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13268 | app.adjType = "bg-services"; |
| 13269 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13270 | } |
| 13271 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13272 | if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13273 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13274 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13275 | while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13276 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13277 | ContentProviderRecord cpr = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13278 | if (cpr.clients.size() != 0) { |
| 13279 | Iterator<ProcessRecord> kt = cpr.clients.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13280 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13281 | ProcessRecord client = kt.next(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 13282 | if (client == app) { |
| 13283 | // Being our own client is not interesting. |
| 13284 | continue; |
| 13285 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13286 | int myHiddenAdj = hiddenAdj; |
| 13287 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13288 | if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13289 | myHiddenAdj = client.hiddenAdj; |
| 13290 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13291 | myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13292 | } |
| 13293 | } |
| 13294 | int clientAdj = computeOomAdjLocked( |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13295 | client, myHiddenAdj, TOP_APP, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13296 | if (adj > clientAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13297 | if (app.hasShownUi && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13298 | app.adjType = "bg-ui-provider"; |
| 13299 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13300 | adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ |
| 13301 | ? clientAdj : ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13302 | app.adjType = "provider"; |
| 13303 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13304 | if (!client.hidden) { |
| 13305 | app.hidden = false; |
| 13306 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13307 | if (client.keeping) { |
| 13308 | app.keeping = true; |
| 13309 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 13310 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13311 | .REASON_PROVIDER_IN_USE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13312 | app.adjSource = client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13313 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 13314 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13315 | } |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13316 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 13317 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13318 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13319 | } |
| 13320 | } |
| 13321 | // If the provider has external (non-framework) process |
| 13322 | // dependencies, ensure that its adjustment is at least |
| 13323 | // FOREGROUND_APP_ADJ. |
| 13324 | if (cpr.externals != 0) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13325 | if (adj > ProcessList.FOREGROUND_APP_ADJ) { |
| 13326 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13327 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13328 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13329 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13330 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 13331 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13332 | } |
| 13333 | } |
| 13334 | } |
| 13335 | } |
| 13336 | |
| 13337 | app.curRawAdj = adj; |
| 13338 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13339 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13340 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 13341 | if (adj > app.maxAdj) { |
| 13342 | adj = app.maxAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13343 | if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13344 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13345 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13346 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13347 | if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13348 | app.keeping = true; |
| 13349 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13350 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13351 | if (app.hasAboveClient) { |
| 13352 | // If this process has bound to any services with BIND_ABOVE_CLIENT, |
| 13353 | // then we need to drop its adjustment to be lower than the service's |
| 13354 | // in order to honor the request. We want to drop it by one adjustment |
| 13355 | // level... but there is special meaning applied to various levels so |
| 13356 | // we will skip some of them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13357 | if (adj < ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13358 | // System process will not get dropped, ever |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13359 | } else if (adj < ProcessList.VISIBLE_APP_ADJ) { |
| 13360 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13361 | } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13362 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13363 | } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13364 | adj = ProcessList.HIDDEN_APP_MIN_ADJ; |
| 13365 | } else if (adj < ProcessList.EMPTY_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13366 | adj++; |
| 13367 | } |
| 13368 | } |
| 13369 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13370 | app.curAdj = adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13371 | app.curSchedGroup = schedGroup; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13372 | |
| 13373 | if (hadForegroundActivities != app.foregroundActivities) { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 13374 | mHandler.obtainMessage(DISPATCH_FOREGROUND_ACTIVITIES_CHANGED, app.pid, app.info.uid, |
| 13375 | app.foregroundActivities).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13376 | } |
| 13377 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13378 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13379 | } |
| 13380 | |
| 13381 | /** |
| 13382 | * Ask a given process to GC right now. |
| 13383 | */ |
| 13384 | final void performAppGcLocked(ProcessRecord app) { |
| 13385 | try { |
| 13386 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 13387 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13388 | if (app.reportLowMemory) { |
| 13389 | app.reportLowMemory = false; |
| 13390 | app.thread.scheduleLowMemory(); |
| 13391 | } else { |
| 13392 | app.thread.processInBackground(); |
| 13393 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13394 | } |
| 13395 | } catch (Exception e) { |
| 13396 | // whatever. |
| 13397 | } |
| 13398 | } |
| 13399 | |
| 13400 | /** |
| 13401 | * Returns true if things are idle enough to perform GCs. |
| 13402 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13403 | private final boolean canGcNowLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13404 | return mParallelBroadcasts.size() == 0 |
| 13405 | && mOrderedBroadcasts.size() == 0 |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13406 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 13407 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13408 | } |
| 13409 | |
| 13410 | /** |
| 13411 | * Perform GCs on all processes that are waiting for it, but only |
| 13412 | * if things are idle. |
| 13413 | */ |
| 13414 | final void performAppGcsLocked() { |
| 13415 | final int N = mProcessesToGc.size(); |
| 13416 | if (N <= 0) { |
| 13417 | return; |
| 13418 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13419 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13420 | while (mProcessesToGc.size() > 0) { |
| 13421 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13422 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13423 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 13424 | <= SystemClock.uptimeMillis()) { |
| 13425 | // To avoid spamming the system, we will GC processes one |
| 13426 | // at a time, waiting a few seconds between each. |
| 13427 | performAppGcLocked(proc); |
| 13428 | scheduleAppGcsLocked(); |
| 13429 | return; |
| 13430 | } else { |
| 13431 | // It hasn't been long enough since we last GCed this |
| 13432 | // process... put it in the list to wait for its time. |
| 13433 | addProcessToGcListLocked(proc); |
| 13434 | break; |
| 13435 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13436 | } |
| 13437 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13438 | |
| 13439 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13440 | } |
| 13441 | } |
| 13442 | |
| 13443 | /** |
| 13444 | * If all looks good, perform GCs on all processes waiting for them. |
| 13445 | */ |
| 13446 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13447 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13448 | performAppGcsLocked(); |
| 13449 | return; |
| 13450 | } |
| 13451 | // Still not idle, wait some more. |
| 13452 | scheduleAppGcsLocked(); |
| 13453 | } |
| 13454 | |
| 13455 | /** |
| 13456 | * Schedule the execution of all pending app GCs. |
| 13457 | */ |
| 13458 | final void scheduleAppGcsLocked() { |
| 13459 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13460 | |
| 13461 | if (mProcessesToGc.size() > 0) { |
| 13462 | // Schedule a GC for the time to the next process. |
| 13463 | ProcessRecord proc = mProcessesToGc.get(0); |
| 13464 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 13465 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13466 | long when = proc.lastRequestedGc + GC_MIN_INTERVAL; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13467 | long now = SystemClock.uptimeMillis(); |
| 13468 | if (when < (now+GC_TIMEOUT)) { |
| 13469 | when = now + GC_TIMEOUT; |
| 13470 | } |
| 13471 | mHandler.sendMessageAtTime(msg, when); |
| 13472 | } |
| 13473 | } |
| 13474 | |
| 13475 | /** |
| 13476 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 13477 | * list in sorted order by the last GC time. The process can't already be |
| 13478 | * on the list. |
| 13479 | */ |
| 13480 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 13481 | boolean added = false; |
| 13482 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 13483 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 13484 | proc.lastRequestedGc) { |
| 13485 | added = true; |
| 13486 | mProcessesToGc.add(i+1, proc); |
| 13487 | break; |
| 13488 | } |
| 13489 | } |
| 13490 | if (!added) { |
| 13491 | mProcessesToGc.add(0, proc); |
| 13492 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13493 | } |
| 13494 | |
| 13495 | /** |
| 13496 | * Set up to ask a process to GC itself. This will either do it |
| 13497 | * immediately, or put it on the list of processes to gc the next |
| 13498 | * time things are idle. |
| 13499 | */ |
| 13500 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 13501 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13502 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13503 | return; |
| 13504 | } |
| 13505 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13506 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13507 | scheduleAppGcsLocked(); |
| 13508 | } |
| 13509 | } |
| 13510 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13511 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 13512 | updateCpuStatsNow(); |
| 13513 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13514 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13515 | boolean doWakeKills = doKills; |
| 13516 | boolean doCpuKills = doKills; |
| 13517 | if (mLastPowerCheckRealtime == 0) { |
| 13518 | doWakeKills = false; |
| 13519 | } |
| 13520 | if (mLastPowerCheckUptime == 0) { |
| 13521 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13522 | } |
| 13523 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13524 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13525 | } |
| 13526 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13527 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 13528 | final long curUptime = SystemClock.uptimeMillis(); |
| 13529 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 13530 | mLastPowerCheckRealtime = curRealtime; |
| 13531 | mLastPowerCheckUptime = curUptime; |
| 13532 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 13533 | doWakeKills = false; |
| 13534 | } |
| 13535 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 13536 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13537 | } |
| 13538 | int i = mLruProcesses.size(); |
| 13539 | while (i > 0) { |
| 13540 | i--; |
| 13541 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13542 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13543 | long wtime; |
| 13544 | synchronized (stats) { |
| 13545 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 13546 | app.pid, curRealtime); |
| 13547 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13548 | long wtimeUsed = wtime - app.lastWakeTime; |
| 13549 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 13550 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13551 | StringBuilder sb = new StringBuilder(128); |
| 13552 | sb.append("Wake for "); |
| 13553 | app.toShortString(sb); |
| 13554 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13555 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13556 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13557 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13558 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13559 | sb.append((wtimeUsed*100)/realtimeSince); |
| 13560 | sb.append("%)"); |
| 13561 | Slog.i(TAG, sb.toString()); |
| 13562 | sb.setLength(0); |
| 13563 | sb.append("CPU for "); |
| 13564 | app.toShortString(sb); |
| 13565 | sb.append(": over "); |
| 13566 | TimeUtils.formatDuration(uptimeSince, sb); |
| 13567 | sb.append(" used "); |
| 13568 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 13569 | sb.append(" ("); |
| 13570 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13571 | sb.append("%)"); |
| 13572 | Slog.i(TAG, sb.toString()); |
| 13573 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13574 | // If a process has held a wake lock for more |
| 13575 | // than 50% of the time during this period, |
| 13576 | // that sounds pad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13577 | if (doWakeKills && realtimeSince > 0 |
| 13578 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 13579 | synchronized (stats) { |
| 13580 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 13581 | realtimeSince, wtimeUsed); |
| 13582 | } |
| 13583 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 13584 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 13585 | + " during " + realtimeSince); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13586 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13587 | app.processName, app.setAdj, "excessive wake lock"); |
| 13588 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13589 | } else if (doCpuKills && uptimeSince > 0 |
| 13590 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 13591 | synchronized (stats) { |
| 13592 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 13593 | uptimeSince, cputimeUsed); |
| 13594 | } |
| 13595 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 13596 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 13597 | + " during " + uptimeSince); |
| 13598 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13599 | app.processName, app.setAdj, "excessive cpu"); |
| 13600 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13601 | } else { |
| 13602 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13603 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13604 | } |
| 13605 | } |
| 13606 | } |
| 13607 | } |
| 13608 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13609 | private final boolean updateOomAdjLocked( |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13610 | ProcessRecord app, int hiddenAdj, ProcessRecord TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13611 | app.hiddenAdj = hiddenAdj; |
| 13612 | |
| 13613 | if (app.thread == null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13614 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13615 | } |
| 13616 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13617 | final boolean wasKeeping = app.keeping; |
| 13618 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13619 | boolean success = true; |
| 13620 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13621 | computeOomAdjLocked(app, hiddenAdj, TOP_APP, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13622 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13623 | if (app.curRawAdj != app.setRawAdj) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13624 | if (false) { |
| 13625 | // Removing for now. Forcing GCs is not so useful anymore |
| 13626 | // with Dalvik, and the new memory level hint facility is |
| 13627 | // better for what we need to do these days. |
| 13628 | if (app.curRawAdj > ProcessList.FOREGROUND_APP_ADJ |
| 13629 | && app.setRawAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
| 13630 | // If this app is transitioning from foreground to |
| 13631 | // non-foreground, have it do a gc. |
| 13632 | scheduleAppGcLocked(app); |
| 13633 | } else if (app.curRawAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13634 | && app.setRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13635 | // Likewise do a gc when an app is moving in to the |
| 13636 | // background (such as a service stopping). |
| 13637 | scheduleAppGcLocked(app); |
| 13638 | } |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13639 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13640 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13641 | if (wasKeeping && !app.keeping) { |
| 13642 | // This app is no longer something we want to keep. Note |
| 13643 | // its current wake lock time to later know to kill it if |
| 13644 | // it is not behaving well. |
| 13645 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 13646 | synchronized (stats) { |
| 13647 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 13648 | app.pid, SystemClock.elapsedRealtime()); |
| 13649 | } |
| 13650 | app.lastCpuTime = app.curCpuTime; |
| 13651 | } |
| 13652 | |
| 13653 | app.setRawAdj = app.curRawAdj; |
| 13654 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13655 | if (app.curAdj != app.setAdj) { |
| 13656 | if (Process.setOomAdj(app.pid, app.curAdj)) { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13657 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
| 13658 | TAG, "Set app " + app.processName + |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13659 | " oom adj to " + app.curAdj + " because " + app.adjType); |
| 13660 | app.setAdj = app.curAdj; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13661 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13662 | success = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13663 | Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13664 | } |
| 13665 | } |
| 13666 | if (app.setSchedGroup != app.curSchedGroup) { |
| 13667 | app.setSchedGroup = app.curSchedGroup; |
| 13668 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
| 13669 | "Setting process group of " + app.processName |
| 13670 | + " to " + app.curSchedGroup); |
| 13671 | if (app.waitingToKill != null && |
| 13672 | app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 13673 | Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill); |
| 13674 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13675 | app.processName, app.setAdj, app.waitingToKill); |
| 13676 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13677 | success = false; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13678 | } else { |
| 13679 | if (true) { |
| 13680 | long oldId = Binder.clearCallingIdentity(); |
| 13681 | try { |
| 13682 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 13683 | } catch (Exception e) { |
| 13684 | Slog.w(TAG, "Failed setting process group of " + app.pid |
| 13685 | + " to " + app.curSchedGroup); |
| 13686 | e.printStackTrace(); |
| 13687 | } finally { |
| 13688 | Binder.restoreCallingIdentity(oldId); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13689 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13690 | } else { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13691 | if (app.thread != null) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13692 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13693 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 13694 | } catch (RemoteException e) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13695 | } |
| 13696 | } |
| 13697 | } |
| 13698 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13699 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13700 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13701 | } |
| 13702 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13703 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13704 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13705 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13706 | resumedActivity = mMainStack.mPausingActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13707 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13708 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13709 | } |
| 13710 | } |
| 13711 | return resumedActivity; |
| 13712 | } |
| 13713 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13714 | private final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13715 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13716 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13717 | int curAdj = app.curAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13718 | final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13719 | && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13720 | |
| 13721 | mAdjSeq++; |
| 13722 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13723 | boolean success = updateOomAdjLocked(app, app.hiddenAdj, TOP_APP); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13724 | final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13725 | && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13726 | if (nowHidden != wasHidden) { |
| 13727 | // Changed to/from hidden state, so apps after it in the LRU |
| 13728 | // list may also be changed. |
| 13729 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13730 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13731 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13732 | } |
| 13733 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13734 | final void updateOomAdjLocked() { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13735 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13736 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13737 | |
| 13738 | if (false) { |
| 13739 | RuntimeException e = new RuntimeException(); |
| 13740 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13741 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13742 | } |
| 13743 | |
| 13744 | mAdjSeq++; |
| 13745 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13746 | // Let's determine how many processes we have running vs. |
| 13747 | // how many slots we have for background processes; we may want |
| 13748 | // to put multiple processes in a slot of there are enough of |
| 13749 | // them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13750 | int numSlots = ProcessList.HIDDEN_APP_MAX_ADJ - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13751 | int factor = (mLruProcesses.size()-4)/numSlots; |
| 13752 | if (factor < 1) factor = 1; |
| 13753 | int step = 0; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13754 | int numHidden = 0; |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13755 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13756 | // First update the OOM adjustment for each of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13757 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13758 | int i = mLruProcesses.size(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13759 | int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13760 | int numBg = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13761 | while (i > 0) { |
| 13762 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13763 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13764 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13765 | updateOomAdjLocked(app, curHiddenAdj, TOP_APP); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13766 | if (curHiddenAdj < ProcessList.EMPTY_APP_ADJ |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13767 | && app.curAdj == curHiddenAdj) { |
| 13768 | step++; |
| 13769 | if (step >= factor) { |
| 13770 | step = 0; |
| 13771 | curHiddenAdj++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13772 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13773 | } |
| 13774 | if (!app.killedBackground) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13775 | if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13776 | numHidden++; |
| 13777 | if (numHidden > mProcessLimit) { |
| 13778 | Slog.i(TAG, "No longer want " + app.processName |
| 13779 | + " (pid " + app.pid + "): hidden #" + numHidden); |
| 13780 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13781 | app.processName, app.setAdj, "too many background"); |
| 13782 | app.killedBackground = true; |
| 13783 | Process.killProcessQuiet(app.pid); |
| 13784 | } else { |
| 13785 | numBg++; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13786 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13787 | } else if (app.curAdj >= ProcessList.HOME_APP_ADJ) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13788 | numBg++; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13789 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13790 | } |
| 13791 | } |
| 13792 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13793 | // Now determine the memory trimming level of background processes. |
| 13794 | // Unfortunately we need to start at the back of the list to do this |
| 13795 | // properly. We only do this if the number of background apps we |
| 13796 | // are managing to keep around is less than half the maximum we desire; |
| 13797 | // if we are keeping a good number around, we'll let them use whatever |
| 13798 | // memory they want. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13799 | if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/2)) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13800 | final int N = mLruProcesses.size(); |
| 13801 | factor = numBg/3; |
| 13802 | step = 0; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13803 | int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13804 | for (i=0; i<N; i++) { |
| 13805 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13806 | if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ && !app.killedBackground) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13807 | if (app.trimMemoryLevel < curLevel && app.thread != null) { |
| 13808 | try { |
| 13809 | app.thread.scheduleTrimMemory(curLevel); |
| 13810 | } catch (RemoteException e) { |
| 13811 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13812 | if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13813 | // For these apps we will also finish their activities |
| 13814 | // to help them free memory. |
| 13815 | mMainStack.destroyActivitiesLocked(app, false); |
| 13816 | } |
| 13817 | } |
| 13818 | app.trimMemoryLevel = curLevel; |
| 13819 | step++; |
| 13820 | if (step >= factor) { |
| 13821 | switch (curLevel) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13822 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 13823 | curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13824 | break; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13825 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 13826 | curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13827 | break; |
| 13828 | } |
| 13829 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13830 | } else if (app.curAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13831 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13832 | && app.thread != null) { |
| 13833 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13834 | app.thread.scheduleTrimMemory( |
| 13835 | ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13836 | } catch (RemoteException e) { |
| 13837 | } |
| 13838 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13839 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13840 | } else if ((app.curAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13841 | && app.pendingUiClean) { |
| 13842 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 13843 | && app.thread != null) { |
| 13844 | try { |
| 13845 | app.thread.scheduleTrimMemory( |
| 13846 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 13847 | } catch (RemoteException e) { |
| 13848 | } |
| 13849 | } |
| 13850 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 13851 | app.pendingUiClean = false; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13852 | } else { |
| 13853 | app.trimMemoryLevel = 0; |
| 13854 | } |
| 13855 | } |
| 13856 | } else { |
| 13857 | final int N = mLruProcesses.size(); |
| 13858 | for (i=0; i<N; i++) { |
| 13859 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13860 | if ((app.curAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13861 | && app.pendingUiClean) { |
| 13862 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 13863 | && app.thread != null) { |
| 13864 | try { |
| 13865 | app.thread.scheduleTrimMemory( |
| 13866 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 13867 | } catch (RemoteException e) { |
| 13868 | } |
| 13869 | } |
| 13870 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 13871 | app.pendingUiClean = false; |
| 13872 | } else { |
| 13873 | app.trimMemoryLevel = 0; |
| 13874 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13875 | } |
| 13876 | } |
| 13877 | |
| 13878 | if (mAlwaysFinishActivities) { |
| 13879 | mMainStack.destroyActivitiesLocked(null, false); |
| 13880 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13881 | } |
| 13882 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13883 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13884 | synchronized (this) { |
| 13885 | int i; |
| 13886 | |
| 13887 | // First remove any unused application processes whose package |
| 13888 | // has been removed. |
| 13889 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 13890 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 13891 | if (app.activities.size() == 0 |
| 13892 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13893 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13894 | TAG, "Exiting empty application process " |
| 13895 | + app.processName + " (" |
| 13896 | + (app.thread != null ? app.thread.asBinder() : null) |
| 13897 | + ")\n"); |
| 13898 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 13899 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13900 | app.processName, app.setAdj, "empty"); |
| 13901 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13902 | } else { |
| 13903 | try { |
| 13904 | app.thread.scheduleExit(); |
| 13905 | } catch (Exception e) { |
| 13906 | // Ignore exceptions. |
| 13907 | } |
| 13908 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13909 | cleanUpApplicationRecordLocked(app, false, true, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13910 | mRemovedProcesses.remove(i); |
| 13911 | |
| 13912 | if (app.persistent) { |
| 13913 | if (app.persistent) { |
| 13914 | addAppLocked(app.info); |
| 13915 | } |
| 13916 | } |
| 13917 | } |
| 13918 | } |
| 13919 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13920 | // Now update the oom adj for all processes. |
| 13921 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13922 | } |
| 13923 | } |
| 13924 | |
| 13925 | /** This method sends the specified signal to each of the persistent apps */ |
| 13926 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 13927 | if (sig != Process.SIGNAL_USR1) { |
| 13928 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 13929 | } |
| 13930 | |
| 13931 | synchronized (this) { |
| 13932 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 13933 | != PackageManager.PERMISSION_GRANTED) { |
| 13934 | throw new SecurityException("Requires permission " |
| 13935 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 13936 | } |
| 13937 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13938 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 13939 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13940 | if (r.thread != null && r.persistent) { |
| 13941 | Process.sendSignal(r.pid, sig); |
| 13942 | } |
| 13943 | } |
| 13944 | } |
| 13945 | } |
| 13946 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13947 | private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) { |
| 13948 | if (proc == null || proc == mProfileProc) { |
| 13949 | proc = mProfileProc; |
| 13950 | path = mProfileFile; |
| 13951 | profileType = mProfileType; |
| 13952 | clearProfilerLocked(); |
| 13953 | } |
| 13954 | if (proc == null) { |
| 13955 | return; |
| 13956 | } |
| 13957 | try { |
| 13958 | proc.thread.profilerControl(false, path, null, profileType); |
| 13959 | } catch (RemoteException e) { |
| 13960 | throw new IllegalStateException("Process disappeared"); |
| 13961 | } |
| 13962 | } |
| 13963 | |
| 13964 | private void clearProfilerLocked() { |
| 13965 | if (mProfileFd != null) { |
| 13966 | try { |
| 13967 | mProfileFd.close(); |
| 13968 | } catch (IOException e) { |
| 13969 | } |
| 13970 | } |
| 13971 | mProfileApp = null; |
| 13972 | mProfileProc = null; |
| 13973 | mProfileFile = null; |
| 13974 | mProfileType = 0; |
| 13975 | mAutoStopProfiler = false; |
| 13976 | } |
| 13977 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13978 | public boolean profileControl(String process, boolean start, |
Romain Guy | 7eabe55 | 2011-07-21 14:56:34 -0700 | [diff] [blame] | 13979 | String path, ParcelFileDescriptor fd, int profileType) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13980 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13981 | try { |
| 13982 | synchronized (this) { |
| 13983 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 13984 | // its own permission. |
| 13985 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 13986 | != PackageManager.PERMISSION_GRANTED) { |
| 13987 | throw new SecurityException("Requires permission " |
| 13988 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13989 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13990 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13991 | if (start && fd == null) { |
| 13992 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13993 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13994 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13995 | ProcessRecord proc = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13996 | if (process != null) { |
| 13997 | try { |
| 13998 | int pid = Integer.parseInt(process); |
| 13999 | synchronized (mPidsSelfLocked) { |
| 14000 | proc = mPidsSelfLocked.get(pid); |
| 14001 | } |
| 14002 | } catch (NumberFormatException e) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14003 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14004 | |
| 14005 | if (proc == null) { |
| 14006 | HashMap<String, SparseArray<ProcessRecord>> all |
| 14007 | = mProcessNames.getMap(); |
| 14008 | SparseArray<ProcessRecord> procs = all.get(process); |
| 14009 | if (procs != null && procs.size() > 0) { |
| 14010 | proc = procs.valueAt(0); |
| 14011 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14012 | } |
| 14013 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14014 | |
| 14015 | if (start && (proc == null || proc.thread == null)) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14016 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14017 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14018 | |
| 14019 | if (start) { |
| 14020 | stopProfilerLocked(null, null, 0); |
| 14021 | setProfileApp(proc.info, proc.processName, path, fd, false); |
| 14022 | mProfileProc = proc; |
| 14023 | mProfileType = profileType; |
| 14024 | try { |
| 14025 | fd = fd.dup(); |
| 14026 | } catch (IOException e) { |
| 14027 | fd = null; |
| 14028 | } |
| 14029 | proc.thread.profilerControl(start, path, fd, profileType); |
| 14030 | fd = null; |
| 14031 | mProfileFd = null; |
| 14032 | } else { |
| 14033 | stopProfilerLocked(proc, path, profileType); |
| 14034 | if (fd != null) { |
| 14035 | try { |
| 14036 | fd.close(); |
| 14037 | } catch (IOException e) { |
| 14038 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14039 | } |
| 14040 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14041 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14042 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14043 | } |
| 14044 | } catch (RemoteException e) { |
| 14045 | throw new IllegalStateException("Process disappeared"); |
| 14046 | } finally { |
| 14047 | if (fd != null) { |
| 14048 | try { |
| 14049 | fd.close(); |
| 14050 | } catch (IOException e) { |
| 14051 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14052 | } |
| 14053 | } |
| 14054 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14055 | |
| 14056 | public boolean dumpHeap(String process, boolean managed, |
| 14057 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 14058 | |
| 14059 | try { |
| 14060 | synchronized (this) { |
| 14061 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14062 | // its own permission (same as profileControl). |
| 14063 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14064 | != PackageManager.PERMISSION_GRANTED) { |
| 14065 | throw new SecurityException("Requires permission " |
| 14066 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 14067 | } |
| 14068 | |
| 14069 | if (fd == null) { |
| 14070 | throw new IllegalArgumentException("null fd"); |
| 14071 | } |
| 14072 | |
| 14073 | ProcessRecord proc = null; |
| 14074 | try { |
| 14075 | int pid = Integer.parseInt(process); |
| 14076 | synchronized (mPidsSelfLocked) { |
| 14077 | proc = mPidsSelfLocked.get(pid); |
| 14078 | } |
| 14079 | } catch (NumberFormatException e) { |
| 14080 | } |
| 14081 | |
| 14082 | if (proc == null) { |
| 14083 | HashMap<String, SparseArray<ProcessRecord>> all |
| 14084 | = mProcessNames.getMap(); |
| 14085 | SparseArray<ProcessRecord> procs = all.get(process); |
| 14086 | if (procs != null && procs.size() > 0) { |
| 14087 | proc = procs.valueAt(0); |
| 14088 | } |
| 14089 | } |
| 14090 | |
| 14091 | if (proc == null || proc.thread == null) { |
| 14092 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14093 | } |
| 14094 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 14095 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 14096 | if (!isDebuggable) { |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14097 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 14098 | throw new SecurityException("Process not debuggable: " + proc); |
| 14099 | } |
| 14100 | } |
| 14101 | |
| 14102 | proc.thread.dumpHeap(managed, path, fd); |
| 14103 | fd = null; |
| 14104 | return true; |
| 14105 | } |
| 14106 | } catch (RemoteException e) { |
| 14107 | throw new IllegalStateException("Process disappeared"); |
| 14108 | } finally { |
| 14109 | if (fd != null) { |
| 14110 | try { |
| 14111 | fd.close(); |
| 14112 | } catch (IOException e) { |
| 14113 | } |
| 14114 | } |
| 14115 | } |
| 14116 | } |
| 14117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14118 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 14119 | public void monitor() { |
| 14120 | synchronized (this) { } |
| 14121 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 14122 | |
| 14123 | public void onCoreSettingsChange(Bundle settings) { |
| 14124 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 14125 | ProcessRecord processRecord = mLruProcesses.get(i); |
| 14126 | try { |
| 14127 | if (processRecord.thread != null) { |
| 14128 | processRecord.thread.setCoreSettings(settings); |
| 14129 | } |
| 14130 | } catch (RemoteException re) { |
| 14131 | /* ignore */ |
| 14132 | } |
| 14133 | } |
| 14134 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14135 | |
| 14136 | // Multi-user methods |
| 14137 | |
| 14138 | public boolean switchUser(int userid) { |
| 14139 | // TODO |
| 14140 | return true; |
| 14141 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14142 | } |