The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.am; |
| 18 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import com.android.internal.os.BatteryStatsImpl; |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 21 | import com.android.server.AttributeCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import com.android.server.IntentResolver; |
| 23 | import com.android.server.ProcessMap; |
| 24 | import com.android.server.ProcessStats; |
| 25 | import com.android.server.SystemServer; |
| 26 | import com.android.server.Watchdog; |
| 27 | import com.android.server.WindowManagerService; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 28 | import com.android.server.am.ActivityStack.ActivityState; |
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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.app.IServiceConnection; |
| 46 | import android.app.IThumbnailReceiver; |
| 47 | import android.app.Instrumentation; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 48 | import android.app.Notification; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 49 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.app.PendingIntent; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 51 | import android.app.Service; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 52 | import android.app.backup.IBackupManager; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 53 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 54 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.content.ComponentName; |
| 56 | import android.content.ContentResolver; |
| 57 | import android.content.Context; |
Christian Mehlmauer | 7664e20 | 2010-07-20 08:46:17 +0200 | [diff] [blame] | 58 | import android.content.DialogInterface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.content.Intent; |
| 60 | import android.content.IntentFilter; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 61 | import android.content.IIntentReceiver; |
| 62 | import android.content.IIntentSender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 63 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.content.pm.ActivityInfo; |
| 65 | import android.content.pm.ApplicationInfo; |
| 66 | import android.content.pm.ConfigurationInfo; |
| 67 | import android.content.pm.IPackageDataObserver; |
| 68 | import android.content.pm.IPackageManager; |
| 69 | import android.content.pm.InstrumentationInfo; |
Dan Egnor | 66c40e7 | 2010-01-26 16:23:11 -0800 | [diff] [blame] | 70 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | import android.content.pm.PackageManager; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 72 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.content.pm.ProviderInfo; |
| 74 | import android.content.pm.ResolveInfo; |
| 75 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 76 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | import android.content.res.Configuration; |
| 78 | import android.graphics.Bitmap; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 79 | import android.net.Proxy; |
| 80 | import android.net.ProxyProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import android.net.Uri; |
| 82 | import android.os.Binder; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 83 | import android.os.Build; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 84 | import android.os.Bundle; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 85 | import android.os.Debug; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 86 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | import android.os.Environment; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 88 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | import android.os.FileUtils; |
| 90 | import android.os.Handler; |
| 91 | import android.os.IBinder; |
| 92 | import android.os.IPermissionController; |
| 93 | import android.os.Looper; |
| 94 | import android.os.Message; |
| 95 | import android.os.Parcel; |
| 96 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | import android.os.Process; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 98 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | import android.os.RemoteException; |
| 100 | import android.os.ServiceManager; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 101 | import android.os.StrictMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | import android.os.SystemClock; |
| 103 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | import android.util.Config; |
| 106 | import android.util.EventLog; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 107 | import android.util.Slog; |
Joe Onorato | 5d3bea6 | 2010-03-01 13:44:29 -0800 | [diff] [blame] | 108 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | import android.util.PrintWriterPrinter; |
| 110 | import android.util.SparseArray; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 111 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | import android.view.Gravity; |
| 113 | import android.view.LayoutInflater; |
| 114 | import android.view.View; |
| 115 | import android.view.WindowManager; |
| 116 | import android.view.WindowManagerPolicy; |
| 117 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 118 | import java.io.BufferedInputStream; |
| 119 | import java.io.BufferedOutputStream; |
| 120 | import java.io.DataInputStream; |
| 121 | import java.io.DataOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | import java.io.File; |
| 123 | import java.io.FileDescriptor; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 124 | import java.io.FileInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | import java.io.FileNotFoundException; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 126 | import java.io.FileOutputStream; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 127 | import java.io.IOException; |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 128 | import java.io.InputStreamReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | import java.io.PrintWriter; |
| 130 | import java.lang.IllegalStateException; |
| 131 | import java.lang.ref.WeakReference; |
| 132 | import java.util.ArrayList; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 133 | import java.util.Collections; |
| 134 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | import java.util.HashMap; |
| 136 | import java.util.HashSet; |
| 137 | import java.util.Iterator; |
| 138 | import java.util.List; |
| 139 | import java.util.Locale; |
| 140 | import java.util.Map; |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 141 | import java.util.Set; |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 142 | import java.util.concurrent.atomic.AtomicBoolean; |
| 143 | import java.util.concurrent.atomic.AtomicLong; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 145 | public final class ActivityManagerService extends ActivityManagerNative |
| 146 | implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | static final String TAG = "ActivityManager"; |
| 148 | static final boolean DEBUG = false; |
| 149 | static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV; |
| 150 | static final boolean DEBUG_SWITCH = localLOGV || false; |
| 151 | static final boolean DEBUG_TASKS = localLOGV || false; |
| 152 | static final boolean DEBUG_PAUSE = localLOGV || false; |
| 153 | static final boolean DEBUG_OOM_ADJ = localLOGV || false; |
| 154 | static final boolean DEBUG_TRANSITION = localLOGV || false; |
| 155 | static final boolean DEBUG_BROADCAST = localLOGV || false; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 156 | static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | static final boolean DEBUG_SERVICE = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 158 | static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | static final boolean DEBUG_VISBILITY = localLOGV || false; |
| 160 | static final boolean DEBUG_PROCESSES = localLOGV || false; |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 161 | static final boolean DEBUG_PROVIDER = localLOGV || false; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 162 | static final boolean DEBUG_URI_PERMISSION = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | static final boolean DEBUG_USER_LEAVING = localLOGV || false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 164 | static final boolean DEBUG_RESULTS = localLOGV || false; |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 165 | static final boolean DEBUG_BACKUP = localLOGV || false; |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 166 | static final boolean DEBUG_CONFIGURATION = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 167 | static final boolean DEBUG_POWER = localLOGV || false; |
| 168 | static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | static final boolean VALIDATE_TOKENS = false; |
| 170 | static final boolean SHOW_ACTIVITY_START_TIME = true; |
| 171 | |
| 172 | // Control over CPU and battery monitoring. |
| 173 | static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes. |
| 174 | static final boolean MONITOR_CPU_USAGE = true; |
| 175 | static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds. |
| 176 | static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample. |
| 177 | static final boolean MONITOR_THREAD_CPU_USAGE = false; |
| 178 | |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 179 | // 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] | 180 | static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | private static final String SYSTEM_SECURE = "ro.secure"; |
| 183 | |
| 184 | // This is the maximum number of application processes we would like |
| 185 | // to have running. Due to the asynchronous nature of things, we can |
| 186 | // temporarily go beyond this limit. |
| 187 | static final int MAX_PROCESSES = 2; |
| 188 | |
| 189 | // Set to false to leave processes running indefinitely, relying on |
| 190 | // the kernel killing them as resources are required. |
| 191 | static final boolean ENFORCE_PROCESS_LIMIT = false; |
| 192 | |
| 193 | // This is the maximum number of activities that we would like to have |
| 194 | // running at a given time. |
| 195 | static final int MAX_ACTIVITIES = 20; |
| 196 | |
| 197 | // Maximum number of recent tasks that we can remember. |
| 198 | static final int MAX_RECENT_TASKS = 20; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 199 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 200 | // Amount of time after a call to stopAppSwitches() during which we will |
| 201 | // prevent further untrusted switches from happening. |
| 202 | static final long APP_SWITCH_DELAY_TIME = 5*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | |
| 204 | // How long we wait for a launched process to attach to the activity manager |
| 205 | // before we decide it's never going to come up for real. |
| 206 | static final int PROC_START_TIMEOUT = 10*1000; |
| 207 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | // How long to wait after going idle before forcing apps to GC. |
| 209 | static final int GC_TIMEOUT = 5*1000; |
| 210 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 211 | // The minimum amount of time between successive GC requests for a process. |
| 212 | static final int GC_MIN_INTERVAL = 60*1000; |
| 213 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 214 | // The rate at which we check for apps using excessive power -- 15 mins. |
| 215 | static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000; |
| 216 | |
| 217 | // The minimum sample duration we will allow before deciding we have |
| 218 | // enough data on wake locks to start killing things. |
| 219 | static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
| 220 | |
| 221 | // The minimum sample duration we will allow before deciding we have |
| 222 | // enough data on CPU usage to start killing things. |
| 223 | 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] | 224 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | // How long we allow a receiver to run before giving up on it. |
| 226 | static final int BROADCAST_TIMEOUT = 10*1000; |
| 227 | |
| 228 | // How long we wait for a service to finish executing. |
| 229 | static final int SERVICE_TIMEOUT = 20*1000; |
| 230 | |
| 231 | // How long a service needs to be running until restarting its process |
| 232 | // is no longer considered to be a relaunch of the service. |
| 233 | static final int SERVICE_RESTART_DURATION = 5*1000; |
| 234 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 235 | // How long a service needs to be running until it will start back at |
| 236 | // SERVICE_RESTART_DURATION after being killed. |
| 237 | static final int SERVICE_RESET_RUN_DURATION = 60*1000; |
| 238 | |
| 239 | // Multiplying factor to increase restart duration time by, for each time |
| 240 | // a service is killed before it has run for SERVICE_RESET_RUN_DURATION. |
| 241 | static final int SERVICE_RESTART_DURATION_FACTOR = 4; |
| 242 | |
| 243 | // The minimum amount of time between restarting services that we allow. |
| 244 | // That is, when multiple services are restarting, we won't allow each |
| 245 | // to restart less than this amount of time from the last one. |
| 246 | static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000; |
| 247 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | // Maximum amount of time for there to be no activity on a service before |
| 249 | // we consider it non-essential and allow its process to go on the |
| 250 | // LRU background list. |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 251 | static final int MAX_SERVICE_INACTIVITY = 30*60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | |
| 253 | // How long we wait until we timeout on key dispatching. |
| 254 | static final int KEY_DISPATCHING_TIMEOUT = 5*1000; |
| 255 | |
| 256 | // The minimum time we allow between crashes, for us to consider this |
| 257 | // application to be bad and stop and its services and reject broadcasts. |
| 258 | static final int MIN_CRASH_INTERVAL = 60*1000; |
| 259 | |
| 260 | // How long we wait until we timeout on key dispatching during instrumentation. |
| 261 | static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000; |
| 262 | |
| 263 | // OOM adjustments for processes in various states: |
| 264 | |
| 265 | // This is a process without anything currently running in it. Definitely |
| 266 | // the first to go! Value set in system/rootdir/init.rc on startup. |
| 267 | // This value is initalized in the constructor, careful when refering to |
| 268 | // this static variable externally. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 269 | static final int EMPTY_APP_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | |
| 271 | // This is a process only hosting activities that are not visible, |
| 272 | // so it can be killed without any disruption. Value set in |
| 273 | // system/rootdir/init.rc on startup. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 274 | static final int HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | static int HIDDEN_APP_MIN_ADJ; |
| 276 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 277 | // This is a process holding the home application -- we want to try |
| 278 | // avoiding killing it, even if it would normally be in the background, |
| 279 | // because the user interacts with it so much. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 280 | static final int HOME_APP_ADJ; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 281 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 282 | // This is a process currently hosting a backup operation. Killing it |
| 283 | // is not entirely fatal but is generally a bad idea. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 284 | static final int BACKUP_APP_ADJ; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 285 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | // This is a process holding a secondary server -- killing it will not |
| 287 | // have much of an impact as far as the user is concerned. Value set in |
| 288 | // system/rootdir/init.rc on startup. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 289 | static final int SECONDARY_SERVER_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 291 | // This is a process with a heavy-weight application. It is in the |
| 292 | // background, but we want to try to avoid killing it. Value set in |
| 293 | // system/rootdir/init.rc on startup. |
| 294 | static final int HEAVY_WEIGHT_APP_ADJ; |
| 295 | |
| 296 | // This is a process only hosting components that are perceptible to the |
| 297 | // user, and we really want to avoid killing them, but they are not |
| 298 | // immediately visible. An example is background music playback. Value set in |
| 299 | // system/rootdir/init.rc on startup. |
| 300 | static final int PERCEPTIBLE_APP_ADJ; |
| 301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | // This is a process only hosting activities that are visible to the |
| 303 | // user, so we'd prefer they don't disappear. Value set in |
| 304 | // system/rootdir/init.rc on startup. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 305 | static final int VISIBLE_APP_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | |
| 307 | // This is the process running the current foreground app. We'd really |
| 308 | // rather not kill it! Value set in system/rootdir/init.rc on startup. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 309 | static final int FOREGROUND_APP_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | |
| 311 | // This is a process running a core server, such as telephony. Definitely |
| 312 | // don't want to kill it, but doing so is not completely fatal. |
| 313 | static final int CORE_SERVER_ADJ = -12; |
| 314 | |
| 315 | // The system process runs at the default adjustment. |
| 316 | static final int SYSTEM_ADJ = -16; |
| 317 | |
| 318 | // Memory pages are 4K. |
| 319 | static final int PAGE_SIZE = 4*1024; |
| 320 | |
| 321 | // Corresponding memory levels for above adjustments. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 322 | static final int EMPTY_APP_MEM; |
| 323 | static final int HIDDEN_APP_MEM; |
| 324 | static final int HOME_APP_MEM; |
| 325 | static final int BACKUP_APP_MEM; |
| 326 | static final int SECONDARY_SERVER_MEM; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 327 | static final int HEAVY_WEIGHT_APP_MEM; |
| 328 | static final int PERCEPTIBLE_APP_MEM; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 329 | static final int VISIBLE_APP_MEM; |
| 330 | static final int FOREGROUND_APP_MEM; |
| 331 | |
| 332 | // The minimum number of hidden apps we want to be able to keep around, |
| 333 | // without empty apps being able to push them out of memory. |
| 334 | static final int MIN_HIDDEN_APPS = 2; |
| 335 | |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 336 | // The maximum number of hidden processes we will keep around before |
| 337 | // killing them; this is just a control to not let us go too crazy with |
| 338 | // keeping around processes on devices with large amounts of RAM. |
| 339 | static final int MAX_HIDDEN_APPS = 15; |
| 340 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 341 | // We put empty content processes after any hidden processes that have |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 342 | // been idle for less than 15 seconds. |
| 343 | static final long CONTENT_APP_IDLE_OFFSET = 15*1000; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 344 | |
| 345 | // We put empty content processes after any hidden processes that have |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 346 | // been idle for less than 120 seconds. |
| 347 | static final long EMPTY_APP_IDLE_OFFSET = 120*1000; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 348 | |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 349 | static int getIntProp(String name, boolean allowZero) { |
| 350 | String str = SystemProperties.get(name); |
| 351 | if (str == null) { |
| 352 | throw new IllegalArgumentException("Property not defined: " + name); |
| 353 | } |
| 354 | int val = Integer.valueOf(str); |
| 355 | if (val == 0 && !allowZero) { |
| 356 | throw new IllegalArgumentException("Property must not be zero: " + name); |
| 357 | } |
| 358 | return val; |
| 359 | } |
| 360 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 361 | static { |
| 362 | // These values are set in system/rootdir/init.rc on startup. |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 363 | FOREGROUND_APP_ADJ = getIntProp("ro.FOREGROUND_APP_ADJ", true); |
| 364 | VISIBLE_APP_ADJ = getIntProp("ro.VISIBLE_APP_ADJ", true); |
| 365 | PERCEPTIBLE_APP_ADJ = getIntProp("ro.PERCEPTIBLE_APP_ADJ", true); |
| 366 | HEAVY_WEIGHT_APP_ADJ = getIntProp("ro.HEAVY_WEIGHT_APP_ADJ", true); |
| 367 | SECONDARY_SERVER_ADJ = getIntProp("ro.SECONDARY_SERVER_ADJ", true); |
| 368 | BACKUP_APP_ADJ = getIntProp("ro.BACKUP_APP_ADJ", true); |
| 369 | HOME_APP_ADJ = getIntProp("ro.HOME_APP_ADJ", true); |
| 370 | HIDDEN_APP_MIN_ADJ = getIntProp("ro.HIDDEN_APP_MIN_ADJ", true); |
| 371 | EMPTY_APP_ADJ = getIntProp("ro.EMPTY_APP_ADJ", true); |
| 372 | // These days we use the last empty slot for hidden apps as well. |
| 373 | HIDDEN_APP_MAX_ADJ = EMPTY_APP_ADJ; |
| 374 | FOREGROUND_APP_MEM = getIntProp("ro.FOREGROUND_APP_MEM", false)*PAGE_SIZE; |
| 375 | VISIBLE_APP_MEM = getIntProp("ro.VISIBLE_APP_MEM", false)*PAGE_SIZE; |
| 376 | PERCEPTIBLE_APP_MEM = getIntProp("ro.PERCEPTIBLE_APP_MEM", false)*PAGE_SIZE; |
| 377 | HEAVY_WEIGHT_APP_MEM = getIntProp("ro.HEAVY_WEIGHT_APP_MEM", false)*PAGE_SIZE; |
| 378 | SECONDARY_SERVER_MEM = getIntProp("ro.SECONDARY_SERVER_MEM", false)*PAGE_SIZE; |
| 379 | BACKUP_APP_MEM = getIntProp("ro.BACKUP_APP_MEM", false)*PAGE_SIZE; |
| 380 | HOME_APP_MEM = getIntProp("ro.HOME_APP_MEM", false)*PAGE_SIZE; |
| 381 | HIDDEN_APP_MEM = getIntProp("ro.HIDDEN_APP_MEM", false)*PAGE_SIZE; |
| 382 | EMPTY_APP_MEM = getIntProp("ro.EMPTY_APP_MEM", false)*PAGE_SIZE; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 383 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 385 | static final int MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | |
| 387 | static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 388 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 389 | public ActivityStack mMainStack; |
| 390 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 392 | * Description of a request to start a new activity, which has been held |
| 393 | * due to app switches being disabled. |
| 394 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 395 | static class PendingActivityLaunch { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 396 | ActivityRecord r; |
| 397 | ActivityRecord sourceRecord; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 398 | Uri[] grantedUriPermissions; |
| 399 | int grantedMode; |
| 400 | boolean onlyIfNeeded; |
| 401 | } |
| 402 | |
| 403 | final ArrayList<PendingActivityLaunch> mPendingActivityLaunches |
| 404 | = new ArrayList<PendingActivityLaunch>(); |
| 405 | |
| 406 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | * List of all active broadcasts that are to be executed immediately |
| 408 | * (without waiting for another broadcast to finish). Currently this only |
| 409 | * contains broadcasts to registered receivers, to avoid spinning up |
| 410 | * a bunch of processes to execute IntentReceiver components. |
| 411 | */ |
| 412 | final ArrayList<BroadcastRecord> mParallelBroadcasts |
| 413 | = new ArrayList<BroadcastRecord>(); |
| 414 | |
| 415 | /** |
| 416 | * List of all active broadcasts that are to be executed one at a time. |
| 417 | * The object at the top of the list is the currently activity broadcasts; |
| 418 | * those after it are waiting for the top to finish.. |
| 419 | */ |
| 420 | final ArrayList<BroadcastRecord> mOrderedBroadcasts |
| 421 | = new ArrayList<BroadcastRecord>(); |
| 422 | |
| 423 | /** |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 424 | * Historical data of past broadcasts, for debugging. |
| 425 | */ |
| 426 | static final int MAX_BROADCAST_HISTORY = 100; |
| 427 | final BroadcastRecord[] mBroadcastHistory |
| 428 | = new BroadcastRecord[MAX_BROADCAST_HISTORY]; |
| 429 | |
| 430 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | * Set when we current have a BROADCAST_INTENT_MSG in flight. |
| 432 | */ |
| 433 | boolean mBroadcastsScheduled = false; |
| 434 | |
| 435 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | * Activity we have told the window manager to have key focus. |
| 437 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 438 | ActivityRecord mFocusedActivity = null; |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 439 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | * List of intents that were used to start the most recent tasks. |
| 441 | */ |
| 442 | final ArrayList<TaskRecord> mRecentTasks |
| 443 | = new ArrayList<TaskRecord>(); |
| 444 | |
| 445 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | * All of the applications we currently have running organized by name. |
| 447 | * The keys are strings of the application package name (as |
| 448 | * returned by the package manager), and the keys are ApplicationRecord |
| 449 | * objects. |
| 450 | */ |
| 451 | final ProcessMap<ProcessRecord> mProcessNames |
| 452 | = new ProcessMap<ProcessRecord>(); |
| 453 | |
| 454 | /** |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 455 | * The currently running heavy-weight process, if any. |
| 456 | */ |
| 457 | ProcessRecord mHeavyWeightProcess = null; |
| 458 | |
| 459 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | * The last time that various processes have crashed. |
| 461 | */ |
| 462 | final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>(); |
| 463 | |
| 464 | /** |
| 465 | * Set of applications that we consider to be bad, and will reject |
| 466 | * incoming broadcasts from (which the user has no control over). |
| 467 | * Processes are added to this set when they have crashed twice within |
| 468 | * a minimum amount of time; they are removed from it when they are |
| 469 | * later restarted (hopefully due to some user action). The value is the |
| 470 | * time it was added to the list. |
| 471 | */ |
| 472 | final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>(); |
| 473 | |
| 474 | /** |
| 475 | * All of the processes we currently have running organized by pid. |
| 476 | * The keys are the pid running the application. |
| 477 | * |
| 478 | * <p>NOTE: This object is protected by its own lock, NOT the global |
| 479 | * activity manager lock! |
| 480 | */ |
| 481 | final SparseArray<ProcessRecord> mPidsSelfLocked |
| 482 | = new SparseArray<ProcessRecord>(); |
| 483 | |
| 484 | /** |
| 485 | * All of the processes that have been forced to be foreground. The key |
| 486 | * is the pid of the caller who requested it (we hold a death |
| 487 | * link on it). |
| 488 | */ |
| 489 | abstract class ForegroundToken implements IBinder.DeathRecipient { |
| 490 | int pid; |
| 491 | IBinder token; |
| 492 | } |
| 493 | final SparseArray<ForegroundToken> mForegroundProcesses |
| 494 | = new SparseArray<ForegroundToken>(); |
| 495 | |
| 496 | /** |
| 497 | * List of records for processes that someone had tried to start before the |
| 498 | * system was ready. We don't start them at that point, but ensure they |
| 499 | * are started by the time booting is complete. |
| 500 | */ |
| 501 | final ArrayList<ProcessRecord> mProcessesOnHold |
| 502 | = new ArrayList<ProcessRecord>(); |
| 503 | |
| 504 | /** |
| 505 | * List of records for processes that we have started and are waiting |
| 506 | * for them to call back. This is really only needed when running in |
| 507 | * single processes mode, in which case we do not have a unique pid for |
| 508 | * each process. |
| 509 | */ |
| 510 | final ArrayList<ProcessRecord> mStartingProcesses |
| 511 | = new ArrayList<ProcessRecord>(); |
| 512 | |
| 513 | /** |
| 514 | * List of persistent applications that are in the process |
| 515 | * of being started. |
| 516 | */ |
| 517 | final ArrayList<ProcessRecord> mPersistentStartingProcesses |
| 518 | = new ArrayList<ProcessRecord>(); |
| 519 | |
| 520 | /** |
| 521 | * Processes that are being forcibly torn down. |
| 522 | */ |
| 523 | final ArrayList<ProcessRecord> mRemovedProcesses |
| 524 | = new ArrayList<ProcessRecord>(); |
| 525 | |
| 526 | /** |
| 527 | * List of running applications, sorted by recent usage. |
| 528 | * The first entry in the list is the least recently used. |
| 529 | * It contains ApplicationRecord objects. This list does NOT include |
| 530 | * any persistent application records (since we never want to exit them). |
| 531 | */ |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 532 | final ArrayList<ProcessRecord> mLruProcesses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | = new ArrayList<ProcessRecord>(); |
| 534 | |
| 535 | /** |
| 536 | * List of processes that should gc as soon as things are idle. |
| 537 | */ |
| 538 | final ArrayList<ProcessRecord> mProcessesToGc |
| 539 | = new ArrayList<ProcessRecord>(); |
| 540 | |
| 541 | /** |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 542 | * This is the process holding what we currently consider to be |
| 543 | * the "home" activity. |
| 544 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 545 | ProcessRecord mHomeProcess; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 546 | |
| 547 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | * Set of PendingResultRecord objects that are currently active. |
| 549 | */ |
| 550 | final HashSet mPendingResultRecords = new HashSet(); |
| 551 | |
| 552 | /** |
| 553 | * Set of IntentSenderRecord objects that are currently active. |
| 554 | */ |
| 555 | final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords |
| 556 | = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>(); |
| 557 | |
| 558 | /** |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 559 | * Fingerprints (hashCode()) of stack traces that we've |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 560 | * already logged DropBox entries for. Guarded by itself. If |
| 561 | * something (rogue user app) forces this over |
| 562 | * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared. |
| 563 | */ |
| 564 | private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>(); |
| 565 | private static final int MAX_DUP_SUPPRESSED_STACKS = 5000; |
| 566 | |
| 567 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 568 | * Strict Mode background batched logging state. |
| 569 | * |
| 570 | * The string buffer is guarded by itself, and its lock is also |
| 571 | * used to determine if another batched write is already |
| 572 | * in-flight. |
| 573 | */ |
| 574 | private final StringBuilder mStrictModeBuffer = new StringBuilder(); |
| 575 | |
| 576 | /** |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 577 | * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler. |
| 578 | */ |
| 579 | private boolean mPendingBroadcastTimeoutMessage; |
| 580 | |
| 581 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | * Intent broadcast that we have tried to start, but are |
| 583 | * waiting for its application's process to be created. We only |
| 584 | * need one (instead of a list) because we always process broadcasts |
| 585 | * one at a time, so no others can be started while waiting for this |
| 586 | * one. |
| 587 | */ |
| 588 | BroadcastRecord mPendingBroadcast = null; |
| 589 | |
| 590 | /** |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 591 | * The receiver index that is pending, to restart the broadcast if needed. |
| 592 | */ |
| 593 | int mPendingBroadcastRecvIndex; |
| 594 | |
| 595 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | * Keeps track of all IIntentReceivers that have been registered for |
| 597 | * broadcasts. Hash keys are the receiver IBinder, hash value is |
| 598 | * a ReceiverList. |
| 599 | */ |
| 600 | final HashMap mRegisteredReceivers = new HashMap(); |
| 601 | |
| 602 | /** |
| 603 | * Resolver for broadcast intents to registered receivers. |
| 604 | * Holds BroadcastFilter (subclass of IntentFilter). |
| 605 | */ |
| 606 | final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver |
| 607 | = new IntentResolver<BroadcastFilter, BroadcastFilter>() { |
| 608 | @Override |
| 609 | protected boolean allowFilterResult( |
| 610 | BroadcastFilter filter, List<BroadcastFilter> dest) { |
| 611 | IBinder target = filter.receiverList.receiver.asBinder(); |
| 612 | for (int i=dest.size()-1; i>=0; i--) { |
| 613 | if (dest.get(i).receiverList.receiver.asBinder() == target) { |
| 614 | return false; |
| 615 | } |
| 616 | } |
| 617 | return true; |
| 618 | } |
| 619 | }; |
| 620 | |
| 621 | /** |
| 622 | * State of all active sticky broadcasts. Keys are the action of the |
| 623 | * sticky Intent, values are an ArrayList of all broadcasted intents with |
| 624 | * that action (which should usually be one). |
| 625 | */ |
| 626 | final HashMap<String, ArrayList<Intent>> mStickyBroadcasts = |
| 627 | new HashMap<String, ArrayList<Intent>>(); |
| 628 | |
| 629 | /** |
| 630 | * All currently running services. |
| 631 | */ |
| 632 | final HashMap<ComponentName, ServiceRecord> mServices = |
| 633 | new HashMap<ComponentName, ServiceRecord>(); |
| 634 | |
| 635 | /** |
| 636 | * All currently running services indexed by the Intent used to start them. |
| 637 | */ |
| 638 | final HashMap<Intent.FilterComparison, ServiceRecord> mServicesByIntent = |
| 639 | new HashMap<Intent.FilterComparison, ServiceRecord>(); |
| 640 | |
| 641 | /** |
| 642 | * All currently bound service connections. Keys are the IBinder of |
| 643 | * the client's IServiceConnection. |
| 644 | */ |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 645 | final HashMap<IBinder, ArrayList<ConnectionRecord>> mServiceConnections |
| 646 | = new HashMap<IBinder, ArrayList<ConnectionRecord>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | |
| 648 | /** |
| 649 | * List of services that we have been asked to start, |
| 650 | * but haven't yet been able to. It is used to hold start requests |
| 651 | * while waiting for their corresponding application thread to get |
| 652 | * going. |
| 653 | */ |
| 654 | final ArrayList<ServiceRecord> mPendingServices |
| 655 | = new ArrayList<ServiceRecord>(); |
| 656 | |
| 657 | /** |
| 658 | * List of services that are scheduled to restart following a crash. |
| 659 | */ |
| 660 | final ArrayList<ServiceRecord> mRestartingServices |
| 661 | = new ArrayList<ServiceRecord>(); |
| 662 | |
| 663 | /** |
| 664 | * List of services that are in the process of being stopped. |
| 665 | */ |
| 666 | final ArrayList<ServiceRecord> mStoppingServices |
| 667 | = new ArrayList<ServiceRecord>(); |
| 668 | |
| 669 | /** |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 670 | * Backup/restore process management |
| 671 | */ |
| 672 | String mBackupAppName = null; |
| 673 | BackupRecord mBackupTarget = null; |
| 674 | |
| 675 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | * List of PendingThumbnailsRecord objects of clients who are still |
| 677 | * waiting to receive all of the thumbnails for a task. |
| 678 | */ |
| 679 | final ArrayList mPendingThumbnails = new ArrayList(); |
| 680 | |
| 681 | /** |
| 682 | * List of HistoryRecord objects that have been finished and must |
| 683 | * still report back to a pending thumbnail receiver. |
| 684 | */ |
| 685 | final ArrayList mCancelledThumbnails = new ArrayList(); |
| 686 | |
| 687 | /** |
| 688 | * All of the currently running global content providers. Keys are a |
| 689 | * string containing the provider name and values are a |
| 690 | * ContentProviderRecord object containing the data about it. Note |
| 691 | * that a single provider may be published under multiple names, so |
| 692 | * there may be multiple entries here for a single one in mProvidersByClass. |
| 693 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 694 | final HashMap<String, ContentProviderRecord> mProvidersByName |
| 695 | = new HashMap<String, ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | |
| 697 | /** |
| 698 | * All of the currently running global content providers. Keys are a |
| 699 | * string containing the provider's implementation class and values are a |
| 700 | * ContentProviderRecord object containing the data about it. |
| 701 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 702 | final HashMap<String, ContentProviderRecord> mProvidersByClass |
| 703 | = new HashMap<String, ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | |
| 705 | /** |
| 706 | * List of content providers who have clients waiting for them. The |
| 707 | * application is currently being launched and the provider will be |
| 708 | * removed from this list once it is published. |
| 709 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 710 | final ArrayList<ContentProviderRecord> mLaunchingProviders |
| 711 | = new ArrayList<ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | |
| 713 | /** |
| 714 | * Global set of specific Uri permissions that have been granted. |
| 715 | */ |
| 716 | final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions |
| 717 | = new SparseArray<HashMap<Uri, UriPermission>>(); |
| 718 | |
| 719 | /** |
| 720 | * Thread-local storage used to carry caller permissions over through |
| 721 | * indirect content-provider access. |
| 722 | * @see #ActivityManagerService.openContentUri() |
| 723 | */ |
| 724 | private class Identity { |
| 725 | public int pid; |
| 726 | public int uid; |
| 727 | |
| 728 | Identity(int _pid, int _uid) { |
| 729 | pid = _pid; |
| 730 | uid = _uid; |
| 731 | } |
| 732 | } |
| 733 | private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>(); |
| 734 | |
| 735 | /** |
| 736 | * All information we have collected about the runtime performance of |
| 737 | * any user id that can impact battery performance. |
| 738 | */ |
| 739 | final BatteryStatsService mBatteryStatsService; |
| 740 | |
| 741 | /** |
| 742 | * information about component usage |
| 743 | */ |
| 744 | final UsageStatsService mUsageStatsService; |
| 745 | |
| 746 | /** |
| 747 | * Current configuration information. HistoryRecord objects are given |
| 748 | * a reference to this object to indicate which configuration they are |
| 749 | * currently running in, so this object must be kept immutable. |
| 750 | */ |
| 751 | Configuration mConfiguration = new Configuration(); |
| 752 | |
| 753 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 754 | * Current sequencing integer of the configuration, for skipping old |
| 755 | * configurations. |
| 756 | */ |
| 757 | int mConfigurationSeq = 0; |
| 758 | |
| 759 | /** |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 760 | * Hardware-reported OpenGLES version. |
| 761 | */ |
| 762 | final int GL_ES_VERSION; |
| 763 | |
| 764 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | * List of initialization arguments to pass to all processes when binding applications to them. |
| 766 | * For example, references to the commonly used services. |
| 767 | */ |
| 768 | HashMap<String, IBinder> mAppBindArgs; |
| 769 | |
| 770 | /** |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 771 | * Temporary to avoid allocations. Protected by main lock. |
| 772 | */ |
| 773 | final StringBuilder mStringBuilder = new StringBuilder(256); |
| 774 | |
| 775 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | * Used to control how we initialize the service. |
| 777 | */ |
| 778 | boolean mStartRunning = false; |
| 779 | ComponentName mTopComponent; |
| 780 | String mTopAction; |
| 781 | String mTopData; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 782 | boolean mProcessesReady = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | boolean mSystemReady = false; |
| 784 | boolean mBooting = false; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 785 | boolean mWaitingUpdate = false; |
| 786 | boolean mDidUpdate = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 787 | boolean mOnBattery = false; |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 788 | boolean mLaunchWarningShown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | |
| 790 | Context mContext; |
| 791 | |
| 792 | int mFactoryTest; |
| 793 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 794 | boolean mCheckedForSetup; |
| 795 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 797 | * The time at which we will allow normal application switches again, |
| 798 | * after a call to {@link #stopAppSwitches()}. |
| 799 | */ |
| 800 | long mAppSwitchesAllowedTime; |
| 801 | |
| 802 | /** |
| 803 | * This is set to true after the first switch after mAppSwitchesAllowedTime |
| 804 | * is set; any switches after that will clear the time. |
| 805 | */ |
| 806 | boolean mDidAppSwitch; |
| 807 | |
| 808 | /** |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 809 | * Last time (in realtime) at which we checked for power usage. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 810 | */ |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 811 | long mLastPowerCheckRealtime; |
| 812 | |
| 813 | /** |
| 814 | * Last time (in uptime) at which we checked for power usage. |
| 815 | */ |
| 816 | long mLastPowerCheckUptime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 817 | |
| 818 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | * Set while we are wanting to sleep, to prevent any |
| 820 | * activities from being started/resumed. |
| 821 | */ |
| 822 | boolean mSleeping = false; |
| 823 | |
| 824 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 825 | * Set if we are shutting down the system, similar to sleeping. |
| 826 | */ |
| 827 | boolean mShuttingDown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | |
| 829 | /** |
| 830 | * Task identifier that activities are currently being started |
| 831 | * in. Incremented each time a new task is created. |
| 832 | * todo: Replace this with a TokenSpace class that generates non-repeating |
| 833 | * integers that won't wrap. |
| 834 | */ |
| 835 | int mCurTask = 1; |
| 836 | |
| 837 | /** |
| 838 | * Current sequence id for oom_adj computation traversal. |
| 839 | */ |
| 840 | int mAdjSeq = 0; |
| 841 | |
| 842 | /** |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 843 | * Current sequence id for process LRU updating. |
| 844 | */ |
| 845 | int mLruSeq = 0; |
| 846 | |
| 847 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | * Set to true if the ANDROID_SIMPLE_PROCESS_MANAGEMENT envvar |
| 849 | * is set, indicating the user wants processes started in such a way |
| 850 | * that they can use ANDROID_PROCESS_WRAPPER and know what will be |
| 851 | * running in each process (thus no pre-initialized process, etc). |
| 852 | */ |
| 853 | boolean mSimpleProcessManagement = false; |
| 854 | |
| 855 | /** |
| 856 | * System monitoring: number of processes that died since the last |
| 857 | * N procs were started. |
| 858 | */ |
| 859 | int[] mProcDeaths = new int[20]; |
| 860 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 861 | /** |
| 862 | * This is set if we had to do a delayed dexopt of an app before launching |
| 863 | * it, to increasing the ANR timeouts in that case. |
| 864 | */ |
| 865 | boolean mDidDexOpt; |
| 866 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | String mDebugApp = null; |
| 868 | boolean mWaitForDebugger = false; |
| 869 | boolean mDebugTransient = false; |
| 870 | String mOrigDebugApp = null; |
| 871 | boolean mOrigWaitForDebugger = false; |
| 872 | boolean mAlwaysFinishActivities = false; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 873 | IActivityController mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 875 | final RemoteCallbackList<IActivityWatcher> mWatchers |
| 876 | = new RemoteCallbackList<IActivityWatcher>(); |
| 877 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | /** |
| 879 | * Callback of last caller to {@link #requestPss}. |
| 880 | */ |
| 881 | Runnable mRequestPssCallback; |
| 882 | |
| 883 | /** |
| 884 | * Remaining processes for which we are waiting results from the last |
| 885 | * call to {@link #requestPss}. |
| 886 | */ |
| 887 | final ArrayList<ProcessRecord> mRequestPssList |
| 888 | = new ArrayList<ProcessRecord>(); |
| 889 | |
| 890 | /** |
| 891 | * Runtime statistics collection thread. This object's lock is used to |
| 892 | * protect all related state. |
| 893 | */ |
| 894 | final Thread mProcessStatsThread; |
| 895 | |
| 896 | /** |
| 897 | * Used to collect process stats when showing not responding dialog. |
| 898 | * Protected by mProcessStatsThread. |
| 899 | */ |
| 900 | final ProcessStats mProcessStats = new ProcessStats( |
| 901 | MONITOR_THREAD_CPU_USAGE); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 902 | final AtomicLong mLastCpuTime = new AtomicLong(0); |
| 903 | final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true); |
| 904 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | long mLastWriteTime = 0; |
| 906 | |
| 907 | /** |
| 908 | * Set to true after the system has finished booting. |
| 909 | */ |
| 910 | boolean mBooted = false; |
| 911 | |
| 912 | int mProcessLimit = 0; |
| 913 | |
| 914 | WindowManagerService mWindowManager; |
| 915 | |
| 916 | static ActivityManagerService mSelf; |
| 917 | static ActivityThread mSystemThread; |
| 918 | |
| 919 | private final class AppDeathRecipient implements IBinder.DeathRecipient { |
| 920 | final ProcessRecord mApp; |
| 921 | final int mPid; |
| 922 | final IApplicationThread mAppThread; |
| 923 | |
| 924 | AppDeathRecipient(ProcessRecord app, int pid, |
| 925 | IApplicationThread thread) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 926 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | TAG, "New death recipient " + this |
| 928 | + " for thread " + thread.asBinder()); |
| 929 | mApp = app; |
| 930 | mPid = pid; |
| 931 | mAppThread = thread; |
| 932 | } |
| 933 | |
| 934 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 935 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | TAG, "Death received in " + this |
| 937 | + " for thread " + mAppThread.asBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | synchronized(ActivityManagerService.this) { |
| 939 | appDiedLocked(mApp, mPid, mAppThread); |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | static final int SHOW_ERROR_MSG = 1; |
| 945 | static final int SHOW_NOT_RESPONDING_MSG = 2; |
| 946 | static final int SHOW_FACTORY_ERROR_MSG = 3; |
| 947 | static final int UPDATE_CONFIGURATION_MSG = 4; |
| 948 | static final int GC_BACKGROUND_PROCESSES_MSG = 5; |
| 949 | static final int WAIT_FOR_DEBUGGER_MSG = 6; |
| 950 | static final int BROADCAST_INTENT_MSG = 7; |
| 951 | static final int BROADCAST_TIMEOUT_MSG = 8; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | static final int SERVICE_TIMEOUT_MSG = 12; |
| 953 | static final int UPDATE_TIME_ZONE = 13; |
| 954 | static final int SHOW_UID_ERROR_MSG = 14; |
| 955 | static final int IM_FEELING_LUCKY_MSG = 15; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | static final int PROC_START_TIMEOUT_MSG = 20; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 957 | static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 958 | static final int KILL_APPLICATION_MSG = 22; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 959 | static final int FINALIZE_PENDING_INTENT_MSG = 23; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 960 | static final int POST_HEAVY_NOTIFICATION_MSG = 24; |
| 961 | static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 962 | static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 963 | static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 964 | static final int CLEAR_DNS_CACHE = 28; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 965 | static final int UPDATE_HTTP_PROXY = 29; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 966 | |
| 967 | AlertDialog mUidAlert; |
| 968 | |
| 969 | final Handler mHandler = new Handler() { |
| 970 | //public Handler() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 971 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 972 | //} |
| 973 | |
| 974 | public void handleMessage(Message msg) { |
| 975 | switch (msg.what) { |
| 976 | case SHOW_ERROR_MSG: { |
| 977 | HashMap data = (HashMap) msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | synchronized (ActivityManagerService.this) { |
| 979 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 980 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 981 | Slog.e(TAG, "App already has crash dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | return; |
| 983 | } |
| 984 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 985 | if (!mSleeping && !mShuttingDown) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 986 | Dialog d = new AppErrorDialog(mContext, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | d.show(); |
| 988 | proc.crashDialog = d; |
| 989 | } else { |
| 990 | // The device is asleep, so just pretend that the user |
| 991 | // saw a crash dialog and hit "force quit". |
| 992 | res.set(0); |
| 993 | } |
| 994 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 995 | |
| 996 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | } break; |
| 998 | case SHOW_NOT_RESPONDING_MSG: { |
| 999 | synchronized (ActivityManagerService.this) { |
| 1000 | HashMap data = (HashMap) msg.obj; |
| 1001 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 1002 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1003 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | return; |
| 1005 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1006 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1007 | Intent intent = new Intent("android.intent.action.ANR"); |
| 1008 | if (!mProcessesReady) { |
| 1009 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 1010 | } |
| 1011 | broadcastIntentLocked(null, null, intent, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1012 | null, null, 0, null, null, null, |
| 1013 | false, false, MY_PID, Process.SYSTEM_UID); |
| 1014 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1016 | mContext, proc, (ActivityRecord)data.get("activity")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1017 | d.show(); |
| 1018 | proc.anrDialog = d; |
| 1019 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1020 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1021 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1023 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 1024 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 1025 | synchronized (ActivityManagerService.this) { |
| 1026 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 1027 | if (proc == null) { |
| 1028 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 1029 | break; |
| 1030 | } |
| 1031 | if (proc.crashDialog != null) { |
| 1032 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 1033 | return; |
| 1034 | } |
| 1035 | AppErrorResult res = (AppErrorResult) data.get("result"); |
| 1036 | if (!mSleeping && !mShuttingDown) { |
| 1037 | Dialog d = new StrictModeViolationDialog(mContext, res, proc); |
| 1038 | d.show(); |
| 1039 | proc.crashDialog = d; |
| 1040 | } else { |
| 1041 | // The device is asleep, so just pretend that the user |
| 1042 | // saw a crash dialog and hit "force quit". |
| 1043 | res.set(0); |
| 1044 | } |
| 1045 | } |
| 1046 | ensureBootCompleted(); |
| 1047 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | case SHOW_FACTORY_ERROR_MSG: { |
| 1049 | Dialog d = new FactoryErrorDialog( |
| 1050 | mContext, msg.getData().getCharSequence("msg")); |
| 1051 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1052 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | } break; |
| 1054 | case UPDATE_CONFIGURATION_MSG: { |
| 1055 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1056 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 1057 | } break; |
| 1058 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 1059 | synchronized (ActivityManagerService.this) { |
| 1060 | performAppGcsIfAppropriateLocked(); |
| 1061 | } |
| 1062 | } break; |
| 1063 | case WAIT_FOR_DEBUGGER_MSG: { |
| 1064 | synchronized (ActivityManagerService.this) { |
| 1065 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1066 | if (msg.arg1 != 0) { |
| 1067 | if (!app.waitedForDebugger) { |
| 1068 | Dialog d = new AppWaitingForDebuggerDialog( |
| 1069 | ActivityManagerService.this, |
| 1070 | mContext, app); |
| 1071 | app.waitDialog = d; |
| 1072 | app.waitedForDebugger = true; |
| 1073 | d.show(); |
| 1074 | } |
| 1075 | } else { |
| 1076 | if (app.waitDialog != null) { |
| 1077 | app.waitDialog.dismiss(); |
| 1078 | app.waitDialog = null; |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | } break; |
| 1083 | case BROADCAST_INTENT_MSG: { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1084 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | TAG, "Received BROADCAST_INTENT_MSG"); |
| 1086 | processNextBroadcast(true); |
| 1087 | } break; |
| 1088 | case BROADCAST_TIMEOUT_MSG: { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 1089 | synchronized (ActivityManagerService.this) { |
| 1090 | broadcastTimeoutLocked(true); |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 1091 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1094 | if (mDidDexOpt) { |
| 1095 | mDidDexOpt = false; |
| 1096 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 1097 | nmsg.obj = msg.obj; |
| 1098 | mHandler.sendMessageDelayed(nmsg, SERVICE_TIMEOUT); |
| 1099 | return; |
| 1100 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1101 | serviceTimeout((ProcessRecord)msg.obj); |
| 1102 | } break; |
| 1103 | case UPDATE_TIME_ZONE: { |
| 1104 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1105 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1106 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | if (r.thread != null) { |
| 1108 | try { |
| 1109 | r.thread.updateTimeZone(); |
| 1110 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1111 | 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] | 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1116 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1117 | case CLEAR_DNS_CACHE: { |
| 1118 | synchronized (ActivityManagerService.this) { |
| 1119 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1120 | ProcessRecord r = mLruProcesses.get(i); |
| 1121 | if (r.thread != null) { |
| 1122 | try { |
| 1123 | r.thread.clearDnsCache(); |
| 1124 | } catch (RemoteException ex) { |
| 1125 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1131 | case UPDATE_HTTP_PROXY: { |
| 1132 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 1133 | String host = ""; |
| 1134 | String port = ""; |
| 1135 | String exclList = ""; |
| 1136 | if (proxy != null) { |
| 1137 | host = proxy.getHost(); |
| 1138 | port = Integer.toString(proxy.getPort()); |
| 1139 | exclList = proxy.getExclusionList(); |
| 1140 | } |
| 1141 | synchronized (ActivityManagerService.this) { |
| 1142 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1143 | ProcessRecord r = mLruProcesses.get(i); |
| 1144 | if (r.thread != null) { |
| 1145 | try { |
| 1146 | r.thread.setHttpProxy(host, port, exclList); |
| 1147 | } catch (RemoteException ex) { |
| 1148 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1149 | r.info.processName); |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1155 | case SHOW_UID_ERROR_MSG: { |
| 1156 | // XXX This is a temporary dialog, no need to localize. |
| 1157 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1158 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1159 | d.setCancelable(false); |
| 1160 | d.setTitle("System UIDs Inconsistent"); |
| 1161 | 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] | 1162 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1164 | mUidAlert = d; |
| 1165 | d.show(); |
| 1166 | } break; |
| 1167 | case IM_FEELING_LUCKY_MSG: { |
| 1168 | if (mUidAlert != null) { |
| 1169 | mUidAlert.dismiss(); |
| 1170 | mUidAlert = null; |
| 1171 | } |
| 1172 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1174 | if (mDidDexOpt) { |
| 1175 | mDidDexOpt = false; |
| 1176 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1177 | nmsg.obj = msg.obj; |
| 1178 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1179 | return; |
| 1180 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1181 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1182 | synchronized (ActivityManagerService.this) { |
| 1183 | processStartTimedOutLocked(app); |
| 1184 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1185 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1186 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1187 | synchronized (ActivityManagerService.this) { |
| 1188 | doPendingActivityLaunchesLocked(true); |
| 1189 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1190 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1191 | case KILL_APPLICATION_MSG: { |
| 1192 | synchronized (ActivityManagerService.this) { |
| 1193 | int uid = msg.arg1; |
| 1194 | boolean restart = (msg.arg2 == 1); |
| 1195 | String pkg = (String) msg.obj; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1196 | forceStopPackageLocked(pkg, uid, restart, false, true); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1197 | } |
| 1198 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1199 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1200 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1201 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1202 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1203 | INotificationManager inm = NotificationManager.getService(); |
| 1204 | if (inm == null) { |
| 1205 | return; |
| 1206 | } |
| 1207 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1208 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1209 | ProcessRecord process = root.app; |
| 1210 | if (process == null) { |
| 1211 | return; |
| 1212 | } |
| 1213 | |
| 1214 | try { |
| 1215 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1216 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1217 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1218 | Notification notification = new Notification(); |
| 1219 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1220 | notification.when = 0; |
| 1221 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1222 | notification.tickerText = text; |
| 1223 | notification.defaults = 0; // please be quiet |
| 1224 | notification.sound = null; |
| 1225 | notification.vibrate = null; |
| 1226 | notification.setLatestEventInfo(context, text, |
| 1227 | mContext.getText(R.string.heavy_weight_notification_detail), |
| 1228 | PendingIntent.getActivity(mContext, 0, root.intent, |
| 1229 | PendingIntent.FLAG_CANCEL_CURRENT)); |
| 1230 | |
| 1231 | try { |
| 1232 | int[] outId = new int[1]; |
| 1233 | inm.enqueueNotification("android", R.string.heavy_weight_notification, |
| 1234 | notification, outId); |
| 1235 | } catch (RuntimeException e) { |
| 1236 | Slog.w(ActivityManagerService.TAG, |
| 1237 | "Error showing notification for heavy-weight app", e); |
| 1238 | } catch (RemoteException e) { |
| 1239 | } |
| 1240 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1241 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1242 | } |
| 1243 | } break; |
| 1244 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1245 | INotificationManager inm = NotificationManager.getService(); |
| 1246 | if (inm == null) { |
| 1247 | return; |
| 1248 | } |
| 1249 | try { |
| 1250 | inm.cancelNotification("android", |
| 1251 | R.string.heavy_weight_notification); |
| 1252 | } catch (RuntimeException e) { |
| 1253 | Slog.w(ActivityManagerService.TAG, |
| 1254 | "Error canceling notification for service", e); |
| 1255 | } catch (RemoteException e) { |
| 1256 | } |
| 1257 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1258 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1259 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1260 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1261 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1262 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1263 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1264 | } |
| 1265 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1266 | } |
| 1267 | } |
| 1268 | }; |
| 1269 | |
| 1270 | public static void setSystemProcess() { |
| 1271 | try { |
| 1272 | ActivityManagerService m = mSelf; |
| 1273 | |
| 1274 | ServiceManager.addService("activity", m); |
| 1275 | ServiceManager.addService("meminfo", new MemBinder(m)); |
| 1276 | if (MONITOR_CPU_USAGE) { |
| 1277 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1278 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1279 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1280 | |
| 1281 | ApplicationInfo info = |
| 1282 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 1283 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1284 | mSystemThread.installSystemApplicationInfo(info); |
| 1285 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1286 | synchronized (mSelf) { |
| 1287 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1288 | mSystemThread.getApplicationThread(), info, |
| 1289 | info.processName); |
| 1290 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1291 | app.pid = MY_PID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | app.maxAdj = SYSTEM_ADJ; |
| 1293 | mSelf.mProcessNames.put(app.processName, app.info.uid, app); |
| 1294 | synchronized (mSelf.mPidsSelfLocked) { |
| 1295 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1296 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1297 | mSelf.updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | } |
| 1299 | } catch (PackageManager.NameNotFoundException e) { |
| 1300 | throw new RuntimeException( |
| 1301 | "Unable to find android system package", e); |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | public void setWindowManager(WindowManagerService wm) { |
| 1306 | mWindowManager = wm; |
| 1307 | } |
| 1308 | |
| 1309 | public static final Context main(int factoryTest) { |
| 1310 | AThread thr = new AThread(); |
| 1311 | thr.start(); |
| 1312 | |
| 1313 | synchronized (thr) { |
| 1314 | while (thr.mService == null) { |
| 1315 | try { |
| 1316 | thr.wait(); |
| 1317 | } catch (InterruptedException e) { |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | ActivityManagerService m = thr.mService; |
| 1323 | mSelf = m; |
| 1324 | ActivityThread at = ActivityThread.systemMain(); |
| 1325 | mSystemThread = at; |
| 1326 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1327 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1328 | m.mContext = context; |
| 1329 | m.mFactoryTest = factoryTest; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1330 | m.mMainStack = new ActivityStack(m, context, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1331 | |
| 1332 | m.mBatteryStatsService.publish(context); |
| 1333 | m.mUsageStatsService.publish(context); |
| 1334 | |
| 1335 | synchronized (thr) { |
| 1336 | thr.mReady = true; |
| 1337 | thr.notifyAll(); |
| 1338 | } |
| 1339 | |
| 1340 | m.startRunning(null, null, null, null); |
| 1341 | |
| 1342 | return context; |
| 1343 | } |
| 1344 | |
| 1345 | public static ActivityManagerService self() { |
| 1346 | return mSelf; |
| 1347 | } |
| 1348 | |
| 1349 | static class AThread extends Thread { |
| 1350 | ActivityManagerService mService; |
| 1351 | boolean mReady = false; |
| 1352 | |
| 1353 | public AThread() { |
| 1354 | super("ActivityManager"); |
| 1355 | } |
| 1356 | |
| 1357 | public void run() { |
| 1358 | Looper.prepare(); |
| 1359 | |
| 1360 | android.os.Process.setThreadPriority( |
| 1361 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1362 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | |
| 1364 | ActivityManagerService m = new ActivityManagerService(); |
| 1365 | |
| 1366 | synchronized (this) { |
| 1367 | mService = m; |
| 1368 | notifyAll(); |
| 1369 | } |
| 1370 | |
| 1371 | synchronized (this) { |
| 1372 | while (!mReady) { |
| 1373 | try { |
| 1374 | wait(); |
| 1375 | } catch (InterruptedException e) { |
| 1376 | } |
| 1377 | } |
| 1378 | } |
| 1379 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1380 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1381 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1382 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1383 | } |
| 1384 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1385 | Looper.loop(); |
| 1386 | } |
| 1387 | } |
| 1388 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | static class MemBinder extends Binder { |
| 1390 | ActivityManagerService mActivityManagerService; |
| 1391 | MemBinder(ActivityManagerService activityManagerService) { |
| 1392 | mActivityManagerService = activityManagerService; |
| 1393 | } |
| 1394 | |
| 1395 | @Override |
| 1396 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1397 | ActivityManagerService service = mActivityManagerService; |
| 1398 | ArrayList<ProcessRecord> procs; |
| 1399 | synchronized (mActivityManagerService) { |
| 1400 | if (args != null && args.length > 0 |
| 1401 | && args[0].charAt(0) != '-') { |
| 1402 | procs = new ArrayList<ProcessRecord>(); |
| 1403 | int pid = -1; |
| 1404 | try { |
| 1405 | pid = Integer.parseInt(args[0]); |
| 1406 | } catch (NumberFormatException e) { |
| 1407 | |
| 1408 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1409 | for (int i=service.mLruProcesses.size()-1; i>=0; i--) { |
| 1410 | ProcessRecord proc = service.mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1411 | if (proc.pid == pid) { |
| 1412 | procs.add(proc); |
| 1413 | } else if (proc.processName.equals(args[0])) { |
| 1414 | procs.add(proc); |
| 1415 | } |
| 1416 | } |
| 1417 | if (procs.size() <= 0) { |
| 1418 | pw.println("No process found for: " + args[0]); |
| 1419 | return; |
| 1420 | } |
| 1421 | } else { |
Dianne Hackborn | 472ad87 | 2010-04-07 17:31:48 -0700 | [diff] [blame] | 1422 | procs = new ArrayList<ProcessRecord>(service.mLruProcesses); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1423 | } |
| 1424 | } |
| 1425 | dumpApplicationMemoryUsage(fd, pw, procs, " ", args); |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | static class CpuBinder extends Binder { |
| 1430 | ActivityManagerService mActivityManagerService; |
| 1431 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1432 | mActivityManagerService = activityManagerService; |
| 1433 | } |
| 1434 | |
| 1435 | @Override |
| 1436 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1437 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1438 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1439 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1440 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | private ActivityManagerService() { |
| 1446 | String v = System.getenv("ANDROID_SIMPLE_PROCESS_MANAGEMENT"); |
| 1447 | if (v != null && Integer.getInteger(v) != 0) { |
| 1448 | mSimpleProcessManagement = true; |
| 1449 | } |
| 1450 | v = System.getenv("ANDROID_DEBUG_APP"); |
| 1451 | if (v != null) { |
| 1452 | mSimpleProcessManagement = true; |
| 1453 | } |
| 1454 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1455 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1456 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1457 | File dataDir = Environment.getDataDirectory(); |
| 1458 | File systemDir = new File(dataDir, "system"); |
| 1459 | systemDir.mkdirs(); |
| 1460 | mBatteryStatsService = new BatteryStatsService(new File( |
| 1461 | systemDir, "batterystats.bin").toString()); |
| 1462 | mBatteryStatsService.getActiveStatistics().readLocked(); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1463 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1464 | mOnBattery = DEBUG_POWER ? true |
| 1465 | : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1466 | mBatteryStatsService.getActiveStatistics().setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1467 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1468 | mUsageStatsService = new UsageStatsService(new File( |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1469 | systemDir, "usagestats").toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1470 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1471 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1472 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1473 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1474 | mConfiguration.setToDefaults(); |
| 1475 | mConfiguration.locale = Locale.getDefault(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1476 | mProcessStats.init(); |
| 1477 | |
| 1478 | // Add ourself to the Watchdog monitors. |
| 1479 | Watchdog.getInstance().addMonitor(this); |
| 1480 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1481 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1482 | public void run() { |
| 1483 | while (true) { |
| 1484 | try { |
| 1485 | try { |
| 1486 | synchronized(this) { |
| 1487 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1488 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1489 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1490 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | // + ", write delay=" + nextWriteDelay); |
| 1492 | if (nextWriteDelay < nextCpuDelay) { |
| 1493 | nextCpuDelay = nextWriteDelay; |
| 1494 | } |
| 1495 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1496 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1497 | this.wait(nextCpuDelay); |
| 1498 | } |
| 1499 | } |
| 1500 | } catch (InterruptedException e) { |
| 1501 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | updateCpuStatsNow(); |
| 1503 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1504 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1505 | } |
| 1506 | } |
| 1507 | } |
| 1508 | }; |
| 1509 | mProcessStatsThread.start(); |
| 1510 | } |
| 1511 | |
| 1512 | @Override |
| 1513 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1514 | throws RemoteException { |
| 1515 | try { |
| 1516 | return super.onTransact(code, data, reply, flags); |
| 1517 | } catch (RuntimeException e) { |
| 1518 | // The activity manager only throws security exceptions, so let's |
| 1519 | // log all others. |
| 1520 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1521 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1522 | } |
| 1523 | throw e; |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1528 | final long now = SystemClock.uptimeMillis(); |
| 1529 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1530 | return; |
| 1531 | } |
| 1532 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1533 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1534 | mProcessStatsThread.notify(); |
| 1535 | } |
| 1536 | } |
| 1537 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1538 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1539 | void updateCpuStatsNow() { |
| 1540 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1541 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1542 | final long now = SystemClock.uptimeMillis(); |
| 1543 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1544 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1545 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1546 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1547 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1548 | haveNewCpuStats = true; |
| 1549 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1550 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1551 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1552 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1553 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1554 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1555 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1556 | int user = mProcessStats.getLastUserTime(); |
| 1557 | int system = mProcessStats.getLastSystemTime(); |
| 1558 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1559 | int irq = mProcessStats.getLastIrqTime(); |
| 1560 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1561 | int idle = mProcessStats.getLastIdleTime(); |
| 1562 | |
| 1563 | int total = user + system + iowait + irq + softIrq + idle; |
| 1564 | if (total == 0) total = 1; |
| 1565 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1566 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1567 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1568 | (user * 100) / total, |
| 1569 | (system * 100) / total, |
| 1570 | (iowait * 100) / total, |
| 1571 | (irq * 100) / total, |
| 1572 | (softIrq * 100) / total); |
| 1573 | } |
| 1574 | } |
| 1575 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1576 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1577 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1578 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | synchronized(mPidsSelfLocked) { |
| 1580 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1581 | if (mOnBattery) { |
| 1582 | int perc = bstats.startAddingCpuLocked(); |
| 1583 | int totalUTime = 0; |
| 1584 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1585 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1586 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1587 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1588 | if (!st.working) { |
| 1589 | continue; |
| 1590 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1591 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1592 | int otherUTime = (st.rel_utime*perc)/100; |
| 1593 | int otherSTime = (st.rel_stime*perc)/100; |
| 1594 | totalUTime += otherUTime; |
| 1595 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1596 | if (pr != null) { |
| 1597 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1598 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1599 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1600 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1601 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1602 | } else { |
| 1603 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1604 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1605 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1606 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1607 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1608 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1609 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | } |
| 1611 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1612 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1613 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1614 | } |
| 1615 | } |
| 1616 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1617 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1619 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1620 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | } |
| 1624 | } |
| 1625 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1626 | @Override |
| 1627 | public void batteryNeedsCpuUpdate() { |
| 1628 | updateCpuStatsNow(); |
| 1629 | } |
| 1630 | |
| 1631 | @Override |
| 1632 | public void batteryPowerChanged(boolean onBattery) { |
| 1633 | // When plugging in, update the CPU stats first before changing |
| 1634 | // the plug state. |
| 1635 | updateCpuStatsNow(); |
| 1636 | synchronized (this) { |
| 1637 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1638 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1643 | /** |
| 1644 | * Initialize the application bind args. These are passed to each |
| 1645 | * process when the bindApplication() IPC is sent to the process. They're |
| 1646 | * lazily setup to make sure the services are running when they're asked for. |
| 1647 | */ |
| 1648 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1649 | if (mAppBindArgs == null) { |
| 1650 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1651 | |
| 1652 | // Setup the application init args |
| 1653 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1654 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1655 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1656 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1657 | } |
| 1658 | return mAppBindArgs; |
| 1659 | } |
| 1660 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1661 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | if (mFocusedActivity != r) { |
| 1663 | mFocusedActivity = r; |
| 1664 | mWindowManager.setFocusedApp(r, true); |
| 1665 | } |
| 1666 | } |
| 1667 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1668 | private final void updateLruProcessInternalLocked(ProcessRecord app, |
| 1669 | boolean oomAdj, boolean updateActivityTime, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1670 | // 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] | 1671 | int lrui = mLruProcesses.indexOf(app); |
| 1672 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1673 | |
| 1674 | int i = mLruProcesses.size()-1; |
| 1675 | int skipTop = 0; |
| 1676 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1677 | app.lruSeq = mLruSeq; |
| 1678 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1679 | // compute the new weight for this process. |
| 1680 | if (updateActivityTime) { |
| 1681 | app.lastActivityTime = SystemClock.uptimeMillis(); |
| 1682 | } |
| 1683 | if (app.activities.size() > 0) { |
| 1684 | // If this process has activities, we more strongly want to keep |
| 1685 | // it around. |
| 1686 | app.lruWeight = app.lastActivityTime; |
| 1687 | } else if (app.pubProviders.size() > 0) { |
| 1688 | // If this process contains content providers, we want to keep |
| 1689 | // it a little more strongly. |
| 1690 | app.lruWeight = app.lastActivityTime - CONTENT_APP_IDLE_OFFSET; |
| 1691 | // Also don't let it kick out the first few "real" hidden processes. |
| 1692 | skipTop = MIN_HIDDEN_APPS; |
| 1693 | } else { |
| 1694 | // If this process doesn't have activities, we less strongly |
| 1695 | // want to keep it around, and generally want to avoid getting |
| 1696 | // in front of any very recently used activities. |
| 1697 | app.lruWeight = app.lastActivityTime - EMPTY_APP_IDLE_OFFSET; |
| 1698 | // Also don't let it kick out the first few "real" hidden processes. |
| 1699 | skipTop = MIN_HIDDEN_APPS; |
| 1700 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1701 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1702 | while (i >= 0) { |
| 1703 | ProcessRecord p = mLruProcesses.get(i); |
| 1704 | // If this app shouldn't be in front of the first N background |
| 1705 | // apps, then skip over that many that are currently hidden. |
| 1706 | if (skipTop > 0 && p.setAdj >= HIDDEN_APP_MIN_ADJ) { |
| 1707 | skipTop--; |
| 1708 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1709 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1710 | mLruProcesses.add(i+1, app); |
| 1711 | break; |
| 1712 | } |
| 1713 | i--; |
| 1714 | } |
| 1715 | if (i < 0) { |
| 1716 | mLruProcesses.add(0, app); |
| 1717 | } |
| 1718 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1719 | // If the app is currently using a content provider or service, |
| 1720 | // bump those processes as well. |
| 1721 | if (app.connections.size() > 0) { |
| 1722 | for (ConnectionRecord cr : app.connections) { |
| 1723 | if (cr.binding != null && cr.binding.service != null |
| 1724 | && cr.binding.service.app != null |
| 1725 | && cr.binding.service.app.lruSeq != mLruSeq) { |
| 1726 | updateLruProcessInternalLocked(cr.binding.service.app, oomAdj, |
| 1727 | updateActivityTime, i+1); |
| 1728 | } |
| 1729 | } |
| 1730 | } |
| 1731 | if (app.conProviders.size() > 0) { |
| 1732 | for (ContentProviderRecord cpr : app.conProviders.keySet()) { |
| 1733 | if (cpr.app != null && cpr.app.lruSeq != mLruSeq) { |
| 1734 | updateLruProcessInternalLocked(cpr.app, oomAdj, |
| 1735 | updateActivityTime, i+1); |
| 1736 | } |
| 1737 | } |
| 1738 | } |
| 1739 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1740 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1741 | if (oomAdj) { |
| 1742 | updateOomAdjLocked(); |
| 1743 | } |
| 1744 | } |
| 1745 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1746 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1747 | boolean oomAdj, boolean updateActivityTime) { |
| 1748 | mLruSeq++; |
| 1749 | updateLruProcessInternalLocked(app, oomAdj, updateActivityTime, 0); |
| 1750 | } |
| 1751 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1752 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1753 | String processName, int uid) { |
| 1754 | if (uid == Process.SYSTEM_UID) { |
| 1755 | // The system gets to run in any process. If there are multiple |
| 1756 | // processes with the same uid, just pick the first (this |
| 1757 | // should never happen). |
| 1758 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1759 | processName); |
| 1760 | return procs != null ? procs.valueAt(0) : null; |
| 1761 | } |
| 1762 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1763 | return proc; |
| 1764 | } |
| 1765 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1766 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1767 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1768 | try { |
| 1769 | if (pm.performDexOpt(packageName)) { |
| 1770 | mDidDexOpt = true; |
| 1771 | } |
| 1772 | } catch (RemoteException e) { |
| 1773 | } |
| 1774 | } |
| 1775 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1776 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1777 | int transit = mWindowManager.getPendingAppTransition(); |
| 1778 | return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN |
| 1779 | || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN |
| 1780 | || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT; |
| 1781 | } |
| 1782 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1783 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1784 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1785 | String hostingType, ComponentName hostingName, boolean allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1786 | ProcessRecord app = getProcessRecordLocked(processName, info.uid); |
| 1787 | // We don't have to do anything more if: |
| 1788 | // (1) There is an existing application record; and |
| 1789 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 1790 | // object attached to it so we know it couldn't have crashed; and |
| 1791 | // (3) There is a pid assigned to it, so it is either starting or |
| 1792 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1793 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1794 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 1795 | + " thread=" + (app != null ? app.thread : null) |
| 1796 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1797 | if (app != null && app.pid > 0) { |
| 1798 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1799 | // We already have the app running, or are waiting for it to |
| 1800 | // 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] | 1801 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1802 | return app; |
| 1803 | } else { |
| 1804 | // An application record is attached to a previous process, |
| 1805 | // clean it up now. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1806 | if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1807 | handleAppDiedLocked(app, true); |
| 1808 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1809 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1810 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1811 | String hostingNameStr = hostingName != null |
| 1812 | ? hostingName.flattenToShortString() : null; |
| 1813 | |
| 1814 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 1815 | // If we are in the background, then check to see if this process |
| 1816 | // is bad. If so, we will just silently fail. |
| 1817 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1818 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 1819 | + "/" + info.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1820 | return null; |
| 1821 | } |
| 1822 | } else { |
| 1823 | // When the user is explicitly starting a process, then clear its |
| 1824 | // crash count so that we won't make it bad until they see at |
| 1825 | // least one crash dialog again, and make the process good again |
| 1826 | // if it had been bad. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1827 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
| 1828 | + "/" + info.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1829 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 1830 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1831 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1832 | info.processName); |
| 1833 | mBadProcesses.remove(info.processName, info.uid); |
| 1834 | if (app != null) { |
| 1835 | app.bad = false; |
| 1836 | } |
| 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | if (app == null) { |
| 1841 | app = newProcessRecordLocked(null, info, processName); |
| 1842 | mProcessNames.put(processName, info.uid, app); |
| 1843 | } else { |
| 1844 | // If this is a new package in the process, add the package to the list |
| 1845 | app.addPackage(info.packageName); |
| 1846 | } |
| 1847 | |
| 1848 | // If the system is not ready yet, then hold off on starting this |
| 1849 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1850 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1851 | && !isAllowedWhileBooting(info) |
| 1852 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1853 | if (!mProcessesOnHold.contains(app)) { |
| 1854 | mProcessesOnHold.add(app); |
| 1855 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1856 | 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] | 1857 | return app; |
| 1858 | } |
| 1859 | |
| 1860 | startProcessLocked(app, hostingType, hostingNameStr); |
| 1861 | return (app.pid != 0) ? app : null; |
| 1862 | } |
| 1863 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1864 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 1865 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 1866 | } |
| 1867 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1868 | private final void startProcessLocked(ProcessRecord app, |
| 1869 | String hostingType, String hostingNameStr) { |
| 1870 | if (app.pid > 0 && app.pid != MY_PID) { |
| 1871 | synchronized (mPidsSelfLocked) { |
| 1872 | mPidsSelfLocked.remove(app.pid); |
| 1873 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 1874 | } |
| 1875 | app.pid = 0; |
| 1876 | } |
| 1877 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1878 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 1879 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1880 | mProcessesOnHold.remove(app); |
| 1881 | |
| 1882 | updateCpuStats(); |
| 1883 | |
| 1884 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 1885 | mProcDeaths[0] = 0; |
| 1886 | |
| 1887 | try { |
| 1888 | int uid = app.info.uid; |
| 1889 | int[] gids = null; |
| 1890 | try { |
| 1891 | gids = mContext.getPackageManager().getPackageGids( |
| 1892 | app.info.packageName); |
| 1893 | } catch (PackageManager.NameNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1894 | Slog.w(TAG, "Unable to retrieve gids", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1895 | } |
| 1896 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 1897 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 1898 | && mTopComponent != null |
| 1899 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 1900 | uid = 0; |
| 1901 | } |
| 1902 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 1903 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 1904 | uid = 0; |
| 1905 | } |
| 1906 | } |
| 1907 | int debugFlags = 0; |
| 1908 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 1909 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
| 1910 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 1911 | // Run the app in safe mode if its manifest requests so or the |
| 1912 | // system is booted in safe mode. |
| 1913 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 1914 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 1915 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 1916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1917 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 1918 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 1919 | } |
| 1920 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 1921 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 1922 | } |
| 1923 | int pid = Process.start("android.app.ActivityThread", |
| 1924 | mSimpleProcessManagement ? app.processName : null, uid, uid, |
| 1925 | gids, debugFlags, null); |
| 1926 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 1927 | synchronized (bs) { |
| 1928 | if (bs.isOnBattery()) { |
| 1929 | app.batteryStats.incStartsLocked(); |
| 1930 | } |
| 1931 | } |
| 1932 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1933 | EventLog.writeEvent(EventLogTags.AM_PROC_START, pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1934 | app.processName, hostingType, |
| 1935 | hostingNameStr != null ? hostingNameStr : ""); |
| 1936 | |
| 1937 | if (app.persistent) { |
Christopher Tate | c27181c | 2010-06-30 14:41:09 -0700 | [diff] [blame] | 1938 | Watchdog.getInstance().processStarted(app.processName, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1939 | } |
| 1940 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 1941 | StringBuilder buf = mStringBuilder; |
| 1942 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1943 | buf.append("Start proc "); |
| 1944 | buf.append(app.processName); |
| 1945 | buf.append(" for "); |
| 1946 | buf.append(hostingType); |
| 1947 | if (hostingNameStr != null) { |
| 1948 | buf.append(" "); |
| 1949 | buf.append(hostingNameStr); |
| 1950 | } |
| 1951 | buf.append(": pid="); |
| 1952 | buf.append(pid); |
| 1953 | buf.append(" uid="); |
| 1954 | buf.append(uid); |
| 1955 | buf.append(" gids={"); |
| 1956 | if (gids != null) { |
| 1957 | for (int gi=0; gi<gids.length; gi++) { |
| 1958 | if (gi != 0) buf.append(", "); |
| 1959 | buf.append(gids[gi]); |
| 1960 | |
| 1961 | } |
| 1962 | } |
| 1963 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1964 | Slog.i(TAG, buf.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1965 | if (pid == 0 || pid == MY_PID) { |
| 1966 | // Processes are being emulated with threads. |
| 1967 | app.pid = MY_PID; |
| 1968 | app.removed = false; |
| 1969 | mStartingProcesses.add(app); |
| 1970 | } else if (pid > 0) { |
| 1971 | app.pid = pid; |
| 1972 | app.removed = false; |
| 1973 | synchronized (mPidsSelfLocked) { |
| 1974 | this.mPidsSelfLocked.put(pid, app); |
| 1975 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1976 | msg.obj = app; |
| 1977 | mHandler.sendMessageDelayed(msg, PROC_START_TIMEOUT); |
| 1978 | } |
| 1979 | } else { |
| 1980 | app.pid = 0; |
| 1981 | RuntimeException e = new RuntimeException( |
| 1982 | "Failure starting process " + app.processName |
| 1983 | + ": returned pid=" + pid); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1984 | Slog.e(TAG, e.getMessage(), e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1985 | } |
| 1986 | } catch (RuntimeException e) { |
| 1987 | // XXX do better error recovery. |
| 1988 | app.pid = 0; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1989 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1990 | } |
| 1991 | } |
| 1992 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1993 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1994 | if (resumed) { |
| 1995 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 1996 | } else { |
| 1997 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 1998 | } |
| 1999 | } |
| 2000 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2001 | boolean startHomeActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2002 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2003 | && mTopAction == null) { |
| 2004 | // We are running in factory test mode, but unable to find |
| 2005 | // the factory test app, so just sit around displaying the |
| 2006 | // error message and don't try to start anything. |
| 2007 | return false; |
| 2008 | } |
| 2009 | Intent intent = new Intent( |
| 2010 | mTopAction, |
| 2011 | mTopData != null ? Uri.parse(mTopData) : null); |
| 2012 | intent.setComponent(mTopComponent); |
| 2013 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 2014 | intent.addCategory(Intent.CATEGORY_HOME); |
| 2015 | } |
| 2016 | ActivityInfo aInfo = |
| 2017 | intent.resolveActivityInfo(mContext.getPackageManager(), |
| 2018 | STOCK_PM_FLAGS); |
| 2019 | if (aInfo != null) { |
| 2020 | intent.setComponent(new ComponentName( |
| 2021 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2022 | // Don't do this if the home app is currently being |
| 2023 | // instrumented. |
| 2024 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 2025 | aInfo.applicationInfo.uid); |
| 2026 | if (app == null || app.instrumentationClass == null) { |
| 2027 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2028 | mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2029 | null, null, 0, 0, 0, false, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2030 | } |
| 2031 | } |
| 2032 | |
| 2033 | |
| 2034 | return true; |
| 2035 | } |
| 2036 | |
| 2037 | /** |
| 2038 | * Starts the "new version setup screen" if appropriate. |
| 2039 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2040 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2041 | // Only do this once per boot. |
| 2042 | if (mCheckedForSetup) { |
| 2043 | return; |
| 2044 | } |
| 2045 | |
| 2046 | // We will show this screen if the current one is a different |
| 2047 | // version than the last one shown, and we are not running in |
| 2048 | // low-level factory test mode. |
| 2049 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2050 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
| 2051 | Settings.Secure.getInt(resolver, |
| 2052 | Settings.Secure.DEVICE_PROVISIONED, 0) != 0) { |
| 2053 | mCheckedForSetup = true; |
| 2054 | |
| 2055 | // See if we should be showing the platform update setup UI. |
| 2056 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 2057 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 2058 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 2059 | |
| 2060 | // We don't allow third party apps to replace this. |
| 2061 | ResolveInfo ri = null; |
| 2062 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 2063 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 2064 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2065 | ri = ris.get(i); |
| 2066 | break; |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | if (ri != null) { |
| 2071 | String vers = ri.activityInfo.metaData != null |
| 2072 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 2073 | : null; |
| 2074 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2075 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2076 | Intent.METADATA_SETUP_VERSION); |
| 2077 | } |
| 2078 | String lastVers = Settings.Secure.getString( |
| 2079 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2080 | if (vers != null && !vers.equals(lastVers)) { |
| 2081 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2082 | intent.setComponent(new ComponentName( |
| 2083 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2084 | mMainStack.startActivityLocked(null, intent, null, null, 0, ri.activityInfo, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2085 | null, null, 0, 0, 0, false, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | } |
| 2089 | } |
| 2090 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2091 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2092 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2093 | |
| 2094 | final int identHash = System.identityHashCode(r); |
| 2095 | updateUsageStats(r, true); |
| 2096 | |
| 2097 | int i = mWatchers.beginBroadcast(); |
| 2098 | while (i > 0) { |
| 2099 | i--; |
| 2100 | IActivityWatcher w = mWatchers.getBroadcastItem(i); |
| 2101 | if (w != null) { |
| 2102 | try { |
| 2103 | w.activityResuming(identHash); |
| 2104 | } catch (RemoteException e) { |
| 2105 | } |
| 2106 | } |
| 2107 | } |
| 2108 | mWatchers.finishBroadcast(); |
| 2109 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2110 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2111 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2112 | final int N = mPendingActivityLaunches.size(); |
| 2113 | if (N <= 0) { |
| 2114 | return; |
| 2115 | } |
| 2116 | for (int i=0; i<N; i++) { |
| 2117 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2118 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2119 | pal.grantedUriPermissions, pal.grantedMode, pal.onlyIfNeeded, |
| 2120 | doResume && i == (N-1)); |
| 2121 | } |
| 2122 | mPendingActivityLaunches.clear(); |
| 2123 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2124 | |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2125 | public final int startActivity(IApplicationThread caller, |
| 2126 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2127 | int grantedMode, IBinder resultTo, |
| 2128 | String resultWho, int requestCode, boolean onlyIfNeeded, |
| 2129 | boolean debug) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2130 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2131 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
| 2132 | requestCode, onlyIfNeeded, debug, null, null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | public final WaitResult startActivityAndWait(IApplicationThread caller, |
| 2136 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2137 | int grantedMode, IBinder resultTo, |
| 2138 | String resultWho, int requestCode, boolean onlyIfNeeded, |
| 2139 | boolean debug) { |
| 2140 | WaitResult res = new WaitResult(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2141 | mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2142 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
| 2143 | requestCode, onlyIfNeeded, debug, res, null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2144 | return res; |
| 2145 | } |
| 2146 | |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2147 | public final int startActivityWithConfig(IApplicationThread caller, |
| 2148 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2149 | int grantedMode, IBinder resultTo, |
| 2150 | String resultWho, int requestCode, boolean onlyIfNeeded, |
| 2151 | boolean debug, Configuration config) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2152 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2153 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
| 2154 | requestCode, onlyIfNeeded, debug, null, config); |
| 2155 | } |
| 2156 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2157 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2158 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2159 | IBinder resultTo, String resultWho, int requestCode, |
| 2160 | int flagsMask, int flagsValues) { |
| 2161 | // Refuse possible leaked file descriptors |
| 2162 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2163 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2164 | } |
| 2165 | |
| 2166 | IIntentSender sender = intent.getTarget(); |
| 2167 | if (!(sender instanceof PendingIntentRecord)) { |
| 2168 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2169 | } |
| 2170 | |
| 2171 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2172 | |
| 2173 | synchronized (this) { |
| 2174 | // If this is coming from the currently resumed activity, it is |
| 2175 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2176 | if (mMainStack.mResumedActivity != null |
| 2177 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2178 | Binder.getCallingUid()) { |
| 2179 | mAppSwitchesAllowedTime = 0; |
| 2180 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | return pir.sendInner(0, fillInIntent, resolvedType, |
| 2184 | null, resultTo, resultWho, requestCode, flagsMask, flagsValues); |
| 2185 | } |
| 2186 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2187 | public boolean startNextMatchingActivity(IBinder callingActivity, |
| 2188 | Intent intent) { |
| 2189 | // Refuse possible leaked file descriptors |
| 2190 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2191 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2192 | } |
| 2193 | |
| 2194 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2195 | int index = mMainStack.indexOfTokenLocked(callingActivity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2196 | if (index < 0) { |
| 2197 | return false; |
| 2198 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2199 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2200 | if (r.app == null || r.app.thread == null) { |
| 2201 | // The caller is not running... d'oh! |
| 2202 | return false; |
| 2203 | } |
| 2204 | intent = new Intent(intent); |
| 2205 | // The caller is not allowed to change the data. |
| 2206 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2207 | // And we are resetting to find the next component... |
| 2208 | intent.setComponent(null); |
| 2209 | |
| 2210 | ActivityInfo aInfo = null; |
| 2211 | try { |
| 2212 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2213 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2214 | intent, r.resolvedType, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 2215 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2216 | |
| 2217 | // Look for the original activity in the list... |
| 2218 | final int N = resolves != null ? resolves.size() : 0; |
| 2219 | for (int i=0; i<N; i++) { |
| 2220 | ResolveInfo rInfo = resolves.get(i); |
| 2221 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2222 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2223 | // We found the current one... the next matching is |
| 2224 | // after it. |
| 2225 | i++; |
| 2226 | if (i<N) { |
| 2227 | aInfo = resolves.get(i).activityInfo; |
| 2228 | } |
| 2229 | break; |
| 2230 | } |
| 2231 | } |
| 2232 | } catch (RemoteException e) { |
| 2233 | } |
| 2234 | |
| 2235 | if (aInfo == null) { |
| 2236 | // Nobody who is next! |
| 2237 | return false; |
| 2238 | } |
| 2239 | |
| 2240 | intent.setComponent(new ComponentName( |
| 2241 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2242 | intent.setFlags(intent.getFlags()&~( |
| 2243 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2244 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2245 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2246 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2247 | |
| 2248 | // Okay now we need to start the new activity, replacing the |
| 2249 | // currently running activity. This is a little tricky because |
| 2250 | // we want to start the new one as if the current one is finished, |
| 2251 | // but not finish the current one first so that there is no flicker. |
| 2252 | // And thus... |
| 2253 | final boolean wasFinishing = r.finishing; |
| 2254 | r.finishing = true; |
| 2255 | |
| 2256 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2257 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2258 | final String resultWho = r.resultWho; |
| 2259 | final int requestCode = r.requestCode; |
| 2260 | r.resultTo = null; |
| 2261 | if (resultTo != null) { |
| 2262 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2263 | } |
| 2264 | |
| 2265 | final long origId = Binder.clearCallingIdentity(); |
| 2266 | // XXX we are not dealing with propagating grantedUriPermissions... |
| 2267 | // 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] | 2268 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2269 | r.resolvedType, null, 0, aInfo, resultTo, resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2270 | requestCode, -1, r.launchedFromUid, false, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2271 | Binder.restoreCallingIdentity(origId); |
| 2272 | |
| 2273 | r.finishing = wasFinishing; |
| 2274 | if (res != START_SUCCESS) { |
| 2275 | return false; |
| 2276 | } |
| 2277 | return true; |
| 2278 | } |
| 2279 | } |
| 2280 | |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2281 | public final int startActivityInPackage(int uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2282 | Intent intent, String resolvedType, IBinder resultTo, |
| 2283 | String resultWho, int requestCode, boolean onlyIfNeeded) { |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2284 | |
| 2285 | // This is so super not safe, that only the system (or okay root) |
| 2286 | // can do it. |
| 2287 | final int callingUid = Binder.getCallingUid(); |
| 2288 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2289 | throw new SecurityException( |
| 2290 | "startActivityInPackage only available to the system"); |
| 2291 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2292 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2293 | return mMainStack.startActivityMayWait(null, uid, intent, resolvedType, |
| 2294 | null, 0, resultTo, resultWho, requestCode, onlyIfNeeded, false, null, null); |
| 2295 | } |
| 2296 | |
| 2297 | public final int startActivities(IApplicationThread caller, |
| 2298 | Intent[] intents, String[] resolvedTypes, IBinder resultTo) { |
| 2299 | return mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo); |
| 2300 | } |
| 2301 | |
| 2302 | public final int startActivitiesInPackage(int uid, |
| 2303 | Intent[] intents, String[] resolvedTypes, IBinder resultTo) { |
| 2304 | |
| 2305 | // This is so super not safe, that only the system (or okay root) |
| 2306 | // can do it. |
| 2307 | final int callingUid = Binder.getCallingUid(); |
| 2308 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2309 | throw new SecurityException( |
| 2310 | "startActivityInPackage only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2311 | } |
| 2312 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2313 | return mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2314 | } |
| 2315 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2316 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2317 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2318 | // Quick case: check if the top-most recent task is the same. |
| 2319 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2320 | return; |
| 2321 | } |
| 2322 | // 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] | 2323 | for (int i=0; i<N; i++) { |
| 2324 | TaskRecord tr = mRecentTasks.get(i); |
| 2325 | if ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2326 | || (task.intent != null && task.intent.filterEquals(tr.intent))) { |
| 2327 | mRecentTasks.remove(i); |
| 2328 | i--; |
| 2329 | N--; |
| 2330 | if (task.intent == null) { |
| 2331 | // If the new recent task we are adding is not fully |
| 2332 | // specified, then replace it with the existing recent task. |
| 2333 | task = tr; |
| 2334 | } |
| 2335 | } |
| 2336 | } |
| 2337 | if (N >= MAX_RECENT_TASKS) { |
| 2338 | mRecentTasks.remove(N-1); |
| 2339 | } |
| 2340 | mRecentTasks.add(0, task); |
| 2341 | } |
| 2342 | |
| 2343 | public void setRequestedOrientation(IBinder token, |
| 2344 | int requestedOrientation) { |
| 2345 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2346 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2347 | if (index < 0) { |
| 2348 | return; |
| 2349 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2350 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2351 | final long origId = Binder.clearCallingIdentity(); |
| 2352 | mWindowManager.setAppOrientation(r, requestedOrientation); |
| 2353 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2354 | mConfiguration, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2355 | r.mayFreezeScreenLocked(r.app) ? r : null); |
| 2356 | if (config != null) { |
| 2357 | r.frozenBeforeDestroy = true; |
| 2358 | if (!updateConfigurationLocked(config, r)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2359 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2360 | } |
| 2361 | } |
| 2362 | Binder.restoreCallingIdentity(origId); |
| 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | public int getRequestedOrientation(IBinder token) { |
| 2367 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2368 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2369 | if (index < 0) { |
| 2370 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2371 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2372 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2373 | return mWindowManager.getAppOrientation(r); |
| 2374 | } |
| 2375 | } |
| 2376 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2377 | /** |
| 2378 | * This is the internal entry point for handling Activity.finish(). |
| 2379 | * |
| 2380 | * @param token The Binder token referencing the Activity we want to finish. |
| 2381 | * @param resultCode Result code, if any, from this Activity. |
| 2382 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2383 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2384 | * @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] | 2385 | */ |
| 2386 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2387 | // Refuse possible leaked file descriptors |
| 2388 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2389 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2390 | } |
| 2391 | |
| 2392 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2393 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2394 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2395 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2396 | if (next != null) { |
| 2397 | // ask watcher if this is allowed |
| 2398 | boolean resumeOK = true; |
| 2399 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2400 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2401 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2402 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | if (!resumeOK) { |
| 2406 | return false; |
| 2407 | } |
| 2408 | } |
| 2409 | } |
| 2410 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2411 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2412 | resultData, "app-request"); |
| 2413 | Binder.restoreCallingIdentity(origId); |
| 2414 | return res; |
| 2415 | } |
| 2416 | } |
| 2417 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2418 | public final void finishHeavyWeightApp() { |
| 2419 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2420 | != PackageManager.PERMISSION_GRANTED) { |
| 2421 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2422 | + Binder.getCallingPid() |
| 2423 | + ", uid=" + Binder.getCallingUid() |
| 2424 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2425 | Slog.w(TAG, msg); |
| 2426 | throw new SecurityException(msg); |
| 2427 | } |
| 2428 | |
| 2429 | synchronized(this) { |
| 2430 | if (mHeavyWeightProcess == null) { |
| 2431 | return; |
| 2432 | } |
| 2433 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2434 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2435 | mHeavyWeightProcess.activities); |
| 2436 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2437 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2438 | if (!r.finishing) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2439 | int index = mMainStack.indexOfTokenLocked(r); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2440 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2441 | mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED, |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2442 | null, "finish-heavy"); |
| 2443 | } |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | mHeavyWeightProcess = null; |
| 2448 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 2449 | } |
| 2450 | } |
| 2451 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2452 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2453 | String message) { |
| 2454 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2455 | != PackageManager.PERMISSION_GRANTED) { |
| 2456 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2457 | + Binder.getCallingPid() |
| 2458 | + ", uid=" + Binder.getCallingUid() |
| 2459 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2460 | Slog.w(TAG, msg); |
| 2461 | throw new SecurityException(msg); |
| 2462 | } |
| 2463 | |
| 2464 | synchronized(this) { |
| 2465 | ProcessRecord proc = null; |
| 2466 | |
| 2467 | // Figure out which process to kill. We don't trust that initialPid |
| 2468 | // still has any relation to current pids, so must scan through the |
| 2469 | // list. |
| 2470 | synchronized (mPidsSelfLocked) { |
| 2471 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2472 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
| 2473 | if (p.info.uid != uid) { |
| 2474 | continue; |
| 2475 | } |
| 2476 | if (p.pid == initialPid) { |
| 2477 | proc = p; |
| 2478 | break; |
| 2479 | } |
| 2480 | for (String str : p.pkgList) { |
| 2481 | if (str.equals(packageName)) { |
| 2482 | proc = p; |
| 2483 | } |
| 2484 | } |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2489 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2490 | + " initialPid=" + initialPid |
| 2491 | + " packageName=" + packageName); |
| 2492 | return; |
| 2493 | } |
| 2494 | |
| 2495 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2496 | if (proc.pid == Process.myPid()) { |
| 2497 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2498 | return; |
| 2499 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2500 | long ident = Binder.clearCallingIdentity(); |
| 2501 | try { |
| 2502 | proc.thread.scheduleCrash(message); |
| 2503 | } catch (RemoteException e) { |
| 2504 | } |
| 2505 | Binder.restoreCallingIdentity(ident); |
| 2506 | } |
| 2507 | } |
| 2508 | } |
| 2509 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2510 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2511 | int requestCode) { |
| 2512 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2513 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2514 | if (index < 0) { |
| 2515 | return; |
| 2516 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2517 | ActivityRecord self = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2518 | |
| 2519 | final long origId = Binder.clearCallingIdentity(); |
| 2520 | |
| 2521 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2522 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2523 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2524 | if (r.resultTo == self && r.requestCode == requestCode) { |
| 2525 | if ((r.resultWho == null && resultWho == null) || |
| 2526 | (r.resultWho != null && r.resultWho.equals(resultWho))) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2527 | mMainStack.finishActivityLocked(r, i, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2528 | Activity.RESULT_CANCELED, null, "request-sub"); |
| 2529 | } |
| 2530 | } |
| 2531 | } |
| 2532 | |
| 2533 | Binder.restoreCallingIdentity(origId); |
| 2534 | } |
| 2535 | } |
| 2536 | |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2537 | public boolean willActivityBeVisible(IBinder token) { |
| 2538 | synchronized(this) { |
| 2539 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2540 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2541 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2542 | if (r == token) { |
| 2543 | return true; |
| 2544 | } |
| 2545 | if (r.fullscreen && !r.finishing) { |
| 2546 | return false; |
| 2547 | } |
| 2548 | } |
| 2549 | return true; |
| 2550 | } |
| 2551 | } |
| 2552 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2553 | public void overridePendingTransition(IBinder token, String packageName, |
| 2554 | int enterAnim, int exitAnim) { |
| 2555 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2556 | int index = mMainStack.indexOfTokenLocked(token); |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2557 | if (index < 0) { |
| 2558 | return; |
| 2559 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2560 | ActivityRecord self = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2561 | |
| 2562 | final long origId = Binder.clearCallingIdentity(); |
| 2563 | |
| 2564 | if (self.state == ActivityState.RESUMED |
| 2565 | || self.state == ActivityState.PAUSING) { |
| 2566 | mWindowManager.overridePendingAppTransition(packageName, |
| 2567 | enterAnim, exitAnim); |
| 2568 | } |
| 2569 | |
| 2570 | Binder.restoreCallingIdentity(origId); |
| 2571 | } |
| 2572 | } |
| 2573 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2574 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2575 | * Main function for removing an existing process from the activity manager |
| 2576 | * as a result of that process going away. Clears out all connections |
| 2577 | * to the process. |
| 2578 | */ |
| 2579 | private final void handleAppDiedLocked(ProcessRecord app, |
| 2580 | boolean restarting) { |
| 2581 | cleanUpApplicationRecordLocked(app, restarting, -1); |
| 2582 | if (!restarting) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2583 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | // Just in case... |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2587 | if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) { |
| 2588 | if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity); |
| 2589 | mMainStack.mPausingActivity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2590 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2591 | if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) { |
| 2592 | mMainStack.mLastPausedActivity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | // Remove this application's activities from active lists. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2596 | mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2597 | |
| 2598 | boolean atTop = true; |
| 2599 | boolean hasVisibleActivities = false; |
| 2600 | |
| 2601 | // Clean out the history list. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2602 | int i = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2603 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2604 | TAG, "Removing app " + app + " from history with " + i + " entries"); |
| 2605 | while (i > 0) { |
| 2606 | i--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2607 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2608 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2609 | TAG, "Record #" + i + " " + r + ": app=" + r.app); |
| 2610 | if (r.app == app) { |
| 2611 | if ((!r.haveState && !r.stateNotNeeded) || r.finishing) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2612 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2613 | TAG, "Removing this entry! frozen=" + r.haveState |
| 2614 | + " finishing=" + r.finishing); |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 2615 | r.makeFinishing(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2616 | mMainStack.mHistory.remove(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2617 | |
| 2618 | r.inHistory = false; |
| 2619 | mWindowManager.removeAppToken(r); |
| 2620 | if (VALIDATE_TOKENS) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2621 | mWindowManager.validateAppTokens(mMainStack.mHistory); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2622 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2623 | r.removeUriPermissionsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2624 | |
| 2625 | } else { |
| 2626 | // We have the current state for this activity, so |
| 2627 | // it can be restarted later when needed. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2628 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2629 | TAG, "Keeping entry, setting app to null"); |
| 2630 | if (r.visible) { |
| 2631 | hasVisibleActivities = true; |
| 2632 | } |
| 2633 | r.app = null; |
| 2634 | r.nowVisible = false; |
| 2635 | if (!r.haveState) { |
| 2636 | r.icicle = null; |
| 2637 | } |
| 2638 | } |
| 2639 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2640 | r.stack.cleanUpActivityLocked(r, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2641 | r.state = ActivityState.STOPPED; |
| 2642 | } |
| 2643 | atTop = false; |
| 2644 | } |
| 2645 | |
| 2646 | app.activities.clear(); |
| 2647 | |
| 2648 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2649 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2650 | + " running instrumentation " + app.instrumentationClass); |
| 2651 | Bundle info = new Bundle(); |
| 2652 | info.putString("shortMsg", "Process crashed."); |
| 2653 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 2654 | } |
| 2655 | |
| 2656 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2657 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2658 | // If there was nothing to resume, and we are not already |
| 2659 | // restarting this process, but there is a visible activity that |
| 2660 | // is hosted by the process... then make sure all visible |
| 2661 | // activities are running, taking care of restarting this |
| 2662 | // process. |
| 2663 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2664 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2665 | } |
| 2666 | } |
| 2667 | } |
| 2668 | } |
| 2669 | |
| 2670 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 2671 | IBinder threadBinder = thread.asBinder(); |
| 2672 | |
| 2673 | // Find the application record. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2674 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2675 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2676 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 2677 | return i; |
| 2678 | } |
| 2679 | } |
| 2680 | return -1; |
| 2681 | } |
| 2682 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2683 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2684 | IApplicationThread thread) { |
| 2685 | if (thread == null) { |
| 2686 | return null; |
| 2687 | } |
| 2688 | |
| 2689 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2690 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2691 | } |
| 2692 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2693 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2694 | IApplicationThread thread) { |
| 2695 | |
| 2696 | mProcDeaths[0]++; |
| 2697 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2698 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 2699 | synchronized (stats) { |
| 2700 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 2701 | } |
| 2702 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2703 | // Clean up already done if the process has been re-started. |
| 2704 | if (app.pid == pid && app.thread != null && |
| 2705 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 2706 | if (!app.killedBackground) { |
| 2707 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 2708 | + ") has died."); |
| 2709 | } |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 2710 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2711 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2712 | TAG, "Dying app: " + app + ", pid: " + pid |
| 2713 | + ", thread: " + thread.asBinder()); |
| 2714 | boolean doLowMem = app.instrumentationClass == null; |
| 2715 | handleAppDiedLocked(app, false); |
| 2716 | |
| 2717 | if (doLowMem) { |
| 2718 | // If there are no longer any background processes running, |
| 2719 | // and the app that died was not running instrumentation, |
| 2720 | // then tell everyone we are now low on memory. |
| 2721 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2722 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2723 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2724 | if (rec.thread != null && rec.setAdj >= HIDDEN_APP_MIN_ADJ) { |
| 2725 | haveBg = true; |
| 2726 | break; |
| 2727 | } |
| 2728 | } |
| 2729 | |
| 2730 | if (!haveBg) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2731 | Slog.i(TAG, "Low Memory: No more background processes."); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2732 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2733 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2734 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2735 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 2736 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2737 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 2738 | // The low memory report is overriding any current |
| 2739 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2740 | // heavy/important/visible/foreground processes first. |
| 2741 | if (rec.setAdj <= HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2742 | rec.lastRequestedGc = 0; |
| 2743 | } else { |
| 2744 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2745 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2746 | rec.reportLowMemory = true; |
| 2747 | rec.lastLowMemory = now; |
| 2748 | mProcessesToGc.remove(rec); |
| 2749 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2750 | } |
| 2751 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2752 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2753 | } |
| 2754 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2755 | } else if (app.pid != pid) { |
| 2756 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2757 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2758 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | 28a8c2b | 2010-03-01 11:30:02 -0800 | [diff] [blame] | 2759 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 2760 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2761 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2762 | + thread.asBinder()); |
| 2763 | } |
| 2764 | } |
| 2765 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2766 | /** |
| 2767 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2768 | * @param clearTraces causes the dump file to be erased prior to the new |
| 2769 | * traces being written, if true; when false, the new traces will be |
| 2770 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2771 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 2772 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2773 | * @return file containing stack traces, or null if no dump file is configured |
| 2774 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2775 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
| 2776 | ProcessStats processStats, SparseArray<Boolean> lastPids) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2777 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 2778 | if (tracesPath == null || tracesPath.length() == 0) { |
| 2779 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2780 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2781 | |
| 2782 | File tracesFile = new File(tracesPath); |
| 2783 | try { |
| 2784 | File tracesDir = tracesFile.getParentFile(); |
| 2785 | if (!tracesDir.exists()) tracesFile.mkdirs(); |
| 2786 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 2787 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2788 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2789 | tracesFile.createNewFile(); |
| 2790 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 2791 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2792 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2793 | return null; |
| 2794 | } |
| 2795 | |
| 2796 | // Use a FileObserver to detect when traces finish writing. |
| 2797 | // The order of traces is considered important to maintain for legibility. |
| 2798 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 2799 | public synchronized void onEvent(int event, String path) { notify(); } |
| 2800 | }; |
| 2801 | |
| 2802 | try { |
| 2803 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2804 | |
| 2805 | // First collect all of the stacks of the most important pids. |
| 2806 | try { |
| 2807 | int num = firstPids.size(); |
| 2808 | for (int i = 0; i < num; i++) { |
| 2809 | synchronized (observer) { |
| 2810 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 2811 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 2812 | } |
| 2813 | } |
| 2814 | } catch (InterruptedException e) { |
| 2815 | Log.wtf(TAG, e); |
| 2816 | } |
| 2817 | |
| 2818 | // Next measure CPU usage. |
| 2819 | if (processStats != null) { |
| 2820 | processStats.init(); |
| 2821 | System.gc(); |
| 2822 | processStats.update(); |
| 2823 | try { |
| 2824 | synchronized (processStats) { |
| 2825 | processStats.wait(500); // measure over 1/2 second. |
| 2826 | } |
| 2827 | } catch (InterruptedException e) { |
| 2828 | } |
| 2829 | processStats.update(); |
| 2830 | |
| 2831 | // We'll take the stack crawls of just the top apps using CPU. |
| 2832 | final int N = processStats.countWorkingStats(); |
| 2833 | int numProcs = 0; |
| 2834 | for (int i=0; i<N && numProcs<5; i++) { |
| 2835 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 2836 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 2837 | numProcs++; |
| 2838 | try { |
| 2839 | synchronized (observer) { |
| 2840 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 2841 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 2842 | } |
| 2843 | } catch (InterruptedException e) { |
| 2844 | Log.wtf(TAG, e); |
| 2845 | } |
| 2846 | |
| 2847 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2848 | } |
| 2849 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2850 | |
| 2851 | return tracesFile; |
| 2852 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2853 | } finally { |
| 2854 | observer.stopWatching(); |
| 2855 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2856 | } |
| 2857 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 2858 | private final class AppNotResponding implements Runnable { |
| 2859 | private final ProcessRecord mApp; |
| 2860 | private final String mAnnotation; |
| 2861 | |
| 2862 | public AppNotResponding(ProcessRecord app, String annotation) { |
| 2863 | mApp = app; |
| 2864 | mAnnotation = annotation; |
| 2865 | } |
| 2866 | |
| 2867 | @Override |
| 2868 | public void run() { |
| 2869 | appNotResponding(mApp, null, null, mAnnotation); |
| 2870 | } |
| 2871 | } |
| 2872 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2873 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
| 2874 | ActivityRecord parent, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2875 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 2876 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 2877 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 2878 | if (mController != null) { |
| 2879 | try { |
| 2880 | // 0 == continue, -1 = kill process immediately |
| 2881 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 2882 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 2883 | } catch (RemoteException e) { |
| 2884 | mController = null; |
| 2885 | } |
| 2886 | } |
| 2887 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2888 | long anrTime = SystemClock.uptimeMillis(); |
| 2889 | if (MONITOR_CPU_USAGE) { |
| 2890 | updateCpuStatsNow(); |
| 2891 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2892 | |
| 2893 | synchronized (this) { |
| 2894 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 2895 | if (mShuttingDown) { |
| 2896 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 2897 | return; |
| 2898 | } else if (app.notResponding) { |
| 2899 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 2900 | return; |
| 2901 | } else if (app.crashing) { |
| 2902 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 2903 | return; |
| 2904 | } |
| 2905 | |
| 2906 | // In case we come through here for the same app before completing |
| 2907 | // this one, mark as anring now so we will bail out. |
| 2908 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2909 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2910 | // Log the ANR to the event log. |
| 2911 | EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags, |
| 2912 | annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2913 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2914 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2915 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2916 | |
| 2917 | int parentPid = app.pid; |
| 2918 | 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] | 2919 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2920 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2921 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2922 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2923 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 2924 | ProcessRecord r = mLruProcesses.get(i); |
| 2925 | if (r != null && r.thread != null) { |
| 2926 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2927 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 2928 | if (r.persistent) { |
| 2929 | firstPids.add(pid); |
| 2930 | } else { |
| 2931 | lastPids.put(pid, Boolean.TRUE); |
| 2932 | } |
| 2933 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2934 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2935 | } |
| 2936 | } |
| 2937 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2938 | // Log the ANR to the main log. |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 2939 | StringBuilder info = mStringBuilder; |
| 2940 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2941 | info.append("ANR in ").append(app.processName); |
| 2942 | if (activity != null && activity.shortComponentName != null) { |
| 2943 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 2944 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 2945 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2946 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 2947 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2948 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2949 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 2950 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2951 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2952 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 2953 | final ProcessStats processStats = new ProcessStats(true); |
| 2954 | |
| 2955 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids); |
| 2956 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2957 | String cpuInfo = null; |
| 2958 | if (MONITOR_CPU_USAGE) { |
| 2959 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2960 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2961 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2962 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2963 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2964 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2965 | } |
| 2966 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2967 | info.append(processStats.printCurrentState(anrTime)); |
| 2968 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2969 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2970 | if (tracesFile == null) { |
| 2971 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 2972 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 2973 | } |
| 2974 | |
| 2975 | addErrorToDropBox("anr", app, activity, parent, annotation, cpuInfo, tracesFile, null); |
| 2976 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2977 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2978 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2979 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 2980 | 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] | 2981 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2982 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 2983 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2984 | } |
| 2985 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2986 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2987 | } |
| 2988 | } |
| 2989 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2990 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 2991 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 2992 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 2993 | |
| 2994 | synchronized (this) { |
| 2995 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
| 2996 | Process.killProcess(app.pid); |
| 2997 | return; |
| 2998 | } |
| 2999 | |
| 3000 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 3001 | makeAppNotRespondingLocked(app, |
| 3002 | activity != null ? activity.shortComponentName : null, |
| 3003 | annotation != null ? "ANR " + annotation : "ANR", |
| 3004 | info.toString()); |
| 3005 | |
| 3006 | // Bring up the infamous App Not Responding dialog |
| 3007 | Message msg = Message.obtain(); |
| 3008 | HashMap map = new HashMap(); |
| 3009 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3010 | msg.obj = map; |
| 3011 | map.put("app", app); |
| 3012 | if (activity != null) { |
| 3013 | map.put("activity", activity); |
| 3014 | } |
| 3015 | |
| 3016 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3017 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3018 | } |
| 3019 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3020 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3021 | if (!mLaunchWarningShown) { |
| 3022 | mLaunchWarningShown = true; |
| 3023 | mHandler.post(new Runnable() { |
| 3024 | @Override |
| 3025 | public void run() { |
| 3026 | synchronized (ActivityManagerService.this) { |
| 3027 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3028 | d.show(); |
| 3029 | mHandler.postDelayed(new Runnable() { |
| 3030 | @Override |
| 3031 | public void run() { |
| 3032 | synchronized (ActivityManagerService.this) { |
| 3033 | d.dismiss(); |
| 3034 | mLaunchWarningShown = false; |
| 3035 | } |
| 3036 | } |
| 3037 | }, 4000); |
| 3038 | } |
| 3039 | } |
| 3040 | }); |
| 3041 | } |
| 3042 | } |
| 3043 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3044 | public boolean clearApplicationUserData(final String packageName, |
| 3045 | final IPackageDataObserver observer) { |
| 3046 | int uid = Binder.getCallingUid(); |
| 3047 | int pid = Binder.getCallingPid(); |
| 3048 | long callingId = Binder.clearCallingIdentity(); |
| 3049 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3050 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3051 | int pkgUid = -1; |
| 3052 | synchronized(this) { |
| 3053 | try { |
| 3054 | pkgUid = pm.getPackageUid(packageName); |
| 3055 | } catch (RemoteException e) { |
| 3056 | } |
| 3057 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3058 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3059 | return false; |
| 3060 | } |
| 3061 | if (uid == pkgUid || checkComponentPermission( |
| 3062 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3063 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3064 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3065 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3066 | } else { |
| 3067 | throw new SecurityException(pid+" does not have permission:"+ |
| 3068 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3069 | "for process:"+packageName); |
| 3070 | } |
| 3071 | } |
| 3072 | |
| 3073 | try { |
| 3074 | //clear application user data |
| 3075 | pm.clearApplicationUserData(packageName, observer); |
| 3076 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3077 | Uri.fromParts("package", packageName, null)); |
| 3078 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3079 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
| 3080 | null, null, 0, null, null, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3081 | } catch (RemoteException e) { |
| 3082 | } |
| 3083 | } finally { |
| 3084 | Binder.restoreCallingIdentity(callingId); |
| 3085 | } |
| 3086 | return true; |
| 3087 | } |
| 3088 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3089 | public void killBackgroundProcesses(final String packageName) { |
| 3090 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3091 | != PackageManager.PERMISSION_GRANTED && |
| 3092 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3093 | != PackageManager.PERMISSION_GRANTED) { |
| 3094 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3095 | + Binder.getCallingPid() |
| 3096 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3097 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3098 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3099 | throw new SecurityException(msg); |
| 3100 | } |
| 3101 | |
| 3102 | long callingId = Binder.clearCallingIdentity(); |
| 3103 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3104 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3105 | int pkgUid = -1; |
| 3106 | synchronized(this) { |
| 3107 | try { |
| 3108 | pkgUid = pm.getPackageUid(packageName); |
| 3109 | } catch (RemoteException e) { |
| 3110 | } |
| 3111 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3112 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3113 | return; |
| 3114 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3115 | killPackageProcessesLocked(packageName, pkgUid, |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3116 | SECONDARY_SERVER_ADJ, false, true); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3117 | } |
| 3118 | } finally { |
| 3119 | Binder.restoreCallingIdentity(callingId); |
| 3120 | } |
| 3121 | } |
| 3122 | |
| 3123 | public void forceStopPackage(final String packageName) { |
| 3124 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3125 | != PackageManager.PERMISSION_GRANTED) { |
| 3126 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3127 | + Binder.getCallingPid() |
| 3128 | + ", uid=" + Binder.getCallingUid() |
| 3129 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3130 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3131 | throw new SecurityException(msg); |
| 3132 | } |
| 3133 | |
| 3134 | long callingId = Binder.clearCallingIdentity(); |
| 3135 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3136 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3137 | int pkgUid = -1; |
| 3138 | synchronized(this) { |
| 3139 | try { |
| 3140 | pkgUid = pm.getPackageUid(packageName); |
| 3141 | } catch (RemoteException e) { |
| 3142 | } |
| 3143 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3144 | Slog.w(TAG, "Invalid packageName: " + packageName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3145 | return; |
| 3146 | } |
| 3147 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3148 | } |
| 3149 | } finally { |
| 3150 | Binder.restoreCallingIdentity(callingId); |
| 3151 | } |
| 3152 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3153 | |
| 3154 | /* |
| 3155 | * The pkg name and uid have to be specified. |
| 3156 | * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int) |
| 3157 | */ |
| 3158 | public void killApplicationWithUid(String pkg, int uid) { |
| 3159 | if (pkg == null) { |
| 3160 | return; |
| 3161 | } |
| 3162 | // Make sure the uid is valid. |
| 3163 | if (uid < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3164 | Slog.w(TAG, "Invalid uid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3165 | return; |
| 3166 | } |
| 3167 | int callerUid = Binder.getCallingUid(); |
| 3168 | // Only the system server can kill an application |
| 3169 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3170 | // Post an aysnc message to kill the application |
| 3171 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
| 3172 | msg.arg1 = uid; |
| 3173 | msg.arg2 = 0; |
| 3174 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3175 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3176 | } else { |
| 3177 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3178 | pkg); |
| 3179 | } |
| 3180 | } |
| 3181 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3182 | public void closeSystemDialogs(String reason) { |
| 3183 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3184 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3185 | if (reason != null) { |
| 3186 | intent.putExtra("reason", reason); |
| 3187 | } |
| 3188 | |
| 3189 | final int uid = Binder.getCallingUid(); |
| 3190 | final long origId = Binder.clearCallingIdentity(); |
| 3191 | synchronized (this) { |
| 3192 | int i = mWatchers.beginBroadcast(); |
| 3193 | while (i > 0) { |
| 3194 | i--; |
| 3195 | IActivityWatcher w = mWatchers.getBroadcastItem(i); |
| 3196 | if (w != null) { |
| 3197 | try { |
| 3198 | w.closingSystemDialogs(reason); |
| 3199 | } catch (RemoteException e) { |
| 3200 | } |
| 3201 | } |
| 3202 | } |
| 3203 | mWatchers.finishBroadcast(); |
| 3204 | |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3205 | mWindowManager.closeSystemDialogs(reason); |
| 3206 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3207 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 3208 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3209 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3210 | r.stack.finishActivityLocked(r, i, |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3211 | Activity.RESULT_CANCELED, null, "close-sys"); |
| 3212 | } |
| 3213 | } |
| 3214 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3215 | broadcastIntentLocked(null, null, intent, null, |
| 3216 | null, 0, null, null, null, false, false, -1, uid); |
| 3217 | } |
| 3218 | Binder.restoreCallingIdentity(origId); |
| 3219 | } |
| 3220 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3221 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3222 | throws RemoteException { |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3223 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3224 | for (int i=pids.length-1; i>=0; i--) { |
| 3225 | infos[i] = new Debug.MemoryInfo(); |
| 3226 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3227 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3228 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3229 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3230 | |
| 3231 | public void killApplicationProcess(String processName, int uid) { |
| 3232 | if (processName == null) { |
| 3233 | return; |
| 3234 | } |
| 3235 | |
| 3236 | int callerUid = Binder.getCallingUid(); |
| 3237 | // Only the system server can kill an application |
| 3238 | if (callerUid == Process.SYSTEM_UID) { |
| 3239 | synchronized (this) { |
| 3240 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
| 3241 | if (app != null) { |
| 3242 | try { |
| 3243 | app.thread.scheduleSuicide(); |
| 3244 | } catch (RemoteException e) { |
| 3245 | // If the other end already died, then our work here is done. |
| 3246 | } |
| 3247 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3248 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3249 | + processName + " / " + uid); |
| 3250 | } |
| 3251 | } |
| 3252 | } else { |
| 3253 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3254 | processName); |
| 3255 | } |
| 3256 | } |
| 3257 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3258 | private void forceStopPackageLocked(final String packageName, int uid) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3259 | forceStopPackageLocked(packageName, uid, false, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3260 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3261 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3262 | if (!mProcessesReady) { |
| 3263 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3264 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3265 | intent.putExtra(Intent.EXTRA_UID, uid); |
| 3266 | broadcastIntentLocked(null, null, intent, |
| 3267 | null, null, 0, null, null, null, |
| 3268 | false, false, MY_PID, Process.SYSTEM_UID); |
| 3269 | } |
| 3270 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3271 | private final boolean killPackageProcessesLocked(String packageName, int uid, |
| 3272 | int minOomAdj, boolean callerWillRestart, boolean doit) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3273 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3274 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3275 | // Remove all processes this package may have touched: all with the |
| 3276 | // same UID (except for the system or root user), and all whose name |
| 3277 | // matches the package name. |
| 3278 | final String procNamePrefix = packageName + ":"; |
| 3279 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3280 | final int NA = apps.size(); |
| 3281 | for (int ia=0; ia<NA; ia++) { |
| 3282 | ProcessRecord app = apps.valueAt(ia); |
| 3283 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3284 | if (doit) { |
| 3285 | procs.add(app); |
| 3286 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3287 | } else if ((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid) |
| 3288 | || app.processName.equals(packageName) |
| 3289 | || app.processName.startsWith(procNamePrefix)) { |
| 3290 | if (app.setAdj >= minOomAdj) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3291 | if (!doit) { |
| 3292 | return true; |
| 3293 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3294 | app.removed = true; |
| 3295 | procs.add(app); |
| 3296 | } |
| 3297 | } |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | int N = procs.size(); |
| 3302 | for (int i=0; i<N; i++) { |
| 3303 | removeProcessLocked(procs.get(i), callerWillRestart); |
| 3304 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3305 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3306 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3307 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3308 | private final boolean forceStopPackageLocked(String name, int uid, |
| 3309 | boolean callerWillRestart, boolean purgeCache, boolean doit) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3310 | int i, N; |
| 3311 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3312 | if (uid < 0) { |
| 3313 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3314 | uid = AppGlobals.getPackageManager().getPackageUid(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3315 | } catch (RemoteException e) { |
| 3316 | } |
| 3317 | } |
| 3318 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3319 | if (doit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3320 | Slog.i(TAG, "Force stopping package " + name + " uid=" + uid); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3321 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3322 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3323 | while (badApps.hasNext()) { |
| 3324 | SparseArray<Long> ba = badApps.next(); |
| 3325 | if (ba.get(uid) != null) { |
| 3326 | badApps.remove(); |
| 3327 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3328 | } |
| 3329 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3330 | |
| 3331 | boolean didSomething = killPackageProcessesLocked(name, uid, -100, |
| 3332 | callerWillRestart, doit); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3333 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3334 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 3335 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3336 | if (r.packageName.equals(name)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3337 | if (!doit) { |
| 3338 | return true; |
| 3339 | } |
| 3340 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3341 | Slog.i(TAG, " Force finishing activity " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3342 | if (r.app != null) { |
| 3343 | r.app.removed = true; |
| 3344 | } |
| 3345 | r.app = null; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3346 | r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, null, "uninstall"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3347 | } |
| 3348 | } |
| 3349 | |
| 3350 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
| 3351 | for (ServiceRecord service : mServices.values()) { |
| 3352 | if (service.packageName.equals(name)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3353 | if (!doit) { |
| 3354 | return true; |
| 3355 | } |
| 3356 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3357 | Slog.i(TAG, " Force stopping service " + service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3358 | if (service.app != null) { |
| 3359 | service.app.removed = true; |
| 3360 | } |
| 3361 | service.app = null; |
| 3362 | services.add(service); |
| 3363 | } |
| 3364 | } |
| 3365 | |
| 3366 | N = services.size(); |
| 3367 | for (i=0; i<N; i++) { |
| 3368 | bringDownServiceLocked(services.get(i), true); |
| 3369 | } |
| 3370 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3371 | if (doit) { |
| 3372 | if (purgeCache) { |
| 3373 | AttributeCache ac = AttributeCache.instance(); |
| 3374 | if (ac != null) { |
| 3375 | ac.removePackage(name); |
| 3376 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3377 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3378 | mMainStack.resumeTopActivityLocked(null); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3379 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3380 | |
| 3381 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | private final boolean removeProcessLocked(ProcessRecord app, boolean callerWillRestart) { |
| 3385 | final String name = app.processName; |
| 3386 | final int uid = app.info.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3387 | if (DEBUG_PROCESSES) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3388 | TAG, "Force removing process " + app + " (" + name |
| 3389 | + "/" + uid + ")"); |
| 3390 | |
| 3391 | mProcessNames.remove(name, uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3392 | if (mHeavyWeightProcess == app) { |
| 3393 | mHeavyWeightProcess = null; |
| 3394 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3395 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3396 | boolean needRestart = false; |
| 3397 | if (app.pid > 0 && app.pid != MY_PID) { |
| 3398 | int pid = app.pid; |
| 3399 | synchronized (mPidsSelfLocked) { |
| 3400 | mPidsSelfLocked.remove(pid); |
| 3401 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3402 | } |
| 3403 | handleAppDiedLocked(app, true); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3404 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3405 | Process.killProcess(pid); |
| 3406 | |
| 3407 | if (app.persistent) { |
| 3408 | if (!callerWillRestart) { |
| 3409 | addAppLocked(app.info); |
| 3410 | } else { |
| 3411 | needRestart = true; |
| 3412 | } |
| 3413 | } |
| 3414 | } else { |
| 3415 | mRemovedProcesses.add(app); |
| 3416 | } |
| 3417 | |
| 3418 | return needRestart; |
| 3419 | } |
| 3420 | |
| 3421 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 3422 | final int pid = app.pid; |
| 3423 | boolean gone = false; |
| 3424 | synchronized (mPidsSelfLocked) { |
| 3425 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 3426 | if (knownApp != null && knownApp.thread == null) { |
| 3427 | mPidsSelfLocked.remove(pid); |
| 3428 | gone = true; |
| 3429 | } |
| 3430 | } |
| 3431 | |
| 3432 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3433 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3434 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.info.uid, |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3435 | app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3436 | mProcessNames.remove(app.processName, app.info.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3437 | if (mHeavyWeightProcess == app) { |
| 3438 | mHeavyWeightProcess = null; |
| 3439 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3440 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3441 | // Take care of any launching providers waiting for this process. |
| 3442 | checkAppInLaunchingProvidersLocked(app, true); |
| 3443 | // Take care of any services that are waiting for the process. |
| 3444 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3445 | ServiceRecord sr = mPendingServices.get(i); |
| 3446 | if (app.info.uid == sr.appInfo.uid |
| 3447 | && app.processName.equals(sr.processName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3448 | Slog.w(TAG, "Forcing bringing down service: " + sr); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3449 | mPendingServices.remove(i); |
| 3450 | i--; |
| 3451 | bringDownServiceLocked(sr, true); |
| 3452 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3453 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3454 | Process.killProcess(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3455 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3456 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3457 | try { |
| 3458 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 3459 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 3460 | bm.agentDisconnected(app.info.packageName); |
| 3461 | } catch (RemoteException e) { |
| 3462 | // Can't happen; the backup manager is local |
| 3463 | } |
| 3464 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3465 | if (mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3466 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3467 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 3468 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3469 | mPendingBroadcast = null; |
| 3470 | scheduleBroadcastsLocked(); |
| 3471 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3472 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3473 | 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] | 3474 | } |
| 3475 | } |
| 3476 | |
| 3477 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 3478 | int pid) { |
| 3479 | |
| 3480 | // Find the application record that is being attached... either via |
| 3481 | // the pid if we are running in multiple processes, or just pull the |
| 3482 | // next app record if we are emulating process with anonymous threads. |
| 3483 | ProcessRecord app; |
| 3484 | if (pid != MY_PID && pid >= 0) { |
| 3485 | synchronized (mPidsSelfLocked) { |
| 3486 | app = mPidsSelfLocked.get(pid); |
| 3487 | } |
| 3488 | } else if (mStartingProcesses.size() > 0) { |
| 3489 | app = mStartingProcesses.remove(0); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3490 | app.setPid(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3491 | } else { |
| 3492 | app = null; |
| 3493 | } |
| 3494 | |
| 3495 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3496 | 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] | 3497 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3498 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3499 | if (pid > 0 && pid != MY_PID) { |
| 3500 | Process.killProcess(pid); |
| 3501 | } else { |
| 3502 | try { |
| 3503 | thread.scheduleExit(); |
| 3504 | } catch (Exception e) { |
| 3505 | // Ignore exceptions. |
| 3506 | } |
| 3507 | } |
| 3508 | return false; |
| 3509 | } |
| 3510 | |
| 3511 | // If this application record is still attached to a previous |
| 3512 | // process, clean it up now. |
| 3513 | if (app.thread != null) { |
| 3514 | handleAppDiedLocked(app, true); |
| 3515 | } |
| 3516 | |
| 3517 | // Tell the process all about itself. |
| 3518 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3519 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3520 | TAG, "Binding process pid " + pid + " to record " + app); |
| 3521 | |
| 3522 | String processName = app.processName; |
| 3523 | try { |
| 3524 | thread.asBinder().linkToDeath(new AppDeathRecipient( |
| 3525 | app, pid, thread), 0); |
| 3526 | } catch (RemoteException e) { |
| 3527 | app.resetPackageList(); |
| 3528 | startProcessLocked(app, "link fail", processName); |
| 3529 | return false; |
| 3530 | } |
| 3531 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3532 | 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] | 3533 | |
| 3534 | app.thread = thread; |
| 3535 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 3536 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 3537 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3538 | app.forcingToForeground = null; |
| 3539 | app.foregroundServices = false; |
| 3540 | app.debugging = false; |
| 3541 | |
| 3542 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3543 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3544 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3545 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3546 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3547 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3548 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3549 | } |
| 3550 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3551 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3552 | TAG, "New app record " + app |
| 3553 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 3554 | try { |
| 3555 | int testMode = IApplicationThread.DEBUG_OFF; |
| 3556 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 3557 | testMode = mWaitForDebugger |
| 3558 | ? IApplicationThread.DEBUG_WAIT |
| 3559 | : IApplicationThread.DEBUG_ON; |
| 3560 | app.debugging = true; |
| 3561 | if (mDebugTransient) { |
| 3562 | mDebugApp = mOrigDebugApp; |
| 3563 | mWaitForDebugger = mOrigWaitForDebugger; |
| 3564 | } |
| 3565 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3566 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3567 | // If the app is being launched for restore or full backup, set it up specially |
| 3568 | boolean isRestrictedBackupMode = false; |
| 3569 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 3570 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
| 3571 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 3572 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3573 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 3574 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 3575 | ? app.instrumentationInfo.packageName |
| 3576 | : app.info.packageName); |
| 3577 | if (app.instrumentationClass != null) { |
| 3578 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3579 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3580 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 3581 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 3582 | thread.bindApplication(processName, app.instrumentationInfo != null |
| 3583 | ? app.instrumentationInfo : app.info, providers, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3584 | app.instrumentationClass, app.instrumentationProfileFile, |
| 3585 | app.instrumentationArguments, app.instrumentationWatcher, testMode, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3586 | isRestrictedBackupMode || !normalMode, |
| 3587 | mConfiguration, getCommonServicesLocked()); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3588 | updateLruProcessLocked(app, false, true); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3589 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3590 | } catch (Exception e) { |
| 3591 | // todo: Yikes! What should we do? For now we will try to |
| 3592 | // start another process, but that could easily get us in |
| 3593 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3594 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3595 | |
| 3596 | app.resetPackageList(); |
| 3597 | startProcessLocked(app, "bind fail", processName); |
| 3598 | return false; |
| 3599 | } |
| 3600 | |
| 3601 | // Remove this record from the list of starting applications. |
| 3602 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3603 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 3604 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3605 | mProcessesOnHold.remove(app); |
| 3606 | |
| 3607 | boolean badApp = false; |
| 3608 | boolean didSomething = false; |
| 3609 | |
| 3610 | // 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] | 3611 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 3612 | if (hr != null && normalMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3613 | if (hr.app == null && app.info.uid == hr.info.applicationInfo.uid |
| 3614 | && processName.equals(hr.processName)) { |
| 3615 | try { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3616 | if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3617 | didSomething = true; |
| 3618 | } |
| 3619 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3620 | 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] | 3621 | + hr.intent.getComponent().flattenToShortString(), e); |
| 3622 | badApp = true; |
| 3623 | } |
| 3624 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3625 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3626 | } |
| 3627 | } |
| 3628 | |
| 3629 | // Find any services that should be running in this process... |
| 3630 | if (!badApp && mPendingServices.size() > 0) { |
| 3631 | ServiceRecord sr = null; |
| 3632 | try { |
| 3633 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3634 | sr = mPendingServices.get(i); |
| 3635 | if (app.info.uid != sr.appInfo.uid |
| 3636 | || !processName.equals(sr.processName)) { |
| 3637 | continue; |
| 3638 | } |
| 3639 | |
| 3640 | mPendingServices.remove(i); |
| 3641 | i--; |
| 3642 | realStartServiceLocked(sr, app); |
| 3643 | didSomething = true; |
| 3644 | } |
| 3645 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3646 | 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] | 3647 | + sr.shortName, e); |
| 3648 | badApp = true; |
| 3649 | } |
| 3650 | } |
| 3651 | |
| 3652 | // Check if the next broadcast receiver is in this process... |
| 3653 | BroadcastRecord br = mPendingBroadcast; |
| 3654 | if (!badApp && br != null && br.curApp == app) { |
| 3655 | try { |
| 3656 | mPendingBroadcast = null; |
| 3657 | processCurBroadcastLocked(br, app); |
| 3658 | didSomething = true; |
| 3659 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3660 | 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] | 3661 | + br.curComponent.flattenToShortString(), e); |
| 3662 | badApp = true; |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 3663 | logBroadcastReceiverDiscardLocked(br); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3664 | finishReceiverLocked(br.receiver, br.resultCode, br.resultData, |
| 3665 | br.resultExtras, br.resultAbort, true); |
| 3666 | scheduleBroadcastsLocked(); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3667 | // We need to reset the state if we fails to start the receiver. |
| 3668 | br.state = BroadcastRecord.IDLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3669 | } |
| 3670 | } |
| 3671 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3672 | // Check whether the next backup agent is in this process... |
| 3673 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.info.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3674 | 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] | 3675 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3676 | try { |
| 3677 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, mBackupTarget.backupMode); |
| 3678 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3679 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3680 | e.printStackTrace(); |
| 3681 | } |
| 3682 | } |
| 3683 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3684 | if (badApp) { |
| 3685 | // todo: Also need to kill application to deal with all |
| 3686 | // kinds of exceptions. |
| 3687 | handleAppDiedLocked(app, false); |
| 3688 | return false; |
| 3689 | } |
| 3690 | |
| 3691 | if (!didSomething) { |
| 3692 | updateOomAdjLocked(); |
| 3693 | } |
| 3694 | |
| 3695 | return true; |
| 3696 | } |
| 3697 | |
| 3698 | public final void attachApplication(IApplicationThread thread) { |
| 3699 | synchronized (this) { |
| 3700 | int callingPid = Binder.getCallingPid(); |
| 3701 | final long origId = Binder.clearCallingIdentity(); |
| 3702 | attachApplicationLocked(thread, callingPid); |
| 3703 | Binder.restoreCallingIdentity(origId); |
| 3704 | } |
| 3705 | } |
| 3706 | |
Dianne Hackborn | e88846e | 2009-09-30 21:34:25 -0700 | [diff] [blame] | 3707 | public final void activityIdle(IBinder token, Configuration config) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3708 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3709 | mMainStack.activityIdleInternal(token, false, config); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3710 | Binder.restoreCallingIdentity(origId); |
| 3711 | } |
| 3712 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3713 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3714 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3715 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3716 | mWindowManager.enableScreenAfterBoot(); |
| 3717 | } |
| 3718 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3719 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3720 | IntentFilter pkgFilter = new IntentFilter(); |
| 3721 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 3722 | pkgFilter.addDataScheme("package"); |
| 3723 | mContext.registerReceiver(new BroadcastReceiver() { |
| 3724 | @Override |
| 3725 | public void onReceive(Context context, Intent intent) { |
| 3726 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 3727 | if (pkgs != null) { |
| 3728 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 3729 | synchronized (ActivityManagerService.this) { |
| 3730 | if (forceStopPackageLocked(pkg, -1, false, false, false)) { |
| 3731 | setResultCode(Activity.RESULT_OK); |
| 3732 | return; |
| 3733 | } |
| 3734 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3735 | } |
| 3736 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3737 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3738 | }, pkgFilter); |
| 3739 | |
| 3740 | synchronized (this) { |
| 3741 | // Ensure that any processes we had put on hold are now started |
| 3742 | // up. |
| 3743 | final int NP = mProcessesOnHold.size(); |
| 3744 | if (NP > 0) { |
| 3745 | ArrayList<ProcessRecord> procs = |
| 3746 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 3747 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3748 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 3749 | + procs.get(ip)); |
| 3750 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3751 | } |
| 3752 | } |
| 3753 | |
| 3754 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3755 | // Start looking for apps that are abusing wake locks. |
| 3756 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3757 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3758 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 3759 | SystemProperties.set("sys.boot_completed", "1"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3760 | broadcastIntentLocked(null, null, |
| 3761 | new Intent(Intent.ACTION_BOOT_COMPLETED, null), |
| 3762 | null, null, 0, null, null, |
| 3763 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
| 3764 | false, false, MY_PID, Process.SYSTEM_UID); |
| 3765 | } |
| 3766 | } |
| 3767 | } |
| 3768 | |
| 3769 | final void ensureBootCompleted() { |
| 3770 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 3771 | boolean enableScreen; |
| 3772 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3773 | booting = mBooting; |
| 3774 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 3775 | enableScreen = !mBooted; |
| 3776 | mBooted = true; |
| 3777 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3778 | |
| 3779 | if (booting) { |
| 3780 | finishBooting(); |
| 3781 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 3782 | |
| 3783 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 3784 | enableScreenAfterBoot(); |
| 3785 | } |
| 3786 | } |
| 3787 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 3788 | public final void activityPaused(IBinder token) { |
| 3789 | final long origId = Binder.clearCallingIdentity(); |
| 3790 | mMainStack.activityPaused(token, false); |
| 3791 | Binder.restoreCallingIdentity(origId); |
| 3792 | } |
| 3793 | |
| 3794 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 3795 | CharSequence description) { |
| 3796 | if (localLOGV) Slog.v( |
| 3797 | TAG, "Activity stopped: token=" + token); |
| 3798 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3799 | // Refuse possible leaked file descriptors |
| 3800 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 3801 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 3802 | } |
| 3803 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3804 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3805 | |
| 3806 | final long origId = Binder.clearCallingIdentity(); |
| 3807 | |
| 3808 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3809 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3810 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3811 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 3812 | r.icicle = icicle; |
| 3813 | r.haveState = true; |
| 3814 | if (thumbnail != null) { |
| 3815 | r.thumbnail = thumbnail; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 3816 | if (r.task != null) { |
| 3817 | r.task.lastThumbnail = r.thumbnail; |
| 3818 | } |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 3819 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3820 | r.description = description; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 3821 | if (r.task != null) { |
| 3822 | r.task.lastDescription = r.description; |
| 3823 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3824 | r.stopped = true; |
| 3825 | r.state = ActivityState.STOPPED; |
| 3826 | if (!r.finishing) { |
| 3827 | if (r.configDestroy) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3828 | r.stack.destroyActivityLocked(r, true); |
| 3829 | r.stack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3830 | } |
| 3831 | } |
| 3832 | } |
| 3833 | } |
| 3834 | |
| 3835 | if (r != null) { |
| 3836 | sendPendingThumbnail(r, null, null, null, false); |
| 3837 | } |
| 3838 | |
| 3839 | trimApplications(); |
| 3840 | |
| 3841 | Binder.restoreCallingIdentity(origId); |
| 3842 | } |
| 3843 | |
| 3844 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3845 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3846 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | public String getCallingPackage(IBinder token) { |
| 3850 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3851 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 3852 | 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] | 3853 | } |
| 3854 | } |
| 3855 | |
| 3856 | public ComponentName getCallingActivity(IBinder token) { |
| 3857 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3858 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3859 | return r != null ? r.intent.getComponent() : null; |
| 3860 | } |
| 3861 | } |
| 3862 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3863 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3864 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3865 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3866 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3867 | if (r != null) { |
| 3868 | return r.resultTo; |
| 3869 | } |
| 3870 | } |
| 3871 | return null; |
| 3872 | } |
| 3873 | |
| 3874 | public ComponentName getActivityClassForToken(IBinder token) { |
| 3875 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3876 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3877 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3878 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3879 | return r.intent.getComponent(); |
| 3880 | } |
| 3881 | return null; |
| 3882 | } |
| 3883 | } |
| 3884 | |
| 3885 | public String getPackageForToken(IBinder token) { |
| 3886 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3887 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3888 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3889 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3890 | return r.packageName; |
| 3891 | } |
| 3892 | return null; |
| 3893 | } |
| 3894 | } |
| 3895 | |
| 3896 | public IIntentSender getIntentSender(int type, |
| 3897 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3898 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3899 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3900 | if (intents != null) { |
| 3901 | if (intents.length < 1) { |
| 3902 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 3903 | } |
| 3904 | for (int i=0; i<intents.length; i++) { |
| 3905 | Intent intent = intents[i]; |
| 3906 | if (intent == null) { |
| 3907 | throw new IllegalArgumentException("Null intent at index " + i); |
| 3908 | } |
| 3909 | if (intent.hasFileDescriptors()) { |
| 3910 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 3911 | } |
| 3912 | if (type == INTENT_SENDER_BROADCAST && |
| 3913 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 3914 | throw new IllegalArgumentException( |
| 3915 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 3916 | } |
| 3917 | intents[i] = new Intent(intent); |
| 3918 | } |
| 3919 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3920 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3921 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3922 | } |
| 3923 | } |
| 3924 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3925 | synchronized(this) { |
| 3926 | int callingUid = Binder.getCallingUid(); |
| 3927 | try { |
| 3928 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID && |
| 3929 | Process.supportsProcesses()) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3930 | int uid = AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3931 | .getPackageUid(packageName); |
| 3932 | if (uid != Binder.getCallingUid()) { |
| 3933 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 3934 | + Binder.getCallingPid() |
| 3935 | + ", uid=" + Binder.getCallingUid() |
| 3936 | + ", (need uid=" + uid + ")" |
| 3937 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3938 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3939 | throw new SecurityException(msg); |
| 3940 | } |
| 3941 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3942 | |
| 3943 | return getIntentSenderLocked(type, packageName, callingUid, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3944 | token, resultWho, requestCode, intents, resolvedTypes, flags); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3945 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3946 | } catch (RemoteException e) { |
| 3947 | throw new SecurityException(e); |
| 3948 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | IIntentSender getIntentSenderLocked(int type, |
| 3953 | String packageName, int callingUid, IBinder token, String resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3954 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3955 | ActivityRecord activity = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3956 | if (type == INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3957 | int index = mMainStack.indexOfTokenLocked(token); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3958 | if (index < 0) { |
| 3959 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3960 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3961 | activity = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3962 | if (activity.finishing) { |
| 3963 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3964 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 3968 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 3969 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 3970 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 3971 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 3972 | |
| 3973 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 3974 | type, packageName, activity, resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3975 | requestCode, intents, resolvedTypes, flags); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3976 | WeakReference<PendingIntentRecord> ref; |
| 3977 | ref = mIntentSenderRecords.get(key); |
| 3978 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 3979 | if (rec != null) { |
| 3980 | if (!cancelCurrent) { |
| 3981 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 3982 | if (rec.key.requestIntent != null) { |
| 3983 | rec.key.requestIntent.replaceExtras(intents != null ? intents[0] : null); |
| 3984 | } |
| 3985 | if (intents != null) { |
| 3986 | intents[intents.length-1] = rec.key.requestIntent; |
| 3987 | rec.key.allIntents = intents; |
| 3988 | rec.key.allResolvedTypes = resolvedTypes; |
| 3989 | } else { |
| 3990 | rec.key.allIntents = null; |
| 3991 | rec.key.allResolvedTypes = null; |
| 3992 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3993 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3994 | return rec; |
| 3995 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3996 | rec.canceled = true; |
| 3997 | mIntentSenderRecords.remove(key); |
| 3998 | } |
| 3999 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4000 | return rec; |
| 4001 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4002 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4003 | mIntentSenderRecords.put(key, rec.ref); |
| 4004 | if (type == INTENT_SENDER_ACTIVITY_RESULT) { |
| 4005 | if (activity.pendingResults == null) { |
| 4006 | activity.pendingResults |
| 4007 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4008 | } |
| 4009 | activity.pendingResults.add(rec.ref); |
| 4010 | } |
| 4011 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4012 | } |
| 4013 | |
| 4014 | public void cancelIntentSender(IIntentSender sender) { |
| 4015 | if (!(sender instanceof PendingIntentRecord)) { |
| 4016 | return; |
| 4017 | } |
| 4018 | synchronized(this) { |
| 4019 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4020 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4021 | int uid = AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4022 | .getPackageUid(rec.key.packageName); |
| 4023 | if (uid != Binder.getCallingUid()) { |
| 4024 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4025 | + Binder.getCallingPid() |
| 4026 | + ", uid=" + Binder.getCallingUid() |
| 4027 | + " is not allowed to cancel packges " |
| 4028 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4029 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4030 | throw new SecurityException(msg); |
| 4031 | } |
| 4032 | } catch (RemoteException e) { |
| 4033 | throw new SecurityException(e); |
| 4034 | } |
| 4035 | cancelIntentSenderLocked(rec, true); |
| 4036 | } |
| 4037 | } |
| 4038 | |
| 4039 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4040 | rec.canceled = true; |
| 4041 | mIntentSenderRecords.remove(rec.key); |
| 4042 | if (cleanActivity && rec.key.activity != null) { |
| 4043 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4044 | } |
| 4045 | } |
| 4046 | |
| 4047 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4048 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4049 | return null; |
| 4050 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4051 | try { |
| 4052 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4053 | return res.key.packageName; |
| 4054 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4055 | } |
| 4056 | return null; |
| 4057 | } |
| 4058 | |
| 4059 | public void setProcessLimit(int max) { |
| 4060 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4061 | "setProcessLimit()"); |
| 4062 | mProcessLimit = max; |
| 4063 | } |
| 4064 | |
| 4065 | public int getProcessLimit() { |
| 4066 | return mProcessLimit; |
| 4067 | } |
| 4068 | |
| 4069 | void foregroundTokenDied(ForegroundToken token) { |
| 4070 | synchronized (ActivityManagerService.this) { |
| 4071 | synchronized (mPidsSelfLocked) { |
| 4072 | ForegroundToken cur |
| 4073 | = mForegroundProcesses.get(token.pid); |
| 4074 | if (cur != token) { |
| 4075 | return; |
| 4076 | } |
| 4077 | mForegroundProcesses.remove(token.pid); |
| 4078 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4079 | if (pr == null) { |
| 4080 | return; |
| 4081 | } |
| 4082 | pr.forcingToForeground = null; |
| 4083 | pr.foregroundServices = false; |
| 4084 | } |
| 4085 | updateOomAdjLocked(); |
| 4086 | } |
| 4087 | } |
| 4088 | |
| 4089 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4090 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4091 | "setProcessForeground()"); |
| 4092 | synchronized(this) { |
| 4093 | boolean changed = false; |
| 4094 | |
| 4095 | synchronized (mPidsSelfLocked) { |
| 4096 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
| 4097 | if (pr == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4098 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4099 | return; |
| 4100 | } |
| 4101 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4102 | if (oldToken != null) { |
| 4103 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4104 | mForegroundProcesses.remove(pid); |
| 4105 | pr.forcingToForeground = null; |
| 4106 | changed = true; |
| 4107 | } |
| 4108 | if (isForeground && token != null) { |
| 4109 | ForegroundToken newToken = new ForegroundToken() { |
| 4110 | public void binderDied() { |
| 4111 | foregroundTokenDied(this); |
| 4112 | } |
| 4113 | }; |
| 4114 | newToken.pid = pid; |
| 4115 | newToken.token = token; |
| 4116 | try { |
| 4117 | token.linkToDeath(newToken, 0); |
| 4118 | mForegroundProcesses.put(pid, newToken); |
| 4119 | pr.forcingToForeground = token; |
| 4120 | changed = true; |
| 4121 | } catch (RemoteException e) { |
| 4122 | // If the process died while doing this, we will later |
| 4123 | // do the cleanup with the process death link. |
| 4124 | } |
| 4125 | } |
| 4126 | } |
| 4127 | |
| 4128 | if (changed) { |
| 4129 | updateOomAdjLocked(); |
| 4130 | } |
| 4131 | } |
| 4132 | } |
| 4133 | |
| 4134 | // ========================================================= |
| 4135 | // PERMISSIONS |
| 4136 | // ========================================================= |
| 4137 | |
| 4138 | static class PermissionController extends IPermissionController.Stub { |
| 4139 | ActivityManagerService mActivityManagerService; |
| 4140 | PermissionController(ActivityManagerService activityManagerService) { |
| 4141 | mActivityManagerService = activityManagerService; |
| 4142 | } |
| 4143 | |
| 4144 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4145 | return mActivityManagerService.checkPermission(permission, pid, |
| 4146 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4147 | } |
| 4148 | } |
| 4149 | |
| 4150 | /** |
| 4151 | * This can be called with or without the global lock held. |
| 4152 | */ |
| 4153 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4154 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4155 | // We might be performing an operation on behalf of an indirect binder |
| 4156 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4157 | // client identity accordingly before proceeding. |
| 4158 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4159 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4160 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4161 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4162 | uid = tlsIdentity.uid; |
| 4163 | pid = tlsIdentity.pid; |
| 4164 | } |
| 4165 | |
| 4166 | // Root, system server and our own process get to do everything. |
| 4167 | if (uid == 0 || uid == Process.SYSTEM_UID || pid == MY_PID || |
| 4168 | !Process.supportsProcesses()) { |
| 4169 | return PackageManager.PERMISSION_GRANTED; |
| 4170 | } |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4171 | // If there is a uid that owns whatever is being accessed, it has |
| 4172 | // blanket access to it regardless of the permissions it requires. |
| 4173 | if (owningUid >= 0 && uid == owningUid) { |
| 4174 | return PackageManager.PERMISSION_GRANTED; |
| 4175 | } |
| 4176 | // If the target is not exported, then nobody else can get to it. |
| 4177 | if (!exported) { |
| 4178 | Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4179 | return PackageManager.PERMISSION_DENIED; |
| 4180 | } |
| 4181 | if (permission == null) { |
| 4182 | return PackageManager.PERMISSION_GRANTED; |
| 4183 | } |
| 4184 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4185 | return AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4186 | .checkUidPermission(permission, uid); |
| 4187 | } catch (RemoteException e) { |
| 4188 | // Should never happen, but if it does... deny! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4189 | Slog.e(TAG, "PackageManager is dead?!?", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4190 | } |
| 4191 | return PackageManager.PERMISSION_DENIED; |
| 4192 | } |
| 4193 | |
| 4194 | /** |
| 4195 | * As the only public entry point for permissions checking, this method |
| 4196 | * can enforce the semantic that requesting a check on a null global |
| 4197 | * permission is automatically denied. (Internally a null permission |
| 4198 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4199 | * when only uid-based security is needed.) |
| 4200 | * |
| 4201 | * This can be called with or without the global lock held. |
| 4202 | */ |
| 4203 | public int checkPermission(String permission, int pid, int uid) { |
| 4204 | if (permission == null) { |
| 4205 | return PackageManager.PERMISSION_DENIED; |
| 4206 | } |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4207 | return checkComponentPermission(permission, pid, uid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | /** |
| 4211 | * Binder IPC calls go through the public entry point. |
| 4212 | * This can be called with or without the global lock held. |
| 4213 | */ |
| 4214 | int checkCallingPermission(String permission) { |
| 4215 | return checkPermission(permission, |
| 4216 | Binder.getCallingPid(), |
| 4217 | Binder.getCallingUid()); |
| 4218 | } |
| 4219 | |
| 4220 | /** |
| 4221 | * This can be called with or without the global lock held. |
| 4222 | */ |
| 4223 | void enforceCallingPermission(String permission, String func) { |
| 4224 | if (checkCallingPermission(permission) |
| 4225 | == PackageManager.PERMISSION_GRANTED) { |
| 4226 | return; |
| 4227 | } |
| 4228 | |
| 4229 | String msg = "Permission Denial: " + func + " from pid=" |
| 4230 | + Binder.getCallingPid() |
| 4231 | + ", uid=" + Binder.getCallingUid() |
| 4232 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4233 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4234 | throw new SecurityException(msg); |
| 4235 | } |
| 4236 | |
| 4237 | private final boolean checkHoldingPermissionsLocked(IPackageManager pm, |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4238 | ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
| 4239 | boolean readPerm = (modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 4240 | boolean writePerm = (modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0; |
| 4241 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4242 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4243 | try { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4244 | // Is the component private from the target uid? |
| 4245 | final boolean prv = !pi.exported && pi.applicationInfo.uid != uid; |
| 4246 | |
| 4247 | // Acceptable if the there is no read permission needed from the |
| 4248 | // target or the target is holding the read permission. |
| 4249 | if (!readPerm) { |
| 4250 | if ((!prv && pi.readPermission == null) || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4251 | (pm.checkUidPermission(pi.readPermission, uid) |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4252 | == PackageManager.PERMISSION_GRANTED)) { |
| 4253 | readPerm = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4254 | } |
| 4255 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4256 | |
| 4257 | // Acceptable if the there is no write permission needed from the |
| 4258 | // target or the target is holding the read permission. |
| 4259 | if (!writePerm) { |
| 4260 | if (!prv && (pi.writePermission == null) || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4261 | (pm.checkUidPermission(pi.writePermission, uid) |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4262 | == PackageManager.PERMISSION_GRANTED)) { |
| 4263 | writePerm = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4264 | } |
| 4265 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4266 | |
| 4267 | // Acceptable if there is a path permission matching the URI that |
| 4268 | // the target holds the permission on. |
| 4269 | PathPermission[] pps = pi.pathPermissions; |
| 4270 | if (pps != null && (!readPerm || !writePerm)) { |
| 4271 | final String path = uri.getPath(); |
| 4272 | int i = pps.length; |
| 4273 | while (i > 0 && (!readPerm || !writePerm)) { |
| 4274 | i--; |
| 4275 | PathPermission pp = pps[i]; |
| 4276 | if (!readPerm) { |
| 4277 | final String pprperm = pp.getReadPermission(); |
| 4278 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 4279 | + pprperm + " for " + pp.getPath() |
| 4280 | + ": match=" + pp.match(path) |
| 4281 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 4282 | if (pprperm != null && pp.match(path) && |
| 4283 | (pm.checkUidPermission(pprperm, uid) |
| 4284 | == PackageManager.PERMISSION_GRANTED)) { |
| 4285 | readPerm = true; |
| 4286 | } |
| 4287 | } |
| 4288 | if (!writePerm) { |
| 4289 | final String ppwperm = pp.getWritePermission(); |
| 4290 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 4291 | + ppwperm + " for " + pp.getPath() |
| 4292 | + ": match=" + pp.match(path) |
| 4293 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 4294 | if (ppwperm != null && pp.match(path) && |
| 4295 | (pm.checkUidPermission(ppwperm, uid) |
| 4296 | == PackageManager.PERMISSION_GRANTED)) { |
| 4297 | writePerm = true; |
| 4298 | } |
| 4299 | } |
| 4300 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4301 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4302 | } catch (RemoteException e) { |
| 4303 | return false; |
| 4304 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4305 | |
| 4306 | return readPerm && writePerm; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 4310 | int modeFlags) { |
| 4311 | // Root gets to do everything. |
| 4312 | if (uid == 0 || !Process.supportsProcesses()) { |
| 4313 | return true; |
| 4314 | } |
| 4315 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 4316 | if (perms == null) return false; |
| 4317 | UriPermission perm = perms.get(uri); |
| 4318 | if (perm == null) return false; |
| 4319 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 4320 | } |
| 4321 | |
| 4322 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
| 4323 | // Another redirected-binder-call permissions check as in |
| 4324 | // {@link checkComponentPermission}. |
| 4325 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4326 | if (tlsIdentity != null) { |
| 4327 | uid = tlsIdentity.uid; |
| 4328 | pid = tlsIdentity.pid; |
| 4329 | } |
| 4330 | |
| 4331 | // Our own process gets to do everything. |
| 4332 | if (pid == MY_PID) { |
| 4333 | return PackageManager.PERMISSION_GRANTED; |
| 4334 | } |
| 4335 | synchronized(this) { |
| 4336 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 4337 | ? PackageManager.PERMISSION_GRANTED |
| 4338 | : PackageManager.PERMISSION_DENIED; |
| 4339 | } |
| 4340 | } |
| 4341 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4342 | /** |
| 4343 | * Check if the targetPkg can be granted permission to access uri by |
| 4344 | * the callingUid using the given modeFlags. Throws a security exception |
| 4345 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 4346 | * if the URI permission grant should be performed; returns -1 if it is not |
| 4347 | * needed (for example targetPkg already has permission to access the URI). |
| 4348 | */ |
| 4349 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
| 4350 | Uri uri, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4351 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4352 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4353 | if (modeFlags == 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4354 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4355 | } |
| 4356 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4357 | if (targetPkg != null) { |
| 4358 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4359 | "Checking grant " + targetPkg + " permission to " + uri); |
| 4360 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4361 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4362 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4363 | |
| 4364 | // If this is not a content: uri, we can't do anything with it. |
| 4365 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4366 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4367 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4368 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4369 | } |
| 4370 | |
| 4371 | String name = uri.getAuthority(); |
| 4372 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4373 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4374 | if (cpr != null) { |
| 4375 | pi = cpr.info; |
| 4376 | } else { |
| 4377 | try { |
| 4378 | pi = pm.resolveContentProvider(name, |
| 4379 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4380 | } catch (RemoteException ex) { |
| 4381 | } |
| 4382 | } |
| 4383 | if (pi == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4384 | Slog.w(TAG, "No content provider found for: " + name); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4385 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4386 | } |
| 4387 | |
| 4388 | int targetUid; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4389 | if (targetPkg != null) { |
| 4390 | try { |
| 4391 | targetUid = pm.getPackageUid(targetPkg); |
| 4392 | if (targetUid < 0) { |
| 4393 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4394 | "Can't grant URI permission no uid for: " + targetPkg); |
| 4395 | return -1; |
| 4396 | } |
| 4397 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4398 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4399 | } |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4400 | } else { |
| 4401 | targetUid = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4402 | } |
| 4403 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4404 | if (targetUid >= 0) { |
| 4405 | // First... does the target actually need this permission? |
| 4406 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 4407 | // No need to grant the target this permission. |
| 4408 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4409 | "Target " + targetPkg + " already has full permission to " + uri); |
| 4410 | return -1; |
| 4411 | } |
| 4412 | } else { |
| 4413 | // First... there is no target package, so can anyone access it? |
| 4414 | boolean allowed = pi.exported; |
| 4415 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4416 | if (pi.readPermission != null) { |
| 4417 | allowed = false; |
| 4418 | } |
| 4419 | } |
| 4420 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4421 | if (pi.writePermission != null) { |
| 4422 | allowed = false; |
| 4423 | } |
| 4424 | } |
| 4425 | if (allowed) { |
| 4426 | return -1; |
| 4427 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4428 | } |
| 4429 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4430 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4431 | if (!pi.grantUriPermissions) { |
| 4432 | throw new SecurityException("Provider " + pi.packageName |
| 4433 | + "/" + pi.name |
| 4434 | + " does not allow granting of Uri permissions (uri " |
| 4435 | + uri + ")"); |
| 4436 | } |
| 4437 | if (pi.uriPermissionPatterns != null) { |
| 4438 | final int N = pi.uriPermissionPatterns.length; |
| 4439 | boolean allowed = false; |
| 4440 | for (int i=0; i<N; i++) { |
| 4441 | if (pi.uriPermissionPatterns[i] != null |
| 4442 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 4443 | allowed = true; |
| 4444 | break; |
| 4445 | } |
| 4446 | } |
| 4447 | if (!allowed) { |
| 4448 | throw new SecurityException("Provider " + pi.packageName |
| 4449 | + "/" + pi.name |
| 4450 | + " does not allow granting of permission to path of Uri " |
| 4451 | + uri); |
| 4452 | } |
| 4453 | } |
| 4454 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4455 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4456 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4457 | if (callingUid != Process.myUid()) { |
| 4458 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 4459 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4460 | throw new SecurityException("Uid " + callingUid |
| 4461 | + " does not have permission to uri " + uri); |
| 4462 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4463 | } |
| 4464 | } |
| 4465 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4466 | return targetUid; |
| 4467 | } |
| 4468 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4469 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 4470 | Uri uri, int modeFlags) { |
| 4471 | synchronized(this) { |
| 4472 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags); |
| 4473 | } |
| 4474 | } |
| 4475 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4476 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 4477 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 4478 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4479 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4480 | if (modeFlags == 0) { |
| 4481 | return; |
| 4482 | } |
| 4483 | |
| 4484 | // 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] | 4485 | // to the uri, and the target doesn't. Let's now give this to |
| 4486 | // the target. |
| 4487 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4488 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4489 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4490 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4491 | HashMap<Uri, UriPermission> targetUris |
| 4492 | = mGrantedUriPermissions.get(targetUid); |
| 4493 | if (targetUris == null) { |
| 4494 | targetUris = new HashMap<Uri, UriPermission>(); |
| 4495 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 4496 | } |
| 4497 | |
| 4498 | UriPermission perm = targetUris.get(uri); |
| 4499 | if (perm == null) { |
| 4500 | perm = new UriPermission(targetUid, uri); |
| 4501 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4502 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4503 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4504 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4505 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4506 | perm.globalModeFlags |= modeFlags; |
| 4507 | } else if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4508 | perm.readOwners.add(owner); |
| 4509 | owner.addReadPermission(perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4510 | } else if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4511 | perm.writeOwners.add(owner); |
| 4512 | owner.addWritePermission(perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4513 | } |
| 4514 | } |
| 4515 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4516 | void grantUriPermissionLocked(int callingUid, |
| 4517 | String targetPkg, Uri uri, int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4518 | if (targetPkg == null) { |
| 4519 | throw new NullPointerException("targetPkg"); |
| 4520 | } |
| 4521 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4522 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags); |
| 4523 | if (targetUid < 0) { |
| 4524 | return; |
| 4525 | } |
| 4526 | |
| 4527 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 4528 | } |
| 4529 | |
| 4530 | /** |
| 4531 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 4532 | */ |
| 4533 | int checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 4534 | String targetPkg, Intent intent) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4535 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4536 | "Checking URI perm to " + (intent != null ? intent.getData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4537 | + " from " + intent + "; flags=0x" |
| 4538 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 4539 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4540 | if (targetPkg == null) { |
| 4541 | throw new NullPointerException("targetPkg"); |
| 4542 | } |
| 4543 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4544 | if (intent == null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4545 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4546 | } |
| 4547 | Uri data = intent.getData(); |
| 4548 | if (data == null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4549 | return -1; |
| 4550 | } |
| 4551 | return checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 4552 | intent.getFlags()); |
| 4553 | } |
| 4554 | |
| 4555 | /** |
| 4556 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 4557 | */ |
| 4558 | void grantUriPermissionUncheckedFromIntentLocked(int targetUid, |
| 4559 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
| 4560 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, intent.getData(), |
| 4561 | intent.getFlags(), owner); |
| 4562 | } |
| 4563 | |
| 4564 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 4565 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
| 4566 | int targetUid = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, intent); |
| 4567 | if (targetUid < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4568 | return; |
| 4569 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4570 | |
| 4571 | grantUriPermissionUncheckedFromIntentLocked(targetUid, targetPkg, intent, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 4575 | Uri uri, int modeFlags) { |
| 4576 | synchronized(this) { |
| 4577 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 4578 | if (r == null) { |
| 4579 | throw new SecurityException("Unable to find app for caller " |
| 4580 | + caller |
| 4581 | + " when granting permission to uri " + uri); |
| 4582 | } |
| 4583 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4584 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4585 | } |
| 4586 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4587 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4588 | } |
| 4589 | |
| 4590 | grantUriPermissionLocked(r.info.uid, targetPkg, uri, modeFlags, |
| 4591 | null); |
| 4592 | } |
| 4593 | } |
| 4594 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4595 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4596 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4597 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 4598 | HashMap<Uri, UriPermission> perms |
| 4599 | = mGrantedUriPermissions.get(perm.uid); |
| 4600 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4601 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4602 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4603 | perms.remove(perm.uri); |
| 4604 | if (perms.size() == 0) { |
| 4605 | mGrantedUriPermissions.remove(perm.uid); |
| 4606 | } |
| 4607 | } |
| 4608 | } |
| 4609 | } |
| 4610 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4611 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 4612 | int modeFlags) { |
| 4613 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4614 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4615 | if (modeFlags == 0) { |
| 4616 | return; |
| 4617 | } |
| 4618 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4619 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4620 | "Revoking all granted permissions to " + uri); |
| 4621 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4622 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4623 | |
| 4624 | final String authority = uri.getAuthority(); |
| 4625 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4626 | ContentProviderRecord cpr = mProvidersByName.get(authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4627 | if (cpr != null) { |
| 4628 | pi = cpr.info; |
| 4629 | } else { |
| 4630 | try { |
| 4631 | pi = pm.resolveContentProvider(authority, |
| 4632 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4633 | } catch (RemoteException ex) { |
| 4634 | } |
| 4635 | } |
| 4636 | if (pi == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4637 | Slog.w(TAG, "No content provider found for: " + authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4638 | return; |
| 4639 | } |
| 4640 | |
| 4641 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4642 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4643 | // Right now, if you are not the original owner of the permission, |
| 4644 | // you are not allowed to revoke it. |
| 4645 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4646 | throw new SecurityException("Uid " + callingUid |
| 4647 | + " does not have permission to uri " + uri); |
| 4648 | //} |
| 4649 | } |
| 4650 | |
| 4651 | // Go through all of the permissions and remove any that match. |
| 4652 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 4653 | if (SEGMENTS != null) { |
| 4654 | final int NS = SEGMENTS.size(); |
| 4655 | int N = mGrantedUriPermissions.size(); |
| 4656 | for (int i=0; i<N; i++) { |
| 4657 | HashMap<Uri, UriPermission> perms |
| 4658 | = mGrantedUriPermissions.valueAt(i); |
| 4659 | Iterator<UriPermission> it = perms.values().iterator(); |
| 4660 | toploop: |
| 4661 | while (it.hasNext()) { |
| 4662 | UriPermission perm = it.next(); |
| 4663 | Uri targetUri = perm.uri; |
| 4664 | if (!authority.equals(targetUri.getAuthority())) { |
| 4665 | continue; |
| 4666 | } |
| 4667 | List<String> targetSegments = targetUri.getPathSegments(); |
| 4668 | if (targetSegments == null) { |
| 4669 | continue; |
| 4670 | } |
| 4671 | if (targetSegments.size() < NS) { |
| 4672 | continue; |
| 4673 | } |
| 4674 | for (int j=0; j<NS; j++) { |
| 4675 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 4676 | continue toploop; |
| 4677 | } |
| 4678 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4679 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4680 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4681 | perm.clearModes(modeFlags); |
| 4682 | if (perm.modeFlags == 0) { |
| 4683 | it.remove(); |
| 4684 | } |
| 4685 | } |
| 4686 | if (perms.size() == 0) { |
| 4687 | mGrantedUriPermissions.remove( |
| 4688 | mGrantedUriPermissions.keyAt(i)); |
| 4689 | N--; |
| 4690 | i--; |
| 4691 | } |
| 4692 | } |
| 4693 | } |
| 4694 | } |
| 4695 | |
| 4696 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 4697 | int modeFlags) { |
| 4698 | synchronized(this) { |
| 4699 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 4700 | if (r == null) { |
| 4701 | throw new SecurityException("Unable to find app for caller " |
| 4702 | + caller |
| 4703 | + " when revoking permission to uri " + uri); |
| 4704 | } |
| 4705 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4706 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4707 | return; |
| 4708 | } |
| 4709 | |
| 4710 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4711 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4712 | if (modeFlags == 0) { |
| 4713 | return; |
| 4714 | } |
| 4715 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4716 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4717 | |
| 4718 | final String authority = uri.getAuthority(); |
| 4719 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4720 | ContentProviderRecord cpr = mProvidersByName.get(authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4721 | if (cpr != null) { |
| 4722 | pi = cpr.info; |
| 4723 | } else { |
| 4724 | try { |
| 4725 | pi = pm.resolveContentProvider(authority, |
| 4726 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4727 | } catch (RemoteException ex) { |
| 4728 | } |
| 4729 | } |
| 4730 | if (pi == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4731 | Slog.w(TAG, "No content provider found for: " + authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4732 | return; |
| 4733 | } |
| 4734 | |
| 4735 | revokeUriPermissionLocked(r.info.uid, uri, modeFlags); |
| 4736 | } |
| 4737 | } |
| 4738 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4739 | @Override |
| 4740 | public IBinder newUriPermissionOwner(String name) { |
| 4741 | synchronized(this) { |
| 4742 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 4743 | return owner.getExternalTokenLocked(); |
| 4744 | } |
| 4745 | } |
| 4746 | |
| 4747 | @Override |
| 4748 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 4749 | Uri uri, int modeFlags) { |
| 4750 | synchronized(this) { |
| 4751 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 4752 | if (owner == null) { |
| 4753 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 4754 | } |
| 4755 | if (fromUid != Binder.getCallingUid()) { |
| 4756 | if (Binder.getCallingUid() != Process.myUid()) { |
| 4757 | // Only system code can grant URI permissions on behalf |
| 4758 | // of other users. |
| 4759 | throw new SecurityException("nice try"); |
| 4760 | } |
| 4761 | } |
| 4762 | if (targetPkg == null) { |
| 4763 | throw new IllegalArgumentException("null target"); |
| 4764 | } |
| 4765 | if (uri == null) { |
| 4766 | throw new IllegalArgumentException("null uri"); |
| 4767 | } |
| 4768 | |
| 4769 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 4770 | } |
| 4771 | } |
| 4772 | |
| 4773 | @Override |
| 4774 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 4775 | synchronized(this) { |
| 4776 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 4777 | if (owner == null) { |
| 4778 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 4779 | } |
| 4780 | |
| 4781 | if (uri == null) { |
| 4782 | owner.removeUriPermissionsLocked(mode); |
| 4783 | } else { |
| 4784 | owner.removeUriPermissionLocked(uri, mode); |
| 4785 | } |
| 4786 | } |
| 4787 | } |
| 4788 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4789 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 4790 | synchronized (this) { |
| 4791 | ProcessRecord app = |
| 4792 | who != null ? getRecordForAppLocked(who) : null; |
| 4793 | if (app == null) return; |
| 4794 | |
| 4795 | Message msg = Message.obtain(); |
| 4796 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 4797 | msg.obj = app; |
| 4798 | msg.arg1 = waiting ? 1 : 0; |
| 4799 | mHandler.sendMessage(msg); |
| 4800 | } |
| 4801 | } |
| 4802 | |
| 4803 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
| 4804 | outInfo.availMem = Process.getFreeMemory(); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 4805 | outInfo.threshold = HOME_APP_MEM; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4806 | outInfo.lowMemory = outInfo.availMem < |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 4807 | (HOME_APP_MEM + ((HIDDEN_APP_MEM-HOME_APP_MEM)/2)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4808 | } |
| 4809 | |
| 4810 | // ========================================================= |
| 4811 | // TASK MANAGEMENT |
| 4812 | // ========================================================= |
| 4813 | |
| 4814 | public List getTasks(int maxNum, int flags, |
| 4815 | IThumbnailReceiver receiver) { |
| 4816 | ArrayList list = new ArrayList(); |
| 4817 | |
| 4818 | PendingThumbnailsRecord pending = null; |
| 4819 | IApplicationThread topThumbnail = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4820 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4821 | |
| 4822 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4823 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4824 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 4825 | + ", receiver=" + receiver); |
| 4826 | |
| 4827 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 4828 | != PackageManager.PERMISSION_GRANTED) { |
| 4829 | if (receiver != null) { |
| 4830 | // If the caller wants to wait for pending thumbnails, |
| 4831 | // it ain't gonna get them. |
| 4832 | try { |
| 4833 | receiver.finished(); |
| 4834 | } catch (RemoteException ex) { |
| 4835 | } |
| 4836 | } |
| 4837 | String msg = "Permission Denial: getTasks() from pid=" |
| 4838 | + Binder.getCallingPid() |
| 4839 | + ", uid=" + Binder.getCallingUid() |
| 4840 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4841 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4842 | throw new SecurityException(msg); |
| 4843 | } |
| 4844 | |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 4845 | final boolean canReadFb = (flags&ActivityManager.TASKS_GET_THUMBNAILS) != 0 |
| 4846 | && checkCallingPermission( |
| 4847 | android.Manifest.permission.READ_FRAME_BUFFER) |
| 4848 | == PackageManager.PERMISSION_GRANTED; |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4849 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4850 | int pos = mMainStack.mHistory.size()-1; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4851 | ActivityRecord next = |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4852 | pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4853 | ActivityRecord top = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4854 | CharSequence topDescription = null; |
| 4855 | TaskRecord curTask = null; |
| 4856 | int numActivities = 0; |
| 4857 | int numRunning = 0; |
| 4858 | while (pos >= 0 && maxNum > 0) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4859 | final ActivityRecord r = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4860 | pos--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4861 | next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4862 | |
| 4863 | // Initialize state for next task if needed. |
| 4864 | if (top == null || |
| 4865 | (top.state == ActivityState.INITIALIZING |
| 4866 | && top.task == r.task)) { |
| 4867 | top = r; |
| 4868 | topDescription = r.description; |
| 4869 | curTask = r.task; |
| 4870 | numActivities = numRunning = 0; |
| 4871 | } |
| 4872 | |
| 4873 | // Add 'r' into the current task. |
| 4874 | numActivities++; |
| 4875 | if (r.app != null && r.app.thread != null) { |
| 4876 | numRunning++; |
| 4877 | } |
| 4878 | if (topDescription == null) { |
| 4879 | topDescription = r.description; |
| 4880 | } |
| 4881 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4882 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4883 | TAG, r.intent.getComponent().flattenToShortString() |
| 4884 | + ": task=" + r.task); |
| 4885 | |
| 4886 | // If the next one is a different task, generate a new |
| 4887 | // TaskInfo entry for what we have. |
| 4888 | if (next == null || next.task != curTask) { |
| 4889 | ActivityManager.RunningTaskInfo ci |
| 4890 | = new ActivityManager.RunningTaskInfo(); |
| 4891 | ci.id = curTask.taskId; |
| 4892 | ci.baseActivity = r.intent.getComponent(); |
| 4893 | ci.topActivity = top.intent.getComponent(); |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4894 | if (canReadFb) { |
| 4895 | if (top.thumbnail != null) { |
| 4896 | ci.thumbnail = top.thumbnail; |
| 4897 | } else if (top.state == ActivityState.RESUMED) { |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 4898 | ci.thumbnail = top.stack.screenshotActivities(top); |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4899 | } |
| 4900 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4901 | ci.description = topDescription; |
| 4902 | ci.numActivities = numActivities; |
| 4903 | ci.numRunning = numRunning; |
| 4904 | //System.out.println( |
| 4905 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 4906 | if (ci.thumbnail == null && receiver != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4907 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4908 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 4909 | + " app=" + top.app |
| 4910 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 4911 | if (top.state == ActivityState.RESUMED |
| 4912 | || top.state == ActivityState.PAUSING) { |
| 4913 | if (top.idle && top.app != null |
| 4914 | && top.app.thread != null) { |
| 4915 | topRecord = top; |
| 4916 | topThumbnail = top.app.thread; |
| 4917 | } else { |
| 4918 | top.thumbnailNeeded = true; |
| 4919 | } |
| 4920 | } |
| 4921 | if (pending == null) { |
| 4922 | pending = new PendingThumbnailsRecord(receiver); |
| 4923 | } |
| 4924 | pending.pendingRecords.add(top); |
| 4925 | } |
| 4926 | list.add(ci); |
| 4927 | maxNum--; |
| 4928 | top = null; |
| 4929 | } |
| 4930 | } |
| 4931 | |
| 4932 | if (pending != null) { |
| 4933 | mPendingThumbnails.add(pending); |
| 4934 | } |
| 4935 | } |
| 4936 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4937 | 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] | 4938 | |
| 4939 | if (topThumbnail != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4940 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4941 | try { |
| 4942 | topThumbnail.requestThumbnail(topRecord); |
| 4943 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4944 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4945 | sendPendingThumbnail(null, topRecord, null, null, true); |
| 4946 | } |
| 4947 | } |
| 4948 | |
| 4949 | if (pending == null && receiver != null) { |
| 4950 | // In this case all thumbnails were available and the client |
| 4951 | // is being asked to be told when the remaining ones come in... |
| 4952 | // which is unusually, since the top-most currently running |
| 4953 | // activity should never have a canned thumbnail! Oh well. |
| 4954 | try { |
| 4955 | receiver.finished(); |
| 4956 | } catch (RemoteException ex) { |
| 4957 | } |
| 4958 | } |
| 4959 | |
| 4960 | return list; |
| 4961 | } |
| 4962 | |
| 4963 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
| 4964 | int flags) { |
| 4965 | synchronized (this) { |
| 4966 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 4967 | "getRecentTasks()"); |
| 4968 | |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 4969 | final boolean canReadFb = (flags&ActivityManager.TASKS_GET_THUMBNAILS) != 0 |
| 4970 | && checkCallingPermission( |
| 4971 | android.Manifest.permission.READ_FRAME_BUFFER) |
| 4972 | == PackageManager.PERMISSION_GRANTED; |
| 4973 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4974 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 4975 | |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 4976 | ActivityRecord resumed = mMainStack.mResumedActivity; |
| 4977 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4978 | final int N = mRecentTasks.size(); |
| 4979 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 4980 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 4981 | maxNum < N ? maxNum : N); |
| 4982 | for (int i=0; i<N && maxNum > 0; i++) { |
| 4983 | TaskRecord tr = mRecentTasks.get(i); |
| 4984 | if (((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
| 4985 | || (tr.intent == null) |
| 4986 | || ((tr.intent.getFlags() |
| 4987 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 4988 | ActivityManager.RecentTaskInfo rti |
| 4989 | = new ActivityManager.RecentTaskInfo(); |
| 4990 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
| 4991 | rti.baseIntent = new Intent( |
| 4992 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 4993 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 4994 | |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 4995 | if (canReadFb) { |
| 4996 | if (resumed != null && resumed.task == tr) { |
| 4997 | rti.thumbnail = resumed.stack.screenshotActivities(resumed); |
| 4998 | } else { |
| 4999 | rti.thumbnail = tr.lastThumbnail; |
| 5000 | } |
| 5001 | } |
| 5002 | rti.description = tr.lastDescription; |
| 5003 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5004 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5005 | // Check whether this activity is currently available. |
| 5006 | try { |
| 5007 | if (rti.origActivity != null) { |
| 5008 | if (pm.getActivityInfo(rti.origActivity, 0) == null) { |
| 5009 | continue; |
| 5010 | } |
| 5011 | } else if (rti.baseIntent != null) { |
| 5012 | if (pm.queryIntentActivities(rti.baseIntent, |
| 5013 | null, 0) == null) { |
| 5014 | continue; |
| 5015 | } |
| 5016 | } |
| 5017 | } catch (RemoteException e) { |
| 5018 | // Will never happen. |
| 5019 | } |
| 5020 | } |
| 5021 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5022 | res.add(rti); |
| 5023 | maxNum--; |
| 5024 | } |
| 5025 | } |
| 5026 | return res; |
| 5027 | } |
| 5028 | } |
| 5029 | |
| 5030 | private final int findAffinityTaskTopLocked(int startIndex, String affinity) { |
| 5031 | int j; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5032 | TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5033 | TaskRecord jt = startTask; |
| 5034 | |
| 5035 | // First look backwards |
| 5036 | for (j=startIndex-1; j>=0; j--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5037 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5038 | if (r.task != jt) { |
| 5039 | jt = r.task; |
| 5040 | if (affinity.equals(jt.affinity)) { |
| 5041 | return j; |
| 5042 | } |
| 5043 | } |
| 5044 | } |
| 5045 | |
| 5046 | // Now look forwards |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5047 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5048 | jt = startTask; |
| 5049 | for (j=startIndex+1; j<N; j++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5050 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5051 | if (r.task != jt) { |
| 5052 | if (affinity.equals(jt.affinity)) { |
| 5053 | return j; |
| 5054 | } |
| 5055 | jt = r.task; |
| 5056 | } |
| 5057 | } |
| 5058 | |
| 5059 | // Might it be at the top? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5060 | 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] | 5061 | return N-1; |
| 5062 | } |
| 5063 | |
| 5064 | return -1; |
| 5065 | } |
| 5066 | |
| 5067 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5068 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5069 | */ |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5070 | public void moveTaskToFront(int task, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5071 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5072 | "moveTaskToFront()"); |
| 5073 | |
| 5074 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5075 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5076 | Binder.getCallingUid(), "Task to front")) { |
| 5077 | return; |
| 5078 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5079 | final long origId = Binder.clearCallingIdentity(); |
| 5080 | try { |
| 5081 | int N = mRecentTasks.size(); |
| 5082 | for (int i=0; i<N; i++) { |
| 5083 | TaskRecord tr = mRecentTasks.get(i); |
| 5084 | if (tr.taskId == task) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5085 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5086 | // Caller wants the home activity moved with it. To accomplish this, |
| 5087 | // we'll just move the home task to the top first. |
| 5088 | mMainStack.moveHomeToFrontLocked(); |
| 5089 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5090 | mMainStack.moveTaskToFrontLocked(tr, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5091 | return; |
| 5092 | } |
| 5093 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5094 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 5095 | ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5096 | if (hr.task.taskId == task) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5097 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5098 | // Caller wants the home activity moved with it. To accomplish this, |
| 5099 | // we'll just move the home task to the top first. |
| 5100 | mMainStack.moveHomeToFrontLocked(); |
| 5101 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5102 | mMainStack.moveTaskToFrontLocked(hr.task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5103 | return; |
| 5104 | } |
| 5105 | } |
| 5106 | } finally { |
| 5107 | Binder.restoreCallingIdentity(origId); |
| 5108 | } |
| 5109 | } |
| 5110 | } |
| 5111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5112 | public void moveTaskToBack(int task) { |
| 5113 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5114 | "moveTaskToBack()"); |
| 5115 | |
| 5116 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5117 | if (mMainStack.mResumedActivity != null |
| 5118 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5119 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5120 | Binder.getCallingUid(), "Task to back")) { |
| 5121 | return; |
| 5122 | } |
| 5123 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5124 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5125 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5126 | Binder.restoreCallingIdentity(origId); |
| 5127 | } |
| 5128 | } |
| 5129 | |
| 5130 | /** |
| 5131 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 5132 | * of the history stack. The activity's order within the task is unchanged. |
| 5133 | * |
| 5134 | * @param token A reference to the activity we wish to move |
| 5135 | * @param nonRoot If false then this only works if the activity is the root |
| 5136 | * of a task; if true it will work for any activity in a task. |
| 5137 | * @return Returns true if the move completed, false if not. |
| 5138 | */ |
| 5139 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
| 5140 | synchronized(this) { |
| 5141 | final long origId = Binder.clearCallingIdentity(); |
| 5142 | int taskId = getTaskForActivityLocked(token, !nonRoot); |
| 5143 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5144 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5145 | } |
| 5146 | Binder.restoreCallingIdentity(origId); |
| 5147 | } |
| 5148 | return false; |
| 5149 | } |
| 5150 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5151 | public void moveTaskBackwards(int task) { |
| 5152 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5153 | "moveTaskBackwards()"); |
| 5154 | |
| 5155 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5156 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5157 | Binder.getCallingUid(), "Task backwards")) { |
| 5158 | return; |
| 5159 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5160 | final long origId = Binder.clearCallingIdentity(); |
| 5161 | moveTaskBackwardsLocked(task); |
| 5162 | Binder.restoreCallingIdentity(origId); |
| 5163 | } |
| 5164 | } |
| 5165 | |
| 5166 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5167 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5168 | } |
| 5169 | |
| 5170 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 5171 | synchronized(this) { |
| 5172 | return getTaskForActivityLocked(token, onlyRoot); |
| 5173 | } |
| 5174 | } |
| 5175 | |
| 5176 | int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5177 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5178 | TaskRecord lastTask = null; |
| 5179 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5180 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5181 | if (r == token) { |
| 5182 | if (!onlyRoot || lastTask != r.task) { |
| 5183 | return r.task.taskId; |
| 5184 | } |
| 5185 | return -1; |
| 5186 | } |
| 5187 | lastTask = r.task; |
| 5188 | } |
| 5189 | |
| 5190 | return -1; |
| 5191 | } |
| 5192 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5193 | public void finishOtherInstances(IBinder token, ComponentName className) { |
| 5194 | synchronized(this) { |
| 5195 | final long origId = Binder.clearCallingIdentity(); |
| 5196 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5197 | int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5198 | TaskRecord lastTask = null; |
| 5199 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5200 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5201 | if (r.realActivity.equals(className) |
| 5202 | && r != token && lastTask != r.task) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5203 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5204 | null, "others")) { |
| 5205 | i--; |
| 5206 | N--; |
| 5207 | } |
| 5208 | } |
| 5209 | lastTask = r.task; |
| 5210 | } |
| 5211 | |
| 5212 | Binder.restoreCallingIdentity(origId); |
| 5213 | } |
| 5214 | } |
| 5215 | |
| 5216 | // ========================================================= |
| 5217 | // THUMBNAILS |
| 5218 | // ========================================================= |
| 5219 | |
| 5220 | public void reportThumbnail(IBinder token, |
| 5221 | Bitmap thumbnail, CharSequence description) { |
| 5222 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 5223 | final long origId = Binder.clearCallingIdentity(); |
| 5224 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 5225 | Binder.restoreCallingIdentity(origId); |
| 5226 | } |
| 5227 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5228 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5229 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 5230 | TaskRecord task = null; |
| 5231 | ArrayList receivers = null; |
| 5232 | |
| 5233 | //System.out.println("Send pending thumbnail: " + r); |
| 5234 | |
| 5235 | synchronized(this) { |
| 5236 | if (r == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5237 | int index = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5238 | if (index < 0) { |
| 5239 | return; |
| 5240 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5241 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5242 | } |
| 5243 | if (thumbnail == null) { |
| 5244 | thumbnail = r.thumbnail; |
| 5245 | description = r.description; |
| 5246 | } |
| 5247 | if (thumbnail == null && !always) { |
| 5248 | // If there is no thumbnail, and this entry is not actually |
| 5249 | // going away, then abort for now and pick up the next |
| 5250 | // thumbnail we get. |
| 5251 | return; |
| 5252 | } |
| 5253 | task = r.task; |
| 5254 | |
| 5255 | int N = mPendingThumbnails.size(); |
| 5256 | int i=0; |
| 5257 | while (i<N) { |
| 5258 | PendingThumbnailsRecord pr = |
| 5259 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 5260 | //System.out.println("Looking in " + pr.pendingRecords); |
| 5261 | if (pr.pendingRecords.remove(r)) { |
| 5262 | if (receivers == null) { |
| 5263 | receivers = new ArrayList(); |
| 5264 | } |
| 5265 | receivers.add(pr); |
| 5266 | if (pr.pendingRecords.size() == 0) { |
| 5267 | pr.finished = true; |
| 5268 | mPendingThumbnails.remove(i); |
| 5269 | N--; |
| 5270 | continue; |
| 5271 | } |
| 5272 | } |
| 5273 | i++; |
| 5274 | } |
| 5275 | } |
| 5276 | |
| 5277 | if (receivers != null) { |
| 5278 | final int N = receivers.size(); |
| 5279 | for (int i=0; i<N; i++) { |
| 5280 | try { |
| 5281 | PendingThumbnailsRecord pr = |
| 5282 | (PendingThumbnailsRecord)receivers.get(i); |
| 5283 | pr.receiver.newThumbnail( |
| 5284 | task != null ? task.taskId : -1, thumbnail, description); |
| 5285 | if (pr.finished) { |
| 5286 | pr.receiver.finished(); |
| 5287 | } |
| 5288 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5289 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5290 | } |
| 5291 | } |
| 5292 | } |
| 5293 | } |
| 5294 | |
| 5295 | // ========================================================= |
| 5296 | // CONTENT PROVIDERS |
| 5297 | // ========================================================= |
| 5298 | |
| 5299 | private final List generateApplicationProvidersLocked(ProcessRecord app) { |
| 5300 | List providers = null; |
| 5301 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5302 | providers = AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5303 | queryContentProviders(app.processName, app.info.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5304 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5305 | } catch (RemoteException ex) { |
| 5306 | } |
| 5307 | if (providers != null) { |
| 5308 | final int N = providers.size(); |
| 5309 | for (int i=0; i<N; i++) { |
| 5310 | ProviderInfo cpi = |
| 5311 | (ProviderInfo)providers.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5312 | ContentProviderRecord cpr = mProvidersByClass.get(cpi.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5313 | if (cpr == null) { |
| 5314 | cpr = new ContentProviderRecord(cpi, app.info); |
| 5315 | mProvidersByClass.put(cpi.name, cpr); |
| 5316 | } |
| 5317 | app.pubProviders.put(cpi.name, cpr); |
| 5318 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 5319 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5320 | } |
| 5321 | } |
| 5322 | return providers; |
| 5323 | } |
| 5324 | |
| 5325 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5326 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5327 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
| 5328 | final int callingUid = (r != null) ? r.info.uid : Binder.getCallingUid(); |
| 5329 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5330 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5331 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5332 | return null; |
| 5333 | } |
| 5334 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5335 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5336 | == PackageManager.PERMISSION_GRANTED) { |
| 5337 | return null; |
| 5338 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5339 | |
| 5340 | PathPermission[] pps = cpi.pathPermissions; |
| 5341 | if (pps != null) { |
| 5342 | int i = pps.length; |
| 5343 | while (i > 0) { |
| 5344 | i--; |
| 5345 | PathPermission pp = pps[i]; |
| 5346 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5347 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5348 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5349 | return null; |
| 5350 | } |
| 5351 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5352 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5353 | == PackageManager.PERMISSION_GRANTED) { |
| 5354 | return null; |
| 5355 | } |
| 5356 | } |
| 5357 | } |
| 5358 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5359 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 5360 | if (perms != null) { |
| 5361 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 5362 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 5363 | return null; |
| 5364 | } |
| 5365 | } |
| 5366 | } |
| 5367 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5368 | String msg; |
| 5369 | if (!cpi.exported) { |
| 5370 | msg = "Permission Denial: opening provider " + cpi.name |
| 5371 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 5372 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 5373 | + cpi.applicationInfo.uid; |
| 5374 | } else { |
| 5375 | msg = "Permission Denial: opening provider " + cpi.name |
| 5376 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 5377 | + ", uid=" + callingUid + ") requires " |
| 5378 | + cpi.readPermission + " or " + cpi.writePermission; |
| 5379 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5380 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5381 | return msg; |
| 5382 | } |
| 5383 | |
| 5384 | private final ContentProviderHolder getContentProviderImpl( |
| 5385 | IApplicationThread caller, String name) { |
| 5386 | ContentProviderRecord cpr; |
| 5387 | ProviderInfo cpi = null; |
| 5388 | |
| 5389 | synchronized(this) { |
| 5390 | ProcessRecord r = null; |
| 5391 | if (caller != null) { |
| 5392 | r = getRecordForAppLocked(caller); |
| 5393 | if (r == null) { |
| 5394 | throw new SecurityException( |
| 5395 | "Unable to find app for caller " + caller |
| 5396 | + " (pid=" + Binder.getCallingPid() |
| 5397 | + ") when getting content provider " + name); |
| 5398 | } |
| 5399 | } |
| 5400 | |
| 5401 | // First check if this content provider has been published... |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5402 | cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5403 | if (cpr != null) { |
| 5404 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5405 | String msg; |
| 5406 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 5407 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | if (r != null && cpr.canRunHere(r)) { |
| 5411 | // This provider has been published or is in the process |
| 5412 | // of being published... but it is also allowed to run |
| 5413 | // in the caller's process, so don't make a connection |
| 5414 | // and just let the caller instantiate its own instance. |
| 5415 | if (cpr.provider != null) { |
| 5416 | // don't give caller the provider object, it needs |
| 5417 | // to make its own. |
| 5418 | cpr = new ContentProviderRecord(cpr); |
| 5419 | } |
| 5420 | return cpr; |
| 5421 | } |
| 5422 | |
| 5423 | final long origId = Binder.clearCallingIdentity(); |
| 5424 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5425 | // 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] | 5426 | // return it right away. |
| 5427 | if (r != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5428 | if (DEBUG_PROVIDER) Slog.v(TAG, |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5429 | "Adding provider requested by " |
| 5430 | + r.processName + " from process " |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 5431 | + cpr.info.processName); |
| 5432 | Integer cnt = r.conProviders.get(cpr); |
| 5433 | if (cnt == null) { |
| 5434 | r.conProviders.put(cpr, new Integer(1)); |
| 5435 | } else { |
| 5436 | r.conProviders.put(cpr, new Integer(cnt.intValue()+1)); |
| 5437 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5438 | cpr.clients.add(r); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 5439 | if (cpr.app != null && r.setAdj <= PERCEPTIBLE_APP_ADJ) { |
| 5440 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 5441 | // make sure to count it as being accessed and thus |
| 5442 | // back up on the LRU list. This is good because |
| 5443 | // content providers are often expensive to start. |
| 5444 | updateLruProcessLocked(cpr.app, false, true); |
| 5445 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 5446 | } else { |
| 5447 | cpr.externals++; |
| 5448 | } |
| 5449 | |
| 5450 | if (cpr.app != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5451 | updateOomAdjLocked(cpr.app); |
| 5452 | } |
| 5453 | |
| 5454 | Binder.restoreCallingIdentity(origId); |
| 5455 | |
| 5456 | } else { |
| 5457 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5458 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5459 | resolveContentProvider(name, |
| 5460 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5461 | } catch (RemoteException ex) { |
| 5462 | } |
| 5463 | if (cpi == null) { |
| 5464 | return null; |
| 5465 | } |
| 5466 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5467 | String msg; |
| 5468 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 5469 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5470 | } |
| 5471 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 5472 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 5473 | && !cpi.processName.equals("system")) { |
| 5474 | // If this content provider does not run in the system |
| 5475 | // process, and the system is not yet ready to run other |
| 5476 | // processes, then fail fast instead of hanging. |
| 5477 | throw new IllegalArgumentException( |
| 5478 | "Attempt to launch content provider before system ready"); |
| 5479 | } |
| 5480 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5481 | cpr = mProvidersByClass.get(cpi.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5482 | final boolean firstClass = cpr == null; |
| 5483 | if (firstClass) { |
| 5484 | try { |
| 5485 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5486 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5487 | getApplicationInfo( |
| 5488 | cpi.applicationInfo.packageName, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5489 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5490 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5491 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5492 | + cpi.name); |
| 5493 | return null; |
| 5494 | } |
| 5495 | cpr = new ContentProviderRecord(cpi, ai); |
| 5496 | } catch (RemoteException ex) { |
| 5497 | // pm is in same process, this will never happen. |
| 5498 | } |
| 5499 | } |
| 5500 | |
| 5501 | if (r != null && cpr.canRunHere(r)) { |
| 5502 | // If this is a multiprocess provider, then just return its |
| 5503 | // info and allow the caller to instantiate it. Only do |
| 5504 | // this if the provider is the same user as the caller's |
| 5505 | // process, or can run as root (so can be in any process). |
| 5506 | return cpr; |
| 5507 | } |
| 5508 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5509 | if (DEBUG_PROVIDER) { |
| 5510 | RuntimeException e = new RuntimeException("here"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5511 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.info.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5512 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5513 | } |
| 5514 | |
| 5515 | // This is single process, and our app is now connecting to it. |
| 5516 | // See if we are already in the process of launching this |
| 5517 | // provider. |
| 5518 | final int N = mLaunchingProviders.size(); |
| 5519 | int i; |
| 5520 | for (i=0; i<N; i++) { |
| 5521 | if (mLaunchingProviders.get(i) == cpr) { |
| 5522 | break; |
| 5523 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5524 | } |
| 5525 | |
| 5526 | // If the provider is not already being launched, then get it |
| 5527 | // started. |
| 5528 | if (i >= N) { |
| 5529 | final long origId = Binder.clearCallingIdentity(); |
| 5530 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 5531 | cpr.appInfo, false, 0, "content provider", |
| 5532 | new ComponentName(cpi.applicationInfo.packageName, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 5533 | cpi.name), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5534 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5535 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5536 | + cpi.applicationInfo.packageName + "/" |
| 5537 | + cpi.applicationInfo.uid + " for provider " |
| 5538 | + name + ": process is bad"); |
| 5539 | return null; |
| 5540 | } |
| 5541 | cpr.launchingApp = proc; |
| 5542 | mLaunchingProviders.add(cpr); |
| 5543 | Binder.restoreCallingIdentity(origId); |
| 5544 | } |
| 5545 | |
| 5546 | // Make sure the provider is published (the same provider class |
| 5547 | // may be published under multiple names). |
| 5548 | if (firstClass) { |
| 5549 | mProvidersByClass.put(cpi.name, cpr); |
| 5550 | } |
| 5551 | mProvidersByName.put(name, cpr); |
| 5552 | |
| 5553 | if (r != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5554 | if (DEBUG_PROVIDER) Slog.v(TAG, |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5555 | "Adding provider requested by " |
| 5556 | + r.processName + " from process " |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 5557 | + cpr.info.processName); |
| 5558 | Integer cnt = r.conProviders.get(cpr); |
| 5559 | if (cnt == null) { |
| 5560 | r.conProviders.put(cpr, new Integer(1)); |
| 5561 | } else { |
| 5562 | r.conProviders.put(cpr, new Integer(cnt.intValue()+1)); |
| 5563 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5564 | cpr.clients.add(r); |
| 5565 | } else { |
| 5566 | cpr.externals++; |
| 5567 | } |
| 5568 | } |
| 5569 | } |
| 5570 | |
| 5571 | // Wait for the provider to be published... |
| 5572 | synchronized (cpr) { |
| 5573 | while (cpr.provider == null) { |
| 5574 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5575 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5576 | + cpi.applicationInfo.packageName + "/" |
| 5577 | + cpi.applicationInfo.uid + " for provider " |
| 5578 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 5579 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5580 | cpi.applicationInfo.packageName, |
| 5581 | cpi.applicationInfo.uid, name); |
| 5582 | return null; |
| 5583 | } |
| 5584 | try { |
| 5585 | cpr.wait(); |
| 5586 | } catch (InterruptedException ex) { |
| 5587 | } |
| 5588 | } |
| 5589 | } |
| 5590 | return cpr; |
| 5591 | } |
| 5592 | |
| 5593 | public final ContentProviderHolder getContentProvider( |
| 5594 | IApplicationThread caller, String name) { |
| 5595 | if (caller == null) { |
| 5596 | String msg = "null IApplicationThread when getting content provider " |
| 5597 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5598 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5599 | throw new SecurityException(msg); |
| 5600 | } |
| 5601 | |
| 5602 | return getContentProviderImpl(caller, name); |
| 5603 | } |
| 5604 | |
| 5605 | private ContentProviderHolder getContentProviderExternal(String name) { |
| 5606 | return getContentProviderImpl(null, name); |
| 5607 | } |
| 5608 | |
| 5609 | /** |
| 5610 | * Drop a content provider from a ProcessRecord's bookkeeping |
| 5611 | * @param cpr |
| 5612 | */ |
| 5613 | public void removeContentProvider(IApplicationThread caller, String name) { |
| 5614 | synchronized (this) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5615 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5616 | if(cpr == null) { |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5617 | // remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5618 | if (DEBUG_PROVIDER) Slog.v(TAG, name + |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5619 | " provider not found in providers list"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5620 | return; |
| 5621 | } |
| 5622 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5623 | if (r == null) { |
| 5624 | throw new SecurityException( |
| 5625 | "Unable to find app for caller " + caller + |
| 5626 | " when removing content provider " + name); |
| 5627 | } |
| 5628 | //update content provider record entry info |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5629 | ContentProviderRecord localCpr = mProvidersByClass.get(cpr.info.name); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5630 | if (DEBUG_PROVIDER) Slog.v(TAG, "Removing provider requested by " |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5631 | + r.info.processName + " from process " |
| 5632 | + localCpr.appInfo.processName); |
| 5633 | if (localCpr.app == r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5634 | //should not happen. taken care of as a local provider |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5635 | Slog.w(TAG, "removeContentProvider called on local provider: " |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5636 | + cpr.info.name + " in process " + r.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5637 | return; |
| 5638 | } else { |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 5639 | Integer cnt = r.conProviders.get(localCpr); |
| 5640 | if (cnt == null || cnt.intValue() <= 1) { |
| 5641 | localCpr.clients.remove(r); |
| 5642 | r.conProviders.remove(localCpr); |
| 5643 | } else { |
| 5644 | r.conProviders.put(localCpr, new Integer(cnt.intValue()-1)); |
| 5645 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5646 | } |
| 5647 | updateOomAdjLocked(); |
| 5648 | } |
| 5649 | } |
| 5650 | |
| 5651 | private void removeContentProviderExternal(String name) { |
| 5652 | synchronized (this) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5653 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5654 | if(cpr == null) { |
| 5655 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5656 | 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] | 5657 | return; |
| 5658 | } |
| 5659 | |
| 5660 | //update content provider record entry info |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5661 | ContentProviderRecord localCpr = mProvidersByClass.get(cpr.info.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5662 | localCpr.externals--; |
| 5663 | if (localCpr.externals < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5664 | Slog.e(TAG, "Externals < 0 for content provider " + localCpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5665 | } |
| 5666 | updateOomAdjLocked(); |
| 5667 | } |
| 5668 | } |
| 5669 | |
| 5670 | public final void publishContentProviders(IApplicationThread caller, |
| 5671 | List<ContentProviderHolder> providers) { |
| 5672 | if (providers == null) { |
| 5673 | return; |
| 5674 | } |
| 5675 | |
| 5676 | synchronized(this) { |
| 5677 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5678 | if (r == null) { |
| 5679 | throw new SecurityException( |
| 5680 | "Unable to find app for caller " + caller |
| 5681 | + " (pid=" + Binder.getCallingPid() |
| 5682 | + ") when publishing content providers"); |
| 5683 | } |
| 5684 | |
| 5685 | final long origId = Binder.clearCallingIdentity(); |
| 5686 | |
| 5687 | final int N = providers.size(); |
| 5688 | for (int i=0; i<N; i++) { |
| 5689 | ContentProviderHolder src = providers.get(i); |
| 5690 | if (src == null || src.info == null || src.provider == null) { |
| 5691 | continue; |
| 5692 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5693 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5694 | if (dst != null) { |
| 5695 | mProvidersByClass.put(dst.info.name, dst); |
| 5696 | String names[] = dst.info.authority.split(";"); |
| 5697 | for (int j = 0; j < names.length; j++) { |
| 5698 | mProvidersByName.put(names[j], dst); |
| 5699 | } |
| 5700 | |
| 5701 | int NL = mLaunchingProviders.size(); |
| 5702 | int j; |
| 5703 | for (j=0; j<NL; j++) { |
| 5704 | if (mLaunchingProviders.get(j) == dst) { |
| 5705 | mLaunchingProviders.remove(j); |
| 5706 | j--; |
| 5707 | NL--; |
| 5708 | } |
| 5709 | } |
| 5710 | synchronized (dst) { |
| 5711 | dst.provider = src.provider; |
| 5712 | dst.app = r; |
| 5713 | dst.notifyAll(); |
| 5714 | } |
| 5715 | updateOomAdjLocked(r); |
| 5716 | } |
| 5717 | } |
| 5718 | |
| 5719 | Binder.restoreCallingIdentity(origId); |
| 5720 | } |
| 5721 | } |
| 5722 | |
| 5723 | public static final void installSystemProviders() { |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 5724 | List providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 5725 | synchronized (mSelf) { |
| 5726 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 5727 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 5728 | if (providers != null) { |
| 5729 | for (int i=providers.size()-1; i>=0; i--) { |
| 5730 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 5731 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 5732 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 5733 | + ": not system .apk"); |
| 5734 | providers.remove(i); |
| 5735 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 5736 | } |
| 5737 | } |
| 5738 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 5739 | if (providers != null) { |
| 5740 | mSystemThread.installSystemProviders(providers); |
| 5741 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5742 | } |
| 5743 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 5744 | /** |
| 5745 | * Allows app to retrieve the MIME type of a URI without having permission |
| 5746 | * to access its content provider. |
| 5747 | * |
| 5748 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 5749 | * |
| 5750 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 5751 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 5752 | */ |
| 5753 | public String getProviderMimeType(Uri uri) { |
| 5754 | final String name = uri.getAuthority(); |
| 5755 | final long ident = Binder.clearCallingIdentity(); |
| 5756 | ContentProviderHolder holder = null; |
| 5757 | |
| 5758 | try { |
| 5759 | holder = getContentProviderExternal(name); |
| 5760 | if (holder != null) { |
| 5761 | return holder.provider.getType(uri); |
| 5762 | } |
| 5763 | } catch (RemoteException e) { |
| 5764 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 5765 | return null; |
| 5766 | } finally { |
| 5767 | if (holder != null) { |
| 5768 | removeContentProviderExternal(name); |
| 5769 | } |
| 5770 | Binder.restoreCallingIdentity(ident); |
| 5771 | } |
| 5772 | |
| 5773 | return null; |
| 5774 | } |
| 5775 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5776 | // ========================================================= |
| 5777 | // GLOBAL MANAGEMENT |
| 5778 | // ========================================================= |
| 5779 | |
| 5780 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
| 5781 | ApplicationInfo info, String customProcess) { |
| 5782 | String proc = customProcess != null ? customProcess : info.processName; |
| 5783 | BatteryStatsImpl.Uid.Proc ps = null; |
| 5784 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 5785 | synchronized (stats) { |
| 5786 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 5787 | } |
| 5788 | return new ProcessRecord(ps, thread, info, proc); |
| 5789 | } |
| 5790 | |
| 5791 | final ProcessRecord addAppLocked(ApplicationInfo info) { |
| 5792 | ProcessRecord app = getProcessRecordLocked(info.processName, info.uid); |
| 5793 | |
| 5794 | if (app == null) { |
| 5795 | app = newProcessRecordLocked(null, info, null); |
| 5796 | mProcessNames.put(info.processName, info.uid, app); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 5797 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5798 | } |
| 5799 | |
| 5800 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 5801 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 5802 | app.persistent = true; |
| 5803 | app.maxAdj = CORE_SERVER_ADJ; |
| 5804 | } |
| 5805 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 5806 | mPersistentStartingProcesses.add(app); |
| 5807 | startProcessLocked(app, "added application", app.processName); |
| 5808 | } |
| 5809 | |
| 5810 | return app; |
| 5811 | } |
| 5812 | |
| 5813 | public void unhandledBack() { |
| 5814 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 5815 | "unhandledBack()"); |
| 5816 | |
| 5817 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5818 | int count = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5819 | if (DEBUG_SWITCH) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5820 | TAG, "Performing unhandledBack(): stack size = " + count); |
| 5821 | if (count > 1) { |
| 5822 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5823 | mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5824 | count-1, Activity.RESULT_CANCELED, null, "unhandled-back"); |
| 5825 | Binder.restoreCallingIdentity(origId); |
| 5826 | } |
| 5827 | } |
| 5828 | } |
| 5829 | |
| 5830 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
| 5831 | String name = uri.getAuthority(); |
| 5832 | ContentProviderHolder cph = getContentProviderExternal(name); |
| 5833 | ParcelFileDescriptor pfd = null; |
| 5834 | if (cph != null) { |
| 5835 | // We record the binder invoker's uid in thread-local storage before |
| 5836 | // going to the content provider to open the file. Later, in the code |
| 5837 | // that handles all permissions checks, we look for this uid and use |
| 5838 | // that rather than the Activity Manager's own uid. The effect is that |
| 5839 | // we do the check against the caller's permissions even though it looks |
| 5840 | // to the content provider like the Activity Manager itself is making |
| 5841 | // the request. |
| 5842 | sCallerIdentity.set(new Identity( |
| 5843 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 5844 | try { |
| 5845 | pfd = cph.provider.openFile(uri, "r"); |
| 5846 | } catch (FileNotFoundException e) { |
| 5847 | // do nothing; pfd will be returned null |
| 5848 | } finally { |
| 5849 | // Ensure that whatever happens, we clean up the identity state |
| 5850 | sCallerIdentity.remove(); |
| 5851 | } |
| 5852 | |
| 5853 | // We've got the fd now, so we're done with the provider. |
| 5854 | removeContentProviderExternal(name); |
| 5855 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5856 | 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] | 5857 | } |
| 5858 | return pfd; |
| 5859 | } |
| 5860 | |
| 5861 | public void goingToSleep() { |
| 5862 | synchronized(this) { |
| 5863 | mSleeping = true; |
| 5864 | mWindowManager.setEventDispatching(false); |
| 5865 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5866 | if (mMainStack.mResumedActivity != null) { |
| 5867 | mMainStack.pauseIfSleepingLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5868 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5869 | Slog.w(TAG, "goingToSleep with no resumed activity!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5870 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 5871 | |
| 5872 | // Initialize the wake times of all processes. |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 5873 | checkExcessivePowerUsageLocked(false); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 5874 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 5875 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 5876 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5877 | } |
| 5878 | } |
| 5879 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 5880 | public boolean shutdown(int timeout) { |
| 5881 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 5882 | != PackageManager.PERMISSION_GRANTED) { |
| 5883 | throw new SecurityException("Requires permission " |
| 5884 | + android.Manifest.permission.SHUTDOWN); |
| 5885 | } |
| 5886 | |
| 5887 | boolean timedout = false; |
| 5888 | |
| 5889 | synchronized(this) { |
| 5890 | mShuttingDown = true; |
| 5891 | mWindowManager.setEventDispatching(false); |
| 5892 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5893 | if (mMainStack.mResumedActivity != null) { |
| 5894 | mMainStack.pauseIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 5895 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5896 | while (mMainStack.mResumedActivity != null |
| 5897 | || mMainStack.mPausingActivity != null) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 5898 | long delay = endTime - System.currentTimeMillis(); |
| 5899 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5900 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 5901 | timedout = true; |
| 5902 | break; |
| 5903 | } |
| 5904 | try { |
| 5905 | this.wait(); |
| 5906 | } catch (InterruptedException e) { |
| 5907 | } |
| 5908 | } |
| 5909 | } |
| 5910 | } |
| 5911 | |
| 5912 | mUsageStatsService.shutdown(); |
| 5913 | mBatteryStatsService.shutdown(); |
| 5914 | |
| 5915 | return timedout; |
| 5916 | } |
| 5917 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5918 | public void wakingUp() { |
| 5919 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5920 | if (mMainStack.mGoingToSleep.isHeld()) { |
| 5921 | mMainStack.mGoingToSleep.release(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5922 | } |
| 5923 | mWindowManager.setEventDispatching(true); |
| 5924 | mSleeping = false; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5925 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5926 | } |
| 5927 | } |
| 5928 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5929 | public void stopAppSwitches() { |
| 5930 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 5931 | != PackageManager.PERMISSION_GRANTED) { |
| 5932 | throw new SecurityException("Requires permission " |
| 5933 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 5934 | } |
| 5935 | |
| 5936 | synchronized(this) { |
| 5937 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 5938 | + APP_SWITCH_DELAY_TIME; |
| 5939 | mDidAppSwitch = false; |
| 5940 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 5941 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 5942 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 5943 | } |
| 5944 | } |
| 5945 | |
| 5946 | public void resumeAppSwitches() { |
| 5947 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 5948 | != PackageManager.PERMISSION_GRANTED) { |
| 5949 | throw new SecurityException("Requires permission " |
| 5950 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 5951 | } |
| 5952 | |
| 5953 | synchronized(this) { |
| 5954 | // Note that we don't execute any pending app switches... we will |
| 5955 | // let those wait until either the timeout, or the next start |
| 5956 | // activity request. |
| 5957 | mAppSwitchesAllowedTime = 0; |
| 5958 | } |
| 5959 | } |
| 5960 | |
| 5961 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 5962 | String name) { |
| 5963 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 5964 | return true; |
| 5965 | } |
| 5966 | |
| 5967 | final int perm = checkComponentPermission( |
| 5968 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5969 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5970 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 5971 | return true; |
| 5972 | } |
| 5973 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5974 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5975 | return false; |
| 5976 | } |
| 5977 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5978 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 5979 | boolean persistent) { |
| 5980 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 5981 | "setDebugApp()"); |
| 5982 | |
| 5983 | // Note that this is not really thread safe if there are multiple |
| 5984 | // callers into it at the same time, but that's not a situation we |
| 5985 | // care about. |
| 5986 | if (persistent) { |
| 5987 | final ContentResolver resolver = mContext.getContentResolver(); |
| 5988 | Settings.System.putString( |
| 5989 | resolver, Settings.System.DEBUG_APP, |
| 5990 | packageName); |
| 5991 | Settings.System.putInt( |
| 5992 | resolver, Settings.System.WAIT_FOR_DEBUGGER, |
| 5993 | waitForDebugger ? 1 : 0); |
| 5994 | } |
| 5995 | |
| 5996 | synchronized (this) { |
| 5997 | if (!persistent) { |
| 5998 | mOrigDebugApp = mDebugApp; |
| 5999 | mOrigWaitForDebugger = mWaitForDebugger; |
| 6000 | } |
| 6001 | mDebugApp = packageName; |
| 6002 | mWaitForDebugger = waitForDebugger; |
| 6003 | mDebugTransient = !persistent; |
| 6004 | if (packageName != null) { |
| 6005 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 6006 | forceStopPackageLocked(packageName, -1, false, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6007 | Binder.restoreCallingIdentity(origId); |
| 6008 | } |
| 6009 | } |
| 6010 | } |
| 6011 | |
| 6012 | public void setAlwaysFinish(boolean enabled) { |
| 6013 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 6014 | "setAlwaysFinish()"); |
| 6015 | |
| 6016 | Settings.System.putInt( |
| 6017 | mContext.getContentResolver(), |
| 6018 | Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
| 6019 | |
| 6020 | synchronized (this) { |
| 6021 | mAlwaysFinishActivities = enabled; |
| 6022 | } |
| 6023 | } |
| 6024 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6025 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6026 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6027 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6028 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6029 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6030 | } |
| 6031 | } |
| 6032 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 6033 | public boolean isUserAMonkey() { |
| 6034 | // For now the fact that there is a controller implies |
| 6035 | // we have a monkey. |
| 6036 | synchronized (this) { |
| 6037 | return mController != null; |
| 6038 | } |
| 6039 | } |
| 6040 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6041 | public void registerActivityWatcher(IActivityWatcher watcher) { |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6042 | synchronized (this) { |
| 6043 | mWatchers.register(watcher); |
| 6044 | } |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6045 | } |
| 6046 | |
| 6047 | public void unregisterActivityWatcher(IActivityWatcher watcher) { |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6048 | synchronized (this) { |
| 6049 | mWatchers.unregister(watcher); |
| 6050 | } |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6051 | } |
| 6052 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6053 | public void setImmersive(IBinder token, boolean immersive) { |
| 6054 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6055 | int index = (token != null) ? mMainStack.indexOfTokenLocked(token) : -1; |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6056 | if (index < 0) { |
| 6057 | throw new IllegalArgumentException(); |
| 6058 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6059 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6060 | r.immersive = immersive; |
| 6061 | } |
| 6062 | } |
| 6063 | |
| 6064 | public boolean isImmersive(IBinder token) { |
| 6065 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6066 | int index = (token != null) ? mMainStack.indexOfTokenLocked(token) : -1; |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6067 | if (index < 0) { |
| 6068 | throw new IllegalArgumentException(); |
| 6069 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6070 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6071 | return r.immersive; |
| 6072 | } |
| 6073 | } |
| 6074 | |
| 6075 | public boolean isTopActivityImmersive() { |
| 6076 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6077 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6078 | return (r != null) ? r.immersive : false; |
| 6079 | } |
| 6080 | } |
| 6081 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6082 | public final void enterSafeMode() { |
| 6083 | synchronized(this) { |
| 6084 | // It only makes sense to do this before the system is ready |
| 6085 | // and started launching other packages. |
| 6086 | if (!mSystemReady) { |
| 6087 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6088 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6089 | } catch (RemoteException e) { |
| 6090 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6091 | } |
| 6092 | } |
| 6093 | } |
| 6094 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 6095 | public final void showSafeModeOverlay() { |
| 6096 | View v = LayoutInflater.from(mContext).inflate( |
| 6097 | com.android.internal.R.layout.safe_mode, null); |
| 6098 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 6099 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 6100 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6101 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6102 | lp.gravity = Gravity.BOTTOM | Gravity.LEFT; |
| 6103 | lp.format = v.getBackground().getOpacity(); |
| 6104 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 6105 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 6106 | ((WindowManager)mContext.getSystemService( |
| 6107 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 6108 | } |
| 6109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6110 | public void noteWakeupAlarm(IIntentSender sender) { |
| 6111 | if (!(sender instanceof PendingIntentRecord)) { |
| 6112 | return; |
| 6113 | } |
| 6114 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 6115 | synchronized (stats) { |
| 6116 | if (mBatteryStatsService.isOnBattery()) { |
| 6117 | mBatteryStatsService.enforceCallingPermission(); |
| 6118 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 6119 | int MY_UID = Binder.getCallingUid(); |
| 6120 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 6121 | BatteryStatsImpl.Uid.Pkg pkg = |
| 6122 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 6123 | pkg.incWakeupsLocked(); |
| 6124 | } |
| 6125 | } |
| 6126 | } |
| 6127 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6128 | public boolean killPids(int[] pids, String pReason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6129 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6130 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6131 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6132 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6133 | // XXX Note: don't acquire main activity lock here, because the window |
| 6134 | // manager calls in with its locks held. |
| 6135 | |
| 6136 | boolean killed = false; |
| 6137 | synchronized (mPidsSelfLocked) { |
| 6138 | int[] types = new int[pids.length]; |
| 6139 | int worstType = 0; |
| 6140 | for (int i=0; i<pids.length; i++) { |
| 6141 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 6142 | if (proc != null) { |
| 6143 | int type = proc.setAdj; |
| 6144 | types[i] = type; |
| 6145 | if (type > worstType) { |
| 6146 | worstType = type; |
| 6147 | } |
| 6148 | } |
| 6149 | } |
| 6150 | |
| 6151 | // If the worse oom_adj is somewhere in the hidden proc LRU range, |
| 6152 | // then constrain it so we will kill all hidden procs. |
| 6153 | if (worstType < EMPTY_APP_ADJ && worstType > HIDDEN_APP_MIN_ADJ) { |
| 6154 | worstType = HIDDEN_APP_MIN_ADJ; |
| 6155 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6156 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6157 | for (int i=0; i<pids.length; i++) { |
| 6158 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 6159 | if (proc == null) { |
| 6160 | continue; |
| 6161 | } |
| 6162 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6163 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 6164 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6165 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid, |
| 6166 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6167 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6168 | proc.killedBackground = true; |
| 6169 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6170 | } |
| 6171 | } |
| 6172 | } |
| 6173 | return killed; |
| 6174 | } |
| 6175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6176 | public final void startRunning(String pkg, String cls, String action, |
| 6177 | String data) { |
| 6178 | synchronized(this) { |
| 6179 | if (mStartRunning) { |
| 6180 | return; |
| 6181 | } |
| 6182 | mStartRunning = true; |
| 6183 | mTopComponent = pkg != null && cls != null |
| 6184 | ? new ComponentName(pkg, cls) : null; |
| 6185 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 6186 | mTopData = data; |
| 6187 | if (!mSystemReady) { |
| 6188 | return; |
| 6189 | } |
| 6190 | } |
| 6191 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6192 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6193 | } |
| 6194 | |
| 6195 | private void retrieveSettings() { |
| 6196 | final ContentResolver resolver = mContext.getContentResolver(); |
| 6197 | String debugApp = Settings.System.getString( |
| 6198 | resolver, Settings.System.DEBUG_APP); |
| 6199 | boolean waitForDebugger = Settings.System.getInt( |
| 6200 | resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0; |
| 6201 | boolean alwaysFinishActivities = Settings.System.getInt( |
| 6202 | resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
| 6203 | |
| 6204 | Configuration configuration = new Configuration(); |
| 6205 | Settings.System.getConfiguration(resolver, configuration); |
| 6206 | |
| 6207 | synchronized (this) { |
| 6208 | mDebugApp = mOrigDebugApp = debugApp; |
| 6209 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 6210 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 6211 | // This happens before any activities are started, so we can |
| 6212 | // change mConfiguration in-place. |
| 6213 | mConfiguration.updateFrom(configuration); |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 6214 | mConfigurationSeq = mConfiguration.seq = 1; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6215 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6216 | } |
| 6217 | } |
| 6218 | |
| 6219 | public boolean testIsSystemReady() { |
| 6220 | // no need to synchronize(this) just to read & return the value |
| 6221 | return mSystemReady; |
| 6222 | } |
| 6223 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6224 | private static File getCalledPreBootReceiversFile() { |
| 6225 | File dataDir = Environment.getDataDirectory(); |
| 6226 | File systemDir = new File(dataDir, "system"); |
| 6227 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 6228 | return fname; |
| 6229 | } |
| 6230 | |
| 6231 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 6232 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 6233 | File file = getCalledPreBootReceiversFile(); |
| 6234 | FileInputStream fis = null; |
| 6235 | try { |
| 6236 | fis = new FileInputStream(file); |
| 6237 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
| 6238 | int vers = dis.readInt(); |
| 6239 | String codename = dis.readUTF(); |
| 6240 | if (vers == android.os.Build.VERSION.SDK_INT |
| 6241 | && codename.equals(android.os.Build.VERSION.CODENAME)) { |
| 6242 | int num = dis.readInt(); |
| 6243 | while (num > 0) { |
| 6244 | num--; |
| 6245 | String pkg = dis.readUTF(); |
| 6246 | String cls = dis.readUTF(); |
| 6247 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 6248 | } |
| 6249 | } |
| 6250 | } catch (FileNotFoundException e) { |
| 6251 | } catch (IOException e) { |
| 6252 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 6253 | } finally { |
| 6254 | if (fis != null) { |
| 6255 | try { |
| 6256 | fis.close(); |
| 6257 | } catch (IOException e) { |
| 6258 | } |
| 6259 | } |
| 6260 | } |
| 6261 | return lastDoneReceivers; |
| 6262 | } |
| 6263 | |
| 6264 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 6265 | File file = getCalledPreBootReceiversFile(); |
| 6266 | FileOutputStream fos = null; |
| 6267 | DataOutputStream dos = null; |
| 6268 | try { |
| 6269 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 6270 | fos = new FileOutputStream(file); |
| 6271 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
| 6272 | dos.writeInt(android.os.Build.VERSION.SDK_INT); |
| 6273 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
| 6274 | dos.writeInt(list.size()); |
| 6275 | for (int i=0; i<list.size(); i++) { |
| 6276 | dos.writeUTF(list.get(i).getPackageName()); |
| 6277 | dos.writeUTF(list.get(i).getClassName()); |
| 6278 | } |
| 6279 | } catch (IOException e) { |
| 6280 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 6281 | file.delete(); |
| 6282 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 6283 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6284 | if (dos != null) { |
| 6285 | try { |
| 6286 | dos.close(); |
| 6287 | } catch (IOException e) { |
| 6288 | // TODO Auto-generated catch block |
| 6289 | e.printStackTrace(); |
| 6290 | } |
| 6291 | } |
| 6292 | } |
| 6293 | } |
| 6294 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6295 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6296 | // In the simulator, startRunning will never have been called, which |
| 6297 | // normally sets a few crucial variables. Do it here instead. |
| 6298 | if (!Process.supportsProcesses()) { |
| 6299 | mStartRunning = true; |
| 6300 | mTopAction = Intent.ACTION_MAIN; |
| 6301 | } |
| 6302 | |
| 6303 | synchronized(this) { |
| 6304 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6305 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6306 | return; |
| 6307 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6308 | |
| 6309 | // Check to see if there are any update receivers to run. |
| 6310 | if (!mDidUpdate) { |
| 6311 | if (mWaitingUpdate) { |
| 6312 | return; |
| 6313 | } |
| 6314 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 6315 | List<ResolveInfo> ris = null; |
| 6316 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6317 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6318 | intent, null, 0); |
| 6319 | } catch (RemoteException e) { |
| 6320 | } |
| 6321 | if (ris != null) { |
| 6322 | for (int i=ris.size()-1; i>=0; i--) { |
| 6323 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 6324 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6325 | ris.remove(i); |
| 6326 | } |
| 6327 | } |
| 6328 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6329 | |
| 6330 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
| 6331 | |
| 6332 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6333 | for (int i=0; i<ris.size(); i++) { |
| 6334 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6335 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 6336 | if (lastDoneReceivers.contains(comp)) { |
| 6337 | ris.remove(i); |
| 6338 | i--; |
| 6339 | } |
| 6340 | } |
| 6341 | |
| 6342 | for (int i=0; i<ris.size(); i++) { |
| 6343 | ActivityInfo ai = ris.get(i).activityInfo; |
| 6344 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 6345 | doneReceivers.add(comp); |
| 6346 | intent.setComponent(comp); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6347 | IIntentReceiver finisher = null; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 6348 | if (i == ris.size()-1) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6349 | finisher = new IIntentReceiver.Stub() { |
| 6350 | public void performReceive(Intent intent, int resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 6351 | String data, Bundle extras, boolean ordered, |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 6352 | boolean sticky) { |
| 6353 | // The raw IIntentReceiver interface is called |
| 6354 | // with the AM lock held, so redispatch to |
| 6355 | // execute our code without the lock. |
| 6356 | mHandler.post(new Runnable() { |
| 6357 | public void run() { |
| 6358 | synchronized (ActivityManagerService.this) { |
| 6359 | mDidUpdate = true; |
| 6360 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6361 | writeLastDonePreBootReceivers(doneReceivers); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 6362 | systemReady(goingCallback); |
| 6363 | } |
| 6364 | }); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6365 | } |
| 6366 | }; |
| 6367 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6368 | Slog.i(TAG, "Sending system update to: " + intent.getComponent()); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6369 | broadcastIntentLocked(null, null, intent, null, finisher, |
| 6370 | 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 6371 | if (finisher != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6372 | mWaitingUpdate = true; |
| 6373 | } |
| 6374 | } |
| 6375 | } |
| 6376 | if (mWaitingUpdate) { |
| 6377 | return; |
| 6378 | } |
| 6379 | mDidUpdate = true; |
| 6380 | } |
| 6381 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6382 | mSystemReady = true; |
| 6383 | if (!mStartRunning) { |
| 6384 | return; |
| 6385 | } |
| 6386 | } |
| 6387 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6388 | ArrayList<ProcessRecord> procsToKill = null; |
| 6389 | synchronized(mPidsSelfLocked) { |
| 6390 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 6391 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 6392 | if (!isAllowedWhileBooting(proc.info)){ |
| 6393 | if (procsToKill == null) { |
| 6394 | procsToKill = new ArrayList<ProcessRecord>(); |
| 6395 | } |
| 6396 | procsToKill.add(proc); |
| 6397 | } |
| 6398 | } |
| 6399 | } |
| 6400 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6401 | synchronized(this) { |
| 6402 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6403 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 6404 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6405 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6406 | removeProcessLocked(proc, true); |
| 6407 | } |
| 6408 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6409 | |
| 6410 | // Now that we have cleaned up any update processes, we |
| 6411 | // are ready to start launching real processes and know that |
| 6412 | // we won't trample on them any more. |
| 6413 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6414 | } |
| 6415 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6416 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6417 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6418 | SystemClock.uptimeMillis()); |
| 6419 | |
| 6420 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6421 | // Make sure we have no pre-ready processes sitting around. |
| 6422 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6423 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 6424 | ResolveInfo ri = mContext.getPackageManager() |
| 6425 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6426 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6427 | CharSequence errorMsg = null; |
| 6428 | if (ri != null) { |
| 6429 | ActivityInfo ai = ri.activityInfo; |
| 6430 | ApplicationInfo app = ai.applicationInfo; |
| 6431 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 6432 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 6433 | mTopData = null; |
| 6434 | mTopComponent = new ComponentName(app.packageName, |
| 6435 | ai.name); |
| 6436 | } else { |
| 6437 | errorMsg = mContext.getResources().getText( |
| 6438 | com.android.internal.R.string.factorytest_not_system); |
| 6439 | } |
| 6440 | } else { |
| 6441 | errorMsg = mContext.getResources().getText( |
| 6442 | com.android.internal.R.string.factorytest_no_action); |
| 6443 | } |
| 6444 | if (errorMsg != null) { |
| 6445 | mTopAction = null; |
| 6446 | mTopData = null; |
| 6447 | mTopComponent = null; |
| 6448 | Message msg = Message.obtain(); |
| 6449 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 6450 | msg.getData().putCharSequence("msg", errorMsg); |
| 6451 | mHandler.sendMessage(msg); |
| 6452 | } |
| 6453 | } |
| 6454 | } |
| 6455 | |
| 6456 | retrieveSettings(); |
| 6457 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6458 | if (goingCallback != null) goingCallback.run(); |
| 6459 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6460 | synchronized (this) { |
| 6461 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 6462 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6463 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6464 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6465 | if (apps != null) { |
| 6466 | int N = apps.size(); |
| 6467 | int i; |
| 6468 | for (i=0; i<N; i++) { |
| 6469 | ApplicationInfo info |
| 6470 | = (ApplicationInfo)apps.get(i); |
| 6471 | if (info != null && |
| 6472 | !info.packageName.equals("android")) { |
| 6473 | addAppLocked(info); |
| 6474 | } |
| 6475 | } |
| 6476 | } |
| 6477 | } catch (RemoteException ex) { |
| 6478 | // pm is in same process, this will never happen. |
| 6479 | } |
| 6480 | } |
| 6481 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6482 | // Start up initial activity. |
| 6483 | mBooting = true; |
| 6484 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6485 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6486 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6487 | Message msg = Message.obtain(); |
| 6488 | msg.what = SHOW_UID_ERROR_MSG; |
| 6489 | mHandler.sendMessage(msg); |
| 6490 | } |
| 6491 | } catch (RemoteException e) { |
| 6492 | } |
| 6493 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6494 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6495 | } |
| 6496 | } |
| 6497 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6498 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6499 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6500 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6501 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6502 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6503 | startAppProblemLocked(app); |
| 6504 | app.stopFreezingAllLocked(); |
| 6505 | return handleAppCrashLocked(app); |
| 6506 | } |
| 6507 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6508 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6509 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6510 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6511 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6512 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 6513 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6514 | startAppProblemLocked(app); |
| 6515 | app.stopFreezingAllLocked(); |
| 6516 | } |
| 6517 | |
| 6518 | /** |
| 6519 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 6520 | * |
| 6521 | * @param app The ProcessRecord in which the error occurred. |
| 6522 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 6523 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6524 | * @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] | 6525 | * @param shortMsg Short message describing the crash. |
| 6526 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6527 | * @param stackTrace Full crash stack trace, may be null. |
| 6528 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6529 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 6530 | */ |
| 6531 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6532 | 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] | 6533 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6534 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6535 | report.condition = condition; |
| 6536 | report.processName = app.processName; |
| 6537 | report.pid = app.pid; |
| 6538 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6539 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6540 | report.shortMsg = shortMsg; |
| 6541 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6542 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6543 | |
| 6544 | return report; |
| 6545 | } |
| 6546 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 6547 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6548 | synchronized (this) { |
| 6549 | app.crashing = false; |
| 6550 | app.crashingReport = null; |
| 6551 | app.notResponding = false; |
| 6552 | app.notRespondingReport = null; |
| 6553 | if (app.anrDialog == fromDialog) { |
| 6554 | app.anrDialog = null; |
| 6555 | } |
| 6556 | if (app.waitDialog == fromDialog) { |
| 6557 | app.waitDialog = null; |
| 6558 | } |
| 6559 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 6560 | handleAppCrashLocked(app); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 6561 | Slog.i(ActivityManagerService.TAG, "Killing " |
| 6562 | + app.processName + " (pid=" + app.pid + "): user's request"); |
| 6563 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 6564 | app.processName, app.setAdj, "user's request after error"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6565 | Process.killProcess(app.pid); |
| 6566 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6567 | } |
| 6568 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 6569 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6570 | private boolean handleAppCrashLocked(ProcessRecord app) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6571 | long now = SystemClock.uptimeMillis(); |
| 6572 | |
| 6573 | Long crashTime = mProcessCrashTimes.get(app.info.processName, |
| 6574 | app.info.uid); |
| 6575 | if (crashTime != null && now < crashTime+MIN_CRASH_INTERVAL) { |
| 6576 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6577 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6578 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6579 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6580 | app.info.processName, app.info.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6581 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 6582 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6583 | if (r.app == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6584 | Slog.w(TAG, " Force finishing activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6585 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6586 | 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] | 6587 | } |
| 6588 | } |
| 6589 | if (!app.persistent) { |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 6590 | // Don't let services in this process be restarted and potentially |
| 6591 | // annoy the user repeatedly. Unless it is persistent, since those |
| 6592 | // processes run critical code. |
| 6593 | killServicesLocked(app, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6594 | // We don't want to start this process again until the user |
| 6595 | // explicitly does so... but for persistent process, we really |
| 6596 | // need to keep it running. If a persistent process is actually |
| 6597 | // repeatedly crashing, then badness for everyone. |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6598 | EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.info.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6599 | app.info.processName); |
| 6600 | mBadProcesses.put(app.info.processName, app.info.uid, now); |
| 6601 | app.bad = true; |
| 6602 | mProcessCrashTimes.remove(app.info.processName, app.info.uid); |
| 6603 | app.removed = true; |
| 6604 | removeProcessLocked(app, false); |
| 6605 | return false; |
| 6606 | } |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6607 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6608 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6609 | if (r.app == app) { |
| 6610 | // If the top running activity is from this crashing |
| 6611 | // process, then terminate it to avoid getting in a loop. |
| 6612 | Slog.w(TAG, " Force finishing activity " |
| 6613 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6614 | int index = mMainStack.indexOfTokenLocked(r); |
| 6615 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6616 | Activity.RESULT_CANCELED, null, "crashed"); |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 6617 | // Also terminate any activities below it that aren't yet |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6618 | // stopped, to avoid a situation where one will get |
| 6619 | // re-start our crashing activity once it gets resumed again. |
| 6620 | index--; |
| 6621 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6622 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6623 | if (r.state == ActivityState.RESUMED |
| 6624 | || r.state == ActivityState.PAUSING |
| 6625 | || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 6626 | if (!r.isHomeActivity || mHomeProcess != r.app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6627 | Slog.w(TAG, " Force finishing activity " |
| 6628 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6629 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 6630 | Activity.RESULT_CANCELED, null, "crashed"); |
| 6631 | } |
| 6632 | } |
| 6633 | } |
| 6634 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6635 | } |
| 6636 | |
| 6637 | // Bump up the crash count of any services currently running in the proc. |
| 6638 | if (app.services.size() != 0) { |
| 6639 | // Any services running in the application need to be placed |
| 6640 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6641 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6642 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6643 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6644 | sr.crashCount++; |
| 6645 | } |
| 6646 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 6647 | |
| 6648 | // If the crashing process is what we consider to be the "home process" and it has been |
| 6649 | // replaced by a third-party app, clear the package preferred activities from packages |
| 6650 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 6651 | // from blocking the user to manually clear the list. |
| 6652 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 6653 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6654 | Iterator it = mHomeProcess.activities.iterator(); |
| 6655 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 6656 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 6657 | if (r.isHomeActivity) { |
| 6658 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 6659 | try { |
| 6660 | ActivityThread.getPackageManager() |
| 6661 | .clearPackagePreferredActivities(r.packageName); |
| 6662 | } catch (RemoteException c) { |
| 6663 | // pm is in same process, this will never happen. |
| 6664 | } |
| 6665 | } |
| 6666 | } |
| 6667 | } |
| 6668 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6669 | mProcessCrashTimes.put(app.info.processName, app.info.uid, now); |
| 6670 | return true; |
| 6671 | } |
| 6672 | |
| 6673 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 6674 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 6675 | mContext, app.info.packageName, app.info.flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6676 | skipCurrentReceiverLocked(app); |
| 6677 | } |
| 6678 | |
| 6679 | void skipCurrentReceiverLocked(ProcessRecord app) { |
| 6680 | boolean reschedule = false; |
| 6681 | BroadcastRecord r = app.curReceiver; |
| 6682 | if (r != null) { |
| 6683 | // The current broadcast is waiting for this app's receiver |
| 6684 | // to be finished. Looks like that's not going to happen, so |
| 6685 | // let the broadcast continue. |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 6686 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6687 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 6688 | r.resultExtras, r.resultAbort, true); |
| 6689 | reschedule = true; |
| 6690 | } |
| 6691 | r = mPendingBroadcast; |
| 6692 | if (r != null && r.curApp == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6693 | if (DEBUG_BROADCAST) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6694 | "skip & discard pending app " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 6695 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6696 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 6697 | r.resultExtras, r.resultAbort, true); |
| 6698 | reschedule = true; |
| 6699 | } |
| 6700 | if (reschedule) { |
| 6701 | scheduleBroadcastsLocked(); |
| 6702 | } |
| 6703 | } |
| 6704 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6705 | /** |
| 6706 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 6707 | * The application process will exit immediately after this call returns. |
| 6708 | * @param app object of the crashing app, null for the system server |
| 6709 | * @param crashInfo describing the exception |
| 6710 | */ |
| 6711 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
| 6712 | ProcessRecord r = findAppProcess(app); |
| 6713 | |
| 6714 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
| 6715 | app == null ? "system" : (r == null ? "unknown" : r.processName), |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 6716 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6717 | crashInfo.exceptionClassName, |
| 6718 | crashInfo.exceptionMessage, |
| 6719 | crashInfo.throwFileName, |
| 6720 | crashInfo.throwLineNumber); |
| 6721 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 6722 | addErrorToDropBox("crash", r, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6723 | |
| 6724 | crashApplication(r, crashInfo); |
| 6725 | } |
| 6726 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 6727 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 6728 | IBinder app, |
| 6729 | int violationMask, |
| 6730 | StrictMode.ViolationInfo info) { |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 6731 | ProcessRecord r = findAppProcess(app); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 6732 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 6733 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 6734 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 6735 | boolean logIt = true; |
| 6736 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 6737 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 6738 | logIt = false; |
| 6739 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 6740 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 6741 | // the relative pain numbers, without logging all |
| 6742 | // the stack traces repeatedly. We'd want to do |
| 6743 | // likewise in the client code, which also does |
| 6744 | // dup suppression, before the Binder call. |
| 6745 | } else { |
| 6746 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 6747 | mAlreadyLoggedViolatedStacks.clear(); |
| 6748 | } |
| 6749 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 6750 | } |
| 6751 | } |
| 6752 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 6753 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 6754 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 6755 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 6756 | |
| 6757 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 6758 | AppErrorResult result = new AppErrorResult(); |
| 6759 | synchronized (this) { |
| 6760 | final long origId = Binder.clearCallingIdentity(); |
| 6761 | |
| 6762 | Message msg = Message.obtain(); |
| 6763 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 6764 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 6765 | data.put("result", result); |
| 6766 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 6767 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 6768 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 6769 | msg.obj = data; |
| 6770 | mHandler.sendMessage(msg); |
| 6771 | |
| 6772 | Binder.restoreCallingIdentity(origId); |
| 6773 | } |
| 6774 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6775 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 6776 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 6777 | } |
| 6778 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 6779 | // Depending on the policy in effect, there could be a bunch of |
| 6780 | // these in quick succession so we try to batch these together to |
| 6781 | // minimize disk writes, number of dropbox entries, and maximize |
| 6782 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 6783 | private void logStrictModeViolationToDropBox( |
| 6784 | ProcessRecord process, |
| 6785 | StrictMode.ViolationInfo info) { |
| 6786 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 6787 | return; |
| 6788 | } |
| 6789 | final boolean isSystemApp = process == null || |
| 6790 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 6791 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
| 6792 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 6793 | final DropBoxManager dbox = (DropBoxManager) |
| 6794 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 6795 | |
| 6796 | // Exit early if the dropbox isn't configured to accept this report type. |
| 6797 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 6798 | |
| 6799 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 6800 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 6801 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 6802 | synchronized (sb) { |
| 6803 | bufferWasEmpty = sb.length() == 0; |
| 6804 | appendDropBoxProcessHeaders(process, sb); |
| 6805 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 6806 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 6807 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 6808 | if (info.violationNumThisLoop != 0) { |
| 6809 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 6810 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 6811 | if (info.numAnimationsRunning != 0) { |
| 6812 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 6813 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 6814 | if (info.broadcastIntentAction != null) { |
| 6815 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 6816 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 6817 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 6818 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 6819 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame^] | 6820 | if (info.numInstances != -1) { |
| 6821 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 6822 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 6823 | if (info.tags != null) { |
| 6824 | for (String tag : info.tags) { |
| 6825 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 6826 | } |
| 6827 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 6828 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 6829 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 6830 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 6831 | } |
| 6832 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 6833 | |
| 6834 | // Only buffer up to ~64k. Various logging bits truncate |
| 6835 | // things at 128k. |
| 6836 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 6837 | } |
| 6838 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 6839 | // Flush immediately if the buffer's grown too large, or this |
| 6840 | // is a non-system app. Non-system apps are isolated with a |
| 6841 | // different tag & policy and not batched. |
| 6842 | // |
| 6843 | // Batching is useful during internal testing with |
| 6844 | // StrictMode settings turned up high. Without batching, |
| 6845 | // thousands of separate files could be created on boot. |
| 6846 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 6847 | new Thread("Error dump: " + dropboxTag) { |
| 6848 | @Override |
| 6849 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 6850 | String report; |
| 6851 | synchronized (sb) { |
| 6852 | report = sb.toString(); |
| 6853 | sb.delete(0, sb.length()); |
| 6854 | sb.trimToSize(); |
| 6855 | } |
| 6856 | if (report.length() != 0) { |
| 6857 | dbox.addText(dropboxTag, report); |
| 6858 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 6859 | } |
| 6860 | }.start(); |
| 6861 | return; |
| 6862 | } |
| 6863 | |
| 6864 | // System app batching: |
| 6865 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 6866 | // An existing dropbox-writing thread is outstanding, so |
| 6867 | // we don't need to start it up. The existing thread will |
| 6868 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 6869 | return; |
| 6870 | } |
| 6871 | |
| 6872 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 6873 | // (After this point, we shouldn't access AMS internal data structures.) |
| 6874 | new Thread("Error dump: " + dropboxTag) { |
| 6875 | @Override |
| 6876 | public void run() { |
| 6877 | // 5 second sleep to let stacks arrive and be batched together |
| 6878 | try { |
| 6879 | Thread.sleep(5000); // 5 seconds |
| 6880 | } catch (InterruptedException e) {} |
| 6881 | |
| 6882 | String errorReport; |
| 6883 | synchronized (mStrictModeBuffer) { |
| 6884 | errorReport = mStrictModeBuffer.toString(); |
| 6885 | if (errorReport.length() == 0) { |
| 6886 | return; |
| 6887 | } |
| 6888 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 6889 | mStrictModeBuffer.trimToSize(); |
| 6890 | } |
| 6891 | dbox.addText(dropboxTag, errorReport); |
| 6892 | } |
| 6893 | }.start(); |
| 6894 | } |
| 6895 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6896 | /** |
| 6897 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 6898 | * @param app object of the crashing app, null for the system server |
| 6899 | * @param tag reported by the caller |
| 6900 | * @param crashInfo describing the context of the error |
| 6901 | * @return true if the process should exit immediately (WTF is fatal) |
| 6902 | */ |
| 6903 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 6904 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6905 | ProcessRecord r = findAppProcess(app); |
| 6906 | |
| 6907 | EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(), |
| 6908 | app == null ? "system" : (r == null ? "unknown" : r.processName), |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 6909 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6910 | tag, crashInfo.exceptionMessage); |
| 6911 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 6912 | addErrorToDropBox("wtf", r, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6913 | |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 6914 | if (Settings.Secure.getInt(mContext.getContentResolver(), |
| 6915 | Settings.Secure.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6916 | crashApplication(r, crashInfo); |
| 6917 | return true; |
| 6918 | } else { |
| 6919 | return false; |
| 6920 | } |
| 6921 | } |
| 6922 | |
| 6923 | /** |
| 6924 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 6925 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 6926 | */ |
| 6927 | private ProcessRecord findAppProcess(IBinder app) { |
| 6928 | if (app == null) { |
| 6929 | return null; |
| 6930 | } |
| 6931 | |
| 6932 | synchronized (this) { |
| 6933 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 6934 | final int NA = apps.size(); |
| 6935 | for (int ia=0; ia<NA; ia++) { |
| 6936 | ProcessRecord p = apps.valueAt(ia); |
| 6937 | if (p.thread != null && p.thread.asBinder() == app) { |
| 6938 | return p; |
| 6939 | } |
| 6940 | } |
| 6941 | } |
| 6942 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6943 | Slog.w(TAG, "Can't find mystery application: " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6944 | return null; |
| 6945 | } |
| 6946 | } |
| 6947 | |
| 6948 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 6949 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 6950 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6951 | */ |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 6952 | private void appendDropBoxProcessHeaders(ProcessRecord process, StringBuilder sb) { |
| 6953 | // Note: ProcessRecord 'process' is guarded by the service |
| 6954 | // instance. (notably process.pkgList, which could otherwise change |
| 6955 | // concurrently during execution of this method) |
| 6956 | synchronized (this) { |
| 6957 | if (process == null || process.pid == MY_PID) { |
| 6958 | sb.append("Process: system_server\n"); |
| 6959 | } else { |
| 6960 | sb.append("Process: ").append(process.processName).append("\n"); |
| 6961 | } |
| 6962 | if (process == null) { |
| 6963 | return; |
| 6964 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 6965 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6966 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 6967 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 6968 | for (String pkg : process.pkgList) { |
| 6969 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 6970 | try { |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 6971 | PackageInfo pi = pm.getPackageInfo(pkg, 0); |
| 6972 | if (pi != null) { |
| 6973 | sb.append(" v").append(pi.versionCode); |
| 6974 | if (pi.versionName != null) { |
| 6975 | sb.append(" (").append(pi.versionName).append(")"); |
| 6976 | } |
| 6977 | } |
| 6978 | } catch (RemoteException e) { |
| 6979 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6980 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 6981 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 6982 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 6983 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 6984 | } |
| 6985 | |
| 6986 | private static String processClass(ProcessRecord process) { |
| 6987 | if (process == null || process.pid == MY_PID) { |
| 6988 | return "system_server"; |
| 6989 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 6990 | return "system_app"; |
| 6991 | } else { |
| 6992 | return "data_app"; |
| 6993 | } |
| 6994 | } |
| 6995 | |
| 6996 | /** |
| 6997 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 6998 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 6999 | * @param process which caused the error, null means the system server |
| 7000 | * @param activity which triggered the error, null if unknown |
| 7001 | * @param parent activity related to the error, null if unknown |
| 7002 | * @param subject line related to the error, null if absent |
| 7003 | * @param report in long form describing the error, null if absent |
| 7004 | * @param logFile to include in the report, null if none |
| 7005 | * @param crashInfo giving an application stack trace, null if absent |
| 7006 | */ |
| 7007 | public void addErrorToDropBox(String eventType, |
| 7008 | ProcessRecord process, ActivityRecord activity, ActivityRecord parent, String subject, |
| 7009 | final String report, final File logFile, |
| 7010 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 7011 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 7012 | // otherwise the watchdog may be prevented from resetting the system. |
| 7013 | |
| 7014 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 7015 | final DropBoxManager dbox = (DropBoxManager) |
| 7016 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7017 | |
| 7018 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7019 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7020 | |
| 7021 | final StringBuilder sb = new StringBuilder(1024); |
| 7022 | appendDropBoxProcessHeaders(process, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7023 | if (activity != null) { |
| 7024 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 7025 | } |
| 7026 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 7027 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 7028 | } |
| 7029 | if (parent != null && parent != activity) { |
| 7030 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 7031 | } |
| 7032 | if (subject != null) { |
| 7033 | sb.append("Subject: ").append(subject).append("\n"); |
| 7034 | } |
| 7035 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 7036 | if (Debug.isDebuggerConnected()) { |
| 7037 | sb.append("Debugger: Connected\n"); |
| 7038 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7039 | sb.append("\n"); |
| 7040 | |
| 7041 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 7042 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7043 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 7044 | @Override |
| 7045 | public void run() { |
| 7046 | if (report != null) { |
| 7047 | sb.append(report); |
| 7048 | } |
| 7049 | if (logFile != null) { |
| 7050 | try { |
| 7051 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 7052 | } catch (IOException e) { |
| 7053 | Slog.e(TAG, "Error reading " + logFile, e); |
| 7054 | } |
| 7055 | } |
| 7056 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 7057 | sb.append(crashInfo.stackTrace); |
| 7058 | } |
| 7059 | |
| 7060 | String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 7061 | int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0); |
| 7062 | if (lines > 0) { |
| 7063 | sb.append("\n"); |
| 7064 | |
| 7065 | // Merge several logcat streams, and take the last N lines |
| 7066 | InputStreamReader input = null; |
| 7067 | try { |
| 7068 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 7069 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 7070 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 7071 | |
| 7072 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 7073 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 7074 | input = new InputStreamReader(logcat.getInputStream()); |
| 7075 | |
| 7076 | int num; |
| 7077 | char[] buf = new char[8192]; |
| 7078 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 7079 | } catch (IOException e) { |
| 7080 | Slog.e(TAG, "Error running logcat", e); |
| 7081 | } finally { |
| 7082 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 7083 | } |
| 7084 | } |
| 7085 | |
| 7086 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7087 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7088 | }; |
| 7089 | |
| 7090 | if (process == null || process.pid == MY_PID) { |
| 7091 | worker.run(); // We may be about to die -- need to run this synchronously |
| 7092 | } else { |
| 7093 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7094 | } |
| 7095 | } |
| 7096 | |
| 7097 | /** |
| 7098 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 7099 | * Deal with edge cases (intercepts from instrumented applications, |
| 7100 | * ActivityController, error intent receivers, that sort of thing). |
| 7101 | * @param r the application crashing |
| 7102 | * @param crashInfo describing the failure |
| 7103 | */ |
| 7104 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7105 | long timeMillis = System.currentTimeMillis(); |
| 7106 | String shortMsg = crashInfo.exceptionClassName; |
| 7107 | String longMsg = crashInfo.exceptionMessage; |
| 7108 | String stackTrace = crashInfo.stackTrace; |
| 7109 | if (shortMsg != null && longMsg != null) { |
| 7110 | longMsg = shortMsg + ": " + longMsg; |
| 7111 | } else if (shortMsg != null) { |
| 7112 | longMsg = shortMsg; |
| 7113 | } |
| 7114 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7115 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7116 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7117 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7118 | try { |
| 7119 | String name = r != null ? r.processName : null; |
| 7120 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7121 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7122 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7123 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7124 | + " at watcher's request"); |
| 7125 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7126 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7127 | } |
| 7128 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7129 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7130 | } |
| 7131 | } |
| 7132 | |
| 7133 | final long origId = Binder.clearCallingIdentity(); |
| 7134 | |
| 7135 | // If this process is running instrumentation, finish it. |
| 7136 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7137 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7138 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7139 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 7140 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7141 | Bundle info = new Bundle(); |
| 7142 | info.putString("shortMsg", shortMsg); |
| 7143 | info.putString("longMsg", longMsg); |
| 7144 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 7145 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7146 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7147 | } |
| 7148 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7149 | // If we can't identify the process or it's already exceeded its crash quota, |
| 7150 | // quit right away without showing a crash dialog. |
| 7151 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7152 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7153 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7154 | } |
| 7155 | |
| 7156 | Message msg = Message.obtain(); |
| 7157 | msg.what = SHOW_ERROR_MSG; |
| 7158 | HashMap data = new HashMap(); |
| 7159 | data.put("result", result); |
| 7160 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7161 | msg.obj = data; |
| 7162 | mHandler.sendMessage(msg); |
| 7163 | |
| 7164 | Binder.restoreCallingIdentity(origId); |
| 7165 | } |
| 7166 | |
| 7167 | int res = result.get(); |
| 7168 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7169 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7170 | synchronized (this) { |
| 7171 | if (r != null) { |
| 7172 | mProcessCrashTimes.put(r.info.processName, r.info.uid, |
| 7173 | SystemClock.uptimeMillis()); |
| 7174 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7175 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7176 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7177 | } |
| 7178 | } |
| 7179 | |
| 7180 | if (appErrorIntent != null) { |
| 7181 | try { |
| 7182 | mContext.startActivity(appErrorIntent); |
| 7183 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7184 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7185 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7186 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7187 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7188 | |
| 7189 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 7190 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 7191 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7192 | if (report == null) { |
| 7193 | return null; |
| 7194 | } |
| 7195 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 7196 | result.setComponent(r.errorReportReceiver); |
| 7197 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 7198 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 7199 | return result; |
| 7200 | } |
| 7201 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7202 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 7203 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7204 | if (r.errorReportReceiver == null) { |
| 7205 | return null; |
| 7206 | } |
| 7207 | |
| 7208 | if (!r.crashing && !r.notResponding) { |
| 7209 | return null; |
| 7210 | } |
| 7211 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7212 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 7213 | report.packageName = r.info.packageName; |
| 7214 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 7215 | report.processName = r.processName; |
| 7216 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 7217 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7218 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7219 | if (r.crashing) { |
| 7220 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 7221 | report.crashInfo = crashInfo; |
| 7222 | } else if (r.notResponding) { |
| 7223 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 7224 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7225 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7226 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 7227 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 7228 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7229 | } |
| 7230 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7231 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7232 | } |
| 7233 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7234 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
| 7235 | // assume our apps are happy - lazy create the list |
| 7236 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 7237 | |
| 7238 | synchronized (this) { |
| 7239 | |
| 7240 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7241 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 7242 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7243 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 7244 | // This one's in trouble, so we'll generate a report for it |
| 7245 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 7246 | ActivityManager.ProcessErrorStateInfo report = null; |
| 7247 | if (app.crashing) { |
| 7248 | report = app.crashingReport; |
| 7249 | } else if (app.notResponding) { |
| 7250 | report = app.notRespondingReport; |
| 7251 | } |
| 7252 | |
| 7253 | if (report != null) { |
| 7254 | if (errList == null) { |
| 7255 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 7256 | } |
| 7257 | errList.add(report); |
| 7258 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7259 | 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] | 7260 | " crashing = " + app.crashing + |
| 7261 | " notResponding = " + app.notResponding); |
| 7262 | } |
| 7263 | } |
| 7264 | } |
| 7265 | } |
| 7266 | |
| 7267 | return errList; |
| 7268 | } |
| 7269 | |
| 7270 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
| 7271 | // Lazy instantiation of list |
| 7272 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
| 7273 | synchronized (this) { |
| 7274 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7275 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 7276 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7277 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 7278 | // Generate process state info for running application |
| 7279 | ActivityManager.RunningAppProcessInfo currApp = |
| 7280 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 7281 | app.pid, app.getPackageList()); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 7282 | currApp.uid = app.info.uid; |
Dianne Hackborn | baf42c6 | 2010-06-24 11:23:39 -0700 | [diff] [blame] | 7283 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 482566e | 2010-09-03 12:51:28 -0700 | [diff] [blame] | 7284 | currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
Dianne Hackborn | baf42c6 | 2010-06-24 11:23:39 -0700 | [diff] [blame] | 7285 | } |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 7286 | if (app.persistent) { |
| 7287 | currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 7288 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7289 | int adj = app.curAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7290 | if (adj >= EMPTY_APP_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7291 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_EMPTY; |
| 7292 | } else if (adj >= HIDDEN_APP_MIN_ADJ) { |
| 7293 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 7294 | currApp.lru = adj - HIDDEN_APP_MIN_ADJ + 1; |
| 7295 | } else if (adj >= HOME_APP_ADJ) { |
| 7296 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 7297 | currApp.lru = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7298 | } else if (adj >= SECONDARY_SERVER_ADJ) { |
| 7299 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 7300 | } else if (adj >= HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | 5383f50 | 2010-10-22 12:59:20 -0700 | [diff] [blame] | 7301 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 7302 | } else if (adj >= PERCEPTIBLE_APP_ADJ) { |
| 7303 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7304 | } else if (adj >= VISIBLE_APP_ADJ) { |
| 7305 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 7306 | } else { |
| 7307 | currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 7308 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 7309 | currApp.importanceReasonCode = app.adjTypeCode; |
| 7310 | if (app.adjSource instanceof ProcessRecord) { |
| 7311 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7312 | } else if (app.adjSource instanceof ActivityRecord) { |
| 7313 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 7314 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 7315 | } |
| 7316 | if (app.adjTarget instanceof ComponentName) { |
| 7317 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 7318 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7319 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7320 | // + " lru=" + currApp.lru); |
| 7321 | if (runList == null) { |
| 7322 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 7323 | } |
| 7324 | runList.add(currApp); |
| 7325 | } |
| 7326 | } |
| 7327 | } |
| 7328 | return runList; |
| 7329 | } |
| 7330 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7331 | public List<ApplicationInfo> getRunningExternalApplications() { |
| 7332 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 7333 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 7334 | if (runningApps != null && runningApps.size() > 0) { |
| 7335 | Set<String> extList = new HashSet<String>(); |
| 7336 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 7337 | if (app.pkgList != null) { |
| 7338 | for (String pkg : app.pkgList) { |
| 7339 | extList.add(pkg); |
| 7340 | } |
| 7341 | } |
| 7342 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7343 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7344 | for (String pkg : extList) { |
| 7345 | try { |
| 7346 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0); |
| 7347 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 7348 | retList.add(info); |
| 7349 | } |
| 7350 | } catch (RemoteException e) { |
| 7351 | } |
| 7352 | } |
| 7353 | } |
| 7354 | return retList; |
| 7355 | } |
| 7356 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7357 | @Override |
| 7358 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7359 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 7360 | != PackageManager.PERMISSION_GRANTED) { |
| 7361 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 7362 | + Binder.getCallingPid() |
| 7363 | + ", uid=" + Binder.getCallingUid() |
| 7364 | + " without permission " |
| 7365 | + android.Manifest.permission.DUMP); |
| 7366 | return; |
| 7367 | } |
| 7368 | |
| 7369 | boolean dumpAll = false; |
| 7370 | |
| 7371 | int opti = 0; |
| 7372 | while (opti < args.length) { |
| 7373 | String opt = args[opti]; |
| 7374 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 7375 | break; |
| 7376 | } |
| 7377 | opti++; |
| 7378 | if ("-a".equals(opt)) { |
| 7379 | dumpAll = true; |
| 7380 | } else if ("-h".equals(opt)) { |
| 7381 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 7382 | pw.println(" [-a] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7383 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7384 | pw.println(" a[ctivities]: activity stack state"); |
| 7385 | pw.println(" b[roadcasts]: broadcast state"); |
| 7386 | pw.println(" i[ntents]: pending intent state"); |
| 7387 | pw.println(" p[rocesses]: process state"); |
| 7388 | pw.println(" o[om]: out of memory management"); |
| 7389 | pw.println(" prov[iders]: content provider state"); |
| 7390 | pw.println(" s[ervices]: service state"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7391 | pw.println(" service [name]: service client-side state"); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 7392 | pw.println(" cmd may also be a component name (com.foo/.myApp),"); |
| 7393 | pw.println(" a partial substring in a component name, or an"); |
| 7394 | pw.println(" ActivityRecord hex object identifier."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7395 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7396 | } else { |
| 7397 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7398 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7399 | } |
| 7400 | |
| 7401 | // Is the caller requesting to dump a particular piece of data? |
| 7402 | if (opti < args.length) { |
| 7403 | String cmd = args[opti]; |
| 7404 | opti++; |
| 7405 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 7406 | synchronized (this) { |
| 7407 | dumpActivitiesLocked(fd, pw, args, opti, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7408 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7409 | return; |
| 7410 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
| 7411 | synchronized (this) { |
| 7412 | dumpBroadcastsLocked(fd, pw, args, opti, true); |
| 7413 | } |
| 7414 | return; |
| 7415 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
| 7416 | synchronized (this) { |
| 7417 | dumpPendingIntentsLocked(fd, pw, args, opti, true); |
| 7418 | } |
| 7419 | return; |
| 7420 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
| 7421 | synchronized (this) { |
| 7422 | dumpProcessesLocked(fd, pw, args, opti, true); |
| 7423 | } |
| 7424 | return; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7425 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 7426 | synchronized (this) { |
| 7427 | dumpOomLocked(fd, pw, args, opti, true); |
| 7428 | } |
| 7429 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7430 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 7431 | synchronized (this) { |
| 7432 | dumpProvidersLocked(fd, pw, args, opti, true); |
| 7433 | } |
| 7434 | return; |
| 7435 | } else if ("service".equals(cmd)) { |
Joe Onorato | eb95b08 | 2010-10-21 14:54:19 -0400 | [diff] [blame] | 7436 | dumpService(fd, pw, args, opti); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7437 | return; |
| 7438 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 7439 | synchronized (this) { |
| 7440 | dumpServicesLocked(fd, pw, args, opti, true); |
| 7441 | } |
| 7442 | return; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 7443 | } else { |
| 7444 | // Dumping a single activity? |
| 7445 | if (dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 7446 | return; |
| 7447 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 7448 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 7449 | pw.println("Use -h for help."); |
| 7450 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7451 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7452 | } |
| 7453 | |
| 7454 | // No piece of data specified, dump everything. |
| 7455 | synchronized (this) { |
| 7456 | boolean needSep; |
| 7457 | if (dumpAll) { |
| 7458 | pw.println("Providers in Current Activity Manager State:"); |
| 7459 | } |
| 7460 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll); |
| 7461 | if (needSep) { |
| 7462 | pw.println(" "); |
| 7463 | } |
| 7464 | if (dumpAll) { |
| 7465 | pw.println("-------------------------------------------------------------------------------"); |
| 7466 | pw.println("Broadcasts in Current Activity Manager State:"); |
| 7467 | } |
| 7468 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll); |
| 7469 | if (needSep) { |
| 7470 | pw.println(" "); |
| 7471 | } |
| 7472 | if (dumpAll) { |
| 7473 | pw.println("-------------------------------------------------------------------------------"); |
| 7474 | pw.println("Services in Current Activity Manager State:"); |
| 7475 | } |
| 7476 | needSep = dumpServicesLocked(fd, pw, args, opti, dumpAll); |
| 7477 | if (needSep) { |
| 7478 | pw.println(" "); |
| 7479 | } |
| 7480 | if (dumpAll) { |
| 7481 | pw.println("-------------------------------------------------------------------------------"); |
| 7482 | pw.println("PendingIntents in Current Activity Manager State:"); |
| 7483 | } |
| 7484 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll); |
| 7485 | if (needSep) { |
| 7486 | pw.println(" "); |
| 7487 | } |
| 7488 | if (dumpAll) { |
| 7489 | pw.println("-------------------------------------------------------------------------------"); |
| 7490 | pw.println("Activities in Current Activity Manager State:"); |
| 7491 | } |
| 7492 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, !dumpAll); |
| 7493 | if (needSep) { |
| 7494 | pw.println(" "); |
| 7495 | } |
| 7496 | if (dumpAll) { |
| 7497 | pw.println("-------------------------------------------------------------------------------"); |
| 7498 | pw.println("Processes in Current Activity Manager State:"); |
| 7499 | } |
| 7500 | dumpProcessesLocked(fd, pw, args, opti, dumpAll); |
| 7501 | } |
| 7502 | } |
| 7503 | |
| 7504 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 7505 | int opti, boolean dumpAll, boolean needHeader) { |
| 7506 | if (needHeader) { |
| 7507 | pw.println(" Activity stack:"); |
| 7508 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7509 | dumpHistoryList(pw, mMainStack.mHistory, " ", "Hist", true); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7510 | pw.println(" "); |
| 7511 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7512 | dumpHistoryList(pw, mMainStack.mLRUActivities, " ", "Run", false); |
| 7513 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7514 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7515 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7516 | dumpHistoryList(pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7517 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7518 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7519 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7520 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7521 | dumpHistoryList(pw, mMainStack.mStoppingActivities, " ", "Stop", false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7522 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7523 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7524 | pw.println(" "); |
| 7525 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7526 | dumpHistoryList(pw, mMainStack.mFinishingActivities, " ", "Fin", false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7527 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7528 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7529 | pw.println(" "); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7530 | pw.println(" mPausingActivity: " + mMainStack.mPausingActivity); |
| 7531 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7532 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7533 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7534 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7535 | if (dumpAll && mRecentTasks.size() > 0) { |
| 7536 | pw.println(" "); |
| 7537 | pw.println("Recent tasks in Current Activity Manager State:"); |
| 7538 | |
| 7539 | final int N = mRecentTasks.size(); |
| 7540 | for (int i=0; i<N; i++) { |
| 7541 | TaskRecord tr = mRecentTasks.get(i); |
| 7542 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 7543 | pw.println(tr); |
| 7544 | mRecentTasks.get(i).dump(pw, " "); |
| 7545 | } |
| 7546 | } |
| 7547 | |
| 7548 | pw.println(" "); |
| 7549 | pw.println(" mCurTask: " + mCurTask); |
| 7550 | |
| 7551 | return true; |
| 7552 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7553 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7554 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 7555 | int opti, boolean dumpAll) { |
| 7556 | boolean needSep = false; |
| 7557 | int numPers = 0; |
| 7558 | |
| 7559 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7560 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 7561 | final int NA = procs.size(); |
| 7562 | for (int ia=0; ia<NA; ia++) { |
| 7563 | if (!needSep) { |
| 7564 | pw.println(" All known processes:"); |
| 7565 | needSep = true; |
| 7566 | } |
| 7567 | ProcessRecord r = procs.valueAt(ia); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 7568 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 7569 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 7570 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7571 | r.dump(pw, " "); |
| 7572 | if (r.persistent) { |
| 7573 | numPers++; |
| 7574 | } |
| 7575 | } |
| 7576 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7577 | } |
| 7578 | |
| 7579 | if (mLruProcesses.size() > 0) { |
| 7580 | if (needSep) pw.println(" "); |
| 7581 | needSep = true; |
| 7582 | pw.println(" Running processes (most recent first):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7583 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
| 7584 | "Proc", "PERS", false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7585 | needSep = true; |
| 7586 | } |
| 7587 | |
| 7588 | synchronized (mPidsSelfLocked) { |
| 7589 | if (mPidsSelfLocked.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7590 | if (needSep) pw.println(" "); |
| 7591 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7592 | pw.println(" PID mappings:"); |
| 7593 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 7594 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 7595 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7596 | } |
| 7597 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7598 | } |
| 7599 | |
| 7600 | if (mForegroundProcesses.size() > 0) { |
| 7601 | if (needSep) pw.println(" "); |
| 7602 | needSep = true; |
| 7603 | pw.println(" Foreground Processes:"); |
| 7604 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 7605 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 7606 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7607 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7608 | } |
| 7609 | |
| 7610 | if (mPersistentStartingProcesses.size() > 0) { |
| 7611 | if (needSep) pw.println(" "); |
| 7612 | needSep = true; |
| 7613 | pw.println(" Persisent processes that are starting:"); |
| 7614 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7615 | "Starting Norm", "Restarting PERS"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7616 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7617 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7618 | if (mStartingProcesses.size() > 0) { |
| 7619 | if (needSep) pw.println(" "); |
| 7620 | needSep = true; |
| 7621 | pw.println(" Processes that are starting:"); |
| 7622 | dumpProcessList(pw, this, mStartingProcesses, " ", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7623 | "Starting Norm", "Starting PERS"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7624 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7625 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7626 | if (mRemovedProcesses.size() > 0) { |
| 7627 | if (needSep) pw.println(" "); |
| 7628 | needSep = true; |
| 7629 | pw.println(" Processes that are being removed:"); |
| 7630 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7631 | "Removed Norm", "Removed PERS"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7632 | } |
| 7633 | |
| 7634 | if (mProcessesOnHold.size() > 0) { |
| 7635 | if (needSep) pw.println(" "); |
| 7636 | needSep = true; |
| 7637 | pw.println(" Processes that are on old until the system is ready:"); |
| 7638 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7639 | "OnHold Norm", "OnHold PERS"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7640 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7641 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7642 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7643 | |
| 7644 | if (mProcessCrashTimes.getMap().size() > 0) { |
| 7645 | if (needSep) pw.println(" "); |
| 7646 | needSep = true; |
| 7647 | pw.println(" Time since processes crashed:"); |
| 7648 | long now = SystemClock.uptimeMillis(); |
| 7649 | for (Map.Entry<String, SparseArray<Long>> procs |
| 7650 | : mProcessCrashTimes.getMap().entrySet()) { |
| 7651 | SparseArray<Long> uids = procs.getValue(); |
| 7652 | final int N = uids.size(); |
| 7653 | for (int i=0; i<N; i++) { |
| 7654 | pw.print(" Process "); pw.print(procs.getKey()); |
| 7655 | pw.print(" uid "); pw.print(uids.keyAt(i)); |
| 7656 | pw.print(": last crashed "); |
| 7657 | pw.print((now-uids.valueAt(i))); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 7658 | pw.println(" ms ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 7659 | } |
| 7660 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7661 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7662 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7663 | if (mBadProcesses.getMap().size() > 0) { |
| 7664 | if (needSep) pw.println(" "); |
| 7665 | needSep = true; |
| 7666 | pw.println(" Bad processes:"); |
| 7667 | for (Map.Entry<String, SparseArray<Long>> procs |
| 7668 | : mBadProcesses.getMap().entrySet()) { |
| 7669 | SparseArray<Long> uids = procs.getValue(); |
| 7670 | final int N = uids.size(); |
| 7671 | for (int i=0; i<N; i++) { |
| 7672 | pw.print(" Bad process "); pw.print(procs.getKey()); |
| 7673 | pw.print(" uid "); pw.print(uids.keyAt(i)); |
| 7674 | pw.print(": crashed at time "); |
| 7675 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7676 | } |
| 7677 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7678 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7679 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7680 | pw.println(" "); |
| 7681 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7682 | if (mHeavyWeightProcess != null) { |
| 7683 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 7684 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7685 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7686 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7687 | pw.println(" mSleeping=" + mSleeping + " mShuttingDown=" + mShuttingDown); |
| 7688 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 7689 | || mOrigWaitForDebugger) { |
| 7690 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 7691 | + " mDebugTransient=" + mDebugTransient |
| 7692 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 7693 | } |
| 7694 | if (mAlwaysFinishActivities || mController != null) { |
| 7695 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 7696 | + " mController=" + mController); |
| 7697 | } |
| 7698 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7699 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7700 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 7701 | + " mProcessesReady=" + mProcessesReady |
| 7702 | + " mSystemReady=" + mSystemReady); |
| 7703 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7704 | + " mBooted=" + mBooted |
| 7705 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7706 | pw.print(" mLastPowerCheckRealtime="); |
| 7707 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 7708 | pw.println(""); |
| 7709 | pw.print(" mLastPowerCheckUptime="); |
| 7710 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 7711 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7712 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 7713 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7714 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7715 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7716 | |
| 7717 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7718 | } |
| 7719 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7720 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
| 7721 | int opti, boolean needSep, boolean dumpAll) { |
| 7722 | if (mProcessesToGc.size() > 0) { |
| 7723 | if (needSep) pw.println(" "); |
| 7724 | needSep = true; |
| 7725 | pw.println(" Processes that are waiting to GC:"); |
| 7726 | long now = SystemClock.uptimeMillis(); |
| 7727 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 7728 | ProcessRecord proc = mProcessesToGc.get(i); |
| 7729 | pw.print(" Process "); pw.println(proc); |
| 7730 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 7731 | pw.print(", last gced="); |
| 7732 | pw.print(now-proc.lastRequestedGc); |
| 7733 | pw.print(" ms ago, last lowMem="); |
| 7734 | pw.print(now-proc.lastLowMemory); |
| 7735 | pw.println(" ms ago"); |
| 7736 | |
| 7737 | } |
| 7738 | } |
| 7739 | return needSep; |
| 7740 | } |
| 7741 | |
| 7742 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 7743 | int opti, boolean dumpAll) { |
| 7744 | boolean needSep = false; |
| 7745 | |
| 7746 | if (mLruProcesses.size() > 0) { |
| 7747 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 7748 | |
| 7749 | Comparator<ProcessRecord> comparator = new Comparator<ProcessRecord>() { |
| 7750 | @Override |
| 7751 | public int compare(ProcessRecord object1, ProcessRecord object2) { |
| 7752 | if (object1.setAdj != object2.setAdj) { |
| 7753 | return object1.setAdj > object2.setAdj ? -1 : 1; |
| 7754 | } |
| 7755 | if (object1.setSchedGroup != object2.setSchedGroup) { |
| 7756 | return object1.setSchedGroup > object2.setSchedGroup ? -1 : 1; |
| 7757 | } |
| 7758 | if (object1.keeping != object2.keeping) { |
| 7759 | return object1.keeping ? -1 : 1; |
| 7760 | } |
| 7761 | if (object1.pid != object2.pid) { |
| 7762 | return object1.pid > object2.pid ? -1 : 1; |
| 7763 | } |
| 7764 | return 0; |
| 7765 | } |
| 7766 | }; |
| 7767 | |
| 7768 | Collections.sort(procs, comparator); |
| 7769 | |
| 7770 | if (needSep) pw.println(" "); |
| 7771 | needSep = true; |
| 7772 | pw.println(" Process OOM control:"); |
| 7773 | dumpProcessOomList(pw, this, procs, " ", |
| 7774 | "Proc", "PERS", true); |
| 7775 | needSep = true; |
| 7776 | } |
| 7777 | |
| 7778 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll); |
| 7779 | |
| 7780 | pw.println(" "); |
| 7781 | pw.println(" mHomeProcess: " + mHomeProcess); |
| 7782 | if (mHeavyWeightProcess != null) { |
| 7783 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 7784 | } |
| 7785 | |
| 7786 | return true; |
| 7787 | } |
| 7788 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7789 | /** |
| 7790 | * There are three ways to call this: |
| 7791 | * - no service specified: dump all the services |
| 7792 | * - a flattened component name that matched an existing service was specified as the |
| 7793 | * first arg: dump that one service |
| 7794 | * - the first arg isn't the flattened component name of an existing service: |
| 7795 | * dump all services whose component contains the first arg as a substring |
| 7796 | */ |
Joe Onorato | eb95b08 | 2010-10-21 14:54:19 -0400 | [diff] [blame] | 7797 | protected void dumpService(FileDescriptor fd, PrintWriter pw, String[] args, int opti) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7798 | String[] newArgs; |
| 7799 | String componentNameString; |
| 7800 | ServiceRecord r; |
Kenny Root | 3619b9ab | 2010-02-13 10:05:42 -0800 | [diff] [blame] | 7801 | if (opti >= args.length) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7802 | componentNameString = null; |
| 7803 | newArgs = EMPTY_STRING_ARRAY; |
| 7804 | r = null; |
| 7805 | } else { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7806 | componentNameString = args[opti]; |
| 7807 | opti++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7808 | ComponentName componentName = ComponentName.unflattenFromString(componentNameString); |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 7809 | synchronized (this) { |
| 7810 | r = componentName != null ? mServices.get(componentName) : null; |
| 7811 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7812 | newArgs = new String[args.length - opti]; |
| 7813 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7814 | } |
| 7815 | |
| 7816 | if (r != null) { |
Joe Onorato | eb95b08 | 2010-10-21 14:54:19 -0400 | [diff] [blame] | 7817 | dumpService(fd, pw, r, newArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7818 | } else { |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 7819 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
| 7820 | synchronized (this) { |
| 7821 | for (ServiceRecord r1 : mServices.values()) { |
| 7822 | if (componentNameString == null |
| 7823 | || r1.name.flattenToString().contains(componentNameString)) { |
| 7824 | services.add(r1); |
| 7825 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7826 | } |
| 7827 | } |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 7828 | for (int i=0; i<services.size(); i++) { |
Joe Onorato | eb95b08 | 2010-10-21 14:54:19 -0400 | [diff] [blame] | 7829 | dumpService(fd, pw, services.get(i), newArgs); |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 7830 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7831 | } |
| 7832 | } |
| 7833 | |
| 7834 | /** |
| 7835 | * Invokes IApplicationThread.dumpService() on the thread of the specified service if |
| 7836 | * there is a thread associated with the service. |
| 7837 | */ |
Joe Onorato | eb95b08 | 2010-10-21 14:54:19 -0400 | [diff] [blame] | 7838 | private void dumpService(FileDescriptor fd, PrintWriter pw, ServiceRecord r, String[] args) { |
| 7839 | pw.println("------------------------------------------------------------" |
| 7840 | + "-------------------"); |
| 7841 | pw.println("APP SERVICE: " + r.name.flattenToString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7842 | if (r.app != null && r.app.thread != null) { |
| 7843 | try { |
| 7844 | // flush anything that is already in the PrintWriter since the thread is going |
| 7845 | // to write to the file descriptor directly |
| 7846 | pw.flush(); |
| 7847 | r.app.thread.dumpService(fd, r, args); |
| 7848 | pw.print("\n"); |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 7849 | pw.flush(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7850 | } catch (RemoteException e) { |
| 7851 | pw.println("got a RemoteException while dumping the service"); |
| 7852 | } |
| 7853 | } |
| 7854 | } |
| 7855 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 7856 | /** |
| 7857 | * There are three things that cmd can be: |
| 7858 | * - a flattened component name that matched an existing activity |
| 7859 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 7860 | * dump all activity whose component contains the cmd as a substring |
| 7861 | * - A hex number of the ActivityRecord object instance. |
| 7862 | */ |
| 7863 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 7864 | int opti, boolean dumpAll) { |
| 7865 | String[] newArgs; |
| 7866 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 7867 | int objectId = 0; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 7868 | if (componentName == null) { |
| 7869 | // Not a '/' separated full component name; maybe an object ID? |
| 7870 | try { |
| 7871 | objectId = Integer.parseInt(name, 16); |
| 7872 | name = null; |
| 7873 | componentName = null; |
| 7874 | } catch (RuntimeException e) { |
| 7875 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 7876 | } |
| 7877 | newArgs = new String[args.length - opti]; |
| 7878 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 7879 | |
| 7880 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
| 7881 | synchronized (this) { |
| 7882 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
| 7883 | if (componentName != null) { |
| 7884 | if (r1.intent.getComponent().equals(componentName)) { |
| 7885 | activities.add(r1); |
| 7886 | } |
| 7887 | } else if (name != null) { |
| 7888 | if (r1.intent.getComponent().flattenToString().contains(name)) { |
| 7889 | activities.add(r1); |
| 7890 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 7891 | } else if (System.identityHashCode(r1) == objectId) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 7892 | activities.add(r1); |
| 7893 | } |
| 7894 | } |
| 7895 | } |
| 7896 | |
| 7897 | if (activities.size() <= 0) { |
| 7898 | return false; |
| 7899 | } |
| 7900 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 7901 | TaskRecord lastTask = null; |
| 7902 | for (int i=activities.size()-1; i>=0; i--) { |
| 7903 | ActivityRecord r = (ActivityRecord)activities.get(i); |
| 7904 | if (lastTask != r.task) { |
| 7905 | lastTask = r.task; |
| 7906 | pw.print("* Task "); pw.print(lastTask.affinity); |
| 7907 | pw.print(" id="); pw.println(lastTask.taskId); |
| 7908 | if (dumpAll) { |
| 7909 | lastTask.dump(pw, " "); |
| 7910 | } |
| 7911 | } |
| 7912 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 7913 | } |
| 7914 | return true; |
| 7915 | } |
| 7916 | |
| 7917 | /** |
| 7918 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 7919 | * there is a thread associated with the activity. |
| 7920 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 7921 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
| 7922 | ActivityRecord r, String[] args, boolean dumpAll) { |
| 7923 | synchronized (this) { |
| 7924 | pw.print(prefix); pw.print("* Activity "); |
| 7925 | pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 7926 | pw.print(" "); pw.print(r.shortComponentName); pw.print(" pid="); |
| 7927 | if (r.app != null) pw.println(r.app.pid); |
| 7928 | else pw.println("(not running)"); |
| 7929 | if (dumpAll) { |
| 7930 | r.dump(pw, prefix + " "); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 7931 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 7932 | } |
| 7933 | if (r.app != null && r.app.thread != null) { |
| 7934 | try { |
| 7935 | // flush anything that is already in the PrintWriter since the thread is going |
| 7936 | // to write to the file descriptor directly |
| 7937 | pw.flush(); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 7938 | r.app.thread.dumpActivity(fd, r, prefix + " ", args); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 7939 | pw.flush(); |
| 7940 | } catch (RemoteException e) { |
| 7941 | pw.println("got a RemoteException while dumping the activity"); |
| 7942 | } |
| 7943 | } |
| 7944 | } |
| 7945 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7946 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 7947 | int opti, boolean dumpAll) { |
| 7948 | boolean needSep = false; |
| 7949 | |
| 7950 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7951 | if (mRegisteredReceivers.size() > 0) { |
| 7952 | pw.println(" "); |
| 7953 | pw.println(" Registered Receivers:"); |
| 7954 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 7955 | while (it.hasNext()) { |
| 7956 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 7957 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7958 | r.dump(pw, " "); |
| 7959 | } |
| 7960 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7961 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7962 | pw.println(" "); |
| 7963 | pw.println("Receiver Resolver Table:"); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7964 | mReceiverResolver.dump(pw, null, " ", null, false); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7965 | needSep = true; |
| 7966 | } |
| 7967 | |
| 7968 | if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0 |
| 7969 | || mPendingBroadcast != null) { |
| 7970 | if (mParallelBroadcasts.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7971 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7972 | pw.println(" Active broadcasts:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7973 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7974 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 7975 | pw.println(" Broadcast #" + i + ":"); |
| 7976 | mParallelBroadcasts.get(i).dump(pw, " "); |
| 7977 | } |
| 7978 | if (mOrderedBroadcasts.size() > 0) { |
| 7979 | pw.println(" "); |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 7980 | pw.println(" Active ordered broadcasts:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7981 | } |
| 7982 | for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) { |
| 7983 | pw.println(" Serialized Broadcast #" + i + ":"); |
| 7984 | mOrderedBroadcasts.get(i).dump(pw, " "); |
| 7985 | } |
| 7986 | pw.println(" "); |
| 7987 | pw.println(" Pending broadcast:"); |
| 7988 | if (mPendingBroadcast != null) { |
| 7989 | mPendingBroadcast.dump(pw, " "); |
| 7990 | } else { |
| 7991 | pw.println(" (null)"); |
| 7992 | } |
| 7993 | needSep = true; |
| 7994 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7995 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7996 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7997 | pw.println(" "); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 7998 | pw.println(" Historical broadcasts:"); |
| 7999 | for (int i=0; i<MAX_BROADCAST_HISTORY; i++) { |
| 8000 | BroadcastRecord r = mBroadcastHistory[i]; |
| 8001 | if (r == null) { |
| 8002 | break; |
| 8003 | } |
| 8004 | pw.println(" Historical Broadcast #" + i + ":"); |
| 8005 | r.dump(pw, " "); |
| 8006 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8007 | needSep = true; |
| 8008 | } |
| 8009 | |
| 8010 | if (mStickyBroadcasts != null) { |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 8011 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8012 | pw.println(" Sticky broadcasts:"); |
| 8013 | StringBuilder sb = new StringBuilder(128); |
| 8014 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 8015 | : mStickyBroadcasts.entrySet()) { |
| 8016 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
| 8017 | pw.println(":"); |
| 8018 | ArrayList<Intent> intents = ent.getValue(); |
| 8019 | final int N = intents.size(); |
| 8020 | for (int i=0; i<N; i++) { |
| 8021 | sb.setLength(0); |
| 8022 | sb.append(" Intent: "); |
| 8023 | intents.get(i).toShortString(sb, true, false); |
| 8024 | pw.println(sb.toString()); |
| 8025 | Bundle bundle = intents.get(i).getExtras(); |
| 8026 | if (bundle != null) { |
| 8027 | pw.print(" "); |
| 8028 | pw.println(bundle.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8029 | } |
| 8030 | } |
| 8031 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8032 | needSep = true; |
| 8033 | } |
| 8034 | |
| 8035 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8036 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8037 | pw.println(" mBroadcastsScheduled=" + mBroadcastsScheduled); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8038 | pw.println(" mHandler:"); |
| 8039 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8040 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8041 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8042 | |
| 8043 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8044 | } |
| 8045 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8046 | boolean dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8047 | int opti, boolean dumpAll) { |
| 8048 | boolean needSep = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8049 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8050 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8051 | if (mServices.size() > 0) { |
| 8052 | pw.println(" Active services:"); |
| 8053 | Iterator<ServiceRecord> it = mServices.values().iterator(); |
| 8054 | while (it.hasNext()) { |
| 8055 | ServiceRecord r = it.next(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8056 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8057 | r.dump(pw, " "); |
| 8058 | } |
| 8059 | needSep = true; |
| 8060 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8061 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8062 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8063 | if (mPendingServices.size() > 0) { |
| 8064 | if (needSep) pw.println(" "); |
| 8065 | pw.println(" Pending services:"); |
| 8066 | for (int i=0; i<mPendingServices.size(); i++) { |
| 8067 | ServiceRecord r = mPendingServices.get(i); |
| 8068 | pw.print(" * Pending "); pw.println(r); |
| 8069 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8070 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8071 | needSep = true; |
| 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 (mRestartingServices.size() > 0) { |
| 8075 | if (needSep) pw.println(" "); |
| 8076 | pw.println(" Restarting services:"); |
| 8077 | for (int i=0; i<mRestartingServices.size(); i++) { |
| 8078 | ServiceRecord r = mRestartingServices.get(i); |
| 8079 | pw.print(" * Restarting "); pw.println(r); |
| 8080 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8081 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8082 | needSep = true; |
| 8083 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8084 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8085 | if (mStoppingServices.size() > 0) { |
| 8086 | if (needSep) pw.println(" "); |
| 8087 | pw.println(" Stopping services:"); |
| 8088 | for (int i=0; i<mStoppingServices.size(); i++) { |
| 8089 | ServiceRecord r = mStoppingServices.get(i); |
| 8090 | pw.print(" * Stopping "); pw.println(r); |
| 8091 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8092 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8093 | needSep = true; |
| 8094 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8095 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8096 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8097 | if (mServiceConnections.size() > 0) { |
| 8098 | if (needSep) pw.println(" "); |
| 8099 | pw.println(" Connection bindings to services:"); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8100 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8101 | = mServiceConnections.values().iterator(); |
| 8102 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8103 | ArrayList<ConnectionRecord> r = it.next(); |
| 8104 | for (int i=0; i<r.size(); i++) { |
| 8105 | pw.print(" * "); pw.println(r.get(i)); |
| 8106 | r.get(i).dump(pw, " "); |
| 8107 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8108 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8109 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8110 | } |
| 8111 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8112 | |
| 8113 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8114 | } |
| 8115 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8116 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8117 | int opti, boolean dumpAll) { |
| 8118 | boolean needSep = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8119 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8120 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8121 | if (mProvidersByClass.size() > 0) { |
| 8122 | if (needSep) pw.println(" "); |
| 8123 | pw.println(" Published content providers (by class):"); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8124 | Iterator<Map.Entry<String, ContentProviderRecord>> it |
| 8125 | = mProvidersByClass.entrySet().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8126 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8127 | Map.Entry<String, ContentProviderRecord> e = it.next(); |
| 8128 | ContentProviderRecord r = e.getValue(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8129 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8130 | r.dump(pw, " "); |
| 8131 | } |
| 8132 | needSep = true; |
| 8133 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8134 | |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8135 | if (mProvidersByName.size() > 0) { |
| 8136 | pw.println(" "); |
| 8137 | pw.println(" Authority to provider mappings:"); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8138 | Iterator<Map.Entry<String, ContentProviderRecord>> it |
| 8139 | = mProvidersByName.entrySet().iterator(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8140 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8141 | Map.Entry<String, ContentProviderRecord> e = it.next(); |
| 8142 | ContentProviderRecord r = e.getValue(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8143 | pw.print(" "); pw.print(e.getKey()); pw.print(": "); |
| 8144 | pw.println(r); |
| 8145 | } |
| 8146 | needSep = true; |
| 8147 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8148 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8149 | |
| 8150 | if (mLaunchingProviders.size() > 0) { |
| 8151 | if (needSep) pw.println(" "); |
| 8152 | pw.println(" Launching content providers:"); |
| 8153 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
| 8154 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
| 8155 | pw.println(mLaunchingProviders.get(i)); |
| 8156 | } |
| 8157 | needSep = true; |
| 8158 | } |
| 8159 | |
| 8160 | if (mGrantedUriPermissions.size() > 0) { |
| 8161 | pw.println(); |
| 8162 | pw.println("Granted Uri Permissions:"); |
| 8163 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 8164 | int uid = mGrantedUriPermissions.keyAt(i); |
| 8165 | HashMap<Uri, UriPermission> perms |
| 8166 | = mGrantedUriPermissions.valueAt(i); |
| 8167 | pw.print(" * UID "); pw.print(uid); |
| 8168 | pw.println(" holds:"); |
| 8169 | for (UriPermission perm : perms.values()) { |
| 8170 | pw.print(" "); pw.println(perm); |
| 8171 | perm.dump(pw, " "); |
| 8172 | } |
| 8173 | } |
| 8174 | needSep = true; |
| 8175 | } |
| 8176 | |
| 8177 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8178 | } |
| 8179 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8180 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8181 | int opti, boolean dumpAll) { |
| 8182 | boolean needSep = false; |
| 8183 | |
| 8184 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8185 | if (this.mIntentSenderRecords.size() > 0) { |
| 8186 | Iterator<WeakReference<PendingIntentRecord>> it |
| 8187 | = mIntentSenderRecords.values().iterator(); |
| 8188 | while (it.hasNext()) { |
| 8189 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 8190 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8191 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8192 | if (rec != null) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8193 | pw.print(" * "); pw.println(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8194 | rec.dump(pw, " "); |
| 8195 | } else { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8196 | pw.print(" * "); pw.print(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8197 | } |
| 8198 | } |
| 8199 | } |
| 8200 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8201 | |
| 8202 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8203 | } |
| 8204 | |
| 8205 | private static final void dumpHistoryList(PrintWriter pw, List list, |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 8206 | String prefix, String label, boolean complete) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8207 | TaskRecord lastTask = null; |
| 8208 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8209 | ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8210 | final boolean full = complete || !r.inHistory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8211 | if (lastTask != r.task) { |
| 8212 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8213 | pw.print(prefix); |
| 8214 | pw.print(full ? "* " : " "); |
| 8215 | pw.println(lastTask); |
| 8216 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 8217 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 8218 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8219 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8220 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 8221 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 8222 | pw.println(r); |
| 8223 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 8224 | r.dump(pw, prefix + " "); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 8225 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8226 | } |
| 8227 | } |
| 8228 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 8229 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 8230 | if (val == base) { |
| 8231 | if (space == null) return prefix; |
| 8232 | return prefix + " "; |
| 8233 | } |
| 8234 | return prefix + "+" + Integer.toString(val-base); |
| 8235 | } |
| 8236 | |
| 8237 | private static final int dumpProcessList(PrintWriter pw, |
| 8238 | ActivityManagerService service, List list, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8239 | String prefix, String normalLabel, String persistentLabel) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8240 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 8241 | final int N = list.size()-1; |
| 8242 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8243 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8244 | pw.println(String.format("%s%s #%2d: %s", |
| 8245 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 8246 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8247 | if (r.persistent) { |
| 8248 | numPers++; |
| 8249 | } |
| 8250 | } |
| 8251 | return numPers; |
| 8252 | } |
| 8253 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8254 | private static final void dumpProcessOomList(PrintWriter pw, |
| 8255 | ActivityManagerService service, List<ProcessRecord> list, |
| 8256 | String prefix, String normalLabel, String persistentLabel, |
| 8257 | boolean inclDetails) { |
| 8258 | |
| 8259 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 8260 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 8261 | final long curUptime = SystemClock.uptimeMillis(); |
| 8262 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 8263 | |
| 8264 | final int N = list.size()-1; |
| 8265 | for (int i=N; i>=0; i--) { |
| 8266 | ProcessRecord r = list.get(i); |
| 8267 | String oomAdj; |
| 8268 | if (r.setAdj >= EMPTY_APP_ADJ) { |
| 8269 | oomAdj = buildOomTag("empty", null, r.setAdj, EMPTY_APP_ADJ); |
| 8270 | } else if (r.setAdj >= HIDDEN_APP_MIN_ADJ) { |
| 8271 | oomAdj = buildOomTag("bak", " ", r.setAdj, HIDDEN_APP_MIN_ADJ); |
| 8272 | } else if (r.setAdj >= HOME_APP_ADJ) { |
| 8273 | oomAdj = buildOomTag("home ", null, r.setAdj, HOME_APP_ADJ); |
| 8274 | } else if (r.setAdj >= SECONDARY_SERVER_ADJ) { |
| 8275 | oomAdj = buildOomTag("svc", " ", r.setAdj, SECONDARY_SERVER_ADJ); |
| 8276 | } else if (r.setAdj >= BACKUP_APP_ADJ) { |
| 8277 | oomAdj = buildOomTag("bckup", null, r.setAdj, BACKUP_APP_ADJ); |
| 8278 | } else if (r.setAdj >= HEAVY_WEIGHT_APP_ADJ) { |
| 8279 | oomAdj = buildOomTag("hvy ", null, r.setAdj, HEAVY_WEIGHT_APP_ADJ); |
| 8280 | } else if (r.setAdj >= PERCEPTIBLE_APP_ADJ) { |
| 8281 | oomAdj = buildOomTag("prcp ", null, r.setAdj, PERCEPTIBLE_APP_ADJ); |
| 8282 | } else if (r.setAdj >= VISIBLE_APP_ADJ) { |
| 8283 | oomAdj = buildOomTag("vis ", null, r.setAdj, VISIBLE_APP_ADJ); |
| 8284 | } else if (r.setAdj >= FOREGROUND_APP_ADJ) { |
| 8285 | oomAdj = buildOomTag("fore ", null, r.setAdj, FOREGROUND_APP_ADJ); |
| 8286 | } else if (r.setAdj >= CORE_SERVER_ADJ) { |
| 8287 | oomAdj = buildOomTag("core ", null, r.setAdj, CORE_SERVER_ADJ); |
| 8288 | } else if (r.setAdj >= SYSTEM_ADJ) { |
| 8289 | oomAdj = buildOomTag("sys ", null, r.setAdj, SYSTEM_ADJ); |
| 8290 | } else { |
| 8291 | oomAdj = Integer.toString(r.setAdj); |
| 8292 | } |
| 8293 | String schedGroup; |
| 8294 | switch (r.setSchedGroup) { |
| 8295 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 8296 | schedGroup = "B"; |
| 8297 | break; |
| 8298 | case Process.THREAD_GROUP_DEFAULT: |
| 8299 | schedGroup = "F"; |
| 8300 | break; |
| 8301 | default: |
| 8302 | schedGroup = Integer.toString(r.setSchedGroup); |
| 8303 | break; |
| 8304 | } |
| 8305 | pw.println(String.format("%s%s #%2d: adj=%s/%s %s (%s)", |
| 8306 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 8307 | N-i, oomAdj, schedGroup, r.toShortString(), r.adjType)); |
| 8308 | if (r.adjSource != null || r.adjTarget != null) { |
| 8309 | pw.print(prefix); |
| 8310 | pw.print(" "); |
| 8311 | if (r.adjTarget instanceof ComponentName) { |
| 8312 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 8313 | } else if (r.adjTarget != null) { |
| 8314 | pw.print(r.adjTarget.toString()); |
| 8315 | } else { |
| 8316 | pw.print("{null}"); |
| 8317 | } |
| 8318 | pw.print("<="); |
| 8319 | if (r.adjSource instanceof ProcessRecord) { |
| 8320 | pw.print("Proc{"); |
| 8321 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 8322 | pw.println("}"); |
| 8323 | } else if (r.adjSource != null) { |
| 8324 | pw.println(r.adjSource.toString()); |
| 8325 | } else { |
| 8326 | pw.println("{null}"); |
| 8327 | } |
| 8328 | } |
| 8329 | if (inclDetails) { |
| 8330 | pw.print(prefix); |
| 8331 | pw.print(" "); |
| 8332 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 8333 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
| 8334 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 8335 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 8336 | pw.print(" cur="); pw.print(r.curAdj); |
| 8337 | pw.print(" set="); pw.println(r.setAdj); |
| 8338 | pw.print(prefix); |
| 8339 | pw.print(" "); |
| 8340 | pw.print("keeping="); pw.print(r.keeping); |
| 8341 | pw.print(" hidden="); pw.print(r.hidden); |
| 8342 | pw.print(" empty="); pw.println(r.empty); |
| 8343 | |
| 8344 | if (!r.keeping) { |
| 8345 | if (r.lastWakeTime != 0) { |
| 8346 | long wtime; |
| 8347 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 8348 | synchronized (stats) { |
| 8349 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 8350 | r.pid, curRealtime); |
| 8351 | } |
| 8352 | long timeUsed = wtime - r.lastWakeTime; |
| 8353 | pw.print(prefix); |
| 8354 | pw.print(" "); |
| 8355 | pw.print("keep awake over "); |
| 8356 | TimeUtils.formatDuration(realtimeSince, pw); |
| 8357 | pw.print(" used "); |
| 8358 | TimeUtils.formatDuration(timeUsed, pw); |
| 8359 | pw.print(" ("); |
| 8360 | pw.print((timeUsed*100)/realtimeSince); |
| 8361 | pw.println("%)"); |
| 8362 | } |
| 8363 | if (r.lastCpuTime != 0) { |
| 8364 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 8365 | pw.print(prefix); |
| 8366 | pw.print(" "); |
| 8367 | pw.print("run cpu over "); |
| 8368 | TimeUtils.formatDuration(uptimeSince, pw); |
| 8369 | pw.print(" used "); |
| 8370 | TimeUtils.formatDuration(timeUsed, pw); |
| 8371 | pw.print(" ("); |
| 8372 | pw.print((timeUsed*100)/uptimeSince); |
| 8373 | pw.println("%)"); |
| 8374 | } |
| 8375 | } |
| 8376 | } |
| 8377 | } |
| 8378 | } |
| 8379 | |
Dianne Hackborn | 472ad87 | 2010-04-07 17:31:48 -0700 | [diff] [blame] | 8380 | static final void dumpApplicationMemoryUsage(FileDescriptor fd, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8381 | PrintWriter pw, List list, String prefix, String[] args) { |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 8382 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8383 | long uptime = SystemClock.uptimeMillis(); |
| 8384 | long realtime = SystemClock.elapsedRealtime(); |
| 8385 | |
| 8386 | if (isCheckinRequest) { |
| 8387 | // short checkin version |
| 8388 | pw.println(uptime + "," + realtime); |
| 8389 | pw.flush(); |
| 8390 | } else { |
| 8391 | pw.println("Applications Memory Usage (kB):"); |
| 8392 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 8393 | } |
| 8394 | for (int i = list.size() - 1 ; i >= 0 ; i--) { |
| 8395 | ProcessRecord r = (ProcessRecord)list.get(i); |
| 8396 | if (r.thread != null) { |
| 8397 | if (!isCheckinRequest) { |
| 8398 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 8399 | pw.flush(); |
| 8400 | } |
| 8401 | try { |
| 8402 | r.thread.asBinder().dump(fd, args); |
| 8403 | } catch (RemoteException e) { |
| 8404 | if (!isCheckinRequest) { |
| 8405 | pw.println("Got RemoteException!"); |
| 8406 | pw.flush(); |
| 8407 | } |
| 8408 | } |
| 8409 | } |
| 8410 | } |
| 8411 | } |
| 8412 | |
| 8413 | /** |
| 8414 | * Searches array of arguments for the specified string |
| 8415 | * @param args array of argument strings |
| 8416 | * @param value value to search for |
| 8417 | * @return true if the value is contained in the array |
| 8418 | */ |
| 8419 | private static boolean scanArgs(String[] args, String value) { |
| 8420 | if (args != null) { |
| 8421 | for (String arg : args) { |
| 8422 | if (value.equals(arg)) { |
| 8423 | return true; |
| 8424 | } |
| 8425 | } |
| 8426 | } |
| 8427 | return false; |
| 8428 | } |
| 8429 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8430 | private final void killServicesLocked(ProcessRecord app, |
| 8431 | boolean allowRestart) { |
| 8432 | // Report disconnected services. |
| 8433 | if (false) { |
| 8434 | // XXX we are letting the client link to the service for |
| 8435 | // death notifications. |
| 8436 | if (app.services.size() > 0) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8437 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8438 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8439 | ServiceRecord r = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8440 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8441 | Iterator<ArrayList<ConnectionRecord>> jt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8442 | = r.connections.values().iterator(); |
| 8443 | while (jt.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8444 | ArrayList<ConnectionRecord> cl = jt.next(); |
| 8445 | for (int i=0; i<cl.size(); i++) { |
| 8446 | ConnectionRecord c = cl.get(i); |
| 8447 | if (c.binding.client != app) { |
| 8448 | try { |
| 8449 | //c.conn.connected(r.className, null); |
| 8450 | } catch (Exception e) { |
| 8451 | // todo: this should be asynchronous! |
| 8452 | Slog.w(TAG, "Exception thrown disconnected servce " |
| 8453 | + r.shortName |
| 8454 | + " from app " + app.processName, e); |
| 8455 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8456 | } |
| 8457 | } |
| 8458 | } |
| 8459 | } |
| 8460 | } |
| 8461 | } |
| 8462 | } |
| 8463 | |
| 8464 | // Clean up any connections this application has to other services. |
| 8465 | if (app.connections.size() > 0) { |
| 8466 | Iterator<ConnectionRecord> it = app.connections.iterator(); |
| 8467 | while (it.hasNext()) { |
| 8468 | ConnectionRecord r = it.next(); |
| 8469 | removeConnectionLocked(r, app, null); |
| 8470 | } |
| 8471 | } |
| 8472 | app.connections.clear(); |
| 8473 | |
| 8474 | if (app.services.size() != 0) { |
| 8475 | // Any services running in the application need to be placed |
| 8476 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8477 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8478 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8479 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8480 | synchronized (sr.stats.getBatteryStats()) { |
| 8481 | sr.stats.stopLaunchedLocked(); |
| 8482 | } |
| 8483 | sr.app = null; |
| 8484 | sr.executeNesting = 0; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8485 | if (mStoppingServices.remove(sr)) { |
| 8486 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
| 8487 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 8488 | |
| 8489 | boolean hasClients = sr.bindings.size() > 0; |
| 8490 | if (hasClients) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8491 | Iterator<IntentBindRecord> bindings |
| 8492 | = sr.bindings.values().iterator(); |
| 8493 | while (bindings.hasNext()) { |
| 8494 | IntentBindRecord b = bindings.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8495 | if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8496 | + ": shouldUnbind=" + b.hasBound); |
| 8497 | b.binder = null; |
| 8498 | b.requested = b.received = b.hasBound = false; |
| 8499 | } |
| 8500 | } |
| 8501 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 8502 | if (sr.crashCount >= 2 && (sr.serviceInfo.applicationInfo.flags |
| 8503 | &ApplicationInfo.FLAG_PERSISTENT) == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8504 | Slog.w(TAG, "Service crashed " + sr.crashCount |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8505 | + " times, stopping: " + sr); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 8506 | EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8507 | sr.crashCount, sr.shortName, app.pid); |
| 8508 | bringDownServiceLocked(sr, true); |
| 8509 | } else if (!allowRestart) { |
| 8510 | bringDownServiceLocked(sr, true); |
| 8511 | } else { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 8512 | boolean canceled = scheduleServiceRestartLocked(sr, true); |
| 8513 | |
| 8514 | // Should the service remain running? Note that in the |
| 8515 | // extreme case of so many attempts to deliver a command |
| 8516 | // that it failed, that we also will stop it here. |
| 8517 | if (sr.startRequested && (sr.stopIfKilled || canceled)) { |
| 8518 | if (sr.pendingStarts.size() == 0) { |
| 8519 | sr.startRequested = false; |
| 8520 | if (!hasClients) { |
| 8521 | // Whoops, no reason to restart! |
| 8522 | bringDownServiceLocked(sr, true); |
| 8523 | } |
| 8524 | } |
| 8525 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8526 | } |
| 8527 | } |
| 8528 | |
| 8529 | if (!allowRestart) { |
| 8530 | app.services.clear(); |
| 8531 | } |
| 8532 | } |
| 8533 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 8534 | // Make sure we have no more records on the stopping list. |
| 8535 | int i = mStoppingServices.size(); |
| 8536 | while (i > 0) { |
| 8537 | i--; |
| 8538 | ServiceRecord sr = mStoppingServices.get(i); |
| 8539 | if (sr.app == app) { |
| 8540 | mStoppingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8541 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 8542 | } |
| 8543 | } |
| 8544 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8545 | app.executingServices.clear(); |
| 8546 | } |
| 8547 | |
| 8548 | private final void removeDyingProviderLocked(ProcessRecord proc, |
| 8549 | ContentProviderRecord cpr) { |
| 8550 | synchronized (cpr) { |
| 8551 | cpr.launchingApp = null; |
| 8552 | cpr.notifyAll(); |
| 8553 | } |
| 8554 | |
| 8555 | mProvidersByClass.remove(cpr.info.name); |
| 8556 | String names[] = cpr.info.authority.split(";"); |
| 8557 | for (int j = 0; j < names.length; j++) { |
| 8558 | mProvidersByName.remove(names[j]); |
| 8559 | } |
| 8560 | |
| 8561 | Iterator<ProcessRecord> cit = cpr.clients.iterator(); |
| 8562 | while (cit.hasNext()) { |
| 8563 | ProcessRecord capp = cit.next(); |
| 8564 | if (!capp.persistent && capp.thread != null |
| 8565 | && capp.pid != 0 |
| 8566 | && capp.pid != MY_PID) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 8567 | Slog.i(TAG, "Kill " + capp.processName |
| 8568 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
| 8569 | + " in dying process " + proc.processName); |
| 8570 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid, |
| 8571 | capp.processName, capp.setAdj, "dying provider " + proc.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8572 | Process.killProcess(capp.pid); |
| 8573 | } |
| 8574 | } |
| 8575 | |
| 8576 | mLaunchingProviders.remove(cpr); |
| 8577 | } |
| 8578 | |
| 8579 | /** |
| 8580 | * Main code for cleaning up a process when it has gone away. This is |
| 8581 | * called both as a result of the process dying, or directly when stopping |
| 8582 | * a process when running in single process mode. |
| 8583 | */ |
| 8584 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
| 8585 | boolean restarting, int index) { |
| 8586 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8587 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8588 | } |
| 8589 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 8590 | mProcessesToGc.remove(app); |
| 8591 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8592 | // Dismiss any open dialogs. |
| 8593 | if (app.crashDialog != null) { |
| 8594 | app.crashDialog.dismiss(); |
| 8595 | app.crashDialog = null; |
| 8596 | } |
| 8597 | if (app.anrDialog != null) { |
| 8598 | app.anrDialog.dismiss(); |
| 8599 | app.anrDialog = null; |
| 8600 | } |
| 8601 | if (app.waitDialog != null) { |
| 8602 | app.waitDialog.dismiss(); |
| 8603 | app.waitDialog = null; |
| 8604 | } |
| 8605 | |
| 8606 | app.crashing = false; |
| 8607 | app.notResponding = false; |
| 8608 | |
| 8609 | app.resetPackageList(); |
| 8610 | app.thread = null; |
| 8611 | app.forcingToForeground = null; |
| 8612 | app.foregroundServices = false; |
| 8613 | |
| 8614 | killServicesLocked(app, true); |
| 8615 | |
| 8616 | boolean restart = false; |
| 8617 | |
| 8618 | int NL = mLaunchingProviders.size(); |
| 8619 | |
| 8620 | // Remove published content providers. |
| 8621 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8622 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8623 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8624 | ContentProviderRecord cpr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8625 | cpr.provider = null; |
| 8626 | cpr.app = null; |
| 8627 | |
| 8628 | // See if someone is waiting for this provider... in which |
| 8629 | // case we don't remove it, but just let it restart. |
| 8630 | int i = 0; |
| 8631 | if (!app.bad) { |
| 8632 | for (; i<NL; i++) { |
| 8633 | if (mLaunchingProviders.get(i) == cpr) { |
| 8634 | restart = true; |
| 8635 | break; |
| 8636 | } |
| 8637 | } |
| 8638 | } else { |
| 8639 | i = NL; |
| 8640 | } |
| 8641 | |
| 8642 | if (i >= NL) { |
| 8643 | removeDyingProviderLocked(app, cpr); |
| 8644 | NL = mLaunchingProviders.size(); |
| 8645 | } |
| 8646 | } |
| 8647 | app.pubProviders.clear(); |
| 8648 | } |
| 8649 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 8650 | // Take care of any launching providers waiting for this process. |
| 8651 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 8652 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8653 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 8654 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8655 | // Unregister from connected content providers. |
| 8656 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 8657 | Iterator it = app.conProviders.keySet().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8658 | while (it.hasNext()) { |
| 8659 | ContentProviderRecord cpr = (ContentProviderRecord)it.next(); |
| 8660 | cpr.clients.remove(app); |
| 8661 | } |
| 8662 | app.conProviders.clear(); |
| 8663 | } |
| 8664 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 8665 | // At this point there may be remaining entries in mLaunchingProviders |
| 8666 | // where we were the only one waiting, so they are no longer of use. |
| 8667 | // Look for these and clean up if found. |
| 8668 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 8669 | // the actual situation to identify what is actually going on. |
| 8670 | if (false) { |
| 8671 | for (int i=0; i<NL; i++) { |
| 8672 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 8673 | mLaunchingProviders.get(i); |
| 8674 | if (cpr.clients.size() <= 0 && cpr.externals <= 0) { |
| 8675 | synchronized (cpr) { |
| 8676 | cpr.launchingApp = null; |
| 8677 | cpr.notifyAll(); |
| 8678 | } |
| 8679 | } |
| 8680 | } |
| 8681 | } |
| 8682 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8683 | skipCurrentReceiverLocked(app); |
| 8684 | |
| 8685 | // Unregister any receivers. |
| 8686 | if (app.receivers.size() > 0) { |
| 8687 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 8688 | while (it.hasNext()) { |
| 8689 | removeReceiverLocked(it.next()); |
| 8690 | } |
| 8691 | app.receivers.clear(); |
| 8692 | } |
| 8693 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 8694 | // If the app is undergoing backup, tell the backup manager about it |
| 8695 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8696 | if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 8697 | try { |
| 8698 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 8699 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 8700 | bm.agentDisconnected(app.info.packageName); |
| 8701 | } catch (RemoteException e) { |
| 8702 | // can't happen; backup manager is local |
| 8703 | } |
| 8704 | } |
| 8705 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8706 | // If the caller is restarting this app, then leave it in its |
| 8707 | // current lists and let the caller take care of it. |
| 8708 | if (restarting) { |
| 8709 | return; |
| 8710 | } |
| 8711 | |
| 8712 | if (!app.persistent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8713 | if (DEBUG_PROCESSES) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8714 | "Removing non-persistent process during cleanup: " + app); |
| 8715 | mProcessNames.remove(app.processName, app.info.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8716 | if (mHeavyWeightProcess == app) { |
| 8717 | mHeavyWeightProcess = null; |
| 8718 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 8719 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8720 | } else if (!app.removed) { |
| 8721 | // This app is persistent, so we need to keep its record around. |
| 8722 | // If it is not already on the pending app list, add it there |
| 8723 | // and start a new process for it. |
| 8724 | app.thread = null; |
| 8725 | app.forcingToForeground = null; |
| 8726 | app.foregroundServices = false; |
| 8727 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 8728 | mPersistentStartingProcesses.add(app); |
| 8729 | restart = true; |
| 8730 | } |
| 8731 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 8732 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 8733 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8734 | mProcessesOnHold.remove(app); |
| 8735 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 8736 | if (app == mHomeProcess) { |
| 8737 | mHomeProcess = null; |
| 8738 | } |
| 8739 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8740 | if (restart) { |
| 8741 | // We have components that still need to be running in the |
| 8742 | // process, so re-launch it. |
| 8743 | mProcessNames.put(app.processName, app.info.uid, app); |
| 8744 | startProcessLocked(app, "restart", app.processName); |
| 8745 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 8746 | // Goodbye! |
| 8747 | synchronized (mPidsSelfLocked) { |
| 8748 | mPidsSelfLocked.remove(app.pid); |
| 8749 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 8750 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 8751 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8752 | } |
| 8753 | } |
| 8754 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 8755 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 8756 | // Look through the content providers we are waiting to have launched, |
| 8757 | // and if any run in this process then either schedule a restart of |
| 8758 | // the process or kill the client waiting for it if this process has |
| 8759 | // gone bad. |
| 8760 | int NL = mLaunchingProviders.size(); |
| 8761 | boolean restart = false; |
| 8762 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8763 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 8764 | if (cpr.launchingApp == app) { |
| 8765 | if (!alwaysBad && !app.bad) { |
| 8766 | restart = true; |
| 8767 | } else { |
| 8768 | removeDyingProviderLocked(app, cpr); |
| 8769 | NL = mLaunchingProviders.size(); |
| 8770 | } |
| 8771 | } |
| 8772 | } |
| 8773 | return restart; |
| 8774 | } |
| 8775 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8776 | // ========================================================= |
| 8777 | // SERVICES |
| 8778 | // ========================================================= |
| 8779 | |
| 8780 | ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) { |
| 8781 | ActivityManager.RunningServiceInfo info = |
| 8782 | new ActivityManager.RunningServiceInfo(); |
| 8783 | info.service = r.name; |
| 8784 | if (r.app != null) { |
| 8785 | info.pid = r.app.pid; |
| 8786 | } |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 8787 | info.uid = r.appInfo.uid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8788 | info.process = r.processName; |
| 8789 | info.foreground = r.isForeground; |
| 8790 | info.activeSince = r.createTime; |
| 8791 | info.started = r.startRequested; |
| 8792 | info.clientCount = r.connections.size(); |
| 8793 | info.crashCount = r.crashCount; |
| 8794 | info.lastActivityTime = r.lastActivity; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 8795 | if (r.isForeground) { |
| 8796 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND; |
| 8797 | } |
| 8798 | if (r.startRequested) { |
| 8799 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED; |
| 8800 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8801 | if (r.app != null && r.app.pid == MY_PID) { |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 8802 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS; |
| 8803 | } |
| 8804 | if (r.app != null && r.app.persistent) { |
| 8805 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS; |
| 8806 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8807 | |
| 8808 | for (ArrayList<ConnectionRecord> connl : r.connections.values()) { |
| 8809 | for (int i=0; i<connl.size(); i++) { |
| 8810 | ConnectionRecord conn = connl.get(i); |
| 8811 | if (conn.clientLabel != 0) { |
| 8812 | info.clientPackage = conn.binding.client.info.packageName; |
| 8813 | info.clientLabel = conn.clientLabel; |
| 8814 | return info; |
| 8815 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8816 | } |
| 8817 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8818 | return info; |
| 8819 | } |
| 8820 | |
| 8821 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 8822 | int flags) { |
| 8823 | synchronized (this) { |
| 8824 | ArrayList<ActivityManager.RunningServiceInfo> res |
| 8825 | = new ArrayList<ActivityManager.RunningServiceInfo>(); |
| 8826 | |
| 8827 | if (mServices.size() > 0) { |
| 8828 | Iterator<ServiceRecord> it = mServices.values().iterator(); |
| 8829 | while (it.hasNext() && res.size() < maxNum) { |
| 8830 | res.add(makeRunningServiceInfoLocked(it.next())); |
| 8831 | } |
| 8832 | } |
| 8833 | |
| 8834 | for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { |
| 8835 | ServiceRecord r = mRestartingServices.get(i); |
| 8836 | ActivityManager.RunningServiceInfo info = |
| 8837 | makeRunningServiceInfoLocked(r); |
| 8838 | info.restarting = r.nextRestartTime; |
| 8839 | res.add(info); |
| 8840 | } |
| 8841 | |
| 8842 | return res; |
| 8843 | } |
| 8844 | } |
| 8845 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8846 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
| 8847 | synchronized (this) { |
| 8848 | ServiceRecord r = mServices.get(name); |
| 8849 | if (r != null) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 8850 | for (ArrayList<ConnectionRecord> conn : r.connections.values()) { |
| 8851 | for (int i=0; i<conn.size(); i++) { |
| 8852 | if (conn.get(i).clientIntent != null) { |
| 8853 | return conn.get(i).clientIntent; |
| 8854 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8855 | } |
| 8856 | } |
| 8857 | } |
| 8858 | } |
| 8859 | return null; |
| 8860 | } |
| 8861 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8862 | private final ServiceRecord findServiceLocked(ComponentName name, |
| 8863 | IBinder token) { |
| 8864 | ServiceRecord r = mServices.get(name); |
| 8865 | return r == token ? r : null; |
| 8866 | } |
| 8867 | |
| 8868 | private final class ServiceLookupResult { |
| 8869 | final ServiceRecord record; |
| 8870 | final String permission; |
| 8871 | |
| 8872 | ServiceLookupResult(ServiceRecord _record, String _permission) { |
| 8873 | record = _record; |
| 8874 | permission = _permission; |
| 8875 | } |
| 8876 | }; |
| 8877 | |
| 8878 | private ServiceLookupResult findServiceLocked(Intent service, |
| 8879 | String resolvedType) { |
| 8880 | ServiceRecord r = null; |
| 8881 | if (service.getComponent() != null) { |
| 8882 | r = mServices.get(service.getComponent()); |
| 8883 | } |
| 8884 | if (r == null) { |
| 8885 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
| 8886 | r = mServicesByIntent.get(filter); |
| 8887 | } |
| 8888 | |
| 8889 | if (r == null) { |
| 8890 | try { |
| 8891 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8892 | AppGlobals.getPackageManager().resolveService( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8893 | service, resolvedType, 0); |
| 8894 | ServiceInfo sInfo = |
| 8895 | rInfo != null ? rInfo.serviceInfo : null; |
| 8896 | if (sInfo == null) { |
| 8897 | return null; |
| 8898 | } |
| 8899 | |
| 8900 | ComponentName name = new ComponentName( |
| 8901 | sInfo.applicationInfo.packageName, sInfo.name); |
| 8902 | r = mServices.get(name); |
| 8903 | } catch (RemoteException ex) { |
| 8904 | // pm is in same process, this will never happen. |
| 8905 | } |
| 8906 | } |
| 8907 | if (r != null) { |
| 8908 | int callingPid = Binder.getCallingPid(); |
| 8909 | int callingUid = Binder.getCallingUid(); |
| 8910 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 8911 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8912 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 8913 | if (!r.exported) { |
| 8914 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 8915 | + " from pid=" + callingPid |
| 8916 | + ", uid=" + callingUid |
| 8917 | + " that is not exported from uid " + r.appInfo.uid); |
| 8918 | return new ServiceLookupResult(null, "not exported from uid " |
| 8919 | + r.appInfo.uid); |
| 8920 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8921 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8922 | + " from pid=" + callingPid |
| 8923 | + ", uid=" + callingUid |
| 8924 | + " requires " + r.permission); |
| 8925 | return new ServiceLookupResult(null, r.permission); |
| 8926 | } |
| 8927 | return new ServiceLookupResult(r, null); |
| 8928 | } |
| 8929 | return null; |
| 8930 | } |
| 8931 | |
| 8932 | private class ServiceRestarter implements Runnable { |
| 8933 | private ServiceRecord mService; |
| 8934 | |
| 8935 | void setService(ServiceRecord service) { |
| 8936 | mService = service; |
| 8937 | } |
| 8938 | |
| 8939 | public void run() { |
| 8940 | synchronized(ActivityManagerService.this) { |
| 8941 | performServiceRestartLocked(mService); |
| 8942 | } |
| 8943 | } |
| 8944 | } |
| 8945 | |
| 8946 | private ServiceLookupResult retrieveServiceLocked(Intent service, |
| 8947 | String resolvedType, int callingPid, int callingUid) { |
| 8948 | ServiceRecord r = null; |
| 8949 | if (service.getComponent() != null) { |
| 8950 | r = mServices.get(service.getComponent()); |
| 8951 | } |
| 8952 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
| 8953 | r = mServicesByIntent.get(filter); |
| 8954 | if (r == null) { |
| 8955 | try { |
| 8956 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8957 | AppGlobals.getPackageManager().resolveService( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 8958 | service, resolvedType, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8959 | ServiceInfo sInfo = |
| 8960 | rInfo != null ? rInfo.serviceInfo : null; |
| 8961 | if (sInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8962 | Slog.w(TAG, "Unable to start service " + service + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8963 | ": not found"); |
| 8964 | return null; |
| 8965 | } |
| 8966 | |
| 8967 | ComponentName name = new ComponentName( |
| 8968 | sInfo.applicationInfo.packageName, sInfo.name); |
| 8969 | r = mServices.get(name); |
| 8970 | if (r == null) { |
| 8971 | filter = new Intent.FilterComparison(service.cloneFilter()); |
| 8972 | ServiceRestarter res = new ServiceRestarter(); |
| 8973 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 8974 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 8975 | synchronized (stats) { |
| 8976 | ss = stats.getServiceStatsLocked( |
| 8977 | sInfo.applicationInfo.uid, sInfo.packageName, |
| 8978 | sInfo.name); |
| 8979 | } |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 8980 | r = new ServiceRecord(this, ss, name, filter, sInfo, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8981 | res.setService(r); |
| 8982 | mServices.put(name, r); |
| 8983 | mServicesByIntent.put(filter, r); |
| 8984 | |
| 8985 | // Make sure this component isn't in the pending list. |
| 8986 | int N = mPendingServices.size(); |
| 8987 | for (int i=0; i<N; i++) { |
| 8988 | ServiceRecord pr = mPendingServices.get(i); |
| 8989 | if (pr.name.equals(name)) { |
| 8990 | mPendingServices.remove(i); |
| 8991 | i--; |
| 8992 | N--; |
| 8993 | } |
| 8994 | } |
| 8995 | } |
| 8996 | } catch (RemoteException ex) { |
| 8997 | // pm is in same process, this will never happen. |
| 8998 | } |
| 8999 | } |
| 9000 | if (r != null) { |
| 9001 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 9002 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9003 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 9004 | if (!r.exported) { |
| 9005 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 9006 | + " from pid=" + callingPid |
| 9007 | + ", uid=" + callingUid |
| 9008 | + " that is not exported from uid " + r.appInfo.uid); |
| 9009 | return new ServiceLookupResult(null, "not exported from uid " |
| 9010 | + r.appInfo.uid); |
| 9011 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9012 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 9013 | + " from pid=" + callingPid |
| 9014 | + ", uid=" + callingUid |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9015 | + " requires " + r.permission); |
| 9016 | return new ServiceLookupResult(null, r.permission); |
| 9017 | } |
| 9018 | return new ServiceLookupResult(r, null); |
| 9019 | } |
| 9020 | return null; |
| 9021 | } |
| 9022 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9023 | private final void bumpServiceExecutingLocked(ServiceRecord r, String why) { |
| 9024 | if (DEBUG_SERVICE) Log.v(TAG, ">>> EXECUTING " |
| 9025 | + why + " of " + r + " in app " + r.app); |
| 9026 | else if (DEBUG_SERVICE_EXECUTING) Log.v(TAG, ">>> EXECUTING " |
| 9027 | + why + " of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9028 | long now = SystemClock.uptimeMillis(); |
| 9029 | if (r.executeNesting == 0 && r.app != null) { |
| 9030 | if (r.app.executingServices.size() == 0) { |
| 9031 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 9032 | msg.obj = r.app; |
| 9033 | mHandler.sendMessageAtTime(msg, now+SERVICE_TIMEOUT); |
| 9034 | } |
| 9035 | r.app.executingServices.add(r); |
| 9036 | } |
| 9037 | r.executeNesting++; |
| 9038 | r.executingStart = now; |
| 9039 | } |
| 9040 | |
| 9041 | private final void sendServiceArgsLocked(ServiceRecord r, |
| 9042 | boolean oomAdjusted) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9043 | final int N = r.pendingStarts.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9044 | if (N == 0) { |
| 9045 | return; |
| 9046 | } |
| 9047 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9048 | while (r.pendingStarts.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9049 | try { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9050 | ServiceRecord.StartItem si = r.pendingStarts.remove(0); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9051 | if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to: " |
| 9052 | + r + " " + r.intent + " args=" + si.intent); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9053 | if (si.intent == null) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9054 | // If somehow we got a dummy start at the front, then |
| 9055 | // just drop it here. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9056 | continue; |
| 9057 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9058 | si.deliveredTime = SystemClock.uptimeMillis(); |
| 9059 | r.deliveredStarts.add(si); |
| 9060 | si.deliveryCount++; |
| 9061 | if (si.targetPermissionUid >= 0) { |
| 9062 | grantUriPermissionUncheckedFromIntentLocked(si.targetPermissionUid, |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 9063 | r.packageName, si.intent, si.getUriPermissionsLocked()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9064 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9065 | bumpServiceExecutingLocked(r, "start"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9066 | if (!oomAdjusted) { |
| 9067 | oomAdjusted = true; |
| 9068 | updateOomAdjLocked(r.app); |
| 9069 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9070 | int flags = 0; |
| 9071 | if (si.deliveryCount > 0) { |
| 9072 | flags |= Service.START_FLAG_RETRY; |
| 9073 | } |
| 9074 | if (si.doneExecutingCount > 0) { |
| 9075 | flags |= Service.START_FLAG_REDELIVERY; |
| 9076 | } |
| 9077 | r.app.thread.scheduleServiceArgs(r, si.id, flags, si.intent); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9078 | } catch (RemoteException e) { |
| 9079 | // Remote process gone... we'll let the normal cleanup take |
| 9080 | // care of this. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9081 | if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while scheduling start: " + r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9082 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9083 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9084 | Slog.w(TAG, "Unexpected exception", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9085 | break; |
| 9086 | } |
| 9087 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9088 | } |
| 9089 | |
| 9090 | private final boolean requestServiceBindingLocked(ServiceRecord r, |
| 9091 | IntentBindRecord i, boolean rebind) { |
| 9092 | if (r.app == null || r.app.thread == null) { |
| 9093 | // If service is not currently running, can't yet bind. |
| 9094 | return false; |
| 9095 | } |
| 9096 | if ((!i.requested || rebind) && i.apps.size() > 0) { |
| 9097 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9098 | bumpServiceExecutingLocked(r, "bind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9099 | r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind); |
| 9100 | if (!rebind) { |
| 9101 | i.requested = true; |
| 9102 | } |
| 9103 | i.hasBound = true; |
| 9104 | i.doRebind = false; |
| 9105 | } catch (RemoteException e) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9106 | 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] | 9107 | return false; |
| 9108 | } |
| 9109 | } |
| 9110 | return true; |
| 9111 | } |
| 9112 | |
| 9113 | private final void requestServiceBindingsLocked(ServiceRecord r) { |
| 9114 | Iterator<IntentBindRecord> bindings = r.bindings.values().iterator(); |
| 9115 | while (bindings.hasNext()) { |
| 9116 | IntentBindRecord i = bindings.next(); |
| 9117 | if (!requestServiceBindingLocked(r, i, false)) { |
| 9118 | break; |
| 9119 | } |
| 9120 | } |
| 9121 | } |
| 9122 | |
| 9123 | private final void realStartServiceLocked(ServiceRecord r, |
| 9124 | ProcessRecord app) throws RemoteException { |
| 9125 | if (app.thread == null) { |
| 9126 | throw new RemoteException(); |
| 9127 | } |
| 9128 | |
| 9129 | r.app = app; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 9130 | r.restartTime = r.lastActivity = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9131 | |
| 9132 | app.services.add(r); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9133 | bumpServiceExecutingLocked(r, "create"); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 9134 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9135 | |
| 9136 | boolean created = false; |
| 9137 | try { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 9138 | mStringBuilder.setLength(0); |
| 9139 | r.intent.getIntent().toShortString(mStringBuilder, false, true); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 9140 | EventLog.writeEvent(EventLogTags.AM_CREATE_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9141 | System.identityHashCode(r), r.shortName, |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 9142 | mStringBuilder.toString(), r.app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9143 | synchronized (r.stats.getBatteryStats()) { |
| 9144 | r.stats.startLaunchedLocked(); |
| 9145 | } |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 9146 | ensurePackageDexOpt(r.serviceInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9147 | app.thread.scheduleCreateService(r, r.serviceInfo); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 9148 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9149 | created = true; |
| 9150 | } finally { |
| 9151 | if (!created) { |
| 9152 | app.services.remove(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9153 | scheduleServiceRestartLocked(r, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9154 | } |
| 9155 | } |
| 9156 | |
| 9157 | requestServiceBindingsLocked(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9158 | |
| 9159 | // If the service is in the started state, and there are no |
| 9160 | // pending arguments, then fake up one so its onStartCommand() will |
| 9161 | // be called. |
| 9162 | if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) { |
| 9163 | r.lastStartId++; |
| 9164 | if (r.lastStartId < 1) { |
| 9165 | r.lastStartId = 1; |
| 9166 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9167 | r.pendingStarts.add(new ServiceRecord.StartItem(r, r.lastStartId, null, -1)); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9168 | } |
| 9169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9170 | sendServiceArgsLocked(r, true); |
| 9171 | } |
| 9172 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9173 | private final boolean scheduleServiceRestartLocked(ServiceRecord r, |
| 9174 | boolean allowCancel) { |
| 9175 | boolean canceled = false; |
| 9176 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9177 | final long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9178 | long minDuration = SERVICE_RESTART_DURATION; |
Dianne Hackborn | 6ccd2af | 2009-08-27 12:26:44 -0700 | [diff] [blame] | 9179 | long resetTime = SERVICE_RESET_RUN_DURATION; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9180 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 9181 | if ((r.serviceInfo.applicationInfo.flags |
| 9182 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 9183 | minDuration /= 4; |
| 9184 | } |
| 9185 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9186 | // Any delivered but not yet finished starts should be put back |
| 9187 | // on the pending list. |
| 9188 | final int N = r.deliveredStarts.size(); |
| 9189 | if (N > 0) { |
| 9190 | for (int i=N-1; i>=0; i--) { |
| 9191 | ServiceRecord.StartItem si = r.deliveredStarts.get(i); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9192 | si.removeUriPermissionsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9193 | if (si.intent == null) { |
| 9194 | // We'll generate this again if needed. |
| 9195 | } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT |
| 9196 | && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) { |
| 9197 | r.pendingStarts.add(0, si); |
| 9198 | long dur = SystemClock.uptimeMillis() - si.deliveredTime; |
| 9199 | dur *= 2; |
| 9200 | if (minDuration < dur) minDuration = dur; |
| 9201 | if (resetTime < dur) resetTime = dur; |
| 9202 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9203 | Slog.w(TAG, "Canceling start item " + si.intent + " in service " |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9204 | + r.name); |
| 9205 | canceled = true; |
| 9206 | } |
| 9207 | } |
| 9208 | r.deliveredStarts.clear(); |
| 9209 | } |
| 9210 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9211 | r.totalRestartCount++; |
| 9212 | if (r.restartDelay == 0) { |
| 9213 | r.restartCount++; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9214 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9215 | } else { |
| 9216 | // If it has been a "reasonably long time" since the service |
| 9217 | // was started, then reset our restart duration back to |
| 9218 | // the beginning, so we don't infinitely increase the duration |
| 9219 | // on a service that just occasionally gets killed (which is |
| 9220 | // a normal case, due to process being killed to reclaim memory). |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9221 | if (now > (r.restartTime+resetTime)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9222 | r.restartCount = 1; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9223 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9224 | } else { |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 9225 | if ((r.serviceInfo.applicationInfo.flags |
| 9226 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 9227 | // Services in peristent processes will restart much more |
| 9228 | // quickly, since they are pretty important. (Think SystemUI). |
| 9229 | r.restartDelay += minDuration/2; |
| 9230 | } else { |
| 9231 | r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR; |
| 9232 | if (r.restartDelay < minDuration) { |
| 9233 | r.restartDelay = minDuration; |
| 9234 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9235 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9236 | } |
| 9237 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9238 | |
| 9239 | r.nextRestartTime = now + r.restartDelay; |
| 9240 | |
| 9241 | // Make sure that we don't end up restarting a bunch of services |
| 9242 | // all at the same time. |
| 9243 | boolean repeat; |
| 9244 | do { |
| 9245 | repeat = false; |
| 9246 | for (int i=mRestartingServices.size()-1; i>=0; i--) { |
| 9247 | ServiceRecord r2 = mRestartingServices.get(i); |
| 9248 | if (r2 != r && r.nextRestartTime |
| 9249 | >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN) |
| 9250 | && r.nextRestartTime |
| 9251 | < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) { |
| 9252 | r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN; |
| 9253 | r.restartDelay = r.nextRestartTime - now; |
| 9254 | repeat = true; |
| 9255 | break; |
| 9256 | } |
| 9257 | } |
| 9258 | } while (repeat); |
| 9259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9260 | if (!mRestartingServices.contains(r)) { |
| 9261 | mRestartingServices.add(r); |
| 9262 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9263 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 9264 | r.cancelNotification(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9265 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9266 | mHandler.removeCallbacks(r.restarter); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9267 | mHandler.postAtTime(r.restarter, r.nextRestartTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9268 | r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9269 | Slog.w(TAG, "Scheduling restart of crashed service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9270 | + r.shortName + " in " + r.restartDelay + "ms"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 9271 | EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9272 | r.shortName, r.restartDelay); |
| 9273 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9274 | return canceled; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9275 | } |
| 9276 | |
| 9277 | final void performServiceRestartLocked(ServiceRecord r) { |
| 9278 | if (!mRestartingServices.contains(r)) { |
| 9279 | return; |
| 9280 | } |
| 9281 | bringUpServiceLocked(r, r.intent.getIntent().getFlags(), true); |
| 9282 | } |
| 9283 | |
| 9284 | private final boolean unscheduleServiceRestartLocked(ServiceRecord r) { |
| 9285 | if (r.restartDelay == 0) { |
| 9286 | return false; |
| 9287 | } |
| 9288 | r.resetRestartCounter(); |
| 9289 | mRestartingServices.remove(r); |
| 9290 | mHandler.removeCallbacks(r.restarter); |
| 9291 | return true; |
| 9292 | } |
| 9293 | |
| 9294 | private final boolean bringUpServiceLocked(ServiceRecord r, |
| 9295 | int intentFlags, boolean whileRestarting) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9296 | //Slog.i(TAG, "Bring up service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9297 | //r.dump(" "); |
| 9298 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 9299 | if (r.app != null && r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9300 | sendServiceArgsLocked(r, false); |
| 9301 | return true; |
| 9302 | } |
| 9303 | |
| 9304 | if (!whileRestarting && r.restartDelay > 0) { |
| 9305 | // If waiting for a restart, then do nothing. |
| 9306 | return true; |
| 9307 | } |
| 9308 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9309 | 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] | 9310 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 9311 | // We are now bringing the service up, so no longer in the |
| 9312 | // restarting state. |
| 9313 | mRestartingServices.remove(r); |
| 9314 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9315 | final String appName = r.processName; |
| 9316 | ProcessRecord app = getProcessRecordLocked(appName, r.appInfo.uid); |
| 9317 | if (app != null && app.thread != null) { |
| 9318 | try { |
| 9319 | realStartServiceLocked(r, app); |
| 9320 | return true; |
| 9321 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9322 | 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] | 9323 | } |
| 9324 | |
| 9325 | // If a dead object exception was thrown -- fall through to |
| 9326 | // restart the application. |
| 9327 | } |
| 9328 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 9329 | // Not running -- get it started, and enqueue this service record |
| 9330 | // to be executed when the app comes up. |
| 9331 | if (startProcessLocked(appName, r.appInfo, true, intentFlags, |
| 9332 | "service", r.name, false) == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9333 | Slog.w(TAG, "Unable to launch app " |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 9334 | + r.appInfo.packageName + "/" |
| 9335 | + r.appInfo.uid + " for service " |
| 9336 | + r.intent.getIntent() + ": process is bad"); |
| 9337 | bringDownServiceLocked(r, true); |
| 9338 | return false; |
| 9339 | } |
| 9340 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9341 | if (!mPendingServices.contains(r)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9342 | mPendingServices.add(r); |
| 9343 | } |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 9344 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9345 | return true; |
| 9346 | } |
| 9347 | |
| 9348 | private final void bringDownServiceLocked(ServiceRecord r, boolean force) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9349 | //Slog.i(TAG, "Bring down service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9350 | //r.dump(" "); |
| 9351 | |
| 9352 | // Does it still need to run? |
| 9353 | if (!force && r.startRequested) { |
| 9354 | return; |
| 9355 | } |
| 9356 | if (r.connections.size() > 0) { |
| 9357 | if (!force) { |
| 9358 | // XXX should probably keep a count of the number of auto-create |
| 9359 | // connections directly in the service. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9360 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9361 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9362 | ArrayList<ConnectionRecord> cr = it.next(); |
| 9363 | for (int i=0; i<cr.size(); i++) { |
| 9364 | if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) { |
| 9365 | return; |
| 9366 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9367 | } |
| 9368 | } |
| 9369 | } |
| 9370 | |
| 9371 | // Report to all of the connections that the service is no longer |
| 9372 | // available. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9373 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9374 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9375 | ArrayList<ConnectionRecord> c = it.next(); |
| 9376 | for (int i=0; i<c.size(); i++) { |
| 9377 | try { |
| 9378 | c.get(i).conn.connected(r.name, null); |
| 9379 | } catch (Exception e) { |
| 9380 | Slog.w(TAG, "Failure disconnecting service " + r.name + |
| 9381 | " to connection " + c.get(i).conn.asBinder() + |
| 9382 | " (in " + c.get(i).binding.client.processName + ")", e); |
| 9383 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9384 | } |
| 9385 | } |
| 9386 | } |
| 9387 | |
| 9388 | // Tell the service that it has been unbound. |
| 9389 | if (r.bindings.size() > 0 && r.app != null && r.app.thread != null) { |
| 9390 | Iterator<IntentBindRecord> it = r.bindings.values().iterator(); |
| 9391 | while (it.hasNext()) { |
| 9392 | IntentBindRecord ibr = it.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9393 | 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] | 9394 | + ": hasBound=" + ibr.hasBound); |
| 9395 | if (r.app != null && r.app.thread != null && ibr.hasBound) { |
| 9396 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9397 | bumpServiceExecutingLocked(r, "bring down unbind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9398 | updateOomAdjLocked(r.app); |
| 9399 | ibr.hasBound = false; |
| 9400 | r.app.thread.scheduleUnbindService(r, |
| 9401 | ibr.intent.getIntent()); |
| 9402 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9403 | Slog.w(TAG, "Exception when unbinding service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9404 | + r.shortName, e); |
| 9405 | serviceDoneExecutingLocked(r, true); |
| 9406 | } |
| 9407 | } |
| 9408 | } |
| 9409 | } |
| 9410 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9411 | if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down " + r + " " + r.intent); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 9412 | EventLog.writeEvent(EventLogTags.AM_DESTROY_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9413 | System.identityHashCode(r), r.shortName, |
| 9414 | (r.app != null) ? r.app.pid : -1); |
| 9415 | |
| 9416 | mServices.remove(r.name); |
| 9417 | mServicesByIntent.remove(r.intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9418 | r.totalRestartCount = 0; |
| 9419 | unscheduleServiceRestartLocked(r); |
| 9420 | |
| 9421 | // Also make sure it is not on the pending list. |
| 9422 | int N = mPendingServices.size(); |
| 9423 | for (int i=0; i<N; i++) { |
| 9424 | if (mPendingServices.get(i) == r) { |
| 9425 | mPendingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9426 | if (DEBUG_SERVICE) Slog.v(TAG, "Removed pending: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9427 | i--; |
| 9428 | N--; |
| 9429 | } |
| 9430 | } |
| 9431 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 9432 | r.cancelNotification(); |
| 9433 | r.isForeground = false; |
| 9434 | r.foregroundId = 0; |
| 9435 | r.foregroundNoti = null; |
| 9436 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9437 | // Clear start entries. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9438 | r.clearDeliveredStartsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9439 | r.pendingStarts.clear(); |
| 9440 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9441 | if (r.app != null) { |
| 9442 | synchronized (r.stats.getBatteryStats()) { |
| 9443 | r.stats.stopLaunchedLocked(); |
| 9444 | } |
| 9445 | r.app.services.remove(r); |
| 9446 | if (r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9447 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9448 | bumpServiceExecutingLocked(r, "stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9449 | mStoppingServices.add(r); |
| 9450 | updateOomAdjLocked(r.app); |
| 9451 | r.app.thread.scheduleStopService(r); |
| 9452 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9453 | Slog.w(TAG, "Exception when stopping service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9454 | + r.shortName, e); |
| 9455 | serviceDoneExecutingLocked(r, true); |
| 9456 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 9457 | updateServiceForegroundLocked(r.app, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9458 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9459 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9460 | TAG, "Removed service that has no process: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9461 | } |
| 9462 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9463 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9464 | TAG, "Removed service that is not running: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9465 | } |
Vairavan Srinivasan | a207ce2 | 2010-12-23 13:51:48 -0800 | [diff] [blame] | 9466 | |
| 9467 | if (r.bindings.size() > 0) { |
| 9468 | r.bindings.clear(); |
| 9469 | } |
| 9470 | |
| 9471 | if (r.restarter instanceof ServiceRestarter) { |
| 9472 | ((ServiceRestarter)r.restarter).setService(null); |
| 9473 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9474 | } |
| 9475 | |
| 9476 | ComponentName startServiceLocked(IApplicationThread caller, |
| 9477 | Intent service, String resolvedType, |
| 9478 | int callingPid, int callingUid) { |
| 9479 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9480 | if (DEBUG_SERVICE) Slog.v(TAG, "startService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9481 | + " type=" + resolvedType + " args=" + service.getExtras()); |
| 9482 | |
| 9483 | if (caller != null) { |
| 9484 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 9485 | if (callerApp == null) { |
| 9486 | throw new SecurityException( |
| 9487 | "Unable to find app for caller " + caller |
| 9488 | + " (pid=" + Binder.getCallingPid() |
| 9489 | + ") when starting service " + service); |
| 9490 | } |
| 9491 | } |
| 9492 | |
| 9493 | ServiceLookupResult res = |
| 9494 | retrieveServiceLocked(service, resolvedType, |
| 9495 | callingPid, callingUid); |
| 9496 | if (res == null) { |
| 9497 | return null; |
| 9498 | } |
| 9499 | if (res.record == null) { |
| 9500 | return new ComponentName("!", res.permission != null |
| 9501 | ? res.permission : "private to package"); |
| 9502 | } |
| 9503 | ServiceRecord r = res.record; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9504 | int targetPermissionUid = checkGrantUriPermissionFromIntentLocked( |
| 9505 | callingUid, r.packageName, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9506 | if (unscheduleServiceRestartLocked(r)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9507 | 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] | 9508 | } |
| 9509 | r.startRequested = true; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9510 | r.callStart = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9511 | r.lastStartId++; |
| 9512 | if (r.lastStartId < 1) { |
| 9513 | r.lastStartId = 1; |
| 9514 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9515 | r.pendingStarts.add(new ServiceRecord.StartItem(r, r.lastStartId, |
| 9516 | service, targetPermissionUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9517 | r.lastActivity = SystemClock.uptimeMillis(); |
| 9518 | synchronized (r.stats.getBatteryStats()) { |
| 9519 | r.stats.startRunningLocked(); |
| 9520 | } |
| 9521 | if (!bringUpServiceLocked(r, service.getFlags(), false)) { |
| 9522 | return new ComponentName("!", "Service process is bad"); |
| 9523 | } |
| 9524 | return r.name; |
| 9525 | } |
| 9526 | } |
| 9527 | |
| 9528 | public ComponentName startService(IApplicationThread caller, Intent service, |
| 9529 | String resolvedType) { |
| 9530 | // Refuse possible leaked file descriptors |
| 9531 | if (service != null && service.hasFileDescriptors() == true) { |
| 9532 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 9533 | } |
| 9534 | |
| 9535 | synchronized(this) { |
| 9536 | final int callingPid = Binder.getCallingPid(); |
| 9537 | final int callingUid = Binder.getCallingUid(); |
| 9538 | final long origId = Binder.clearCallingIdentity(); |
| 9539 | ComponentName res = startServiceLocked(caller, service, |
| 9540 | resolvedType, callingPid, callingUid); |
| 9541 | Binder.restoreCallingIdentity(origId); |
| 9542 | return res; |
| 9543 | } |
| 9544 | } |
| 9545 | |
| 9546 | ComponentName startServiceInPackage(int uid, |
| 9547 | Intent service, String resolvedType) { |
| 9548 | synchronized(this) { |
| 9549 | final long origId = Binder.clearCallingIdentity(); |
| 9550 | ComponentName res = startServiceLocked(null, service, |
| 9551 | resolvedType, -1, uid); |
| 9552 | Binder.restoreCallingIdentity(origId); |
| 9553 | return res; |
| 9554 | } |
| 9555 | } |
| 9556 | |
| 9557 | public int stopService(IApplicationThread caller, Intent service, |
| 9558 | String resolvedType) { |
| 9559 | // Refuse possible leaked file descriptors |
| 9560 | if (service != null && service.hasFileDescriptors() == true) { |
| 9561 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 9562 | } |
| 9563 | |
| 9564 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9565 | if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9566 | + " type=" + resolvedType); |
| 9567 | |
| 9568 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 9569 | if (caller != null && callerApp == null) { |
| 9570 | throw new SecurityException( |
| 9571 | "Unable to find app for caller " + caller |
| 9572 | + " (pid=" + Binder.getCallingPid() |
| 9573 | + ") when stopping service " + service); |
| 9574 | } |
| 9575 | |
| 9576 | // If this service is active, make sure it is stopped. |
| 9577 | ServiceLookupResult r = findServiceLocked(service, resolvedType); |
| 9578 | if (r != null) { |
| 9579 | if (r.record != null) { |
| 9580 | synchronized (r.record.stats.getBatteryStats()) { |
| 9581 | r.record.stats.stopRunningLocked(); |
| 9582 | } |
| 9583 | r.record.startRequested = false; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9584 | r.record.callStart = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9585 | final long origId = Binder.clearCallingIdentity(); |
| 9586 | bringDownServiceLocked(r.record, false); |
| 9587 | Binder.restoreCallingIdentity(origId); |
| 9588 | return 1; |
| 9589 | } |
| 9590 | return -1; |
| 9591 | } |
| 9592 | } |
| 9593 | |
| 9594 | return 0; |
| 9595 | } |
| 9596 | |
| 9597 | public IBinder peekService(Intent service, String resolvedType) { |
| 9598 | // Refuse possible leaked file descriptors |
| 9599 | if (service != null && service.hasFileDescriptors() == true) { |
| 9600 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 9601 | } |
| 9602 | |
| 9603 | IBinder ret = null; |
| 9604 | |
| 9605 | synchronized(this) { |
| 9606 | ServiceLookupResult r = findServiceLocked(service, resolvedType); |
| 9607 | |
| 9608 | if (r != null) { |
| 9609 | // r.record is null if findServiceLocked() failed the caller permission check |
| 9610 | if (r.record == null) { |
| 9611 | throw new SecurityException( |
lge-aosp | ea278a9 | 2011-01-07 13:45:37 +0900 | [diff] [blame] | 9612 | "Permission Denial: Accessing service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9613 | + " from pid=" + Binder.getCallingPid() |
| 9614 | + ", uid=" + Binder.getCallingUid() |
| 9615 | + " requires " + r.permission); |
| 9616 | } |
| 9617 | IntentBindRecord ib = r.record.bindings.get(r.record.intent); |
| 9618 | if (ib != null) { |
| 9619 | ret = ib.binder; |
| 9620 | } |
| 9621 | } |
| 9622 | } |
| 9623 | |
| 9624 | return ret; |
| 9625 | } |
| 9626 | |
| 9627 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 9628 | int startId) { |
| 9629 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9630 | if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9631 | + " " + token + " startId=" + startId); |
| 9632 | ServiceRecord r = findServiceLocked(className, token); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9633 | if (r != null) { |
| 9634 | if (startId >= 0) { |
| 9635 | // Asked to only stop if done with all work. Note that |
| 9636 | // to avoid leaks, we will take this as dropping all |
| 9637 | // start items up to and including this one. |
| 9638 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 9639 | if (si != null) { |
| 9640 | while (r.deliveredStarts.size() > 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 9641 | ServiceRecord.StartItem cur = r.deliveredStarts.remove(0); |
| 9642 | cur.removeUriPermissionsLocked(); |
| 9643 | if (cur == si) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9644 | break; |
| 9645 | } |
| 9646 | } |
| 9647 | } |
| 9648 | |
| 9649 | if (r.lastStartId != startId) { |
| 9650 | return false; |
| 9651 | } |
| 9652 | |
| 9653 | if (r.deliveredStarts.size() > 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9654 | Slog.w(TAG, "stopServiceToken startId " + startId |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9655 | + " is last, but have " + r.deliveredStarts.size() |
| 9656 | + " remaining args"); |
| 9657 | } |
| 9658 | } |
| 9659 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9660 | synchronized (r.stats.getBatteryStats()) { |
| 9661 | r.stats.stopRunningLocked(); |
| 9662 | r.startRequested = false; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 9663 | r.callStart = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9664 | } |
| 9665 | final long origId = Binder.clearCallingIdentity(); |
| 9666 | bringDownServiceLocked(r, false); |
| 9667 | Binder.restoreCallingIdentity(origId); |
| 9668 | return true; |
| 9669 | } |
| 9670 | } |
| 9671 | return false; |
| 9672 | } |
| 9673 | |
| 9674 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 9675 | int id, Notification notification, boolean removeNotification) { |
| 9676 | final long origId = Binder.clearCallingIdentity(); |
| 9677 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9678 | synchronized(this) { |
| 9679 | ServiceRecord r = findServiceLocked(className, token); |
| 9680 | if (r != null) { |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 9681 | if (id != 0) { |
| 9682 | if (notification == null) { |
| 9683 | throw new IllegalArgumentException("null notification"); |
| 9684 | } |
| 9685 | if (r.foregroundId != id) { |
| 9686 | r.cancelNotification(); |
| 9687 | r.foregroundId = id; |
| 9688 | } |
| 9689 | notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 9690 | r.foregroundNoti = notification; |
| 9691 | r.isForeground = true; |
| 9692 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9693 | if (r.app != null) { |
| 9694 | updateServiceForegroundLocked(r.app, true); |
| 9695 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 9696 | } else { |
| 9697 | if (r.isForeground) { |
| 9698 | r.isForeground = false; |
| 9699 | if (r.app != null) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 9700 | updateLruProcessLocked(r.app, false, true); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 9701 | updateServiceForegroundLocked(r.app, true); |
| 9702 | } |
| 9703 | } |
| 9704 | if (removeNotification) { |
| 9705 | r.cancelNotification(); |
| 9706 | r.foregroundId = 0; |
| 9707 | r.foregroundNoti = null; |
| 9708 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9709 | } |
| 9710 | } |
| 9711 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 9712 | } finally { |
| 9713 | Binder.restoreCallingIdentity(origId); |
| 9714 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9715 | } |
| 9716 | |
| 9717 | public void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) { |
| 9718 | boolean anyForeground = false; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9719 | for (ServiceRecord sr : proc.services) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9720 | if (sr.isForeground) { |
| 9721 | anyForeground = true; |
| 9722 | break; |
| 9723 | } |
| 9724 | } |
| 9725 | if (anyForeground != proc.foregroundServices) { |
| 9726 | proc.foregroundServices = anyForeground; |
| 9727 | if (oomAdj) { |
| 9728 | updateOomAdjLocked(); |
| 9729 | } |
| 9730 | } |
| 9731 | } |
| 9732 | |
| 9733 | public int bindService(IApplicationThread caller, IBinder token, |
| 9734 | Intent service, String resolvedType, |
| 9735 | IServiceConnection connection, int flags) { |
| 9736 | // Refuse possible leaked file descriptors |
| 9737 | if (service != null && service.hasFileDescriptors() == true) { |
| 9738 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 9739 | } |
| 9740 | |
| 9741 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9742 | if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9743 | + " type=" + resolvedType + " conn=" + connection.asBinder() |
| 9744 | + " flags=0x" + Integer.toHexString(flags)); |
| 9745 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 9746 | if (callerApp == null) { |
| 9747 | throw new SecurityException( |
| 9748 | "Unable to find app for caller " + caller |
| 9749 | + " (pid=" + Binder.getCallingPid() |
| 9750 | + ") when binding service " + service); |
| 9751 | } |
| 9752 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 9753 | ActivityRecord activity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9754 | if (token != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9755 | int aindex = mMainStack.indexOfTokenLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9756 | if (aindex < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9757 | Slog.w(TAG, "Binding with unknown activity: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9758 | return 0; |
| 9759 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9760 | activity = (ActivityRecord)mMainStack.mHistory.get(aindex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9761 | } |
| 9762 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 9763 | int clientLabel = 0; |
| 9764 | PendingIntent clientIntent = null; |
| 9765 | |
| 9766 | if (callerApp.info.uid == Process.SYSTEM_UID) { |
| 9767 | // Hacky kind of thing -- allow system stuff to tell us |
| 9768 | // what they are, so we can report this elsewhere for |
| 9769 | // others to know why certain services are running. |
| 9770 | try { |
| 9771 | clientIntent = (PendingIntent)service.getParcelableExtra( |
| 9772 | Intent.EXTRA_CLIENT_INTENT); |
| 9773 | } catch (RuntimeException e) { |
| 9774 | } |
| 9775 | if (clientIntent != null) { |
| 9776 | clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0); |
| 9777 | if (clientLabel != 0) { |
| 9778 | // There are no useful extras in the intent, trash them. |
| 9779 | // System code calling with this stuff just needs to know |
| 9780 | // this will happen. |
| 9781 | service = service.cloneFilter(); |
| 9782 | } |
| 9783 | } |
| 9784 | } |
| 9785 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9786 | ServiceLookupResult res = |
| 9787 | retrieveServiceLocked(service, resolvedType, |
| 9788 | Binder.getCallingPid(), Binder.getCallingUid()); |
| 9789 | if (res == null) { |
| 9790 | return 0; |
| 9791 | } |
| 9792 | if (res.record == null) { |
| 9793 | return -1; |
| 9794 | } |
| 9795 | ServiceRecord s = res.record; |
| 9796 | |
| 9797 | final long origId = Binder.clearCallingIdentity(); |
| 9798 | |
| 9799 | if (unscheduleServiceRestartLocked(s)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9800 | if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: " |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9801 | + s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9802 | } |
| 9803 | |
| 9804 | AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp); |
| 9805 | ConnectionRecord c = new ConnectionRecord(b, activity, |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 9806 | connection, flags, clientLabel, clientIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9807 | |
| 9808 | IBinder binder = connection.asBinder(); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9809 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 9810 | if (clist == null) { |
| 9811 | clist = new ArrayList<ConnectionRecord>(); |
| 9812 | s.connections.put(binder, clist); |
| 9813 | } |
| 9814 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9815 | b.connections.add(c); |
| 9816 | if (activity != null) { |
| 9817 | if (activity.connections == null) { |
| 9818 | activity.connections = new HashSet<ConnectionRecord>(); |
| 9819 | } |
| 9820 | activity.connections.add(c); |
| 9821 | } |
| 9822 | b.client.connections.add(c); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9823 | clist = mServiceConnections.get(binder); |
| 9824 | if (clist == null) { |
| 9825 | clist = new ArrayList<ConnectionRecord>(); |
| 9826 | mServiceConnections.put(binder, clist); |
| 9827 | } |
| 9828 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9829 | |
| 9830 | if ((flags&Context.BIND_AUTO_CREATE) != 0) { |
| 9831 | s.lastActivity = SystemClock.uptimeMillis(); |
| 9832 | if (!bringUpServiceLocked(s, service.getFlags(), false)) { |
| 9833 | return 0; |
| 9834 | } |
| 9835 | } |
| 9836 | |
| 9837 | if (s.app != null) { |
| 9838 | // This could have made the service more important. |
| 9839 | updateOomAdjLocked(s.app); |
| 9840 | } |
| 9841 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9842 | 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] | 9843 | + ": received=" + b.intent.received |
| 9844 | + " apps=" + b.intent.apps.size() |
| 9845 | + " doRebind=" + b.intent.doRebind); |
| 9846 | |
| 9847 | if (s.app != null && b.intent.received) { |
| 9848 | // Service is already running, so we can immediately |
| 9849 | // publish the connection. |
| 9850 | try { |
| 9851 | c.conn.connected(s.name, b.intent.binder); |
| 9852 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9853 | Slog.w(TAG, "Failure sending service " + s.shortName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9854 | + " to connection " + c.conn.asBinder() |
| 9855 | + " (in " + c.binding.client.processName + ")", e); |
| 9856 | } |
| 9857 | |
| 9858 | // If this is the first app connected back to this binding, |
| 9859 | // and the service had previously asked to be told when |
| 9860 | // rebound, then do so. |
| 9861 | if (b.intent.apps.size() == 1 && b.intent.doRebind) { |
| 9862 | requestServiceBindingLocked(s, b.intent, true); |
| 9863 | } |
| 9864 | } else if (!b.intent.requested) { |
| 9865 | requestServiceBindingLocked(s, b.intent, false); |
| 9866 | } |
| 9867 | |
| 9868 | Binder.restoreCallingIdentity(origId); |
| 9869 | } |
| 9870 | |
| 9871 | return 1; |
| 9872 | } |
| 9873 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9874 | void removeConnectionLocked( |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 9875 | ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9876 | IBinder binder = c.conn.asBinder(); |
| 9877 | AppBindRecord b = c.binding; |
| 9878 | ServiceRecord s = b.service; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9879 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 9880 | if (clist != null) { |
| 9881 | clist.remove(c); |
| 9882 | if (clist.size() == 0) { |
| 9883 | s.connections.remove(binder); |
| 9884 | } |
| 9885 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9886 | b.connections.remove(c); |
| 9887 | if (c.activity != null && c.activity != skipAct) { |
| 9888 | if (c.activity.connections != null) { |
| 9889 | c.activity.connections.remove(c); |
| 9890 | } |
| 9891 | } |
| 9892 | if (b.client != skipApp) { |
| 9893 | b.client.connections.remove(c); |
| 9894 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9895 | clist = mServiceConnections.get(binder); |
| 9896 | if (clist != null) { |
| 9897 | clist.remove(c); |
| 9898 | if (clist.size() == 0) { |
| 9899 | mServiceConnections.remove(binder); |
| 9900 | } |
| 9901 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9902 | |
| 9903 | if (b.connections.size() == 0) { |
| 9904 | b.intent.apps.remove(b.client); |
| 9905 | } |
| 9906 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9907 | if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9908 | + ": shouldUnbind=" + b.intent.hasBound); |
| 9909 | if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0 |
| 9910 | && b.intent.hasBound) { |
| 9911 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9912 | bumpServiceExecutingLocked(s, "unbind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9913 | updateOomAdjLocked(s.app); |
| 9914 | b.intent.hasBound = false; |
| 9915 | // Assume the client doesn't want to know about a rebind; |
| 9916 | // we will deal with that later if it asks for one. |
| 9917 | b.intent.doRebind = false; |
| 9918 | s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent()); |
| 9919 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9920 | Slog.w(TAG, "Exception when unbinding service " + s.shortName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9921 | serviceDoneExecutingLocked(s, true); |
| 9922 | } |
| 9923 | } |
| 9924 | |
| 9925 | if ((c.flags&Context.BIND_AUTO_CREATE) != 0) { |
| 9926 | bringDownServiceLocked(s, false); |
| 9927 | } |
| 9928 | } |
| 9929 | |
| 9930 | public boolean unbindService(IServiceConnection connection) { |
| 9931 | synchronized (this) { |
| 9932 | IBinder binder = connection.asBinder(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9933 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9934 | ArrayList<ConnectionRecord> clist = mServiceConnections.get(binder); |
| 9935 | if (clist == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 9936 | 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] | 9937 | + connection.asBinder()); |
| 9938 | return false; |
| 9939 | } |
| 9940 | |
| 9941 | final long origId = Binder.clearCallingIdentity(); |
| 9942 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9943 | while (clist.size() > 0) { |
| 9944 | ConnectionRecord r = clist.get(0); |
| 9945 | removeConnectionLocked(r, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9946 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9947 | if (r.binding.service.app != null) { |
| 9948 | // This could have made the service less important. |
| 9949 | updateOomAdjLocked(r.binding.service.app); |
| 9950 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9951 | } |
| 9952 | |
| 9953 | Binder.restoreCallingIdentity(origId); |
| 9954 | } |
| 9955 | |
| 9956 | return true; |
| 9957 | } |
| 9958 | |
| 9959 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 9960 | // Refuse possible leaked file descriptors |
| 9961 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 9962 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 9963 | } |
| 9964 | |
| 9965 | synchronized(this) { |
| 9966 | if (!(token instanceof ServiceRecord)) { |
| 9967 | throw new IllegalArgumentException("Invalid service token"); |
| 9968 | } |
| 9969 | ServiceRecord r = (ServiceRecord)token; |
| 9970 | |
| 9971 | final long origId = Binder.clearCallingIdentity(); |
| 9972 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9973 | if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9974 | + " " + intent + ": " + service); |
| 9975 | if (r != null) { |
| 9976 | Intent.FilterComparison filter |
| 9977 | = new Intent.FilterComparison(intent); |
| 9978 | IntentBindRecord b = r.bindings.get(filter); |
| 9979 | if (b != null && !b.received) { |
| 9980 | b.binder = service; |
| 9981 | b.requested = true; |
| 9982 | b.received = true; |
| 9983 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9984 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9985 | = r.connections.values().iterator(); |
| 9986 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9987 | ArrayList<ConnectionRecord> clist = it.next(); |
| 9988 | for (int i=0; i<clist.size(); i++) { |
| 9989 | ConnectionRecord c = clist.get(i); |
| 9990 | if (!filter.equals(c.binding.intent.intent)) { |
| 9991 | if (DEBUG_SERVICE) Slog.v( |
| 9992 | TAG, "Not publishing to: " + c); |
| 9993 | if (DEBUG_SERVICE) Slog.v( |
| 9994 | TAG, "Bound intent: " + c.binding.intent.intent); |
| 9995 | if (DEBUG_SERVICE) Slog.v( |
| 9996 | TAG, "Published intent: " + intent); |
| 9997 | continue; |
| 9998 | } |
| 9999 | if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c); |
| 10000 | try { |
| 10001 | c.conn.connected(r.name, service); |
| 10002 | } catch (Exception e) { |
| 10003 | Slog.w(TAG, "Failure sending service " + r.name + |
| 10004 | " to connection " + c.conn.asBinder() + |
| 10005 | " (in " + c.binding.client.processName + ")", e); |
| 10006 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10007 | } |
| 10008 | } |
| 10009 | } |
| 10010 | } |
| 10011 | |
| 10012 | serviceDoneExecutingLocked(r, mStoppingServices.contains(r)); |
| 10013 | |
| 10014 | Binder.restoreCallingIdentity(origId); |
| 10015 | } |
| 10016 | } |
| 10017 | } |
| 10018 | |
| 10019 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 10020 | // Refuse possible leaked file descriptors |
| 10021 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 10022 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10023 | } |
| 10024 | |
| 10025 | synchronized(this) { |
| 10026 | if (!(token instanceof ServiceRecord)) { |
| 10027 | throw new IllegalArgumentException("Invalid service token"); |
| 10028 | } |
| 10029 | ServiceRecord r = (ServiceRecord)token; |
| 10030 | |
| 10031 | final long origId = Binder.clearCallingIdentity(); |
| 10032 | |
| 10033 | if (r != null) { |
| 10034 | Intent.FilterComparison filter |
| 10035 | = new Intent.FilterComparison(intent); |
| 10036 | IntentBindRecord b = r.bindings.get(filter); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10037 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10038 | + " at " + b + ": apps=" |
| 10039 | + (b != null ? b.apps.size() : 0)); |
| 10040 | if (b != null) { |
| 10041 | if (b.apps.size() > 0) { |
| 10042 | // Applications have already bound since the last |
| 10043 | // unbind, so just rebind right here. |
| 10044 | requestServiceBindingLocked(r, b, true); |
| 10045 | } else { |
| 10046 | // Note to tell the service the next time there is |
| 10047 | // a new client. |
| 10048 | b.doRebind = true; |
| 10049 | } |
| 10050 | } |
| 10051 | |
| 10052 | serviceDoneExecutingLocked(r, mStoppingServices.contains(r)); |
| 10053 | |
| 10054 | Binder.restoreCallingIdentity(origId); |
| 10055 | } |
| 10056 | } |
| 10057 | } |
| 10058 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10059 | 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] | 10060 | synchronized(this) { |
| 10061 | if (!(token instanceof ServiceRecord)) { |
| 10062 | throw new IllegalArgumentException("Invalid service token"); |
| 10063 | } |
| 10064 | ServiceRecord r = (ServiceRecord)token; |
| 10065 | boolean inStopping = mStoppingServices.contains(token); |
| 10066 | if (r != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10067 | if (r != token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10068 | Slog.w(TAG, "Done executing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10069 | + " with incorrect token: given " + token |
| 10070 | + ", expected " + r); |
| 10071 | return; |
| 10072 | } |
| 10073 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10074 | if (type == 1) { |
| 10075 | // This is a call from a service start... take care of |
| 10076 | // book-keeping. |
| 10077 | r.callStart = true; |
| 10078 | switch (res) { |
| 10079 | case Service.START_STICKY_COMPATIBILITY: |
| 10080 | case Service.START_STICKY: { |
| 10081 | // We are done with the associated start arguments. |
| 10082 | r.findDeliveredStart(startId, true); |
| 10083 | // Don't stop if killed. |
| 10084 | r.stopIfKilled = false; |
| 10085 | break; |
| 10086 | } |
| 10087 | case Service.START_NOT_STICKY: { |
| 10088 | // We are done with the associated start arguments. |
| 10089 | r.findDeliveredStart(startId, true); |
| 10090 | if (r.lastStartId == startId) { |
| 10091 | // There is no more work, and this service |
| 10092 | // doesn't want to hang around if killed. |
| 10093 | r.stopIfKilled = true; |
| 10094 | } |
| 10095 | break; |
| 10096 | } |
| 10097 | case Service.START_REDELIVER_INTENT: { |
| 10098 | // We'll keep this item until they explicitly |
| 10099 | // call stop for it, but keep track of the fact |
| 10100 | // that it was delivered. |
| 10101 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 10102 | if (si != null) { |
| 10103 | si.deliveryCount = 0; |
| 10104 | si.doneExecutingCount++; |
| 10105 | // Don't stop if killed. |
| 10106 | r.stopIfKilled = true; |
| 10107 | } |
| 10108 | break; |
| 10109 | } |
| 10110 | default: |
| 10111 | throw new IllegalArgumentException( |
| 10112 | "Unknown service start result: " + res); |
| 10113 | } |
| 10114 | if (res == Service.START_STICKY_COMPATIBILITY) { |
| 10115 | r.callStart = false; |
| 10116 | } |
| 10117 | } |
| 10118 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10119 | final long origId = Binder.clearCallingIdentity(); |
| 10120 | serviceDoneExecutingLocked(r, inStopping); |
| 10121 | Binder.restoreCallingIdentity(origId); |
| 10122 | } else { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10123 | Slog.w(TAG, "Done executing unknown service from pid " |
| 10124 | + Binder.getCallingPid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10125 | } |
| 10126 | } |
| 10127 | } |
| 10128 | |
| 10129 | public void serviceDoneExecutingLocked(ServiceRecord r, boolean inStopping) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10130 | if (DEBUG_SERVICE) Slog.v(TAG, "<<< DONE EXECUTING " + r |
| 10131 | + ": nesting=" + r.executeNesting |
| 10132 | + ", inStopping=" + inStopping + ", app=" + r.app); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10133 | 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] | 10134 | r.executeNesting--; |
| 10135 | if (r.executeNesting <= 0 && r.app != null) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10136 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 10137 | "Nesting at 0 of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10138 | r.app.executingServices.remove(r); |
| 10139 | if (r.app.executingServices.size() == 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10140 | if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG, |
| 10141 | "No more executingServices of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10142 | mHandler.removeMessages(SERVICE_TIMEOUT_MSG, r.app); |
| 10143 | } |
| 10144 | if (inStopping) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10145 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 10146 | "doneExecuting remove stopping " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10147 | mStoppingServices.remove(r); |
Mattias Petersson | 3996b41 | 2010-10-27 09:32:51 +0200 | [diff] [blame] | 10148 | r.bindings.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10149 | } |
| 10150 | updateOomAdjLocked(r.app); |
| 10151 | } |
| 10152 | } |
| 10153 | |
| 10154 | void serviceTimeout(ProcessRecord proc) { |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 10155 | String anrMessage = null; |
| 10156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10157 | synchronized(this) { |
| 10158 | if (proc.executingServices.size() == 0 || proc.thread == null) { |
| 10159 | return; |
| 10160 | } |
| 10161 | long maxTime = SystemClock.uptimeMillis() - SERVICE_TIMEOUT; |
| 10162 | Iterator<ServiceRecord> it = proc.executingServices.iterator(); |
| 10163 | ServiceRecord timeout = null; |
| 10164 | long nextTime = 0; |
| 10165 | while (it.hasNext()) { |
| 10166 | ServiceRecord sr = it.next(); |
| 10167 | if (sr.executingStart < maxTime) { |
| 10168 | timeout = sr; |
| 10169 | break; |
| 10170 | } |
| 10171 | if (sr.executingStart > nextTime) { |
| 10172 | nextTime = sr.executingStart; |
| 10173 | } |
| 10174 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10175 | if (timeout != null && mLruProcesses.contains(proc)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10176 | Slog.w(TAG, "Timeout executing service: " + timeout); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 10177 | anrMessage = "Executing service " + timeout.shortName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10178 | } else { |
| 10179 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 10180 | msg.obj = proc; |
| 10181 | mHandler.sendMessageAtTime(msg, nextTime+SERVICE_TIMEOUT); |
| 10182 | } |
| 10183 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 10184 | |
| 10185 | if (anrMessage != null) { |
| 10186 | appNotResponding(proc, null, null, anrMessage); |
| 10187 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10188 | } |
| 10189 | |
| 10190 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10191 | // BACKUP AND RESTORE |
| 10192 | // ========================================================= |
| 10193 | |
| 10194 | // Cause the target app to be launched if necessary and its backup agent |
| 10195 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 10196 | // activity manager to announce its creation. |
| 10197 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10198 | if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10199 | enforceCallingPermission("android.permission.BACKUP", "startBackupAgent"); |
| 10200 | |
| 10201 | synchronized(this) { |
| 10202 | // !!! TODO: currently no check here that we're already bound |
| 10203 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 10204 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 10205 | synchronized (stats) { |
| 10206 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 10207 | } |
| 10208 | |
| 10209 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
| 10210 | ComponentName hostingName = new ComponentName(app.packageName, app.backupAgentName); |
| 10211 | // startProcessLocked() returns existing proc's record if it's already running |
| 10212 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 10213 | false, 0, "backup", hostingName, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10214 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10215 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10216 | return false; |
| 10217 | } |
| 10218 | |
| 10219 | r.app = proc; |
| 10220 | mBackupTarget = r; |
| 10221 | mBackupAppName = app.packageName; |
| 10222 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 10223 | // Try not to kill the process during backup |
| 10224 | updateOomAdjLocked(proc); |
| 10225 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10226 | // If the process is already attached, schedule the creation of the backup agent now. |
| 10227 | // If it is not yet live, this will be done when it attaches to the framework. |
| 10228 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10229 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10230 | try { |
| 10231 | proc.thread.scheduleCreateBackupAgent(app, backupMode); |
| 10232 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 10233 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10234 | } |
| 10235 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10236 | 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] | 10237 | } |
| 10238 | // Invariants: at this point, the target app process exists and the application |
| 10239 | // is either already running or in the process of coming up. mBackupTarget and |
| 10240 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 10241 | // know that it's scheduled for a backup-agent operation. |
| 10242 | } |
| 10243 | |
| 10244 | return true; |
| 10245 | } |
| 10246 | |
| 10247 | // A backup agent has just come up |
| 10248 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10249 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10250 | + " = " + agent); |
| 10251 | |
| 10252 | synchronized(this) { |
| 10253 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10254 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10255 | return; |
| 10256 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 10257 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10258 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 10259 | long oldIdent = Binder.clearCallingIdentity(); |
| 10260 | try { |
| 10261 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10262 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10263 | bm.agentConnected(agentPackageName, agent); |
| 10264 | } catch (RemoteException e) { |
| 10265 | // can't happen; the backup manager service is local |
| 10266 | } catch (Exception e) { |
| 10267 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 10268 | e.printStackTrace(); |
| 10269 | } finally { |
| 10270 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10271 | } |
| 10272 | } |
| 10273 | |
| 10274 | // done with this agent |
| 10275 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10276 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10277 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10278 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10279 | return; |
| 10280 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10281 | |
| 10282 | synchronized(this) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10283 | if (mBackupAppName == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10284 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10285 | return; |
| 10286 | } |
| 10287 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10288 | if (!mBackupAppName.equals(appInfo.packageName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10289 | 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] | 10290 | return; |
| 10291 | } |
| 10292 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 10293 | ProcessRecord proc = mBackupTarget.app; |
| 10294 | mBackupTarget = null; |
| 10295 | mBackupAppName = null; |
| 10296 | |
| 10297 | // Not backing this app up any more; reset its OOM adjustment |
| 10298 | updateOomAdjLocked(proc); |
| 10299 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 10300 | // If the app crashed during backup, 'thread' will be null here |
| 10301 | if (proc.thread != null) { |
| 10302 | try { |
| 10303 | proc.thread.scheduleDestroyBackupAgent(appInfo); |
| 10304 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10305 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 10306 | e.printStackTrace(); |
| 10307 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10308 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10309 | } |
| 10310 | } |
| 10311 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10312 | // BROADCASTS |
| 10313 | // ========================================================= |
| 10314 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 10315 | private final List getStickiesLocked(String action, IntentFilter filter, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10316 | List cur) { |
| 10317 | final ContentResolver resolver = mContext.getContentResolver(); |
| 10318 | final ArrayList<Intent> list = mStickyBroadcasts.get(action); |
| 10319 | if (list == null) { |
| 10320 | return cur; |
| 10321 | } |
| 10322 | int N = list.size(); |
| 10323 | for (int i=0; i<N; i++) { |
| 10324 | Intent intent = list.get(i); |
| 10325 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 10326 | if (cur == null) { |
| 10327 | cur = new ArrayList<Intent>(); |
| 10328 | } |
| 10329 | cur.add(intent); |
| 10330 | } |
| 10331 | } |
| 10332 | return cur; |
| 10333 | } |
| 10334 | |
| 10335 | private final void scheduleBroadcastsLocked() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10336 | if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts: current=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10337 | + mBroadcastsScheduled); |
| 10338 | |
| 10339 | if (mBroadcastsScheduled) { |
| 10340 | return; |
| 10341 | } |
| 10342 | mHandler.sendEmptyMessage(BROADCAST_INTENT_MSG); |
| 10343 | mBroadcastsScheduled = true; |
| 10344 | } |
| 10345 | |
| 10346 | public Intent registerReceiver(IApplicationThread caller, |
| 10347 | IIntentReceiver receiver, IntentFilter filter, String permission) { |
| 10348 | synchronized(this) { |
| 10349 | ProcessRecord callerApp = null; |
| 10350 | if (caller != null) { |
| 10351 | callerApp = getRecordForAppLocked(caller); |
| 10352 | if (callerApp == null) { |
| 10353 | throw new SecurityException( |
| 10354 | "Unable to find app for caller " + caller |
| 10355 | + " (pid=" + Binder.getCallingPid() |
| 10356 | + ") when registering receiver " + receiver); |
| 10357 | } |
| 10358 | } |
| 10359 | |
| 10360 | List allSticky = null; |
| 10361 | |
| 10362 | // Look for any matching sticky broadcasts... |
| 10363 | Iterator actions = filter.actionsIterator(); |
| 10364 | if (actions != null) { |
| 10365 | while (actions.hasNext()) { |
| 10366 | String action = (String)actions.next(); |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 10367 | allSticky = getStickiesLocked(action, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10368 | } |
| 10369 | } else { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 10370 | allSticky = getStickiesLocked(null, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10371 | } |
| 10372 | |
| 10373 | // The first sticky in the list is returned directly back to |
| 10374 | // the client. |
| 10375 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 10376 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10377 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10378 | + ": " + sticky); |
| 10379 | |
| 10380 | if (receiver == null) { |
| 10381 | return sticky; |
| 10382 | } |
| 10383 | |
| 10384 | ReceiverList rl |
| 10385 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 10386 | if (rl == null) { |
| 10387 | rl = new ReceiverList(this, callerApp, |
| 10388 | Binder.getCallingPid(), |
| 10389 | Binder.getCallingUid(), receiver); |
| 10390 | if (rl.app != null) { |
| 10391 | rl.app.receivers.add(rl); |
| 10392 | } else { |
| 10393 | try { |
| 10394 | receiver.asBinder().linkToDeath(rl, 0); |
| 10395 | } catch (RemoteException e) { |
| 10396 | return sticky; |
| 10397 | } |
| 10398 | rl.linkedToDeath = true; |
| 10399 | } |
| 10400 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
| 10401 | } |
| 10402 | BroadcastFilter bf = new BroadcastFilter(filter, rl, permission); |
| 10403 | rl.add(bf); |
| 10404 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10405 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10406 | } |
| 10407 | mReceiverResolver.addFilter(bf); |
| 10408 | |
| 10409 | // Enqueue broadcasts for all existing stickies that match |
| 10410 | // this filter. |
| 10411 | if (allSticky != null) { |
| 10412 | ArrayList receivers = new ArrayList(); |
| 10413 | receivers.add(bf); |
| 10414 | |
| 10415 | int N = allSticky.size(); |
| 10416 | for (int i=0; i<N; i++) { |
| 10417 | Intent intent = (Intent)allSticky.get(i); |
| 10418 | BroadcastRecord r = new BroadcastRecord(intent, null, |
| 10419 | null, -1, -1, null, receivers, null, 0, null, null, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 10420 | false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10421 | if (mParallelBroadcasts.size() == 0) { |
| 10422 | scheduleBroadcastsLocked(); |
| 10423 | } |
| 10424 | mParallelBroadcasts.add(r); |
| 10425 | } |
| 10426 | } |
| 10427 | |
| 10428 | return sticky; |
| 10429 | } |
| 10430 | } |
| 10431 | |
| 10432 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10433 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10434 | |
| 10435 | boolean doNext = false; |
| 10436 | |
| 10437 | synchronized(this) { |
| 10438 | ReceiverList rl |
| 10439 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 10440 | if (rl != null) { |
| 10441 | if (rl.curBroadcast != null) { |
| 10442 | BroadcastRecord r = rl.curBroadcast; |
| 10443 | doNext = finishReceiverLocked( |
| 10444 | receiver.asBinder(), r.resultCode, r.resultData, |
| 10445 | r.resultExtras, r.resultAbort, true); |
| 10446 | } |
| 10447 | |
| 10448 | if (rl.app != null) { |
| 10449 | rl.app.receivers.remove(rl); |
| 10450 | } |
| 10451 | removeReceiverLocked(rl); |
| 10452 | if (rl.linkedToDeath) { |
| 10453 | rl.linkedToDeath = false; |
| 10454 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 10455 | } |
| 10456 | } |
| 10457 | } |
| 10458 | |
| 10459 | if (!doNext) { |
| 10460 | return; |
| 10461 | } |
| 10462 | |
| 10463 | final long origId = Binder.clearCallingIdentity(); |
| 10464 | processNextBroadcast(false); |
| 10465 | trimApplications(); |
| 10466 | Binder.restoreCallingIdentity(origId); |
| 10467 | } |
| 10468 | |
| 10469 | void removeReceiverLocked(ReceiverList rl) { |
| 10470 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 10471 | int N = rl.size(); |
| 10472 | for (int i=0; i<N; i++) { |
| 10473 | mReceiverResolver.removeFilter(rl.get(i)); |
| 10474 | } |
| 10475 | } |
| 10476 | |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 10477 | private final void sendPackageBroadcastLocked(int cmd, String[] packages) { |
| 10478 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 10479 | ProcessRecord r = mLruProcesses.get(i); |
| 10480 | if (r.thread != null) { |
| 10481 | try { |
| 10482 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 10483 | } catch (RemoteException ex) { |
| 10484 | } |
| 10485 | } |
| 10486 | } |
| 10487 | } |
| 10488 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10489 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 10490 | String callerPackage, Intent intent, String resolvedType, |
| 10491 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 10492 | Bundle map, String requiredPermission, |
| 10493 | boolean ordered, boolean sticky, int callingPid, int callingUid) { |
| 10494 | intent = new Intent(intent); |
| 10495 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10496 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10497 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
| 10498 | + " ordered=" + ordered); |
| 10499 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10500 | 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] | 10501 | } |
| 10502 | |
| 10503 | // Handle special intents: if this broadcast is from the package |
| 10504 | // manager about a package being removed, we need to remove all of |
| 10505 | // its activities from the history stack. |
| 10506 | final boolean uidRemoved = intent.ACTION_UID_REMOVED.equals( |
| 10507 | intent.getAction()); |
| 10508 | if (intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 10509 | || intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 10510 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10511 | || uidRemoved) { |
| 10512 | if (checkComponentPermission( |
| 10513 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10514 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10515 | == PackageManager.PERMISSION_GRANTED) { |
| 10516 | if (uidRemoved) { |
| 10517 | final Bundle intentExtras = intent.getExtras(); |
| 10518 | final int uid = intentExtras != null |
| 10519 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 10520 | if (uid >= 0) { |
| 10521 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 10522 | synchronized (bs) { |
| 10523 | bs.removeUidStatsLocked(uid); |
| 10524 | } |
| 10525 | } |
| 10526 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 10527 | // If resources are unvailble just force stop all |
| 10528 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 10529 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 10530 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 10531 | if (list != null && (list.length > 0)) { |
| 10532 | for (String pkg : list) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 10533 | forceStopPackageLocked(pkg, -1, false, true, true); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 10534 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 10535 | sendPackageBroadcastLocked( |
| 10536 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 10537 | } |
| 10538 | } else { |
| 10539 | Uri data = intent.getData(); |
| 10540 | String ssp; |
| 10541 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 10542 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 10543 | forceStopPackageLocked(ssp, |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 10544 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 10545 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 10546 | if (intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
| 10547 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
| 10548 | new String[] {ssp}); |
| 10549 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10550 | } |
| 10551 | } |
| 10552 | } |
| 10553 | } else { |
| 10554 | String msg = "Permission Denial: " + intent.getAction() |
| 10555 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 10556 | + ", uid=" + callingUid + ")" |
| 10557 | + " requires " |
| 10558 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10559 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10560 | throw new SecurityException(msg); |
| 10561 | } |
| 10562 | } |
| 10563 | |
| 10564 | /* |
| 10565 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 10566 | * of all currently running processes. This message will get queued up before the broadcast |
| 10567 | * happens. |
| 10568 | */ |
| 10569 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 10570 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 10571 | } |
| 10572 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 10573 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 10574 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 10575 | } |
| 10576 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 10577 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 10578 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 10579 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 10580 | } |
| 10581 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 10582 | /* |
| 10583 | * Prevent non-system code (defined here to be non-persistent |
| 10584 | * processes) from sending protected broadcasts. |
| 10585 | */ |
| 10586 | if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID |
| 10587 | || callingUid == Process.SHELL_UID || callingUid == 0) { |
| 10588 | // Always okay. |
| 10589 | } else if (callerApp == null || !callerApp.persistent) { |
| 10590 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10591 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 10592 | intent.getAction())) { |
| 10593 | String msg = "Permission Denial: not allowed to send broadcast " |
| 10594 | + intent.getAction() + " from pid=" |
| 10595 | + callingPid + ", uid=" + callingUid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10596 | Slog.w(TAG, msg); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 10597 | throw new SecurityException(msg); |
| 10598 | } |
| 10599 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10600 | Slog.w(TAG, "Remote exception", e); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 10601 | return BROADCAST_SUCCESS; |
| 10602 | } |
| 10603 | } |
| 10604 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10605 | // Add to the sticky list if requested. |
| 10606 | if (sticky) { |
| 10607 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 10608 | callingPid, callingUid) |
| 10609 | != PackageManager.PERMISSION_GRANTED) { |
| 10610 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 10611 | + callingPid + ", uid=" + callingUid |
| 10612 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10613 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10614 | throw new SecurityException(msg); |
| 10615 | } |
| 10616 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10617 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10618 | + " and enforce permission " + requiredPermission); |
| 10619 | return BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
| 10620 | } |
| 10621 | if (intent.getComponent() != null) { |
| 10622 | throw new SecurityException( |
| 10623 | "Sticky broadcasts can't target a specific component"); |
| 10624 | } |
| 10625 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 10626 | if (list == null) { |
| 10627 | list = new ArrayList<Intent>(); |
| 10628 | mStickyBroadcasts.put(intent.getAction(), list); |
| 10629 | } |
| 10630 | int N = list.size(); |
| 10631 | int i; |
| 10632 | for (i=0; i<N; i++) { |
| 10633 | if (intent.filterEquals(list.get(i))) { |
| 10634 | // This sticky already exists, replace it. |
| 10635 | list.set(i, new Intent(intent)); |
| 10636 | break; |
| 10637 | } |
| 10638 | } |
| 10639 | if (i >= N) { |
| 10640 | list.add(new Intent(intent)); |
| 10641 | } |
| 10642 | } |
| 10643 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10644 | // Figure out who all will receive this broadcast. |
| 10645 | List receivers = null; |
| 10646 | List<BroadcastFilter> registeredReceivers = null; |
| 10647 | try { |
| 10648 | if (intent.getComponent() != null) { |
| 10649 | // Broadcast is going to one specific receiver class... |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10650 | ActivityInfo ai = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 10651 | getReceiverInfo(intent.getComponent(), STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10652 | if (ai != null) { |
| 10653 | receivers = new ArrayList(); |
| 10654 | ResolveInfo ri = new ResolveInfo(); |
| 10655 | ri.activityInfo = ai; |
| 10656 | receivers.add(ri); |
| 10657 | } |
| 10658 | } else { |
| 10659 | // Need to resolve the intent to interested receivers... |
| 10660 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 10661 | == 0) { |
| 10662 | receivers = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10663 | AppGlobals.getPackageManager().queryIntentReceivers( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 10664 | intent, resolvedType, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10665 | } |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 10666 | registeredReceivers = mReceiverResolver.queryIntent(intent, resolvedType, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10667 | } |
| 10668 | } catch (RemoteException ex) { |
| 10669 | // pm is in same process, this will never happen. |
| 10670 | } |
| 10671 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 10672 | final boolean replacePending = |
| 10673 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 10674 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10675 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 10676 | + " replacePending=" + replacePending); |
| 10677 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10678 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 10679 | if (!ordered && NR > 0) { |
| 10680 | // If we are not serializing this broadcast, then send the |
| 10681 | // registered receivers separately so they don't wait for the |
| 10682 | // components to be launched. |
| 10683 | BroadcastRecord r = new BroadcastRecord(intent, callerApp, |
| 10684 | callerPackage, callingPid, callingUid, requiredPermission, |
| 10685 | registeredReceivers, resultTo, resultCode, resultData, map, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 10686 | ordered, sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10687 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10688 | TAG, "Enqueueing parallel broadcast " + r |
| 10689 | + ": prev had " + mParallelBroadcasts.size()); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 10690 | boolean replaced = false; |
| 10691 | if (replacePending) { |
| 10692 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 10693 | if (intent.filterEquals(mParallelBroadcasts.get(i).intent)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10694 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 10695 | "***** DROPPING PARALLEL: " + intent); |
| 10696 | mParallelBroadcasts.set(i, r); |
| 10697 | replaced = true; |
| 10698 | break; |
| 10699 | } |
| 10700 | } |
| 10701 | } |
| 10702 | if (!replaced) { |
| 10703 | mParallelBroadcasts.add(r); |
| 10704 | scheduleBroadcastsLocked(); |
| 10705 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10706 | registeredReceivers = null; |
| 10707 | NR = 0; |
| 10708 | } |
| 10709 | |
| 10710 | // Merge into one list. |
| 10711 | int ir = 0; |
| 10712 | if (receivers != null) { |
| 10713 | // A special case for PACKAGE_ADDED: do not allow the package |
| 10714 | // being added to see this broadcast. This prevents them from |
| 10715 | // using this as a back door to get run as soon as they are |
| 10716 | // installed. Maybe in the future we want to have a special install |
| 10717 | // broadcast or such for apps, but we'd like to deliberately make |
| 10718 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 10719 | String skipPackages[] = null; |
| 10720 | if (intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 10721 | || intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 10722 | || intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
| 10723 | Uri data = intent.getData(); |
| 10724 | if (data != null) { |
| 10725 | String pkgName = data.getSchemeSpecificPart(); |
| 10726 | if (pkgName != null) { |
| 10727 | skipPackages = new String[] { pkgName }; |
| 10728 | } |
| 10729 | } |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 10730 | } else if (intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 10731 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 10732 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 10733 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 10734 | for (String skipPackage : skipPackages) { |
| 10735 | if (skipPackage != null) { |
| 10736 | int NT = receivers.size(); |
| 10737 | for (int it=0; it<NT; it++) { |
| 10738 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 10739 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 10740 | receivers.remove(it); |
| 10741 | it--; |
| 10742 | NT--; |
| 10743 | } |
| 10744 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10745 | } |
| 10746 | } |
| 10747 | } |
| 10748 | |
| 10749 | int NT = receivers != null ? receivers.size() : 0; |
| 10750 | int it = 0; |
| 10751 | ResolveInfo curt = null; |
| 10752 | BroadcastFilter curr = null; |
| 10753 | while (it < NT && ir < NR) { |
| 10754 | if (curt == null) { |
| 10755 | curt = (ResolveInfo)receivers.get(it); |
| 10756 | } |
| 10757 | if (curr == null) { |
| 10758 | curr = registeredReceivers.get(ir); |
| 10759 | } |
| 10760 | if (curr.getPriority() >= curt.priority) { |
| 10761 | // Insert this broadcast record into the final list. |
| 10762 | receivers.add(it, curr); |
| 10763 | ir++; |
| 10764 | curr = null; |
| 10765 | it++; |
| 10766 | NT++; |
| 10767 | } else { |
| 10768 | // Skip to the next ResolveInfo in the final list. |
| 10769 | it++; |
| 10770 | curt = null; |
| 10771 | } |
| 10772 | } |
| 10773 | } |
| 10774 | while (ir < NR) { |
| 10775 | if (receivers == null) { |
| 10776 | receivers = new ArrayList(); |
| 10777 | } |
| 10778 | receivers.add(registeredReceivers.get(ir)); |
| 10779 | ir++; |
| 10780 | } |
| 10781 | |
| 10782 | if ((receivers != null && receivers.size() > 0) |
| 10783 | || resultTo != null) { |
| 10784 | BroadcastRecord r = new BroadcastRecord(intent, callerApp, |
| 10785 | callerPackage, callingPid, callingUid, requiredPermission, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 10786 | receivers, resultTo, resultCode, resultData, map, ordered, |
| 10787 | sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10788 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10789 | TAG, "Enqueueing ordered broadcast " + r |
| 10790 | + ": prev had " + mOrderedBroadcasts.size()); |
| 10791 | if (DEBUG_BROADCAST) { |
| 10792 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10793 | 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] | 10794 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 10795 | boolean replaced = false; |
| 10796 | if (replacePending) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10797 | for (int i=mOrderedBroadcasts.size()-1; i>0; i--) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 10798 | if (intent.filterEquals(mOrderedBroadcasts.get(i).intent)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10799 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 10800 | "***** DROPPING ORDERED: " + intent); |
| 10801 | mOrderedBroadcasts.set(i, r); |
| 10802 | replaced = true; |
| 10803 | break; |
| 10804 | } |
| 10805 | } |
| 10806 | } |
| 10807 | if (!replaced) { |
| 10808 | mOrderedBroadcasts.add(r); |
| 10809 | scheduleBroadcastsLocked(); |
| 10810 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10811 | } |
| 10812 | |
| 10813 | return BROADCAST_SUCCESS; |
| 10814 | } |
| 10815 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10816 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10817 | // Refuse possible leaked file descriptors |
| 10818 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 10819 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10820 | } |
| 10821 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10822 | int flags = intent.getFlags(); |
| 10823 | |
| 10824 | if (!mProcessesReady) { |
| 10825 | // if the caller really truly claims to know what they're doing, go |
| 10826 | // ahead and allow the broadcast without launching any receivers |
| 10827 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 10828 | intent = new Intent(intent); |
| 10829 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 10830 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 10831 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 10832 | + " before boot completion"); |
| 10833 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 10834 | } |
| 10835 | } |
| 10836 | |
| 10837 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 10838 | throw new IllegalArgumentException( |
| 10839 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 10840 | } |
| 10841 | |
| 10842 | return intent; |
| 10843 | } |
| 10844 | |
| 10845 | public final int broadcastIntent(IApplicationThread caller, |
| 10846 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 10847 | int resultCode, String resultData, Bundle map, |
| 10848 | String requiredPermission, boolean serialized, boolean sticky) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10849 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10850 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 10851 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10852 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 10853 | final int callingPid = Binder.getCallingPid(); |
| 10854 | final int callingUid = Binder.getCallingUid(); |
| 10855 | final long origId = Binder.clearCallingIdentity(); |
| 10856 | int res = broadcastIntentLocked(callerApp, |
| 10857 | callerApp != null ? callerApp.info.packageName : null, |
| 10858 | intent, resolvedType, resultTo, |
| 10859 | resultCode, resultData, map, requiredPermission, serialized, |
| 10860 | sticky, callingPid, callingUid); |
| 10861 | Binder.restoreCallingIdentity(origId); |
| 10862 | return res; |
| 10863 | } |
| 10864 | } |
| 10865 | |
| 10866 | int broadcastIntentInPackage(String packageName, int uid, |
| 10867 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 10868 | int resultCode, String resultData, Bundle map, |
| 10869 | String requiredPermission, boolean serialized, boolean sticky) { |
| 10870 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10871 | intent = verifyBroadcastLocked(intent); |
| 10872 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10873 | final long origId = Binder.clearCallingIdentity(); |
| 10874 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 10875 | resultTo, resultCode, resultData, map, requiredPermission, |
| 10876 | serialized, sticky, -1, uid); |
| 10877 | Binder.restoreCallingIdentity(origId); |
| 10878 | return res; |
| 10879 | } |
| 10880 | } |
| 10881 | |
| 10882 | public final void unbroadcastIntent(IApplicationThread caller, |
| 10883 | Intent intent) { |
| 10884 | // Refuse possible leaked file descriptors |
| 10885 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 10886 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10887 | } |
| 10888 | |
| 10889 | synchronized(this) { |
| 10890 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 10891 | != PackageManager.PERMISSION_GRANTED) { |
| 10892 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 10893 | + Binder.getCallingPid() |
| 10894 | + ", uid=" + Binder.getCallingUid() |
| 10895 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10896 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10897 | throw new SecurityException(msg); |
| 10898 | } |
| 10899 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 10900 | if (list != null) { |
| 10901 | int N = list.size(); |
| 10902 | int i; |
| 10903 | for (i=0; i<N; i++) { |
| 10904 | if (intent.filterEquals(list.get(i))) { |
| 10905 | list.remove(i); |
| 10906 | break; |
| 10907 | } |
| 10908 | } |
| 10909 | } |
| 10910 | } |
| 10911 | } |
| 10912 | |
| 10913 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 10914 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 10915 | boolean explicit) { |
| 10916 | if (mOrderedBroadcasts.size() == 0) { |
| 10917 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10918 | Slog.w(TAG, "finishReceiver called but no pending broadcasts"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10919 | } |
| 10920 | return false; |
| 10921 | } |
| 10922 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 10923 | if (r.receiver == null) { |
| 10924 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10925 | Slog.w(TAG, "finishReceiver called but none active"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10926 | } |
| 10927 | return false; |
| 10928 | } |
| 10929 | if (r.receiver != receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10930 | 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] | 10931 | return false; |
| 10932 | } |
| 10933 | int state = r.state; |
| 10934 | r.state = r.IDLE; |
| 10935 | if (state == r.IDLE) { |
| 10936 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10937 | Slog.w(TAG, "finishReceiver called but state is IDLE"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10938 | } |
| 10939 | } |
| 10940 | r.receiver = null; |
| 10941 | r.intent.setComponent(null); |
| 10942 | if (r.curApp != null) { |
| 10943 | r.curApp.curReceiver = null; |
| 10944 | } |
| 10945 | if (r.curFilter != null) { |
| 10946 | r.curFilter.receiverList.curBroadcast = null; |
| 10947 | } |
| 10948 | r.curFilter = null; |
| 10949 | r.curApp = null; |
| 10950 | r.curComponent = null; |
| 10951 | r.curReceiver = null; |
| 10952 | mPendingBroadcast = null; |
| 10953 | |
| 10954 | r.resultCode = resultCode; |
| 10955 | r.resultData = resultData; |
| 10956 | r.resultExtras = resultExtras; |
| 10957 | r.resultAbort = resultAbort; |
| 10958 | |
| 10959 | // We will process the next receiver right now if this is finishing |
| 10960 | // an app receiver (which is always asynchronous) or after we have |
| 10961 | // come back from calling a receiver. |
| 10962 | return state == BroadcastRecord.APP_RECEIVE |
| 10963 | || state == BroadcastRecord.CALL_DONE_RECEIVE; |
| 10964 | } |
| 10965 | |
| 10966 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 10967 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10968 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10969 | |
| 10970 | // Refuse possible leaked file descriptors |
| 10971 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 10972 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 10973 | } |
| 10974 | |
| 10975 | boolean doNext; |
| 10976 | |
| 10977 | final long origId = Binder.clearCallingIdentity(); |
| 10978 | |
| 10979 | synchronized(this) { |
| 10980 | doNext = finishReceiverLocked( |
| 10981 | who, resultCode, resultData, resultExtras, resultAbort, true); |
| 10982 | } |
| 10983 | |
| 10984 | if (doNext) { |
| 10985 | processNextBroadcast(false); |
| 10986 | } |
| 10987 | trimApplications(); |
| 10988 | |
| 10989 | Binder.restoreCallingIdentity(origId); |
| 10990 | } |
| 10991 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 10992 | private final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10993 | if (r.nextReceiver > 0) { |
| 10994 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 10995 | if (curReceiver instanceof BroadcastFilter) { |
| 10996 | BroadcastFilter bf = (BroadcastFilter) curReceiver; |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10997 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10998 | System.identityHashCode(r), |
| 10999 | r.intent.getAction(), |
| 11000 | r.nextReceiver - 1, |
| 11001 | System.identityHashCode(bf)); |
| 11002 | } else { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 11003 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11004 | System.identityHashCode(r), |
| 11005 | r.intent.getAction(), |
| 11006 | r.nextReceiver - 1, |
| 11007 | ((ResolveInfo)curReceiver).toString()); |
| 11008 | } |
| 11009 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11010 | 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] | 11011 | + r); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 11012 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11013 | System.identityHashCode(r), |
| 11014 | r.intent.getAction(), |
| 11015 | r.nextReceiver, |
| 11016 | "NONE"); |
| 11017 | } |
| 11018 | } |
| 11019 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11020 | private final void setBroadcastTimeoutLocked(long timeoutTime) { |
| 11021 | if (! mPendingBroadcastTimeoutMessage) { |
| 11022 | Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG); |
| 11023 | mHandler.sendMessageAtTime(msg, timeoutTime); |
| 11024 | mPendingBroadcastTimeoutMessage = true; |
| 11025 | } |
| 11026 | } |
| 11027 | |
| 11028 | private final void cancelBroadcastTimeoutLocked() { |
| 11029 | if (mPendingBroadcastTimeoutMessage) { |
| 11030 | mHandler.removeMessages(BROADCAST_TIMEOUT_MSG); |
| 11031 | mPendingBroadcastTimeoutMessage = false; |
| 11032 | } |
| 11033 | } |
| 11034 | |
| 11035 | private final void broadcastTimeoutLocked(boolean fromMsg) { |
| 11036 | if (fromMsg) { |
| 11037 | mPendingBroadcastTimeoutMessage = false; |
| 11038 | } |
| 11039 | |
| 11040 | if (mOrderedBroadcasts.size() == 0) { |
| 11041 | return; |
| 11042 | } |
| 11043 | |
| 11044 | long now = SystemClock.uptimeMillis(); |
| 11045 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 11046 | if (fromMsg) { |
| 11047 | if (mDidDexOpt) { |
| 11048 | // Delay timeouts until dexopt finishes. |
| 11049 | mDidDexOpt = false; |
| 11050 | long timeoutTime = SystemClock.uptimeMillis() + BROADCAST_TIMEOUT; |
| 11051 | setBroadcastTimeoutLocked(timeoutTime); |
| 11052 | return; |
| 11053 | } |
| 11054 | if (! mProcessesReady) { |
| 11055 | // Only process broadcast timeouts if the system is ready. That way |
| 11056 | // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended |
| 11057 | // to do heavy lifting for system up. |
| 11058 | return; |
| 11059 | } |
| 11060 | |
| 11061 | long timeoutTime = r.receiverTime + BROADCAST_TIMEOUT; |
| 11062 | if (timeoutTime > now) { |
| 11063 | // We can observe premature timeouts because we do not cancel and reset the |
| 11064 | // broadcast timeout message after each receiver finishes. Instead, we set up |
| 11065 | // an initial timeout then kick it down the road a little further as needed |
| 11066 | // when it expires. |
| 11067 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 11068 | "Premature timeout @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for " |
| 11069 | + timeoutTime); |
| 11070 | setBroadcastTimeoutLocked(timeoutTime); |
| 11071 | return; |
| 11072 | } |
| 11073 | } |
| 11074 | |
| 11075 | Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r.receiver |
| 11076 | + ", started " + (now - r.receiverTime) + "ms ago"); |
| 11077 | r.receiverTime = now; |
| 11078 | r.anrCount++; |
| 11079 | |
| 11080 | // Current receiver has passed its expiration date. |
| 11081 | if (r.nextReceiver <= 0) { |
| 11082 | Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0"); |
| 11083 | return; |
| 11084 | } |
| 11085 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11086 | ProcessRecord app = null; |
| 11087 | String anrMessage = null; |
| 11088 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11089 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 11090 | Slog.w(TAG, "Receiver during timeout: " + curReceiver); |
| 11091 | logBroadcastReceiverDiscardLocked(r); |
| 11092 | if (curReceiver instanceof BroadcastFilter) { |
| 11093 | BroadcastFilter bf = (BroadcastFilter)curReceiver; |
| 11094 | if (bf.receiverList.pid != 0 |
| 11095 | && bf.receiverList.pid != MY_PID) { |
| 11096 | synchronized (this.mPidsSelfLocked) { |
| 11097 | app = this.mPidsSelfLocked.get( |
| 11098 | bf.receiverList.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11099 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11100 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11101 | } else { |
| 11102 | app = r.curApp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11103 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11104 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11105 | if (app != null) { |
| 11106 | anrMessage = "Broadcast of " + r.intent.toString(); |
| 11107 | } |
| 11108 | |
| 11109 | if (mPendingBroadcast == r) { |
| 11110 | mPendingBroadcast = null; |
| 11111 | } |
| 11112 | |
| 11113 | // Move on to the next receiver. |
| 11114 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 11115 | r.resultExtras, r.resultAbort, true); |
| 11116 | scheduleBroadcastsLocked(); |
| 11117 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11118 | if (anrMessage != null) { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11119 | // Post the ANR to the handler since we do not want to process ANRs while |
| 11120 | // potentially holding our lock. |
| 11121 | mHandler.post(new AppNotResponding(app, anrMessage)); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11122 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11123 | } |
| 11124 | |
| 11125 | private final void processCurBroadcastLocked(BroadcastRecord r, |
| 11126 | ProcessRecord app) throws RemoteException { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11127 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 11128 | "Process cur broadcast " + r + " for app " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11129 | if (app.thread == null) { |
| 11130 | throw new RemoteException(); |
| 11131 | } |
| 11132 | r.receiver = app.thread.asBinder(); |
| 11133 | r.curApp = app; |
| 11134 | app.curReceiver = r; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11135 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11136 | |
| 11137 | // Tell the application to launch this receiver. |
| 11138 | r.intent.setComponent(r.curComponent); |
| 11139 | |
| 11140 | boolean started = false; |
| 11141 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11142 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11143 | "Delivering to component " + r.curComponent |
| 11144 | + ": " + r); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 11145 | ensurePackageDexOpt(r.intent.getComponent().getPackageName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11146 | app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver, |
| 11147 | r.resultCode, r.resultData, r.resultExtras, r.ordered); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11148 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 11149 | "Process cur broadcast " + r + " DELIVERED for app " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11150 | started = true; |
| 11151 | } finally { |
| 11152 | if (!started) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11153 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 11154 | "Process cur broadcast " + r + ": NOT STARTED!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11155 | r.receiver = null; |
| 11156 | r.curApp = null; |
| 11157 | app.curReceiver = null; |
| 11158 | } |
| 11159 | } |
| 11160 | |
| 11161 | } |
| 11162 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11163 | static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 11164 | Intent intent, int resultCode, String data, Bundle extras, |
| 11165 | boolean ordered, boolean sticky) throws RemoteException { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11166 | // 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] | 11167 | if (app != null && app.thread != null) { |
| 11168 | // If we have an app thread, do the call through that so it is |
| 11169 | // correctly ordered with other one-way calls. |
| 11170 | app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 11171 | data, extras, ordered, sticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11172 | } else { |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 11173 | receiver.performReceive(intent, resultCode, data, extras, ordered, sticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11174 | } |
| 11175 | } |
| 11176 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11177 | private final void deliverToRegisteredReceiverLocked(BroadcastRecord r, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11178 | BroadcastFilter filter, boolean ordered) { |
| 11179 | boolean skip = false; |
| 11180 | if (filter.requiredPermission != null) { |
| 11181 | int perm = checkComponentPermission(filter.requiredPermission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11182 | r.callingPid, r.callingUid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11183 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11184 | Slog.w(TAG, "Permission Denial: broadcasting " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11185 | + r.intent.toString() |
| 11186 | + " from " + r.callerPackage + " (pid=" |
| 11187 | + r.callingPid + ", uid=" + r.callingUid + ")" |
| 11188 | + " requires " + filter.requiredPermission |
| 11189 | + " due to registered receiver " + filter); |
| 11190 | skip = true; |
| 11191 | } |
| 11192 | } |
| 11193 | if (r.requiredPermission != null) { |
| 11194 | int perm = checkComponentPermission(r.requiredPermission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11195 | filter.receiverList.pid, filter.receiverList.uid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11196 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11197 | Slog.w(TAG, "Permission Denial: receiving " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11198 | + r.intent.toString() |
| 11199 | + " to " + filter.receiverList.app |
| 11200 | + " (pid=" + filter.receiverList.pid |
| 11201 | + ", uid=" + filter.receiverList.uid + ")" |
| 11202 | + " requires " + r.requiredPermission |
| 11203 | + " due to sender " + r.callerPackage |
| 11204 | + " (uid " + r.callingUid + ")"); |
| 11205 | skip = true; |
| 11206 | } |
| 11207 | } |
| 11208 | |
| 11209 | if (!skip) { |
| 11210 | // If this is not being sent as an ordered broadcast, then we |
| 11211 | // don't want to touch the fields that keep track of the current |
| 11212 | // state of ordered broadcasts. |
| 11213 | if (ordered) { |
| 11214 | r.receiver = filter.receiverList.receiver.asBinder(); |
| 11215 | r.curFilter = filter; |
| 11216 | filter.receiverList.curBroadcast = r; |
| 11217 | r.state = BroadcastRecord.CALL_IN_RECEIVE; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11218 | if (filter.receiverList.app != null) { |
| 11219 | // Bump hosting application to no longer be in background |
| 11220 | // scheduling class. Note that we can't do that if there |
| 11221 | // isn't an app... but we can only be in that case for |
| 11222 | // things that directly call the IActivityManager API, which |
| 11223 | // are already core system stuff so don't matter for this. |
| 11224 | r.curApp = filter.receiverList.app; |
| 11225 | filter.receiverList.app.curReceiver = r; |
| 11226 | updateOomAdjLocked(); |
| 11227 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11228 | } |
| 11229 | try { |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11230 | if (DEBUG_BROADCAST_LIGHT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11231 | int seq = r.intent.getIntExtra("seq", -1); |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 11232 | Slog.i(TAG, "Delivering to " + filter |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11233 | + " (seq=" + seq + "): " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11234 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11235 | performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11236 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11237 | r.resultData, r.resultExtras, r.ordered, r.initialSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11238 | if (ordered) { |
| 11239 | r.state = BroadcastRecord.CALL_DONE_RECEIVE; |
| 11240 | } |
| 11241 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11242 | Slog.w(TAG, "Failure sending broadcast " + r.intent, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11243 | if (ordered) { |
| 11244 | r.receiver = null; |
| 11245 | r.curFilter = null; |
| 11246 | filter.receiverList.curBroadcast = null; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11247 | if (filter.receiverList.app != null) { |
| 11248 | filter.receiverList.app.curReceiver = null; |
| 11249 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11250 | } |
| 11251 | } |
| 11252 | } |
| 11253 | } |
| 11254 | |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11255 | private final void addBroadcastToHistoryLocked(BroadcastRecord r) { |
| 11256 | if (r.callingUid < 0) { |
| 11257 | // This was from a registerReceiver() call; ignore it. |
| 11258 | return; |
| 11259 | } |
| 11260 | System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1, |
| 11261 | MAX_BROADCAST_HISTORY-1); |
| 11262 | r.finishTime = SystemClock.uptimeMillis(); |
| 11263 | mBroadcastHistory[0] = r; |
| 11264 | } |
| 11265 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11266 | private final void processNextBroadcast(boolean fromMsg) { |
| 11267 | synchronized(this) { |
| 11268 | BroadcastRecord r; |
| 11269 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11270 | if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11271 | + mParallelBroadcasts.size() + " broadcasts, " |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 11272 | + mOrderedBroadcasts.size() + " ordered broadcasts"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11273 | |
| 11274 | updateCpuStats(); |
| 11275 | |
| 11276 | if (fromMsg) { |
| 11277 | mBroadcastsScheduled = false; |
| 11278 | } |
| 11279 | |
| 11280 | // First, deliver any non-serialized broadcasts right away. |
| 11281 | while (mParallelBroadcasts.size() > 0) { |
| 11282 | r = mParallelBroadcasts.remove(0); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11283 | r.dispatchTime = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11284 | final int N = r.receivers.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11285 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11286 | + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11287 | for (int i=0; i<N; i++) { |
| 11288 | Object target = r.receivers.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11289 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 11290 | "Delivering non-ordered to registered " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11291 | + target + ": " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11292 | deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11293 | } |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11294 | addBroadcastToHistoryLocked(r); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11295 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11296 | + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11297 | } |
| 11298 | |
| 11299 | // Now take care of the next serialized one... |
| 11300 | |
| 11301 | // If we are waiting for a process to come up to handle the next |
| 11302 | // broadcast, then do nothing at this point. Just in case, we |
| 11303 | // check that the process we're waiting for still exists. |
| 11304 | if (mPendingBroadcast != null) { |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 11305 | if (DEBUG_BROADCAST_LIGHT) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11306 | Slog.v(TAG, "processNextBroadcast: waiting for " |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 11307 | + mPendingBroadcast.curApp); |
| 11308 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11309 | |
| 11310 | boolean isDead; |
| 11311 | synchronized (mPidsSelfLocked) { |
| 11312 | isDead = (mPidsSelfLocked.get(mPendingBroadcast.curApp.pid) == null); |
| 11313 | } |
| 11314 | if (!isDead) { |
| 11315 | // It's still alive, so keep waiting |
| 11316 | return; |
| 11317 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11318 | Slog.w(TAG, "pending app " + mPendingBroadcast.curApp |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11319 | + " died before responding to broadcast"); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11320 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 11321 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11322 | mPendingBroadcast = null; |
| 11323 | } |
| 11324 | } |
| 11325 | |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11326 | boolean looped = false; |
| 11327 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11328 | do { |
| 11329 | if (mOrderedBroadcasts.size() == 0) { |
| 11330 | // No more broadcasts pending, so all done! |
| 11331 | scheduleAppGcsLocked(); |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11332 | if (looped) { |
| 11333 | // If we had finished the last ordered broadcast, then |
| 11334 | // make sure all processes have correct oom and sched |
| 11335 | // adjustments. |
| 11336 | updateOomAdjLocked(); |
| 11337 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11338 | return; |
| 11339 | } |
| 11340 | r = mOrderedBroadcasts.get(0); |
| 11341 | boolean forceReceive = false; |
| 11342 | |
| 11343 | // Ensure that even if something goes awry with the timeout |
| 11344 | // detection, we catch "hung" broadcasts here, discard them, |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 11345 | // and continue to make progress. |
| 11346 | // |
| 11347 | // 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] | 11348 | // receivers don't get executed with timeouts. They're intended for |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 11349 | // one time heavy lifting after system upgrades and can take |
| 11350 | // significant amounts of time. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11351 | int numReceivers = (r.receivers != null) ? r.receivers.size() : 0; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11352 | if (mProcessesReady && r.dispatchTime > 0) { |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 11353 | long now = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11354 | if ((numReceivers > 0) && |
| 11355 | (now > r.dispatchTime + (2*BROADCAST_TIMEOUT*numReceivers))) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11356 | Slog.w(TAG, "Hung broadcast discarded after timeout failure:" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11357 | + " now=" + now |
| 11358 | + " dispatchTime=" + r.dispatchTime |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11359 | + " startTime=" + r.receiverTime |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11360 | + " intent=" + r.intent |
| 11361 | + " numReceivers=" + numReceivers |
| 11362 | + " nextReceiver=" + r.nextReceiver |
| 11363 | + " state=" + r.state); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11364 | broadcastTimeoutLocked(false); // forcibly finish this broadcast |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11365 | forceReceive = true; |
| 11366 | r.state = BroadcastRecord.IDLE; |
| 11367 | } |
| 11368 | } |
| 11369 | |
| 11370 | if (r.state != BroadcastRecord.IDLE) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11371 | if (DEBUG_BROADCAST) Slog.d(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11372 | "processNextBroadcast() called when not idle (state=" |
| 11373 | + r.state + ")"); |
| 11374 | return; |
| 11375 | } |
| 11376 | |
| 11377 | if (r.receivers == null || r.nextReceiver >= numReceivers |
| 11378 | || r.resultAbort || forceReceive) { |
| 11379 | // No more receivers for this broadcast! Send the final |
| 11380 | // result if requested... |
| 11381 | if (r.resultTo != null) { |
| 11382 | try { |
| 11383 | if (DEBUG_BROADCAST) { |
| 11384 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11385 | Slog.i(TAG, "Finishing broadcast " + r.intent.getAction() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11386 | + " seq=" + seq + " app=" + r.callerApp); |
| 11387 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11388 | performReceiveLocked(r.callerApp, r.resultTo, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11389 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 11390 | r.resultData, r.resultExtras, false, false); |
Johannes Carlsson | b5a8654 | 2010-10-27 10:08:10 +0200 | [diff] [blame] | 11391 | // Set this to null so that the reference |
| 11392 | // (local and remote) isnt kept in the mBroadcastHistory. |
| 11393 | r.resultTo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11394 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11395 | 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] | 11396 | } |
| 11397 | } |
| 11398 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11399 | if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG"); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11400 | cancelBroadcastTimeoutLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11401 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11402 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11403 | + r); |
| 11404 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11405 | // ... and on to the next... |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11406 | addBroadcastToHistoryLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11407 | mOrderedBroadcasts.remove(0); |
| 11408 | r = null; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11409 | looped = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11410 | continue; |
| 11411 | } |
| 11412 | } while (r == null); |
| 11413 | |
| 11414 | // Get the next receiver... |
| 11415 | int recIdx = r.nextReceiver++; |
| 11416 | |
| 11417 | // Keep track of when this receiver started, and make sure there |
| 11418 | // is a timeout message pending to kill it if need be. |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11419 | r.receiverTime = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11420 | if (recIdx == 0) { |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11421 | r.dispatchTime = r.receiverTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11422 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11423 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 11424 | + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11425 | } |
| 11426 | if (! mPendingBroadcastTimeoutMessage) { |
| 11427 | long timeoutTime = r.receiverTime + BROADCAST_TIMEOUT; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11428 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11429 | "Submitting BROADCAST_TIMEOUT_MSG for " + r + " at " + timeoutTime); |
| 11430 | setBroadcastTimeoutLocked(timeoutTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11431 | } |
| 11432 | |
| 11433 | Object nextReceiver = r.receivers.get(recIdx); |
| 11434 | if (nextReceiver instanceof BroadcastFilter) { |
| 11435 | // Simple case: this is a registered receiver who gets |
| 11436 | // a direct call. |
| 11437 | BroadcastFilter filter = (BroadcastFilter)nextReceiver; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11438 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 11439 | "Delivering ordered to registered " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11440 | + filter + ": " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11441 | deliverToRegisteredReceiverLocked(r, filter, r.ordered); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11442 | if (r.receiver == null || !r.ordered) { |
| 11443 | // The receiver has already finished, so schedule to |
| 11444 | // process the next one. |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 11445 | if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing: ordered=" |
| 11446 | + r.ordered + " receiver=" + r.receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11447 | r.state = BroadcastRecord.IDLE; |
| 11448 | scheduleBroadcastsLocked(); |
| 11449 | } |
| 11450 | return; |
| 11451 | } |
| 11452 | |
| 11453 | // Hard case: need to instantiate the receiver, possibly |
| 11454 | // starting its application process to host it. |
| 11455 | |
| 11456 | ResolveInfo info = |
| 11457 | (ResolveInfo)nextReceiver; |
| 11458 | |
| 11459 | boolean skip = false; |
| 11460 | int perm = checkComponentPermission(info.activityInfo.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11461 | r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid, |
| 11462 | info.activityInfo.exported); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11463 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11464 | if (!info.activityInfo.exported) { |
| 11465 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 11466 | + r.intent.toString() |
| 11467 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 11468 | + ", uid=" + r.callingUid + ")" |
| 11469 | + " is not exported from uid " + info.activityInfo.applicationInfo.uid |
| 11470 | + " due to receiver " + info.activityInfo.packageName |
| 11471 | + "/" + info.activityInfo.name); |
| 11472 | } else { |
| 11473 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 11474 | + r.intent.toString() |
| 11475 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 11476 | + ", uid=" + r.callingUid + ")" |
| 11477 | + " requires " + info.activityInfo.permission |
| 11478 | + " due to receiver " + info.activityInfo.packageName |
| 11479 | + "/" + info.activityInfo.name); |
| 11480 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11481 | skip = true; |
| 11482 | } |
| 11483 | if (r.callingUid != Process.SYSTEM_UID && |
| 11484 | r.requiredPermission != null) { |
| 11485 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11486 | perm = AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11487 | checkPermission(r.requiredPermission, |
| 11488 | info.activityInfo.applicationInfo.packageName); |
| 11489 | } catch (RemoteException e) { |
| 11490 | perm = PackageManager.PERMISSION_DENIED; |
| 11491 | } |
| 11492 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11493 | Slog.w(TAG, "Permission Denial: receiving " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11494 | + r.intent + " to " |
| 11495 | + info.activityInfo.applicationInfo.packageName |
| 11496 | + " requires " + r.requiredPermission |
| 11497 | + " due to sender " + r.callerPackage |
| 11498 | + " (uid " + r.callingUid + ")"); |
| 11499 | skip = true; |
| 11500 | } |
| 11501 | } |
| 11502 | if (r.curApp != null && r.curApp.crashing) { |
| 11503 | // If the target process is crashing, just skip it. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11504 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 11505 | "Skipping deliver ordered " + r + " to " + r.curApp |
| 11506 | + ": process crashing"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11507 | skip = true; |
| 11508 | } |
| 11509 | |
| 11510 | if (skip) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11511 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 11512 | "Skipping delivery of ordered " + r + " for whatever reason"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11513 | r.receiver = null; |
| 11514 | r.curFilter = null; |
| 11515 | r.state = BroadcastRecord.IDLE; |
| 11516 | scheduleBroadcastsLocked(); |
| 11517 | return; |
| 11518 | } |
| 11519 | |
| 11520 | r.state = BroadcastRecord.APP_RECEIVE; |
| 11521 | String targetProcess = info.activityInfo.processName; |
| 11522 | r.curComponent = new ComponentName( |
| 11523 | info.activityInfo.applicationInfo.packageName, |
| 11524 | info.activityInfo.name); |
| 11525 | r.curReceiver = info.activityInfo; |
| 11526 | |
| 11527 | // Is this receiver's application already running? |
| 11528 | ProcessRecord app = getProcessRecordLocked(targetProcess, |
| 11529 | info.activityInfo.applicationInfo.uid); |
| 11530 | if (app != null && app.thread != null) { |
| 11531 | try { |
| 11532 | processCurBroadcastLocked(r, app); |
| 11533 | return; |
| 11534 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11535 | Slog.w(TAG, "Exception when sending broadcast to " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11536 | + r.curComponent, e); |
| 11537 | } |
| 11538 | |
| 11539 | // If a dead object exception was thrown -- fall through to |
| 11540 | // restart the application. |
| 11541 | } |
| 11542 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 11543 | // 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] | 11544 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 11545 | "Need to start app " + targetProcess + " for broadcast " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11546 | if ((r.curApp=startProcessLocked(targetProcess, |
| 11547 | info.activityInfo.applicationInfo, true, |
| 11548 | r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 11549 | "broadcast", r.curComponent, |
| 11550 | (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0)) |
| 11551 | == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11552 | // Ah, this recipient is unavailable. Finish it if necessary, |
| 11553 | // and mark the broadcast record as ready for the next. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11554 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11555 | + info.activityInfo.applicationInfo.packageName + "/" |
| 11556 | + info.activityInfo.applicationInfo.uid + " for broadcast " |
| 11557 | + r.intent + ": process is bad"); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11558 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11559 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 11560 | r.resultExtras, r.resultAbort, true); |
| 11561 | scheduleBroadcastsLocked(); |
| 11562 | r.state = BroadcastRecord.IDLE; |
| 11563 | return; |
| 11564 | } |
| 11565 | |
| 11566 | mPendingBroadcast = r; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11567 | mPendingBroadcastRecvIndex = recIdx; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11568 | } |
| 11569 | } |
| 11570 | |
| 11571 | // ========================================================= |
| 11572 | // INSTRUMENTATION |
| 11573 | // ========================================================= |
| 11574 | |
| 11575 | public boolean startInstrumentation(ComponentName className, |
| 11576 | String profileFile, int flags, Bundle arguments, |
| 11577 | IInstrumentationWatcher watcher) { |
| 11578 | // Refuse possible leaked file descriptors |
| 11579 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 11580 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 11581 | } |
| 11582 | |
| 11583 | synchronized(this) { |
| 11584 | InstrumentationInfo ii = null; |
| 11585 | ApplicationInfo ai = null; |
| 11586 | try { |
| 11587 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11588 | className, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11589 | ai = mContext.getPackageManager().getApplicationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11590 | ii.targetPackage, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11591 | } catch (PackageManager.NameNotFoundException e) { |
| 11592 | } |
| 11593 | if (ii == null) { |
| 11594 | reportStartInstrumentationFailure(watcher, className, |
| 11595 | "Unable to find instrumentation info for: " + className); |
| 11596 | return false; |
| 11597 | } |
| 11598 | if (ai == null) { |
| 11599 | reportStartInstrumentationFailure(watcher, className, |
| 11600 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 11601 | return false; |
| 11602 | } |
| 11603 | |
| 11604 | int match = mContext.getPackageManager().checkSignatures( |
| 11605 | ii.targetPackage, ii.packageName); |
| 11606 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 11607 | String msg = "Permission Denial: starting instrumentation " |
| 11608 | + className + " from pid=" |
| 11609 | + Binder.getCallingPid() |
| 11610 | + ", uid=" + Binder.getCallingPid() |
| 11611 | + " not allowed because package " + ii.packageName |
| 11612 | + " does not have a signature matching the target " |
| 11613 | + ii.targetPackage; |
| 11614 | reportStartInstrumentationFailure(watcher, className, msg); |
| 11615 | throw new SecurityException(msg); |
| 11616 | } |
| 11617 | |
| 11618 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 11619 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11620 | ProcessRecord app = addAppLocked(ai); |
| 11621 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11622 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11623 | app.instrumentationProfileFile = profileFile; |
| 11624 | app.instrumentationArguments = arguments; |
| 11625 | app.instrumentationWatcher = watcher; |
| 11626 | app.instrumentationResultClass = className; |
| 11627 | Binder.restoreCallingIdentity(origId); |
| 11628 | } |
| 11629 | |
| 11630 | return true; |
| 11631 | } |
| 11632 | |
| 11633 | /** |
| 11634 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 11635 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 11636 | * the "am" command to report errors with more information. |
| 11637 | * |
| 11638 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 11639 | * @param cn The component name of the instrumentation. |
| 11640 | * @param report The error report. |
| 11641 | */ |
| 11642 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 11643 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11644 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11645 | try { |
| 11646 | if (watcher != null) { |
| 11647 | Bundle results = new Bundle(); |
| 11648 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 11649 | results.putString("Error", report); |
| 11650 | watcher.instrumentationStatus(cn, -1, results); |
| 11651 | } |
| 11652 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11653 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11654 | } |
| 11655 | } |
| 11656 | |
| 11657 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 11658 | if (app.instrumentationWatcher != null) { |
| 11659 | try { |
| 11660 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 11661 | app.instrumentationWatcher.instrumentationFinished( |
| 11662 | app.instrumentationClass, |
| 11663 | resultCode, |
| 11664 | results); |
| 11665 | } catch (RemoteException e) { |
| 11666 | } |
| 11667 | } |
| 11668 | app.instrumentationWatcher = null; |
| 11669 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11670 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11671 | app.instrumentationProfileFile = null; |
| 11672 | app.instrumentationArguments = null; |
| 11673 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 11674 | forceStopPackageLocked(app.processName, -1, false, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11675 | } |
| 11676 | |
| 11677 | public void finishInstrumentation(IApplicationThread target, |
| 11678 | int resultCode, Bundle results) { |
| 11679 | // Refuse possible leaked file descriptors |
| 11680 | if (results != null && results.hasFileDescriptors()) { |
| 11681 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11682 | } |
| 11683 | |
| 11684 | synchronized(this) { |
| 11685 | ProcessRecord app = getRecordForAppLocked(target); |
| 11686 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11687 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11688 | return; |
| 11689 | } |
| 11690 | final long origId = Binder.clearCallingIdentity(); |
| 11691 | finishInstrumentationLocked(app, resultCode, results); |
| 11692 | Binder.restoreCallingIdentity(origId); |
| 11693 | } |
| 11694 | } |
| 11695 | |
| 11696 | // ========================================================= |
| 11697 | // CONFIGURATION |
| 11698 | // ========================================================= |
| 11699 | |
| 11700 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 11701 | ConfigurationInfo config = new ConfigurationInfo(); |
| 11702 | synchronized (this) { |
| 11703 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 11704 | config.reqKeyboardType = mConfiguration.keyboard; |
| 11705 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 11706 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 11707 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11708 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 11709 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 11710 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 11711 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11712 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 11713 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 11714 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11715 | } |
| 11716 | return config; |
| 11717 | } |
| 11718 | |
| 11719 | public Configuration getConfiguration() { |
| 11720 | Configuration ci; |
| 11721 | synchronized(this) { |
| 11722 | ci = new Configuration(mConfiguration); |
| 11723 | } |
| 11724 | return ci; |
| 11725 | } |
| 11726 | |
| 11727 | public void updateConfiguration(Configuration values) { |
| 11728 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 11729 | "updateConfiguration()"); |
| 11730 | |
| 11731 | synchronized(this) { |
| 11732 | if (values == null && mWindowManager != null) { |
| 11733 | // sentinel: fetch the current configuration from the window manager |
| 11734 | values = mWindowManager.computeNewConfiguration(); |
| 11735 | } |
| 11736 | |
| 11737 | final long origId = Binder.clearCallingIdentity(); |
| 11738 | updateConfigurationLocked(values, null); |
| 11739 | Binder.restoreCallingIdentity(origId); |
| 11740 | } |
| 11741 | } |
| 11742 | |
| 11743 | /** |
| 11744 | * Do either or both things: (1) change the current configuration, and (2) |
| 11745 | * make sure the given activity is running with the (now) current |
| 11746 | * configuration. Returns true if the activity has been left running, or |
| 11747 | * false if <var>starting</var> is being destroyed to match the new |
| 11748 | * configuration. |
| 11749 | */ |
| 11750 | public boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11751 | ActivityRecord starting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11752 | int changes = 0; |
| 11753 | |
| 11754 | boolean kept = true; |
| 11755 | |
| 11756 | if (values != null) { |
| 11757 | Configuration newConfig = new Configuration(mConfiguration); |
| 11758 | changes = newConfig.updateFrom(values); |
| 11759 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 11760 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11761 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11762 | } |
| 11763 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 11764 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11765 | |
| 11766 | if (values.locale != null) { |
| 11767 | saveLocaleLocked(values.locale, |
| 11768 | !values.locale.equals(mConfiguration.locale), |
| 11769 | values.userSetLocale); |
| 11770 | } |
| 11771 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 11772 | mConfigurationSeq++; |
| 11773 | if (mConfigurationSeq <= 0) { |
| 11774 | mConfigurationSeq = 1; |
| 11775 | } |
| 11776 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11777 | mConfiguration = newConfig; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11778 | Slog.i(TAG, "Config changed: " + newConfig); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 11779 | |
| 11780 | AttributeCache ac = AttributeCache.instance(); |
| 11781 | if (ac != null) { |
| 11782 | ac.updateConfiguration(mConfiguration); |
| 11783 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11784 | |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 11785 | if (Settings.System.hasInterestingConfigurationChanges(changes)) { |
| 11786 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
| 11787 | msg.obj = new Configuration(mConfiguration); |
| 11788 | mHandler.sendMessage(msg); |
| 11789 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11790 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11791 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 11792 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11793 | try { |
| 11794 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11795 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 11796 | + app.processName + " new config " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11797 | app.thread.scheduleConfigurationChanged(mConfiguration); |
| 11798 | } |
| 11799 | } catch (Exception e) { |
| 11800 | } |
| 11801 | } |
| 11802 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11803 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 11804 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11805 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
| 11806 | null, false, false, MY_PID, Process.SYSTEM_UID); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 11807 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
| 11808 | broadcastIntentLocked(null, null, |
| 11809 | new Intent(Intent.ACTION_LOCALE_CHANGED), |
| 11810 | null, null, 0, null, null, |
| 11811 | null, false, false, MY_PID, Process.SYSTEM_UID); |
| 11812 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11813 | } |
| 11814 | } |
| 11815 | |
| 11816 | if (changes != 0 && starting == null) { |
| 11817 | // If the configuration changed, and the caller is not already |
| 11818 | // in the process of starting an activity, then find the top |
| 11819 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 11820 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11821 | } |
| 11822 | |
| 11823 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 11824 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 11825 | // And we need to make sure at this point that all other activities |
| 11826 | // are made visible with the correct configuration. |
| 11827 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11828 | } |
| 11829 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 11830 | if (values != null && mWindowManager != null) { |
| 11831 | mWindowManager.setNewConfiguration(mConfiguration); |
| 11832 | } |
| 11833 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11834 | return kept; |
| 11835 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11836 | |
| 11837 | /** |
| 11838 | * Save the locale. You must be inside a synchronized (this) block. |
| 11839 | */ |
| 11840 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 11841 | if(isDiff) { |
| 11842 | SystemProperties.set("user.language", l.getLanguage()); |
| 11843 | SystemProperties.set("user.region", l.getCountry()); |
| 11844 | } |
| 11845 | |
| 11846 | if(isPersist) { |
| 11847 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 11848 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 11849 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 11850 | } |
| 11851 | } |
| 11852 | |
| 11853 | // ========================================================= |
| 11854 | // LIFETIME MANAGEMENT |
| 11855 | // ========================================================= |
| 11856 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11857 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, |
| 11858 | ProcessRecord TOP_APP, boolean recursed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11859 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11860 | // This adjustment has already been computed. If we are calling |
| 11861 | // from the top, we may have already computed our adjustment with |
| 11862 | // an earlier hidden adjustment that isn't really for us... if |
| 11863 | // so, use the new hidden adjustment. |
| 11864 | if (!recursed && app.hidden) { |
| 11865 | app.curAdj = hiddenAdj; |
| 11866 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11867 | return app.curAdj; |
| 11868 | } |
| 11869 | |
| 11870 | if (app.thread == null) { |
| 11871 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11872 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11873 | return (app.curAdj=EMPTY_APP_ADJ); |
| 11874 | } |
| 11875 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11876 | if (app.maxAdj <= FOREGROUND_APP_ADJ) { |
| 11877 | // The max adjustment doesn't allow this app to be anything |
| 11878 | // below foreground, so it is not worth doing work for it. |
| 11879 | app.adjType = "fixed"; |
| 11880 | app.adjSeq = mAdjSeq; |
| 11881 | app.curRawAdj = app.maxAdj; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11882 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11883 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 11884 | return (app.curAdj=app.maxAdj); |
| 11885 | } |
| 11886 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11887 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11888 | app.adjSource = null; |
| 11889 | app.adjTarget = null; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11890 | app.keeping = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11891 | app.empty = false; |
| 11892 | app.hidden = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11893 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 11894 | // Determine the importance of the process, starting with most |
| 11895 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11896 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11897 | int schedGroup; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11898 | int N; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11899 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11900 | // The last app on the list is the foreground app. |
| 11901 | adj = FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11902 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 11903 | app.adjType = "top-activity"; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11904 | } else if (app.instrumentationClass != null) { |
| 11905 | // Don't want to kill running instrumentation. |
| 11906 | adj = FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11907 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 11908 | app.adjType = "instrumentation"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11909 | } else if (app.curReceiver != null || |
| 11910 | (mPendingBroadcast != null && mPendingBroadcast.curApp == app)) { |
| 11911 | // An app that is currently receiving a broadcast also |
| 11912 | // counts as being in the foreground. |
| 11913 | adj = FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11914 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11915 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11916 | } else if (app.executingServices.size() > 0) { |
| 11917 | // An app that is currently executing a service callback also |
| 11918 | // counts as being in the foreground. |
| 11919 | adj = FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11920 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11921 | app.adjType = "exec-service"; |
| 11922 | } else if (app.foregroundServices) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11923 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 11924 | adj = PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11925 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11926 | app.adjType = "foreground-service"; |
| 11927 | } else if (app.forcingToForeground != null) { |
| 11928 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 11929 | adj = PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11930 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11931 | app.adjType = "force-foreground"; |
| 11932 | app.adjSource = app.forcingToForeground; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 11933 | } else if (app == mHeavyWeightProcess) { |
| 11934 | // We don't want to kill the current heavy-weight process. |
| 11935 | adj = HEAVY_WEIGHT_APP_ADJ; |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 11936 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 11937 | app.adjType = "heavy"; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 11938 | } else if (app == mHomeProcess) { |
| 11939 | // This process is hosting what we currently consider to be the |
| 11940 | // home app, so we don't want to let it go into the background. |
| 11941 | adj = HOME_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11942 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11943 | app.adjType = "home"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11944 | } else if ((N=app.activities.size()) != 0) { |
| 11945 | // This app is in the background with paused activities. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11946 | app.hidden = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11947 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11948 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 11949 | app.adjType = "bg-activities"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11950 | N = app.activities.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11951 | for (int j=0; j<N; j++) { |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 11952 | ActivityRecord r = app.activities.get(j); |
| 11953 | if (r.visible) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11954 | // This app has a visible activity! |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11955 | app.hidden = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11956 | adj = VISIBLE_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11957 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11958 | app.adjType = "visible"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11959 | break; |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 11960 | } else if (r.state == ActivityState.PAUSING |
| 11961 | || r.state == ActivityState.PAUSED |
| 11962 | || r.state == ActivityState.STOPPING) { |
| 11963 | adj = PERCEPTIBLE_APP_ADJ; |
| 11964 | app.adjType = "stopping"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11965 | } |
| 11966 | } |
| 11967 | } else { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11968 | // A very not-needed process. If this is lower in the lru list, |
| 11969 | // we will push it in to the empty bucket. |
| 11970 | app.hidden = true; |
| 11971 | app.empty = true; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11972 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11973 | adj = hiddenAdj; |
| 11974 | app.adjType = "bg-empty"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11975 | } |
| 11976 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11977 | //Slog.i(TAG, "OOM " + app + ": initial adj=" + adj); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11978 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 11979 | // 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] | 11980 | // there are applications dependent on our services or providers, but |
| 11981 | // this gives us a baseline and makes sure we don't get into an |
| 11982 | // infinite recursion. |
| 11983 | app.adjSeq = mAdjSeq; |
| 11984 | app.curRawAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11985 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11986 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 11987 | // If possible we want to avoid killing apps while they're being backed up |
| 11988 | if (adj > BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11989 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11990 | adj = BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11991 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11992 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11993 | } |
| 11994 | } |
| 11995 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 11996 | if (app.services.size() != 0 && (adj > FOREGROUND_APP_ADJ |
| 11997 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11998 | final long now = SystemClock.uptimeMillis(); |
| 11999 | // This process is more important if the top activity is |
| 12000 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12001 | Iterator<ServiceRecord> jt = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12002 | while (jt.hasNext() && adj > FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12003 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12004 | if (s.startRequested) { |
| 12005 | if (now < (s.lastActivity+MAX_SERVICE_INACTIVITY)) { |
| 12006 | // This service has seen some activity within |
| 12007 | // recent memory, so we will keep its process ahead |
| 12008 | // of the background processes. |
| 12009 | if (adj > SECONDARY_SERVER_ADJ) { |
| 12010 | adj = SECONDARY_SERVER_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12011 | app.adjType = "started-services"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12012 | app.hidden = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12013 | } |
| 12014 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12015 | // If we have let the service slide into the background |
| 12016 | // state, still have some text describing what it is doing |
| 12017 | // even though the service no longer has an impact. |
| 12018 | if (adj > SECONDARY_SERVER_ADJ) { |
| 12019 | app.adjType = "started-bg-services"; |
| 12020 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12021 | // Don't kill this process because it is doing work; it |
| 12022 | // has said it is doing work. |
| 12023 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12024 | } |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12025 | if (s.connections.size() > 0 && (adj > FOREGROUND_APP_ADJ |
| 12026 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12027 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12028 | = s.connections.values().iterator(); |
| 12029 | while (kt.hasNext() && adj > FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12030 | ArrayList<ConnectionRecord> clist = kt.next(); |
| 12031 | for (int i=0; i<clist.size() && adj > FOREGROUND_APP_ADJ; i++) { |
| 12032 | // XXX should compute this based on the max of |
| 12033 | // all connected clients. |
| 12034 | ConnectionRecord cr = clist.get(i); |
| 12035 | if (cr.binding.client == app) { |
| 12036 | // Binding to ourself is not interesting. |
| 12037 | continue; |
| 12038 | } |
| 12039 | if ((cr.flags&Context.BIND_AUTO_CREATE) != 0) { |
| 12040 | ProcessRecord client = cr.binding.client; |
| 12041 | int myHiddenAdj = hiddenAdj; |
| 12042 | if (myHiddenAdj > client.hiddenAdj) { |
| 12043 | if (client.hiddenAdj >= VISIBLE_APP_ADJ) { |
| 12044 | myHiddenAdj = client.hiddenAdj; |
| 12045 | } else { |
| 12046 | myHiddenAdj = VISIBLE_APP_ADJ; |
| 12047 | } |
| 12048 | } |
| 12049 | int clientAdj = computeOomAdjLocked( |
| 12050 | client, myHiddenAdj, TOP_APP, true); |
| 12051 | if (adj > clientAdj) { |
| 12052 | adj = clientAdj >= VISIBLE_APP_ADJ |
| 12053 | ? clientAdj : VISIBLE_APP_ADJ; |
| 12054 | if (!client.hidden) { |
| 12055 | app.hidden = false; |
| 12056 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12057 | if (client.keeping) { |
| 12058 | app.keeping = true; |
| 12059 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12060 | app.adjType = "service"; |
| 12061 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12062 | .REASON_SERVICE_IN_USE; |
| 12063 | app.adjSource = cr.binding.client; |
| 12064 | app.adjTarget = s.name; |
| 12065 | } |
| 12066 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 12067 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 12068 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12069 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12070 | } |
| 12071 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12072 | ActivityRecord a = cr.activity; |
| 12073 | //if (a != null) { |
| 12074 | // Slog.i(TAG, "Connection to " + a ": state=" + a.state); |
| 12075 | //} |
| 12076 | if (a != null && adj > FOREGROUND_APP_ADJ && |
| 12077 | (a.state == ActivityState.RESUMED |
| 12078 | || a.state == ActivityState.PAUSING)) { |
| 12079 | adj = FOREGROUND_APP_ADJ; |
| 12080 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12081 | app.hidden = false; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12082 | app.adjType = "service"; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 12083 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12084 | .REASON_SERVICE_IN_USE; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12085 | app.adjSource = a; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 12086 | app.adjTarget = s.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12087 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12088 | } |
| 12089 | } |
| 12090 | } |
| 12091 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12092 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12093 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12094 | // would like to avoid killing it unless it would prevent the current |
| 12095 | // application from running. By default we put the process in |
| 12096 | // with the rest of the background processes; as we scan through |
| 12097 | // its services we may bump it up from there. |
| 12098 | if (adj > hiddenAdj) { |
| 12099 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12100 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12101 | app.adjType = "bg-services"; |
| 12102 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12103 | } |
| 12104 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12105 | if (app.pubProviders.size() != 0 && (adj > FOREGROUND_APP_ADJ |
| 12106 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12107 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12108 | while (jt.hasNext() && (adj > FOREGROUND_APP_ADJ |
| 12109 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12110 | ContentProviderRecord cpr = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12111 | if (cpr.clients.size() != 0) { |
| 12112 | Iterator<ProcessRecord> kt = cpr.clients.iterator(); |
| 12113 | while (kt.hasNext() && adj > FOREGROUND_APP_ADJ) { |
| 12114 | ProcessRecord client = kt.next(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 12115 | if (client == app) { |
| 12116 | // Being our own client is not interesting. |
| 12117 | continue; |
| 12118 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12119 | int myHiddenAdj = hiddenAdj; |
| 12120 | if (myHiddenAdj > client.hiddenAdj) { |
| 12121 | if (client.hiddenAdj > FOREGROUND_APP_ADJ) { |
| 12122 | myHiddenAdj = client.hiddenAdj; |
| 12123 | } else { |
| 12124 | myHiddenAdj = FOREGROUND_APP_ADJ; |
| 12125 | } |
| 12126 | } |
| 12127 | int clientAdj = computeOomAdjLocked( |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12128 | client, myHiddenAdj, TOP_APP, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12129 | if (adj > clientAdj) { |
| 12130 | adj = clientAdj > FOREGROUND_APP_ADJ |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12131 | ? clientAdj : FOREGROUND_APP_ADJ; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12132 | if (!client.hidden) { |
| 12133 | app.hidden = false; |
| 12134 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12135 | if (client.keeping) { |
| 12136 | app.keeping = true; |
| 12137 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12138 | app.adjType = "provider"; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 12139 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12140 | .REASON_PROVIDER_IN_USE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12141 | app.adjSource = client; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 12142 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12143 | } |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12144 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 12145 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12146 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12147 | } |
| 12148 | } |
| 12149 | // If the provider has external (non-framework) process |
| 12150 | // dependencies, ensure that its adjustment is at least |
| 12151 | // FOREGROUND_APP_ADJ. |
| 12152 | if (cpr.externals != 0) { |
| 12153 | if (adj > FOREGROUND_APP_ADJ) { |
| 12154 | adj = FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12155 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12156 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12157 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12158 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 12159 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12160 | } |
| 12161 | } |
| 12162 | } |
| 12163 | } |
| 12164 | |
| 12165 | app.curRawAdj = adj; |
| 12166 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12167 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12168 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 12169 | if (adj > app.maxAdj) { |
| 12170 | adj = app.maxAdj; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 12171 | if (app.maxAdj <= PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12172 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12173 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12174 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12175 | if (adj < HIDDEN_APP_MIN_ADJ) { |
| 12176 | app.keeping = true; |
| 12177 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12178 | |
| 12179 | app.curAdj = adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12180 | app.curSchedGroup = schedGroup; |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 12181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12182 | return adj; |
| 12183 | } |
| 12184 | |
| 12185 | /** |
| 12186 | * Ask a given process to GC right now. |
| 12187 | */ |
| 12188 | final void performAppGcLocked(ProcessRecord app) { |
| 12189 | try { |
| 12190 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 12191 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12192 | if (app.reportLowMemory) { |
| 12193 | app.reportLowMemory = false; |
| 12194 | app.thread.scheduleLowMemory(); |
| 12195 | } else { |
| 12196 | app.thread.processInBackground(); |
| 12197 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12198 | } |
| 12199 | } catch (Exception e) { |
| 12200 | // whatever. |
| 12201 | } |
| 12202 | } |
| 12203 | |
| 12204 | /** |
| 12205 | * Returns true if things are idle enough to perform GCs. |
| 12206 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12207 | private final boolean canGcNowLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12208 | return mParallelBroadcasts.size() == 0 |
| 12209 | && mOrderedBroadcasts.size() == 0 |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12210 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 12211 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12212 | } |
| 12213 | |
| 12214 | /** |
| 12215 | * Perform GCs on all processes that are waiting for it, but only |
| 12216 | * if things are idle. |
| 12217 | */ |
| 12218 | final void performAppGcsLocked() { |
| 12219 | final int N = mProcessesToGc.size(); |
| 12220 | if (N <= 0) { |
| 12221 | return; |
| 12222 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12223 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12224 | while (mProcessesToGc.size() > 0) { |
| 12225 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 12226 | if (proc.curRawAdj > PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12227 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 12228 | <= SystemClock.uptimeMillis()) { |
| 12229 | // To avoid spamming the system, we will GC processes one |
| 12230 | // at a time, waiting a few seconds between each. |
| 12231 | performAppGcLocked(proc); |
| 12232 | scheduleAppGcsLocked(); |
| 12233 | return; |
| 12234 | } else { |
| 12235 | // It hasn't been long enough since we last GCed this |
| 12236 | // process... put it in the list to wait for its time. |
| 12237 | addProcessToGcListLocked(proc); |
| 12238 | break; |
| 12239 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12240 | } |
| 12241 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12242 | |
| 12243 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12244 | } |
| 12245 | } |
| 12246 | |
| 12247 | /** |
| 12248 | * If all looks good, perform GCs on all processes waiting for them. |
| 12249 | */ |
| 12250 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12251 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12252 | performAppGcsLocked(); |
| 12253 | return; |
| 12254 | } |
| 12255 | // Still not idle, wait some more. |
| 12256 | scheduleAppGcsLocked(); |
| 12257 | } |
| 12258 | |
| 12259 | /** |
| 12260 | * Schedule the execution of all pending app GCs. |
| 12261 | */ |
| 12262 | final void scheduleAppGcsLocked() { |
| 12263 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12264 | |
| 12265 | if (mProcessesToGc.size() > 0) { |
| 12266 | // Schedule a GC for the time to the next process. |
| 12267 | ProcessRecord proc = mProcessesToGc.get(0); |
| 12268 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 12269 | |
| 12270 | long when = mProcessesToGc.get(0).lastRequestedGc + GC_MIN_INTERVAL; |
| 12271 | long now = SystemClock.uptimeMillis(); |
| 12272 | if (when < (now+GC_TIMEOUT)) { |
| 12273 | when = now + GC_TIMEOUT; |
| 12274 | } |
| 12275 | mHandler.sendMessageAtTime(msg, when); |
| 12276 | } |
| 12277 | } |
| 12278 | |
| 12279 | /** |
| 12280 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 12281 | * list in sorted order by the last GC time. The process can't already be |
| 12282 | * on the list. |
| 12283 | */ |
| 12284 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 12285 | boolean added = false; |
| 12286 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 12287 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 12288 | proc.lastRequestedGc) { |
| 12289 | added = true; |
| 12290 | mProcessesToGc.add(i+1, proc); |
| 12291 | break; |
| 12292 | } |
| 12293 | } |
| 12294 | if (!added) { |
| 12295 | mProcessesToGc.add(0, proc); |
| 12296 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12297 | } |
| 12298 | |
| 12299 | /** |
| 12300 | * Set up to ask a process to GC itself. This will either do it |
| 12301 | * immediately, or put it on the list of processes to gc the next |
| 12302 | * time things are idle. |
| 12303 | */ |
| 12304 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 12305 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12306 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12307 | return; |
| 12308 | } |
| 12309 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12310 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12311 | scheduleAppGcsLocked(); |
| 12312 | } |
| 12313 | } |
| 12314 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12315 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 12316 | updateCpuStatsNow(); |
| 12317 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12318 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12319 | boolean doWakeKills = doKills; |
| 12320 | boolean doCpuKills = doKills; |
| 12321 | if (mLastPowerCheckRealtime == 0) { |
| 12322 | doWakeKills = false; |
| 12323 | } |
| 12324 | if (mLastPowerCheckUptime == 0) { |
| 12325 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12326 | } |
| 12327 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12328 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12329 | } |
| 12330 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12331 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 12332 | final long curUptime = SystemClock.uptimeMillis(); |
| 12333 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 12334 | mLastPowerCheckRealtime = curRealtime; |
| 12335 | mLastPowerCheckUptime = curUptime; |
| 12336 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 12337 | doWakeKills = false; |
| 12338 | } |
| 12339 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 12340 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12341 | } |
| 12342 | int i = mLruProcesses.size(); |
| 12343 | while (i > 0) { |
| 12344 | i--; |
| 12345 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12346 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12347 | long wtime; |
| 12348 | synchronized (stats) { |
| 12349 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 12350 | app.pid, curRealtime); |
| 12351 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12352 | long wtimeUsed = wtime - app.lastWakeTime; |
| 12353 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 12354 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 12355 | StringBuilder sb = new StringBuilder(128); |
| 12356 | sb.append("Wake for "); |
| 12357 | app.toShortString(sb); |
| 12358 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12359 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 12360 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12361 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 12362 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12363 | sb.append((wtimeUsed*100)/realtimeSince); |
| 12364 | sb.append("%)"); |
| 12365 | Slog.i(TAG, sb.toString()); |
| 12366 | sb.setLength(0); |
| 12367 | sb.append("CPU for "); |
| 12368 | app.toShortString(sb); |
| 12369 | sb.append(": over "); |
| 12370 | TimeUtils.formatDuration(uptimeSince, sb); |
| 12371 | sb.append(" used "); |
| 12372 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 12373 | sb.append(" ("); |
| 12374 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 12375 | sb.append("%)"); |
| 12376 | Slog.i(TAG, sb.toString()); |
| 12377 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12378 | // If a process has held a wake lock for more |
| 12379 | // than 50% of the time during this period, |
| 12380 | // that sounds pad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12381 | if (doWakeKills && realtimeSince > 0 |
| 12382 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 12383 | synchronized (stats) { |
| 12384 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 12385 | realtimeSince, wtimeUsed); |
| 12386 | } |
| 12387 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 12388 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 12389 | + " during " + realtimeSince); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12390 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 12391 | app.processName, app.setAdj, "excessive wake lock"); |
| 12392 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12393 | } else if (doCpuKills && uptimeSince > 0 |
| 12394 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 12395 | synchronized (stats) { |
| 12396 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 12397 | uptimeSince, cputimeUsed); |
| 12398 | } |
| 12399 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 12400 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 12401 | + " during " + uptimeSince); |
| 12402 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 12403 | app.processName, app.setAdj, "excessive cpu"); |
| 12404 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12405 | } else { |
| 12406 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12407 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12408 | } |
| 12409 | } |
| 12410 | } |
| 12411 | } |
| 12412 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12413 | private final boolean updateOomAdjLocked( |
| 12414 | ProcessRecord app, int hiddenAdj, ProcessRecord TOP_APP) { |
| 12415 | app.hiddenAdj = hiddenAdj; |
| 12416 | |
| 12417 | if (app.thread == null) { |
| 12418 | return true; |
| 12419 | } |
| 12420 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12421 | final boolean wasKeeping = app.keeping; |
| 12422 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12423 | int adj = computeOomAdjLocked(app, hiddenAdj, TOP_APP, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12424 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12425 | if ((app.pid != 0 && app.pid != MY_PID) || Process.supportsProcesses()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12426 | if (app.curRawAdj != app.setRawAdj) { |
| 12427 | if (app.curRawAdj > FOREGROUND_APP_ADJ |
| 12428 | && app.setRawAdj <= FOREGROUND_APP_ADJ) { |
| 12429 | // If this app is transitioning from foreground to |
| 12430 | // non-foreground, have it do a gc. |
| 12431 | scheduleAppGcLocked(app); |
| 12432 | } else if (app.curRawAdj >= HIDDEN_APP_MIN_ADJ |
| 12433 | && app.setRawAdj < HIDDEN_APP_MIN_ADJ) { |
| 12434 | // Likewise do a gc when an app is moving in to the |
| 12435 | // background (such as a service stopping). |
| 12436 | scheduleAppGcLocked(app); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12437 | } |
| 12438 | |
| 12439 | if (wasKeeping && !app.keeping) { |
| 12440 | // This app is no longer something we want to keep. Note |
| 12441 | // its current wake lock time to later know to kill it if |
| 12442 | // it is not behaving well. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 12443 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 12444 | synchronized (stats) { |
| 12445 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 12446 | app.pid, SystemClock.elapsedRealtime()); |
| 12447 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12448 | app.lastCpuTime = app.curCpuTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12449 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12450 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12451 | app.setRawAdj = app.curRawAdj; |
| 12452 | } |
| 12453 | if (adj != app.setAdj) { |
| 12454 | if (Process.setOomAdj(app.pid, adj)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12455 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12456 | TAG, "Set app " + app.processName + |
| 12457 | " oom adj to " + adj); |
| 12458 | app.setAdj = adj; |
| 12459 | } else { |
| 12460 | return false; |
| 12461 | } |
| 12462 | } |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 12463 | if (app.setSchedGroup != app.curSchedGroup) { |
| 12464 | app.setSchedGroup = app.curSchedGroup; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12465 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 12466 | "Setting process group of " + app.processName |
| 12467 | + " to " + app.curSchedGroup); |
| 12468 | if (true) { |
San Mehat | 9438de2 | 2009-06-10 09:11:28 -0700 | [diff] [blame] | 12469 | long oldId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 12470 | try { |
| 12471 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 12472 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12473 | Slog.w(TAG, "Failed setting process group of " + app.pid |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 12474 | + " to " + app.curSchedGroup); |
San Mehat | 9438de2 | 2009-06-10 09:11:28 -0700 | [diff] [blame] | 12475 | e.printStackTrace(); |
| 12476 | } finally { |
| 12477 | Binder.restoreCallingIdentity(oldId); |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 12478 | } |
| 12479 | } |
| 12480 | if (false) { |
| 12481 | if (app.thread != null) { |
| 12482 | try { |
| 12483 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 12484 | } catch (RemoteException e) { |
| 12485 | } |
| 12486 | } |
| 12487 | } |
| 12488 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12489 | } |
| 12490 | |
| 12491 | return true; |
| 12492 | } |
| 12493 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12494 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12495 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12496 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12497 | resumedActivity = mMainStack.mPausingActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12498 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12499 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12500 | } |
| 12501 | } |
| 12502 | return resumedActivity; |
| 12503 | } |
| 12504 | |
| 12505 | private final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12506 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12507 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 12508 | int curAdj = app.curAdj; |
| 12509 | final boolean wasHidden = app.curAdj >= HIDDEN_APP_MIN_ADJ |
| 12510 | && app.curAdj <= HIDDEN_APP_MAX_ADJ; |
| 12511 | |
| 12512 | mAdjSeq++; |
| 12513 | |
| 12514 | final boolean res = updateOomAdjLocked(app, app.hiddenAdj, TOP_APP); |
| 12515 | if (res) { |
| 12516 | final boolean nowHidden = app.curAdj >= HIDDEN_APP_MIN_ADJ |
| 12517 | && app.curAdj <= HIDDEN_APP_MAX_ADJ; |
| 12518 | if (nowHidden != wasHidden) { |
| 12519 | // Changed to/from hidden state, so apps after it in the LRU |
| 12520 | // list may also be changed. |
| 12521 | updateOomAdjLocked(); |
| 12522 | } |
| 12523 | } |
| 12524 | return res; |
| 12525 | } |
| 12526 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12527 | final boolean updateOomAdjLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12528 | boolean didOomAdj = true; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12529 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12530 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 12531 | |
| 12532 | if (false) { |
| 12533 | RuntimeException e = new RuntimeException(); |
| 12534 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12535 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12536 | } |
| 12537 | |
| 12538 | mAdjSeq++; |
| 12539 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12540 | // Let's determine how many processes we have running vs. |
| 12541 | // how many slots we have for background processes; we may want |
| 12542 | // to put multiple processes in a slot of there are enough of |
| 12543 | // them. |
| 12544 | int numSlots = HIDDEN_APP_MAX_ADJ - HIDDEN_APP_MIN_ADJ + 1; |
| 12545 | int factor = (mLruProcesses.size()-4)/numSlots; |
| 12546 | if (factor < 1) factor = 1; |
| 12547 | int step = 0; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 12548 | int numHidden = 0; |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12549 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12550 | // First try updating the OOM adjustment for each of the |
| 12551 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12552 | int i = mLruProcesses.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12553 | int curHiddenAdj = HIDDEN_APP_MIN_ADJ; |
| 12554 | while (i > 0) { |
| 12555 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12556 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12557 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12558 | if (updateOomAdjLocked(app, curHiddenAdj, TOP_APP)) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12559 | if (curHiddenAdj < EMPTY_APP_ADJ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12560 | && app.curAdj == curHiddenAdj) { |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12561 | step++; |
| 12562 | if (step >= factor) { |
| 12563 | step = 0; |
| 12564 | curHiddenAdj++; |
| 12565 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12566 | } |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 12567 | if (app.curAdj >= HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 149427c | 2010-04-23 14:20:03 -0700 | [diff] [blame] | 12568 | if (!app.killedBackground) { |
| 12569 | numHidden++; |
| 12570 | if (numHidden > MAX_HIDDEN_APPS) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 12571 | Slog.i(TAG, "No longer want " + app.processName |
| 12572 | + " (pid " + app.pid + "): hidden #" + numHidden); |
Dianne Hackborn | 149427c | 2010-04-23 14:20:03 -0700 | [diff] [blame] | 12573 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 12574 | app.processName, app.setAdj, "too many background"); |
| 12575 | app.killedBackground = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 12576 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 149427c | 2010-04-23 14:20:03 -0700 | [diff] [blame] | 12577 | } |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 12578 | } |
| 12579 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12580 | } else { |
| 12581 | didOomAdj = false; |
| 12582 | } |
| 12583 | } |
| 12584 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12585 | // If we return false, we will fall back on killing processes to |
| 12586 | // have a fixed limit. Do this if a limit has been requested; else |
| 12587 | // only return false if one of the adjustments failed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12588 | return ENFORCE_PROCESS_LIMIT || mProcessLimit > 0 ? false : didOomAdj; |
| 12589 | } |
| 12590 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12591 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12592 | synchronized (this) { |
| 12593 | int i; |
| 12594 | |
| 12595 | // First remove any unused application processes whose package |
| 12596 | // has been removed. |
| 12597 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 12598 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 12599 | if (app.activities.size() == 0 |
| 12600 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12601 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12602 | TAG, "Exiting empty application process " |
| 12603 | + app.processName + " (" |
| 12604 | + (app.thread != null ? app.thread.asBinder() : null) |
| 12605 | + ")\n"); |
| 12606 | if (app.pid > 0 && app.pid != MY_PID) { |
| 12607 | Process.killProcess(app.pid); |
| 12608 | } else { |
| 12609 | try { |
| 12610 | app.thread.scheduleExit(); |
| 12611 | } catch (Exception e) { |
| 12612 | // Ignore exceptions. |
| 12613 | } |
| 12614 | } |
| 12615 | cleanUpApplicationRecordLocked(app, false, -1); |
| 12616 | mRemovedProcesses.remove(i); |
| 12617 | |
| 12618 | if (app.persistent) { |
| 12619 | if (app.persistent) { |
| 12620 | addAppLocked(app.info); |
| 12621 | } |
| 12622 | } |
| 12623 | } |
| 12624 | } |
| 12625 | |
| 12626 | // Now try updating the OOM adjustment for each of the |
| 12627 | // application processes based on their current state. |
| 12628 | // If the setOomAdj() API is not supported, then go with our |
| 12629 | // back-up plan... |
| 12630 | if (!updateOomAdjLocked()) { |
| 12631 | |
| 12632 | // Count how many processes are running services. |
| 12633 | int numServiceProcs = 0; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12634 | for (i=mLruProcesses.size()-1; i>=0; i--) { |
| 12635 | final ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12636 | |
| 12637 | if (app.persistent || app.services.size() != 0 |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 12638 | || app.curReceiver != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12639 | // Don't count processes holding services against our |
| 12640 | // maximum process count. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12641 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12642 | TAG, "Not trimming app " + app + " with services: " |
| 12643 | + app.services); |
| 12644 | numServiceProcs++; |
| 12645 | } |
| 12646 | } |
| 12647 | |
| 12648 | int curMaxProcs = mProcessLimit; |
| 12649 | if (curMaxProcs <= 0) curMaxProcs = MAX_PROCESSES; |
| 12650 | if (mAlwaysFinishActivities) { |
| 12651 | curMaxProcs = 1; |
| 12652 | } |
| 12653 | curMaxProcs += numServiceProcs; |
| 12654 | |
| 12655 | // Quit as many processes as we can to get down to the desired |
| 12656 | // process count. First remove any processes that no longer |
| 12657 | // have activites running in them. |
| 12658 | for ( i=0; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12659 | i<mLruProcesses.size() |
| 12660 | && mLruProcesses.size() > curMaxProcs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12661 | i++) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12662 | final ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12663 | // Quit an application only if it is not currently |
| 12664 | // running any activities. |
| 12665 | if (!app.persistent && app.activities.size() == 0 |
| 12666 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12667 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12668 | TAG, "Exiting empty application process " |
| 12669 | + app.processName + " (" |
| 12670 | + (app.thread != null ? app.thread.asBinder() : null) |
| 12671 | + ")\n"); |
| 12672 | if (app.pid > 0 && app.pid != MY_PID) { |
| 12673 | Process.killProcess(app.pid); |
| 12674 | } else { |
| 12675 | try { |
| 12676 | app.thread.scheduleExit(); |
| 12677 | } catch (Exception e) { |
| 12678 | // Ignore exceptions. |
| 12679 | } |
| 12680 | } |
| 12681 | // todo: For now we assume the application is not buggy |
| 12682 | // or evil, and will quit as a result of our request. |
| 12683 | // Eventually we need to drive this off of the death |
| 12684 | // notification, and kill the process if it takes too long. |
| 12685 | cleanUpApplicationRecordLocked(app, false, i); |
| 12686 | i--; |
| 12687 | } |
| 12688 | } |
| 12689 | |
| 12690 | // If we still have too many processes, now from the least |
| 12691 | // recently used process we start finishing activities. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12692 | if (Config.LOGV) Slog.v( |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12693 | TAG, "*** NOW HAVE " + mLruProcesses.size() + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12694 | " of " + curMaxProcs + " processes"); |
| 12695 | for ( i=0; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12696 | i<mLruProcesses.size() |
| 12697 | && mLruProcesses.size() > curMaxProcs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12698 | i++) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12699 | final ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12700 | // Quit the application only if we have a state saved for |
| 12701 | // all of its activities. |
| 12702 | boolean canQuit = !app.persistent && app.curReceiver == null |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 12703 | && app.services.size() == 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12704 | int NUMA = app.activities.size(); |
| 12705 | int j; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12706 | if (Config.LOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12707 | TAG, "Looking to quit " + app.processName); |
| 12708 | for (j=0; j<NUMA && canQuit; j++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12709 | ActivityRecord r = app.activities.get(j); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12710 | if (Config.LOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12711 | TAG, " " + r.intent.getComponent().flattenToShortString() |
| 12712 | + ": frozen=" + r.haveState + ", visible=" + r.visible); |
| 12713 | canQuit = (r.haveState || !r.stateNotNeeded) |
| 12714 | && !r.visible && r.stopped; |
| 12715 | } |
| 12716 | if (canQuit) { |
| 12717 | // Finish all of the activities, and then the app itself. |
| 12718 | for (j=0; j<NUMA; j++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12719 | ActivityRecord r = app.activities.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12720 | if (!r.finishing) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12721 | r.stack.destroyActivityLocked(r, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12722 | } |
| 12723 | r.resultTo = null; |
| 12724 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12725 | Slog.i(TAG, "Exiting application process " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12726 | + app.processName + " (" |
| 12727 | + (app.thread != null ? app.thread.asBinder() : null) |
| 12728 | + ")\n"); |
| 12729 | if (app.pid > 0 && app.pid != MY_PID) { |
| 12730 | Process.killProcess(app.pid); |
| 12731 | } else { |
| 12732 | try { |
| 12733 | app.thread.scheduleExit(); |
| 12734 | } catch (Exception e) { |
| 12735 | // Ignore exceptions. |
| 12736 | } |
| 12737 | } |
| 12738 | // todo: For now we assume the application is not buggy |
| 12739 | // or evil, and will quit as a result of our request. |
| 12740 | // Eventually we need to drive this off of the death |
| 12741 | // notification, and kill the process if it takes too long. |
| 12742 | cleanUpApplicationRecordLocked(app, false, i); |
| 12743 | i--; |
| 12744 | //dump(); |
| 12745 | } |
| 12746 | } |
| 12747 | |
| 12748 | } |
| 12749 | |
| 12750 | int curMaxActivities = MAX_ACTIVITIES; |
| 12751 | if (mAlwaysFinishActivities) { |
| 12752 | curMaxActivities = 1; |
| 12753 | } |
| 12754 | |
| 12755 | // Finally, if there are too many activities now running, try to |
| 12756 | // finish as many as we can to get back down to the limit. |
| 12757 | for ( i=0; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12758 | i<mMainStack.mLRUActivities.size() |
| 12759 | && mMainStack.mLRUActivities.size() > curMaxActivities; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12760 | i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12761 | final ActivityRecord r |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12762 | = (ActivityRecord)mMainStack.mLRUActivities.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12763 | |
| 12764 | // We can finish this one if we have its icicle saved and |
| 12765 | // it is not persistent. |
| 12766 | if ((r.haveState || !r.stateNotNeeded) && !r.visible |
Dianne Hackborn | 3c4c2b7 | 2010-10-05 18:07:54 -0700 | [diff] [blame] | 12767 | && r.stopped && !r.finishing) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12768 | final int origSize = mMainStack.mLRUActivities.size(); |
| 12769 | r.stack.destroyActivityLocked(r, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12770 | |
| 12771 | // This will remove it from the LRU list, so keep |
| 12772 | // our index at the same value. Note that this check to |
| 12773 | // see if the size changes is just paranoia -- if |
| 12774 | // something unexpected happens, we don't want to end up |
| 12775 | // in an infinite loop. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12776 | if (origSize > mMainStack.mLRUActivities.size()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12777 | i--; |
| 12778 | } |
| 12779 | } |
| 12780 | } |
| 12781 | } |
| 12782 | } |
| 12783 | |
| 12784 | /** This method sends the specified signal to each of the persistent apps */ |
| 12785 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 12786 | if (sig != Process.SIGNAL_USR1) { |
| 12787 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 12788 | } |
| 12789 | |
| 12790 | synchronized (this) { |
| 12791 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 12792 | != PackageManager.PERMISSION_GRANTED) { |
| 12793 | throw new SecurityException("Requires permission " |
| 12794 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 12795 | } |
| 12796 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12797 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 12798 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12799 | if (r.thread != null && r.persistent) { |
| 12800 | Process.sendSignal(r.pid, sig); |
| 12801 | } |
| 12802 | } |
| 12803 | } |
| 12804 | } |
| 12805 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 12806 | public boolean profileControl(String process, boolean start, |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 12807 | String path, ParcelFileDescriptor fd) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 12808 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 12809 | try { |
| 12810 | synchronized (this) { |
| 12811 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 12812 | // its own permission. |
| 12813 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 12814 | != PackageManager.PERMISSION_GRANTED) { |
| 12815 | throw new SecurityException("Requires permission " |
| 12816 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 12817 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 12818 | |
| 12819 | if (start && fd == null) { |
| 12820 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 12821 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 12822 | |
| 12823 | ProcessRecord proc = null; |
| 12824 | try { |
| 12825 | int pid = Integer.parseInt(process); |
| 12826 | synchronized (mPidsSelfLocked) { |
| 12827 | proc = mPidsSelfLocked.get(pid); |
| 12828 | } |
| 12829 | } catch (NumberFormatException e) { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 12830 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 12831 | |
| 12832 | if (proc == null) { |
| 12833 | HashMap<String, SparseArray<ProcessRecord>> all |
| 12834 | = mProcessNames.getMap(); |
| 12835 | SparseArray<ProcessRecord> procs = all.get(process); |
| 12836 | if (procs != null && procs.size() > 0) { |
| 12837 | proc = procs.valueAt(0); |
| 12838 | } |
| 12839 | } |
| 12840 | |
| 12841 | if (proc == null || proc.thread == null) { |
| 12842 | throw new IllegalArgumentException("Unknown process: " + process); |
| 12843 | } |
| 12844 | |
| 12845 | boolean isSecure = "1".equals(SystemProperties.get(SYSTEM_SECURE, "0")); |
| 12846 | if (isSecure) { |
| 12847 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 12848 | throw new SecurityException("Process not debuggable: " + proc); |
| 12849 | } |
| 12850 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 12851 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 12852 | proc.thread.profilerControl(start, path, fd); |
| 12853 | fd = null; |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 12854 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 12855 | } |
| 12856 | } catch (RemoteException e) { |
| 12857 | throw new IllegalStateException("Process disappeared"); |
| 12858 | } finally { |
| 12859 | if (fd != null) { |
| 12860 | try { |
| 12861 | fd.close(); |
| 12862 | } catch (IOException e) { |
| 12863 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 12864 | } |
| 12865 | } |
| 12866 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 12867 | |
| 12868 | public boolean dumpHeap(String process, boolean managed, |
| 12869 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 12870 | |
| 12871 | try { |
| 12872 | synchronized (this) { |
| 12873 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 12874 | // its own permission (same as profileControl). |
| 12875 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 12876 | != PackageManager.PERMISSION_GRANTED) { |
| 12877 | throw new SecurityException("Requires permission " |
| 12878 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 12879 | } |
| 12880 | |
| 12881 | if (fd == null) { |
| 12882 | throw new IllegalArgumentException("null fd"); |
| 12883 | } |
| 12884 | |
| 12885 | ProcessRecord proc = null; |
| 12886 | try { |
| 12887 | int pid = Integer.parseInt(process); |
| 12888 | synchronized (mPidsSelfLocked) { |
| 12889 | proc = mPidsSelfLocked.get(pid); |
| 12890 | } |
| 12891 | } catch (NumberFormatException e) { |
| 12892 | } |
| 12893 | |
| 12894 | if (proc == null) { |
| 12895 | HashMap<String, SparseArray<ProcessRecord>> all |
| 12896 | = mProcessNames.getMap(); |
| 12897 | SparseArray<ProcessRecord> procs = all.get(process); |
| 12898 | if (procs != null && procs.size() > 0) { |
| 12899 | proc = procs.valueAt(0); |
| 12900 | } |
| 12901 | } |
| 12902 | |
| 12903 | if (proc == null || proc.thread == null) { |
| 12904 | throw new IllegalArgumentException("Unknown process: " + process); |
| 12905 | } |
| 12906 | |
| 12907 | boolean isSecure = "1".equals(SystemProperties.get(SYSTEM_SECURE, "0")); |
| 12908 | if (isSecure) { |
| 12909 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 12910 | throw new SecurityException("Process not debuggable: " + proc); |
| 12911 | } |
| 12912 | } |
| 12913 | |
| 12914 | proc.thread.dumpHeap(managed, path, fd); |
| 12915 | fd = null; |
| 12916 | return true; |
| 12917 | } |
| 12918 | } catch (RemoteException e) { |
| 12919 | throw new IllegalStateException("Process disappeared"); |
| 12920 | } finally { |
| 12921 | if (fd != null) { |
| 12922 | try { |
| 12923 | fd.close(); |
| 12924 | } catch (IOException e) { |
| 12925 | } |
| 12926 | } |
| 12927 | } |
| 12928 | } |
| 12929 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12930 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 12931 | public void monitor() { |
| 12932 | synchronized (this) { } |
| 12933 | } |
| 12934 | } |