The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.am; |
| 18 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import com.android.internal.os.BatteryStatsImpl; |
Dianne Hackborn | 45ce864 | 2011-07-14 16:10:16 -0700 | [diff] [blame] | 21 | import com.android.internal.os.ProcessStats; |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 22 | import com.android.server.AttributeCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import com.android.server.IntentResolver; |
| 24 | import com.android.server.ProcessMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import com.android.server.SystemServer; |
| 26 | import com.android.server.Watchdog; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 27 | import com.android.server.am.ActivityStack.ActivityState; |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 28 | import com.android.server.wm.WindowManagerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 30 | import dalvik.system.Zygote; |
| 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.app.Activity; |
| 33 | import android.app.ActivityManager; |
| 34 | import android.app.ActivityManagerNative; |
| 35 | import android.app.ActivityThread; |
| 36 | import android.app.AlertDialog; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 37 | import android.app.AppGlobals; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 38 | import android.app.ApplicationErrorReport; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.app.Dialog; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 40 | import android.app.IActivityController; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.app.IActivityWatcher; |
| 42 | import android.app.IApplicationThread; |
| 43 | import android.app.IInstrumentationWatcher; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 44 | import android.app.INotificationManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 45 | import android.app.IProcessObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.app.IServiceConnection; |
| 47 | import android.app.IThumbnailReceiver; |
| 48 | import android.app.Instrumentation; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 49 | import android.app.Notification; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 50 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.app.PendingIntent; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 52 | import android.app.Service; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 53 | import android.app.backup.IBackupManager; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 54 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 55 | import android.content.BroadcastReceiver; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 56 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.content.ComponentName; |
| 58 | import android.content.ContentResolver; |
| 59 | import android.content.Context; |
Christian Mehlmauer | 7664e20 | 2010-07-20 08:46:17 +0200 | [diff] [blame] | 60 | import android.content.DialogInterface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.content.Intent; |
| 62 | import android.content.IntentFilter; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 63 | import android.content.IIntentReceiver; |
| 64 | import android.content.IIntentSender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 65 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.content.pm.ActivityInfo; |
| 67 | import android.content.pm.ApplicationInfo; |
| 68 | import android.content.pm.ConfigurationInfo; |
| 69 | import android.content.pm.IPackageDataObserver; |
| 70 | import android.content.pm.IPackageManager; |
| 71 | import android.content.pm.InstrumentationInfo; |
Dan Egnor | 66c40e7 | 2010-01-26 16:23:11 -0800 | [diff] [blame] | 72 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.content.pm.PackageManager; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 74 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import android.content.pm.ProviderInfo; |
| 76 | import android.content.pm.ResolveInfo; |
| 77 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 78 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 79 | import android.content.res.CompatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.content.res.Configuration; |
| 81 | import android.graphics.Bitmap; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 82 | import android.net.Proxy; |
| 83 | import android.net.ProxyProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.net.Uri; |
| 85 | import android.os.Binder; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 86 | import android.os.Build; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 87 | import android.os.Bundle; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 88 | import android.os.Debug; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 89 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | import android.os.Environment; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 91 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.os.FileUtils; |
| 93 | import android.os.Handler; |
| 94 | import android.os.IBinder; |
| 95 | import android.os.IPermissionController; |
| 96 | import android.os.Looper; |
| 97 | import android.os.Message; |
| 98 | import android.os.Parcel; |
| 99 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import android.os.Process; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 101 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | import android.os.RemoteException; |
| 103 | import android.os.ServiceManager; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 104 | import android.os.StrictMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | import android.os.SystemClock; |
| 106 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.util.EventLog; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 109 | import android.util.Pair; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 110 | import android.util.Slog; |
Joe Onorato | 5d3bea6 | 2010-03-01 13:44:29 -0800 | [diff] [blame] | 111 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | import android.util.PrintWriterPrinter; |
| 113 | import android.util.SparseArray; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 114 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import android.view.Gravity; |
| 116 | import android.view.LayoutInflater; |
| 117 | import android.view.View; |
| 118 | import android.view.WindowManager; |
| 119 | import android.view.WindowManagerPolicy; |
| 120 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 121 | import java.io.BufferedInputStream; |
| 122 | import java.io.BufferedOutputStream; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 123 | import java.io.BufferedReader; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 124 | import java.io.DataInputStream; |
| 125 | import java.io.DataOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | import java.io.File; |
| 127 | import java.io.FileDescriptor; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 128 | import java.io.FileInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | import java.io.FileNotFoundException; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 130 | import java.io.FileOutputStream; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 131 | import java.io.IOException; |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 132 | import java.io.InputStreamReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | import java.io.PrintWriter; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 134 | import java.io.StringWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | import java.lang.IllegalStateException; |
| 136 | import java.lang.ref.WeakReference; |
| 137 | import java.util.ArrayList; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 138 | import java.util.Collections; |
| 139 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | import java.util.HashMap; |
| 141 | import java.util.HashSet; |
| 142 | import java.util.Iterator; |
| 143 | import java.util.List; |
| 144 | import java.util.Locale; |
| 145 | import java.util.Map; |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 146 | import java.util.Set; |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 147 | import java.util.concurrent.atomic.AtomicBoolean; |
| 148 | import java.util.concurrent.atomic.AtomicLong; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 150 | public final class ActivityManagerService extends ActivityManagerNative |
| 151 | implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | static final String TAG = "ActivityManager"; |
| 153 | static final boolean DEBUG = false; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 154 | static final boolean localLOGV = DEBUG; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | static final boolean DEBUG_SWITCH = localLOGV || false; |
| 156 | static final boolean DEBUG_TASKS = localLOGV || false; |
| 157 | static final boolean DEBUG_PAUSE = localLOGV || false; |
| 158 | static final boolean DEBUG_OOM_ADJ = localLOGV || false; |
| 159 | static final boolean DEBUG_TRANSITION = localLOGV || false; |
| 160 | static final boolean DEBUG_BROADCAST = localLOGV || false; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 161 | static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | static final boolean DEBUG_SERVICE = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 163 | static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | static final boolean DEBUG_VISBILITY = localLOGV || false; |
| 165 | static final boolean DEBUG_PROCESSES = localLOGV || false; |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 166 | static final boolean DEBUG_PROVIDER = localLOGV || false; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 167 | static final boolean DEBUG_URI_PERMISSION = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | static final boolean DEBUG_USER_LEAVING = localLOGV || false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 169 | static final boolean DEBUG_RESULTS = localLOGV || false; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 170 | static final boolean DEBUG_BACKUP = localLOGV || false; |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 171 | static final boolean DEBUG_CONFIGURATION = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 172 | static final boolean DEBUG_POWER = localLOGV || false; |
| 173 | static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | static final boolean VALIDATE_TOKENS = false; |
| 175 | static final boolean SHOW_ACTIVITY_START_TIME = true; |
| 176 | |
| 177 | // Control over CPU and battery monitoring. |
| 178 | static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes. |
| 179 | static final boolean MONITOR_CPU_USAGE = true; |
| 180 | static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds. |
| 181 | static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample. |
| 182 | static final boolean MONITOR_THREAD_CPU_USAGE = false; |
| 183 | |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 184 | // The flags that are set for all calls we make to the package manager. |
Dianne Hackborn | 11b822d | 2009-07-21 20:03:02 -0700 | [diff] [blame] | 185 | static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 186 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 187 | private static final String SYSTEM_DEBUGGABLE = "ro.debuggable"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | // Maximum number of recent tasks that we can remember. |
| 190 | static final int MAX_RECENT_TASKS = 20; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 191 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 192 | // Amount of time after a call to stopAppSwitches() during which we will |
| 193 | // prevent further untrusted switches from happening. |
| 194 | static final long APP_SWITCH_DELAY_TIME = 5*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | |
| 196 | // How long we wait for a launched process to attach to the activity manager |
| 197 | // before we decide it's never going to come up for real. |
| 198 | static final int PROC_START_TIMEOUT = 10*1000; |
| 199 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 200 | // How long we wait for a launched process to attach to the activity manager |
| 201 | // before we decide it's never going to come up for real, when the process was |
| 202 | // started with a wrapper for instrumentation (such as Valgrind) because it |
| 203 | // could take much longer than usual. |
| 204 | static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000; |
| 205 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | // How long to wait after going idle before forcing apps to GC. |
| 207 | static final int GC_TIMEOUT = 5*1000; |
| 208 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 209 | // The minimum amount of time between successive GC requests for a process. |
| 210 | static final int GC_MIN_INTERVAL = 60*1000; |
| 211 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 212 | // The rate at which we check for apps using excessive power -- 15 mins. |
| 213 | static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000; |
| 214 | |
| 215 | // The minimum sample duration we will allow before deciding we have |
| 216 | // enough data on wake locks to start killing things. |
| 217 | static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
| 218 | |
| 219 | // The minimum sample duration we will allow before deciding we have |
| 220 | // enough data on CPU usage to start killing things. |
| 221 | static final int CPU_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | // How long we allow a receiver to run before giving up on it. |
| 224 | static final int BROADCAST_TIMEOUT = 10*1000; |
| 225 | |
| 226 | // How long we wait for a service to finish executing. |
| 227 | static final int SERVICE_TIMEOUT = 20*1000; |
| 228 | |
| 229 | // How long a service needs to be running until restarting its process |
| 230 | // is no longer considered to be a relaunch of the service. |
| 231 | static final int SERVICE_RESTART_DURATION = 5*1000; |
| 232 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 233 | // How long a service needs to be running until it will start back at |
| 234 | // SERVICE_RESTART_DURATION after being killed. |
| 235 | static final int SERVICE_RESET_RUN_DURATION = 60*1000; |
| 236 | |
| 237 | // Multiplying factor to increase restart duration time by, for each time |
| 238 | // a service is killed before it has run for SERVICE_RESET_RUN_DURATION. |
| 239 | static final int SERVICE_RESTART_DURATION_FACTOR = 4; |
| 240 | |
| 241 | // The minimum amount of time between restarting services that we allow. |
| 242 | // That is, when multiple services are restarting, we won't allow each |
| 243 | // to restart less than this amount of time from the last one. |
| 244 | static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000; |
| 245 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | // Maximum amount of time for there to be no activity on a service before |
| 247 | // we consider it non-essential and allow its process to go on the |
| 248 | // LRU background list. |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 249 | static final int MAX_SERVICE_INACTIVITY = 30*60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | |
| 251 | // How long we wait until we timeout on key dispatching. |
| 252 | static final int KEY_DISPATCHING_TIMEOUT = 5*1000; |
| 253 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | // How long we wait until we timeout on key dispatching during instrumentation. |
| 255 | static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000; |
| 256 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 257 | static final int MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | |
| 259 | static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 260 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 261 | public ActivityStack mMainStack; |
| 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 264 | * Description of a request to start a new activity, which has been held |
| 265 | * due to app switches being disabled. |
| 266 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 267 | static class PendingActivityLaunch { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 268 | ActivityRecord r; |
| 269 | ActivityRecord sourceRecord; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 270 | Uri[] grantedUriPermissions; |
| 271 | int grantedMode; |
| 272 | boolean onlyIfNeeded; |
| 273 | } |
| 274 | |
| 275 | final ArrayList<PendingActivityLaunch> mPendingActivityLaunches |
| 276 | = new ArrayList<PendingActivityLaunch>(); |
| 277 | |
| 278 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | * List of all active broadcasts that are to be executed immediately |
| 280 | * (without waiting for another broadcast to finish). Currently this only |
| 281 | * contains broadcasts to registered receivers, to avoid spinning up |
| 282 | * a bunch of processes to execute IntentReceiver components. |
| 283 | */ |
| 284 | final ArrayList<BroadcastRecord> mParallelBroadcasts |
| 285 | = new ArrayList<BroadcastRecord>(); |
| 286 | |
| 287 | /** |
| 288 | * List of all active broadcasts that are to be executed one at a time. |
| 289 | * The object at the top of the list is the currently activity broadcasts; |
| 290 | * those after it are waiting for the top to finish.. |
| 291 | */ |
| 292 | final ArrayList<BroadcastRecord> mOrderedBroadcasts |
| 293 | = new ArrayList<BroadcastRecord>(); |
| 294 | |
| 295 | /** |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 296 | * Historical data of past broadcasts, for debugging. |
| 297 | */ |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 298 | static final int MAX_BROADCAST_HISTORY = 25; |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 299 | final BroadcastRecord[] mBroadcastHistory |
| 300 | = new BroadcastRecord[MAX_BROADCAST_HISTORY]; |
| 301 | |
| 302 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | * Set when we current have a BROADCAST_INTENT_MSG in flight. |
| 304 | */ |
| 305 | boolean mBroadcastsScheduled = false; |
| 306 | |
| 307 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | * Activity we have told the window manager to have key focus. |
| 309 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 310 | ActivityRecord mFocusedActivity = null; |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 311 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | * List of intents that were used to start the most recent tasks. |
| 313 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 314 | final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | |
| 316 | /** |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 317 | * Process management. |
| 318 | */ |
| 319 | final ProcessList mProcessList = new ProcessList(); |
| 320 | |
| 321 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | * All of the applications we currently have running organized by name. |
| 323 | * The keys are strings of the application package name (as |
| 324 | * returned by the package manager), and the keys are ApplicationRecord |
| 325 | * objects. |
| 326 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 327 | final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | |
| 329 | /** |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 330 | * The currently running heavy-weight process, if any. |
| 331 | */ |
| 332 | ProcessRecord mHeavyWeightProcess = null; |
| 333 | |
| 334 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | * The last time that various processes have crashed. |
| 336 | */ |
| 337 | final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>(); |
| 338 | |
| 339 | /** |
| 340 | * Set of applications that we consider to be bad, and will reject |
| 341 | * incoming broadcasts from (which the user has no control over). |
| 342 | * Processes are added to this set when they have crashed twice within |
| 343 | * a minimum amount of time; they are removed from it when they are |
| 344 | * later restarted (hopefully due to some user action). The value is the |
| 345 | * time it was added to the list. |
| 346 | */ |
| 347 | final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>(); |
| 348 | |
| 349 | /** |
| 350 | * All of the processes we currently have running organized by pid. |
| 351 | * The keys are the pid running the application. |
| 352 | * |
| 353 | * <p>NOTE: This object is protected by its own lock, NOT the global |
| 354 | * activity manager lock! |
| 355 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 356 | final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | |
| 358 | /** |
| 359 | * All of the processes that have been forced to be foreground. The key |
| 360 | * is the pid of the caller who requested it (we hold a death |
| 361 | * link on it). |
| 362 | */ |
| 363 | abstract class ForegroundToken implements IBinder.DeathRecipient { |
| 364 | int pid; |
| 365 | IBinder token; |
| 366 | } |
| 367 | final SparseArray<ForegroundToken> mForegroundProcesses |
| 368 | = new SparseArray<ForegroundToken>(); |
| 369 | |
| 370 | /** |
| 371 | * List of records for processes that someone had tried to start before the |
| 372 | * system was ready. We don't start them at that point, but ensure they |
| 373 | * are started by the time booting is complete. |
| 374 | */ |
| 375 | final ArrayList<ProcessRecord> mProcessesOnHold |
| 376 | = new ArrayList<ProcessRecord>(); |
| 377 | |
| 378 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | * List of persistent applications that are in the process |
| 380 | * of being started. |
| 381 | */ |
| 382 | final ArrayList<ProcessRecord> mPersistentStartingProcesses |
| 383 | = new ArrayList<ProcessRecord>(); |
| 384 | |
| 385 | /** |
| 386 | * Processes that are being forcibly torn down. |
| 387 | */ |
| 388 | final ArrayList<ProcessRecord> mRemovedProcesses |
| 389 | = new ArrayList<ProcessRecord>(); |
| 390 | |
| 391 | /** |
| 392 | * List of running applications, sorted by recent usage. |
| 393 | * The first entry in the list is the least recently used. |
| 394 | * It contains ApplicationRecord objects. This list does NOT include |
| 395 | * any persistent application records (since we never want to exit them). |
| 396 | */ |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 397 | final ArrayList<ProcessRecord> mLruProcesses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | = new ArrayList<ProcessRecord>(); |
| 399 | |
| 400 | /** |
| 401 | * List of processes that should gc as soon as things are idle. |
| 402 | */ |
| 403 | final ArrayList<ProcessRecord> mProcessesToGc |
| 404 | = new ArrayList<ProcessRecord>(); |
| 405 | |
| 406 | /** |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 407 | * This is the process holding what we currently consider to be |
| 408 | * the "home" activity. |
| 409 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 410 | ProcessRecord mHomeProcess; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 411 | |
| 412 | /** |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 413 | * This is the process holding the activity the user last visited that |
| 414 | * is in a different process from the one they are currently in. |
| 415 | */ |
| 416 | ProcessRecord mPreviousProcess; |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 417 | |
| 418 | /** |
| 419 | * The time at which the previous process was last visible. |
| 420 | */ |
| 421 | long mPreviousProcessVisibleTime; |
| 422 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 423 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 424 | * Packages that the user has asked to have run in screen size |
| 425 | * compatibility mode instead of filling the screen. |
| 426 | */ |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 427 | final CompatModePackages mCompatModePackages; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 428 | |
| 429 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | * Set of PendingResultRecord objects that are currently active. |
| 431 | */ |
| 432 | final HashSet mPendingResultRecords = new HashSet(); |
| 433 | |
| 434 | /** |
| 435 | * Set of IntentSenderRecord objects that are currently active. |
| 436 | */ |
| 437 | final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords |
| 438 | = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>(); |
| 439 | |
| 440 | /** |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 441 | * Fingerprints (hashCode()) of stack traces that we've |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 442 | * already logged DropBox entries for. Guarded by itself. If |
| 443 | * something (rogue user app) forces this over |
| 444 | * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared. |
| 445 | */ |
| 446 | private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>(); |
| 447 | private static final int MAX_DUP_SUPPRESSED_STACKS = 5000; |
| 448 | |
| 449 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 450 | * Strict Mode background batched logging state. |
| 451 | * |
| 452 | * The string buffer is guarded by itself, and its lock is also |
| 453 | * used to determine if another batched write is already |
| 454 | * in-flight. |
| 455 | */ |
| 456 | private final StringBuilder mStrictModeBuffer = new StringBuilder(); |
| 457 | |
| 458 | /** |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 459 | * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler. |
| 460 | */ |
| 461 | private boolean mPendingBroadcastTimeoutMessage; |
| 462 | |
| 463 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | * Intent broadcast that we have tried to start, but are |
| 465 | * waiting for its application's process to be created. We only |
| 466 | * need one (instead of a list) because we always process broadcasts |
| 467 | * one at a time, so no others can be started while waiting for this |
| 468 | * one. |
| 469 | */ |
| 470 | BroadcastRecord mPendingBroadcast = null; |
| 471 | |
| 472 | /** |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 473 | * The receiver index that is pending, to restart the broadcast if needed. |
| 474 | */ |
| 475 | int mPendingBroadcastRecvIndex; |
| 476 | |
| 477 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | * Keeps track of all IIntentReceivers that have been registered for |
| 479 | * broadcasts. Hash keys are the receiver IBinder, hash value is |
| 480 | * a ReceiverList. |
| 481 | */ |
| 482 | final HashMap mRegisteredReceivers = new HashMap(); |
| 483 | |
| 484 | /** |
| 485 | * Resolver for broadcast intents to registered receivers. |
| 486 | * Holds BroadcastFilter (subclass of IntentFilter). |
| 487 | */ |
| 488 | final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver |
| 489 | = new IntentResolver<BroadcastFilter, BroadcastFilter>() { |
| 490 | @Override |
| 491 | protected boolean allowFilterResult( |
| 492 | BroadcastFilter filter, List<BroadcastFilter> dest) { |
| 493 | IBinder target = filter.receiverList.receiver.asBinder(); |
| 494 | for (int i=dest.size()-1; i>=0; i--) { |
| 495 | if (dest.get(i).receiverList.receiver.asBinder() == target) { |
| 496 | return false; |
| 497 | } |
| 498 | } |
| 499 | return true; |
| 500 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 501 | |
| 502 | @Override |
| 503 | protected String packageForFilter(BroadcastFilter filter) { |
| 504 | return filter.packageName; |
| 505 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | }; |
| 507 | |
| 508 | /** |
| 509 | * State of all active sticky broadcasts. Keys are the action of the |
| 510 | * sticky Intent, values are an ArrayList of all broadcasted intents with |
| 511 | * that action (which should usually be one). |
| 512 | */ |
| 513 | final HashMap<String, ArrayList<Intent>> mStickyBroadcasts = |
| 514 | new HashMap<String, ArrayList<Intent>>(); |
| 515 | |
| 516 | /** |
| 517 | * All currently running services. |
| 518 | */ |
| 519 | final HashMap<ComponentName, ServiceRecord> mServices = |
| 520 | new HashMap<ComponentName, ServiceRecord>(); |
| 521 | |
| 522 | /** |
| 523 | * All currently running services indexed by the Intent used to start them. |
| 524 | */ |
| 525 | final HashMap<Intent.FilterComparison, ServiceRecord> mServicesByIntent = |
| 526 | new HashMap<Intent.FilterComparison, ServiceRecord>(); |
| 527 | |
| 528 | /** |
| 529 | * All currently bound service connections. Keys are the IBinder of |
| 530 | * the client's IServiceConnection. |
| 531 | */ |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 532 | final HashMap<IBinder, ArrayList<ConnectionRecord>> mServiceConnections |
| 533 | = new HashMap<IBinder, ArrayList<ConnectionRecord>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | |
| 535 | /** |
| 536 | * List of services that we have been asked to start, |
| 537 | * but haven't yet been able to. It is used to hold start requests |
| 538 | * while waiting for their corresponding application thread to get |
| 539 | * going. |
| 540 | */ |
| 541 | final ArrayList<ServiceRecord> mPendingServices |
| 542 | = new ArrayList<ServiceRecord>(); |
| 543 | |
| 544 | /** |
| 545 | * List of services that are scheduled to restart following a crash. |
| 546 | */ |
| 547 | final ArrayList<ServiceRecord> mRestartingServices |
| 548 | = new ArrayList<ServiceRecord>(); |
| 549 | |
| 550 | /** |
| 551 | * List of services that are in the process of being stopped. |
| 552 | */ |
| 553 | final ArrayList<ServiceRecord> mStoppingServices |
| 554 | = new ArrayList<ServiceRecord>(); |
| 555 | |
| 556 | /** |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 557 | * Backup/restore process management |
| 558 | */ |
| 559 | String mBackupAppName = null; |
| 560 | BackupRecord mBackupTarget = null; |
| 561 | |
| 562 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | * List of PendingThumbnailsRecord objects of clients who are still |
| 564 | * waiting to receive all of the thumbnails for a task. |
| 565 | */ |
| 566 | final ArrayList mPendingThumbnails = new ArrayList(); |
| 567 | |
| 568 | /** |
| 569 | * List of HistoryRecord objects that have been finished and must |
| 570 | * still report back to a pending thumbnail receiver. |
| 571 | */ |
| 572 | final ArrayList mCancelledThumbnails = new ArrayList(); |
| 573 | |
| 574 | /** |
| 575 | * All of the currently running global content providers. Keys are a |
| 576 | * string containing the provider name and values are a |
| 577 | * ContentProviderRecord object containing the data about it. Note |
| 578 | * that a single provider may be published under multiple names, so |
| 579 | * there may be multiple entries here for a single one in mProvidersByClass. |
| 580 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 581 | final HashMap<String, ContentProviderRecord> mProvidersByName |
| 582 | = new HashMap<String, ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | |
| 584 | /** |
| 585 | * All of the currently running global content providers. Keys are a |
| 586 | * string containing the provider's implementation class and values are a |
| 587 | * ContentProviderRecord object containing the data about it. |
| 588 | */ |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 589 | final HashMap<ComponentName, ContentProviderRecord> mProvidersByClass |
| 590 | = new HashMap<ComponentName, ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | |
| 592 | /** |
| 593 | * List of content providers who have clients waiting for them. The |
| 594 | * application is currently being launched and the provider will be |
| 595 | * removed from this list once it is published. |
| 596 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 597 | final ArrayList<ContentProviderRecord> mLaunchingProviders |
| 598 | = new ArrayList<ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | |
| 600 | /** |
| 601 | * Global set of specific Uri permissions that have been granted. |
| 602 | */ |
| 603 | final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions |
| 604 | = new SparseArray<HashMap<Uri, UriPermission>>(); |
| 605 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 606 | CoreSettingsObserver mCoreSettingsObserver; |
| 607 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | /** |
| 609 | * Thread-local storage used to carry caller permissions over through |
| 610 | * indirect content-provider access. |
| 611 | * @see #ActivityManagerService.openContentUri() |
| 612 | */ |
| 613 | private class Identity { |
| 614 | public int pid; |
| 615 | public int uid; |
| 616 | |
| 617 | Identity(int _pid, int _uid) { |
| 618 | pid = _pid; |
| 619 | uid = _uid; |
| 620 | } |
| 621 | } |
| 622 | private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>(); |
| 623 | |
| 624 | /** |
| 625 | * All information we have collected about the runtime performance of |
| 626 | * any user id that can impact battery performance. |
| 627 | */ |
| 628 | final BatteryStatsService mBatteryStatsService; |
| 629 | |
| 630 | /** |
| 631 | * information about component usage |
| 632 | */ |
| 633 | final UsageStatsService mUsageStatsService; |
| 634 | |
| 635 | /** |
| 636 | * Current configuration information. HistoryRecord objects are given |
| 637 | * a reference to this object to indicate which configuration they are |
| 638 | * currently running in, so this object must be kept immutable. |
| 639 | */ |
| 640 | Configuration mConfiguration = new Configuration(); |
| 641 | |
| 642 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 643 | * Current sequencing integer of the configuration, for skipping old |
| 644 | * configurations. |
| 645 | */ |
| 646 | int mConfigurationSeq = 0; |
| 647 | |
| 648 | /** |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 649 | * Hardware-reported OpenGLES version. |
| 650 | */ |
| 651 | final int GL_ES_VERSION; |
| 652 | |
| 653 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | * List of initialization arguments to pass to all processes when binding applications to them. |
| 655 | * For example, references to the commonly used services. |
| 656 | */ |
| 657 | HashMap<String, IBinder> mAppBindArgs; |
| 658 | |
| 659 | /** |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 660 | * Temporary to avoid allocations. Protected by main lock. |
| 661 | */ |
| 662 | final StringBuilder mStringBuilder = new StringBuilder(256); |
| 663 | |
| 664 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | * Used to control how we initialize the service. |
| 666 | */ |
| 667 | boolean mStartRunning = false; |
| 668 | ComponentName mTopComponent; |
| 669 | String mTopAction; |
| 670 | String mTopData; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 671 | boolean mProcessesReady = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | boolean mSystemReady = false; |
| 673 | boolean mBooting = false; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 674 | boolean mWaitingUpdate = false; |
| 675 | boolean mDidUpdate = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 676 | boolean mOnBattery = false; |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 677 | boolean mLaunchWarningShown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | |
| 679 | Context mContext; |
| 680 | |
| 681 | int mFactoryTest; |
| 682 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 683 | boolean mCheckedForSetup; |
| 684 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 685 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 686 | * The time at which we will allow normal application switches again, |
| 687 | * after a call to {@link #stopAppSwitches()}. |
| 688 | */ |
| 689 | long mAppSwitchesAllowedTime; |
| 690 | |
| 691 | /** |
| 692 | * This is set to true after the first switch after mAppSwitchesAllowedTime |
| 693 | * is set; any switches after that will clear the time. |
| 694 | */ |
| 695 | boolean mDidAppSwitch; |
| 696 | |
| 697 | /** |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 698 | * Last time (in realtime) at which we checked for power usage. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 699 | */ |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 700 | long mLastPowerCheckRealtime; |
| 701 | |
| 702 | /** |
| 703 | * Last time (in uptime) at which we checked for power usage. |
| 704 | */ |
| 705 | long mLastPowerCheckUptime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 706 | |
| 707 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | * Set while we are wanting to sleep, to prevent any |
| 709 | * activities from being started/resumed. |
| 710 | */ |
| 711 | boolean mSleeping = false; |
| 712 | |
| 713 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 714 | * Set if we are shutting down the system, similar to sleeping. |
| 715 | */ |
| 716 | boolean mShuttingDown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | |
| 718 | /** |
| 719 | * Task identifier that activities are currently being started |
| 720 | * in. Incremented each time a new task is created. |
| 721 | * todo: Replace this with a TokenSpace class that generates non-repeating |
| 722 | * integers that won't wrap. |
| 723 | */ |
| 724 | int mCurTask = 1; |
| 725 | |
| 726 | /** |
| 727 | * Current sequence id for oom_adj computation traversal. |
| 728 | */ |
| 729 | int mAdjSeq = 0; |
| 730 | |
| 731 | /** |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 732 | * Current sequence id for process LRU updating. |
| 733 | */ |
| 734 | int mLruSeq = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | |
| 736 | /** |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 737 | * Keep track of the number of service processes we last found, to |
| 738 | * determine on the next iteration which should be B services. |
| 739 | */ |
| 740 | int mNumServiceProcs = 0; |
| 741 | int mNewNumServiceProcs = 0; |
| 742 | |
| 743 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | * System monitoring: number of processes that died since the last |
| 745 | * N procs were started. |
| 746 | */ |
| 747 | int[] mProcDeaths = new int[20]; |
| 748 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 749 | /** |
| 750 | * This is set if we had to do a delayed dexopt of an app before launching |
| 751 | * it, to increasing the ANR timeouts in that case. |
| 752 | */ |
| 753 | boolean mDidDexOpt; |
| 754 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | String mDebugApp = null; |
| 756 | boolean mWaitForDebugger = false; |
| 757 | boolean mDebugTransient = false; |
| 758 | String mOrigDebugApp = null; |
| 759 | boolean mOrigWaitForDebugger = false; |
| 760 | boolean mAlwaysFinishActivities = false; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 761 | IActivityController mController = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 762 | String mProfileApp = null; |
| 763 | ProcessRecord mProfileProc = null; |
| 764 | String mProfileFile; |
| 765 | ParcelFileDescriptor mProfileFd; |
| 766 | int mProfileType = 0; |
| 767 | boolean mAutoStopProfiler = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 768 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 769 | final RemoteCallbackList<IActivityWatcher> mWatchers |
| 770 | = new RemoteCallbackList<IActivityWatcher>(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 771 | |
| 772 | final RemoteCallbackList<IProcessObserver> mProcessObservers |
| 773 | = new RemoteCallbackList<IProcessObserver>(); |
| 774 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | /** |
| 776 | * Callback of last caller to {@link #requestPss}. |
| 777 | */ |
| 778 | Runnable mRequestPssCallback; |
| 779 | |
| 780 | /** |
| 781 | * Remaining processes for which we are waiting results from the last |
| 782 | * call to {@link #requestPss}. |
| 783 | */ |
| 784 | final ArrayList<ProcessRecord> mRequestPssList |
| 785 | = new ArrayList<ProcessRecord>(); |
| 786 | |
| 787 | /** |
| 788 | * Runtime statistics collection thread. This object's lock is used to |
| 789 | * protect all related state. |
| 790 | */ |
| 791 | final Thread mProcessStatsThread; |
| 792 | |
| 793 | /** |
| 794 | * Used to collect process stats when showing not responding dialog. |
| 795 | * Protected by mProcessStatsThread. |
| 796 | */ |
| 797 | final ProcessStats mProcessStats = new ProcessStats( |
| 798 | MONITOR_THREAD_CPU_USAGE); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 799 | final AtomicLong mLastCpuTime = new AtomicLong(0); |
| 800 | final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true); |
| 801 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 802 | long mLastWriteTime = 0; |
| 803 | |
| 804 | /** |
| 805 | * Set to true after the system has finished booting. |
| 806 | */ |
| 807 | boolean mBooted = false; |
| 808 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 809 | int mProcessLimit = ProcessList.MAX_HIDDEN_APPS; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 810 | int mProcessLimitOverride = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | |
| 812 | WindowManagerService mWindowManager; |
| 813 | |
| 814 | static ActivityManagerService mSelf; |
| 815 | static ActivityThread mSystemThread; |
| 816 | |
| 817 | private final class AppDeathRecipient implements IBinder.DeathRecipient { |
| 818 | final ProcessRecord mApp; |
| 819 | final int mPid; |
| 820 | final IApplicationThread mAppThread; |
| 821 | |
| 822 | AppDeathRecipient(ProcessRecord app, int pid, |
| 823 | IApplicationThread thread) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 824 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | TAG, "New death recipient " + this |
| 826 | + " for thread " + thread.asBinder()); |
| 827 | mApp = app; |
| 828 | mPid = pid; |
| 829 | mAppThread = thread; |
| 830 | } |
| 831 | |
| 832 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 833 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | TAG, "Death received in " + this |
| 835 | + " for thread " + mAppThread.asBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | synchronized(ActivityManagerService.this) { |
| 837 | appDiedLocked(mApp, mPid, mAppThread); |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | static final int SHOW_ERROR_MSG = 1; |
| 843 | static final int SHOW_NOT_RESPONDING_MSG = 2; |
| 844 | static final int SHOW_FACTORY_ERROR_MSG = 3; |
| 845 | static final int UPDATE_CONFIGURATION_MSG = 4; |
| 846 | static final int GC_BACKGROUND_PROCESSES_MSG = 5; |
| 847 | static final int WAIT_FOR_DEBUGGER_MSG = 6; |
| 848 | static final int BROADCAST_INTENT_MSG = 7; |
| 849 | static final int BROADCAST_TIMEOUT_MSG = 8; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | static final int SERVICE_TIMEOUT_MSG = 12; |
| 851 | static final int UPDATE_TIME_ZONE = 13; |
| 852 | static final int SHOW_UID_ERROR_MSG = 14; |
| 853 | static final int IM_FEELING_LUCKY_MSG = 15; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | static final int PROC_START_TIMEOUT_MSG = 20; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 855 | static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 856 | static final int KILL_APPLICATION_MSG = 22; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 857 | static final int FINALIZE_PENDING_INTENT_MSG = 23; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 858 | static final int POST_HEAVY_NOTIFICATION_MSG = 24; |
| 859 | static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 860 | static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 861 | static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 862 | static final int CLEAR_DNS_CACHE = 28; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 863 | static final int UPDATE_HTTP_PROXY = 29; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 864 | static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30; |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 865 | static final int DISPATCH_FOREGROUND_ACTIVITIES_CHANGED = 31; |
| 866 | static final int DISPATCH_PROCESS_DIED = 32; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 867 | static final int REPORT_MEM_USAGE = 33; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | |
| 869 | AlertDialog mUidAlert; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 870 | CompatModeDialog mCompatModeDialog; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 871 | long mLastMemUsageReportTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 872 | |
| 873 | final Handler mHandler = new Handler() { |
| 874 | //public Handler() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 875 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | //} |
| 877 | |
| 878 | public void handleMessage(Message msg) { |
| 879 | switch (msg.what) { |
| 880 | case SHOW_ERROR_MSG: { |
| 881 | HashMap data = (HashMap) msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | synchronized (ActivityManagerService.this) { |
| 883 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 884 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 885 | Slog.e(TAG, "App already has crash dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | return; |
| 887 | } |
| 888 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 889 | if (!mSleeping && !mShuttingDown) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 890 | Dialog d = new AppErrorDialog(mContext, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | d.show(); |
| 892 | proc.crashDialog = d; |
| 893 | } else { |
| 894 | // The device is asleep, so just pretend that the user |
| 895 | // saw a crash dialog and hit "force quit". |
| 896 | res.set(0); |
| 897 | } |
| 898 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 899 | |
| 900 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | } break; |
| 902 | case SHOW_NOT_RESPONDING_MSG: { |
| 903 | synchronized (ActivityManagerService.this) { |
| 904 | HashMap data = (HashMap) msg.obj; |
| 905 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 906 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 907 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | return; |
| 909 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 910 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 911 | Intent intent = new Intent("android.intent.action.ANR"); |
| 912 | if (!mProcessesReady) { |
| 913 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 914 | } |
| 915 | broadcastIntentLocked(null, null, intent, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 916 | null, null, 0, null, null, null, |
| 917 | false, false, MY_PID, Process.SYSTEM_UID); |
| 918 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 920 | mContext, proc, (ActivityRecord)data.get("activity")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | d.show(); |
| 922 | proc.anrDialog = d; |
| 923 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 924 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 925 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 927 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 928 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 929 | synchronized (ActivityManagerService.this) { |
| 930 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 931 | if (proc == null) { |
| 932 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 933 | break; |
| 934 | } |
| 935 | if (proc.crashDialog != null) { |
| 936 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 937 | return; |
| 938 | } |
| 939 | AppErrorResult res = (AppErrorResult) data.get("result"); |
| 940 | if (!mSleeping && !mShuttingDown) { |
| 941 | Dialog d = new StrictModeViolationDialog(mContext, res, proc); |
| 942 | d.show(); |
| 943 | proc.crashDialog = d; |
| 944 | } else { |
| 945 | // The device is asleep, so just pretend that the user |
| 946 | // saw a crash dialog and hit "force quit". |
| 947 | res.set(0); |
| 948 | } |
| 949 | } |
| 950 | ensureBootCompleted(); |
| 951 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | case SHOW_FACTORY_ERROR_MSG: { |
| 953 | Dialog d = new FactoryErrorDialog( |
| 954 | mContext, msg.getData().getCharSequence("msg")); |
| 955 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 956 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | } break; |
| 958 | case UPDATE_CONFIGURATION_MSG: { |
| 959 | final ContentResolver resolver = mContext.getContentResolver(); |
| 960 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 961 | } break; |
| 962 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 963 | synchronized (ActivityManagerService.this) { |
| 964 | performAppGcsIfAppropriateLocked(); |
| 965 | } |
| 966 | } break; |
| 967 | case WAIT_FOR_DEBUGGER_MSG: { |
| 968 | synchronized (ActivityManagerService.this) { |
| 969 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 970 | if (msg.arg1 != 0) { |
| 971 | if (!app.waitedForDebugger) { |
| 972 | Dialog d = new AppWaitingForDebuggerDialog( |
| 973 | ActivityManagerService.this, |
| 974 | mContext, app); |
| 975 | app.waitDialog = d; |
| 976 | app.waitedForDebugger = true; |
| 977 | d.show(); |
| 978 | } |
| 979 | } else { |
| 980 | if (app.waitDialog != null) { |
| 981 | app.waitDialog.dismiss(); |
| 982 | app.waitDialog = null; |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | } break; |
| 987 | case BROADCAST_INTENT_MSG: { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 988 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | TAG, "Received BROADCAST_INTENT_MSG"); |
| 990 | processNextBroadcast(true); |
| 991 | } break; |
| 992 | case BROADCAST_TIMEOUT_MSG: { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 993 | synchronized (ActivityManagerService.this) { |
| 994 | broadcastTimeoutLocked(true); |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 995 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 998 | if (mDidDexOpt) { |
| 999 | mDidDexOpt = false; |
| 1000 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 1001 | nmsg.obj = msg.obj; |
| 1002 | mHandler.sendMessageDelayed(nmsg, SERVICE_TIMEOUT); |
| 1003 | return; |
| 1004 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | serviceTimeout((ProcessRecord)msg.obj); |
| 1006 | } break; |
| 1007 | case UPDATE_TIME_ZONE: { |
| 1008 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1009 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1010 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | if (r.thread != null) { |
| 1012 | try { |
| 1013 | r.thread.updateTimeZone(); |
| 1014 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1015 | 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] | 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1020 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1021 | case CLEAR_DNS_CACHE: { |
| 1022 | synchronized (ActivityManagerService.this) { |
| 1023 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1024 | ProcessRecord r = mLruProcesses.get(i); |
| 1025 | if (r.thread != null) { |
| 1026 | try { |
| 1027 | r.thread.clearDnsCache(); |
| 1028 | } catch (RemoteException ex) { |
| 1029 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1035 | case UPDATE_HTTP_PROXY: { |
| 1036 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 1037 | String host = ""; |
| 1038 | String port = ""; |
| 1039 | String exclList = ""; |
| 1040 | if (proxy != null) { |
| 1041 | host = proxy.getHost(); |
| 1042 | port = Integer.toString(proxy.getPort()); |
| 1043 | exclList = proxy.getExclusionList(); |
| 1044 | } |
| 1045 | synchronized (ActivityManagerService.this) { |
| 1046 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1047 | ProcessRecord r = mLruProcesses.get(i); |
| 1048 | if (r.thread != null) { |
| 1049 | try { |
| 1050 | r.thread.setHttpProxy(host, port, exclList); |
| 1051 | } catch (RemoteException ex) { |
| 1052 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1053 | r.info.processName); |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1059 | case SHOW_UID_ERROR_MSG: { |
| 1060 | // XXX This is a temporary dialog, no need to localize. |
| 1061 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1062 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1063 | d.setCancelable(false); |
| 1064 | d.setTitle("System UIDs Inconsistent"); |
| 1065 | 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] | 1066 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1068 | mUidAlert = d; |
| 1069 | d.show(); |
| 1070 | } break; |
| 1071 | case IM_FEELING_LUCKY_MSG: { |
| 1072 | if (mUidAlert != null) { |
| 1073 | mUidAlert.dismiss(); |
| 1074 | mUidAlert = null; |
| 1075 | } |
| 1076 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1078 | if (mDidDexOpt) { |
| 1079 | mDidDexOpt = false; |
| 1080 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1081 | nmsg.obj = msg.obj; |
| 1082 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1083 | return; |
| 1084 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1086 | synchronized (ActivityManagerService.this) { |
| 1087 | processStartTimedOutLocked(app); |
| 1088 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1089 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1090 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1091 | synchronized (ActivityManagerService.this) { |
| 1092 | doPendingActivityLaunchesLocked(true); |
| 1093 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1094 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1095 | case KILL_APPLICATION_MSG: { |
| 1096 | synchronized (ActivityManagerService.this) { |
| 1097 | int uid = msg.arg1; |
| 1098 | boolean restart = (msg.arg2 == 1); |
| 1099 | String pkg = (String) msg.obj; |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 1100 | forceStopPackageLocked(pkg, uid, restart, false, true, false); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1101 | } |
| 1102 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1103 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1104 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1105 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1106 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1107 | INotificationManager inm = NotificationManager.getService(); |
| 1108 | if (inm == null) { |
| 1109 | return; |
| 1110 | } |
| 1111 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1112 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1113 | ProcessRecord process = root.app; |
| 1114 | if (process == null) { |
| 1115 | return; |
| 1116 | } |
| 1117 | |
| 1118 | try { |
| 1119 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1120 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1121 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1122 | Notification notification = new Notification(); |
| 1123 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1124 | notification.when = 0; |
| 1125 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1126 | notification.tickerText = text; |
| 1127 | notification.defaults = 0; // please be quiet |
| 1128 | notification.sound = null; |
| 1129 | notification.vibrate = null; |
| 1130 | notification.setLatestEventInfo(context, text, |
| 1131 | mContext.getText(R.string.heavy_weight_notification_detail), |
| 1132 | PendingIntent.getActivity(mContext, 0, root.intent, |
| 1133 | PendingIntent.FLAG_CANCEL_CURRENT)); |
| 1134 | |
| 1135 | try { |
| 1136 | int[] outId = new int[1]; |
| 1137 | inm.enqueueNotification("android", R.string.heavy_weight_notification, |
| 1138 | notification, outId); |
| 1139 | } catch (RuntimeException e) { |
| 1140 | Slog.w(ActivityManagerService.TAG, |
| 1141 | "Error showing notification for heavy-weight app", e); |
| 1142 | } catch (RemoteException e) { |
| 1143 | } |
| 1144 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1145 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1146 | } |
| 1147 | } break; |
| 1148 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1149 | INotificationManager inm = NotificationManager.getService(); |
| 1150 | if (inm == null) { |
| 1151 | return; |
| 1152 | } |
| 1153 | try { |
| 1154 | inm.cancelNotification("android", |
| 1155 | R.string.heavy_weight_notification); |
| 1156 | } catch (RuntimeException e) { |
| 1157 | Slog.w(ActivityManagerService.TAG, |
| 1158 | "Error canceling notification for service", e); |
| 1159 | } catch (RemoteException e) { |
| 1160 | } |
| 1161 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1162 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1163 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1164 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1165 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1166 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1167 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1168 | } |
| 1169 | } break; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1170 | case SHOW_COMPAT_MODE_DIALOG_MSG: { |
| 1171 | synchronized (ActivityManagerService.this) { |
| 1172 | ActivityRecord ar = (ActivityRecord)msg.obj; |
| 1173 | if (mCompatModeDialog != null) { |
| 1174 | if (mCompatModeDialog.mAppInfo.packageName.equals( |
| 1175 | ar.info.applicationInfo.packageName)) { |
| 1176 | return; |
| 1177 | } |
| 1178 | mCompatModeDialog.dismiss(); |
| 1179 | mCompatModeDialog = null; |
| 1180 | } |
Dianne Hackborn | 2947826 | 2011-06-07 15:44:22 -0700 | [diff] [blame] | 1181 | if (ar != null && false) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1182 | if (mCompatModePackages.getPackageAskCompatModeLocked( |
| 1183 | ar.packageName)) { |
| 1184 | int mode = mCompatModePackages.computeCompatModeLocked( |
| 1185 | ar.info.applicationInfo); |
| 1186 | if (mode == ActivityManager.COMPAT_MODE_DISABLED |
| 1187 | || mode == ActivityManager.COMPAT_MODE_ENABLED) { |
| 1188 | mCompatModeDialog = new CompatModeDialog( |
| 1189 | ActivityManagerService.this, mContext, |
| 1190 | ar.info.applicationInfo); |
| 1191 | mCompatModeDialog.show(); |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | } |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 1196 | break; |
| 1197 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1198 | case DISPATCH_FOREGROUND_ACTIVITIES_CHANGED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1199 | final int pid = msg.arg1; |
| 1200 | final int uid = msg.arg2; |
| 1201 | final boolean foregroundActivities = (Boolean) msg.obj; |
| 1202 | dispatchForegroundActivitiesChanged(pid, uid, foregroundActivities); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1203 | break; |
| 1204 | } |
| 1205 | case DISPATCH_PROCESS_DIED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1206 | final int pid = msg.arg1; |
| 1207 | final int uid = msg.arg2; |
| 1208 | dispatchProcessDied(pid, uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1209 | break; |
| 1210 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1211 | case REPORT_MEM_USAGE: { |
| 1212 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 1213 | if (!isDebuggable) { |
| 1214 | return; |
| 1215 | } |
| 1216 | synchronized (ActivityManagerService.this) { |
| 1217 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1218 | if (now < (mLastMemUsageReportTime+5*60*1000)) { |
| 1219 | // Don't report more than every 5 minutes to somewhat |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1220 | // avoid spamming. |
| 1221 | return; |
| 1222 | } |
| 1223 | mLastMemUsageReportTime = now; |
| 1224 | } |
| 1225 | Thread thread = new Thread() { |
| 1226 | @Override public void run() { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1227 | StringBuilder dropBuilder = new StringBuilder(1024); |
| 1228 | StringBuilder logBuilder = new StringBuilder(1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1229 | StringWriter oomSw = new StringWriter(); |
| 1230 | PrintWriter oomPw = new PrintWriter(oomSw); |
| 1231 | StringWriter catSw = new StringWriter(); |
| 1232 | PrintWriter catPw = new PrintWriter(catSw); |
| 1233 | String[] emptyArgs = new String[] { }; |
| 1234 | StringBuilder tag = new StringBuilder(128); |
| 1235 | StringBuilder stack = new StringBuilder(128); |
| 1236 | tag.append("Low on memory -- "); |
| 1237 | dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw, |
| 1238 | tag, stack); |
| 1239 | dropBuilder.append(stack); |
| 1240 | dropBuilder.append('\n'); |
| 1241 | dropBuilder.append('\n'); |
| 1242 | String oomString = oomSw.toString(); |
| 1243 | dropBuilder.append(oomString); |
| 1244 | dropBuilder.append('\n'); |
| 1245 | logBuilder.append(oomString); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1246 | try { |
| 1247 | java.lang.Process proc = Runtime.getRuntime().exec(new String[] { |
| 1248 | "procrank", }); |
| 1249 | final InputStreamReader converter = new InputStreamReader( |
| 1250 | proc.getInputStream()); |
| 1251 | BufferedReader in = new BufferedReader(converter); |
| 1252 | String line; |
| 1253 | while (true) { |
| 1254 | line = in.readLine(); |
| 1255 | if (line == null) { |
| 1256 | break; |
| 1257 | } |
| 1258 | if (line.length() > 0) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1259 | logBuilder.append(line); |
| 1260 | logBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1261 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1262 | dropBuilder.append(line); |
| 1263 | dropBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1264 | } |
| 1265 | converter.close(); |
| 1266 | } catch (IOException e) { |
| 1267 | } |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1268 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1269 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1270 | dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1271 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1272 | dumpServicesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1273 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1274 | dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1275 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1276 | dropBuilder.append(catSw.toString()); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1277 | addErrorToDropBox("lowmem", null, "system_server", null, |
| 1278 | null, tag.toString(), dropBuilder.toString(), null, null); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1279 | Slog.i(TAG, logBuilder.toString()); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1280 | synchronized (ActivityManagerService.this) { |
| 1281 | long now = SystemClock.uptimeMillis(); |
| 1282 | if (mLastMemUsageReportTime < now) { |
| 1283 | mLastMemUsageReportTime = now; |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | }; |
| 1288 | thread.start(); |
| 1289 | break; |
| 1290 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | }; |
| 1294 | |
| 1295 | public static void setSystemProcess() { |
| 1296 | try { |
| 1297 | ActivityManagerService m = mSelf; |
| 1298 | |
| 1299 | ServiceManager.addService("activity", m); |
| 1300 | ServiceManager.addService("meminfo", new MemBinder(m)); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1301 | ServiceManager.addService("gfxinfo", new GraphicsBinder(m)); |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1302 | ServiceManager.addService("dbinfo", new DbBinder(m)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | if (MONITOR_CPU_USAGE) { |
| 1304 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1305 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1306 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1307 | |
| 1308 | ApplicationInfo info = |
| 1309 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 1310 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1311 | mSystemThread.installSystemApplicationInfo(info); |
| 1312 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1313 | synchronized (mSelf) { |
| 1314 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1315 | mSystemThread.getApplicationThread(), info, |
| 1316 | info.processName); |
| 1317 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1318 | app.pid = MY_PID; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1319 | app.maxAdj = ProcessList.SYSTEM_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1320 | mSelf.mProcessNames.put(app.processName, app.info.uid, app); |
| 1321 | synchronized (mSelf.mPidsSelfLocked) { |
| 1322 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1323 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1324 | mSelf.updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1325 | } |
| 1326 | } catch (PackageManager.NameNotFoundException e) { |
| 1327 | throw new RuntimeException( |
| 1328 | "Unable to find android system package", e); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | public void setWindowManager(WindowManagerService wm) { |
| 1333 | mWindowManager = wm; |
| 1334 | } |
| 1335 | |
| 1336 | public static final Context main(int factoryTest) { |
| 1337 | AThread thr = new AThread(); |
| 1338 | thr.start(); |
| 1339 | |
| 1340 | synchronized (thr) { |
| 1341 | while (thr.mService == null) { |
| 1342 | try { |
| 1343 | thr.wait(); |
| 1344 | } catch (InterruptedException e) { |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | ActivityManagerService m = thr.mService; |
| 1350 | mSelf = m; |
| 1351 | ActivityThread at = ActivityThread.systemMain(); |
| 1352 | mSystemThread = at; |
| 1353 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1354 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1355 | m.mContext = context; |
| 1356 | m.mFactoryTest = factoryTest; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1357 | m.mMainStack = new ActivityStack(m, context, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | |
| 1359 | m.mBatteryStatsService.publish(context); |
| 1360 | m.mUsageStatsService.publish(context); |
| 1361 | |
| 1362 | synchronized (thr) { |
| 1363 | thr.mReady = true; |
| 1364 | thr.notifyAll(); |
| 1365 | } |
| 1366 | |
| 1367 | m.startRunning(null, null, null, null); |
| 1368 | |
| 1369 | return context; |
| 1370 | } |
| 1371 | |
| 1372 | public static ActivityManagerService self() { |
| 1373 | return mSelf; |
| 1374 | } |
| 1375 | |
| 1376 | static class AThread extends Thread { |
| 1377 | ActivityManagerService mService; |
| 1378 | boolean mReady = false; |
| 1379 | |
| 1380 | public AThread() { |
| 1381 | super("ActivityManager"); |
| 1382 | } |
| 1383 | |
| 1384 | public void run() { |
| 1385 | Looper.prepare(); |
| 1386 | |
| 1387 | android.os.Process.setThreadPriority( |
| 1388 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1389 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1390 | |
| 1391 | ActivityManagerService m = new ActivityManagerService(); |
| 1392 | |
| 1393 | synchronized (this) { |
| 1394 | mService = m; |
| 1395 | notifyAll(); |
| 1396 | } |
| 1397 | |
| 1398 | synchronized (this) { |
| 1399 | while (!mReady) { |
| 1400 | try { |
| 1401 | wait(); |
| 1402 | } catch (InterruptedException e) { |
| 1403 | } |
| 1404 | } |
| 1405 | } |
| 1406 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1407 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1408 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1409 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1410 | } |
| 1411 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1412 | Looper.loop(); |
| 1413 | } |
| 1414 | } |
| 1415 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1416 | static class MemBinder extends Binder { |
| 1417 | ActivityManagerService mActivityManagerService; |
| 1418 | MemBinder(ActivityManagerService activityManagerService) { |
| 1419 | mActivityManagerService = activityManagerService; |
| 1420 | } |
| 1421 | |
| 1422 | @Override |
| 1423 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1424 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1425 | != PackageManager.PERMISSION_GRANTED) { |
| 1426 | pw.println("Permission Denial: can't dump meminfo from from pid=" |
| 1427 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1428 | + " without permission " + android.Manifest.permission.DUMP); |
| 1429 | return; |
| 1430 | } |
| 1431 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1432 | mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1433 | false, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1434 | } |
| 1435 | } |
| 1436 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1437 | static class GraphicsBinder extends Binder { |
| 1438 | ActivityManagerService mActivityManagerService; |
| 1439 | GraphicsBinder(ActivityManagerService activityManagerService) { |
| 1440 | mActivityManagerService = activityManagerService; |
| 1441 | } |
| 1442 | |
| 1443 | @Override |
| 1444 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1445 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1446 | != PackageManager.PERMISSION_GRANTED) { |
| 1447 | pw.println("Permission Denial: can't dump gfxinfo from from pid=" |
| 1448 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1449 | + " without permission " + android.Manifest.permission.DUMP); |
| 1450 | return; |
| 1451 | } |
| 1452 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1453 | mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1454 | } |
| 1455 | } |
| 1456 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1457 | static class DbBinder extends Binder { |
| 1458 | ActivityManagerService mActivityManagerService; |
| 1459 | DbBinder(ActivityManagerService activityManagerService) { |
| 1460 | mActivityManagerService = activityManagerService; |
| 1461 | } |
| 1462 | |
| 1463 | @Override |
| 1464 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1465 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1466 | != PackageManager.PERMISSION_GRANTED) { |
| 1467 | pw.println("Permission Denial: can't dump dbinfo from from pid=" |
| 1468 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1469 | + " without permission " + android.Manifest.permission.DUMP); |
| 1470 | return; |
| 1471 | } |
| 1472 | |
| 1473 | mActivityManagerService.dumpDbInfo(fd, pw, args); |
| 1474 | } |
| 1475 | } |
| 1476 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1477 | static class CpuBinder extends Binder { |
| 1478 | ActivityManagerService mActivityManagerService; |
| 1479 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1480 | mActivityManagerService = activityManagerService; |
| 1481 | } |
| 1482 | |
| 1483 | @Override |
| 1484 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1485 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1486 | != PackageManager.PERMISSION_GRANTED) { |
| 1487 | pw.println("Permission Denial: can't dump cpuinfo from from pid=" |
| 1488 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1489 | + " without permission " + android.Manifest.permission.DUMP); |
| 1490 | return; |
| 1491 | } |
| 1492 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1493 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1494 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1495 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1496 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1497 | } |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | private ActivityManagerService() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1502 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1503 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1504 | File dataDir = Environment.getDataDirectory(); |
| 1505 | File systemDir = new File(dataDir, "system"); |
| 1506 | systemDir.mkdirs(); |
| 1507 | mBatteryStatsService = new BatteryStatsService(new File( |
| 1508 | systemDir, "batterystats.bin").toString()); |
| 1509 | mBatteryStatsService.getActiveStatistics().readLocked(); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1510 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1511 | mOnBattery = DEBUG_POWER ? true |
| 1512 | : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1513 | mBatteryStatsService.getActiveStatistics().setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1514 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1515 | mUsageStatsService = new UsageStatsService(new File( |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1516 | systemDir, "usagestats").toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1517 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1518 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1519 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1520 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1521 | mConfiguration.setToDefaults(); |
| 1522 | mConfiguration.locale = Locale.getDefault(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 1523 | mConfigurationSeq = mConfiguration.seq = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1524 | mProcessStats.init(); |
| 1525 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 1526 | mCompatModePackages = new CompatModePackages(this, systemDir); |
| 1527 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1528 | // Add ourself to the Watchdog monitors. |
| 1529 | Watchdog.getInstance().addMonitor(this); |
| 1530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1531 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1532 | public void run() { |
| 1533 | while (true) { |
| 1534 | try { |
| 1535 | try { |
| 1536 | synchronized(this) { |
| 1537 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1538 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1539 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1540 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1541 | // + ", write delay=" + nextWriteDelay); |
| 1542 | if (nextWriteDelay < nextCpuDelay) { |
| 1543 | nextCpuDelay = nextWriteDelay; |
| 1544 | } |
| 1545 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1546 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1547 | this.wait(nextCpuDelay); |
| 1548 | } |
| 1549 | } |
| 1550 | } catch (InterruptedException e) { |
| 1551 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1552 | updateCpuStatsNow(); |
| 1553 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1554 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | }; |
| 1559 | mProcessStatsThread.start(); |
| 1560 | } |
| 1561 | |
| 1562 | @Override |
| 1563 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1564 | throws RemoteException { |
| 1565 | try { |
| 1566 | return super.onTransact(code, data, reply, flags); |
| 1567 | } catch (RuntimeException e) { |
| 1568 | // The activity manager only throws security exceptions, so let's |
| 1569 | // log all others. |
| 1570 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1571 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1572 | } |
| 1573 | throw e; |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1578 | final long now = SystemClock.uptimeMillis(); |
| 1579 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1580 | return; |
| 1581 | } |
| 1582 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1583 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1584 | mProcessStatsThread.notify(); |
| 1585 | } |
| 1586 | } |
| 1587 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1588 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1589 | void updateCpuStatsNow() { |
| 1590 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1591 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1592 | final long now = SystemClock.uptimeMillis(); |
| 1593 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1595 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1596 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1597 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1598 | haveNewCpuStats = true; |
| 1599 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1600 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1601 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1602 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1603 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1604 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1605 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1606 | int user = mProcessStats.getLastUserTime(); |
| 1607 | int system = mProcessStats.getLastSystemTime(); |
| 1608 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1609 | int irq = mProcessStats.getLastIrqTime(); |
| 1610 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1611 | int idle = mProcessStats.getLastIdleTime(); |
| 1612 | |
| 1613 | int total = user + system + iowait + irq + softIrq + idle; |
| 1614 | if (total == 0) total = 1; |
| 1615 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1616 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1617 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1618 | (user * 100) / total, |
| 1619 | (system * 100) / total, |
| 1620 | (iowait * 100) / total, |
| 1621 | (irq * 100) / total, |
| 1622 | (softIrq * 100) / total); |
| 1623 | } |
| 1624 | } |
| 1625 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1626 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1627 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1628 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1629 | synchronized(mPidsSelfLocked) { |
| 1630 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1631 | if (mOnBattery) { |
| 1632 | int perc = bstats.startAddingCpuLocked(); |
| 1633 | int totalUTime = 0; |
| 1634 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1635 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1637 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1638 | if (!st.working) { |
| 1639 | continue; |
| 1640 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1642 | int otherUTime = (st.rel_utime*perc)/100; |
| 1643 | int otherSTime = (st.rel_stime*perc)/100; |
| 1644 | totalUTime += otherUTime; |
| 1645 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1646 | if (pr != null) { |
| 1647 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1648 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1649 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1650 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1651 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1652 | } else { |
| 1653 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1654 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1655 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1656 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1657 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1658 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1659 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1660 | } |
| 1661 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1662 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1663 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1664 | } |
| 1665 | } |
| 1666 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1667 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1668 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1669 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1670 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1671 | } |
| 1672 | } |
| 1673 | } |
| 1674 | } |
| 1675 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1676 | @Override |
| 1677 | public void batteryNeedsCpuUpdate() { |
| 1678 | updateCpuStatsNow(); |
| 1679 | } |
| 1680 | |
| 1681 | @Override |
| 1682 | public void batteryPowerChanged(boolean onBattery) { |
| 1683 | // When plugging in, update the CPU stats first before changing |
| 1684 | // the plug state. |
| 1685 | updateCpuStatsNow(); |
| 1686 | synchronized (this) { |
| 1687 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1688 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1689 | } |
| 1690 | } |
| 1691 | } |
| 1692 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1693 | /** |
| 1694 | * Initialize the application bind args. These are passed to each |
| 1695 | * process when the bindApplication() IPC is sent to the process. They're |
| 1696 | * lazily setup to make sure the services are running when they're asked for. |
| 1697 | */ |
| 1698 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1699 | if (mAppBindArgs == null) { |
| 1700 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1701 | |
| 1702 | // Setup the application init args |
| 1703 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1704 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1705 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1706 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1707 | } |
| 1708 | return mAppBindArgs; |
| 1709 | } |
| 1710 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1711 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1712 | if (mFocusedActivity != r) { |
| 1713 | mFocusedActivity = r; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1714 | if (r != null) { |
| 1715 | mWindowManager.setFocusedApp(r.appToken, true); |
| 1716 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1720 | private final void updateLruProcessInternalLocked(ProcessRecord app, |
| 1721 | boolean oomAdj, boolean updateActivityTime, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1722 | // 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] | 1723 | int lrui = mLruProcesses.indexOf(app); |
| 1724 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1725 | |
| 1726 | int i = mLruProcesses.size()-1; |
| 1727 | int skipTop = 0; |
| 1728 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1729 | app.lruSeq = mLruSeq; |
| 1730 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1731 | // compute the new weight for this process. |
| 1732 | if (updateActivityTime) { |
| 1733 | app.lastActivityTime = SystemClock.uptimeMillis(); |
| 1734 | } |
| 1735 | if (app.activities.size() > 0) { |
| 1736 | // If this process has activities, we more strongly want to keep |
| 1737 | // it around. |
| 1738 | app.lruWeight = app.lastActivityTime; |
| 1739 | } else if (app.pubProviders.size() > 0) { |
| 1740 | // If this process contains content providers, we want to keep |
| 1741 | // it a little more strongly. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1742 | app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1743 | // Also don't let it kick out the first few "real" hidden processes. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1744 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1745 | } else { |
| 1746 | // If this process doesn't have activities, we less strongly |
| 1747 | // want to keep it around, and generally want to avoid getting |
| 1748 | // in front of any very recently used activities. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1749 | app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1750 | // Also don't let it kick out the first few "real" hidden processes. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1751 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1752 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1753 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1754 | while (i >= 0) { |
| 1755 | ProcessRecord p = mLruProcesses.get(i); |
| 1756 | // If this app shouldn't be in front of the first N background |
| 1757 | // apps, then skip over that many that are currently hidden. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1758 | if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1759 | skipTop--; |
| 1760 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1761 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1762 | mLruProcesses.add(i+1, app); |
| 1763 | break; |
| 1764 | } |
| 1765 | i--; |
| 1766 | } |
| 1767 | if (i < 0) { |
| 1768 | mLruProcesses.add(0, app); |
| 1769 | } |
| 1770 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1771 | // If the app is currently using a content provider or service, |
| 1772 | // bump those processes as well. |
| 1773 | if (app.connections.size() > 0) { |
| 1774 | for (ConnectionRecord cr : app.connections) { |
| 1775 | if (cr.binding != null && cr.binding.service != null |
| 1776 | && cr.binding.service.app != null |
| 1777 | && cr.binding.service.app.lruSeq != mLruSeq) { |
| 1778 | updateLruProcessInternalLocked(cr.binding.service.app, oomAdj, |
| 1779 | updateActivityTime, i+1); |
| 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | if (app.conProviders.size() > 0) { |
| 1784 | for (ContentProviderRecord cpr : app.conProviders.keySet()) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 1785 | if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) { |
| 1786 | updateLruProcessInternalLocked(cpr.proc, oomAdj, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1787 | updateActivityTime, i+1); |
| 1788 | } |
| 1789 | } |
| 1790 | } |
| 1791 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1792 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1793 | if (oomAdj) { |
| 1794 | updateOomAdjLocked(); |
| 1795 | } |
| 1796 | } |
| 1797 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1798 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1799 | boolean oomAdj, boolean updateActivityTime) { |
| 1800 | mLruSeq++; |
| 1801 | updateLruProcessInternalLocked(app, oomAdj, updateActivityTime, 0); |
| 1802 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1803 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1804 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1805 | String processName, int uid) { |
| 1806 | if (uid == Process.SYSTEM_UID) { |
| 1807 | // The system gets to run in any process. If there are multiple |
| 1808 | // processes with the same uid, just pick the first (this |
| 1809 | // should never happen). |
| 1810 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1811 | processName); |
| 1812 | return procs != null ? procs.valueAt(0) : null; |
| 1813 | } |
| 1814 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1815 | return proc; |
| 1816 | } |
| 1817 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1818 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1819 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1820 | try { |
| 1821 | if (pm.performDexOpt(packageName)) { |
| 1822 | mDidDexOpt = true; |
| 1823 | } |
| 1824 | } catch (RemoteException e) { |
| 1825 | } |
| 1826 | } |
| 1827 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1828 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1829 | int transit = mWindowManager.getPendingAppTransition(); |
| 1830 | return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN |
| 1831 | || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN |
| 1832 | || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT; |
| 1833 | } |
| 1834 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1835 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1836 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1837 | String hostingType, ComponentName hostingName, boolean allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1838 | ProcessRecord app = getProcessRecordLocked(processName, info.uid); |
| 1839 | // We don't have to do anything more if: |
| 1840 | // (1) There is an existing application record; and |
| 1841 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 1842 | // object attached to it so we know it couldn't have crashed; and |
| 1843 | // (3) There is a pid assigned to it, so it is either starting or |
| 1844 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1845 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1846 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 1847 | + " thread=" + (app != null ? app.thread : null) |
| 1848 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1849 | if (app != null && app.pid > 0) { |
| 1850 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1851 | // We already have the app running, or are waiting for it to |
| 1852 | // 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] | 1853 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1854 | // If this is a new package in the process, add the package to the list |
| 1855 | app.addPackage(info.packageName); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1856 | return app; |
| 1857 | } else { |
| 1858 | // An application record is attached to a previous process, |
| 1859 | // clean it up now. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1860 | if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 1861 | handleAppDiedLocked(app, true, true); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1862 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1863 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1864 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1865 | String hostingNameStr = hostingName != null |
| 1866 | ? hostingName.flattenToShortString() : null; |
| 1867 | |
| 1868 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 1869 | // If we are in the background, then check to see if this process |
| 1870 | // is bad. If so, we will just silently fail. |
| 1871 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1872 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 1873 | + "/" + info.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1874 | return null; |
| 1875 | } |
| 1876 | } else { |
| 1877 | // When the user is explicitly starting a process, then clear its |
| 1878 | // crash count so that we won't make it bad until they see at |
| 1879 | // least one crash dialog again, and make the process good again |
| 1880 | // if it had been bad. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1881 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
| 1882 | + "/" + info.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1883 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 1884 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1885 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1886 | info.processName); |
| 1887 | mBadProcesses.remove(info.processName, info.uid); |
| 1888 | if (app != null) { |
| 1889 | app.bad = false; |
| 1890 | } |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | if (app == null) { |
| 1895 | app = newProcessRecordLocked(null, info, processName); |
| 1896 | mProcessNames.put(processName, info.uid, app); |
| 1897 | } else { |
| 1898 | // If this is a new package in the process, add the package to the list |
| 1899 | app.addPackage(info.packageName); |
| 1900 | } |
| 1901 | |
| 1902 | // If the system is not ready yet, then hold off on starting this |
| 1903 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1904 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1905 | && !isAllowedWhileBooting(info) |
| 1906 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1907 | if (!mProcessesOnHold.contains(app)) { |
| 1908 | mProcessesOnHold.add(app); |
| 1909 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1910 | 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] | 1911 | return app; |
| 1912 | } |
| 1913 | |
| 1914 | startProcessLocked(app, hostingType, hostingNameStr); |
| 1915 | return (app.pid != 0) ? app : null; |
| 1916 | } |
| 1917 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1918 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 1919 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 1920 | } |
| 1921 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1922 | private final void startProcessLocked(ProcessRecord app, |
| 1923 | String hostingType, String hostingNameStr) { |
| 1924 | if (app.pid > 0 && app.pid != MY_PID) { |
| 1925 | synchronized (mPidsSelfLocked) { |
| 1926 | mPidsSelfLocked.remove(app.pid); |
| 1927 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 1928 | } |
| 1929 | app.pid = 0; |
| 1930 | } |
| 1931 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1932 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 1933 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1934 | mProcessesOnHold.remove(app); |
| 1935 | |
| 1936 | updateCpuStats(); |
| 1937 | |
| 1938 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 1939 | mProcDeaths[0] = 0; |
| 1940 | |
| 1941 | try { |
| 1942 | int uid = app.info.uid; |
| 1943 | int[] gids = null; |
| 1944 | try { |
| 1945 | gids = mContext.getPackageManager().getPackageGids( |
| 1946 | app.info.packageName); |
| 1947 | } catch (PackageManager.NameNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1948 | Slog.w(TAG, "Unable to retrieve gids", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1949 | } |
| 1950 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 1951 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 1952 | && mTopComponent != null |
| 1953 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 1954 | uid = 0; |
| 1955 | } |
| 1956 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 1957 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 1958 | uid = 0; |
| 1959 | } |
| 1960 | } |
| 1961 | int debugFlags = 0; |
| 1962 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 1963 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
Elliott Hughes | fa36aee | 2011-06-17 14:39:41 -0700 | [diff] [blame] | 1964 | // Also turn on CheckJNI for debuggable apps. It's quite |
| 1965 | // awkward to turn on otherwise. |
| 1966 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1967 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 1968 | // Run the app in safe mode if its manifest requests so or the |
| 1969 | // system is booted in safe mode. |
| 1970 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 1971 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 1972 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 1973 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1974 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 1975 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 1976 | } |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 1977 | if ("1".equals(SystemProperties.get("debug.jni.logging"))) { |
| 1978 | debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING; |
| 1979 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1980 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 1981 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 1982 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1983 | |
| 1984 | // Start the process. It will either succeed and return a result containing |
| 1985 | // the PID of the new process, or else throw a RuntimeException. |
| 1986 | Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread", |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 1987 | app.processName, uid, uid, gids, debugFlags, |
| 1988 | app.info.targetSdkVersion, null); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1989 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1990 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 1991 | synchronized (bs) { |
| 1992 | if (bs.isOnBattery()) { |
| 1993 | app.batteryStats.incStartsLocked(); |
| 1994 | } |
| 1995 | } |
| 1996 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 1997 | EventLog.writeEvent(EventLogTags.AM_PROC_START, startResult.pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1998 | app.processName, hostingType, |
| 1999 | hostingNameStr != null ? hostingNameStr : ""); |
| 2000 | |
| 2001 | if (app.persistent) { |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2002 | Watchdog.getInstance().processStarted(app.processName, startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2003 | } |
| 2004 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 2005 | StringBuilder buf = mStringBuilder; |
| 2006 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2007 | buf.append("Start proc "); |
| 2008 | buf.append(app.processName); |
| 2009 | buf.append(" for "); |
| 2010 | buf.append(hostingType); |
| 2011 | if (hostingNameStr != null) { |
| 2012 | buf.append(" "); |
| 2013 | buf.append(hostingNameStr); |
| 2014 | } |
| 2015 | buf.append(": pid="); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2016 | buf.append(startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2017 | buf.append(" uid="); |
| 2018 | buf.append(uid); |
| 2019 | buf.append(" gids={"); |
| 2020 | if (gids != null) { |
| 2021 | for (int gi=0; gi<gids.length; gi++) { |
| 2022 | if (gi != 0) buf.append(", "); |
| 2023 | buf.append(gids[gi]); |
| 2024 | |
| 2025 | } |
| 2026 | } |
| 2027 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2028 | Slog.i(TAG, buf.toString()); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2029 | app.pid = startResult.pid; |
| 2030 | app.usingWrapper = startResult.usingWrapper; |
| 2031 | app.removed = false; |
| 2032 | synchronized (mPidsSelfLocked) { |
| 2033 | this.mPidsSelfLocked.put(startResult.pid, app); |
| 2034 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 2035 | msg.obj = app; |
| 2036 | mHandler.sendMessageDelayed(msg, startResult.usingWrapper |
| 2037 | ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2038 | } |
| 2039 | } catch (RuntimeException e) { |
| 2040 | // XXX do better error recovery. |
| 2041 | app.pid = 0; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2042 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2043 | } |
| 2044 | } |
| 2045 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2046 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2047 | if (resumed) { |
| 2048 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 2049 | } else { |
| 2050 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 2051 | } |
| 2052 | } |
| 2053 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2054 | boolean startHomeActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2055 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2056 | && mTopAction == null) { |
| 2057 | // We are running in factory test mode, but unable to find |
| 2058 | // the factory test app, so just sit around displaying the |
| 2059 | // error message and don't try to start anything. |
| 2060 | return false; |
| 2061 | } |
| 2062 | Intent intent = new Intent( |
| 2063 | mTopAction, |
| 2064 | mTopData != null ? Uri.parse(mTopData) : null); |
| 2065 | intent.setComponent(mTopComponent); |
| 2066 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 2067 | intent.addCategory(Intent.CATEGORY_HOME); |
| 2068 | } |
| 2069 | ActivityInfo aInfo = |
| 2070 | intent.resolveActivityInfo(mContext.getPackageManager(), |
| 2071 | STOCK_PM_FLAGS); |
| 2072 | if (aInfo != null) { |
| 2073 | intent.setComponent(new ComponentName( |
| 2074 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2075 | // Don't do this if the home app is currently being |
| 2076 | // instrumented. |
| 2077 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 2078 | aInfo.applicationInfo.uid); |
| 2079 | if (app == null || app.instrumentationClass == null) { |
| 2080 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2081 | mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2082 | null, null, 0, 0, 0, false, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | |
| 2087 | return true; |
| 2088 | } |
| 2089 | |
| 2090 | /** |
| 2091 | * Starts the "new version setup screen" if appropriate. |
| 2092 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2093 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2094 | // Only do this once per boot. |
| 2095 | if (mCheckedForSetup) { |
| 2096 | return; |
| 2097 | } |
| 2098 | |
| 2099 | // We will show this screen if the current one is a different |
| 2100 | // version than the last one shown, and we are not running in |
| 2101 | // low-level factory test mode. |
| 2102 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2103 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
| 2104 | Settings.Secure.getInt(resolver, |
| 2105 | Settings.Secure.DEVICE_PROVISIONED, 0) != 0) { |
| 2106 | mCheckedForSetup = true; |
| 2107 | |
| 2108 | // See if we should be showing the platform update setup UI. |
| 2109 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 2110 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 2111 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 2112 | |
| 2113 | // We don't allow third party apps to replace this. |
| 2114 | ResolveInfo ri = null; |
| 2115 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 2116 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 2117 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2118 | ri = ris.get(i); |
| 2119 | break; |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | if (ri != null) { |
| 2124 | String vers = ri.activityInfo.metaData != null |
| 2125 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 2126 | : null; |
| 2127 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2128 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2129 | Intent.METADATA_SETUP_VERSION); |
| 2130 | } |
| 2131 | String lastVers = Settings.Secure.getString( |
| 2132 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2133 | if (vers != null && !vers.equals(lastVers)) { |
| 2134 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2135 | intent.setComponent(new ComponentName( |
| 2136 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2137 | mMainStack.startActivityLocked(null, intent, null, null, 0, ri.activityInfo, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2138 | null, null, 0, 0, 0, false, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2139 | } |
| 2140 | } |
| 2141 | } |
| 2142 | } |
| 2143 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2144 | CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2145 | return mCompatModePackages.compatibilityInfoForPackageLocked(ai); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2146 | } |
| 2147 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2148 | public int getFrontActivityScreenCompatMode() { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2149 | synchronized (this) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2150 | return mCompatModePackages.getFrontActivityScreenCompatModeLocked(); |
| 2151 | } |
| 2152 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2153 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2154 | public void setFrontActivityScreenCompatMode(int mode) { |
| 2155 | synchronized (this) { |
| 2156 | mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode); |
| 2157 | } |
| 2158 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2159 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2160 | public int getPackageScreenCompatMode(String packageName) { |
| 2161 | synchronized (this) { |
| 2162 | return mCompatModePackages.getPackageScreenCompatModeLocked(packageName); |
| 2163 | } |
| 2164 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2165 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2166 | public void setPackageScreenCompatMode(String packageName, int mode) { |
| 2167 | synchronized (this) { |
| 2168 | mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2169 | } |
| 2170 | } |
| 2171 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2172 | public boolean getPackageAskScreenCompat(String packageName) { |
| 2173 | synchronized (this) { |
| 2174 | return mCompatModePackages.getPackageAskCompatModeLocked(packageName); |
| 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | public void setPackageAskScreenCompat(String packageName, boolean ask) { |
| 2179 | synchronized (this) { |
| 2180 | mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask); |
| 2181 | } |
| 2182 | } |
| 2183 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2184 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2185 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2186 | |
| 2187 | final int identHash = System.identityHashCode(r); |
| 2188 | updateUsageStats(r, true); |
| 2189 | |
| 2190 | int i = mWatchers.beginBroadcast(); |
| 2191 | while (i > 0) { |
| 2192 | i--; |
| 2193 | IActivityWatcher w = mWatchers.getBroadcastItem(i); |
| 2194 | if (w != null) { |
| 2195 | try { |
| 2196 | w.activityResuming(identHash); |
| 2197 | } catch (RemoteException e) { |
| 2198 | } |
| 2199 | } |
| 2200 | } |
| 2201 | mWatchers.finishBroadcast(); |
| 2202 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2203 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2204 | private void dispatchForegroundActivitiesChanged(int pid, int uid, boolean foregroundActivities) { |
| 2205 | int i = mProcessObservers.beginBroadcast(); |
| 2206 | while (i > 0) { |
| 2207 | i--; |
| 2208 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2209 | if (observer != null) { |
| 2210 | try { |
| 2211 | observer.onForegroundActivitiesChanged(pid, uid, foregroundActivities); |
| 2212 | } catch (RemoteException e) { |
| 2213 | } |
| 2214 | } |
| 2215 | } |
| 2216 | mProcessObservers.finishBroadcast(); |
| 2217 | } |
| 2218 | |
| 2219 | private void dispatchProcessDied(int pid, int uid) { |
| 2220 | int i = mProcessObservers.beginBroadcast(); |
| 2221 | while (i > 0) { |
| 2222 | i--; |
| 2223 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2224 | if (observer != null) { |
| 2225 | try { |
| 2226 | observer.onProcessDied(pid, uid); |
| 2227 | } catch (RemoteException e) { |
| 2228 | } |
| 2229 | } |
| 2230 | } |
| 2231 | mProcessObservers.finishBroadcast(); |
| 2232 | } |
| 2233 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2234 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2235 | final int N = mPendingActivityLaunches.size(); |
| 2236 | if (N <= 0) { |
| 2237 | return; |
| 2238 | } |
| 2239 | for (int i=0; i<N; i++) { |
| 2240 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2241 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2242 | pal.grantedUriPermissions, pal.grantedMode, pal.onlyIfNeeded, |
| 2243 | doResume && i == (N-1)); |
| 2244 | } |
| 2245 | mPendingActivityLaunches.clear(); |
| 2246 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2247 | |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2248 | public final int startActivity(IApplicationThread caller, |
| 2249 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2250 | int grantedMode, IBinder resultTo, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2251 | String resultWho, int requestCode, boolean onlyIfNeeded, boolean debug, |
| 2252 | String profileFile, ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2253 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2254 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2255 | requestCode, onlyIfNeeded, debug, profileFile, profileFd, autoStopProfiler, |
| 2256 | null, null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | public final WaitResult startActivityAndWait(IApplicationThread caller, |
| 2260 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2261 | int grantedMode, IBinder resultTo, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2262 | String resultWho, int requestCode, boolean onlyIfNeeded, boolean debug, |
| 2263 | String profileFile, ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2264 | WaitResult res = new WaitResult(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2265 | mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2266 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2267 | requestCode, onlyIfNeeded, debug, profileFile, profileFd, autoStopProfiler, |
| 2268 | res, null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2269 | return res; |
| 2270 | } |
| 2271 | |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2272 | public final int startActivityWithConfig(IApplicationThread caller, |
| 2273 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2274 | int grantedMode, IBinder resultTo, |
| 2275 | String resultWho, int requestCode, boolean onlyIfNeeded, |
| 2276 | boolean debug, Configuration config) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2277 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2278 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2279 | requestCode, onlyIfNeeded, debug, null, null, false, null, config); |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2282 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2283 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2284 | IBinder resultTo, String resultWho, int requestCode, |
| 2285 | int flagsMask, int flagsValues) { |
| 2286 | // Refuse possible leaked file descriptors |
| 2287 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2288 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2289 | } |
| 2290 | |
| 2291 | IIntentSender sender = intent.getTarget(); |
| 2292 | if (!(sender instanceof PendingIntentRecord)) { |
| 2293 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2294 | } |
| 2295 | |
| 2296 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2297 | |
| 2298 | synchronized (this) { |
| 2299 | // If this is coming from the currently resumed activity, it is |
| 2300 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2301 | if (mMainStack.mResumedActivity != null |
| 2302 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2303 | Binder.getCallingUid()) { |
| 2304 | mAppSwitchesAllowedTime = 0; |
| 2305 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2306 | } |
| 2307 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 2308 | return pir.sendInner(0, fillInIntent, resolvedType, null, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2309 | null, resultTo, resultWho, requestCode, flagsMask, flagsValues); |
| 2310 | } |
| 2311 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2312 | public boolean startNextMatchingActivity(IBinder callingActivity, |
| 2313 | Intent intent) { |
| 2314 | // Refuse possible leaked file descriptors |
| 2315 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2316 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2317 | } |
| 2318 | |
| 2319 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2320 | ActivityRecord r = mMainStack.isInStackLocked(callingActivity); |
| 2321 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2322 | return false; |
| 2323 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2324 | if (r.app == null || r.app.thread == null) { |
| 2325 | // The caller is not running... d'oh! |
| 2326 | return false; |
| 2327 | } |
| 2328 | intent = new Intent(intent); |
| 2329 | // The caller is not allowed to change the data. |
| 2330 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2331 | // And we are resetting to find the next component... |
| 2332 | intent.setComponent(null); |
| 2333 | |
| 2334 | ActivityInfo aInfo = null; |
| 2335 | try { |
| 2336 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2337 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2338 | intent, r.resolvedType, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 2339 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2340 | |
| 2341 | // Look for the original activity in the list... |
| 2342 | final int N = resolves != null ? resolves.size() : 0; |
| 2343 | for (int i=0; i<N; i++) { |
| 2344 | ResolveInfo rInfo = resolves.get(i); |
| 2345 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2346 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2347 | // We found the current one... the next matching is |
| 2348 | // after it. |
| 2349 | i++; |
| 2350 | if (i<N) { |
| 2351 | aInfo = resolves.get(i).activityInfo; |
| 2352 | } |
| 2353 | break; |
| 2354 | } |
| 2355 | } |
| 2356 | } catch (RemoteException e) { |
| 2357 | } |
| 2358 | |
| 2359 | if (aInfo == null) { |
| 2360 | // Nobody who is next! |
| 2361 | return false; |
| 2362 | } |
| 2363 | |
| 2364 | intent.setComponent(new ComponentName( |
| 2365 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2366 | intent.setFlags(intent.getFlags()&~( |
| 2367 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2368 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2369 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2370 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2371 | |
| 2372 | // Okay now we need to start the new activity, replacing the |
| 2373 | // currently running activity. This is a little tricky because |
| 2374 | // we want to start the new one as if the current one is finished, |
| 2375 | // but not finish the current one first so that there is no flicker. |
| 2376 | // And thus... |
| 2377 | final boolean wasFinishing = r.finishing; |
| 2378 | r.finishing = true; |
| 2379 | |
| 2380 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2381 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2382 | final String resultWho = r.resultWho; |
| 2383 | final int requestCode = r.requestCode; |
| 2384 | r.resultTo = null; |
| 2385 | if (resultTo != null) { |
| 2386 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2387 | } |
| 2388 | |
| 2389 | final long origId = Binder.clearCallingIdentity(); |
| 2390 | // XXX we are not dealing with propagating grantedUriPermissions... |
| 2391 | // 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] | 2392 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2393 | r.resolvedType, null, 0, aInfo, |
| 2394 | resultTo != null ? resultTo.appToken : null, resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2395 | requestCode, -1, r.launchedFromUid, false, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2396 | Binder.restoreCallingIdentity(origId); |
| 2397 | |
| 2398 | r.finishing = wasFinishing; |
| 2399 | if (res != START_SUCCESS) { |
| 2400 | return false; |
| 2401 | } |
| 2402 | return true; |
| 2403 | } |
| 2404 | } |
| 2405 | |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2406 | public final int startActivityInPackage(int uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2407 | Intent intent, String resolvedType, IBinder resultTo, |
| 2408 | String resultWho, int requestCode, boolean onlyIfNeeded) { |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2409 | |
| 2410 | // This is so super not safe, that only the system (or okay root) |
| 2411 | // can do it. |
| 2412 | final int callingUid = Binder.getCallingUid(); |
| 2413 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2414 | throw new SecurityException( |
| 2415 | "startActivityInPackage only available to the system"); |
| 2416 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2417 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2418 | return mMainStack.startActivityMayWait(null, uid, intent, resolvedType, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2419 | null, 0, resultTo, resultWho, requestCode, onlyIfNeeded, false, |
| 2420 | null, null, false, null, null); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2421 | } |
| 2422 | |
| 2423 | public final int startActivities(IApplicationThread caller, |
| 2424 | Intent[] intents, String[] resolvedTypes, IBinder resultTo) { |
| 2425 | return mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo); |
| 2426 | } |
| 2427 | |
| 2428 | public final int startActivitiesInPackage(int uid, |
| 2429 | Intent[] intents, String[] resolvedTypes, IBinder resultTo) { |
| 2430 | |
| 2431 | // This is so super not safe, that only the system (or okay root) |
| 2432 | // can do it. |
| 2433 | final int callingUid = Binder.getCallingUid(); |
| 2434 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2435 | throw new SecurityException( |
| 2436 | "startActivityInPackage only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2437 | } |
| 2438 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2439 | return mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2440 | } |
| 2441 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2442 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2443 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2444 | // Quick case: check if the top-most recent task is the same. |
| 2445 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2446 | return; |
| 2447 | } |
| 2448 | // 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] | 2449 | for (int i=0; i<N; i++) { |
| 2450 | TaskRecord tr = mRecentTasks.get(i); |
| 2451 | if ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2452 | || (task.intent != null && task.intent.filterEquals(tr.intent))) { |
| 2453 | mRecentTasks.remove(i); |
| 2454 | i--; |
| 2455 | N--; |
| 2456 | if (task.intent == null) { |
| 2457 | // If the new recent task we are adding is not fully |
| 2458 | // specified, then replace it with the existing recent task. |
| 2459 | task = tr; |
| 2460 | } |
| 2461 | } |
| 2462 | } |
| 2463 | if (N >= MAX_RECENT_TASKS) { |
| 2464 | mRecentTasks.remove(N-1); |
| 2465 | } |
| 2466 | mRecentTasks.add(0, task); |
| 2467 | } |
| 2468 | |
| 2469 | public void setRequestedOrientation(IBinder token, |
| 2470 | int requestedOrientation) { |
| 2471 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2472 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2473 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2474 | return; |
| 2475 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2476 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2477 | mWindowManager.setAppOrientation(r.appToken, requestedOrientation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2478 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2479 | mConfiguration, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2480 | r.mayFreezeScreenLocked(r.app) ? r.appToken : null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2481 | if (config != null) { |
| 2482 | r.frozenBeforeDestroy = true; |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 2483 | if (!updateConfigurationLocked(config, r, false, false)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2484 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2485 | } |
| 2486 | } |
| 2487 | Binder.restoreCallingIdentity(origId); |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | public int getRequestedOrientation(IBinder token) { |
| 2492 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2493 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2494 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2495 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2496 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2497 | return mWindowManager.getAppOrientation(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2498 | } |
| 2499 | } |
| 2500 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2501 | /** |
| 2502 | * This is the internal entry point for handling Activity.finish(). |
| 2503 | * |
| 2504 | * @param token The Binder token referencing the Activity we want to finish. |
| 2505 | * @param resultCode Result code, if any, from this Activity. |
| 2506 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2507 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2508 | * @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] | 2509 | */ |
| 2510 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2511 | // Refuse possible leaked file descriptors |
| 2512 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2513 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2514 | } |
| 2515 | |
| 2516 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2517 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2518 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2519 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2520 | if (next != null) { |
| 2521 | // ask watcher if this is allowed |
| 2522 | boolean resumeOK = true; |
| 2523 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2524 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2525 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2526 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | if (!resumeOK) { |
| 2530 | return false; |
| 2531 | } |
| 2532 | } |
| 2533 | } |
| 2534 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2535 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2536 | resultData, "app-request"); |
| 2537 | Binder.restoreCallingIdentity(origId); |
| 2538 | return res; |
| 2539 | } |
| 2540 | } |
| 2541 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2542 | public final void finishHeavyWeightApp() { |
| 2543 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2544 | != PackageManager.PERMISSION_GRANTED) { |
| 2545 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2546 | + Binder.getCallingPid() |
| 2547 | + ", uid=" + Binder.getCallingUid() |
| 2548 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2549 | Slog.w(TAG, msg); |
| 2550 | throw new SecurityException(msg); |
| 2551 | } |
| 2552 | |
| 2553 | synchronized(this) { |
| 2554 | if (mHeavyWeightProcess == null) { |
| 2555 | return; |
| 2556 | } |
| 2557 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2558 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2559 | mHeavyWeightProcess.activities); |
| 2560 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2561 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2562 | if (!r.finishing) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2563 | int index = mMainStack.indexOfTokenLocked(r.appToken); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2564 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2565 | mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED, |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2566 | null, "finish-heavy"); |
| 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | mHeavyWeightProcess = null; |
| 2572 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 2573 | } |
| 2574 | } |
| 2575 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2576 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2577 | String message) { |
| 2578 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2579 | != PackageManager.PERMISSION_GRANTED) { |
| 2580 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2581 | + Binder.getCallingPid() |
| 2582 | + ", uid=" + Binder.getCallingUid() |
| 2583 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2584 | Slog.w(TAG, msg); |
| 2585 | throw new SecurityException(msg); |
| 2586 | } |
| 2587 | |
| 2588 | synchronized(this) { |
| 2589 | ProcessRecord proc = null; |
| 2590 | |
| 2591 | // Figure out which process to kill. We don't trust that initialPid |
| 2592 | // still has any relation to current pids, so must scan through the |
| 2593 | // list. |
| 2594 | synchronized (mPidsSelfLocked) { |
| 2595 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2596 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
| 2597 | if (p.info.uid != uid) { |
| 2598 | continue; |
| 2599 | } |
| 2600 | if (p.pid == initialPid) { |
| 2601 | proc = p; |
| 2602 | break; |
| 2603 | } |
| 2604 | for (String str : p.pkgList) { |
| 2605 | if (str.equals(packageName)) { |
| 2606 | proc = p; |
| 2607 | } |
| 2608 | } |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2613 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2614 | + " initialPid=" + initialPid |
| 2615 | + " packageName=" + packageName); |
| 2616 | return; |
| 2617 | } |
| 2618 | |
| 2619 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2620 | if (proc.pid == Process.myPid()) { |
| 2621 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2622 | return; |
| 2623 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2624 | long ident = Binder.clearCallingIdentity(); |
| 2625 | try { |
| 2626 | proc.thread.scheduleCrash(message); |
| 2627 | } catch (RemoteException e) { |
| 2628 | } |
| 2629 | Binder.restoreCallingIdentity(ident); |
| 2630 | } |
| 2631 | } |
| 2632 | } |
| 2633 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2634 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2635 | int requestCode) { |
| 2636 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2637 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2638 | if (self == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2639 | return; |
| 2640 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2641 | |
| 2642 | final long origId = Binder.clearCallingIdentity(); |
| 2643 | |
| 2644 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2645 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2646 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2647 | if (r.resultTo == self && r.requestCode == requestCode) { |
| 2648 | if ((r.resultWho == null && resultWho == null) || |
| 2649 | (r.resultWho != null && r.resultWho.equals(resultWho))) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2650 | mMainStack.finishActivityLocked(r, i, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2651 | Activity.RESULT_CANCELED, null, "request-sub"); |
| 2652 | } |
| 2653 | } |
| 2654 | } |
| 2655 | |
| 2656 | Binder.restoreCallingIdentity(origId); |
| 2657 | } |
| 2658 | } |
| 2659 | |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2660 | public boolean willActivityBeVisible(IBinder token) { |
| 2661 | synchronized(this) { |
| 2662 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2663 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2664 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2665 | if (r.appToken == token) { |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2666 | return true; |
| 2667 | } |
| 2668 | if (r.fullscreen && !r.finishing) { |
| 2669 | return false; |
| 2670 | } |
| 2671 | } |
| 2672 | return true; |
| 2673 | } |
| 2674 | } |
| 2675 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2676 | public void overridePendingTransition(IBinder token, String packageName, |
| 2677 | int enterAnim, int exitAnim) { |
| 2678 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2679 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2680 | if (self == null) { |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2681 | return; |
| 2682 | } |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2683 | |
| 2684 | final long origId = Binder.clearCallingIdentity(); |
| 2685 | |
| 2686 | if (self.state == ActivityState.RESUMED |
| 2687 | || self.state == ActivityState.PAUSING) { |
| 2688 | mWindowManager.overridePendingAppTransition(packageName, |
| 2689 | enterAnim, exitAnim); |
| 2690 | } |
| 2691 | |
| 2692 | Binder.restoreCallingIdentity(origId); |
| 2693 | } |
| 2694 | } |
| 2695 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2696 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2697 | * Main function for removing an existing process from the activity manager |
| 2698 | * as a result of that process going away. Clears out all connections |
| 2699 | * to the process. |
| 2700 | */ |
| 2701 | private final void handleAppDiedLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2702 | boolean restarting, boolean allowRestart) { |
| 2703 | cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2704 | if (!restarting) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2705 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2706 | } |
| 2707 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2708 | if (mProfileProc == app) { |
| 2709 | clearProfilerLocked(); |
| 2710 | } |
| 2711 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2712 | // Just in case... |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2713 | if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) { |
| 2714 | if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity); |
| 2715 | mMainStack.mPausingActivity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2716 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2717 | if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) { |
| 2718 | mMainStack.mLastPausedActivity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | // Remove this application's activities from active lists. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2722 | mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2723 | |
| 2724 | boolean atTop = true; |
| 2725 | boolean hasVisibleActivities = false; |
| 2726 | |
| 2727 | // Clean out the history list. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2728 | int i = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2729 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2730 | TAG, "Removing app " + app + " from history with " + i + " entries"); |
| 2731 | while (i > 0) { |
| 2732 | i--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2733 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2734 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2735 | TAG, "Record #" + i + " " + r + ": app=" + r.app); |
| 2736 | if (r.app == app) { |
| 2737 | if ((!r.haveState && !r.stateNotNeeded) || r.finishing) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2738 | if (ActivityStack.DEBUG_ADD_REMOVE) { |
| 2739 | RuntimeException here = new RuntimeException("here"); |
| 2740 | here.fillInStackTrace(); |
| 2741 | Slog.i(TAG, "Removing activity " + r + " from stack at " + i |
| 2742 | + ": haveState=" + r.haveState |
| 2743 | + " stateNotNeeded=" + r.stateNotNeeded |
| 2744 | + " finishing=" + r.finishing |
| 2745 | + " state=" + r.state, here); |
| 2746 | } |
| 2747 | if (!r.finishing) { |
| 2748 | Slog.w(TAG, "Force removing " + r + ": app died, no saved state"); |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 2749 | EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY, |
| 2750 | System.identityHashCode(r), |
| 2751 | r.task.taskId, r.shortComponentName, |
| 2752 | "proc died without state saved"); |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2753 | } |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 2754 | r.makeFinishing(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2755 | mMainStack.mHistory.remove(i); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 2756 | r.takeFromHistory(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2757 | mWindowManager.removeAppToken(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2758 | if (VALIDATE_TOKENS) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2759 | mMainStack.validateAppTokensLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2760 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2761 | r.removeUriPermissionsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2762 | |
| 2763 | } else { |
| 2764 | // We have the current state for this activity, so |
| 2765 | // it can be restarted later when needed. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2766 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2767 | TAG, "Keeping entry, setting app to null"); |
| 2768 | if (r.visible) { |
| 2769 | hasVisibleActivities = true; |
| 2770 | } |
| 2771 | r.app = null; |
| 2772 | r.nowVisible = false; |
| 2773 | if (!r.haveState) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2774 | if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG, |
| 2775 | "App died, clearing saved state of " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2776 | r.icicle = null; |
| 2777 | } |
| 2778 | } |
| 2779 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2780 | r.stack.cleanUpActivityLocked(r, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2781 | } |
| 2782 | atTop = false; |
| 2783 | } |
| 2784 | |
| 2785 | app.activities.clear(); |
| 2786 | |
| 2787 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2788 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2789 | + " running instrumentation " + app.instrumentationClass); |
| 2790 | Bundle info = new Bundle(); |
| 2791 | info.putString("shortMsg", "Process crashed."); |
| 2792 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 2793 | } |
| 2794 | |
| 2795 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2796 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2797 | // If there was nothing to resume, and we are not already |
| 2798 | // restarting this process, but there is a visible activity that |
| 2799 | // is hosted by the process... then make sure all visible |
| 2800 | // activities are running, taking care of restarting this |
| 2801 | // process. |
| 2802 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2803 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2804 | } |
| 2805 | } |
| 2806 | } |
| 2807 | } |
| 2808 | |
| 2809 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 2810 | IBinder threadBinder = thread.asBinder(); |
| 2811 | |
| 2812 | // Find the application record. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2813 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2814 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2815 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 2816 | return i; |
| 2817 | } |
| 2818 | } |
| 2819 | return -1; |
| 2820 | } |
| 2821 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2822 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2823 | IApplicationThread thread) { |
| 2824 | if (thread == null) { |
| 2825 | return null; |
| 2826 | } |
| 2827 | |
| 2828 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2829 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2830 | } |
| 2831 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2832 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2833 | IApplicationThread thread) { |
| 2834 | |
| 2835 | mProcDeaths[0]++; |
| 2836 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2837 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 2838 | synchronized (stats) { |
| 2839 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 2840 | } |
| 2841 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2842 | // Clean up already done if the process has been re-started. |
| 2843 | if (app.pid == pid && app.thread != null && |
| 2844 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 2845 | if (!app.killedBackground) { |
| 2846 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 2847 | + ") has died."); |
| 2848 | } |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 2849 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2850 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2851 | TAG, "Dying app: " + app + ", pid: " + pid |
| 2852 | + ", thread: " + thread.asBinder()); |
| 2853 | boolean doLowMem = app.instrumentationClass == null; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2854 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2855 | |
| 2856 | if (doLowMem) { |
| 2857 | // If there are no longer any background processes running, |
| 2858 | // and the app that died was not running instrumentation, |
| 2859 | // then tell everyone we are now low on memory. |
| 2860 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2861 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2862 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2863 | if (rec.thread != null && rec.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2864 | haveBg = true; |
| 2865 | break; |
| 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | if (!haveBg) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2870 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2871 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2872 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2873 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 2874 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2875 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 2876 | // The low memory report is overriding any current |
| 2877 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2878 | // heavy/important/visible/foreground processes first. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2879 | if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2880 | rec.lastRequestedGc = 0; |
| 2881 | } else { |
| 2882 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2883 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2884 | rec.reportLowMemory = true; |
| 2885 | rec.lastLowMemory = now; |
| 2886 | mProcessesToGc.remove(rec); |
| 2887 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2888 | } |
| 2889 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 2890 | mHandler.sendEmptyMessage(REPORT_MEM_USAGE); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2891 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2892 | } |
| 2893 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2894 | } else if (app.pid != pid) { |
| 2895 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2896 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2897 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | 28a8c2b | 2010-03-01 11:30:02 -0800 | [diff] [blame] | 2898 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 2899 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2900 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2901 | + thread.asBinder()); |
| 2902 | } |
| 2903 | } |
| 2904 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2905 | /** |
| 2906 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2907 | * @param clearTraces causes the dump file to be erased prior to the new |
| 2908 | * traces being written, if true; when false, the new traces will be |
| 2909 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2910 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 2911 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2912 | * @return file containing stack traces, or null if no dump file is configured |
| 2913 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2914 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
| 2915 | ProcessStats processStats, SparseArray<Boolean> lastPids) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2916 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 2917 | if (tracesPath == null || tracesPath.length() == 0) { |
| 2918 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2919 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2920 | |
| 2921 | File tracesFile = new File(tracesPath); |
| 2922 | try { |
| 2923 | File tracesDir = tracesFile.getParentFile(); |
| 2924 | if (!tracesDir.exists()) tracesFile.mkdirs(); |
| 2925 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 2926 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2927 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2928 | tracesFile.createNewFile(); |
| 2929 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 2930 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2931 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2932 | return null; |
| 2933 | } |
| 2934 | |
| 2935 | // Use a FileObserver to detect when traces finish writing. |
| 2936 | // The order of traces is considered important to maintain for legibility. |
| 2937 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 2938 | public synchronized void onEvent(int event, String path) { notify(); } |
| 2939 | }; |
| 2940 | |
| 2941 | try { |
| 2942 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2943 | |
| 2944 | // First collect all of the stacks of the most important pids. |
| 2945 | try { |
| 2946 | int num = firstPids.size(); |
| 2947 | for (int i = 0; i < num; i++) { |
| 2948 | synchronized (observer) { |
| 2949 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 2950 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 2951 | } |
| 2952 | } |
| 2953 | } catch (InterruptedException e) { |
| 2954 | Log.wtf(TAG, e); |
| 2955 | } |
| 2956 | |
| 2957 | // Next measure CPU usage. |
| 2958 | if (processStats != null) { |
| 2959 | processStats.init(); |
| 2960 | System.gc(); |
| 2961 | processStats.update(); |
| 2962 | try { |
| 2963 | synchronized (processStats) { |
| 2964 | processStats.wait(500); // measure over 1/2 second. |
| 2965 | } |
| 2966 | } catch (InterruptedException e) { |
| 2967 | } |
| 2968 | processStats.update(); |
| 2969 | |
| 2970 | // We'll take the stack crawls of just the top apps using CPU. |
| 2971 | final int N = processStats.countWorkingStats(); |
| 2972 | int numProcs = 0; |
| 2973 | for (int i=0; i<N && numProcs<5; i++) { |
| 2974 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 2975 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 2976 | numProcs++; |
| 2977 | try { |
| 2978 | synchronized (observer) { |
| 2979 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 2980 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 2981 | } |
| 2982 | } catch (InterruptedException e) { |
| 2983 | Log.wtf(TAG, e); |
| 2984 | } |
| 2985 | |
| 2986 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2987 | } |
| 2988 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2989 | |
| 2990 | return tracesFile; |
| 2991 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2992 | } finally { |
| 2993 | observer.stopWatching(); |
| 2994 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2995 | } |
| 2996 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 2997 | private final class AppNotResponding implements Runnable { |
| 2998 | private final ProcessRecord mApp; |
| 2999 | private final String mAnnotation; |
| 3000 | |
| 3001 | public AppNotResponding(ProcessRecord app, String annotation) { |
| 3002 | mApp = app; |
| 3003 | mAnnotation = annotation; |
| 3004 | } |
| 3005 | |
| 3006 | @Override |
| 3007 | public void run() { |
| 3008 | appNotResponding(mApp, null, null, mAnnotation); |
| 3009 | } |
| 3010 | } |
| 3011 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3012 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
| 3013 | ActivityRecord parent, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3014 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 3015 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 3016 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3017 | if (mController != null) { |
| 3018 | try { |
| 3019 | // 0 == continue, -1 = kill process immediately |
| 3020 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 3021 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3022 | } catch (RemoteException e) { |
| 3023 | mController = null; |
| 3024 | } |
| 3025 | } |
| 3026 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3027 | long anrTime = SystemClock.uptimeMillis(); |
| 3028 | if (MONITOR_CPU_USAGE) { |
| 3029 | updateCpuStatsNow(); |
| 3030 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3031 | |
| 3032 | synchronized (this) { |
| 3033 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 3034 | if (mShuttingDown) { |
| 3035 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 3036 | return; |
| 3037 | } else if (app.notResponding) { |
| 3038 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 3039 | return; |
| 3040 | } else if (app.crashing) { |
| 3041 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 3042 | return; |
| 3043 | } |
| 3044 | |
| 3045 | // In case we come through here for the same app before completing |
| 3046 | // this one, mark as anring now so we will bail out. |
| 3047 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3048 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3049 | // Log the ANR to the event log. |
| 3050 | EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags, |
| 3051 | annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3052 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3053 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3054 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3055 | |
| 3056 | int parentPid = app.pid; |
| 3057 | 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] | 3058 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3059 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3060 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3061 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3062 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 3063 | ProcessRecord r = mLruProcesses.get(i); |
| 3064 | if (r != null && r.thread != null) { |
| 3065 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3066 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 3067 | if (r.persistent) { |
| 3068 | firstPids.add(pid); |
| 3069 | } else { |
| 3070 | lastPids.put(pid, Boolean.TRUE); |
| 3071 | } |
| 3072 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3073 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3074 | } |
| 3075 | } |
| 3076 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3077 | // Log the ANR to the main log. |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3078 | StringBuilder info = mStringBuilder; |
| 3079 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3080 | info.append("ANR in ").append(app.processName); |
| 3081 | if (activity != null && activity.shortComponentName != null) { |
| 3082 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 3083 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3084 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3085 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3086 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3087 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3088 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3089 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3090 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3091 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3092 | final ProcessStats processStats = new ProcessStats(true); |
| 3093 | |
| 3094 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids); |
| 3095 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3096 | String cpuInfo = null; |
| 3097 | if (MONITOR_CPU_USAGE) { |
| 3098 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3099 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3100 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3101 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3102 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3103 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3104 | } |
| 3105 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3106 | info.append(processStats.printCurrentState(anrTime)); |
| 3107 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3108 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3109 | if (tracesFile == null) { |
| 3110 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 3111 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 3112 | } |
| 3113 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 3114 | addErrorToDropBox("anr", app, app.processName, activity, parent, annotation, |
| 3115 | cpuInfo, tracesFile, null); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3116 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3117 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3118 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3119 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 3120 | 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] | 3121 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3122 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3123 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3124 | } |
| 3125 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3126 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3127 | } |
| 3128 | } |
| 3129 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3130 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 3131 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 3132 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3133 | |
| 3134 | synchronized (this) { |
| 3135 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3136 | Slog.w(TAG, "Killing " + app + ": background ANR"); |
| 3137 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 3138 | app.processName, app.setAdj, "background ANR"); |
| 3139 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3140 | return; |
| 3141 | } |
| 3142 | |
| 3143 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 3144 | makeAppNotRespondingLocked(app, |
| 3145 | activity != null ? activity.shortComponentName : null, |
| 3146 | annotation != null ? "ANR " + annotation : "ANR", |
| 3147 | info.toString()); |
| 3148 | |
| 3149 | // Bring up the infamous App Not Responding dialog |
| 3150 | Message msg = Message.obtain(); |
| 3151 | HashMap map = new HashMap(); |
| 3152 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3153 | msg.obj = map; |
| 3154 | map.put("app", app); |
| 3155 | if (activity != null) { |
| 3156 | map.put("activity", activity); |
| 3157 | } |
| 3158 | |
| 3159 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3160 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3161 | } |
| 3162 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3163 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3164 | if (!mLaunchWarningShown) { |
| 3165 | mLaunchWarningShown = true; |
| 3166 | mHandler.post(new Runnable() { |
| 3167 | @Override |
| 3168 | public void run() { |
| 3169 | synchronized (ActivityManagerService.this) { |
| 3170 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3171 | d.show(); |
| 3172 | mHandler.postDelayed(new Runnable() { |
| 3173 | @Override |
| 3174 | public void run() { |
| 3175 | synchronized (ActivityManagerService.this) { |
| 3176 | d.dismiss(); |
| 3177 | mLaunchWarningShown = false; |
| 3178 | } |
| 3179 | } |
| 3180 | }, 4000); |
| 3181 | } |
| 3182 | } |
| 3183 | }); |
| 3184 | } |
| 3185 | } |
| 3186 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3187 | public boolean clearApplicationUserData(final String packageName, |
| 3188 | final IPackageDataObserver observer) { |
| 3189 | int uid = Binder.getCallingUid(); |
| 3190 | int pid = Binder.getCallingPid(); |
| 3191 | long callingId = Binder.clearCallingIdentity(); |
| 3192 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3193 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3194 | int pkgUid = -1; |
| 3195 | synchronized(this) { |
| 3196 | try { |
| 3197 | pkgUid = pm.getPackageUid(packageName); |
| 3198 | } catch (RemoteException e) { |
| 3199 | } |
| 3200 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3201 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3202 | return false; |
| 3203 | } |
| 3204 | if (uid == pkgUid || checkComponentPermission( |
| 3205 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3206 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3207 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3208 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3209 | } else { |
| 3210 | throw new SecurityException(pid+" does not have permission:"+ |
| 3211 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3212 | "for process:"+packageName); |
| 3213 | } |
| 3214 | } |
| 3215 | |
| 3216 | try { |
| 3217 | //clear application user data |
| 3218 | pm.clearApplicationUserData(packageName, observer); |
| 3219 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3220 | Uri.fromParts("package", packageName, null)); |
| 3221 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3222 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
| 3223 | null, null, 0, null, null, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3224 | } catch (RemoteException e) { |
| 3225 | } |
| 3226 | } finally { |
| 3227 | Binder.restoreCallingIdentity(callingId); |
| 3228 | } |
| 3229 | return true; |
| 3230 | } |
| 3231 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3232 | public void killBackgroundProcesses(final String packageName) { |
| 3233 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3234 | != PackageManager.PERMISSION_GRANTED && |
| 3235 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3236 | != PackageManager.PERMISSION_GRANTED) { |
| 3237 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3238 | + Binder.getCallingPid() |
| 3239 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3240 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3241 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3242 | throw new SecurityException(msg); |
| 3243 | } |
| 3244 | |
| 3245 | long callingId = Binder.clearCallingIdentity(); |
| 3246 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3247 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3248 | int pkgUid = -1; |
| 3249 | synchronized(this) { |
| 3250 | try { |
| 3251 | pkgUid = pm.getPackageUid(packageName); |
| 3252 | } catch (RemoteException e) { |
| 3253 | } |
| 3254 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3255 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3256 | return; |
| 3257 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3258 | killPackageProcessesLocked(packageName, pkgUid, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3259 | ProcessList.SERVICE_ADJ, false, true, true, false, "kill background"); |
| 3260 | } |
| 3261 | } finally { |
| 3262 | Binder.restoreCallingIdentity(callingId); |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | public void killAllBackgroundProcesses() { |
| 3267 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3268 | != PackageManager.PERMISSION_GRANTED) { |
| 3269 | String msg = "Permission Denial: killAllBackgroundProcesses() from pid=" |
| 3270 | + Binder.getCallingPid() |
| 3271 | + ", uid=" + Binder.getCallingUid() |
| 3272 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
| 3273 | Slog.w(TAG, msg); |
| 3274 | throw new SecurityException(msg); |
| 3275 | } |
| 3276 | |
| 3277 | long callingId = Binder.clearCallingIdentity(); |
| 3278 | try { |
| 3279 | synchronized(this) { |
| 3280 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 3281 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3282 | final int NA = apps.size(); |
| 3283 | for (int ia=0; ia<NA; ia++) { |
| 3284 | ProcessRecord app = apps.valueAt(ia); |
| 3285 | if (app.persistent) { |
| 3286 | // we don't kill persistent processes |
| 3287 | continue; |
| 3288 | } |
| 3289 | if (app.removed) { |
| 3290 | procs.add(app); |
| 3291 | } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 3292 | app.removed = true; |
| 3293 | procs.add(app); |
| 3294 | } |
| 3295 | } |
| 3296 | } |
| 3297 | |
| 3298 | int N = procs.size(); |
| 3299 | for (int i=0; i<N; i++) { |
| 3300 | removeProcessLocked(procs.get(i), false, true, "kill all background"); |
| 3301 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3302 | } |
| 3303 | } finally { |
| 3304 | Binder.restoreCallingIdentity(callingId); |
| 3305 | } |
| 3306 | } |
| 3307 | |
| 3308 | public void forceStopPackage(final String packageName) { |
| 3309 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3310 | != PackageManager.PERMISSION_GRANTED) { |
| 3311 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3312 | + Binder.getCallingPid() |
| 3313 | + ", uid=" + Binder.getCallingUid() |
| 3314 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3315 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3316 | throw new SecurityException(msg); |
| 3317 | } |
| 3318 | |
| 3319 | long callingId = Binder.clearCallingIdentity(); |
| 3320 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3321 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3322 | int pkgUid = -1; |
| 3323 | synchronized(this) { |
| 3324 | try { |
| 3325 | pkgUid = pm.getPackageUid(packageName); |
| 3326 | } catch (RemoteException e) { |
| 3327 | } |
| 3328 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3329 | Slog.w(TAG, "Invalid packageName: " + packageName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3330 | return; |
| 3331 | } |
| 3332 | forceStopPackageLocked(packageName, pkgUid); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3333 | try { |
| 3334 | pm.setPackageStoppedState(packageName, true); |
| 3335 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 3336 | } catch (IllegalArgumentException e) { |
| 3337 | Slog.w(TAG, "Failed trying to unstop package " |
| 3338 | + packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3339 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3340 | } |
| 3341 | } finally { |
| 3342 | Binder.restoreCallingIdentity(callingId); |
| 3343 | } |
| 3344 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3345 | |
| 3346 | /* |
| 3347 | * The pkg name and uid have to be specified. |
| 3348 | * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int) |
| 3349 | */ |
| 3350 | public void killApplicationWithUid(String pkg, int uid) { |
| 3351 | if (pkg == null) { |
| 3352 | return; |
| 3353 | } |
| 3354 | // Make sure the uid is valid. |
| 3355 | if (uid < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3356 | Slog.w(TAG, "Invalid uid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3357 | return; |
| 3358 | } |
| 3359 | int callerUid = Binder.getCallingUid(); |
| 3360 | // Only the system server can kill an application |
| 3361 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3362 | // Post an aysnc message to kill the application |
| 3363 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
| 3364 | msg.arg1 = uid; |
| 3365 | msg.arg2 = 0; |
| 3366 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3367 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3368 | } else { |
| 3369 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3370 | pkg); |
| 3371 | } |
| 3372 | } |
| 3373 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3374 | public void closeSystemDialogs(String reason) { |
| 3375 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3376 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3377 | if (reason != null) { |
| 3378 | intent.putExtra("reason", reason); |
| 3379 | } |
| 3380 | |
| 3381 | final int uid = Binder.getCallingUid(); |
| 3382 | final long origId = Binder.clearCallingIdentity(); |
| 3383 | synchronized (this) { |
| 3384 | int i = mWatchers.beginBroadcast(); |
| 3385 | while (i > 0) { |
| 3386 | i--; |
| 3387 | IActivityWatcher w = mWatchers.getBroadcastItem(i); |
| 3388 | if (w != null) { |
| 3389 | try { |
| 3390 | w.closingSystemDialogs(reason); |
| 3391 | } catch (RemoteException e) { |
| 3392 | } |
| 3393 | } |
| 3394 | } |
| 3395 | mWatchers.finishBroadcast(); |
| 3396 | |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3397 | mWindowManager.closeSystemDialogs(reason); |
| 3398 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3399 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 3400 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3401 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3402 | r.stack.finishActivityLocked(r, i, |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3403 | Activity.RESULT_CANCELED, null, "close-sys"); |
| 3404 | } |
| 3405 | } |
| 3406 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3407 | broadcastIntentLocked(null, null, intent, null, |
| 3408 | null, 0, null, null, null, false, false, -1, uid); |
| 3409 | } |
| 3410 | Binder.restoreCallingIdentity(origId); |
| 3411 | } |
| 3412 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3413 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3414 | throws RemoteException { |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3415 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3416 | for (int i=pids.length-1; i>=0; i--) { |
| 3417 | infos[i] = new Debug.MemoryInfo(); |
| 3418 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3419 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3420 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3421 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3422 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3423 | public long[] getProcessPss(int[] pids) throws RemoteException { |
| 3424 | long[] pss = new long[pids.length]; |
| 3425 | for (int i=pids.length-1; i>=0; i--) { |
| 3426 | pss[i] = Debug.getPss(pids[i]); |
| 3427 | } |
| 3428 | return pss; |
| 3429 | } |
| 3430 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3431 | public void killApplicationProcess(String processName, int uid) { |
| 3432 | if (processName == null) { |
| 3433 | return; |
| 3434 | } |
| 3435 | |
| 3436 | int callerUid = Binder.getCallingUid(); |
| 3437 | // Only the system server can kill an application |
| 3438 | if (callerUid == Process.SYSTEM_UID) { |
| 3439 | synchronized (this) { |
| 3440 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3441 | if (app != null && app.thread != null) { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3442 | try { |
| 3443 | app.thread.scheduleSuicide(); |
| 3444 | } catch (RemoteException e) { |
| 3445 | // If the other end already died, then our work here is done. |
| 3446 | } |
| 3447 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3448 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3449 | + processName + " / " + uid); |
| 3450 | } |
| 3451 | } |
| 3452 | } else { |
| 3453 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3454 | processName); |
| 3455 | } |
| 3456 | } |
| 3457 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3458 | private void forceStopPackageLocked(final String packageName, int uid) { |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3459 | forceStopPackageLocked(packageName, uid, false, false, true, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3460 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3461 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3462 | if (!mProcessesReady) { |
| 3463 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3464 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3465 | intent.putExtra(Intent.EXTRA_UID, uid); |
| 3466 | broadcastIntentLocked(null, null, intent, |
| 3467 | null, null, 0, null, null, null, |
| 3468 | false, false, MY_PID, Process.SYSTEM_UID); |
| 3469 | } |
| 3470 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3471 | private final boolean killPackageProcessesLocked(String packageName, int uid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3472 | int minOomAdj, boolean callerWillRestart, boolean allowRestart, boolean doit, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3473 | boolean evenPersistent, String reason) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3474 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3475 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3476 | // Remove all processes this package may have touched: all with the |
| 3477 | // same UID (except for the system or root user), and all whose name |
| 3478 | // matches the package name. |
| 3479 | final String procNamePrefix = packageName + ":"; |
| 3480 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3481 | final int NA = apps.size(); |
| 3482 | for (int ia=0; ia<NA; ia++) { |
| 3483 | ProcessRecord app = apps.valueAt(ia); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3484 | if (app.persistent && !evenPersistent) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3485 | // we don't kill persistent processes |
| 3486 | continue; |
| 3487 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3488 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3489 | if (doit) { |
| 3490 | procs.add(app); |
| 3491 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3492 | } else if ((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid) |
| 3493 | || app.processName.equals(packageName) |
| 3494 | || app.processName.startsWith(procNamePrefix)) { |
| 3495 | if (app.setAdj >= minOomAdj) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3496 | if (!doit) { |
| 3497 | return true; |
| 3498 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3499 | app.removed = true; |
| 3500 | procs.add(app); |
| 3501 | } |
| 3502 | } |
| 3503 | } |
| 3504 | } |
| 3505 | |
| 3506 | int N = procs.size(); |
| 3507 | for (int i=0; i<N; i++) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3508 | removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3509 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3510 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3511 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3512 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3513 | private final boolean forceStopPackageLocked(String name, int uid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3514 | boolean callerWillRestart, boolean purgeCache, boolean doit, |
| 3515 | boolean evenPersistent) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3516 | int i; |
| 3517 | int N; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3518 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3519 | if (uid < 0) { |
| 3520 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3521 | uid = AppGlobals.getPackageManager().getPackageUid(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3522 | } catch (RemoteException e) { |
| 3523 | } |
| 3524 | } |
| 3525 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3526 | if (doit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3527 | Slog.i(TAG, "Force stopping package " + name + " uid=" + uid); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3528 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3529 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3530 | while (badApps.hasNext()) { |
| 3531 | SparseArray<Long> ba = badApps.next(); |
| 3532 | if (ba.get(uid) != null) { |
| 3533 | badApps.remove(); |
| 3534 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3535 | } |
| 3536 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3537 | |
| 3538 | boolean didSomething = killPackageProcessesLocked(name, uid, -100, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3539 | callerWillRestart, false, doit, evenPersistent, "force stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3540 | |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3541 | TaskRecord lastTask = null; |
| 3542 | for (i=0; i<mMainStack.mHistory.size(); i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3543 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3544 | final boolean samePackage = r.packageName.equals(name); |
| 3545 | if ((samePackage || r.task == lastTask) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3546 | && (r.app == null || evenPersistent || !r.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3547 | if (!doit) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 3548 | if (r.finishing) { |
| 3549 | // If this activity is just finishing, then it is not |
| 3550 | // interesting as far as something to stop. |
| 3551 | continue; |
| 3552 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3553 | return true; |
| 3554 | } |
| 3555 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3556 | Slog.i(TAG, " Force finishing activity " + r); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3557 | if (samePackage) { |
| 3558 | if (r.app != null) { |
| 3559 | r.app.removed = true; |
| 3560 | } |
| 3561 | r.app = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3562 | } |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3563 | lastTask = r.task; |
| 3564 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
| 3565 | null, "force-stop")) { |
| 3566 | i--; |
| 3567 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3568 | } |
| 3569 | } |
| 3570 | |
| 3571 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
| 3572 | for (ServiceRecord service : mServices.values()) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3573 | if (service.packageName.equals(name) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3574 | && (service.app == null || evenPersistent || !service.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3575 | if (!doit) { |
| 3576 | return true; |
| 3577 | } |
| 3578 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3579 | Slog.i(TAG, " Force stopping service " + service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3580 | if (service.app != null) { |
| 3581 | service.app.removed = true; |
| 3582 | } |
| 3583 | service.app = null; |
| 3584 | services.add(service); |
| 3585 | } |
| 3586 | } |
| 3587 | |
| 3588 | N = services.size(); |
| 3589 | for (i=0; i<N; i++) { |
| 3590 | bringDownServiceLocked(services.get(i), true); |
| 3591 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3592 | |
| 3593 | ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); |
| 3594 | for (ContentProviderRecord provider : mProvidersByClass.values()) { |
| 3595 | if (provider.info.packageName.equals(name) |
| 3596 | && (provider.proc == null || evenPersistent || !provider.proc.persistent)) { |
| 3597 | if (!doit) { |
| 3598 | return true; |
| 3599 | } |
| 3600 | didSomething = true; |
| 3601 | providers.add(provider); |
| 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | N = providers.size(); |
| 3606 | for (i=0; i<N; i++) { |
| 3607 | removeDyingProviderLocked(null, providers.get(i)); |
| 3608 | } |
| 3609 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3610 | if (doit) { |
| 3611 | if (purgeCache) { |
| 3612 | AttributeCache ac = AttributeCache.instance(); |
| 3613 | if (ac != null) { |
| 3614 | ac.removePackage(name); |
| 3615 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3616 | } |
Dianne Hackborn | 38cc896 | 2011-10-13 11:33:55 -0700 | [diff] [blame] | 3617 | if (mBooted) { |
| 3618 | mMainStack.resumeTopActivityLocked(null); |
| 3619 | mMainStack.scheduleIdleLocked(); |
| 3620 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3621 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3622 | |
| 3623 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3624 | } |
| 3625 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3626 | private final boolean removeProcessLocked(ProcessRecord app, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3627 | boolean callerWillRestart, boolean allowRestart, String reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3628 | final String name = app.processName; |
| 3629 | final int uid = app.info.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3630 | if (DEBUG_PROCESSES) Slog.d( |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3631 | TAG, "Force removing proc " + app.toShortString() + " (" + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3632 | + "/" + uid + ")"); |
| 3633 | |
| 3634 | mProcessNames.remove(name, uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3635 | if (mHeavyWeightProcess == app) { |
| 3636 | mHeavyWeightProcess = null; |
| 3637 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3638 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3639 | boolean needRestart = false; |
| 3640 | if (app.pid > 0 && app.pid != MY_PID) { |
| 3641 | int pid = app.pid; |
| 3642 | synchronized (mPidsSelfLocked) { |
| 3643 | mPidsSelfLocked.remove(pid); |
| 3644 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3645 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3646 | Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3647 | handleAppDiedLocked(app, true, allowRestart); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3648 | mLruProcesses.remove(app); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3649 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3650 | |
| 3651 | if (app.persistent) { |
| 3652 | if (!callerWillRestart) { |
| 3653 | addAppLocked(app.info); |
| 3654 | } else { |
| 3655 | needRestart = true; |
| 3656 | } |
| 3657 | } |
| 3658 | } else { |
| 3659 | mRemovedProcesses.add(app); |
| 3660 | } |
| 3661 | |
| 3662 | return needRestart; |
| 3663 | } |
| 3664 | |
| 3665 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 3666 | final int pid = app.pid; |
| 3667 | boolean gone = false; |
| 3668 | synchronized (mPidsSelfLocked) { |
| 3669 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 3670 | if (knownApp != null && knownApp.thread == null) { |
| 3671 | mPidsSelfLocked.remove(pid); |
| 3672 | gone = true; |
| 3673 | } |
| 3674 | } |
| 3675 | |
| 3676 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3677 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3678 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.info.uid, |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3679 | app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3680 | mProcessNames.remove(app.processName, app.info.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3681 | if (mHeavyWeightProcess == app) { |
| 3682 | mHeavyWeightProcess = null; |
| 3683 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3684 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3685 | // Take care of any launching providers waiting for this process. |
| 3686 | checkAppInLaunchingProvidersLocked(app, true); |
| 3687 | // Take care of any services that are waiting for the process. |
| 3688 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3689 | ServiceRecord sr = mPendingServices.get(i); |
| 3690 | if (app.info.uid == sr.appInfo.uid |
| 3691 | && app.processName.equals(sr.processName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3692 | Slog.w(TAG, "Forcing bringing down service: " + sr); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3693 | mPendingServices.remove(i); |
| 3694 | i--; |
| 3695 | bringDownServiceLocked(sr, true); |
| 3696 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3697 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3698 | EventLog.writeEvent(EventLogTags.AM_KILL, pid, |
| 3699 | app.processName, app.setAdj, "start timeout"); |
| 3700 | Process.killProcessQuiet(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3701 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3702 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3703 | try { |
| 3704 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 3705 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 3706 | bm.agentDisconnected(app.info.packageName); |
| 3707 | } catch (RemoteException e) { |
| 3708 | // Can't happen; the backup manager is local |
| 3709 | } |
| 3710 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3711 | if (mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3712 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3713 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 3714 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3715 | mPendingBroadcast = null; |
| 3716 | scheduleBroadcastsLocked(); |
| 3717 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3718 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3719 | 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] | 3720 | } |
| 3721 | } |
| 3722 | |
| 3723 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 3724 | int pid) { |
| 3725 | |
| 3726 | // Find the application record that is being attached... either via |
| 3727 | // the pid if we are running in multiple processes, or just pull the |
| 3728 | // next app record if we are emulating process with anonymous threads. |
| 3729 | ProcessRecord app; |
| 3730 | if (pid != MY_PID && pid >= 0) { |
| 3731 | synchronized (mPidsSelfLocked) { |
| 3732 | app = mPidsSelfLocked.get(pid); |
| 3733 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3734 | } else { |
| 3735 | app = null; |
| 3736 | } |
| 3737 | |
| 3738 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3739 | 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] | 3740 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3741 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3742 | if (pid > 0 && pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3743 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3744 | } else { |
| 3745 | try { |
| 3746 | thread.scheduleExit(); |
| 3747 | } catch (Exception e) { |
| 3748 | // Ignore exceptions. |
| 3749 | } |
| 3750 | } |
| 3751 | return false; |
| 3752 | } |
| 3753 | |
| 3754 | // If this application record is still attached to a previous |
| 3755 | // process, clean it up now. |
| 3756 | if (app.thread != null) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3757 | handleAppDiedLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | // Tell the process all about itself. |
| 3761 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3762 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3763 | TAG, "Binding process pid " + pid + " to record " + app); |
| 3764 | |
| 3765 | String processName = app.processName; |
| 3766 | try { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 3767 | AppDeathRecipient adr = new AppDeathRecipient( |
| 3768 | app, pid, thread); |
| 3769 | thread.asBinder().linkToDeath(adr, 0); |
| 3770 | app.deathRecipient = adr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3771 | } catch (RemoteException e) { |
| 3772 | app.resetPackageList(); |
| 3773 | startProcessLocked(app, "link fail", processName); |
| 3774 | return false; |
| 3775 | } |
| 3776 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3777 | 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] | 3778 | |
| 3779 | app.thread = thread; |
| 3780 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 3781 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 3782 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3783 | app.forcingToForeground = null; |
| 3784 | app.foregroundServices = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 3785 | app.hasShownUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3786 | app.debugging = false; |
| 3787 | |
| 3788 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3789 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3790 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3791 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3792 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3793 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3794 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3795 | } |
| 3796 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3797 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3798 | TAG, "New app record " + app |
| 3799 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 3800 | try { |
| 3801 | int testMode = IApplicationThread.DEBUG_OFF; |
| 3802 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 3803 | testMode = mWaitForDebugger |
| 3804 | ? IApplicationThread.DEBUG_WAIT |
| 3805 | : IApplicationThread.DEBUG_ON; |
| 3806 | app.debugging = true; |
| 3807 | if (mDebugTransient) { |
| 3808 | mDebugApp = mOrigDebugApp; |
| 3809 | mWaitForDebugger = mOrigWaitForDebugger; |
| 3810 | } |
| 3811 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3812 | String profileFile = app.instrumentationProfileFile; |
| 3813 | ParcelFileDescriptor profileFd = null; |
| 3814 | boolean profileAutoStop = false; |
| 3815 | if (mProfileApp != null && mProfileApp.equals(processName)) { |
| 3816 | mProfileProc = app; |
| 3817 | profileFile = mProfileFile; |
| 3818 | profileFd = mProfileFd; |
| 3819 | profileAutoStop = mAutoStopProfiler; |
| 3820 | } |
| 3821 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3822 | // If the app is being launched for restore or full backup, set it up specially |
| 3823 | boolean isRestrictedBackupMode = false; |
| 3824 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 3825 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3826 | || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3827 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 3828 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3829 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 3830 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 3831 | ? app.instrumentationInfo.packageName |
| 3832 | : app.info.packageName); |
| 3833 | if (app.instrumentationClass != null) { |
| 3834 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3835 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3836 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 3837 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3838 | ApplicationInfo appInfo = app.instrumentationInfo != null |
| 3839 | ? app.instrumentationInfo : app.info; |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 3840 | app.compat = compatibilityInfoForPackageLocked(appInfo); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3841 | if (profileFd != null) { |
| 3842 | profileFd = profileFd.dup(); |
| 3843 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3844 | thread.bindApplication(processName, appInfo, providers, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3845 | app.instrumentationClass, profileFile, profileFd, profileAutoStop, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3846 | app.instrumentationArguments, app.instrumentationWatcher, testMode, |
Dianne Hackborn | 5d927c2 | 2011-09-02 12:22:18 -0700 | [diff] [blame] | 3847 | isRestrictedBackupMode || !normalMode, app.persistent, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 3848 | new Configuration(mConfiguration), app.compat, getCommonServicesLocked(), |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 3849 | mCoreSettingsObserver.getCoreSettingsLocked()); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3850 | updateLruProcessLocked(app, false, true); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3851 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3852 | } catch (Exception e) { |
| 3853 | // todo: Yikes! What should we do? For now we will try to |
| 3854 | // start another process, but that could easily get us in |
| 3855 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3856 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3857 | |
| 3858 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 3859 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3860 | startProcessLocked(app, "bind fail", processName); |
| 3861 | return false; |
| 3862 | } |
| 3863 | |
| 3864 | // Remove this record from the list of starting applications. |
| 3865 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3866 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 3867 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3868 | mProcessesOnHold.remove(app); |
| 3869 | |
| 3870 | boolean badApp = false; |
| 3871 | boolean didSomething = false; |
| 3872 | |
| 3873 | // 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] | 3874 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 3875 | if (hr != null && normalMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3876 | if (hr.app == null && app.info.uid == hr.info.applicationInfo.uid |
| 3877 | && processName.equals(hr.processName)) { |
| 3878 | try { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3879 | if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3880 | didSomething = true; |
| 3881 | } |
| 3882 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3883 | 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] | 3884 | + hr.intent.getComponent().flattenToShortString(), e); |
| 3885 | badApp = true; |
| 3886 | } |
| 3887 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3888 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3889 | } |
| 3890 | } |
| 3891 | |
| 3892 | // Find any services that should be running in this process... |
| 3893 | if (!badApp && mPendingServices.size() > 0) { |
| 3894 | ServiceRecord sr = null; |
| 3895 | try { |
| 3896 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3897 | sr = mPendingServices.get(i); |
| 3898 | if (app.info.uid != sr.appInfo.uid |
| 3899 | || !processName.equals(sr.processName)) { |
| 3900 | continue; |
| 3901 | } |
| 3902 | |
| 3903 | mPendingServices.remove(i); |
| 3904 | i--; |
| 3905 | realStartServiceLocked(sr, app); |
| 3906 | didSomething = true; |
| 3907 | } |
| 3908 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3909 | 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] | 3910 | + sr.shortName, e); |
| 3911 | badApp = true; |
| 3912 | } |
| 3913 | } |
| 3914 | |
| 3915 | // Check if the next broadcast receiver is in this process... |
| 3916 | BroadcastRecord br = mPendingBroadcast; |
| 3917 | if (!badApp && br != null && br.curApp == app) { |
| 3918 | try { |
| 3919 | mPendingBroadcast = null; |
| 3920 | processCurBroadcastLocked(br, app); |
| 3921 | didSomething = true; |
| 3922 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3923 | 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] | 3924 | + br.curComponent.flattenToShortString(), e); |
| 3925 | badApp = true; |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 3926 | logBroadcastReceiverDiscardLocked(br); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3927 | finishReceiverLocked(br.receiver, br.resultCode, br.resultData, |
| 3928 | br.resultExtras, br.resultAbort, true); |
| 3929 | scheduleBroadcastsLocked(); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3930 | // We need to reset the state if we fails to start the receiver. |
| 3931 | br.state = BroadcastRecord.IDLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3932 | } |
| 3933 | } |
| 3934 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3935 | // Check whether the next backup agent is in this process... |
| 3936 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.info.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3937 | 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] | 3938 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3939 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3940 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, |
| 3941 | compatibilityInfoForPackageLocked(mBackupTarget.appInfo), |
| 3942 | mBackupTarget.backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3943 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3944 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3945 | e.printStackTrace(); |
| 3946 | } |
| 3947 | } |
| 3948 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3949 | if (badApp) { |
| 3950 | // todo: Also need to kill application to deal with all |
| 3951 | // kinds of exceptions. |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3952 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3953 | return false; |
| 3954 | } |
| 3955 | |
| 3956 | if (!didSomething) { |
| 3957 | updateOomAdjLocked(); |
| 3958 | } |
| 3959 | |
| 3960 | return true; |
| 3961 | } |
| 3962 | |
| 3963 | public final void attachApplication(IApplicationThread thread) { |
| 3964 | synchronized (this) { |
| 3965 | int callingPid = Binder.getCallingPid(); |
| 3966 | final long origId = Binder.clearCallingIdentity(); |
| 3967 | attachApplicationLocked(thread, callingPid); |
| 3968 | Binder.restoreCallingIdentity(origId); |
| 3969 | } |
| 3970 | } |
| 3971 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3972 | public final void activityIdle(IBinder token, Configuration config, boolean stopProfiling) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3973 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3974 | ActivityRecord r = mMainStack.activityIdleInternal(token, false, config); |
| 3975 | if (stopProfiling) { |
| 3976 | synchronized (this) { |
| 3977 | if (mProfileProc == r.app) { |
| 3978 | if (mProfileFd != null) { |
| 3979 | try { |
| 3980 | mProfileFd.close(); |
| 3981 | } catch (IOException e) { |
| 3982 | } |
| 3983 | clearProfilerLocked(); |
| 3984 | } |
| 3985 | } |
| 3986 | } |
| 3987 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3988 | Binder.restoreCallingIdentity(origId); |
| 3989 | } |
| 3990 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3991 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3992 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3993 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3994 | mWindowManager.enableScreenAfterBoot(); |
| 3995 | } |
| 3996 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 3997 | public void showBootMessage(final CharSequence msg, final boolean always) { |
| 3998 | mWindowManager.showBootMessage(msg, always); |
| 3999 | } |
| 4000 | |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4001 | public void dismissKeyguardOnNextActivity() { |
| 4002 | synchronized (this) { |
| 4003 | mMainStack.dismissKeyguardOnNextActivityLocked(); |
| 4004 | } |
| 4005 | } |
| 4006 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4007 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4008 | IntentFilter pkgFilter = new IntentFilter(); |
| 4009 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 4010 | pkgFilter.addDataScheme("package"); |
| 4011 | mContext.registerReceiver(new BroadcastReceiver() { |
| 4012 | @Override |
| 4013 | public void onReceive(Context context, Intent intent) { |
| 4014 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 4015 | if (pkgs != null) { |
| 4016 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 4017 | synchronized (ActivityManagerService.this) { |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 4018 | if (forceStopPackageLocked(pkg, -1, false, false, false, false)) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 4019 | setResultCode(Activity.RESULT_OK); |
| 4020 | return; |
| 4021 | } |
| 4022 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4023 | } |
| 4024 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4025 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4026 | }, pkgFilter); |
| 4027 | |
| 4028 | synchronized (this) { |
| 4029 | // Ensure that any processes we had put on hold are now started |
| 4030 | // up. |
| 4031 | final int NP = mProcessesOnHold.size(); |
| 4032 | if (NP > 0) { |
| 4033 | ArrayList<ProcessRecord> procs = |
| 4034 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 4035 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4036 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 4037 | + procs.get(ip)); |
| 4038 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4039 | } |
| 4040 | } |
| 4041 | |
| 4042 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4043 | // Start looking for apps that are abusing wake locks. |
| 4044 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 4045 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4046 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 4047 | SystemProperties.set("sys.boot_completed", "1"); |
Guang Zhu | 191713a | 2012-01-12 12:02:22 -0800 | [diff] [blame^] | 4048 | SystemProperties.set("dev.bootcomplete", "1"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4049 | broadcastIntentLocked(null, null, |
| 4050 | new Intent(Intent.ACTION_BOOT_COMPLETED, null), |
| 4051 | null, null, 0, null, null, |
| 4052 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
| 4053 | false, false, MY_PID, Process.SYSTEM_UID); |
| 4054 | } |
| 4055 | } |
| 4056 | } |
| 4057 | |
| 4058 | final void ensureBootCompleted() { |
| 4059 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4060 | boolean enableScreen; |
| 4061 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4062 | booting = mBooting; |
| 4063 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4064 | enableScreen = !mBooted; |
| 4065 | mBooted = true; |
| 4066 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4067 | |
| 4068 | if (booting) { |
| 4069 | finishBooting(); |
| 4070 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4071 | |
| 4072 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4073 | enableScreenAfterBoot(); |
| 4074 | } |
| 4075 | } |
| 4076 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4077 | public final void activityPaused(IBinder token) { |
| 4078 | final long origId = Binder.clearCallingIdentity(); |
| 4079 | mMainStack.activityPaused(token, false); |
| 4080 | Binder.restoreCallingIdentity(origId); |
| 4081 | } |
| 4082 | |
| 4083 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 4084 | CharSequence description) { |
| 4085 | if (localLOGV) Slog.v( |
| 4086 | TAG, "Activity stopped: token=" + token); |
| 4087 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4088 | // Refuse possible leaked file descriptors |
| 4089 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 4090 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 4091 | } |
| 4092 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4093 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4094 | |
| 4095 | final long origId = Binder.clearCallingIdentity(); |
| 4096 | |
| 4097 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4098 | r = mMainStack.isInStackLocked(token); |
| 4099 | if (r != null) { |
| 4100 | r.stack.activityStoppedLocked(r, icicle, thumbnail, description); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4101 | } |
| 4102 | } |
| 4103 | |
| 4104 | if (r != null) { |
| 4105 | sendPendingThumbnail(r, null, null, null, false); |
| 4106 | } |
| 4107 | |
| 4108 | trimApplications(); |
| 4109 | |
| 4110 | Binder.restoreCallingIdentity(origId); |
| 4111 | } |
| 4112 | |
| 4113 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4114 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4115 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | public String getCallingPackage(IBinder token) { |
| 4119 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4120 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 4121 | 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] | 4122 | } |
| 4123 | } |
| 4124 | |
| 4125 | public ComponentName getCallingActivity(IBinder token) { |
| 4126 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4127 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4128 | return r != null ? r.intent.getComponent() : null; |
| 4129 | } |
| 4130 | } |
| 4131 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4132 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4133 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4134 | if (r == null) { |
| 4135 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4136 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4137 | return r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4138 | } |
| 4139 | |
| 4140 | public ComponentName getActivityClassForToken(IBinder token) { |
| 4141 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4142 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4143 | if (r == null) { |
| 4144 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4145 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4146 | return r.intent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4147 | } |
| 4148 | } |
| 4149 | |
| 4150 | public String getPackageForToken(IBinder token) { |
| 4151 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4152 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4153 | if (r == null) { |
| 4154 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4155 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4156 | return r.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | public IIntentSender getIntentSender(int type, |
| 4161 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4162 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4163 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4164 | if (intents != null) { |
| 4165 | if (intents.length < 1) { |
| 4166 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 4167 | } |
| 4168 | for (int i=0; i<intents.length; i++) { |
| 4169 | Intent intent = intents[i]; |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4170 | if (intent != null) { |
| 4171 | if (intent.hasFileDescriptors()) { |
| 4172 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 4173 | } |
| 4174 | if (type == INTENT_SENDER_BROADCAST && |
| 4175 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 4176 | throw new IllegalArgumentException( |
| 4177 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 4178 | } |
| 4179 | intents[i] = new Intent(intent); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4180 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4181 | } |
| 4182 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4183 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4184 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4185 | } |
| 4186 | } |
| 4187 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4188 | synchronized(this) { |
| 4189 | int callingUid = Binder.getCallingUid(); |
| 4190 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4191 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4192 | int uid = AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4193 | .getPackageUid(packageName); |
| 4194 | if (uid != Binder.getCallingUid()) { |
| 4195 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 4196 | + Binder.getCallingPid() |
| 4197 | + ", uid=" + Binder.getCallingUid() |
| 4198 | + ", (need uid=" + uid + ")" |
| 4199 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4200 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4201 | throw new SecurityException(msg); |
| 4202 | } |
| 4203 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4204 | |
| 4205 | return getIntentSenderLocked(type, packageName, callingUid, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4206 | token, resultWho, requestCode, intents, resolvedTypes, flags); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4207 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4208 | } catch (RemoteException e) { |
| 4209 | throw new SecurityException(e); |
| 4210 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4211 | } |
| 4212 | } |
| 4213 | |
| 4214 | IIntentSender getIntentSenderLocked(int type, |
| 4215 | String packageName, int callingUid, IBinder token, String resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4216 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4217 | ActivityRecord activity = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4218 | if (type == INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4219 | activity = mMainStack.isInStackLocked(token); |
| 4220 | if (activity == null) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4221 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4222 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4223 | if (activity.finishing) { |
| 4224 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4225 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4226 | } |
| 4227 | |
| 4228 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 4229 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 4230 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 4231 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 4232 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 4233 | |
| 4234 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 4235 | type, packageName, activity, resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4236 | requestCode, intents, resolvedTypes, flags); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4237 | WeakReference<PendingIntentRecord> ref; |
| 4238 | ref = mIntentSenderRecords.get(key); |
| 4239 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 4240 | if (rec != null) { |
| 4241 | if (!cancelCurrent) { |
| 4242 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4243 | if (rec.key.requestIntent != null) { |
| 4244 | rec.key.requestIntent.replaceExtras(intents != null ? intents[0] : null); |
| 4245 | } |
| 4246 | if (intents != null) { |
| 4247 | intents[intents.length-1] = rec.key.requestIntent; |
| 4248 | rec.key.allIntents = intents; |
| 4249 | rec.key.allResolvedTypes = resolvedTypes; |
| 4250 | } else { |
| 4251 | rec.key.allIntents = null; |
| 4252 | rec.key.allResolvedTypes = null; |
| 4253 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4254 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4255 | return rec; |
| 4256 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4257 | rec.canceled = true; |
| 4258 | mIntentSenderRecords.remove(key); |
| 4259 | } |
| 4260 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4261 | return rec; |
| 4262 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4263 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4264 | mIntentSenderRecords.put(key, rec.ref); |
| 4265 | if (type == INTENT_SENDER_ACTIVITY_RESULT) { |
| 4266 | if (activity.pendingResults == null) { |
| 4267 | activity.pendingResults |
| 4268 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4269 | } |
| 4270 | activity.pendingResults.add(rec.ref); |
| 4271 | } |
| 4272 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4273 | } |
| 4274 | |
| 4275 | public void cancelIntentSender(IIntentSender sender) { |
| 4276 | if (!(sender instanceof PendingIntentRecord)) { |
| 4277 | return; |
| 4278 | } |
| 4279 | synchronized(this) { |
| 4280 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4281 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4282 | int uid = AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4283 | .getPackageUid(rec.key.packageName); |
| 4284 | if (uid != Binder.getCallingUid()) { |
| 4285 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4286 | + Binder.getCallingPid() |
| 4287 | + ", uid=" + Binder.getCallingUid() |
| 4288 | + " is not allowed to cancel packges " |
| 4289 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4290 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4291 | throw new SecurityException(msg); |
| 4292 | } |
| 4293 | } catch (RemoteException e) { |
| 4294 | throw new SecurityException(e); |
| 4295 | } |
| 4296 | cancelIntentSenderLocked(rec, true); |
| 4297 | } |
| 4298 | } |
| 4299 | |
| 4300 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4301 | rec.canceled = true; |
| 4302 | mIntentSenderRecords.remove(rec.key); |
| 4303 | if (cleanActivity && rec.key.activity != null) { |
| 4304 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4305 | } |
| 4306 | } |
| 4307 | |
| 4308 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4309 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4310 | return null; |
| 4311 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4312 | try { |
| 4313 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4314 | return res.key.packageName; |
| 4315 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4316 | } |
| 4317 | return null; |
| 4318 | } |
| 4319 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 4320 | public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) { |
| 4321 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4322 | return false; |
| 4323 | } |
| 4324 | try { |
| 4325 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4326 | if (res.key.allIntents == null) { |
| 4327 | return false; |
| 4328 | } |
| 4329 | for (int i=0; i<res.key.allIntents.length; i++) { |
| 4330 | Intent intent = res.key.allIntents[i]; |
| 4331 | if (intent.getPackage() != null && intent.getComponent() != null) { |
| 4332 | return false; |
| 4333 | } |
| 4334 | } |
| 4335 | return true; |
| 4336 | } catch (ClassCastException e) { |
| 4337 | } |
| 4338 | return false; |
| 4339 | } |
| 4340 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4341 | public void setProcessLimit(int max) { |
| 4342 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4343 | "setProcessLimit()"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4344 | synchronized (this) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4345 | mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4346 | mProcessLimitOverride = max; |
| 4347 | } |
| 4348 | trimApplications(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4349 | } |
| 4350 | |
| 4351 | public int getProcessLimit() { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4352 | synchronized (this) { |
| 4353 | return mProcessLimitOverride; |
| 4354 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4355 | } |
| 4356 | |
| 4357 | void foregroundTokenDied(ForegroundToken token) { |
| 4358 | synchronized (ActivityManagerService.this) { |
| 4359 | synchronized (mPidsSelfLocked) { |
| 4360 | ForegroundToken cur |
| 4361 | = mForegroundProcesses.get(token.pid); |
| 4362 | if (cur != token) { |
| 4363 | return; |
| 4364 | } |
| 4365 | mForegroundProcesses.remove(token.pid); |
| 4366 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4367 | if (pr == null) { |
| 4368 | return; |
| 4369 | } |
| 4370 | pr.forcingToForeground = null; |
| 4371 | pr.foregroundServices = false; |
| 4372 | } |
| 4373 | updateOomAdjLocked(); |
| 4374 | } |
| 4375 | } |
| 4376 | |
| 4377 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4378 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4379 | "setProcessForeground()"); |
| 4380 | synchronized(this) { |
| 4381 | boolean changed = false; |
| 4382 | |
| 4383 | synchronized (mPidsSelfLocked) { |
| 4384 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
| 4385 | if (pr == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4386 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4387 | return; |
| 4388 | } |
| 4389 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4390 | if (oldToken != null) { |
| 4391 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4392 | mForegroundProcesses.remove(pid); |
| 4393 | pr.forcingToForeground = null; |
| 4394 | changed = true; |
| 4395 | } |
| 4396 | if (isForeground && token != null) { |
| 4397 | ForegroundToken newToken = new ForegroundToken() { |
| 4398 | public void binderDied() { |
| 4399 | foregroundTokenDied(this); |
| 4400 | } |
| 4401 | }; |
| 4402 | newToken.pid = pid; |
| 4403 | newToken.token = token; |
| 4404 | try { |
| 4405 | token.linkToDeath(newToken, 0); |
| 4406 | mForegroundProcesses.put(pid, newToken); |
| 4407 | pr.forcingToForeground = token; |
| 4408 | changed = true; |
| 4409 | } catch (RemoteException e) { |
| 4410 | // If the process died while doing this, we will later |
| 4411 | // do the cleanup with the process death link. |
| 4412 | } |
| 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | if (changed) { |
| 4417 | updateOomAdjLocked(); |
| 4418 | } |
| 4419 | } |
| 4420 | } |
| 4421 | |
| 4422 | // ========================================================= |
| 4423 | // PERMISSIONS |
| 4424 | // ========================================================= |
| 4425 | |
| 4426 | static class PermissionController extends IPermissionController.Stub { |
| 4427 | ActivityManagerService mActivityManagerService; |
| 4428 | PermissionController(ActivityManagerService activityManagerService) { |
| 4429 | mActivityManagerService = activityManagerService; |
| 4430 | } |
| 4431 | |
| 4432 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4433 | return mActivityManagerService.checkPermission(permission, pid, |
| 4434 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4435 | } |
| 4436 | } |
| 4437 | |
| 4438 | /** |
| 4439 | * This can be called with or without the global lock held. |
| 4440 | */ |
| 4441 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4442 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4443 | // We might be performing an operation on behalf of an indirect binder |
| 4444 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4445 | // client identity accordingly before proceeding. |
| 4446 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4447 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4448 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4449 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4450 | uid = tlsIdentity.uid; |
| 4451 | pid = tlsIdentity.pid; |
| 4452 | } |
| 4453 | |
| 4454 | // Root, system server and our own process get to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4455 | if (uid == 0 || uid == Process.SYSTEM_UID || pid == MY_PID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4456 | return PackageManager.PERMISSION_GRANTED; |
| 4457 | } |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4458 | // If there is a uid that owns whatever is being accessed, it has |
| 4459 | // blanket access to it regardless of the permissions it requires. |
| 4460 | if (owningUid >= 0 && uid == owningUid) { |
| 4461 | return PackageManager.PERMISSION_GRANTED; |
| 4462 | } |
| 4463 | // If the target is not exported, then nobody else can get to it. |
| 4464 | if (!exported) { |
| 4465 | Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4466 | return PackageManager.PERMISSION_DENIED; |
| 4467 | } |
| 4468 | if (permission == null) { |
| 4469 | return PackageManager.PERMISSION_GRANTED; |
| 4470 | } |
| 4471 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4472 | return AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4473 | .checkUidPermission(permission, uid); |
| 4474 | } catch (RemoteException e) { |
| 4475 | // Should never happen, but if it does... deny! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4476 | Slog.e(TAG, "PackageManager is dead?!?", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4477 | } |
| 4478 | return PackageManager.PERMISSION_DENIED; |
| 4479 | } |
| 4480 | |
| 4481 | /** |
| 4482 | * As the only public entry point for permissions checking, this method |
| 4483 | * can enforce the semantic that requesting a check on a null global |
| 4484 | * permission is automatically denied. (Internally a null permission |
| 4485 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4486 | * when only uid-based security is needed.) |
| 4487 | * |
| 4488 | * This can be called with or without the global lock held. |
| 4489 | */ |
| 4490 | public int checkPermission(String permission, int pid, int uid) { |
| 4491 | if (permission == null) { |
| 4492 | return PackageManager.PERMISSION_DENIED; |
| 4493 | } |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4494 | return checkComponentPermission(permission, pid, uid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | /** |
| 4498 | * Binder IPC calls go through the public entry point. |
| 4499 | * This can be called with or without the global lock held. |
| 4500 | */ |
| 4501 | int checkCallingPermission(String permission) { |
| 4502 | return checkPermission(permission, |
| 4503 | Binder.getCallingPid(), |
| 4504 | Binder.getCallingUid()); |
| 4505 | } |
| 4506 | |
| 4507 | /** |
| 4508 | * This can be called with or without the global lock held. |
| 4509 | */ |
| 4510 | void enforceCallingPermission(String permission, String func) { |
| 4511 | if (checkCallingPermission(permission) |
| 4512 | == PackageManager.PERMISSION_GRANTED) { |
| 4513 | return; |
| 4514 | } |
| 4515 | |
| 4516 | String msg = "Permission Denial: " + func + " from pid=" |
| 4517 | + Binder.getCallingPid() |
| 4518 | + ", uid=" + Binder.getCallingUid() |
| 4519 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4520 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4521 | throw new SecurityException(msg); |
| 4522 | } |
| 4523 | |
| 4524 | private final boolean checkHoldingPermissionsLocked(IPackageManager pm, |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4525 | ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
| 4526 | boolean readPerm = (modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 4527 | boolean writePerm = (modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0; |
| 4528 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4529 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4530 | try { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4531 | // Is the component private from the target uid? |
| 4532 | final boolean prv = !pi.exported && pi.applicationInfo.uid != uid; |
| 4533 | |
| 4534 | // Acceptable if the there is no read permission needed from the |
| 4535 | // target or the target is holding the read permission. |
| 4536 | if (!readPerm) { |
| 4537 | if ((!prv && pi.readPermission == null) || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4538 | (pm.checkUidPermission(pi.readPermission, uid) |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4539 | == PackageManager.PERMISSION_GRANTED)) { |
| 4540 | readPerm = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4541 | } |
| 4542 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4543 | |
| 4544 | // Acceptable if the there is no write permission needed from the |
| 4545 | // target or the target is holding the read permission. |
| 4546 | if (!writePerm) { |
| 4547 | if (!prv && (pi.writePermission == null) || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4548 | (pm.checkUidPermission(pi.writePermission, uid) |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4549 | == PackageManager.PERMISSION_GRANTED)) { |
| 4550 | writePerm = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4551 | } |
| 4552 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4553 | |
| 4554 | // Acceptable if there is a path permission matching the URI that |
| 4555 | // the target holds the permission on. |
| 4556 | PathPermission[] pps = pi.pathPermissions; |
| 4557 | if (pps != null && (!readPerm || !writePerm)) { |
| 4558 | final String path = uri.getPath(); |
| 4559 | int i = pps.length; |
| 4560 | while (i > 0 && (!readPerm || !writePerm)) { |
| 4561 | i--; |
| 4562 | PathPermission pp = pps[i]; |
| 4563 | if (!readPerm) { |
| 4564 | final String pprperm = pp.getReadPermission(); |
| 4565 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 4566 | + pprperm + " for " + pp.getPath() |
| 4567 | + ": match=" + pp.match(path) |
| 4568 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 4569 | if (pprperm != null && pp.match(path) && |
| 4570 | (pm.checkUidPermission(pprperm, uid) |
| 4571 | == PackageManager.PERMISSION_GRANTED)) { |
| 4572 | readPerm = true; |
| 4573 | } |
| 4574 | } |
| 4575 | if (!writePerm) { |
| 4576 | final String ppwperm = pp.getWritePermission(); |
| 4577 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 4578 | + ppwperm + " for " + pp.getPath() |
| 4579 | + ": match=" + pp.match(path) |
| 4580 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 4581 | if (ppwperm != null && pp.match(path) && |
| 4582 | (pm.checkUidPermission(ppwperm, uid) |
| 4583 | == PackageManager.PERMISSION_GRANTED)) { |
| 4584 | writePerm = true; |
| 4585 | } |
| 4586 | } |
| 4587 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4588 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4589 | } catch (RemoteException e) { |
| 4590 | return false; |
| 4591 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4592 | |
| 4593 | return readPerm && writePerm; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4594 | } |
| 4595 | |
| 4596 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 4597 | int modeFlags) { |
| 4598 | // Root gets to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4599 | if (uid == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4600 | return true; |
| 4601 | } |
| 4602 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 4603 | if (perms == null) return false; |
| 4604 | UriPermission perm = perms.get(uri); |
| 4605 | if (perm == null) return false; |
| 4606 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 4607 | } |
| 4608 | |
| 4609 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
| 4610 | // Another redirected-binder-call permissions check as in |
| 4611 | // {@link checkComponentPermission}. |
| 4612 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4613 | if (tlsIdentity != null) { |
| 4614 | uid = tlsIdentity.uid; |
| 4615 | pid = tlsIdentity.pid; |
| 4616 | } |
| 4617 | |
| 4618 | // Our own process gets to do everything. |
| 4619 | if (pid == MY_PID) { |
| 4620 | return PackageManager.PERMISSION_GRANTED; |
| 4621 | } |
| 4622 | synchronized(this) { |
| 4623 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 4624 | ? PackageManager.PERMISSION_GRANTED |
| 4625 | : PackageManager.PERMISSION_DENIED; |
| 4626 | } |
| 4627 | } |
| 4628 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4629 | /** |
| 4630 | * Check if the targetPkg can be granted permission to access uri by |
| 4631 | * the callingUid using the given modeFlags. Throws a security exception |
| 4632 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 4633 | * if the URI permission grant should be performed; returns -1 if it is not |
| 4634 | * needed (for example targetPkg already has permission to access the URI). |
| 4635 | */ |
| 4636 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
| 4637 | Uri uri, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4638 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4639 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4640 | if (modeFlags == 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4641 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4642 | } |
| 4643 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4644 | if (targetPkg != null) { |
| 4645 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4646 | "Checking grant " + targetPkg + " permission to " + uri); |
| 4647 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4648 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4649 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4650 | |
| 4651 | // If this is not a content: uri, we can't do anything with it. |
| 4652 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4653 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4654 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4655 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4656 | } |
| 4657 | |
| 4658 | String name = uri.getAuthority(); |
| 4659 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4660 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4661 | if (cpr != null) { |
| 4662 | pi = cpr.info; |
| 4663 | } else { |
| 4664 | try { |
| 4665 | pi = pm.resolveContentProvider(name, |
| 4666 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4667 | } catch (RemoteException ex) { |
| 4668 | } |
| 4669 | } |
| 4670 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 4671 | Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4672 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4673 | } |
| 4674 | |
| 4675 | int targetUid; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4676 | if (targetPkg != null) { |
| 4677 | try { |
| 4678 | targetUid = pm.getPackageUid(targetPkg); |
| 4679 | if (targetUid < 0) { |
| 4680 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4681 | "Can't grant URI permission no uid for: " + targetPkg); |
| 4682 | return -1; |
| 4683 | } |
| 4684 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4685 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4686 | } |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4687 | } else { |
| 4688 | targetUid = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4689 | } |
| 4690 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4691 | if (targetUid >= 0) { |
| 4692 | // First... does the target actually need this permission? |
| 4693 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 4694 | // No need to grant the target this permission. |
| 4695 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4696 | "Target " + targetPkg + " already has full permission to " + uri); |
| 4697 | return -1; |
| 4698 | } |
| 4699 | } else { |
| 4700 | // First... there is no target package, so can anyone access it? |
| 4701 | boolean allowed = pi.exported; |
| 4702 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4703 | if (pi.readPermission != null) { |
| 4704 | allowed = false; |
| 4705 | } |
| 4706 | } |
| 4707 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4708 | if (pi.writePermission != null) { |
| 4709 | allowed = false; |
| 4710 | } |
| 4711 | } |
| 4712 | if (allowed) { |
| 4713 | return -1; |
| 4714 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4715 | } |
| 4716 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4717 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4718 | if (!pi.grantUriPermissions) { |
| 4719 | throw new SecurityException("Provider " + pi.packageName |
| 4720 | + "/" + pi.name |
| 4721 | + " does not allow granting of Uri permissions (uri " |
| 4722 | + uri + ")"); |
| 4723 | } |
| 4724 | if (pi.uriPermissionPatterns != null) { |
| 4725 | final int N = pi.uriPermissionPatterns.length; |
| 4726 | boolean allowed = false; |
| 4727 | for (int i=0; i<N; i++) { |
| 4728 | if (pi.uriPermissionPatterns[i] != null |
| 4729 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 4730 | allowed = true; |
| 4731 | break; |
| 4732 | } |
| 4733 | } |
| 4734 | if (!allowed) { |
| 4735 | throw new SecurityException("Provider " + pi.packageName |
| 4736 | + "/" + pi.name |
| 4737 | + " does not allow granting of permission to path of Uri " |
| 4738 | + uri); |
| 4739 | } |
| 4740 | } |
| 4741 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4742 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4743 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4744 | if (callingUid != Process.myUid()) { |
| 4745 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 4746 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4747 | throw new SecurityException("Uid " + callingUid |
| 4748 | + " does not have permission to uri " + uri); |
| 4749 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4750 | } |
| 4751 | } |
| 4752 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4753 | return targetUid; |
| 4754 | } |
| 4755 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4756 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 4757 | Uri uri, int modeFlags) { |
| 4758 | synchronized(this) { |
| 4759 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags); |
| 4760 | } |
| 4761 | } |
| 4762 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4763 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 4764 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 4765 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4766 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4767 | if (modeFlags == 0) { |
| 4768 | return; |
| 4769 | } |
| 4770 | |
| 4771 | // 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] | 4772 | // to the uri, and the target doesn't. Let's now give this to |
| 4773 | // the target. |
| 4774 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4775 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4776 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4777 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4778 | HashMap<Uri, UriPermission> targetUris |
| 4779 | = mGrantedUriPermissions.get(targetUid); |
| 4780 | if (targetUris == null) { |
| 4781 | targetUris = new HashMap<Uri, UriPermission>(); |
| 4782 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 4783 | } |
| 4784 | |
| 4785 | UriPermission perm = targetUris.get(uri); |
| 4786 | if (perm == null) { |
| 4787 | perm = new UriPermission(targetUid, uri); |
| 4788 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4789 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4790 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4791 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4792 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4793 | perm.globalModeFlags |= modeFlags; |
Vairavan Srinivasan | 91c12c2 | 2011-01-21 18:26:06 -0800 | [diff] [blame] | 4794 | } else { |
| 4795 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4796 | perm.readOwners.add(owner); |
| 4797 | owner.addReadPermission(perm); |
| 4798 | } |
| 4799 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4800 | perm.writeOwners.add(owner); |
| 4801 | owner.addWritePermission(perm); |
| 4802 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4803 | } |
| 4804 | } |
| 4805 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4806 | void grantUriPermissionLocked(int callingUid, |
| 4807 | String targetPkg, Uri uri, int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4808 | if (targetPkg == null) { |
| 4809 | throw new NullPointerException("targetPkg"); |
| 4810 | } |
| 4811 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4812 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags); |
| 4813 | if (targetUid < 0) { |
| 4814 | return; |
| 4815 | } |
| 4816 | |
| 4817 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 4818 | } |
| 4819 | |
| 4820 | /** |
| 4821 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 4822 | */ |
| 4823 | int checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 4824 | String targetPkg, Intent intent) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4825 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4826 | "Checking URI perm to " + (intent != null ? intent.getData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4827 | + " from " + intent + "; flags=0x" |
| 4828 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 4829 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4830 | if (targetPkg == null) { |
| 4831 | throw new NullPointerException("targetPkg"); |
| 4832 | } |
| 4833 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4834 | if (intent == null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4835 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4836 | } |
| 4837 | Uri data = intent.getData(); |
| 4838 | if (data == null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4839 | return -1; |
| 4840 | } |
| 4841 | return checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 4842 | intent.getFlags()); |
| 4843 | } |
| 4844 | |
| 4845 | /** |
| 4846 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 4847 | */ |
| 4848 | void grantUriPermissionUncheckedFromIntentLocked(int targetUid, |
| 4849 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
| 4850 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, intent.getData(), |
| 4851 | intent.getFlags(), owner); |
| 4852 | } |
| 4853 | |
| 4854 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 4855 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
| 4856 | int targetUid = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, intent); |
| 4857 | if (targetUid < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4858 | return; |
| 4859 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4860 | |
| 4861 | grantUriPermissionUncheckedFromIntentLocked(targetUid, targetPkg, intent, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4862 | } |
| 4863 | |
| 4864 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 4865 | Uri uri, int modeFlags) { |
| 4866 | synchronized(this) { |
| 4867 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 4868 | if (r == null) { |
| 4869 | throw new SecurityException("Unable to find app for caller " |
| 4870 | + caller |
| 4871 | + " when granting permission to uri " + uri); |
| 4872 | } |
| 4873 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4874 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4875 | } |
| 4876 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4877 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4878 | } |
| 4879 | |
| 4880 | grantUriPermissionLocked(r.info.uid, targetPkg, uri, modeFlags, |
| 4881 | null); |
| 4882 | } |
| 4883 | } |
| 4884 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4885 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4886 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4887 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 4888 | HashMap<Uri, UriPermission> perms |
| 4889 | = mGrantedUriPermissions.get(perm.uid); |
| 4890 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4891 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4892 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4893 | perms.remove(perm.uri); |
| 4894 | if (perms.size() == 0) { |
| 4895 | mGrantedUriPermissions.remove(perm.uid); |
| 4896 | } |
| 4897 | } |
| 4898 | } |
| 4899 | } |
| 4900 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4901 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 4902 | int modeFlags) { |
| 4903 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4904 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4905 | if (modeFlags == 0) { |
| 4906 | return; |
| 4907 | } |
| 4908 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4909 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4910 | "Revoking all granted permissions to " + uri); |
| 4911 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4912 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4913 | |
| 4914 | final String authority = uri.getAuthority(); |
| 4915 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4916 | ContentProviderRecord cpr = mProvidersByName.get(authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4917 | if (cpr != null) { |
| 4918 | pi = cpr.info; |
| 4919 | } else { |
| 4920 | try { |
| 4921 | pi = pm.resolveContentProvider(authority, |
| 4922 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4923 | } catch (RemoteException ex) { |
| 4924 | } |
| 4925 | } |
| 4926 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 4927 | Slog.w(TAG, "No content provider found for permission revoke: " + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4928 | return; |
| 4929 | } |
| 4930 | |
| 4931 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4932 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4933 | // Right now, if you are not the original owner of the permission, |
| 4934 | // you are not allowed to revoke it. |
| 4935 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4936 | throw new SecurityException("Uid " + callingUid |
| 4937 | + " does not have permission to uri " + uri); |
| 4938 | //} |
| 4939 | } |
| 4940 | |
| 4941 | // Go through all of the permissions and remove any that match. |
| 4942 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 4943 | if (SEGMENTS != null) { |
| 4944 | final int NS = SEGMENTS.size(); |
| 4945 | int N = mGrantedUriPermissions.size(); |
| 4946 | for (int i=0; i<N; i++) { |
| 4947 | HashMap<Uri, UriPermission> perms |
| 4948 | = mGrantedUriPermissions.valueAt(i); |
| 4949 | Iterator<UriPermission> it = perms.values().iterator(); |
| 4950 | toploop: |
| 4951 | while (it.hasNext()) { |
| 4952 | UriPermission perm = it.next(); |
| 4953 | Uri targetUri = perm.uri; |
| 4954 | if (!authority.equals(targetUri.getAuthority())) { |
| 4955 | continue; |
| 4956 | } |
| 4957 | List<String> targetSegments = targetUri.getPathSegments(); |
| 4958 | if (targetSegments == null) { |
| 4959 | continue; |
| 4960 | } |
| 4961 | if (targetSegments.size() < NS) { |
| 4962 | continue; |
| 4963 | } |
| 4964 | for (int j=0; j<NS; j++) { |
| 4965 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 4966 | continue toploop; |
| 4967 | } |
| 4968 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4969 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4970 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4971 | perm.clearModes(modeFlags); |
| 4972 | if (perm.modeFlags == 0) { |
| 4973 | it.remove(); |
| 4974 | } |
| 4975 | } |
| 4976 | if (perms.size() == 0) { |
| 4977 | mGrantedUriPermissions.remove( |
| 4978 | mGrantedUriPermissions.keyAt(i)); |
| 4979 | N--; |
| 4980 | i--; |
| 4981 | } |
| 4982 | } |
| 4983 | } |
| 4984 | } |
| 4985 | |
| 4986 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 4987 | int modeFlags) { |
| 4988 | synchronized(this) { |
| 4989 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 4990 | if (r == null) { |
| 4991 | throw new SecurityException("Unable to find app for caller " |
| 4992 | + caller |
| 4993 | + " when revoking permission to uri " + uri); |
| 4994 | } |
| 4995 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4996 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4997 | return; |
| 4998 | } |
| 4999 | |
| 5000 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5001 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5002 | if (modeFlags == 0) { |
| 5003 | return; |
| 5004 | } |
| 5005 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5006 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5007 | |
| 5008 | final String authority = uri.getAuthority(); |
| 5009 | ProviderInfo pi = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5010 | ContentProviderRecord cpr = mProvidersByName.get(authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5011 | if (cpr != null) { |
| 5012 | pi = cpr.info; |
| 5013 | } else { |
| 5014 | try { |
| 5015 | pi = pm.resolveContentProvider(authority, |
| 5016 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 5017 | } catch (RemoteException ex) { |
| 5018 | } |
| 5019 | } |
| 5020 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5021 | Slog.w(TAG, "No content provider found for permission revoke: " |
| 5022 | + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5023 | return; |
| 5024 | } |
| 5025 | |
| 5026 | revokeUriPermissionLocked(r.info.uid, uri, modeFlags); |
| 5027 | } |
| 5028 | } |
| 5029 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5030 | @Override |
| 5031 | public IBinder newUriPermissionOwner(String name) { |
| 5032 | synchronized(this) { |
| 5033 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 5034 | return owner.getExternalTokenLocked(); |
| 5035 | } |
| 5036 | } |
| 5037 | |
| 5038 | @Override |
| 5039 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 5040 | Uri uri, int modeFlags) { |
| 5041 | synchronized(this) { |
| 5042 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5043 | if (owner == null) { |
| 5044 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5045 | } |
| 5046 | if (fromUid != Binder.getCallingUid()) { |
| 5047 | if (Binder.getCallingUid() != Process.myUid()) { |
| 5048 | // Only system code can grant URI permissions on behalf |
| 5049 | // of other users. |
| 5050 | throw new SecurityException("nice try"); |
| 5051 | } |
| 5052 | } |
| 5053 | if (targetPkg == null) { |
| 5054 | throw new IllegalArgumentException("null target"); |
| 5055 | } |
| 5056 | if (uri == null) { |
| 5057 | throw new IllegalArgumentException("null uri"); |
| 5058 | } |
| 5059 | |
| 5060 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 5061 | } |
| 5062 | } |
| 5063 | |
| 5064 | @Override |
| 5065 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 5066 | synchronized(this) { |
| 5067 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5068 | if (owner == null) { |
| 5069 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5070 | } |
| 5071 | |
| 5072 | if (uri == null) { |
| 5073 | owner.removeUriPermissionsLocked(mode); |
| 5074 | } else { |
| 5075 | owner.removeUriPermissionLocked(uri, mode); |
| 5076 | } |
| 5077 | } |
| 5078 | } |
| 5079 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5080 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 5081 | synchronized (this) { |
| 5082 | ProcessRecord app = |
| 5083 | who != null ? getRecordForAppLocked(who) : null; |
| 5084 | if (app == null) return; |
| 5085 | |
| 5086 | Message msg = Message.obtain(); |
| 5087 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 5088 | msg.obj = app; |
| 5089 | msg.arg1 = waiting ? 1 : 0; |
| 5090 | mHandler.sendMessage(msg); |
| 5091 | } |
| 5092 | } |
| 5093 | |
| 5094 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5095 | final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ); |
| 5096 | final long hiddenAppMem = mProcessList.getMemLevel(ProcessList.HIDDEN_APP_MIN_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5097 | outInfo.availMem = Process.getFreeMemory(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5098 | outInfo.threshold = homeAppMem; |
| 5099 | outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2)); |
| 5100 | outInfo.hiddenAppThreshold = hiddenAppMem; |
| 5101 | outInfo.secondaryServerThreshold = mProcessList.getMemLevel( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 5102 | ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5103 | outInfo.visibleAppThreshold = mProcessList.getMemLevel( |
| 5104 | ProcessList.VISIBLE_APP_ADJ); |
| 5105 | outInfo.foregroundAppThreshold = mProcessList.getMemLevel( |
| 5106 | ProcessList.FOREGROUND_APP_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5107 | } |
| 5108 | |
| 5109 | // ========================================================= |
| 5110 | // TASK MANAGEMENT |
| 5111 | // ========================================================= |
| 5112 | |
| 5113 | public List getTasks(int maxNum, int flags, |
| 5114 | IThumbnailReceiver receiver) { |
| 5115 | ArrayList list = new ArrayList(); |
| 5116 | |
| 5117 | PendingThumbnailsRecord pending = null; |
| 5118 | IApplicationThread topThumbnail = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5119 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5120 | |
| 5121 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5122 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5123 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 5124 | + ", receiver=" + receiver); |
| 5125 | |
| 5126 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 5127 | != PackageManager.PERMISSION_GRANTED) { |
| 5128 | if (receiver != null) { |
| 5129 | // If the caller wants to wait for pending thumbnails, |
| 5130 | // it ain't gonna get them. |
| 5131 | try { |
| 5132 | receiver.finished(); |
| 5133 | } catch (RemoteException ex) { |
| 5134 | } |
| 5135 | } |
| 5136 | String msg = "Permission Denial: getTasks() from pid=" |
| 5137 | + Binder.getCallingPid() |
| 5138 | + ", uid=" + Binder.getCallingUid() |
| 5139 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5140 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5141 | throw new SecurityException(msg); |
| 5142 | } |
| 5143 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5144 | int pos = mMainStack.mHistory.size()-1; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5145 | ActivityRecord next = |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5146 | pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5147 | ActivityRecord top = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5148 | TaskRecord curTask = null; |
| 5149 | int numActivities = 0; |
| 5150 | int numRunning = 0; |
| 5151 | while (pos >= 0 && maxNum > 0) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5152 | final ActivityRecord r = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5153 | pos--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5154 | next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5155 | |
| 5156 | // Initialize state for next task if needed. |
| 5157 | if (top == null || |
| 5158 | (top.state == ActivityState.INITIALIZING |
| 5159 | && top.task == r.task)) { |
| 5160 | top = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5161 | curTask = r.task; |
| 5162 | numActivities = numRunning = 0; |
| 5163 | } |
| 5164 | |
| 5165 | // Add 'r' into the current task. |
| 5166 | numActivities++; |
| 5167 | if (r.app != null && r.app.thread != null) { |
| 5168 | numRunning++; |
| 5169 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5170 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5171 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5172 | TAG, r.intent.getComponent().flattenToShortString() |
| 5173 | + ": task=" + r.task); |
| 5174 | |
| 5175 | // If the next one is a different task, generate a new |
| 5176 | // TaskInfo entry for what we have. |
| 5177 | if (next == null || next.task != curTask) { |
| 5178 | ActivityManager.RunningTaskInfo ci |
| 5179 | = new ActivityManager.RunningTaskInfo(); |
| 5180 | ci.id = curTask.taskId; |
| 5181 | ci.baseActivity = r.intent.getComponent(); |
| 5182 | ci.topActivity = top.intent.getComponent(); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5183 | if (top.thumbHolder != null) { |
| 5184 | ci.description = top.thumbHolder.lastDescription; |
| 5185 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5186 | ci.numActivities = numActivities; |
| 5187 | ci.numRunning = numRunning; |
| 5188 | //System.out.println( |
| 5189 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 5190 | if (ci.thumbnail == null && receiver != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5191 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5192 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 5193 | + " app=" + top.app |
| 5194 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 5195 | if (top.state == ActivityState.RESUMED |
| 5196 | || top.state == ActivityState.PAUSING) { |
| 5197 | if (top.idle && top.app != null |
| 5198 | && top.app.thread != null) { |
| 5199 | topRecord = top; |
| 5200 | topThumbnail = top.app.thread; |
| 5201 | } else { |
| 5202 | top.thumbnailNeeded = true; |
| 5203 | } |
| 5204 | } |
| 5205 | if (pending == null) { |
| 5206 | pending = new PendingThumbnailsRecord(receiver); |
| 5207 | } |
| 5208 | pending.pendingRecords.add(top); |
| 5209 | } |
| 5210 | list.add(ci); |
| 5211 | maxNum--; |
| 5212 | top = null; |
| 5213 | } |
| 5214 | } |
| 5215 | |
| 5216 | if (pending != null) { |
| 5217 | mPendingThumbnails.add(pending); |
| 5218 | } |
| 5219 | } |
| 5220 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5221 | 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] | 5222 | |
| 5223 | if (topThumbnail != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5224 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5225 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5226 | topThumbnail.requestThumbnail(topRecord.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5227 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5228 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5229 | sendPendingThumbnail(null, topRecord.appToken, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5230 | } |
| 5231 | } |
| 5232 | |
| 5233 | if (pending == null && receiver != null) { |
| 5234 | // In this case all thumbnails were available and the client |
| 5235 | // is being asked to be told when the remaining ones come in... |
| 5236 | // which is unusually, since the top-most currently running |
| 5237 | // activity should never have a canned thumbnail! Oh well. |
| 5238 | try { |
| 5239 | receiver.finished(); |
| 5240 | } catch (RemoteException ex) { |
| 5241 | } |
| 5242 | } |
| 5243 | |
| 5244 | return list; |
| 5245 | } |
| 5246 | |
| 5247 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
| 5248 | int flags) { |
| 5249 | synchronized (this) { |
| 5250 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 5251 | "getRecentTasks()"); |
| 5252 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5253 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5255 | final int N = mRecentTasks.size(); |
| 5256 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 5257 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 5258 | maxNum < N ? maxNum : N); |
| 5259 | for (int i=0; i<N && maxNum > 0; i++) { |
| 5260 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5261 | // Return the entry if desired by the caller. We always return |
| 5262 | // the first entry, because callers always expect this to be the |
| 5263 | // forground app. We may filter others if the caller has |
| 5264 | // not supplied RECENT_WITH_EXCLUDED and there is some reason |
| 5265 | // we should exclude the entry. |
| 5266 | if (i == 0 |
| 5267 | || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5268 | || (tr.intent == null) |
| 5269 | || ((tr.intent.getFlags() |
| 5270 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 5271 | ActivityManager.RecentTaskInfo rti |
| 5272 | = new ActivityManager.RecentTaskInfo(); |
| 5273 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5274 | rti.persistentId = tr.taskId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5275 | rti.baseIntent = new Intent( |
| 5276 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5277 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 5278 | rti.description = tr.lastDescription; |
| 5279 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5280 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5281 | // Check whether this activity is currently available. |
| 5282 | try { |
| 5283 | if (rti.origActivity != null) { |
| 5284 | if (pm.getActivityInfo(rti.origActivity, 0) == null) { |
| 5285 | continue; |
| 5286 | } |
| 5287 | } else if (rti.baseIntent != null) { |
| 5288 | if (pm.queryIntentActivities(rti.baseIntent, |
| 5289 | null, 0) == null) { |
| 5290 | continue; |
| 5291 | } |
| 5292 | } |
| 5293 | } catch (RemoteException e) { |
| 5294 | // Will never happen. |
| 5295 | } |
| 5296 | } |
| 5297 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5298 | res.add(rti); |
| 5299 | maxNum--; |
| 5300 | } |
| 5301 | } |
| 5302 | return res; |
| 5303 | } |
| 5304 | } |
| 5305 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5306 | private TaskRecord taskForIdLocked(int id) { |
| 5307 | final int N = mRecentTasks.size(); |
| 5308 | for (int i=0; i<N; i++) { |
| 5309 | TaskRecord tr = mRecentTasks.get(i); |
| 5310 | if (tr.taskId == id) { |
| 5311 | return tr; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5312 | } |
| 5313 | } |
| 5314 | return null; |
| 5315 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5316 | |
| 5317 | public ActivityManager.TaskThumbnails getTaskThumbnails(int id) { |
| 5318 | synchronized (this) { |
| 5319 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5320 | "getTaskThumbnails()"); |
| 5321 | TaskRecord tr = taskForIdLocked(id); |
| 5322 | if (tr != null) { |
| 5323 | return mMainStack.getTaskThumbnailsLocked(tr); |
| 5324 | } |
| 5325 | } |
| 5326 | return null; |
| 5327 | } |
| 5328 | |
| 5329 | public boolean removeSubTask(int taskId, int subTaskIndex) { |
| 5330 | synchronized (this) { |
| 5331 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5332 | "removeSubTask()"); |
| 5333 | long ident = Binder.clearCallingIdentity(); |
| 5334 | try { |
| 5335 | return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex) != null; |
| 5336 | } finally { |
| 5337 | Binder.restoreCallingIdentity(ident); |
| 5338 | } |
| 5339 | } |
| 5340 | } |
| 5341 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5342 | private void cleanUpRemovedTaskLocked(ActivityRecord root, boolean killProcesses) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5343 | TaskRecord tr = root.task; |
| 5344 | Intent baseIntent = new Intent( |
| 5345 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5346 | ComponentName component = baseIntent.getComponent(); |
| 5347 | if (component == null) { |
| 5348 | Slog.w(TAG, "Now component for base intent of task: " + tr); |
| 5349 | return; |
| 5350 | } |
| 5351 | |
| 5352 | // Find any running services associated with this app. |
| 5353 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
| 5354 | for (ServiceRecord sr : mServices.values()) { |
| 5355 | if (sr.packageName.equals(component.getPackageName())) { |
| 5356 | services.add(sr); |
| 5357 | } |
| 5358 | } |
| 5359 | |
| 5360 | // Take care of any running services associated with the app. |
| 5361 | for (int i=0; i<services.size(); i++) { |
| 5362 | ServiceRecord sr = services.get(i); |
| 5363 | if (sr.startRequested) { |
| 5364 | if ((sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0) { |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5365 | Slog.i(TAG, "Stopping service " + sr.shortName + ": remove task"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5366 | stopServiceLocked(sr); |
| 5367 | } else { |
| 5368 | sr.pendingStarts.add(new ServiceRecord.StartItem(sr, true, |
| 5369 | sr.makeNextStartId(), baseIntent, -1)); |
| 5370 | if (sr.app != null && sr.app.thread != null) { |
| 5371 | sendServiceArgsLocked(sr, false); |
| 5372 | } |
| 5373 | } |
| 5374 | } |
| 5375 | } |
| 5376 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5377 | if (killProcesses) { |
| 5378 | // Find any running processes associated with this app. |
| 5379 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 5380 | SparseArray<ProcessRecord> appProcs |
| 5381 | = mProcessNames.getMap().get(component.getPackageName()); |
| 5382 | if (appProcs != null) { |
| 5383 | for (int i=0; i<appProcs.size(); i++) { |
| 5384 | procs.add(appProcs.valueAt(i)); |
| 5385 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5386 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5387 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5388 | // Kill the running processes. |
| 5389 | for (int i=0; i<procs.size(); i++) { |
| 5390 | ProcessRecord pr = procs.get(i); |
| 5391 | if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 5392 | Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task"); |
| 5393 | EventLog.writeEvent(EventLogTags.AM_KILL, pr.pid, |
| 5394 | pr.processName, pr.setAdj, "remove task"); |
| 5395 | Process.killProcessQuiet(pr.pid); |
| 5396 | } else { |
| 5397 | pr.waitingToKill = "remove task"; |
| 5398 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5399 | } |
| 5400 | } |
| 5401 | } |
| 5402 | |
| 5403 | public boolean removeTask(int taskId, int flags) { |
| 5404 | synchronized (this) { |
| 5405 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5406 | "removeTask()"); |
| 5407 | long ident = Binder.clearCallingIdentity(); |
| 5408 | try { |
| 5409 | ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1); |
| 5410 | if (r != null) { |
| 5411 | mRecentTasks.remove(r.task); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5412 | cleanUpRemovedTaskLocked(r, |
| 5413 | (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5414 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5415 | } else { |
| 5416 | TaskRecord tr = null; |
| 5417 | int i=0; |
| 5418 | while (i < mRecentTasks.size()) { |
| 5419 | TaskRecord t = mRecentTasks.get(i); |
| 5420 | if (t.taskId == taskId) { |
| 5421 | tr = t; |
| 5422 | break; |
| 5423 | } |
| 5424 | i++; |
| 5425 | } |
| 5426 | if (tr != null) { |
| 5427 | if (tr.numActivities <= 0) { |
| 5428 | // Caller is just removing a recent task that is |
| 5429 | // not actively running. That is easy! |
| 5430 | mRecentTasks.remove(i); |
| 5431 | } else { |
| 5432 | Slog.w(TAG, "removeTask: task " + taskId |
| 5433 | + " does not have activities to remove, " |
| 5434 | + " but numActivities=" + tr.numActivities |
| 5435 | + ": " + tr); |
| 5436 | } |
| 5437 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5438 | } |
| 5439 | } finally { |
| 5440 | Binder.restoreCallingIdentity(ident); |
| 5441 | } |
| 5442 | } |
| 5443 | return false; |
| 5444 | } |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5445 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5446 | private final int findAffinityTaskTopLocked(int startIndex, String affinity) { |
| 5447 | int j; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5448 | TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5449 | TaskRecord jt = startTask; |
| 5450 | |
| 5451 | // First look backwards |
| 5452 | for (j=startIndex-1; j>=0; j--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5453 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5454 | if (r.task != jt) { |
| 5455 | jt = r.task; |
| 5456 | if (affinity.equals(jt.affinity)) { |
| 5457 | return j; |
| 5458 | } |
| 5459 | } |
| 5460 | } |
| 5461 | |
| 5462 | // Now look forwards |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5463 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5464 | jt = startTask; |
| 5465 | for (j=startIndex+1; j<N; j++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5466 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5467 | if (r.task != jt) { |
| 5468 | if (affinity.equals(jt.affinity)) { |
| 5469 | return j; |
| 5470 | } |
| 5471 | jt = r.task; |
| 5472 | } |
| 5473 | } |
| 5474 | |
| 5475 | // Might it be at the top? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5476 | 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] | 5477 | return N-1; |
| 5478 | } |
| 5479 | |
| 5480 | return -1; |
| 5481 | } |
| 5482 | |
| 5483 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5484 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5485 | */ |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5486 | public void moveTaskToFront(int task, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5487 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5488 | "moveTaskToFront()"); |
| 5489 | |
| 5490 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5491 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5492 | Binder.getCallingUid(), "Task to front")) { |
| 5493 | return; |
| 5494 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5495 | final long origId = Binder.clearCallingIdentity(); |
| 5496 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5497 | TaskRecord tr = taskForIdLocked(task); |
| 5498 | if (tr != null) { |
| 5499 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5500 | mMainStack.mUserLeaving = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5501 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5502 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5503 | // Caller wants the home activity moved with it. To accomplish this, |
| 5504 | // we'll just move the home task to the top first. |
| 5505 | mMainStack.moveHomeToFrontLocked(); |
| 5506 | } |
| 5507 | mMainStack.moveTaskToFrontLocked(tr, null); |
| 5508 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5509 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5510 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 5511 | ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5512 | if (hr.task.taskId == task) { |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5513 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5514 | mMainStack.mUserLeaving = true; |
| 5515 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5516 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5517 | // Caller wants the home activity moved with it. To accomplish this, |
| 5518 | // we'll just move the home task to the top first. |
| 5519 | mMainStack.moveHomeToFrontLocked(); |
| 5520 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5521 | mMainStack.moveTaskToFrontLocked(hr.task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5522 | return; |
| 5523 | } |
| 5524 | } |
| 5525 | } finally { |
| 5526 | Binder.restoreCallingIdentity(origId); |
| 5527 | } |
| 5528 | } |
| 5529 | } |
| 5530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5531 | public void moveTaskToBack(int task) { |
| 5532 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5533 | "moveTaskToBack()"); |
| 5534 | |
| 5535 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5536 | if (mMainStack.mResumedActivity != null |
| 5537 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5538 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5539 | Binder.getCallingUid(), "Task to back")) { |
| 5540 | return; |
| 5541 | } |
| 5542 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5543 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5544 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5545 | Binder.restoreCallingIdentity(origId); |
| 5546 | } |
| 5547 | } |
| 5548 | |
| 5549 | /** |
| 5550 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 5551 | * of the history stack. The activity's order within the task is unchanged. |
| 5552 | * |
| 5553 | * @param token A reference to the activity we wish to move |
| 5554 | * @param nonRoot If false then this only works if the activity is the root |
| 5555 | * of a task; if true it will work for any activity in a task. |
| 5556 | * @return Returns true if the move completed, false if not. |
| 5557 | */ |
| 5558 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
| 5559 | synchronized(this) { |
| 5560 | final long origId = Binder.clearCallingIdentity(); |
| 5561 | int taskId = getTaskForActivityLocked(token, !nonRoot); |
| 5562 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5563 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5564 | } |
| 5565 | Binder.restoreCallingIdentity(origId); |
| 5566 | } |
| 5567 | return false; |
| 5568 | } |
| 5569 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5570 | public void moveTaskBackwards(int task) { |
| 5571 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5572 | "moveTaskBackwards()"); |
| 5573 | |
| 5574 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5575 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5576 | Binder.getCallingUid(), "Task backwards")) { |
| 5577 | return; |
| 5578 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5579 | final long origId = Binder.clearCallingIdentity(); |
| 5580 | moveTaskBackwardsLocked(task); |
| 5581 | Binder.restoreCallingIdentity(origId); |
| 5582 | } |
| 5583 | } |
| 5584 | |
| 5585 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5586 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5587 | } |
| 5588 | |
| 5589 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 5590 | synchronized(this) { |
| 5591 | return getTaskForActivityLocked(token, onlyRoot); |
| 5592 | } |
| 5593 | } |
| 5594 | |
| 5595 | int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5596 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5597 | TaskRecord lastTask = null; |
| 5598 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5599 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5600 | if (r.appToken == token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5601 | if (!onlyRoot || lastTask != r.task) { |
| 5602 | return r.task.taskId; |
| 5603 | } |
| 5604 | return -1; |
| 5605 | } |
| 5606 | lastTask = r.task; |
| 5607 | } |
| 5608 | |
| 5609 | return -1; |
| 5610 | } |
| 5611 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5612 | public void finishOtherInstances(IBinder token, ComponentName className) { |
| 5613 | synchronized(this) { |
| 5614 | final long origId = Binder.clearCallingIdentity(); |
| 5615 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5616 | int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5617 | TaskRecord lastTask = null; |
| 5618 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5619 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5620 | if (r.realActivity.equals(className) |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5621 | && r.appToken != token && lastTask != r.task) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5622 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5623 | null, "others")) { |
| 5624 | i--; |
| 5625 | N--; |
| 5626 | } |
| 5627 | } |
| 5628 | lastTask = r.task; |
| 5629 | } |
| 5630 | |
| 5631 | Binder.restoreCallingIdentity(origId); |
| 5632 | } |
| 5633 | } |
| 5634 | |
| 5635 | // ========================================================= |
| 5636 | // THUMBNAILS |
| 5637 | // ========================================================= |
| 5638 | |
| 5639 | public void reportThumbnail(IBinder token, |
| 5640 | Bitmap thumbnail, CharSequence description) { |
| 5641 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 5642 | final long origId = Binder.clearCallingIdentity(); |
| 5643 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 5644 | Binder.restoreCallingIdentity(origId); |
| 5645 | } |
| 5646 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5647 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5648 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 5649 | TaskRecord task = null; |
| 5650 | ArrayList receivers = null; |
| 5651 | |
| 5652 | //System.out.println("Send pending thumbnail: " + r); |
| 5653 | |
| 5654 | synchronized(this) { |
| 5655 | if (r == null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 5656 | r = mMainStack.isInStackLocked(token); |
| 5657 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5658 | return; |
| 5659 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5660 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5661 | if (thumbnail == null && r.thumbHolder != null) { |
| 5662 | thumbnail = r.thumbHolder.lastThumbnail; |
| 5663 | description = r.thumbHolder.lastDescription; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5664 | } |
| 5665 | if (thumbnail == null && !always) { |
| 5666 | // If there is no thumbnail, and this entry is not actually |
| 5667 | // going away, then abort for now and pick up the next |
| 5668 | // thumbnail we get. |
| 5669 | return; |
| 5670 | } |
| 5671 | task = r.task; |
| 5672 | |
| 5673 | int N = mPendingThumbnails.size(); |
| 5674 | int i=0; |
| 5675 | while (i<N) { |
| 5676 | PendingThumbnailsRecord pr = |
| 5677 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 5678 | //System.out.println("Looking in " + pr.pendingRecords); |
| 5679 | if (pr.pendingRecords.remove(r)) { |
| 5680 | if (receivers == null) { |
| 5681 | receivers = new ArrayList(); |
| 5682 | } |
| 5683 | receivers.add(pr); |
| 5684 | if (pr.pendingRecords.size() == 0) { |
| 5685 | pr.finished = true; |
| 5686 | mPendingThumbnails.remove(i); |
| 5687 | N--; |
| 5688 | continue; |
| 5689 | } |
| 5690 | } |
| 5691 | i++; |
| 5692 | } |
| 5693 | } |
| 5694 | |
| 5695 | if (receivers != null) { |
| 5696 | final int N = receivers.size(); |
| 5697 | for (int i=0; i<N; i++) { |
| 5698 | try { |
| 5699 | PendingThumbnailsRecord pr = |
| 5700 | (PendingThumbnailsRecord)receivers.get(i); |
| 5701 | pr.receiver.newThumbnail( |
| 5702 | task != null ? task.taskId : -1, thumbnail, description); |
| 5703 | if (pr.finished) { |
| 5704 | pr.receiver.finished(); |
| 5705 | } |
| 5706 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5707 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5708 | } |
| 5709 | } |
| 5710 | } |
| 5711 | } |
| 5712 | |
| 5713 | // ========================================================= |
| 5714 | // CONTENT PROVIDERS |
| 5715 | // ========================================================= |
| 5716 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 5717 | private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) { |
| 5718 | List<ProviderInfo> providers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5719 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5720 | providers = AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5721 | queryContentProviders(app.processName, app.info.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5722 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5723 | } catch (RemoteException ex) { |
| 5724 | } |
| 5725 | if (providers != null) { |
| 5726 | final int N = providers.size(); |
| 5727 | for (int i=0; i<N; i++) { |
| 5728 | ProviderInfo cpi = |
| 5729 | (ProviderInfo)providers.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5730 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
| 5731 | ContentProviderRecord cpr = mProvidersByClass.get(comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5732 | if (cpr == null) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5733 | cpr = new ContentProviderRecord(cpi, app.info, comp); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5734 | mProvidersByClass.put(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5735 | } |
| 5736 | app.pubProviders.put(cpi.name, cpr); |
| 5737 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 5738 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5739 | } |
| 5740 | } |
| 5741 | return providers; |
| 5742 | } |
| 5743 | |
| 5744 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5745 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5746 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
| 5747 | final int callingUid = (r != null) ? r.info.uid : Binder.getCallingUid(); |
| 5748 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5749 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5750 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5751 | return null; |
| 5752 | } |
| 5753 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5754 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5755 | == PackageManager.PERMISSION_GRANTED) { |
| 5756 | return null; |
| 5757 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5758 | |
| 5759 | PathPermission[] pps = cpi.pathPermissions; |
| 5760 | if (pps != null) { |
| 5761 | int i = pps.length; |
| 5762 | while (i > 0) { |
| 5763 | i--; |
| 5764 | PathPermission pp = pps[i]; |
| 5765 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5766 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5767 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5768 | return null; |
| 5769 | } |
| 5770 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5771 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5772 | == PackageManager.PERMISSION_GRANTED) { |
| 5773 | return null; |
| 5774 | } |
| 5775 | } |
| 5776 | } |
| 5777 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5778 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 5779 | if (perms != null) { |
| 5780 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 5781 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 5782 | return null; |
| 5783 | } |
| 5784 | } |
| 5785 | } |
| 5786 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5787 | String msg; |
| 5788 | if (!cpi.exported) { |
| 5789 | msg = "Permission Denial: opening provider " + cpi.name |
| 5790 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 5791 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 5792 | + cpi.applicationInfo.uid; |
| 5793 | } else { |
| 5794 | msg = "Permission Denial: opening provider " + cpi.name |
| 5795 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 5796 | + ", uid=" + callingUid + ") requires " |
| 5797 | + cpi.readPermission + " or " + cpi.writePermission; |
| 5798 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5799 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5800 | return msg; |
| 5801 | } |
| 5802 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5803 | boolean incProviderCount(ProcessRecord r, ContentProviderRecord cpr) { |
| 5804 | if (r != null) { |
| 5805 | Integer cnt = r.conProviders.get(cpr); |
| 5806 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 5807 | "Adding provider requested by " |
| 5808 | + r.processName + " from process " |
| 5809 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 5810 | + " cnt=" + (cnt == null ? 1 : cnt)); |
| 5811 | if (cnt == null) { |
| 5812 | cpr.clients.add(r); |
| 5813 | r.conProviders.put(cpr, new Integer(1)); |
| 5814 | return true; |
| 5815 | } else { |
| 5816 | r.conProviders.put(cpr, new Integer(cnt.intValue()+1)); |
| 5817 | } |
| 5818 | } else { |
| 5819 | cpr.externals++; |
| 5820 | } |
| 5821 | return false; |
| 5822 | } |
| 5823 | |
| 5824 | boolean decProviderCount(ProcessRecord r, ContentProviderRecord cpr) { |
| 5825 | if (r != null) { |
| 5826 | Integer cnt = r.conProviders.get(cpr); |
| 5827 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 5828 | "Removing provider requested by " |
| 5829 | + r.processName + " from process " |
| 5830 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 5831 | + " cnt=" + cnt); |
| 5832 | if (cnt == null || cnt.intValue() <= 1) { |
| 5833 | cpr.clients.remove(r); |
| 5834 | r.conProviders.remove(cpr); |
| 5835 | return true; |
| 5836 | } else { |
| 5837 | r.conProviders.put(cpr, new Integer(cnt.intValue()-1)); |
| 5838 | } |
| 5839 | } else { |
| 5840 | cpr.externals++; |
| 5841 | } |
| 5842 | return false; |
| 5843 | } |
| 5844 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5845 | private final ContentProviderHolder getContentProviderImpl( |
| 5846 | IApplicationThread caller, String name) { |
| 5847 | ContentProviderRecord cpr; |
| 5848 | ProviderInfo cpi = null; |
| 5849 | |
| 5850 | synchronized(this) { |
| 5851 | ProcessRecord r = null; |
| 5852 | if (caller != null) { |
| 5853 | r = getRecordForAppLocked(caller); |
| 5854 | if (r == null) { |
| 5855 | throw new SecurityException( |
| 5856 | "Unable to find app for caller " + caller |
| 5857 | + " (pid=" + Binder.getCallingPid() |
| 5858 | + ") when getting content provider " + name); |
| 5859 | } |
| 5860 | } |
| 5861 | |
| 5862 | // First check if this content provider has been published... |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 5863 | cpr = mProvidersByName.get(name); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5864 | boolean providerRunning = cpr != null; |
| 5865 | if (providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5866 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5867 | String msg; |
| 5868 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 5869 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5870 | } |
| 5871 | |
| 5872 | if (r != null && cpr.canRunHere(r)) { |
| 5873 | // This provider has been published or is in the process |
| 5874 | // of being published... but it is also allowed to run |
| 5875 | // in the caller's process, so don't make a connection |
| 5876 | // and just let the caller instantiate its own instance. |
| 5877 | if (cpr.provider != null) { |
| 5878 | // don't give caller the provider object, it needs |
| 5879 | // to make its own. |
| 5880 | cpr = new ContentProviderRecord(cpr); |
| 5881 | } |
| 5882 | return cpr; |
| 5883 | } |
| 5884 | |
| 5885 | final long origId = Binder.clearCallingIdentity(); |
| 5886 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5887 | // 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] | 5888 | // return it right away. |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5889 | final boolean countChanged = incProviderCount(r, cpr); |
| 5890 | if (countChanged) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5891 | if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 5892 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 5893 | // make sure to count it as being accessed and thus |
| 5894 | // back up on the LRU list. This is good because |
| 5895 | // content providers are often expensive to start. |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5896 | updateLruProcessLocked(cpr.proc, false, true); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 5897 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 5898 | } |
| 5899 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5900 | if (cpr.proc != null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5901 | if (false) { |
| 5902 | if (cpr.name.flattenToShortString().equals( |
| 5903 | "com.android.providers.calendar/.CalendarProvider2")) { |
| 5904 | Slog.v(TAG, "****************** KILLING " |
| 5905 | + cpr.name.flattenToShortString()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5906 | Process.killProcess(cpr.proc.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5907 | } |
| 5908 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5909 | boolean success = updateOomAdjLocked(cpr.proc); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5910 | if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success); |
| 5911 | // NOTE: there is still a race here where a signal could be |
| 5912 | // pending on the process even though we managed to update its |
| 5913 | // adj level. Not sure what to do about this, but at least |
| 5914 | // the race is now smaller. |
| 5915 | if (!success) { |
| 5916 | // Uh oh... it looks like the provider's process |
| 5917 | // has been killed on us. We need to wait for a new |
| 5918 | // process to be started, and make sure its death |
| 5919 | // doesn't kill our process. |
| 5920 | Slog.i(TAG, |
| 5921 | "Existing provider " + cpr.name.flattenToShortString() |
| 5922 | + " is crashing; detaching " + r); |
| 5923 | boolean lastRef = decProviderCount(r, cpr); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5924 | appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5925 | if (!lastRef) { |
| 5926 | // This wasn't the last ref our process had on |
| 5927 | // the provider... we have now been killed, bail. |
| 5928 | return null; |
| 5929 | } |
| 5930 | providerRunning = false; |
| 5931 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5932 | } |
| 5933 | |
| 5934 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5935 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5936 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5937 | if (!providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5938 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5939 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5940 | resolveContentProvider(name, |
| 5941 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5942 | } catch (RemoteException ex) { |
| 5943 | } |
| 5944 | if (cpi == null) { |
| 5945 | return null; |
| 5946 | } |
| 5947 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5948 | String msg; |
| 5949 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 5950 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5951 | } |
| 5952 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 5953 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 5954 | && !cpi.processName.equals("system")) { |
| 5955 | // If this content provider does not run in the system |
| 5956 | // process, and the system is not yet ready to run other |
| 5957 | // processes, then fail fast instead of hanging. |
| 5958 | throw new IllegalArgumentException( |
| 5959 | "Attempt to launch content provider before system ready"); |
| 5960 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5961 | |
| 5962 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
| 5963 | cpr = mProvidersByClass.get(comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5964 | final boolean firstClass = cpr == null; |
| 5965 | if (firstClass) { |
| 5966 | try { |
| 5967 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5968 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5969 | getApplicationInfo( |
| 5970 | cpi.applicationInfo.packageName, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5971 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5972 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5973 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5974 | + cpi.name); |
| 5975 | return null; |
| 5976 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5977 | cpr = new ContentProviderRecord(cpi, ai, comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5978 | } catch (RemoteException ex) { |
| 5979 | // pm is in same process, this will never happen. |
| 5980 | } |
| 5981 | } |
| 5982 | |
| 5983 | if (r != null && cpr.canRunHere(r)) { |
| 5984 | // If this is a multiprocess provider, then just return its |
| 5985 | // info and allow the caller to instantiate it. Only do |
| 5986 | // this if the provider is the same user as the caller's |
| 5987 | // process, or can run as root (so can be in any process). |
| 5988 | return cpr; |
| 5989 | } |
| 5990 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5991 | if (DEBUG_PROVIDER) { |
| 5992 | RuntimeException e = new RuntimeException("here"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5993 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.info.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5994 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5995 | } |
| 5996 | |
| 5997 | // This is single process, and our app is now connecting to it. |
| 5998 | // See if we are already in the process of launching this |
| 5999 | // provider. |
| 6000 | final int N = mLaunchingProviders.size(); |
| 6001 | int i; |
| 6002 | for (i=0; i<N; i++) { |
| 6003 | if (mLaunchingProviders.get(i) == cpr) { |
| 6004 | break; |
| 6005 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6006 | } |
| 6007 | |
| 6008 | // If the provider is not already being launched, then get it |
| 6009 | // started. |
| 6010 | if (i >= N) { |
| 6011 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6012 | |
| 6013 | try { |
| 6014 | // Content provider is now in use, its package can't be stopped. |
| 6015 | try { |
| 6016 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 6017 | cpr.appInfo.packageName, false); |
| 6018 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6019 | } catch (IllegalArgumentException e) { |
| 6020 | Slog.w(TAG, "Failed trying to unstop package " |
| 6021 | + cpr.appInfo.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6022 | } |
| 6023 | |
| 6024 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 6025 | cpr.appInfo, false, 0, "content provider", |
| 6026 | new ComponentName(cpi.applicationInfo.packageName, |
| 6027 | cpi.name), false); |
| 6028 | if (proc == null) { |
| 6029 | Slog.w(TAG, "Unable to launch app " |
| 6030 | + cpi.applicationInfo.packageName + "/" |
| 6031 | + cpi.applicationInfo.uid + " for provider " |
| 6032 | + name + ": process is bad"); |
| 6033 | return null; |
| 6034 | } |
| 6035 | cpr.launchingApp = proc; |
| 6036 | mLaunchingProviders.add(cpr); |
| 6037 | } finally { |
| 6038 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6039 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6040 | } |
| 6041 | |
| 6042 | // Make sure the provider is published (the same provider class |
| 6043 | // may be published under multiple names). |
| 6044 | if (firstClass) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6045 | mProvidersByClass.put(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6046 | } |
| 6047 | mProvidersByName.put(name, cpr); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6048 | incProviderCount(r, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6049 | } |
| 6050 | } |
| 6051 | |
| 6052 | // Wait for the provider to be published... |
| 6053 | synchronized (cpr) { |
| 6054 | while (cpr.provider == null) { |
| 6055 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6056 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6057 | + cpi.applicationInfo.packageName + "/" |
| 6058 | + cpi.applicationInfo.uid + " for provider " |
| 6059 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6060 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6061 | cpi.applicationInfo.packageName, |
| 6062 | cpi.applicationInfo.uid, name); |
| 6063 | return null; |
| 6064 | } |
| 6065 | try { |
| 6066 | cpr.wait(); |
| 6067 | } catch (InterruptedException ex) { |
| 6068 | } |
| 6069 | } |
| 6070 | } |
| 6071 | return cpr; |
| 6072 | } |
| 6073 | |
| 6074 | public final ContentProviderHolder getContentProvider( |
| 6075 | IApplicationThread caller, String name) { |
| 6076 | if (caller == null) { |
| 6077 | String msg = "null IApplicationThread when getting content provider " |
| 6078 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6079 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6080 | throw new SecurityException(msg); |
| 6081 | } |
| 6082 | |
| 6083 | return getContentProviderImpl(caller, name); |
| 6084 | } |
| 6085 | |
| 6086 | private ContentProviderHolder getContentProviderExternal(String name) { |
| 6087 | return getContentProviderImpl(null, name); |
| 6088 | } |
| 6089 | |
| 6090 | /** |
| 6091 | * Drop a content provider from a ProcessRecord's bookkeeping |
| 6092 | * @param cpr |
| 6093 | */ |
| 6094 | public void removeContentProvider(IApplicationThread caller, String name) { |
| 6095 | synchronized (this) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6096 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6097 | if(cpr == null) { |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6098 | // remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6099 | if (DEBUG_PROVIDER) Slog.v(TAG, name + |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6100 | " provider not found in providers list"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6101 | return; |
| 6102 | } |
| 6103 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 6104 | if (r == null) { |
| 6105 | throw new SecurityException( |
| 6106 | "Unable to find app for caller " + caller + |
| 6107 | " when removing content provider " + name); |
| 6108 | } |
| 6109 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6110 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
| 6111 | ContentProviderRecord localCpr = mProvidersByClass.get(comp); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6112 | if (localCpr.proc == r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6113 | //should not happen. taken care of as a local provider |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6114 | Slog.w(TAG, "removeContentProvider called on local provider: " |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6115 | + cpr.info.name + " in process " + r.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6116 | return; |
| 6117 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6118 | if (decProviderCount(r, localCpr)) { |
| 6119 | updateOomAdjLocked(); |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 6120 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6121 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6122 | } |
| 6123 | } |
| 6124 | |
| 6125 | private void removeContentProviderExternal(String name) { |
| 6126 | synchronized (this) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6127 | ContentProviderRecord cpr = mProvidersByName.get(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6128 | if(cpr == null) { |
| 6129 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6130 | 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] | 6131 | return; |
| 6132 | } |
| 6133 | |
| 6134 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6135 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
| 6136 | ContentProviderRecord localCpr = mProvidersByClass.get(comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6137 | localCpr.externals--; |
| 6138 | if (localCpr.externals < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6139 | Slog.e(TAG, "Externals < 0 for content provider " + localCpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6140 | } |
| 6141 | updateOomAdjLocked(); |
| 6142 | } |
| 6143 | } |
| 6144 | |
| 6145 | public final void publishContentProviders(IApplicationThread caller, |
| 6146 | List<ContentProviderHolder> providers) { |
| 6147 | if (providers == null) { |
| 6148 | return; |
| 6149 | } |
| 6150 | |
| 6151 | synchronized(this) { |
| 6152 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 6153 | if (r == null) { |
| 6154 | throw new SecurityException( |
| 6155 | "Unable to find app for caller " + caller |
| 6156 | + " (pid=" + Binder.getCallingPid() |
| 6157 | + ") when publishing content providers"); |
| 6158 | } |
| 6159 | |
| 6160 | final long origId = Binder.clearCallingIdentity(); |
| 6161 | |
| 6162 | final int N = providers.size(); |
| 6163 | for (int i=0; i<N; i++) { |
| 6164 | ContentProviderHolder src = providers.get(i); |
| 6165 | if (src == null || src.info == null || src.provider == null) { |
| 6166 | continue; |
| 6167 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6168 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6169 | if (dst != null) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6170 | ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name); |
| 6171 | mProvidersByClass.put(comp, dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6172 | String names[] = dst.info.authority.split(";"); |
| 6173 | for (int j = 0; j < names.length; j++) { |
| 6174 | mProvidersByName.put(names[j], dst); |
| 6175 | } |
| 6176 | |
| 6177 | int NL = mLaunchingProviders.size(); |
| 6178 | int j; |
| 6179 | for (j=0; j<NL; j++) { |
| 6180 | if (mLaunchingProviders.get(j) == dst) { |
| 6181 | mLaunchingProviders.remove(j); |
| 6182 | j--; |
| 6183 | NL--; |
| 6184 | } |
| 6185 | } |
| 6186 | synchronized (dst) { |
| 6187 | dst.provider = src.provider; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6188 | dst.proc = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6189 | dst.notifyAll(); |
| 6190 | } |
| 6191 | updateOomAdjLocked(r); |
| 6192 | } |
| 6193 | } |
| 6194 | |
| 6195 | Binder.restoreCallingIdentity(origId); |
| 6196 | } |
| 6197 | } |
| 6198 | |
| 6199 | public static final void installSystemProviders() { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6200 | List<ProviderInfo> providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6201 | synchronized (mSelf) { |
| 6202 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 6203 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 6204 | if (providers != null) { |
| 6205 | for (int i=providers.size()-1; i>=0; i--) { |
| 6206 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 6207 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6208 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 6209 | + ": not system .apk"); |
| 6210 | providers.remove(i); |
| 6211 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6212 | } |
| 6213 | } |
| 6214 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6215 | if (providers != null) { |
| 6216 | mSystemThread.installSystemProviders(providers); |
| 6217 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 6218 | |
| 6219 | mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 6220 | |
| 6221 | mSelf.mUsageStatsService.monitorPackages(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6222 | } |
| 6223 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6224 | /** |
| 6225 | * Allows app to retrieve the MIME type of a URI without having permission |
| 6226 | * to access its content provider. |
| 6227 | * |
| 6228 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 6229 | * |
| 6230 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 6231 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 6232 | */ |
| 6233 | public String getProviderMimeType(Uri uri) { |
| 6234 | final String name = uri.getAuthority(); |
| 6235 | final long ident = Binder.clearCallingIdentity(); |
| 6236 | ContentProviderHolder holder = null; |
| 6237 | |
| 6238 | try { |
| 6239 | holder = getContentProviderExternal(name); |
| 6240 | if (holder != null) { |
| 6241 | return holder.provider.getType(uri); |
| 6242 | } |
| 6243 | } catch (RemoteException e) { |
| 6244 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 6245 | return null; |
| 6246 | } finally { |
| 6247 | if (holder != null) { |
| 6248 | removeContentProviderExternal(name); |
| 6249 | } |
| 6250 | Binder.restoreCallingIdentity(ident); |
| 6251 | } |
| 6252 | |
| 6253 | return null; |
| 6254 | } |
| 6255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6256 | // ========================================================= |
| 6257 | // GLOBAL MANAGEMENT |
| 6258 | // ========================================================= |
| 6259 | |
| 6260 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
| 6261 | ApplicationInfo info, String customProcess) { |
| 6262 | String proc = customProcess != null ? customProcess : info.processName; |
| 6263 | BatteryStatsImpl.Uid.Proc ps = null; |
| 6264 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 6265 | synchronized (stats) { |
| 6266 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 6267 | } |
| 6268 | return new ProcessRecord(ps, thread, info, proc); |
| 6269 | } |
| 6270 | |
| 6271 | final ProcessRecord addAppLocked(ApplicationInfo info) { |
| 6272 | ProcessRecord app = getProcessRecordLocked(info.processName, info.uid); |
| 6273 | |
| 6274 | if (app == null) { |
| 6275 | app = newProcessRecordLocked(null, info, null); |
| 6276 | mProcessNames.put(info.processName, info.uid, app); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 6277 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6278 | } |
| 6279 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6280 | // This package really, really can not be stopped. |
| 6281 | try { |
| 6282 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 6283 | info.packageName, false); |
| 6284 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6285 | } catch (IllegalArgumentException e) { |
| 6286 | Slog.w(TAG, "Failed trying to unstop package " |
| 6287 | + info.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6288 | } |
| 6289 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6290 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 6291 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 6292 | app.persistent = true; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 6293 | app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6294 | } |
| 6295 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 6296 | mPersistentStartingProcesses.add(app); |
| 6297 | startProcessLocked(app, "added application", app.processName); |
| 6298 | } |
| 6299 | |
| 6300 | return app; |
| 6301 | } |
| 6302 | |
| 6303 | public void unhandledBack() { |
| 6304 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 6305 | "unhandledBack()"); |
| 6306 | |
| 6307 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6308 | int count = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6309 | if (DEBUG_SWITCH) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6310 | TAG, "Performing unhandledBack(): stack size = " + count); |
| 6311 | if (count > 1) { |
| 6312 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6313 | mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6314 | count-1, Activity.RESULT_CANCELED, null, "unhandled-back"); |
| 6315 | Binder.restoreCallingIdentity(origId); |
| 6316 | } |
| 6317 | } |
| 6318 | } |
| 6319 | |
| 6320 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
| 6321 | String name = uri.getAuthority(); |
| 6322 | ContentProviderHolder cph = getContentProviderExternal(name); |
| 6323 | ParcelFileDescriptor pfd = null; |
| 6324 | if (cph != null) { |
| 6325 | // We record the binder invoker's uid in thread-local storage before |
| 6326 | // going to the content provider to open the file. Later, in the code |
| 6327 | // that handles all permissions checks, we look for this uid and use |
| 6328 | // that rather than the Activity Manager's own uid. The effect is that |
| 6329 | // we do the check against the caller's permissions even though it looks |
| 6330 | // to the content provider like the Activity Manager itself is making |
| 6331 | // the request. |
| 6332 | sCallerIdentity.set(new Identity( |
| 6333 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 6334 | try { |
| 6335 | pfd = cph.provider.openFile(uri, "r"); |
| 6336 | } catch (FileNotFoundException e) { |
| 6337 | // do nothing; pfd will be returned null |
| 6338 | } finally { |
| 6339 | // Ensure that whatever happens, we clean up the identity state |
| 6340 | sCallerIdentity.remove(); |
| 6341 | } |
| 6342 | |
| 6343 | // We've got the fd now, so we're done with the provider. |
| 6344 | removeContentProviderExternal(name); |
| 6345 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6346 | 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] | 6347 | } |
| 6348 | return pfd; |
| 6349 | } |
| 6350 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6351 | // Actually is sleeping or shutting down or whatever else in the future |
| 6352 | // is an inactive state. |
| 6353 | public boolean isSleeping() { |
| 6354 | return mSleeping || mShuttingDown; |
| 6355 | } |
| 6356 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6357 | public void goingToSleep() { |
| 6358 | synchronized(this) { |
| 6359 | mSleeping = true; |
| 6360 | mWindowManager.setEventDispatching(false); |
| 6361 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6362 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6363 | |
| 6364 | // Initialize the wake times of all processes. |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 6365 | checkExcessivePowerUsageLocked(false); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6366 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 6367 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 6368 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6369 | } |
| 6370 | } |
| 6371 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6372 | public boolean shutdown(int timeout) { |
| 6373 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 6374 | != PackageManager.PERMISSION_GRANTED) { |
| 6375 | throw new SecurityException("Requires permission " |
| 6376 | + android.Manifest.permission.SHUTDOWN); |
| 6377 | } |
| 6378 | |
| 6379 | boolean timedout = false; |
| 6380 | |
| 6381 | synchronized(this) { |
| 6382 | mShuttingDown = true; |
| 6383 | mWindowManager.setEventDispatching(false); |
| 6384 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6385 | if (mMainStack.mResumedActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6386 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6387 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6388 | while (mMainStack.mResumedActivity != null |
| 6389 | || mMainStack.mPausingActivity != null) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6390 | long delay = endTime - System.currentTimeMillis(); |
| 6391 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6392 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6393 | timedout = true; |
| 6394 | break; |
| 6395 | } |
| 6396 | try { |
| 6397 | this.wait(); |
| 6398 | } catch (InterruptedException e) { |
| 6399 | } |
| 6400 | } |
| 6401 | } |
| 6402 | } |
| 6403 | |
| 6404 | mUsageStatsService.shutdown(); |
| 6405 | mBatteryStatsService.shutdown(); |
| 6406 | |
| 6407 | return timedout; |
| 6408 | } |
| 6409 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6410 | public final void activitySlept(IBinder token) { |
| 6411 | if (localLOGV) Slog.v( |
| 6412 | TAG, "Activity slept: token=" + token); |
| 6413 | |
| 6414 | ActivityRecord r = null; |
| 6415 | |
| 6416 | final long origId = Binder.clearCallingIdentity(); |
| 6417 | |
| 6418 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6419 | r = mMainStack.isInStackLocked(token); |
| 6420 | if (r != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6421 | mMainStack.activitySleptLocked(r); |
| 6422 | } |
| 6423 | } |
| 6424 | |
| 6425 | Binder.restoreCallingIdentity(origId); |
| 6426 | } |
| 6427 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6428 | public void wakingUp() { |
| 6429 | synchronized(this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6430 | mWindowManager.setEventDispatching(true); |
| 6431 | mSleeping = false; |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6432 | mMainStack.awakeFromSleepingLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6433 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6434 | } |
| 6435 | } |
| 6436 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6437 | public void stopAppSwitches() { |
| 6438 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6439 | != PackageManager.PERMISSION_GRANTED) { |
| 6440 | throw new SecurityException("Requires permission " |
| 6441 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6442 | } |
| 6443 | |
| 6444 | synchronized(this) { |
| 6445 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 6446 | + APP_SWITCH_DELAY_TIME; |
| 6447 | mDidAppSwitch = false; |
| 6448 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6449 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6450 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 6451 | } |
| 6452 | } |
| 6453 | |
| 6454 | public void resumeAppSwitches() { |
| 6455 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6456 | != PackageManager.PERMISSION_GRANTED) { |
| 6457 | throw new SecurityException("Requires permission " |
| 6458 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6459 | } |
| 6460 | |
| 6461 | synchronized(this) { |
| 6462 | // Note that we don't execute any pending app switches... we will |
| 6463 | // let those wait until either the timeout, or the next start |
| 6464 | // activity request. |
| 6465 | mAppSwitchesAllowedTime = 0; |
| 6466 | } |
| 6467 | } |
| 6468 | |
| 6469 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 6470 | String name) { |
| 6471 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 6472 | return true; |
| 6473 | } |
| 6474 | |
| 6475 | final int perm = checkComponentPermission( |
| 6476 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6477 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6478 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 6479 | return true; |
| 6480 | } |
| 6481 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6482 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6483 | return false; |
| 6484 | } |
| 6485 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6486 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 6487 | boolean persistent) { |
| 6488 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 6489 | "setDebugApp()"); |
| 6490 | |
| 6491 | // Note that this is not really thread safe if there are multiple |
| 6492 | // callers into it at the same time, but that's not a situation we |
| 6493 | // care about. |
| 6494 | if (persistent) { |
| 6495 | final ContentResolver resolver = mContext.getContentResolver(); |
| 6496 | Settings.System.putString( |
| 6497 | resolver, Settings.System.DEBUG_APP, |
| 6498 | packageName); |
| 6499 | Settings.System.putInt( |
| 6500 | resolver, Settings.System.WAIT_FOR_DEBUGGER, |
| 6501 | waitForDebugger ? 1 : 0); |
| 6502 | } |
| 6503 | |
| 6504 | synchronized (this) { |
| 6505 | if (!persistent) { |
| 6506 | mOrigDebugApp = mDebugApp; |
| 6507 | mOrigWaitForDebugger = mWaitForDebugger; |
| 6508 | } |
| 6509 | mDebugApp = packageName; |
| 6510 | mWaitForDebugger = waitForDebugger; |
| 6511 | mDebugTransient = !persistent; |
| 6512 | if (packageName != null) { |
| 6513 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 6514 | forceStopPackageLocked(packageName, -1, false, false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6515 | Binder.restoreCallingIdentity(origId); |
| 6516 | } |
| 6517 | } |
| 6518 | } |
| 6519 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 6520 | void setProfileApp(ApplicationInfo app, String processName, String profileFile, |
| 6521 | ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
| 6522 | synchronized (this) { |
| 6523 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 6524 | if (!isDebuggable) { |
| 6525 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 6526 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 6527 | } |
| 6528 | } |
| 6529 | mProfileApp = processName; |
| 6530 | mProfileFile = profileFile; |
| 6531 | if (mProfileFd != null) { |
| 6532 | try { |
| 6533 | mProfileFd.close(); |
| 6534 | } catch (IOException e) { |
| 6535 | } |
| 6536 | mProfileFd = null; |
| 6537 | } |
| 6538 | mProfileFd = profileFd; |
| 6539 | mProfileType = 0; |
| 6540 | mAutoStopProfiler = autoStopProfiler; |
| 6541 | } |
| 6542 | } |
| 6543 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6544 | public void setAlwaysFinish(boolean enabled) { |
| 6545 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 6546 | "setAlwaysFinish()"); |
| 6547 | |
| 6548 | Settings.System.putInt( |
| 6549 | mContext.getContentResolver(), |
| 6550 | Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
| 6551 | |
| 6552 | synchronized (this) { |
| 6553 | mAlwaysFinishActivities = enabled; |
| 6554 | } |
| 6555 | } |
| 6556 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6557 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6558 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6559 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6560 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6561 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6562 | } |
| 6563 | } |
| 6564 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 6565 | public boolean isUserAMonkey() { |
| 6566 | // For now the fact that there is a controller implies |
| 6567 | // we have a monkey. |
| 6568 | synchronized (this) { |
| 6569 | return mController != null; |
| 6570 | } |
| 6571 | } |
| 6572 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6573 | public void registerActivityWatcher(IActivityWatcher watcher) { |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6574 | synchronized (this) { |
| 6575 | mWatchers.register(watcher); |
| 6576 | } |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6577 | } |
| 6578 | |
| 6579 | public void unregisterActivityWatcher(IActivityWatcher watcher) { |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6580 | synchronized (this) { |
| 6581 | mWatchers.unregister(watcher); |
| 6582 | } |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6583 | } |
| 6584 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 6585 | public void registerProcessObserver(IProcessObserver observer) { |
| 6586 | mProcessObservers.register(observer); |
| 6587 | } |
| 6588 | |
| 6589 | public void unregisterProcessObserver(IProcessObserver observer) { |
| 6590 | mProcessObservers.unregister(observer); |
| 6591 | } |
| 6592 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6593 | public void setImmersive(IBinder token, boolean immersive) { |
| 6594 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6595 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 6596 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6597 | throw new IllegalArgumentException(); |
| 6598 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6599 | r.immersive = immersive; |
| 6600 | } |
| 6601 | } |
| 6602 | |
| 6603 | public boolean isImmersive(IBinder token) { |
| 6604 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6605 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 6606 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6607 | throw new IllegalArgumentException(); |
| 6608 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6609 | return r.immersive; |
| 6610 | } |
| 6611 | } |
| 6612 | |
| 6613 | public boolean isTopActivityImmersive() { |
| 6614 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6615 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6616 | return (r != null) ? r.immersive : false; |
| 6617 | } |
| 6618 | } |
| 6619 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6620 | public final void enterSafeMode() { |
| 6621 | synchronized(this) { |
| 6622 | // It only makes sense to do this before the system is ready |
| 6623 | // and started launching other packages. |
| 6624 | if (!mSystemReady) { |
| 6625 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6626 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6627 | } catch (RemoteException e) { |
| 6628 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6629 | } |
| 6630 | } |
| 6631 | } |
| 6632 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 6633 | public final void showSafeModeOverlay() { |
| 6634 | View v = LayoutInflater.from(mContext).inflate( |
| 6635 | com.android.internal.R.layout.safe_mode, null); |
| 6636 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 6637 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 6638 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6639 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6640 | lp.gravity = Gravity.BOTTOM | Gravity.LEFT; |
| 6641 | lp.format = v.getBackground().getOpacity(); |
| 6642 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 6643 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 6644 | ((WindowManager)mContext.getSystemService( |
| 6645 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 6646 | } |
| 6647 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6648 | public void noteWakeupAlarm(IIntentSender sender) { |
| 6649 | if (!(sender instanceof PendingIntentRecord)) { |
| 6650 | return; |
| 6651 | } |
| 6652 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 6653 | synchronized (stats) { |
| 6654 | if (mBatteryStatsService.isOnBattery()) { |
| 6655 | mBatteryStatsService.enforceCallingPermission(); |
| 6656 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 6657 | int MY_UID = Binder.getCallingUid(); |
| 6658 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 6659 | BatteryStatsImpl.Uid.Pkg pkg = |
| 6660 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 6661 | pkg.incWakeupsLocked(); |
| 6662 | } |
| 6663 | } |
| 6664 | } |
| 6665 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6666 | public boolean killPids(int[] pids, String pReason, boolean secure) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6667 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6668 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6669 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6670 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6671 | // XXX Note: don't acquire main activity lock here, because the window |
| 6672 | // manager calls in with its locks held. |
| 6673 | |
| 6674 | boolean killed = false; |
| 6675 | synchronized (mPidsSelfLocked) { |
| 6676 | int[] types = new int[pids.length]; |
| 6677 | int worstType = 0; |
| 6678 | for (int i=0; i<pids.length; i++) { |
| 6679 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 6680 | if (proc != null) { |
| 6681 | int type = proc.setAdj; |
| 6682 | types[i] = type; |
| 6683 | if (type > worstType) { |
| 6684 | worstType = type; |
| 6685 | } |
| 6686 | } |
| 6687 | } |
| 6688 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6689 | // If the worst oom_adj is somewhere in the hidden proc LRU range, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6690 | // then constrain it so we will kill all hidden procs. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 6691 | if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ |
| 6692 | && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 6693 | worstType = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6694 | } |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6695 | |
| 6696 | // If this is not a secure call, don't let it kill processes that |
| 6697 | // are important. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 6698 | if (!secure && worstType < ProcessList.SERVICE_ADJ) { |
| 6699 | worstType = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6700 | } |
| 6701 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6702 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6703 | for (int i=0; i<pids.length; i++) { |
| 6704 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 6705 | if (proc == null) { |
| 6706 | continue; |
| 6707 | } |
| 6708 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6709 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 6710 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6711 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid, |
| 6712 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6713 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6714 | proc.killedBackground = true; |
| 6715 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6716 | } |
| 6717 | } |
| 6718 | } |
| 6719 | return killed; |
| 6720 | } |
| 6721 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6722 | public final void startRunning(String pkg, String cls, String action, |
| 6723 | String data) { |
| 6724 | synchronized(this) { |
| 6725 | if (mStartRunning) { |
| 6726 | return; |
| 6727 | } |
| 6728 | mStartRunning = true; |
| 6729 | mTopComponent = pkg != null && cls != null |
| 6730 | ? new ComponentName(pkg, cls) : null; |
| 6731 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 6732 | mTopData = data; |
| 6733 | if (!mSystemReady) { |
| 6734 | return; |
| 6735 | } |
| 6736 | } |
| 6737 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6738 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6739 | } |
| 6740 | |
| 6741 | private void retrieveSettings() { |
| 6742 | final ContentResolver resolver = mContext.getContentResolver(); |
| 6743 | String debugApp = Settings.System.getString( |
| 6744 | resolver, Settings.System.DEBUG_APP); |
| 6745 | boolean waitForDebugger = Settings.System.getInt( |
| 6746 | resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0; |
| 6747 | boolean alwaysFinishActivities = Settings.System.getInt( |
| 6748 | resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
| 6749 | |
| 6750 | Configuration configuration = new Configuration(); |
| 6751 | Settings.System.getConfiguration(resolver, configuration); |
| 6752 | |
| 6753 | synchronized (this) { |
| 6754 | mDebugApp = mOrigDebugApp = debugApp; |
| 6755 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 6756 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 6757 | // This happens before any activities are started, so we can |
| 6758 | // change mConfiguration in-place. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 6759 | updateConfigurationLocked(configuration, null, false, true); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6760 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6761 | } |
| 6762 | } |
| 6763 | |
| 6764 | public boolean testIsSystemReady() { |
| 6765 | // no need to synchronize(this) just to read & return the value |
| 6766 | return mSystemReady; |
| 6767 | } |
| 6768 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6769 | private static File getCalledPreBootReceiversFile() { |
| 6770 | File dataDir = Environment.getDataDirectory(); |
| 6771 | File systemDir = new File(dataDir, "system"); |
| 6772 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 6773 | return fname; |
| 6774 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6775 | |
| 6776 | static final int LAST_DONE_VERSION = 10000; |
| 6777 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6778 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 6779 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 6780 | File file = getCalledPreBootReceiversFile(); |
| 6781 | FileInputStream fis = null; |
| 6782 | try { |
| 6783 | fis = new FileInputStream(file); |
| 6784 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6785 | int fvers = dis.readInt(); |
| 6786 | if (fvers == LAST_DONE_VERSION) { |
| 6787 | String vers = dis.readUTF(); |
| 6788 | String codename = dis.readUTF(); |
| 6789 | String build = dis.readUTF(); |
| 6790 | if (android.os.Build.VERSION.RELEASE.equals(vers) |
| 6791 | && android.os.Build.VERSION.CODENAME.equals(codename) |
| 6792 | && android.os.Build.VERSION.INCREMENTAL.equals(build)) { |
| 6793 | int num = dis.readInt(); |
| 6794 | while (num > 0) { |
| 6795 | num--; |
| 6796 | String pkg = dis.readUTF(); |
| 6797 | String cls = dis.readUTF(); |
| 6798 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 6799 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6800 | } |
| 6801 | } |
| 6802 | } catch (FileNotFoundException e) { |
| 6803 | } catch (IOException e) { |
| 6804 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 6805 | } finally { |
| 6806 | if (fis != null) { |
| 6807 | try { |
| 6808 | fis.close(); |
| 6809 | } catch (IOException e) { |
| 6810 | } |
| 6811 | } |
| 6812 | } |
| 6813 | return lastDoneReceivers; |
| 6814 | } |
| 6815 | |
| 6816 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 6817 | File file = getCalledPreBootReceiversFile(); |
| 6818 | FileOutputStream fos = null; |
| 6819 | DataOutputStream dos = null; |
| 6820 | try { |
| 6821 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 6822 | fos = new FileOutputStream(file); |
| 6823 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6824 | dos.writeInt(LAST_DONE_VERSION); |
| 6825 | dos.writeUTF(android.os.Build.VERSION.RELEASE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6826 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6827 | dos.writeUTF(android.os.Build.VERSION.INCREMENTAL); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6828 | dos.writeInt(list.size()); |
| 6829 | for (int i=0; i<list.size(); i++) { |
| 6830 | dos.writeUTF(list.get(i).getPackageName()); |
| 6831 | dos.writeUTF(list.get(i).getClassName()); |
| 6832 | } |
| 6833 | } catch (IOException e) { |
| 6834 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 6835 | file.delete(); |
| 6836 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 6837 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6838 | if (dos != null) { |
| 6839 | try { |
| 6840 | dos.close(); |
| 6841 | } catch (IOException e) { |
| 6842 | // TODO Auto-generated catch block |
| 6843 | e.printStackTrace(); |
| 6844 | } |
| 6845 | } |
| 6846 | } |
| 6847 | } |
| 6848 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6849 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6850 | synchronized(this) { |
| 6851 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6852 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6853 | return; |
| 6854 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6855 | |
| 6856 | // Check to see if there are any update receivers to run. |
| 6857 | if (!mDidUpdate) { |
| 6858 | if (mWaitingUpdate) { |
| 6859 | return; |
| 6860 | } |
| 6861 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 6862 | List<ResolveInfo> ris = null; |
| 6863 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6864 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6865 | intent, null, 0); |
| 6866 | } catch (RemoteException e) { |
| 6867 | } |
| 6868 | if (ris != null) { |
| 6869 | for (int i=ris.size()-1; i>=0; i--) { |
| 6870 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 6871 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6872 | ris.remove(i); |
| 6873 | } |
| 6874 | } |
| 6875 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6876 | |
| 6877 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
| 6878 | |
| 6879 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6880 | for (int i=0; i<ris.size(); i++) { |
| 6881 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6882 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 6883 | if (lastDoneReceivers.contains(comp)) { |
| 6884 | ris.remove(i); |
| 6885 | i--; |
| 6886 | } |
| 6887 | } |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 6888 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6889 | for (int i=0; i<ris.size(); i++) { |
| 6890 | ActivityInfo ai = ris.get(i).activityInfo; |
| 6891 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 6892 | doneReceivers.add(comp); |
| 6893 | intent.setComponent(comp); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6894 | IIntentReceiver finisher = null; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 6895 | if (i == ris.size()-1) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6896 | finisher = new IIntentReceiver.Stub() { |
| 6897 | public void performReceive(Intent intent, int resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 6898 | String data, Bundle extras, boolean ordered, |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 6899 | boolean sticky) { |
| 6900 | // The raw IIntentReceiver interface is called |
| 6901 | // with the AM lock held, so redispatch to |
| 6902 | // execute our code without the lock. |
| 6903 | mHandler.post(new Runnable() { |
| 6904 | public void run() { |
| 6905 | synchronized (ActivityManagerService.this) { |
| 6906 | mDidUpdate = true; |
| 6907 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6908 | writeLastDonePreBootReceivers(doneReceivers); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6909 | showBootMessage(mContext.getText( |
| 6910 | R.string.android_upgrading_complete), |
| 6911 | false); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 6912 | systemReady(goingCallback); |
| 6913 | } |
| 6914 | }); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6915 | } |
| 6916 | }; |
| 6917 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6918 | Slog.i(TAG, "Sending system update to: " + intent.getComponent()); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6919 | broadcastIntentLocked(null, null, intent, null, finisher, |
| 6920 | 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 6921 | if (finisher != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6922 | mWaitingUpdate = true; |
| 6923 | } |
| 6924 | } |
| 6925 | } |
| 6926 | if (mWaitingUpdate) { |
| 6927 | return; |
| 6928 | } |
| 6929 | mDidUpdate = true; |
| 6930 | } |
| 6931 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6932 | mSystemReady = true; |
| 6933 | if (!mStartRunning) { |
| 6934 | return; |
| 6935 | } |
| 6936 | } |
| 6937 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6938 | ArrayList<ProcessRecord> procsToKill = null; |
| 6939 | synchronized(mPidsSelfLocked) { |
| 6940 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 6941 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 6942 | if (!isAllowedWhileBooting(proc.info)){ |
| 6943 | if (procsToKill == null) { |
| 6944 | procsToKill = new ArrayList<ProcessRecord>(); |
| 6945 | } |
| 6946 | procsToKill.add(proc); |
| 6947 | } |
| 6948 | } |
| 6949 | } |
| 6950 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6951 | synchronized(this) { |
| 6952 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6953 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 6954 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6955 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 6956 | removeProcessLocked(proc, true, false, "system update done"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6957 | } |
| 6958 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6959 | |
| 6960 | // Now that we have cleaned up any update processes, we |
| 6961 | // are ready to start launching real processes and know that |
| 6962 | // we won't trample on them any more. |
| 6963 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6964 | } |
| 6965 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6966 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6967 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6968 | SystemClock.uptimeMillis()); |
| 6969 | |
| 6970 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6971 | // Make sure we have no pre-ready processes sitting around. |
| 6972 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6973 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 6974 | ResolveInfo ri = mContext.getPackageManager() |
| 6975 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6976 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6977 | CharSequence errorMsg = null; |
| 6978 | if (ri != null) { |
| 6979 | ActivityInfo ai = ri.activityInfo; |
| 6980 | ApplicationInfo app = ai.applicationInfo; |
| 6981 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 6982 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 6983 | mTopData = null; |
| 6984 | mTopComponent = new ComponentName(app.packageName, |
| 6985 | ai.name); |
| 6986 | } else { |
| 6987 | errorMsg = mContext.getResources().getText( |
| 6988 | com.android.internal.R.string.factorytest_not_system); |
| 6989 | } |
| 6990 | } else { |
| 6991 | errorMsg = mContext.getResources().getText( |
| 6992 | com.android.internal.R.string.factorytest_no_action); |
| 6993 | } |
| 6994 | if (errorMsg != null) { |
| 6995 | mTopAction = null; |
| 6996 | mTopData = null; |
| 6997 | mTopComponent = null; |
| 6998 | Message msg = Message.obtain(); |
| 6999 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 7000 | msg.getData().putCharSequence("msg", errorMsg); |
| 7001 | mHandler.sendMessage(msg); |
| 7002 | } |
| 7003 | } |
| 7004 | } |
| 7005 | |
| 7006 | retrieveSettings(); |
| 7007 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7008 | if (goingCallback != null) goingCallback.run(); |
| 7009 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7010 | synchronized (this) { |
| 7011 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7012 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7013 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7014 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7015 | if (apps != null) { |
| 7016 | int N = apps.size(); |
| 7017 | int i; |
| 7018 | for (i=0; i<N; i++) { |
| 7019 | ApplicationInfo info |
| 7020 | = (ApplicationInfo)apps.get(i); |
| 7021 | if (info != null && |
| 7022 | !info.packageName.equals("android")) { |
| 7023 | addAppLocked(info); |
| 7024 | } |
| 7025 | } |
| 7026 | } |
| 7027 | } catch (RemoteException ex) { |
| 7028 | // pm is in same process, this will never happen. |
| 7029 | } |
| 7030 | } |
| 7031 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7032 | // Start up initial activity. |
| 7033 | mBooting = true; |
| 7034 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7035 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7036 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7037 | Message msg = Message.obtain(); |
| 7038 | msg.what = SHOW_UID_ERROR_MSG; |
| 7039 | mHandler.sendMessage(msg); |
| 7040 | } |
| 7041 | } catch (RemoteException e) { |
| 7042 | } |
| 7043 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7044 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7045 | } |
| 7046 | } |
| 7047 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7048 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7049 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7050 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7051 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7052 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7053 | startAppProblemLocked(app); |
| 7054 | app.stopFreezingAllLocked(); |
| 7055 | return handleAppCrashLocked(app); |
| 7056 | } |
| 7057 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7058 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7059 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7060 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7061 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7062 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 7063 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7064 | startAppProblemLocked(app); |
| 7065 | app.stopFreezingAllLocked(); |
| 7066 | } |
| 7067 | |
| 7068 | /** |
| 7069 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 7070 | * |
| 7071 | * @param app The ProcessRecord in which the error occurred. |
| 7072 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 7073 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7074 | * @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] | 7075 | * @param shortMsg Short message describing the crash. |
| 7076 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7077 | * @param stackTrace Full crash stack trace, may be null. |
| 7078 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7079 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 7080 | */ |
| 7081 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7082 | 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] | 7083 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7084 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7085 | report.condition = condition; |
| 7086 | report.processName = app.processName; |
| 7087 | report.pid = app.pid; |
| 7088 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7089 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7090 | report.shortMsg = shortMsg; |
| 7091 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7092 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7093 | |
| 7094 | return report; |
| 7095 | } |
| 7096 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7097 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7098 | synchronized (this) { |
| 7099 | app.crashing = false; |
| 7100 | app.crashingReport = null; |
| 7101 | app.notResponding = false; |
| 7102 | app.notRespondingReport = null; |
| 7103 | if (app.anrDialog == fromDialog) { |
| 7104 | app.anrDialog = null; |
| 7105 | } |
| 7106 | if (app.waitDialog == fromDialog) { |
| 7107 | app.waitDialog = null; |
| 7108 | } |
| 7109 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7110 | handleAppCrashLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7111 | Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request"); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7112 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 7113 | app.processName, app.setAdj, "user's request after error"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7114 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7115 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7116 | } |
| 7117 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7118 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7119 | private boolean handleAppCrashLocked(ProcessRecord app) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7120 | long now = SystemClock.uptimeMillis(); |
| 7121 | |
| 7122 | Long crashTime = mProcessCrashTimes.get(app.info.processName, |
| 7123 | app.info.uid); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7124 | if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7125 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7126 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7127 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7128 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7129 | app.info.processName, app.info.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7130 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 7131 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7132 | if (r.app == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7133 | Slog.w(TAG, " Force finishing activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7134 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7135 | 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] | 7136 | } |
| 7137 | } |
| 7138 | if (!app.persistent) { |
| 7139 | // We don't want to start this process again until the user |
| 7140 | // explicitly does so... but for persistent process, we really |
| 7141 | // need to keep it running. If a persistent process is actually |
| 7142 | // repeatedly crashing, then badness for everyone. |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7143 | EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.info.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7144 | app.info.processName); |
| 7145 | mBadProcesses.put(app.info.processName, app.info.uid, now); |
| 7146 | app.bad = true; |
| 7147 | mProcessCrashTimes.remove(app.info.processName, app.info.uid); |
| 7148 | app.removed = true; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 7149 | // Don't let services in this process be restarted and potentially |
| 7150 | // annoy the user repeatedly. Unless it is persistent, since those |
| 7151 | // processes run critical code. |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7152 | removeProcessLocked(app, false, false, "crash"); |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7153 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7154 | return false; |
| 7155 | } |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7156 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7157 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7158 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7159 | if (r.app == app) { |
| 7160 | // If the top running activity is from this crashing |
| 7161 | // process, then terminate it to avoid getting in a loop. |
| 7162 | Slog.w(TAG, " Force finishing activity " |
| 7163 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 7164 | int index = mMainStack.indexOfActivityLocked(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7165 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7166 | Activity.RESULT_CANCELED, null, "crashed"); |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 7167 | // Also terminate any activities below it that aren't yet |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7168 | // stopped, to avoid a situation where one will get |
| 7169 | // re-start our crashing activity once it gets resumed again. |
| 7170 | index--; |
| 7171 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7172 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7173 | if (r.state == ActivityState.RESUMED |
| 7174 | || r.state == ActivityState.PAUSING |
| 7175 | || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 7176 | if (!r.isHomeActivity || mHomeProcess != r.app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7177 | Slog.w(TAG, " Force finishing activity " |
| 7178 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7179 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7180 | Activity.RESULT_CANCELED, null, "crashed"); |
| 7181 | } |
| 7182 | } |
| 7183 | } |
| 7184 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7185 | } |
| 7186 | |
| 7187 | // Bump up the crash count of any services currently running in the proc. |
| 7188 | if (app.services.size() != 0) { |
| 7189 | // Any services running in the application need to be placed |
| 7190 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7191 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7192 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7193 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7194 | sr.crashCount++; |
| 7195 | } |
| 7196 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7197 | |
| 7198 | // If the crashing process is what we consider to be the "home process" and it has been |
| 7199 | // replaced by a third-party app, clear the package preferred activities from packages |
| 7200 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 7201 | // from blocking the user to manually clear the list. |
| 7202 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 7203 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7204 | Iterator it = mHomeProcess.activities.iterator(); |
| 7205 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 7206 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7207 | if (r.isHomeActivity) { |
| 7208 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 7209 | try { |
| 7210 | ActivityThread.getPackageManager() |
| 7211 | .clearPackagePreferredActivities(r.packageName); |
| 7212 | } catch (RemoteException c) { |
| 7213 | // pm is in same process, this will never happen. |
| 7214 | } |
| 7215 | } |
| 7216 | } |
| 7217 | } |
| 7218 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7219 | mProcessCrashTimes.put(app.info.processName, app.info.uid, now); |
| 7220 | return true; |
| 7221 | } |
| 7222 | |
| 7223 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 7224 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 7225 | mContext, app.info.packageName, app.info.flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7226 | skipCurrentReceiverLocked(app); |
| 7227 | } |
| 7228 | |
| 7229 | void skipCurrentReceiverLocked(ProcessRecord app) { |
| 7230 | boolean reschedule = false; |
| 7231 | BroadcastRecord r = app.curReceiver; |
| 7232 | if (r != null) { |
| 7233 | // The current broadcast is waiting for this app's receiver |
| 7234 | // to be finished. Looks like that's not going to happen, so |
| 7235 | // let the broadcast continue. |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 7236 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7237 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 7238 | r.resultExtras, r.resultAbort, true); |
| 7239 | reschedule = true; |
| 7240 | } |
| 7241 | r = mPendingBroadcast; |
| 7242 | if (r != null && r.curApp == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7243 | if (DEBUG_BROADCAST) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7244 | "skip & discard pending app " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 7245 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7246 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 7247 | r.resultExtras, r.resultAbort, true); |
| 7248 | reschedule = true; |
| 7249 | } |
| 7250 | if (reschedule) { |
| 7251 | scheduleBroadcastsLocked(); |
| 7252 | } |
| 7253 | } |
| 7254 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7255 | /** |
| 7256 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 7257 | * The application process will exit immediately after this call returns. |
| 7258 | * @param app object of the crashing app, null for the system server |
| 7259 | * @param crashInfo describing the exception |
| 7260 | */ |
| 7261 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7262 | ProcessRecord r = findAppProcess(app, "Crash"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7263 | final String processName = app == null ? "system_server" |
| 7264 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7265 | |
| 7266 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7267 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7268 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7269 | crashInfo.exceptionClassName, |
| 7270 | crashInfo.exceptionMessage, |
| 7271 | crashInfo.throwFileName, |
| 7272 | crashInfo.throwLineNumber); |
| 7273 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7274 | addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7275 | |
| 7276 | crashApplication(r, crashInfo); |
| 7277 | } |
| 7278 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7279 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7280 | IBinder app, |
| 7281 | int violationMask, |
| 7282 | StrictMode.ViolationInfo info) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7283 | ProcessRecord r = findAppProcess(app, "StrictMode"); |
| 7284 | if (r == null) { |
| 7285 | return; |
| 7286 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7287 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7288 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 7289 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7290 | boolean logIt = true; |
| 7291 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 7292 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 7293 | logIt = false; |
| 7294 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7295 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7296 | // the relative pain numbers, without logging all |
| 7297 | // the stack traces repeatedly. We'd want to do |
| 7298 | // likewise in the client code, which also does |
| 7299 | // dup suppression, before the Binder call. |
| 7300 | } else { |
| 7301 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 7302 | mAlreadyLoggedViolatedStacks.clear(); |
| 7303 | } |
| 7304 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 7305 | } |
| 7306 | } |
| 7307 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7308 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7309 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7310 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7311 | |
| 7312 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 7313 | AppErrorResult result = new AppErrorResult(); |
| 7314 | synchronized (this) { |
| 7315 | final long origId = Binder.clearCallingIdentity(); |
| 7316 | |
| 7317 | Message msg = Message.obtain(); |
| 7318 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 7319 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 7320 | data.put("result", result); |
| 7321 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7322 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7323 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7324 | msg.obj = data; |
| 7325 | mHandler.sendMessage(msg); |
| 7326 | |
| 7327 | Binder.restoreCallingIdentity(origId); |
| 7328 | } |
| 7329 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 7330 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7331 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7332 | } |
| 7333 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7334 | // Depending on the policy in effect, there could be a bunch of |
| 7335 | // these in quick succession so we try to batch these together to |
| 7336 | // minimize disk writes, number of dropbox entries, and maximize |
| 7337 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7338 | private void logStrictModeViolationToDropBox( |
| 7339 | ProcessRecord process, |
| 7340 | StrictMode.ViolationInfo info) { |
| 7341 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7342 | return; |
| 7343 | } |
| 7344 | final boolean isSystemApp = process == null || |
| 7345 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 7346 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7347 | final String processName = process == null ? "unknown" : process.processName; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7348 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 7349 | final DropBoxManager dbox = (DropBoxManager) |
| 7350 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7351 | |
| 7352 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7353 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7354 | |
| 7355 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7356 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7357 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 7358 | synchronized (sb) { |
| 7359 | bufferWasEmpty = sb.length() == 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7360 | appendDropBoxProcessHeaders(process, processName, sb); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7361 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 7362 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7363 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 7364 | if (info.violationNumThisLoop != 0) { |
| 7365 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 7366 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 7367 | if (info.numAnimationsRunning != 0) { |
| 7368 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 7369 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 7370 | if (info.broadcastIntentAction != null) { |
| 7371 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 7372 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7373 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7374 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7375 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 7376 | if (info.numInstances != -1) { |
| 7377 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 7378 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7379 | if (info.tags != null) { |
| 7380 | for (String tag : info.tags) { |
| 7381 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 7382 | } |
| 7383 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7384 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7385 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 7386 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7387 | } |
| 7388 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7389 | |
| 7390 | // Only buffer up to ~64k. Various logging bits truncate |
| 7391 | // things at 128k. |
| 7392 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7393 | } |
| 7394 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7395 | // Flush immediately if the buffer's grown too large, or this |
| 7396 | // is a non-system app. Non-system apps are isolated with a |
| 7397 | // different tag & policy and not batched. |
| 7398 | // |
| 7399 | // Batching is useful during internal testing with |
| 7400 | // StrictMode settings turned up high. Without batching, |
| 7401 | // thousands of separate files could be created on boot. |
| 7402 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7403 | new Thread("Error dump: " + dropboxTag) { |
| 7404 | @Override |
| 7405 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7406 | String report; |
| 7407 | synchronized (sb) { |
| 7408 | report = sb.toString(); |
| 7409 | sb.delete(0, sb.length()); |
| 7410 | sb.trimToSize(); |
| 7411 | } |
| 7412 | if (report.length() != 0) { |
| 7413 | dbox.addText(dropboxTag, report); |
| 7414 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7415 | } |
| 7416 | }.start(); |
| 7417 | return; |
| 7418 | } |
| 7419 | |
| 7420 | // System app batching: |
| 7421 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7422 | // An existing dropbox-writing thread is outstanding, so |
| 7423 | // we don't need to start it up. The existing thread will |
| 7424 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7425 | return; |
| 7426 | } |
| 7427 | |
| 7428 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 7429 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7430 | new Thread("Error dump: " + dropboxTag) { |
| 7431 | @Override |
| 7432 | public void run() { |
| 7433 | // 5 second sleep to let stacks arrive and be batched together |
| 7434 | try { |
| 7435 | Thread.sleep(5000); // 5 seconds |
| 7436 | } catch (InterruptedException e) {} |
| 7437 | |
| 7438 | String errorReport; |
| 7439 | synchronized (mStrictModeBuffer) { |
| 7440 | errorReport = mStrictModeBuffer.toString(); |
| 7441 | if (errorReport.length() == 0) { |
| 7442 | return; |
| 7443 | } |
| 7444 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 7445 | mStrictModeBuffer.trimToSize(); |
| 7446 | } |
| 7447 | dbox.addText(dropboxTag, errorReport); |
| 7448 | } |
| 7449 | }.start(); |
| 7450 | } |
| 7451 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7452 | /** |
| 7453 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 7454 | * @param app object of the crashing app, null for the system server |
| 7455 | * @param tag reported by the caller |
| 7456 | * @param crashInfo describing the context of the error |
| 7457 | * @return true if the process should exit immediately (WTF is fatal) |
| 7458 | */ |
| 7459 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7460 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7461 | ProcessRecord r = findAppProcess(app, "WTF"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7462 | final String processName = app == null ? "system_server" |
| 7463 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7464 | |
| 7465 | EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7466 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7467 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7468 | tag, crashInfo.exceptionMessage); |
| 7469 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7470 | addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7471 | |
Dianne Hackborn | 1ab4377 | 2011-03-15 14:38:02 -0700 | [diff] [blame] | 7472 | if (r != null && r.pid != Process.myPid() && |
| 7473 | Settings.Secure.getInt(mContext.getContentResolver(), |
| 7474 | Settings.Secure.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7475 | crashApplication(r, crashInfo); |
| 7476 | return true; |
| 7477 | } else { |
| 7478 | return false; |
| 7479 | } |
| 7480 | } |
| 7481 | |
| 7482 | /** |
| 7483 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 7484 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 7485 | */ |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7486 | private ProcessRecord findAppProcess(IBinder app, String reason) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7487 | if (app == null) { |
| 7488 | return null; |
| 7489 | } |
| 7490 | |
| 7491 | synchronized (this) { |
| 7492 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 7493 | final int NA = apps.size(); |
| 7494 | for (int ia=0; ia<NA; ia++) { |
| 7495 | ProcessRecord p = apps.valueAt(ia); |
| 7496 | if (p.thread != null && p.thread.asBinder() == app) { |
| 7497 | return p; |
| 7498 | } |
| 7499 | } |
| 7500 | } |
| 7501 | |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7502 | Slog.w(TAG, "Can't find mystery application for " + reason |
| 7503 | + " from pid=" + Binder.getCallingPid() |
| 7504 | + " uid=" + Binder.getCallingUid() + ": " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7505 | return null; |
| 7506 | } |
| 7507 | } |
| 7508 | |
| 7509 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7510 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 7511 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7512 | */ |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7513 | private void appendDropBoxProcessHeaders(ProcessRecord process, String processName, |
| 7514 | StringBuilder sb) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 7515 | // Watchdog thread ends up invoking this function (with |
| 7516 | // a null ProcessRecord) to add the stack file to dropbox. |
| 7517 | // Do not acquire a lock on this (am) in such cases, as it |
| 7518 | // could cause a potential deadlock, if and when watchdog |
| 7519 | // is invoked due to unavailability of lock on am and it |
| 7520 | // would prevent watchdog from killing system_server. |
| 7521 | if (process == null) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7522 | sb.append("Process: ").append(processName).append("\n"); |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 7523 | return; |
| 7524 | } |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 7525 | // Note: ProcessRecord 'process' is guarded by the service |
| 7526 | // instance. (notably process.pkgList, which could otherwise change |
| 7527 | // concurrently during execution of this method) |
| 7528 | synchronized (this) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7529 | sb.append("Process: ").append(processName).append("\n"); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7530 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7531 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7532 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 7533 | for (String pkg : process.pkgList) { |
| 7534 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7535 | try { |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7536 | PackageInfo pi = pm.getPackageInfo(pkg, 0); |
| 7537 | if (pi != null) { |
| 7538 | sb.append(" v").append(pi.versionCode); |
| 7539 | if (pi.versionName != null) { |
| 7540 | sb.append(" (").append(pi.versionName).append(")"); |
| 7541 | } |
| 7542 | } |
| 7543 | } catch (RemoteException e) { |
| 7544 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7545 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7546 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7547 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7548 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7549 | } |
| 7550 | |
| 7551 | private static String processClass(ProcessRecord process) { |
| 7552 | if (process == null || process.pid == MY_PID) { |
| 7553 | return "system_server"; |
| 7554 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7555 | return "system_app"; |
| 7556 | } else { |
| 7557 | return "data_app"; |
| 7558 | } |
| 7559 | } |
| 7560 | |
| 7561 | /** |
| 7562 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 7563 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 7564 | * @param process which caused the error, null means the system server |
| 7565 | * @param activity which triggered the error, null if unknown |
| 7566 | * @param parent activity related to the error, null if unknown |
| 7567 | * @param subject line related to the error, null if absent |
| 7568 | * @param report in long form describing the error, null if absent |
| 7569 | * @param logFile to include in the report, null if none |
| 7570 | * @param crashInfo giving an application stack trace, null if absent |
| 7571 | */ |
| 7572 | public void addErrorToDropBox(String eventType, |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7573 | ProcessRecord process, String processName, ActivityRecord activity, |
| 7574 | ActivityRecord parent, String subject, |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7575 | final String report, final File logFile, |
| 7576 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 7577 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 7578 | // otherwise the watchdog may be prevented from resetting the system. |
| 7579 | |
| 7580 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 7581 | final DropBoxManager dbox = (DropBoxManager) |
| 7582 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7583 | |
| 7584 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7585 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7586 | |
| 7587 | final StringBuilder sb = new StringBuilder(1024); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7588 | appendDropBoxProcessHeaders(process, processName, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7589 | if (activity != null) { |
| 7590 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 7591 | } |
| 7592 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 7593 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 7594 | } |
| 7595 | if (parent != null && parent != activity) { |
| 7596 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 7597 | } |
| 7598 | if (subject != null) { |
| 7599 | sb.append("Subject: ").append(subject).append("\n"); |
| 7600 | } |
| 7601 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 7602 | if (Debug.isDebuggerConnected()) { |
| 7603 | sb.append("Debugger: Connected\n"); |
| 7604 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7605 | sb.append("\n"); |
| 7606 | |
| 7607 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 7608 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7609 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 7610 | @Override |
| 7611 | public void run() { |
| 7612 | if (report != null) { |
| 7613 | sb.append(report); |
| 7614 | } |
| 7615 | if (logFile != null) { |
| 7616 | try { |
| 7617 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 7618 | } catch (IOException e) { |
| 7619 | Slog.e(TAG, "Error reading " + logFile, e); |
| 7620 | } |
| 7621 | } |
| 7622 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 7623 | sb.append(crashInfo.stackTrace); |
| 7624 | } |
| 7625 | |
| 7626 | String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 7627 | int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0); |
| 7628 | if (lines > 0) { |
| 7629 | sb.append("\n"); |
| 7630 | |
| 7631 | // Merge several logcat streams, and take the last N lines |
| 7632 | InputStreamReader input = null; |
| 7633 | try { |
| 7634 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 7635 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 7636 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 7637 | |
| 7638 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 7639 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 7640 | input = new InputStreamReader(logcat.getInputStream()); |
| 7641 | |
| 7642 | int num; |
| 7643 | char[] buf = new char[8192]; |
| 7644 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 7645 | } catch (IOException e) { |
| 7646 | Slog.e(TAG, "Error running logcat", e); |
| 7647 | } finally { |
| 7648 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 7649 | } |
| 7650 | } |
| 7651 | |
| 7652 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7653 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7654 | }; |
| 7655 | |
| 7656 | if (process == null || process.pid == MY_PID) { |
| 7657 | worker.run(); // We may be about to die -- need to run this synchronously |
| 7658 | } else { |
| 7659 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7660 | } |
| 7661 | } |
| 7662 | |
| 7663 | /** |
| 7664 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 7665 | * Deal with edge cases (intercepts from instrumented applications, |
| 7666 | * ActivityController, error intent receivers, that sort of thing). |
| 7667 | * @param r the application crashing |
| 7668 | * @param crashInfo describing the failure |
| 7669 | */ |
| 7670 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7671 | long timeMillis = System.currentTimeMillis(); |
| 7672 | String shortMsg = crashInfo.exceptionClassName; |
| 7673 | String longMsg = crashInfo.exceptionMessage; |
| 7674 | String stackTrace = crashInfo.stackTrace; |
| 7675 | if (shortMsg != null && longMsg != null) { |
| 7676 | longMsg = shortMsg + ": " + longMsg; |
| 7677 | } else if (shortMsg != null) { |
| 7678 | longMsg = shortMsg; |
| 7679 | } |
| 7680 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7681 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7682 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7683 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7684 | try { |
| 7685 | String name = r != null ? r.processName : null; |
| 7686 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7687 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7688 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7689 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7690 | + " at watcher's request"); |
| 7691 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7692 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7693 | } |
| 7694 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7695 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7696 | } |
| 7697 | } |
| 7698 | |
| 7699 | final long origId = Binder.clearCallingIdentity(); |
| 7700 | |
| 7701 | // If this process is running instrumentation, finish it. |
| 7702 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7703 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7704 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7705 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 7706 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7707 | Bundle info = new Bundle(); |
| 7708 | info.putString("shortMsg", shortMsg); |
| 7709 | info.putString("longMsg", longMsg); |
| 7710 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 7711 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7712 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7713 | } |
| 7714 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7715 | // If we can't identify the process or it's already exceeded its crash quota, |
| 7716 | // quit right away without showing a crash dialog. |
| 7717 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7718 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7719 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7720 | } |
| 7721 | |
| 7722 | Message msg = Message.obtain(); |
| 7723 | msg.what = SHOW_ERROR_MSG; |
| 7724 | HashMap data = new HashMap(); |
| 7725 | data.put("result", result); |
| 7726 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7727 | msg.obj = data; |
| 7728 | mHandler.sendMessage(msg); |
| 7729 | |
| 7730 | Binder.restoreCallingIdentity(origId); |
| 7731 | } |
| 7732 | |
| 7733 | int res = result.get(); |
| 7734 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7735 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7736 | synchronized (this) { |
| 7737 | if (r != null) { |
| 7738 | mProcessCrashTimes.put(r.info.processName, r.info.uid, |
| 7739 | SystemClock.uptimeMillis()); |
| 7740 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7741 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7742 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7743 | } |
| 7744 | } |
| 7745 | |
| 7746 | if (appErrorIntent != null) { |
| 7747 | try { |
| 7748 | mContext.startActivity(appErrorIntent); |
| 7749 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7750 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7751 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7752 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7753 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7754 | |
| 7755 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 7756 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 7757 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7758 | if (report == null) { |
| 7759 | return null; |
| 7760 | } |
| 7761 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 7762 | result.setComponent(r.errorReportReceiver); |
| 7763 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 7764 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 7765 | return result; |
| 7766 | } |
| 7767 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7768 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 7769 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7770 | if (r.errorReportReceiver == null) { |
| 7771 | return null; |
| 7772 | } |
| 7773 | |
| 7774 | if (!r.crashing && !r.notResponding) { |
| 7775 | return null; |
| 7776 | } |
| 7777 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7778 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 7779 | report.packageName = r.info.packageName; |
| 7780 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 7781 | report.processName = r.processName; |
| 7782 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 7783 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7784 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7785 | if (r.crashing) { |
| 7786 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 7787 | report.crashInfo = crashInfo; |
| 7788 | } else if (r.notResponding) { |
| 7789 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 7790 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7791 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7792 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 7793 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 7794 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7795 | } |
| 7796 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7797 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7798 | } |
| 7799 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7800 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
| 7801 | // assume our apps are happy - lazy create the list |
| 7802 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 7803 | |
| 7804 | synchronized (this) { |
| 7805 | |
| 7806 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7807 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 7808 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7809 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 7810 | // This one's in trouble, so we'll generate a report for it |
| 7811 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 7812 | ActivityManager.ProcessErrorStateInfo report = null; |
| 7813 | if (app.crashing) { |
| 7814 | report = app.crashingReport; |
| 7815 | } else if (app.notResponding) { |
| 7816 | report = app.notRespondingReport; |
| 7817 | } |
| 7818 | |
| 7819 | if (report != null) { |
| 7820 | if (errList == null) { |
| 7821 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 7822 | } |
| 7823 | errList.add(report); |
| 7824 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7825 | 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] | 7826 | " crashing = " + app.crashing + |
| 7827 | " notResponding = " + app.notResponding); |
| 7828 | } |
| 7829 | } |
| 7830 | } |
| 7831 | } |
| 7832 | |
| 7833 | return errList; |
| 7834 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7835 | |
| 7836 | static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) { |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7837 | if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7838 | if (currApp != null) { |
| 7839 | currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
| 7840 | } |
| 7841 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7842 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 7843 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7844 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 7845 | if (currApp != null) { |
| 7846 | currApp.lru = 0; |
| 7847 | } |
| 7848 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7849 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7850 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 7851 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 7852 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 7853 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 7854 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 7855 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 7856 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 7857 | } else { |
| 7858 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 7859 | } |
| 7860 | } |
| 7861 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7862 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
| 7863 | // Lazy instantiation of list |
| 7864 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
| 7865 | synchronized (this) { |
| 7866 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7867 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 7868 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7869 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 7870 | // Generate process state info for running application |
| 7871 | ActivityManager.RunningAppProcessInfo currApp = |
| 7872 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 7873 | app.pid, app.getPackageList()); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 7874 | currApp.uid = app.info.uid; |
Dianne Hackborn | baf42c6 | 2010-06-24 11:23:39 -0700 | [diff] [blame] | 7875 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 482566e | 2010-09-03 12:51:28 -0700 | [diff] [blame] | 7876 | currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
Dianne Hackborn | baf42c6 | 2010-06-24 11:23:39 -0700 | [diff] [blame] | 7877 | } |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 7878 | if (app.persistent) { |
| 7879 | currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 7880 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7881 | int adj = app.curAdj; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7882 | currApp.importance = oomAdjToImportance(adj, currApp); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 7883 | currApp.importanceReasonCode = app.adjTypeCode; |
| 7884 | if (app.adjSource instanceof ProcessRecord) { |
| 7885 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7886 | currApp.importanceReasonImportance = oomAdjToImportance( |
| 7887 | app.adjSourceOom, null); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7888 | } else if (app.adjSource instanceof ActivityRecord) { |
| 7889 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 7890 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 7891 | } |
| 7892 | if (app.adjTarget instanceof ComponentName) { |
| 7893 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 7894 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7895 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7896 | // + " lru=" + currApp.lru); |
| 7897 | if (runList == null) { |
| 7898 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 7899 | } |
| 7900 | runList.add(currApp); |
| 7901 | } |
| 7902 | } |
| 7903 | } |
| 7904 | return runList; |
| 7905 | } |
| 7906 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7907 | public List<ApplicationInfo> getRunningExternalApplications() { |
| 7908 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 7909 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 7910 | if (runningApps != null && runningApps.size() > 0) { |
| 7911 | Set<String> extList = new HashSet<String>(); |
| 7912 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 7913 | if (app.pkgList != null) { |
| 7914 | for (String pkg : app.pkgList) { |
| 7915 | extList.add(pkg); |
| 7916 | } |
| 7917 | } |
| 7918 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7919 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7920 | for (String pkg : extList) { |
| 7921 | try { |
| 7922 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0); |
| 7923 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 7924 | retList.add(info); |
| 7925 | } |
| 7926 | } catch (RemoteException e) { |
| 7927 | } |
| 7928 | } |
| 7929 | } |
| 7930 | return retList; |
| 7931 | } |
| 7932 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7933 | @Override |
| 7934 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7935 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 7936 | != PackageManager.PERMISSION_GRANTED) { |
| 7937 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 7938 | + Binder.getCallingPid() |
| 7939 | + ", uid=" + Binder.getCallingUid() |
| 7940 | + " without permission " |
| 7941 | + android.Manifest.permission.DUMP); |
| 7942 | return; |
| 7943 | } |
| 7944 | |
| 7945 | boolean dumpAll = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7946 | boolean dumpClient = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 7947 | String dumpPackage = null; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7948 | |
| 7949 | int opti = 0; |
| 7950 | while (opti < args.length) { |
| 7951 | String opt = args[opti]; |
| 7952 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 7953 | break; |
| 7954 | } |
| 7955 | opti++; |
| 7956 | if ("-a".equals(opt)) { |
| 7957 | dumpAll = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7958 | } else if ("-c".equals(opt)) { |
| 7959 | dumpClient = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7960 | } else if ("-h".equals(opt)) { |
| 7961 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7962 | pw.println(" [-a] [-c] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7963 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7964 | pw.println(" a[ctivities]: activity stack state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 7965 | pw.println(" b[roadcasts] [PACKAGE_NAME]: broadcast state"); |
| 7966 | pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state"); |
| 7967 | pw.println(" p[rocesses] [PACKAGE_NAME]: process state"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 7968 | pw.println(" o[om]: out of memory management"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 7969 | pw.println(" prov[iders] [COMP_SPEC ...]: content provider state"); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 7970 | pw.println(" provider [COMP_SPEC]: provider client-side state"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 7971 | pw.println(" s[ervices] [COMP_SPEC ...]: service state"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7972 | pw.println(" service [COMP_SPEC]: service client-side state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 7973 | pw.println(" package [PACKAGE_NAME]: all state related to given package"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 7974 | pw.println(" all: dump all activities"); |
| 7975 | pw.println(" top: dump the top activity"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7976 | pw.println(" cmd may also be a COMP_SPEC to dump activities."); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 7977 | pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),"); |
| 7978 | pw.println(" a partial substring in a component name, a"); |
| 7979 | pw.println(" hex object identifier."); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 7980 | pw.println(" -a: include all available server state."); |
| 7981 | pw.println(" -c: include client state."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7982 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7983 | } else { |
| 7984 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7985 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 7986 | } |
| 7987 | |
| 7988 | // Is the caller requesting to dump a particular piece of data? |
| 7989 | if (opti < args.length) { |
| 7990 | String cmd = args[opti]; |
| 7991 | opti++; |
| 7992 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 7993 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 7994 | dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null); |
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 | return; |
| 7997 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 7998 | String[] newArgs; |
| 7999 | String name; |
| 8000 | if (opti >= args.length) { |
| 8001 | name = null; |
| 8002 | newArgs = EMPTY_STRING_ARRAY; |
| 8003 | } else { |
| 8004 | name = args[opti]; |
| 8005 | opti++; |
| 8006 | newArgs = new String[args.length - opti]; |
| 8007 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8008 | args.length - opti); |
| 8009 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8010 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8011 | dumpBroadcastsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8012 | } |
| 8013 | return; |
| 8014 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8015 | String[] newArgs; |
| 8016 | String name; |
| 8017 | if (opti >= args.length) { |
| 8018 | name = null; |
| 8019 | newArgs = EMPTY_STRING_ARRAY; |
| 8020 | } else { |
| 8021 | name = args[opti]; |
| 8022 | opti++; |
| 8023 | newArgs = new String[args.length - opti]; |
| 8024 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8025 | args.length - opti); |
| 8026 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8027 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8028 | dumpPendingIntentsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8029 | } |
| 8030 | return; |
| 8031 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8032 | String[] newArgs; |
| 8033 | String name; |
| 8034 | if (opti >= args.length) { |
| 8035 | name = null; |
| 8036 | newArgs = EMPTY_STRING_ARRAY; |
| 8037 | } else { |
| 8038 | name = args[opti]; |
| 8039 | opti++; |
| 8040 | newArgs = new String[args.length - opti]; |
| 8041 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8042 | args.length - opti); |
| 8043 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8044 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8045 | dumpProcessesLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8046 | } |
| 8047 | return; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8048 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 8049 | synchronized (this) { |
| 8050 | dumpOomLocked(fd, pw, args, opti, true); |
| 8051 | } |
| 8052 | return; |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8053 | } else if ("provider".equals(cmd)) { |
| 8054 | String[] newArgs; |
| 8055 | String name; |
| 8056 | if (opti >= args.length) { |
| 8057 | name = null; |
| 8058 | newArgs = EMPTY_STRING_ARRAY; |
| 8059 | } else { |
| 8060 | name = args[opti]; |
| 8061 | opti++; |
| 8062 | newArgs = new String[args.length - opti]; |
| 8063 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 8064 | } |
| 8065 | if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) { |
| 8066 | pw.println("No providers match: " + name); |
| 8067 | pw.println("Use -h for help."); |
| 8068 | } |
| 8069 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8070 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 8071 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8072 | dumpProvidersLocked(fd, pw, args, opti, true, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8073 | } |
| 8074 | return; |
| 8075 | } else if ("service".equals(cmd)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8076 | String[] newArgs; |
| 8077 | String name; |
| 8078 | if (opti >= args.length) { |
| 8079 | name = null; |
| 8080 | newArgs = EMPTY_STRING_ARRAY; |
| 8081 | } else { |
| 8082 | name = args[opti]; |
| 8083 | opti++; |
| 8084 | newArgs = new String[args.length - opti]; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8085 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8086 | args.length - opti); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8087 | } |
| 8088 | if (!dumpService(fd, pw, name, newArgs, 0, dumpAll)) { |
| 8089 | pw.println("No services match: " + name); |
| 8090 | pw.println("Use -h for help."); |
| 8091 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8092 | return; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8093 | } else if ("package".equals(cmd)) { |
| 8094 | String[] newArgs; |
| 8095 | if (opti >= args.length) { |
| 8096 | pw.println("package: no package name specified"); |
| 8097 | pw.println("Use -h for help."); |
| 8098 | return; |
| 8099 | } else { |
| 8100 | dumpPackage = args[opti]; |
| 8101 | opti++; |
| 8102 | newArgs = new String[args.length - opti]; |
| 8103 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8104 | args.length - opti); |
| 8105 | args = newArgs; |
| 8106 | opti = 0; |
| 8107 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8108 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 8109 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8110 | dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8111 | } |
| 8112 | return; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8113 | } else { |
| 8114 | // Dumping a single activity? |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8115 | if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 8116 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 8117 | pw.println("Use -h for help."); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8118 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8119 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8120 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8121 | } |
| 8122 | |
| 8123 | // No piece of data specified, dump everything. |
| 8124 | synchronized (this) { |
| 8125 | boolean needSep; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8126 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8127 | if (needSep) { |
| 8128 | pw.println(" "); |
| 8129 | } |
| 8130 | if (dumpAll) { |
| 8131 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8132 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8133 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8134 | if (needSep) { |
| 8135 | pw.println(" "); |
| 8136 | } |
| 8137 | if (dumpAll) { |
| 8138 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8139 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8140 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8141 | if (needSep) { |
| 8142 | pw.println(" "); |
| 8143 | } |
| 8144 | if (dumpAll) { |
| 8145 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8146 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8147 | needSep = dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8148 | if (needSep) { |
| 8149 | pw.println(" "); |
| 8150 | } |
| 8151 | if (dumpAll) { |
| 8152 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8153 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8154 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8155 | if (needSep) { |
| 8156 | pw.println(" "); |
| 8157 | } |
| 8158 | if (dumpAll) { |
| 8159 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8160 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8161 | dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8162 | } |
| 8163 | } |
| 8164 | |
| 8165 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8166 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8167 | pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)"); |
| 8168 | pw.println(" Main stack:"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8169 | dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient, |
| 8170 | dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8171 | pw.println(" "); |
| 8172 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8173 | dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false, |
| 8174 | dumpPackage); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8175 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8176 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8177 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8178 | dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8179 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8180 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8181 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8182 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8183 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8184 | dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8185 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8186 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 8187 | if (mMainStack.mGoingToSleepActivities.size() > 0) { |
| 8188 | pw.println(" "); |
| 8189 | pw.println(" Activities waiting to sleep:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8190 | dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8191 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 8192 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8193 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8194 | pw.println(" "); |
| 8195 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8196 | dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8197 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8198 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8199 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8200 | pw.println(" "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8201 | if (mMainStack.mPausingActivity != null) { |
| 8202 | pw.println(" mPausingActivity: " + mMainStack.mPausingActivity); |
| 8203 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8204 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8205 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8206 | if (dumpAll) { |
| 8207 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
| 8208 | pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 8209 | pw.println(" mDismissKeyguardOnNextActivity: " |
| 8210 | + mMainStack.mDismissKeyguardOnNextActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8211 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8212 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8213 | if (mRecentTasks.size() > 0) { |
| 8214 | pw.println(); |
| 8215 | pw.println(" Recent tasks:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8216 | |
| 8217 | final int N = mRecentTasks.size(); |
| 8218 | for (int i=0; i<N; i++) { |
| 8219 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8220 | if (dumpPackage != null) { |
| 8221 | if (tr.realActivity == null || |
| 8222 | !dumpPackage.equals(tr.realActivity)) { |
| 8223 | continue; |
| 8224 | } |
| 8225 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8226 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 8227 | pw.println(tr); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8228 | if (dumpAll) { |
| 8229 | mRecentTasks.get(i).dump(pw, " "); |
| 8230 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8231 | } |
| 8232 | } |
| 8233 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8234 | if (dumpAll) { |
| 8235 | pw.println(" "); |
| 8236 | pw.println(" mCurTask: " + mCurTask); |
| 8237 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8238 | |
| 8239 | return true; |
| 8240 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8241 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8242 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8243 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8244 | boolean needSep = false; |
| 8245 | int numPers = 0; |
| 8246 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8247 | pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)"); |
| 8248 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8249 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8250 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 8251 | final int NA = procs.size(); |
| 8252 | for (int ia=0; ia<NA; ia++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8253 | ProcessRecord r = procs.valueAt(ia); |
| 8254 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8255 | continue; |
| 8256 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8257 | if (!needSep) { |
| 8258 | pw.println(" All known processes:"); |
| 8259 | needSep = true; |
| 8260 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8261 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 8262 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 8263 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8264 | r.dump(pw, " "); |
| 8265 | if (r.persistent) { |
| 8266 | numPers++; |
| 8267 | } |
| 8268 | } |
| 8269 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8270 | } |
| 8271 | |
| 8272 | if (mLruProcesses.size() > 0) { |
| 8273 | if (needSep) pw.println(" "); |
| 8274 | needSep = true; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8275 | pw.println(" Process LRU list (sorted by oom_adj):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8276 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8277 | "Proc", "PERS", false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8278 | needSep = true; |
| 8279 | } |
| 8280 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8281 | if (dumpAll) { |
| 8282 | synchronized (mPidsSelfLocked) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8283 | boolean printed = false; |
| 8284 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 8285 | ProcessRecord r = mPidsSelfLocked.valueAt(i); |
| 8286 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8287 | continue; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8288 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8289 | if (!printed) { |
| 8290 | if (needSep) pw.println(" "); |
| 8291 | needSep = true; |
| 8292 | pw.println(" PID mappings:"); |
| 8293 | printed = true; |
| 8294 | } |
| 8295 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 8296 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8297 | } |
| 8298 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8299 | } |
| 8300 | |
| 8301 | if (mForegroundProcesses.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8302 | synchronized (mPidsSelfLocked) { |
| 8303 | boolean printed = false; |
| 8304 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 8305 | ProcessRecord r = mPidsSelfLocked.get( |
| 8306 | mForegroundProcesses.valueAt(i).pid); |
| 8307 | if (dumpPackage != null && (r == null |
| 8308 | || !dumpPackage.equals(r.info.packageName))) { |
| 8309 | continue; |
| 8310 | } |
| 8311 | if (!printed) { |
| 8312 | if (needSep) pw.println(" "); |
| 8313 | needSep = true; |
| 8314 | pw.println(" Foreground Processes:"); |
| 8315 | printed = true; |
| 8316 | } |
| 8317 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 8318 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
| 8319 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8320 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8321 | } |
| 8322 | |
| 8323 | if (mPersistentStartingProcesses.size() > 0) { |
| 8324 | if (needSep) pw.println(" "); |
| 8325 | needSep = true; |
| 8326 | pw.println(" Persisent processes that are starting:"); |
| 8327 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8328 | "Starting Norm", "Restarting PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8329 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8330 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8331 | if (mRemovedProcesses.size() > 0) { |
| 8332 | if (needSep) pw.println(" "); |
| 8333 | needSep = true; |
| 8334 | pw.println(" Processes that are being removed:"); |
| 8335 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8336 | "Removed Norm", "Removed PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8337 | } |
| 8338 | |
| 8339 | if (mProcessesOnHold.size() > 0) { |
| 8340 | if (needSep) pw.println(" "); |
| 8341 | needSep = true; |
| 8342 | pw.println(" Processes that are on old until the system is ready:"); |
| 8343 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8344 | "OnHold Norm", "OnHold PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8345 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8346 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8347 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8348 | |
| 8349 | if (mProcessCrashTimes.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8350 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8351 | long now = SystemClock.uptimeMillis(); |
| 8352 | for (Map.Entry<String, SparseArray<Long>> procs |
| 8353 | : mProcessCrashTimes.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8354 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8355 | SparseArray<Long> uids = procs.getValue(); |
| 8356 | final int N = uids.size(); |
| 8357 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8358 | int puid = uids.keyAt(i); |
| 8359 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 8360 | if (dumpPackage != null && (r == null |
| 8361 | || !dumpPackage.equals(r.info.packageName))) { |
| 8362 | continue; |
| 8363 | } |
| 8364 | if (!printed) { |
| 8365 | if (needSep) pw.println(" "); |
| 8366 | needSep = true; |
| 8367 | pw.println(" Time since processes crashed:"); |
| 8368 | printed = true; |
| 8369 | } |
| 8370 | pw.print(" Process "); pw.print(pname); |
| 8371 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8372 | pw.print(": last crashed "); |
| 8373 | pw.print((now-uids.valueAt(i))); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 8374 | pw.println(" ms ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 8375 | } |
| 8376 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8377 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8378 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8379 | if (mBadProcesses.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8380 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8381 | for (Map.Entry<String, SparseArray<Long>> procs |
| 8382 | : mBadProcesses.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8383 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8384 | SparseArray<Long> uids = procs.getValue(); |
| 8385 | final int N = uids.size(); |
| 8386 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8387 | int puid = uids.keyAt(i); |
| 8388 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 8389 | if (dumpPackage != null && (r == null |
| 8390 | || !dumpPackage.equals(r.info.packageName))) { |
| 8391 | continue; |
| 8392 | } |
| 8393 | if (!printed) { |
| 8394 | if (needSep) pw.println(" "); |
| 8395 | needSep = true; |
| 8396 | pw.println(" Bad processes:"); |
| 8397 | } |
| 8398 | pw.print(" Bad process "); pw.print(pname); |
| 8399 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8400 | pw.print(": crashed at time "); |
| 8401 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8402 | } |
| 8403 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8404 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8405 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8406 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8407 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 8408 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 8409 | if (dumpAll) { |
| 8410 | StringBuilder sb = new StringBuilder(128); |
| 8411 | sb.append(" mPreviousProcessVisibleTime: "); |
| 8412 | TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb); |
| 8413 | pw.println(sb); |
| 8414 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8415 | if (mHeavyWeightProcess != null) { |
| 8416 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 8417 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8418 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8419 | if (dumpAll) { |
| 8420 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | 3d0724d | 2011-05-12 15:39:41 -0700 | [diff] [blame] | 8421 | if (mCompatModePackages.getPackages().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8422 | boolean printed = false; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 8423 | for (Map.Entry<String, Integer> entry |
| 8424 | : mCompatModePackages.getPackages().entrySet()) { |
| 8425 | String pkg = entry.getKey(); |
| 8426 | int mode = entry.getValue(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8427 | if (dumpPackage != null && !dumpPackage.equals(pkg)) { |
| 8428 | continue; |
| 8429 | } |
| 8430 | if (!printed) { |
| 8431 | pw.println(" mScreenCompatPackages:"); |
| 8432 | printed = true; |
| 8433 | } |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 8434 | pw.print(" "); pw.print(pkg); pw.print(": "); |
| 8435 | pw.print(mode); pw.println(); |
| 8436 | } |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 8437 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8438 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8439 | pw.println(" mSleeping=" + mSleeping + " mShuttingDown=" + mShuttingDown); |
| 8440 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 8441 | || mOrigWaitForDebugger) { |
| 8442 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 8443 | + " mDebugTransient=" + mDebugTransient |
| 8444 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 8445 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 8446 | if (mProfileApp != null || mProfileProc != null || mProfileFile != null |
| 8447 | || mProfileFd != null) { |
| 8448 | pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc); |
| 8449 | pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd); |
| 8450 | pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler=" |
| 8451 | + mAutoStopProfiler); |
| 8452 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8453 | if (mAlwaysFinishActivities || mController != null) { |
| 8454 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 8455 | + " mController=" + mController); |
| 8456 | } |
| 8457 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8458 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8459 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 8460 | + " mProcessesReady=" + mProcessesReady |
| 8461 | + " mSystemReady=" + mSystemReady); |
| 8462 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8463 | + " mBooted=" + mBooted |
| 8464 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8465 | pw.print(" mLastPowerCheckRealtime="); |
| 8466 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 8467 | pw.println(""); |
| 8468 | pw.print(" mLastPowerCheckUptime="); |
| 8469 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 8470 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8471 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 8472 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 8473 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8474 | pw.println(" mNumServiceProcs=" + mNumServiceProcs |
| 8475 | + " mNewNumServiceProcs=" + mNewNumServiceProcs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8476 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8477 | |
| 8478 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8479 | } |
| 8480 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8481 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8482 | int opti, boolean needSep, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8483 | if (mProcessesToGc.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8484 | boolean printed = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8485 | long now = SystemClock.uptimeMillis(); |
| 8486 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 8487 | ProcessRecord proc = mProcessesToGc.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8488 | if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) { |
| 8489 | continue; |
| 8490 | } |
| 8491 | if (!printed) { |
| 8492 | if (needSep) pw.println(" "); |
| 8493 | needSep = true; |
| 8494 | pw.println(" Processes that are waiting to GC:"); |
| 8495 | printed = true; |
| 8496 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8497 | pw.print(" Process "); pw.println(proc); |
| 8498 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 8499 | pw.print(", last gced="); |
| 8500 | pw.print(now-proc.lastRequestedGc); |
| 8501 | pw.print(" ms ago, last lowMem="); |
| 8502 | pw.print(now-proc.lastLowMemory); |
| 8503 | pw.println(" ms ago"); |
| 8504 | |
| 8505 | } |
| 8506 | } |
| 8507 | return needSep; |
| 8508 | } |
| 8509 | |
| 8510 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8511 | int opti, boolean dumpAll) { |
| 8512 | boolean needSep = false; |
| 8513 | |
| 8514 | if (mLruProcesses.size() > 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8515 | if (needSep) pw.println(" "); |
| 8516 | needSep = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 8517 | pw.println(" OOM levels:"); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8518 | pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8519 | pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8520 | pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ); |
| 8521 | pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ); |
| 8522 | pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ); |
| 8523 | pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 8524 | pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8525 | pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8526 | pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 8527 | pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8528 | pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8529 | pw.print(" HIDDEN_APP_MIN_ADJ: "); pw.println(ProcessList.HIDDEN_APP_MIN_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8530 | pw.print(" HIDDEN_APP_MAX_ADJ: "); pw.println(ProcessList.HIDDEN_APP_MAX_ADJ); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 8531 | |
| 8532 | if (needSep) pw.println(" "); |
| 8533 | needSep = true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8534 | pw.println(" Process OOM control:"); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8535 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8536 | "Proc", "PERS", true, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8537 | needSep = true; |
| 8538 | } |
| 8539 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8540 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8541 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8542 | pw.println(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8543 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 8544 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8545 | if (mHeavyWeightProcess != null) { |
| 8546 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 8547 | } |
| 8548 | |
| 8549 | return true; |
| 8550 | } |
| 8551 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8552 | /** |
| 8553 | * There are three ways to call this: |
| 8554 | * - no service specified: dump all the services |
| 8555 | * - a flattened component name that matched an existing service was specified as the |
| 8556 | * first arg: dump that one service |
| 8557 | * - the first arg isn't the flattened component name of an existing service: |
| 8558 | * dump all services whose component contains the first arg as a substring |
| 8559 | */ |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8560 | protected boolean dumpService(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 8561 | int opti, boolean dumpAll) { |
| 8562 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8563 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8564 | if ("all".equals(name)) { |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 8565 | synchronized (this) { |
| 8566 | for (ServiceRecord r1 : mServices.values()) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8567 | services.add(r1); |
| 8568 | } |
| 8569 | } |
| 8570 | } else { |
| 8571 | ComponentName componentName = name != null |
| 8572 | ? ComponentName.unflattenFromString(name) : null; |
| 8573 | int objectId = 0; |
| 8574 | if (componentName == null) { |
| 8575 | // Not a '/' separated full component name; maybe an object ID? |
| 8576 | try { |
| 8577 | objectId = Integer.parseInt(name, 16); |
| 8578 | name = null; |
| 8579 | componentName = null; |
| 8580 | } catch (RuntimeException e) { |
| 8581 | } |
| 8582 | } |
| 8583 | |
| 8584 | synchronized (this) { |
| 8585 | for (ServiceRecord r1 : mServices.values()) { |
| 8586 | if (componentName != null) { |
| 8587 | if (r1.name.equals(componentName)) { |
| 8588 | services.add(r1); |
| 8589 | } |
| 8590 | } else if (name != null) { |
| 8591 | if (r1.name.flattenToString().contains(name)) { |
| 8592 | services.add(r1); |
| 8593 | } |
| 8594 | } else if (System.identityHashCode(r1) == objectId) { |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 8595 | services.add(r1); |
| 8596 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8597 | } |
| 8598 | } |
| 8599 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8600 | |
| 8601 | if (services.size() <= 0) { |
| 8602 | return false; |
| 8603 | } |
| 8604 | |
| 8605 | boolean needSep = false; |
| 8606 | for (int i=0; i<services.size(); i++) { |
| 8607 | if (needSep) { |
| 8608 | pw.println(); |
| 8609 | } |
| 8610 | needSep = true; |
| 8611 | dumpService("", fd, pw, services.get(i), args, dumpAll); |
| 8612 | } |
| 8613 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8614 | } |
| 8615 | |
| 8616 | /** |
| 8617 | * Invokes IApplicationThread.dumpService() on the thread of the specified service if |
| 8618 | * there is a thread associated with the service. |
| 8619 | */ |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8620 | private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw, |
| 8621 | final ServiceRecord r, String[] args, boolean dumpAll) { |
| 8622 | String innerPrefix = prefix + " "; |
| 8623 | synchronized (this) { |
| 8624 | pw.print(prefix); pw.print("SERVICE "); |
| 8625 | pw.print(r.shortName); pw.print(" "); |
| 8626 | pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 8627 | pw.print(" pid="); |
| 8628 | if (r.app != null) pw.println(r.app.pid); |
| 8629 | else pw.println("(not running)"); |
| 8630 | if (dumpAll) { |
| 8631 | r.dump(pw, innerPrefix); |
| 8632 | } |
| 8633 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8634 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8635 | pw.print(prefix); pw.println(" Client:"); |
| 8636 | pw.flush(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8637 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8638 | TransferPipe tp = new TransferPipe(); |
| 8639 | try { |
| 8640 | r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), r, args); |
| 8641 | tp.setBufferPrefix(prefix + " "); |
| 8642 | tp.go(fd); |
| 8643 | } finally { |
| 8644 | tp.kill(); |
| 8645 | } |
| 8646 | } catch (IOException e) { |
| 8647 | pw.println(prefix + " Failure while dumping the service: " + e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8648 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8649 | pw.println(prefix + " Got a RemoteException while dumping the service"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8650 | } |
| 8651 | } |
| 8652 | } |
| 8653 | |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8654 | /** |
| 8655 | * There are three ways to call this: |
| 8656 | * - no provider specified: dump all the providers |
| 8657 | * - a flattened component name that matched an existing provider was specified as the |
| 8658 | * first arg: dump that one provider |
| 8659 | * - the first arg isn't the flattened component name of an existing provider: |
| 8660 | * dump all providers whose component contains the first arg as a substring |
| 8661 | */ |
| 8662 | protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 8663 | int opti, boolean dumpAll) { |
| 8664 | ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); |
| 8665 | |
| 8666 | if ("all".equals(name)) { |
| 8667 | synchronized (this) { |
| 8668 | for (ContentProviderRecord r1 : mProvidersByClass.values()) { |
| 8669 | providers.add(r1); |
| 8670 | } |
| 8671 | } |
| 8672 | } else { |
| 8673 | ComponentName componentName = name != null |
| 8674 | ? ComponentName.unflattenFromString(name) : null; |
| 8675 | int objectId = 0; |
| 8676 | if (componentName == null) { |
| 8677 | // Not a '/' separated full component name; maybe an object ID? |
| 8678 | try { |
| 8679 | objectId = Integer.parseInt(name, 16); |
| 8680 | name = null; |
| 8681 | componentName = null; |
| 8682 | } catch (RuntimeException e) { |
| 8683 | } |
| 8684 | } |
| 8685 | |
| 8686 | synchronized (this) { |
| 8687 | for (ContentProviderRecord r1 : mProvidersByClass.values()) { |
| 8688 | if (componentName != null) { |
| 8689 | if (r1.name.equals(componentName)) { |
| 8690 | providers.add(r1); |
| 8691 | } |
| 8692 | } else if (name != null) { |
| 8693 | if (r1.name.flattenToString().contains(name)) { |
| 8694 | providers.add(r1); |
| 8695 | } |
| 8696 | } else if (System.identityHashCode(r1) == objectId) { |
| 8697 | providers.add(r1); |
| 8698 | } |
| 8699 | } |
| 8700 | } |
| 8701 | } |
| 8702 | |
| 8703 | if (providers.size() <= 0) { |
| 8704 | return false; |
| 8705 | } |
| 8706 | |
| 8707 | boolean needSep = false; |
| 8708 | for (int i=0; i<providers.size(); i++) { |
| 8709 | if (needSep) { |
| 8710 | pw.println(); |
| 8711 | } |
| 8712 | needSep = true; |
| 8713 | dumpProvider("", fd, pw, providers.get(i), args, dumpAll); |
| 8714 | } |
| 8715 | return true; |
| 8716 | } |
| 8717 | |
| 8718 | /** |
| 8719 | * Invokes IApplicationThread.dumpProvider() on the thread of the specified provider if |
| 8720 | * there is a thread associated with the provider. |
| 8721 | */ |
| 8722 | private void dumpProvider(String prefix, FileDescriptor fd, PrintWriter pw, |
| 8723 | final ContentProviderRecord r, String[] args, boolean dumpAll) { |
| 8724 | String innerPrefix = prefix + " "; |
| 8725 | synchronized (this) { |
| 8726 | pw.print(prefix); pw.print("PROVIDER "); |
| 8727 | pw.print(r); |
| 8728 | pw.print(" pid="); |
| 8729 | if (r.proc != null) pw.println(r.proc.pid); |
| 8730 | else pw.println("(not running)"); |
| 8731 | if (dumpAll) { |
| 8732 | r.dump(pw, innerPrefix); |
| 8733 | } |
| 8734 | } |
| 8735 | if (r.proc != null && r.proc.thread != null) { |
| 8736 | pw.println(" Client:"); |
| 8737 | pw.flush(); |
| 8738 | try { |
| 8739 | TransferPipe tp = new TransferPipe(); |
| 8740 | try { |
| 8741 | r.proc.thread.dumpProvider( |
| 8742 | tp.getWriteFd().getFileDescriptor(), r.provider.asBinder(), args); |
| 8743 | tp.setBufferPrefix(" "); |
| 8744 | // Short timeout, since blocking here can |
| 8745 | // deadlock with the application. |
| 8746 | tp.go(fd, 2000); |
| 8747 | } finally { |
| 8748 | tp.kill(); |
| 8749 | } |
| 8750 | } catch (IOException ex) { |
| 8751 | pw.println(" Failure while dumping the provider: " + ex); |
| 8752 | } catch (RemoteException ex) { |
| 8753 | pw.println(" Got a RemoteException while dumping the service"); |
| 8754 | } |
| 8755 | } |
| 8756 | } |
| 8757 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8758 | static class ItemMatcher { |
| 8759 | ArrayList<ComponentName> components; |
| 8760 | ArrayList<String> strings; |
| 8761 | ArrayList<Integer> objects; |
| 8762 | boolean all; |
| 8763 | |
| 8764 | ItemMatcher() { |
| 8765 | all = true; |
| 8766 | } |
| 8767 | |
| 8768 | void build(String name) { |
| 8769 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 8770 | if (componentName != null) { |
| 8771 | if (components == null) { |
| 8772 | components = new ArrayList<ComponentName>(); |
| 8773 | } |
| 8774 | components.add(componentName); |
| 8775 | all = false; |
| 8776 | } else { |
| 8777 | int objectId = 0; |
| 8778 | // Not a '/' separated full component name; maybe an object ID? |
| 8779 | try { |
| 8780 | objectId = Integer.parseInt(name, 16); |
| 8781 | if (objects == null) { |
| 8782 | objects = new ArrayList<Integer>(); |
| 8783 | } |
| 8784 | objects.add(objectId); |
| 8785 | all = false; |
| 8786 | } catch (RuntimeException e) { |
| 8787 | // Not an integer; just do string match. |
| 8788 | if (strings == null) { |
| 8789 | strings = new ArrayList<String>(); |
| 8790 | } |
| 8791 | strings.add(name); |
| 8792 | all = false; |
| 8793 | } |
| 8794 | } |
| 8795 | } |
| 8796 | |
| 8797 | int build(String[] args, int opti) { |
| 8798 | for (; opti<args.length; opti++) { |
| 8799 | String name = args[opti]; |
| 8800 | if ("--".equals(name)) { |
| 8801 | return opti+1; |
| 8802 | } |
| 8803 | build(name); |
| 8804 | } |
| 8805 | return opti; |
| 8806 | } |
| 8807 | |
| 8808 | boolean match(Object object, ComponentName comp) { |
| 8809 | if (all) { |
| 8810 | return true; |
| 8811 | } |
| 8812 | if (components != null) { |
| 8813 | for (int i=0; i<components.size(); i++) { |
| 8814 | if (components.get(i).equals(comp)) { |
| 8815 | return true; |
| 8816 | } |
| 8817 | } |
| 8818 | } |
| 8819 | if (objects != null) { |
| 8820 | for (int i=0; i<objects.size(); i++) { |
| 8821 | if (System.identityHashCode(object) == objects.get(i)) { |
| 8822 | return true; |
| 8823 | } |
| 8824 | } |
| 8825 | } |
| 8826 | if (strings != null) { |
| 8827 | String flat = comp.flattenToString(); |
| 8828 | for (int i=0; i<strings.size(); i++) { |
| 8829 | if (flat.contains(strings.get(i))) { |
| 8830 | return true; |
| 8831 | } |
| 8832 | } |
| 8833 | } |
| 8834 | return false; |
| 8835 | } |
| 8836 | } |
| 8837 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8838 | /** |
| 8839 | * There are three things that cmd can be: |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8840 | * - a flattened component name that matches an existing activity |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8841 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 8842 | * dump all activity whose component contains the cmd as a substring |
| 8843 | * - A hex number of the ActivityRecord object instance. |
| 8844 | */ |
| 8845 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 8846 | int opti, boolean dumpAll) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8847 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8848 | |
| 8849 | if ("all".equals(name)) { |
| 8850 | synchronized (this) { |
| 8851 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8852 | activities.add(r1); |
| 8853 | } |
| 8854 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 8855 | } else if ("top".equals(name)) { |
| 8856 | synchronized (this) { |
| 8857 | final int N = mMainStack.mHistory.size(); |
| 8858 | if (N > 0) { |
| 8859 | activities.add((ActivityRecord)mMainStack.mHistory.get(N-1)); |
| 8860 | } |
| 8861 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8862 | } else { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8863 | ItemMatcher matcher = new ItemMatcher(); |
| 8864 | matcher.build(name); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8865 | |
| 8866 | synchronized (this) { |
| 8867 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8868 | if (matcher.match(r1, r1.intent.getComponent())) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8869 | activities.add(r1); |
| 8870 | } |
| 8871 | } |
| 8872 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8873 | } |
| 8874 | |
| 8875 | if (activities.size() <= 0) { |
| 8876 | return false; |
| 8877 | } |
| 8878 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8879 | String[] newArgs = new String[args.length - opti]; |
| 8880 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 8881 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8882 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8883 | boolean needSep = false; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8884 | for (int i=activities.size()-1; i>=0; i--) { |
| 8885 | ActivityRecord r = (ActivityRecord)activities.get(i); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8886 | if (needSep) { |
| 8887 | pw.println(); |
| 8888 | } |
| 8889 | needSep = true; |
| 8890 | synchronized (this) { |
| 8891 | if (lastTask != r.task) { |
| 8892 | lastTask = r.task; |
| 8893 | pw.print("TASK "); pw.print(lastTask.affinity); |
| 8894 | pw.print(" id="); pw.println(lastTask.taskId); |
| 8895 | if (dumpAll) { |
| 8896 | lastTask.dump(pw, " "); |
| 8897 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8898 | } |
| 8899 | } |
| 8900 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8901 | } |
| 8902 | return true; |
| 8903 | } |
| 8904 | |
| 8905 | /** |
| 8906 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 8907 | * there is a thread associated with the activity. |
| 8908 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8909 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8910 | final ActivityRecord r, String[] args, boolean dumpAll) { |
| 8911 | String innerPrefix = prefix + " "; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8912 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8913 | pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName); |
| 8914 | pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 8915 | pw.print(" pid="); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8916 | if (r.app != null) pw.println(r.app.pid); |
| 8917 | else pw.println("(not running)"); |
| 8918 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8919 | r.dump(pw, innerPrefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8920 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8921 | } |
| 8922 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8923 | // flush anything that is already in the PrintWriter since the thread is going |
| 8924 | // to write to the file descriptor directly |
| 8925 | pw.flush(); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8926 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8927 | TransferPipe tp = new TransferPipe(); |
| 8928 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 8929 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 8930 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8931 | tp.go(fd); |
| 8932 | } finally { |
| 8933 | tp.kill(); |
| 8934 | } |
| 8935 | } catch (IOException e) { |
| 8936 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8937 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8938 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8939 | } |
| 8940 | } |
| 8941 | } |
| 8942 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8943 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8944 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8945 | boolean needSep = false; |
| 8946 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8947 | pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8948 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8949 | if (mRegisteredReceivers.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8950 | boolean printed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8951 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 8952 | while (it.hasNext()) { |
| 8953 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8954 | if (dumpPackage != null && (r.app == null || |
| 8955 | !dumpPackage.equals(r.app.info.packageName))) { |
| 8956 | continue; |
| 8957 | } |
| 8958 | if (!printed) { |
| 8959 | pw.println(" Registered Receivers:"); |
| 8960 | needSep = true; |
| 8961 | printed = true; |
| 8962 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8963 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8964 | r.dump(pw, " "); |
| 8965 | } |
| 8966 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8967 | |
| 8968 | if (mReceiverResolver.dump(pw, needSep ? |
| 8969 | "\n Receiver Resolver Table:" : " Receiver Resolver Table:", |
| 8970 | " ", dumpPackage, false)) { |
| 8971 | needSep = true; |
| 8972 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8973 | } |
| 8974 | |
| 8975 | if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0 |
| 8976 | || mPendingBroadcast != null) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8977 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8978 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8979 | BroadcastRecord br = mParallelBroadcasts.get(i); |
| 8980 | if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { |
| 8981 | continue; |
| 8982 | } |
| 8983 | if (!printed) { |
| 8984 | if (needSep) { |
| 8985 | pw.println(); |
| 8986 | } |
| 8987 | needSep = true; |
| 8988 | pw.println(" Active broadcasts:"); |
| 8989 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8990 | pw.println(" Broadcast #" + i + ":"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8991 | br.dump(pw, " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8992 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8993 | printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8994 | for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8995 | BroadcastRecord br = mOrderedBroadcasts.get(i); |
| 8996 | if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { |
| 8997 | continue; |
| 8998 | } |
| 8999 | if (!printed) { |
| 9000 | if (needSep) { |
| 9001 | pw.println(); |
| 9002 | } |
| 9003 | needSep = true; |
| 9004 | pw.println(" Active ordered broadcasts:"); |
| 9005 | } |
| 9006 | pw.println(" Ordered Broadcast #" + i + ":"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9007 | mOrderedBroadcasts.get(i).dump(pw, " "); |
| 9008 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9009 | if (dumpPackage == null || (mPendingBroadcast != null |
| 9010 | && dumpPackage.equals(mPendingBroadcast.callerPackage))) { |
| 9011 | if (needSep) { |
| 9012 | pw.println(); |
| 9013 | } |
| 9014 | pw.println(" Pending broadcast:"); |
| 9015 | if (mPendingBroadcast != null) { |
| 9016 | mPendingBroadcast.dump(pw, " "); |
| 9017 | } else { |
| 9018 | pw.println(" (null)"); |
| 9019 | } |
| 9020 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9021 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9022 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9023 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9024 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9025 | for (int i=0; i<MAX_BROADCAST_HISTORY; i++) { |
| 9026 | BroadcastRecord r = mBroadcastHistory[i]; |
| 9027 | if (r == null) { |
| 9028 | break; |
| 9029 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9030 | if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) { |
| 9031 | continue; |
| 9032 | } |
| 9033 | if (!printed) { |
| 9034 | if (needSep) { |
| 9035 | pw.println(); |
| 9036 | } |
| 9037 | needSep = true; |
| 9038 | pw.println(" Historical broadcasts:"); |
| 9039 | printed = true; |
| 9040 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9041 | if (dumpAll) { |
| 9042 | pw.print(" Historical Broadcast #"); pw.print(i); pw.println(":"); |
| 9043 | r.dump(pw, " "); |
| 9044 | } else { |
| 9045 | if (i >= 50) { |
| 9046 | pw.println(" ..."); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 9047 | break; |
| 9048 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9049 | pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 9050 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9051 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9052 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9053 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9054 | if (mStickyBroadcasts != null && dumpPackage == null) { |
| 9055 | if (needSep) { |
| 9056 | pw.println(); |
| 9057 | } |
| 9058 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9059 | pw.println(" Sticky broadcasts:"); |
| 9060 | StringBuilder sb = new StringBuilder(128); |
| 9061 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 9062 | : mStickyBroadcasts.entrySet()) { |
| 9063 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9064 | if (dumpAll) { |
| 9065 | pw.println(":"); |
| 9066 | ArrayList<Intent> intents = ent.getValue(); |
| 9067 | final int N = intents.size(); |
| 9068 | for (int i=0; i<N; i++) { |
| 9069 | sb.setLength(0); |
| 9070 | sb.append(" Intent: "); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9071 | intents.get(i).toShortString(sb, false, true, false); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9072 | pw.println(sb.toString()); |
| 9073 | Bundle bundle = intents.get(i).getExtras(); |
| 9074 | if (bundle != null) { |
| 9075 | pw.print(" "); |
| 9076 | pw.println(bundle.toString()); |
| 9077 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9078 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9079 | } else { |
| 9080 | pw.println(""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9081 | } |
| 9082 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9083 | needSep = true; |
| 9084 | } |
| 9085 | |
| 9086 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9087 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9088 | pw.println(" mBroadcastsScheduled=" + mBroadcastsScheduled); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9089 | pw.println(" mHandler:"); |
| 9090 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9091 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9092 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9093 | |
| 9094 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9095 | } |
| 9096 | |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9097 | /** |
| 9098 | * Prints a list of ServiceRecords (dumpsys activity services) |
| 9099 | */ |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9100 | boolean dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9101 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9102 | boolean needSep = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9103 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9104 | ItemMatcher matcher = new ItemMatcher(); |
| 9105 | matcher.build(args, opti); |
| 9106 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9107 | pw.println("ACTIVITY MANAGER SERVICES (dumpsys activity services)"); |
| 9108 | if (mServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9109 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9110 | long nowReal = SystemClock.elapsedRealtime(); |
| 9111 | Iterator<ServiceRecord> it = mServices.values().iterator(); |
| 9112 | needSep = false; |
| 9113 | while (it.hasNext()) { |
| 9114 | ServiceRecord r = it.next(); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9115 | if (!matcher.match(r, r.name)) { |
| 9116 | continue; |
| 9117 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9118 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9119 | continue; |
| 9120 | } |
| 9121 | if (!printed) { |
| 9122 | pw.println(" Active services:"); |
| 9123 | printed = true; |
| 9124 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9125 | if (needSep) { |
| 9126 | pw.println(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9127 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9128 | pw.print(" * "); pw.println(r); |
| 9129 | if (dumpAll) { |
| 9130 | r.dump(pw, " "); |
| 9131 | needSep = true; |
| 9132 | } else { |
| 9133 | pw.print(" app="); pw.println(r.app); |
| 9134 | pw.print(" created="); |
| 9135 | TimeUtils.formatDuration(r.createTime, nowReal, pw); |
| 9136 | pw.print(" started="); pw.print(r.startRequested); |
| 9137 | pw.print(" connections="); pw.println(r.connections.size()); |
Dianne Hackborn | 8ec8d41 | 2011-11-14 18:27:24 -0800 | [diff] [blame] | 9138 | if (r.connections.size() > 0) { |
| 9139 | pw.println(" Connections:"); |
| 9140 | for (ArrayList<ConnectionRecord> clist : r.connections.values()) { |
| 9141 | for (int i=0; i<clist.size(); i++) { |
| 9142 | ConnectionRecord conn = clist.get(i); |
| 9143 | pw.print(" "); |
| 9144 | pw.print(conn.binding.intent.intent.getIntent().toShortString( |
| 9145 | false, false, false)); |
| 9146 | pw.print(" -> "); |
| 9147 | ProcessRecord proc = conn.binding.client; |
| 9148 | pw.println(proc != null ? proc.toShortString() : "null"); |
| 9149 | } |
| 9150 | } |
| 9151 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9152 | } |
| 9153 | if (dumpClient && r.app != null && r.app.thread != null) { |
| 9154 | pw.println(" Client:"); |
| 9155 | pw.flush(); |
| 9156 | try { |
| 9157 | TransferPipe tp = new TransferPipe(); |
| 9158 | try { |
| 9159 | r.app.thread.dumpService( |
| 9160 | tp.getWriteFd().getFileDescriptor(), r, args); |
| 9161 | tp.setBufferPrefix(" "); |
| 9162 | // Short timeout, since blocking here can |
| 9163 | // deadlock with the application. |
| 9164 | tp.go(fd, 2000); |
| 9165 | } finally { |
| 9166 | tp.kill(); |
| 9167 | } |
| 9168 | } catch (IOException e) { |
| 9169 | pw.println(" Failure while dumping the service: " + e); |
| 9170 | } catch (RemoteException e) { |
| 9171 | pw.println(" Got a RemoteException while dumping the service"); |
| 9172 | } |
| 9173 | needSep = true; |
| 9174 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9175 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9176 | needSep = printed; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9177 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9178 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9179 | if (mPendingServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9180 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9181 | for (int i=0; i<mPendingServices.size(); i++) { |
| 9182 | ServiceRecord r = mPendingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9183 | if (!matcher.match(r, r.name)) { |
| 9184 | continue; |
| 9185 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9186 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9187 | continue; |
| 9188 | } |
| 9189 | if (!printed) { |
| 9190 | if (needSep) pw.println(" "); |
| 9191 | needSep = true; |
| 9192 | pw.println(" Pending services:"); |
| 9193 | printed = true; |
| 9194 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9195 | pw.print(" * Pending "); pw.println(r); |
| 9196 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9197 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9198 | needSep = true; |
| 9199 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9200 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9201 | if (mRestartingServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9202 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9203 | for (int i=0; i<mRestartingServices.size(); i++) { |
| 9204 | ServiceRecord r = mRestartingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9205 | if (!matcher.match(r, r.name)) { |
| 9206 | continue; |
| 9207 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9208 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9209 | continue; |
| 9210 | } |
| 9211 | if (!printed) { |
| 9212 | if (needSep) pw.println(" "); |
| 9213 | needSep = true; |
| 9214 | pw.println(" Restarting services:"); |
| 9215 | printed = true; |
| 9216 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9217 | pw.print(" * Restarting "); pw.println(r); |
| 9218 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9219 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9220 | needSep = true; |
| 9221 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9222 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9223 | if (mStoppingServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9224 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9225 | for (int i=0; i<mStoppingServices.size(); i++) { |
| 9226 | ServiceRecord r = mStoppingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9227 | if (!matcher.match(r, r.name)) { |
| 9228 | continue; |
| 9229 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9230 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9231 | continue; |
| 9232 | } |
| 9233 | if (!printed) { |
| 9234 | if (needSep) pw.println(" "); |
| 9235 | needSep = true; |
| 9236 | pw.println(" Stopping services:"); |
| 9237 | printed = true; |
| 9238 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9239 | pw.print(" * Stopping "); pw.println(r); |
| 9240 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9241 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9242 | needSep = true; |
| 9243 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9244 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9245 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9246 | if (mServiceConnections.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9247 | boolean printed = false; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9248 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9249 | = mServiceConnections.values().iterator(); |
| 9250 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9251 | ArrayList<ConnectionRecord> r = it.next(); |
| 9252 | for (int i=0; i<r.size(); i++) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9253 | ConnectionRecord cr = r.get(i); |
| 9254 | if (!matcher.match(cr.binding.service, cr.binding.service.name)) { |
| 9255 | continue; |
| 9256 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9257 | if (dumpPackage != null && (cr.binding.client == null |
| 9258 | || !dumpPackage.equals(cr.binding.client.info.packageName))) { |
| 9259 | continue; |
| 9260 | } |
| 9261 | if (!printed) { |
| 9262 | if (needSep) pw.println(" "); |
| 9263 | needSep = true; |
| 9264 | pw.println(" Connection bindings to services:"); |
| 9265 | printed = true; |
| 9266 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9267 | pw.print(" * "); pw.println(cr); |
| 9268 | cr.dump(pw, " "); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9269 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9270 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9271 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9272 | } |
| 9273 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9274 | |
| 9275 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9276 | } |
| 9277 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9278 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9279 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9280 | boolean needSep = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9281 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9282 | ItemMatcher matcher = new ItemMatcher(); |
| 9283 | matcher.build(args, opti); |
| 9284 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9285 | pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)"); |
| 9286 | if (mProvidersByClass.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9287 | boolean printed = false; |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9288 | Iterator<Map.Entry<ComponentName, ContentProviderRecord>> it |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9289 | = mProvidersByClass.entrySet().iterator(); |
| 9290 | while (it.hasNext()) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9291 | Map.Entry<ComponentName, ContentProviderRecord> e = it.next(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9292 | ContentProviderRecord r = e.getValue(); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9293 | ComponentName comp = e.getKey(); |
| 9294 | String cls = comp.getClassName(); |
| 9295 | int end = cls.lastIndexOf('.'); |
| 9296 | if (end > 0 && end < (cls.length()-2)) { |
| 9297 | cls = cls.substring(end+1); |
| 9298 | } |
| 9299 | if (!matcher.match(r, comp)) { |
| 9300 | continue; |
| 9301 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9302 | if (dumpPackage != null && !dumpPackage.equals(comp.getPackageName())) { |
| 9303 | continue; |
| 9304 | } |
| 9305 | if (!printed) { |
| 9306 | if (needSep) pw.println(" "); |
| 9307 | needSep = true; |
| 9308 | pw.println(" Published content providers (by class):"); |
| 9309 | printed = true; |
| 9310 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9311 | pw.print(" * "); pw.print(cls); pw.print(" ("); |
Dianne Hackborn | 8ec8d41 | 2011-11-14 18:27:24 -0800 | [diff] [blame] | 9312 | pw.print(comp.flattenToShortString()); pw.println(")"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9313 | if (dumpAll) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9314 | r.dump(pw, " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9315 | } else { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 9316 | if (r.proc != null) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 9317 | pw.print(" "); pw.println(r.proc); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9318 | } else { |
| 9319 | pw.println(); |
| 9320 | } |
Dianne Hackborn | 8ec8d41 | 2011-11-14 18:27:24 -0800 | [diff] [blame] | 9321 | if (r.clients.size() > 0) { |
| 9322 | pw.println(" Clients:"); |
| 9323 | for (ProcessRecord cproc : r.clients) { |
| 9324 | pw.print(" - "); pw.println(cproc); |
| 9325 | } |
| 9326 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9327 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9328 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9329 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9330 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9331 | if (dumpAll) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9332 | if (mProvidersByName.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9333 | boolean printed = false; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9334 | Iterator<Map.Entry<String, ContentProviderRecord>> it |
| 9335 | = mProvidersByName.entrySet().iterator(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9336 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9337 | Map.Entry<String, ContentProviderRecord> e = it.next(); |
| 9338 | ContentProviderRecord r = e.getValue(); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9339 | if (!matcher.match(r, r.name)) { |
| 9340 | continue; |
| 9341 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9342 | if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) { |
| 9343 | continue; |
| 9344 | } |
| 9345 | if (!printed) { |
| 9346 | if (needSep) pw.println(" "); |
| 9347 | needSep = true; |
| 9348 | pw.println(" Authority to provider mappings:"); |
| 9349 | printed = true; |
| 9350 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9351 | pw.print(" "); pw.print(e.getKey()); pw.println(":"); |
| 9352 | pw.print(" "); pw.println(r); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9353 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9354 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9355 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9356 | |
| 9357 | if (mLaunchingProviders.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9358 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9359 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9360 | ContentProviderRecord r = mLaunchingProviders.get(i); |
| 9361 | if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) { |
| 9362 | continue; |
| 9363 | } |
| 9364 | if (!printed) { |
| 9365 | if (needSep) pw.println(" "); |
| 9366 | needSep = true; |
| 9367 | pw.println(" Launching content providers:"); |
| 9368 | printed = true; |
| 9369 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9370 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9371 | pw.println(r); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9372 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9373 | } |
| 9374 | |
| 9375 | if (mGrantedUriPermissions.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9376 | if (needSep) pw.println(); |
| 9377 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9378 | pw.println("Granted Uri Permissions:"); |
| 9379 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 9380 | int uid = mGrantedUriPermissions.keyAt(i); |
| 9381 | HashMap<Uri, UriPermission> perms |
| 9382 | = mGrantedUriPermissions.valueAt(i); |
| 9383 | pw.print(" * UID "); pw.print(uid); |
| 9384 | pw.println(" holds:"); |
| 9385 | for (UriPermission perm : perms.values()) { |
| 9386 | pw.print(" "); pw.println(perm); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9387 | if (dumpAll) { |
| 9388 | perm.dump(pw, " "); |
| 9389 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9390 | } |
| 9391 | } |
| 9392 | needSep = true; |
| 9393 | } |
| 9394 | |
| 9395 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9396 | } |
| 9397 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9398 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9399 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9400 | boolean needSep = false; |
| 9401 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9402 | if (mIntentSenderRecords.size() > 0) { |
| 9403 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9404 | Iterator<WeakReference<PendingIntentRecord>> it |
| 9405 | = mIntentSenderRecords.values().iterator(); |
| 9406 | while (it.hasNext()) { |
| 9407 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 9408 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9409 | if (dumpPackage != null && (rec == null |
| 9410 | || !dumpPackage.equals(rec.key.packageName))) { |
| 9411 | continue; |
| 9412 | } |
| 9413 | if (!printed) { |
| 9414 | pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)"); |
| 9415 | printed = true; |
| 9416 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9417 | needSep = true; |
| 9418 | if (rec != null) { |
| 9419 | pw.print(" * "); pw.println(rec); |
| 9420 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9421 | rec.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9422 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9423 | } else { |
| 9424 | pw.print(" * "); pw.println(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9425 | } |
| 9426 | } |
| 9427 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9428 | |
| 9429 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9430 | } |
| 9431 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9432 | private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9433 | String prefix, String label, boolean complete, boolean brief, boolean client, |
| 9434 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9435 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9436 | boolean needNL = false; |
| 9437 | final String innerPrefix = prefix + " "; |
| 9438 | final String[] args = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9439 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9440 | final ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9441 | if (dumpPackage != null && !dumpPackage.equals(r.packageName)) { |
| 9442 | continue; |
| 9443 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 9444 | final boolean full = !brief && (complete || !r.isInHistory()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9445 | if (needNL) { |
| 9446 | pw.println(" "); |
| 9447 | needNL = false; |
| 9448 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9449 | if (lastTask != r.task) { |
| 9450 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9451 | pw.print(prefix); |
| 9452 | pw.print(full ? "* " : " "); |
| 9453 | pw.println(lastTask); |
| 9454 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9455 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9456 | } else if (complete) { |
| 9457 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 9458 | if (lastTask.intent != null) { |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9459 | pw.print(prefix); pw.print(" "); |
| 9460 | pw.println(lastTask.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9461 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9462 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9463 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9464 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 9465 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 9466 | pw.println(r); |
| 9467 | if (full) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9468 | r.dump(pw, innerPrefix); |
| 9469 | } else if (complete) { |
| 9470 | // Complete + brief == give a summary. Isn't that obvious?!? |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9471 | pw.print(innerPrefix); pw.println(r.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9472 | if (r.app != null) { |
| 9473 | pw.print(innerPrefix); pw.println(r.app); |
| 9474 | } |
| 9475 | } |
| 9476 | if (client && r.app != null && r.app.thread != null) { |
| 9477 | // flush anything that is already in the PrintWriter since the thread is going |
| 9478 | // to write to the file descriptor directly |
| 9479 | pw.flush(); |
| 9480 | try { |
| 9481 | TransferPipe tp = new TransferPipe(); |
| 9482 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9483 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9484 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9485 | // Short timeout, since blocking here can |
| 9486 | // deadlock with the application. |
| 9487 | tp.go(fd, 2000); |
| 9488 | } finally { |
| 9489 | tp.kill(); |
| 9490 | } |
| 9491 | } catch (IOException e) { |
| 9492 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
| 9493 | } catch (RemoteException e) { |
| 9494 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
| 9495 | } |
| 9496 | needNL = true; |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9497 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9498 | } |
| 9499 | } |
| 9500 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 9501 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 9502 | if (val == base) { |
| 9503 | if (space == null) return prefix; |
| 9504 | return prefix + " "; |
| 9505 | } |
| 9506 | return prefix + "+" + Integer.toString(val-base); |
| 9507 | } |
| 9508 | |
| 9509 | private static final int dumpProcessList(PrintWriter pw, |
| 9510 | ActivityManagerService service, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9511 | String prefix, String normalLabel, String persistentLabel, |
| 9512 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9513 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 9514 | final int N = list.size()-1; |
| 9515 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9516 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9517 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9518 | continue; |
| 9519 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9520 | pw.println(String.format("%s%s #%2d: %s", |
| 9521 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 9522 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9523 | if (r.persistent) { |
| 9524 | numPers++; |
| 9525 | } |
| 9526 | } |
| 9527 | return numPers; |
| 9528 | } |
| 9529 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9530 | private static final boolean dumpProcessOomList(PrintWriter pw, |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9531 | ActivityManagerService service, List<ProcessRecord> origList, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9532 | String prefix, String normalLabel, String persistentLabel, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9533 | boolean inclDetails, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9534 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9535 | ArrayList<Pair<ProcessRecord, Integer>> list |
| 9536 | = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size()); |
| 9537 | for (int i=0; i<origList.size(); i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9538 | ProcessRecord r = origList.get(i); |
| 9539 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9540 | continue; |
| 9541 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9542 | list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i)); |
| 9543 | } |
| 9544 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9545 | if (list.size() <= 0) { |
| 9546 | return false; |
| 9547 | } |
| 9548 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9549 | Comparator<Pair<ProcessRecord, Integer>> comparator |
| 9550 | = new Comparator<Pair<ProcessRecord, Integer>>() { |
| 9551 | @Override |
| 9552 | public int compare(Pair<ProcessRecord, Integer> object1, |
| 9553 | Pair<ProcessRecord, Integer> object2) { |
| 9554 | if (object1.first.setAdj != object2.first.setAdj) { |
| 9555 | return object1.first.setAdj > object2.first.setAdj ? -1 : 1; |
| 9556 | } |
| 9557 | if (object1.second.intValue() != object2.second.intValue()) { |
| 9558 | return object1.second.intValue() > object2.second.intValue() ? -1 : 1; |
| 9559 | } |
| 9560 | return 0; |
| 9561 | } |
| 9562 | }; |
| 9563 | |
| 9564 | Collections.sort(list, comparator); |
| 9565 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9566 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 9567 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 9568 | final long curUptime = SystemClock.uptimeMillis(); |
| 9569 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 9570 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9571 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9572 | ProcessRecord r = list.get(i).first; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9573 | String oomAdj; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9574 | if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9575 | oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9576 | } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) { |
| 9577 | oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9578 | } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) { |
| 9579 | oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9580 | } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) { |
| 9581 | oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9582 | } else if (r.setAdj >= ProcessList.SERVICE_ADJ) { |
| 9583 | oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9584 | } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9585 | oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9586 | } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 9587 | oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9588 | } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 9589 | oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9590 | } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 9591 | oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ); |
| 9592 | } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 9593 | oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9594 | } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) { |
| 9595 | oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9596 | } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) { |
| 9597 | oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9598 | } else { |
| 9599 | oomAdj = Integer.toString(r.setAdj); |
| 9600 | } |
| 9601 | String schedGroup; |
| 9602 | switch (r.setSchedGroup) { |
| 9603 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 9604 | schedGroup = "B"; |
| 9605 | break; |
| 9606 | case Process.THREAD_GROUP_DEFAULT: |
| 9607 | schedGroup = "F"; |
| 9608 | break; |
| 9609 | default: |
| 9610 | schedGroup = Integer.toString(r.setSchedGroup); |
| 9611 | break; |
| 9612 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 9613 | String foreground; |
| 9614 | if (r.foregroundActivities) { |
| 9615 | foreground = "A"; |
| 9616 | } else if (r.foregroundServices) { |
| 9617 | foreground = "S"; |
| 9618 | } else { |
| 9619 | foreground = " "; |
| 9620 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 9621 | pw.println(String.format("%s%s #%2d: adj=%s/%s%s trm=%2d %s (%s)", |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9622 | prefix, (r.persistent ? persistentLabel : normalLabel), |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9623 | (origList.size()-1)-list.get(i).second, oomAdj, schedGroup, |
| 9624 | foreground, r.trimMemoryLevel, r.toShortString(), r.adjType)); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9625 | if (r.adjSource != null || r.adjTarget != null) { |
| 9626 | pw.print(prefix); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9627 | pw.print(" "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9628 | if (r.adjTarget instanceof ComponentName) { |
| 9629 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 9630 | } else if (r.adjTarget != null) { |
| 9631 | pw.print(r.adjTarget.toString()); |
| 9632 | } else { |
| 9633 | pw.print("{null}"); |
| 9634 | } |
| 9635 | pw.print("<="); |
| 9636 | if (r.adjSource instanceof ProcessRecord) { |
| 9637 | pw.print("Proc{"); |
| 9638 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 9639 | pw.println("}"); |
| 9640 | } else if (r.adjSource != null) { |
| 9641 | pw.println(r.adjSource.toString()); |
| 9642 | } else { |
| 9643 | pw.println("{null}"); |
| 9644 | } |
| 9645 | } |
| 9646 | if (inclDetails) { |
| 9647 | pw.print(prefix); |
| 9648 | pw.print(" "); |
| 9649 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 9650 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
| 9651 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 9652 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 9653 | pw.print(" cur="); pw.print(r.curAdj); |
| 9654 | pw.print(" set="); pw.println(r.setAdj); |
| 9655 | pw.print(prefix); |
| 9656 | pw.print(" "); |
| 9657 | pw.print("keeping="); pw.print(r.keeping); |
| 9658 | pw.print(" hidden="); pw.print(r.hidden); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9659 | pw.print(" empty="); pw.print(r.empty); |
| 9660 | pw.print(" hasAboveClient="); pw.println(r.hasAboveClient); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9661 | |
| 9662 | if (!r.keeping) { |
| 9663 | if (r.lastWakeTime != 0) { |
| 9664 | long wtime; |
| 9665 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 9666 | synchronized (stats) { |
| 9667 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 9668 | r.pid, curRealtime); |
| 9669 | } |
| 9670 | long timeUsed = wtime - r.lastWakeTime; |
| 9671 | pw.print(prefix); |
| 9672 | pw.print(" "); |
| 9673 | pw.print("keep awake over "); |
| 9674 | TimeUtils.formatDuration(realtimeSince, pw); |
| 9675 | pw.print(" used "); |
| 9676 | TimeUtils.formatDuration(timeUsed, pw); |
| 9677 | pw.print(" ("); |
| 9678 | pw.print((timeUsed*100)/realtimeSince); |
| 9679 | pw.println("%)"); |
| 9680 | } |
| 9681 | if (r.lastCpuTime != 0) { |
| 9682 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 9683 | pw.print(prefix); |
| 9684 | pw.print(" "); |
| 9685 | pw.print("run cpu over "); |
| 9686 | TimeUtils.formatDuration(uptimeSince, pw); |
| 9687 | pw.print(" used "); |
| 9688 | TimeUtils.formatDuration(timeUsed, pw); |
| 9689 | pw.print(" ("); |
| 9690 | pw.print((timeUsed*100)/uptimeSince); |
| 9691 | pw.println("%)"); |
| 9692 | } |
| 9693 | } |
| 9694 | } |
| 9695 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9696 | return true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9697 | } |
| 9698 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9699 | ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9700 | ArrayList<ProcessRecord> procs; |
| 9701 | synchronized (this) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9702 | if (args != null && args.length > start |
| 9703 | && args[start].charAt(0) != '-') { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9704 | procs = new ArrayList<ProcessRecord>(); |
| 9705 | int pid = -1; |
| 9706 | try { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9707 | pid = Integer.parseInt(args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9708 | } catch (NumberFormatException e) { |
| 9709 | |
| 9710 | } |
| 9711 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 9712 | ProcessRecord proc = mLruProcesses.get(i); |
| 9713 | if (proc.pid == pid) { |
| 9714 | procs.add(proc); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9715 | } else if (proc.processName.equals(args[start])) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9716 | procs.add(proc); |
| 9717 | } |
| 9718 | } |
| 9719 | if (procs.size() <= 0) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9720 | pw.println("No process found for: " + args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9721 | return null; |
| 9722 | } |
| 9723 | } else { |
| 9724 | procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 9725 | } |
| 9726 | } |
| 9727 | return procs; |
| 9728 | } |
| 9729 | |
| 9730 | final void dumpGraphicsHardwareUsage(FileDescriptor fd, |
| 9731 | PrintWriter pw, String[] args) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9732 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9733 | if (procs == null) { |
| 9734 | return; |
| 9735 | } |
| 9736 | |
| 9737 | long uptime = SystemClock.uptimeMillis(); |
| 9738 | long realtime = SystemClock.elapsedRealtime(); |
| 9739 | pw.println("Applications Graphics Acceleration Info:"); |
| 9740 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 9741 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9742 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9743 | ProcessRecord r = procs.get(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9744 | if (r.thread != null) { |
| 9745 | pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **"); |
| 9746 | pw.flush(); |
| 9747 | try { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9748 | TransferPipe tp = new TransferPipe(); |
| 9749 | try { |
| 9750 | r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 9751 | tp.go(fd); |
| 9752 | } finally { |
| 9753 | tp.kill(); |
| 9754 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9755 | } catch (IOException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9756 | pw.println("Failure while dumping the app: " + r); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9757 | pw.flush(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9758 | } catch (RemoteException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9759 | pw.println("Got a RemoteException while dumping the app " + r); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9760 | pw.flush(); |
| 9761 | } |
| 9762 | } |
| 9763 | } |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9764 | } |
| 9765 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 9766 | final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 9767 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
| 9768 | if (procs == null) { |
| 9769 | return; |
| 9770 | } |
| 9771 | |
| 9772 | pw.println("Applications Database Info:"); |
| 9773 | |
| 9774 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9775 | ProcessRecord r = procs.get(i); |
| 9776 | if (r.thread != null) { |
| 9777 | pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **"); |
| 9778 | pw.flush(); |
| 9779 | try { |
| 9780 | TransferPipe tp = new TransferPipe(); |
| 9781 | try { |
| 9782 | r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 9783 | tp.go(fd); |
| 9784 | } finally { |
| 9785 | tp.kill(); |
| 9786 | } |
| 9787 | } catch (IOException e) { |
| 9788 | pw.println("Failure while dumping the app: " + r); |
| 9789 | pw.flush(); |
| 9790 | } catch (RemoteException e) { |
| 9791 | pw.println("Got a RemoteException while dumping the app " + r); |
| 9792 | pw.flush(); |
| 9793 | } |
| 9794 | } |
| 9795 | } |
| 9796 | } |
| 9797 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9798 | final static class MemItem { |
| 9799 | final String label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9800 | final String shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9801 | final long pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9802 | final int id; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9803 | ArrayList<MemItem> subitems; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9804 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9805 | public MemItem(String _label, String _shortLabel, long _pss, int _id) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9806 | label = _label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9807 | shortLabel = _shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9808 | pss = _pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9809 | id = _id; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9810 | } |
| 9811 | } |
| 9812 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9813 | static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9814 | boolean sort) { |
| 9815 | if (sort) { |
| 9816 | Collections.sort(items, new Comparator<MemItem>() { |
| 9817 | @Override |
| 9818 | public int compare(MemItem lhs, MemItem rhs) { |
| 9819 | if (lhs.pss < rhs.pss) { |
| 9820 | return 1; |
| 9821 | } else if (lhs.pss > rhs.pss) { |
| 9822 | return -1; |
| 9823 | } |
| 9824 | return 0; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9825 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9826 | }); |
| 9827 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9828 | |
| 9829 | for (int i=0; i<items.size(); i++) { |
| 9830 | MemItem mi = items.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9831 | pw.print(prefix); pw.printf("%7d kB: ", mi.pss); pw.println(mi.label); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9832 | if (mi.subitems != null) { |
| 9833 | dumpMemItems(pw, prefix + " ", mi.subitems, true); |
| 9834 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9835 | } |
| 9836 | } |
| 9837 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9838 | // These are in KB. |
| 9839 | static final long[] DUMP_MEM_BUCKETS = new long[] { |
| 9840 | 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024, |
| 9841 | 120*1024, 160*1024, 200*1024, |
| 9842 | 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024, |
| 9843 | 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024 |
| 9844 | }; |
| 9845 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9846 | static final void appendMemBucket(StringBuilder out, long memKB, String label, |
| 9847 | boolean stackLike) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9848 | int start = label.lastIndexOf('.'); |
| 9849 | if (start >= 0) start++; |
| 9850 | else start = 0; |
| 9851 | int end = label.length(); |
| 9852 | for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) { |
| 9853 | if (DUMP_MEM_BUCKETS[i] >= memKB) { |
| 9854 | long bucket = DUMP_MEM_BUCKETS[i]/1024; |
| 9855 | out.append(bucket); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9856 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9857 | out.append(label, start, end); |
| 9858 | return; |
| 9859 | } |
| 9860 | } |
| 9861 | out.append(memKB/1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9862 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9863 | out.append(label, start, end); |
| 9864 | } |
| 9865 | |
| 9866 | static final int[] DUMP_MEM_OOM_ADJ = new int[] { |
| 9867 | ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ, |
| 9868 | ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ, |
| 9869 | ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ, |
| 9870 | ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ |
| 9871 | }; |
| 9872 | static final String[] DUMP_MEM_OOM_LABEL = new String[] { |
| 9873 | "System", "Persistent", "Foreground", |
| 9874 | "Visible", "Perceptible", "Heavy Weight", |
| 9875 | "Backup", "A Services", "Home", "Previous", |
| 9876 | "B Services", "Background" |
| 9877 | }; |
| 9878 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9879 | final void dumpApplicationMemoryUsage(FileDescriptor fd, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9880 | PrintWriter pw, String prefix, String[] args, boolean brief, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9881 | PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9882 | boolean dumpAll = false; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9883 | boolean oomOnly = false; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9884 | |
| 9885 | int opti = 0; |
| 9886 | while (opti < args.length) { |
| 9887 | String opt = args[opti]; |
| 9888 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 9889 | break; |
| 9890 | } |
| 9891 | opti++; |
| 9892 | if ("-a".equals(opt)) { |
| 9893 | dumpAll = true; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9894 | } else if ("--oom".equals(opt)) { |
| 9895 | oomOnly = true; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9896 | } else if ("-h".equals(opt)) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9897 | pw.println("meminfo dump options: [-a] [--oom] [process]"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9898 | pw.println(" -a: include all available information for each process."); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9899 | pw.println(" --oom: only show processes organized by oom adj."); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9900 | pw.println("If [process] is specified it can be the name or "); |
| 9901 | pw.println("pid of a specific process to dump."); |
| 9902 | return; |
| 9903 | } else { |
| 9904 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 9905 | } |
| 9906 | } |
| 9907 | |
| 9908 | ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9909 | if (procs == null) { |
| 9910 | return; |
| 9911 | } |
| 9912 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 9913 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9914 | long uptime = SystemClock.uptimeMillis(); |
| 9915 | long realtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9916 | |
| 9917 | if (procs.size() == 1 || isCheckinRequest) { |
| 9918 | dumpAll = true; |
| 9919 | } |
| 9920 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9921 | if (isCheckinRequest) { |
| 9922 | // short checkin version |
| 9923 | pw.println(uptime + "," + realtime); |
| 9924 | pw.flush(); |
| 9925 | } else { |
| 9926 | pw.println("Applications Memory Usage (kB):"); |
| 9927 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 9928 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9929 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9930 | String[] innerArgs = new String[args.length-opti]; |
| 9931 | System.arraycopy(args, opti, innerArgs, 0, args.length-opti); |
| 9932 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9933 | ArrayList<MemItem> procMems = new ArrayList<MemItem>(); |
| 9934 | long nativePss=0, dalvikPss=0, otherPss=0; |
| 9935 | long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS]; |
| 9936 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9937 | long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length]; |
| 9938 | ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[]) |
| 9939 | new ArrayList[DUMP_MEM_OOM_LABEL.length]; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9940 | |
| 9941 | long totalPss = 0; |
| 9942 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9943 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9944 | ProcessRecord r = procs.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9945 | if (r.thread != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9946 | if (!isCheckinRequest && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9947 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 9948 | pw.flush(); |
| 9949 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9950 | Debug.MemoryInfo mi = null; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9951 | if (dumpAll) { |
| 9952 | try { |
| 9953 | mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs); |
| 9954 | } catch (RemoteException e) { |
| 9955 | if (!isCheckinRequest) { |
| 9956 | pw.println("Got RemoteException!"); |
| 9957 | pw.flush(); |
| 9958 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9959 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9960 | } else { |
| 9961 | mi = new Debug.MemoryInfo(); |
| 9962 | Debug.getMemoryInfo(r.pid, mi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9963 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9964 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9965 | if (!isCheckinRequest && mi != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9966 | long myTotalPss = mi.getTotalPss(); |
| 9967 | totalPss += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9968 | MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")", |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9969 | r.processName, myTotalPss, 0); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9970 | procMems.add(pssItem); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9971 | |
| 9972 | nativePss += mi.nativePss; |
| 9973 | dalvikPss += mi.dalvikPss; |
| 9974 | otherPss += mi.otherPss; |
| 9975 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
| 9976 | long mem = mi.getOtherPss(j); |
| 9977 | miscPss[j] += mem; |
| 9978 | otherPss -= mem; |
| 9979 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9980 | |
| 9981 | for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9982 | if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex] |
| 9983 | || oomIndex == (oomPss.length-1)) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9984 | oomPss[oomIndex] += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9985 | if (oomProcs[oomIndex] == null) { |
| 9986 | oomProcs[oomIndex] = new ArrayList<MemItem>(); |
| 9987 | } |
| 9988 | oomProcs[oomIndex].add(pssItem); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9989 | break; |
| 9990 | } |
| 9991 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9992 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9993 | } |
| 9994 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9995 | |
| 9996 | if (!isCheckinRequest && procs.size() > 1) { |
| 9997 | ArrayList<MemItem> catMems = new ArrayList<MemItem>(); |
| 9998 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9999 | catMems.add(new MemItem("Native", "Native", nativePss, -1)); |
| 10000 | catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2)); |
| 10001 | catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10002 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10003 | String label = Debug.MemoryInfo.getOtherLabel(j); |
| 10004 | catMems.add(new MemItem(label, label, miscPss[j], j)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10005 | } |
| 10006 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10007 | ArrayList<MemItem> oomMems = new ArrayList<MemItem>(); |
| 10008 | for (int j=0; j<oomPss.length; j++) { |
| 10009 | if (oomPss[j] != 0) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10010 | String label = DUMP_MEM_OOM_LABEL[j]; |
| 10011 | MemItem item = new MemItem(label, label, oomPss[j], |
| 10012 | DUMP_MEM_OOM_ADJ[j]); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10013 | item.subitems = oomProcs[j]; |
| 10014 | oomMems.add(item); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10015 | } |
| 10016 | } |
| 10017 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10018 | if (outTag != null || outStack != null) { |
| 10019 | if (outTag != null) { |
| 10020 | appendMemBucket(outTag, totalPss, "total", false); |
| 10021 | } |
| 10022 | if (outStack != null) { |
| 10023 | appendMemBucket(outStack, totalPss, "total", true); |
| 10024 | } |
| 10025 | boolean firstLine = true; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10026 | for (int i=0; i<oomMems.size(); i++) { |
| 10027 | MemItem miCat = oomMems.get(i); |
| 10028 | if (miCat.subitems == null || miCat.subitems.size() < 1) { |
| 10029 | continue; |
| 10030 | } |
| 10031 | if (miCat.id < ProcessList.SERVICE_ADJ |
| 10032 | || miCat.id == ProcessList.HOME_APP_ADJ |
| 10033 | || miCat.id == ProcessList.PREVIOUS_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10034 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10035 | outTag.append(" / "); |
| 10036 | } |
| 10037 | if (outStack != null) { |
| 10038 | if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10039 | if (firstLine) { |
| 10040 | outStack.append(":"); |
| 10041 | firstLine = false; |
| 10042 | } |
| 10043 | outStack.append("\n\t at "); |
| 10044 | } else { |
| 10045 | outStack.append("$"); |
| 10046 | } |
| 10047 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10048 | for (int j=0; j<miCat.subitems.size(); j++) { |
| 10049 | MemItem mi = miCat.subitems.get(j); |
| 10050 | if (j > 0) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10051 | if (outTag != null) { |
| 10052 | outTag.append(" "); |
| 10053 | } |
| 10054 | if (outStack != null) { |
| 10055 | outStack.append("$"); |
| 10056 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10057 | } |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10058 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10059 | appendMemBucket(outTag, mi.pss, mi.shortLabel, false); |
| 10060 | } |
| 10061 | if (outStack != null) { |
| 10062 | appendMemBucket(outStack, mi.pss, mi.shortLabel, true); |
| 10063 | } |
| 10064 | } |
| 10065 | if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10066 | outStack.append("("); |
| 10067 | for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) { |
| 10068 | if (DUMP_MEM_OOM_ADJ[k] == miCat.id) { |
| 10069 | outStack.append(DUMP_MEM_OOM_LABEL[k]); |
| 10070 | outStack.append(":"); |
| 10071 | outStack.append(DUMP_MEM_OOM_ADJ[k]); |
| 10072 | } |
| 10073 | } |
| 10074 | outStack.append(")"); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10075 | } |
| 10076 | } |
| 10077 | } |
| 10078 | } |
| 10079 | |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10080 | if (!brief && !oomOnly) { |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10081 | pw.println(); |
| 10082 | pw.println("Total PSS by process:"); |
| 10083 | dumpMemItems(pw, " ", procMems, true); |
| 10084 | pw.println(); |
| 10085 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10086 | pw.println("Total PSS by OOM adjustment:"); |
| 10087 | dumpMemItems(pw, " ", oomMems, false); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10088 | if (!oomOnly) { |
| 10089 | PrintWriter out = categoryPw != null ? categoryPw : pw; |
| 10090 | out.println(); |
| 10091 | out.println("Total PSS by category:"); |
| 10092 | dumpMemItems(out, " ", catMems, true); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10093 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10094 | pw.println(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10095 | pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10096 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10097 | } |
| 10098 | |
| 10099 | /** |
| 10100 | * Searches array of arguments for the specified string |
| 10101 | * @param args array of argument strings |
| 10102 | * @param value value to search for |
| 10103 | * @return true if the value is contained in the array |
| 10104 | */ |
| 10105 | private static boolean scanArgs(String[] args, String value) { |
| 10106 | if (args != null) { |
| 10107 | for (String arg : args) { |
| 10108 | if (value.equals(arg)) { |
| 10109 | return true; |
| 10110 | } |
| 10111 | } |
| 10112 | } |
| 10113 | return false; |
| 10114 | } |
| 10115 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10116 | private final void killServicesLocked(ProcessRecord app, |
| 10117 | boolean allowRestart) { |
| 10118 | // Report disconnected services. |
| 10119 | if (false) { |
| 10120 | // XXX we are letting the client link to the service for |
| 10121 | // death notifications. |
| 10122 | if (app.services.size() > 0) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10123 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10124 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10125 | ServiceRecord r = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10126 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10127 | Iterator<ArrayList<ConnectionRecord>> jt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10128 | = r.connections.values().iterator(); |
| 10129 | while (jt.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10130 | ArrayList<ConnectionRecord> cl = jt.next(); |
| 10131 | for (int i=0; i<cl.size(); i++) { |
| 10132 | ConnectionRecord c = cl.get(i); |
| 10133 | if (c.binding.client != app) { |
| 10134 | try { |
| 10135 | //c.conn.connected(r.className, null); |
| 10136 | } catch (Exception e) { |
| 10137 | // todo: this should be asynchronous! |
| 10138 | Slog.w(TAG, "Exception thrown disconnected servce " |
| 10139 | + r.shortName |
| 10140 | + " from app " + app.processName, e); |
| 10141 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10142 | } |
| 10143 | } |
| 10144 | } |
| 10145 | } |
| 10146 | } |
| 10147 | } |
| 10148 | } |
| 10149 | |
| 10150 | // Clean up any connections this application has to other services. |
| 10151 | if (app.connections.size() > 0) { |
| 10152 | Iterator<ConnectionRecord> it = app.connections.iterator(); |
| 10153 | while (it.hasNext()) { |
| 10154 | ConnectionRecord r = it.next(); |
| 10155 | removeConnectionLocked(r, app, null); |
| 10156 | } |
| 10157 | } |
| 10158 | app.connections.clear(); |
| 10159 | |
| 10160 | if (app.services.size() != 0) { |
| 10161 | // Any services running in the application need to be placed |
| 10162 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10163 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10164 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10165 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10166 | synchronized (sr.stats.getBatteryStats()) { |
| 10167 | sr.stats.stopLaunchedLocked(); |
| 10168 | } |
| 10169 | sr.app = null; |
| 10170 | sr.executeNesting = 0; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10171 | if (mStoppingServices.remove(sr)) { |
| 10172 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
| 10173 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10174 | |
| 10175 | boolean hasClients = sr.bindings.size() > 0; |
| 10176 | if (hasClients) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10177 | Iterator<IntentBindRecord> bindings |
| 10178 | = sr.bindings.values().iterator(); |
| 10179 | while (bindings.hasNext()) { |
| 10180 | IntentBindRecord b = bindings.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10181 | if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10182 | + ": shouldUnbind=" + b.hasBound); |
| 10183 | b.binder = null; |
| 10184 | b.requested = b.received = b.hasBound = false; |
| 10185 | } |
| 10186 | } |
| 10187 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 10188 | if (sr.crashCount >= 2 && (sr.serviceInfo.applicationInfo.flags |
| 10189 | &ApplicationInfo.FLAG_PERSISTENT) == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10190 | Slog.w(TAG, "Service crashed " + sr.crashCount |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10191 | + " times, stopping: " + sr); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10192 | EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10193 | sr.crashCount, sr.shortName, app.pid); |
| 10194 | bringDownServiceLocked(sr, true); |
| 10195 | } else if (!allowRestart) { |
| 10196 | bringDownServiceLocked(sr, true); |
| 10197 | } else { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10198 | boolean canceled = scheduleServiceRestartLocked(sr, true); |
| 10199 | |
| 10200 | // Should the service remain running? Note that in the |
| 10201 | // extreme case of so many attempts to deliver a command |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10202 | // that it failed we also will stop it here. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10203 | if (sr.startRequested && (sr.stopIfKilled || canceled)) { |
| 10204 | if (sr.pendingStarts.size() == 0) { |
| 10205 | sr.startRequested = false; |
| 10206 | if (!hasClients) { |
| 10207 | // Whoops, no reason to restart! |
| 10208 | bringDownServiceLocked(sr, true); |
| 10209 | } |
| 10210 | } |
| 10211 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10212 | } |
| 10213 | } |
| 10214 | |
| 10215 | if (!allowRestart) { |
| 10216 | app.services.clear(); |
| 10217 | } |
| 10218 | } |
| 10219 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10220 | // Make sure we have no more records on the stopping list. |
| 10221 | int i = mStoppingServices.size(); |
| 10222 | while (i > 0) { |
| 10223 | i--; |
| 10224 | ServiceRecord sr = mStoppingServices.get(i); |
| 10225 | if (sr.app == app) { |
| 10226 | mStoppingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10227 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10228 | } |
| 10229 | } |
| 10230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10231 | app.executingServices.clear(); |
| 10232 | } |
| 10233 | |
| 10234 | private final void removeDyingProviderLocked(ProcessRecord proc, |
| 10235 | ContentProviderRecord cpr) { |
| 10236 | synchronized (cpr) { |
| 10237 | cpr.launchingApp = null; |
| 10238 | cpr.notifyAll(); |
| 10239 | } |
| 10240 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10241 | mProvidersByClass.remove(cpr.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10242 | String names[] = cpr.info.authority.split(";"); |
| 10243 | for (int j = 0; j < names.length; j++) { |
| 10244 | mProvidersByName.remove(names[j]); |
| 10245 | } |
| 10246 | |
| 10247 | Iterator<ProcessRecord> cit = cpr.clients.iterator(); |
| 10248 | while (cit.hasNext()) { |
| 10249 | ProcessRecord capp = cit.next(); |
| 10250 | if (!capp.persistent && capp.thread != null |
| 10251 | && capp.pid != 0 |
| 10252 | && capp.pid != MY_PID) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 10253 | Slog.i(TAG, "Kill " + capp.processName |
| 10254 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10255 | + " in dying process " + (proc != null ? proc.processName : "??")); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 10256 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid, |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10257 | capp.processName, capp.setAdj, "dying provider " |
| 10258 | + cpr.name.toShortString()); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10259 | Process.killProcessQuiet(capp.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10260 | } |
| 10261 | } |
| 10262 | |
| 10263 | mLaunchingProviders.remove(cpr); |
| 10264 | } |
| 10265 | |
| 10266 | /** |
| 10267 | * Main code for cleaning up a process when it has gone away. This is |
| 10268 | * called both as a result of the process dying, or directly when stopping |
| 10269 | * a process when running in single process mode. |
| 10270 | */ |
| 10271 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10272 | boolean restarting, boolean allowRestart, int index) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10273 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10274 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10275 | } |
| 10276 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10277 | mProcessesToGc.remove(app); |
| 10278 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10279 | // Dismiss any open dialogs. |
| 10280 | if (app.crashDialog != null) { |
| 10281 | app.crashDialog.dismiss(); |
| 10282 | app.crashDialog = null; |
| 10283 | } |
| 10284 | if (app.anrDialog != null) { |
| 10285 | app.anrDialog.dismiss(); |
| 10286 | app.anrDialog = null; |
| 10287 | } |
| 10288 | if (app.waitDialog != null) { |
| 10289 | app.waitDialog.dismiss(); |
| 10290 | app.waitDialog = null; |
| 10291 | } |
| 10292 | |
| 10293 | app.crashing = false; |
| 10294 | app.notResponding = false; |
| 10295 | |
| 10296 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 10297 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10298 | app.thread = null; |
| 10299 | app.forcingToForeground = null; |
| 10300 | app.foregroundServices = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10301 | app.foregroundActivities = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 10302 | app.hasShownUi = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10303 | app.hasAboveClient = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10304 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10305 | killServicesLocked(app, allowRestart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10306 | |
| 10307 | boolean restart = false; |
| 10308 | |
| 10309 | int NL = mLaunchingProviders.size(); |
| 10310 | |
| 10311 | // Remove published content providers. |
| 10312 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10313 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10314 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10315 | ContentProviderRecord cpr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10316 | cpr.provider = null; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10317 | cpr.proc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10318 | |
| 10319 | // See if someone is waiting for this provider... in which |
| 10320 | // case we don't remove it, but just let it restart. |
| 10321 | int i = 0; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10322 | if (!app.bad && allowRestart) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10323 | for (; i<NL; i++) { |
| 10324 | if (mLaunchingProviders.get(i) == cpr) { |
| 10325 | restart = true; |
| 10326 | break; |
| 10327 | } |
| 10328 | } |
| 10329 | } else { |
| 10330 | i = NL; |
| 10331 | } |
| 10332 | |
| 10333 | if (i >= NL) { |
| 10334 | removeDyingProviderLocked(app, cpr); |
| 10335 | NL = mLaunchingProviders.size(); |
| 10336 | } |
| 10337 | } |
| 10338 | app.pubProviders.clear(); |
| 10339 | } |
| 10340 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10341 | // Take care of any launching providers waiting for this process. |
| 10342 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 10343 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10344 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10345 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10346 | // Unregister from connected content providers. |
| 10347 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 10348 | Iterator it = app.conProviders.keySet().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10349 | while (it.hasNext()) { |
| 10350 | ContentProviderRecord cpr = (ContentProviderRecord)it.next(); |
| 10351 | cpr.clients.remove(app); |
| 10352 | } |
| 10353 | app.conProviders.clear(); |
| 10354 | } |
| 10355 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10356 | // At this point there may be remaining entries in mLaunchingProviders |
| 10357 | // where we were the only one waiting, so they are no longer of use. |
| 10358 | // Look for these and clean up if found. |
| 10359 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 10360 | // the actual situation to identify what is actually going on. |
| 10361 | if (false) { |
| 10362 | for (int i=0; i<NL; i++) { |
| 10363 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 10364 | mLaunchingProviders.get(i); |
| 10365 | if (cpr.clients.size() <= 0 && cpr.externals <= 0) { |
| 10366 | synchronized (cpr) { |
| 10367 | cpr.launchingApp = null; |
| 10368 | cpr.notifyAll(); |
| 10369 | } |
| 10370 | } |
| 10371 | } |
| 10372 | } |
| 10373 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10374 | skipCurrentReceiverLocked(app); |
| 10375 | |
| 10376 | // Unregister any receivers. |
| 10377 | if (app.receivers.size() > 0) { |
| 10378 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 10379 | while (it.hasNext()) { |
| 10380 | removeReceiverLocked(it.next()); |
| 10381 | } |
| 10382 | app.receivers.clear(); |
| 10383 | } |
| 10384 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10385 | // If the app is undergoing backup, tell the backup manager about it |
| 10386 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10387 | if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10388 | try { |
| 10389 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10390 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10391 | bm.agentDisconnected(app.info.packageName); |
| 10392 | } catch (RemoteException e) { |
| 10393 | // can't happen; backup manager is local |
| 10394 | } |
| 10395 | } |
| 10396 | |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 10397 | mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10398 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10399 | // If the caller is restarting this app, then leave it in its |
| 10400 | // current lists and let the caller take care of it. |
| 10401 | if (restarting) { |
| 10402 | return; |
| 10403 | } |
| 10404 | |
| 10405 | if (!app.persistent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10406 | if (DEBUG_PROCESSES) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10407 | "Removing non-persistent process during cleanup: " + app); |
| 10408 | mProcessNames.remove(app.processName, app.info.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10409 | if (mHeavyWeightProcess == app) { |
| 10410 | mHeavyWeightProcess = null; |
| 10411 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 10412 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10413 | } else if (!app.removed) { |
| 10414 | // This app is persistent, so we need to keep its record around. |
| 10415 | // If it is not already on the pending app list, add it there |
| 10416 | // and start a new process for it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10417 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 10418 | mPersistentStartingProcesses.add(app); |
| 10419 | restart = true; |
| 10420 | } |
| 10421 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10422 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 10423 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10424 | mProcessesOnHold.remove(app); |
| 10425 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 10426 | if (app == mHomeProcess) { |
| 10427 | mHomeProcess = null; |
| 10428 | } |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10429 | if (app == mPreviousProcess) { |
| 10430 | mPreviousProcess = null; |
| 10431 | } |
| 10432 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10433 | if (restart) { |
| 10434 | // We have components that still need to be running in the |
| 10435 | // process, so re-launch it. |
| 10436 | mProcessNames.put(app.processName, app.info.uid, app); |
| 10437 | startProcessLocked(app, "restart", app.processName); |
| 10438 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 10439 | // Goodbye! |
| 10440 | synchronized (mPidsSelfLocked) { |
| 10441 | mPidsSelfLocked.remove(app.pid); |
| 10442 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 10443 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10444 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10445 | } |
| 10446 | } |
| 10447 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10448 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 10449 | // Look through the content providers we are waiting to have launched, |
| 10450 | // and if any run in this process then either schedule a restart of |
| 10451 | // the process or kill the client waiting for it if this process has |
| 10452 | // gone bad. |
| 10453 | int NL = mLaunchingProviders.size(); |
| 10454 | boolean restart = false; |
| 10455 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10456 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10457 | if (cpr.launchingApp == app) { |
| 10458 | if (!alwaysBad && !app.bad) { |
| 10459 | restart = true; |
| 10460 | } else { |
| 10461 | removeDyingProviderLocked(app, cpr); |
| 10462 | NL = mLaunchingProviders.size(); |
| 10463 | } |
| 10464 | } |
| 10465 | } |
| 10466 | return restart; |
| 10467 | } |
| 10468 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10469 | // ========================================================= |
| 10470 | // SERVICES |
| 10471 | // ========================================================= |
| 10472 | |
| 10473 | ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) { |
| 10474 | ActivityManager.RunningServiceInfo info = |
| 10475 | new ActivityManager.RunningServiceInfo(); |
| 10476 | info.service = r.name; |
| 10477 | if (r.app != null) { |
| 10478 | info.pid = r.app.pid; |
| 10479 | } |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 10480 | info.uid = r.appInfo.uid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10481 | info.process = r.processName; |
| 10482 | info.foreground = r.isForeground; |
| 10483 | info.activeSince = r.createTime; |
| 10484 | info.started = r.startRequested; |
| 10485 | info.clientCount = r.connections.size(); |
| 10486 | info.crashCount = r.crashCount; |
| 10487 | info.lastActivityTime = r.lastActivity; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 10488 | if (r.isForeground) { |
| 10489 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND; |
| 10490 | } |
| 10491 | if (r.startRequested) { |
| 10492 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED; |
| 10493 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 10494 | if (r.app != null && r.app.pid == MY_PID) { |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 10495 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS; |
| 10496 | } |
| 10497 | if (r.app != null && r.app.persistent) { |
| 10498 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS; |
| 10499 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10500 | |
| 10501 | for (ArrayList<ConnectionRecord> connl : r.connections.values()) { |
| 10502 | for (int i=0; i<connl.size(); i++) { |
| 10503 | ConnectionRecord conn = connl.get(i); |
| 10504 | if (conn.clientLabel != 0) { |
| 10505 | info.clientPackage = conn.binding.client.info.packageName; |
| 10506 | info.clientLabel = conn.clientLabel; |
| 10507 | return info; |
| 10508 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10509 | } |
| 10510 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10511 | return info; |
| 10512 | } |
| 10513 | |
| 10514 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 10515 | int flags) { |
| 10516 | synchronized (this) { |
| 10517 | ArrayList<ActivityManager.RunningServiceInfo> res |
| 10518 | = new ArrayList<ActivityManager.RunningServiceInfo>(); |
| 10519 | |
| 10520 | if (mServices.size() > 0) { |
| 10521 | Iterator<ServiceRecord> it = mServices.values().iterator(); |
| 10522 | while (it.hasNext() && res.size() < maxNum) { |
| 10523 | res.add(makeRunningServiceInfoLocked(it.next())); |
| 10524 | } |
| 10525 | } |
| 10526 | |
| 10527 | for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { |
| 10528 | ServiceRecord r = mRestartingServices.get(i); |
| 10529 | ActivityManager.RunningServiceInfo info = |
| 10530 | makeRunningServiceInfoLocked(r); |
| 10531 | info.restarting = r.nextRestartTime; |
| 10532 | res.add(info); |
| 10533 | } |
| 10534 | |
| 10535 | return res; |
| 10536 | } |
| 10537 | } |
| 10538 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10539 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
| 10540 | synchronized (this) { |
| 10541 | ServiceRecord r = mServices.get(name); |
| 10542 | if (r != null) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10543 | for (ArrayList<ConnectionRecord> conn : r.connections.values()) { |
| 10544 | for (int i=0; i<conn.size(); i++) { |
| 10545 | if (conn.get(i).clientIntent != null) { |
| 10546 | return conn.get(i).clientIntent; |
| 10547 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10548 | } |
| 10549 | } |
| 10550 | } |
| 10551 | } |
| 10552 | return null; |
| 10553 | } |
| 10554 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10555 | private final ServiceRecord findServiceLocked(ComponentName name, |
| 10556 | IBinder token) { |
| 10557 | ServiceRecord r = mServices.get(name); |
| 10558 | return r == token ? r : null; |
| 10559 | } |
| 10560 | |
| 10561 | private final class ServiceLookupResult { |
| 10562 | final ServiceRecord record; |
| 10563 | final String permission; |
| 10564 | |
| 10565 | ServiceLookupResult(ServiceRecord _record, String _permission) { |
| 10566 | record = _record; |
| 10567 | permission = _permission; |
| 10568 | } |
| 10569 | }; |
| 10570 | |
| 10571 | private ServiceLookupResult findServiceLocked(Intent service, |
| 10572 | String resolvedType) { |
| 10573 | ServiceRecord r = null; |
| 10574 | if (service.getComponent() != null) { |
| 10575 | r = mServices.get(service.getComponent()); |
| 10576 | } |
| 10577 | if (r == null) { |
| 10578 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
| 10579 | r = mServicesByIntent.get(filter); |
| 10580 | } |
| 10581 | |
| 10582 | if (r == null) { |
| 10583 | try { |
| 10584 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10585 | AppGlobals.getPackageManager().resolveService( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10586 | service, resolvedType, 0); |
| 10587 | ServiceInfo sInfo = |
| 10588 | rInfo != null ? rInfo.serviceInfo : null; |
| 10589 | if (sInfo == null) { |
| 10590 | return null; |
| 10591 | } |
| 10592 | |
| 10593 | ComponentName name = new ComponentName( |
| 10594 | sInfo.applicationInfo.packageName, sInfo.name); |
| 10595 | r = mServices.get(name); |
| 10596 | } catch (RemoteException ex) { |
| 10597 | // pm is in same process, this will never happen. |
| 10598 | } |
| 10599 | } |
| 10600 | if (r != null) { |
| 10601 | int callingPid = Binder.getCallingPid(); |
| 10602 | int callingUid = Binder.getCallingUid(); |
| 10603 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10604 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10605 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10606 | if (!r.exported) { |
| 10607 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 10608 | + " from pid=" + callingPid |
| 10609 | + ", uid=" + callingUid |
| 10610 | + " that is not exported from uid " + r.appInfo.uid); |
| 10611 | return new ServiceLookupResult(null, "not exported from uid " |
| 10612 | + r.appInfo.uid); |
| 10613 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10614 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10615 | + " from pid=" + callingPid |
| 10616 | + ", uid=" + callingUid |
| 10617 | + " requires " + r.permission); |
| 10618 | return new ServiceLookupResult(null, r.permission); |
| 10619 | } |
| 10620 | return new ServiceLookupResult(r, null); |
| 10621 | } |
| 10622 | return null; |
| 10623 | } |
| 10624 | |
| 10625 | private class ServiceRestarter implements Runnable { |
| 10626 | private ServiceRecord mService; |
| 10627 | |
| 10628 | void setService(ServiceRecord service) { |
| 10629 | mService = service; |
| 10630 | } |
| 10631 | |
| 10632 | public void run() { |
| 10633 | synchronized(ActivityManagerService.this) { |
| 10634 | performServiceRestartLocked(mService); |
| 10635 | } |
| 10636 | } |
| 10637 | } |
| 10638 | |
| 10639 | private ServiceLookupResult retrieveServiceLocked(Intent service, |
| 10640 | String resolvedType, int callingPid, int callingUid) { |
| 10641 | ServiceRecord r = null; |
| 10642 | if (service.getComponent() != null) { |
| 10643 | r = mServices.get(service.getComponent()); |
| 10644 | } |
| 10645 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
| 10646 | r = mServicesByIntent.get(filter); |
| 10647 | if (r == null) { |
| 10648 | try { |
| 10649 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10650 | AppGlobals.getPackageManager().resolveService( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 10651 | service, resolvedType, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10652 | ServiceInfo sInfo = |
| 10653 | rInfo != null ? rInfo.serviceInfo : null; |
| 10654 | if (sInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10655 | Slog.w(TAG, "Unable to start service " + service + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10656 | ": not found"); |
| 10657 | return null; |
| 10658 | } |
| 10659 | |
| 10660 | ComponentName name = new ComponentName( |
| 10661 | sInfo.applicationInfo.packageName, sInfo.name); |
| 10662 | r = mServices.get(name); |
| 10663 | if (r == null) { |
| 10664 | filter = new Intent.FilterComparison(service.cloneFilter()); |
| 10665 | ServiceRestarter res = new ServiceRestarter(); |
| 10666 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 10667 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 10668 | synchronized (stats) { |
| 10669 | ss = stats.getServiceStatsLocked( |
| 10670 | sInfo.applicationInfo.uid, sInfo.packageName, |
| 10671 | sInfo.name); |
| 10672 | } |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 10673 | r = new ServiceRecord(this, ss, name, filter, sInfo, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10674 | res.setService(r); |
| 10675 | mServices.put(name, r); |
| 10676 | mServicesByIntent.put(filter, r); |
| 10677 | |
| 10678 | // Make sure this component isn't in the pending list. |
| 10679 | int N = mPendingServices.size(); |
| 10680 | for (int i=0; i<N; i++) { |
| 10681 | ServiceRecord pr = mPendingServices.get(i); |
| 10682 | if (pr.name.equals(name)) { |
| 10683 | mPendingServices.remove(i); |
| 10684 | i--; |
| 10685 | N--; |
| 10686 | } |
| 10687 | } |
| 10688 | } |
| 10689 | } catch (RemoteException ex) { |
| 10690 | // pm is in same process, this will never happen. |
| 10691 | } |
| 10692 | } |
| 10693 | if (r != null) { |
| 10694 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10695 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10696 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10697 | if (!r.exported) { |
| 10698 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 10699 | + " from pid=" + callingPid |
| 10700 | + ", uid=" + callingUid |
| 10701 | + " that is not exported from uid " + r.appInfo.uid); |
| 10702 | return new ServiceLookupResult(null, "not exported from uid " |
| 10703 | + r.appInfo.uid); |
| 10704 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10705 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10706 | + " from pid=" + callingPid |
| 10707 | + ", uid=" + callingUid |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10708 | + " requires " + r.permission); |
| 10709 | return new ServiceLookupResult(null, r.permission); |
| 10710 | } |
| 10711 | return new ServiceLookupResult(r, null); |
| 10712 | } |
| 10713 | return null; |
| 10714 | } |
| 10715 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10716 | private final void bumpServiceExecutingLocked(ServiceRecord r, String why) { |
| 10717 | if (DEBUG_SERVICE) Log.v(TAG, ">>> EXECUTING " |
| 10718 | + why + " of " + r + " in app " + r.app); |
| 10719 | else if (DEBUG_SERVICE_EXECUTING) Log.v(TAG, ">>> EXECUTING " |
| 10720 | + why + " of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10721 | long now = SystemClock.uptimeMillis(); |
| 10722 | if (r.executeNesting == 0 && r.app != null) { |
| 10723 | if (r.app.executingServices.size() == 0) { |
| 10724 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 10725 | msg.obj = r.app; |
| 10726 | mHandler.sendMessageAtTime(msg, now+SERVICE_TIMEOUT); |
| 10727 | } |
| 10728 | r.app.executingServices.add(r); |
| 10729 | } |
| 10730 | r.executeNesting++; |
| 10731 | r.executingStart = now; |
| 10732 | } |
| 10733 | |
| 10734 | private final void sendServiceArgsLocked(ServiceRecord r, |
| 10735 | boolean oomAdjusted) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10736 | final int N = r.pendingStarts.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10737 | if (N == 0) { |
| 10738 | return; |
| 10739 | } |
| 10740 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10741 | while (r.pendingStarts.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10742 | try { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10743 | ServiceRecord.StartItem si = r.pendingStarts.remove(0); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10744 | if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to: " |
| 10745 | + r + " " + r.intent + " args=" + si.intent); |
Dianne Hackborn | 3a28f22 | 2011-03-01 12:25:54 -0800 | [diff] [blame] | 10746 | if (si.intent == null && N > 1) { |
| 10747 | // If somehow we got a dummy null intent in the middle, |
| 10748 | // then skip it. DO NOT skip a null intent when it is |
| 10749 | // the only one in the list -- this is to support the |
| 10750 | // onStartCommand(null) case. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10751 | continue; |
| 10752 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10753 | si.deliveredTime = SystemClock.uptimeMillis(); |
| 10754 | r.deliveredStarts.add(si); |
| 10755 | si.deliveryCount++; |
Dianne Hackborn | 3a28f22 | 2011-03-01 12:25:54 -0800 | [diff] [blame] | 10756 | if (si.targetPermissionUid >= 0 && si.intent != null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10757 | grantUriPermissionUncheckedFromIntentLocked(si.targetPermissionUid, |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 10758 | r.packageName, si.intent, si.getUriPermissionsLocked()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10759 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10760 | bumpServiceExecutingLocked(r, "start"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10761 | if (!oomAdjusted) { |
| 10762 | oomAdjusted = true; |
| 10763 | updateOomAdjLocked(r.app); |
| 10764 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10765 | int flags = 0; |
| 10766 | if (si.deliveryCount > 0) { |
| 10767 | flags |= Service.START_FLAG_RETRY; |
| 10768 | } |
| 10769 | if (si.doneExecutingCount > 0) { |
| 10770 | flags |= Service.START_FLAG_REDELIVERY; |
| 10771 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10772 | r.app.thread.scheduleServiceArgs(r, si.taskRemoved, si.id, flags, si.intent); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10773 | } catch (RemoteException e) { |
| 10774 | // Remote process gone... we'll let the normal cleanup take |
| 10775 | // care of this. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10776 | if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while scheduling start: " + r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10777 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10778 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10779 | Slog.w(TAG, "Unexpected exception", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10780 | break; |
| 10781 | } |
| 10782 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10783 | } |
| 10784 | |
| 10785 | private final boolean requestServiceBindingLocked(ServiceRecord r, |
| 10786 | IntentBindRecord i, boolean rebind) { |
| 10787 | if (r.app == null || r.app.thread == null) { |
| 10788 | // If service is not currently running, can't yet bind. |
| 10789 | return false; |
| 10790 | } |
| 10791 | if ((!i.requested || rebind) && i.apps.size() > 0) { |
| 10792 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10793 | bumpServiceExecutingLocked(r, "bind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10794 | r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind); |
| 10795 | if (!rebind) { |
| 10796 | i.requested = true; |
| 10797 | } |
| 10798 | i.hasBound = true; |
| 10799 | i.doRebind = false; |
| 10800 | } catch (RemoteException e) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10801 | 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] | 10802 | return false; |
| 10803 | } |
| 10804 | } |
| 10805 | return true; |
| 10806 | } |
| 10807 | |
| 10808 | private final void requestServiceBindingsLocked(ServiceRecord r) { |
| 10809 | Iterator<IntentBindRecord> bindings = r.bindings.values().iterator(); |
| 10810 | while (bindings.hasNext()) { |
| 10811 | IntentBindRecord i = bindings.next(); |
| 10812 | if (!requestServiceBindingLocked(r, i, false)) { |
| 10813 | break; |
| 10814 | } |
| 10815 | } |
| 10816 | } |
| 10817 | |
| 10818 | private final void realStartServiceLocked(ServiceRecord r, |
| 10819 | ProcessRecord app) throws RemoteException { |
| 10820 | if (app.thread == null) { |
| 10821 | throw new RemoteException(); |
| 10822 | } |
| 10823 | |
| 10824 | r.app = app; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 10825 | r.restartTime = r.lastActivity = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10826 | |
| 10827 | app.services.add(r); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10828 | bumpServiceExecutingLocked(r, "create"); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10829 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10830 | |
| 10831 | boolean created = false; |
| 10832 | try { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 10833 | mStringBuilder.setLength(0); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 10834 | r.intent.getIntent().toShortString(mStringBuilder, true, false, true); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10835 | EventLog.writeEvent(EventLogTags.AM_CREATE_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10836 | System.identityHashCode(r), r.shortName, |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 10837 | mStringBuilder.toString(), r.app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10838 | synchronized (r.stats.getBatteryStats()) { |
| 10839 | r.stats.startLaunchedLocked(); |
| 10840 | } |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 10841 | ensurePackageDexOpt(r.serviceInfo.packageName); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 10842 | app.thread.scheduleCreateService(r, r.serviceInfo, |
| 10843 | compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo)); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10844 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10845 | created = true; |
| 10846 | } finally { |
| 10847 | if (!created) { |
| 10848 | app.services.remove(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10849 | scheduleServiceRestartLocked(r, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10850 | } |
| 10851 | } |
| 10852 | |
| 10853 | requestServiceBindingsLocked(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10854 | |
| 10855 | // If the service is in the started state, and there are no |
| 10856 | // pending arguments, then fake up one so its onStartCommand() will |
| 10857 | // be called. |
| 10858 | if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10859 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
| 10860 | null, -1)); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10861 | } |
| 10862 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10863 | sendServiceArgsLocked(r, true); |
| 10864 | } |
| 10865 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10866 | private final boolean scheduleServiceRestartLocked(ServiceRecord r, |
| 10867 | boolean allowCancel) { |
| 10868 | boolean canceled = false; |
| 10869 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10870 | final long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10871 | long minDuration = SERVICE_RESTART_DURATION; |
Dianne Hackborn | 6ccd2af | 2009-08-27 12:26:44 -0700 | [diff] [blame] | 10872 | long resetTime = SERVICE_RESET_RUN_DURATION; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10873 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 10874 | if ((r.serviceInfo.applicationInfo.flags |
| 10875 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 10876 | minDuration /= 4; |
| 10877 | } |
| 10878 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10879 | // Any delivered but not yet finished starts should be put back |
| 10880 | // on the pending list. |
| 10881 | final int N = r.deliveredStarts.size(); |
| 10882 | if (N > 0) { |
| 10883 | for (int i=N-1; i>=0; i--) { |
| 10884 | ServiceRecord.StartItem si = r.deliveredStarts.get(i); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10885 | si.removeUriPermissionsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10886 | if (si.intent == null) { |
| 10887 | // We'll generate this again if needed. |
| 10888 | } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT |
| 10889 | && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) { |
| 10890 | r.pendingStarts.add(0, si); |
| 10891 | long dur = SystemClock.uptimeMillis() - si.deliveredTime; |
| 10892 | dur *= 2; |
| 10893 | if (minDuration < dur) minDuration = dur; |
| 10894 | if (resetTime < dur) resetTime = dur; |
| 10895 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10896 | Slog.w(TAG, "Canceling start item " + si.intent + " in service " |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10897 | + r.name); |
| 10898 | canceled = true; |
| 10899 | } |
| 10900 | } |
| 10901 | r.deliveredStarts.clear(); |
| 10902 | } |
| 10903 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10904 | r.totalRestartCount++; |
| 10905 | if (r.restartDelay == 0) { |
| 10906 | r.restartCount++; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10907 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10908 | } else { |
| 10909 | // If it has been a "reasonably long time" since the service |
| 10910 | // was started, then reset our restart duration back to |
| 10911 | // the beginning, so we don't infinitely increase the duration |
| 10912 | // on a service that just occasionally gets killed (which is |
| 10913 | // a normal case, due to process being killed to reclaim memory). |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10914 | if (now > (r.restartTime+resetTime)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10915 | r.restartCount = 1; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10916 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10917 | } else { |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 10918 | if ((r.serviceInfo.applicationInfo.flags |
| 10919 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 10920 | // Services in peristent processes will restart much more |
| 10921 | // quickly, since they are pretty important. (Think SystemUI). |
| 10922 | r.restartDelay += minDuration/2; |
| 10923 | } else { |
| 10924 | r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR; |
| 10925 | if (r.restartDelay < minDuration) { |
| 10926 | r.restartDelay = minDuration; |
| 10927 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10928 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10929 | } |
| 10930 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10931 | |
| 10932 | r.nextRestartTime = now + r.restartDelay; |
| 10933 | |
| 10934 | // Make sure that we don't end up restarting a bunch of services |
| 10935 | // all at the same time. |
| 10936 | boolean repeat; |
| 10937 | do { |
| 10938 | repeat = false; |
| 10939 | for (int i=mRestartingServices.size()-1; i>=0; i--) { |
| 10940 | ServiceRecord r2 = mRestartingServices.get(i); |
| 10941 | if (r2 != r && r.nextRestartTime |
| 10942 | >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN) |
| 10943 | && r.nextRestartTime |
| 10944 | < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) { |
| 10945 | r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN; |
| 10946 | r.restartDelay = r.nextRestartTime - now; |
| 10947 | repeat = true; |
| 10948 | break; |
| 10949 | } |
| 10950 | } |
| 10951 | } while (repeat); |
| 10952 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10953 | if (!mRestartingServices.contains(r)) { |
| 10954 | mRestartingServices.add(r); |
| 10955 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10956 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10957 | r.cancelNotification(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10958 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10959 | mHandler.removeCallbacks(r.restarter); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10960 | mHandler.postAtTime(r.restarter, r.nextRestartTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10961 | r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10962 | Slog.w(TAG, "Scheduling restart of crashed service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10963 | + r.shortName + " in " + r.restartDelay + "ms"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10964 | EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10965 | r.shortName, r.restartDelay); |
| 10966 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10967 | return canceled; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10968 | } |
| 10969 | |
| 10970 | final void performServiceRestartLocked(ServiceRecord r) { |
| 10971 | if (!mRestartingServices.contains(r)) { |
| 10972 | return; |
| 10973 | } |
| 10974 | bringUpServiceLocked(r, r.intent.getIntent().getFlags(), true); |
| 10975 | } |
| 10976 | |
| 10977 | private final boolean unscheduleServiceRestartLocked(ServiceRecord r) { |
| 10978 | if (r.restartDelay == 0) { |
| 10979 | return false; |
| 10980 | } |
| 10981 | r.resetRestartCounter(); |
| 10982 | mRestartingServices.remove(r); |
| 10983 | mHandler.removeCallbacks(r.restarter); |
| 10984 | return true; |
| 10985 | } |
| 10986 | |
| 10987 | private final boolean bringUpServiceLocked(ServiceRecord r, |
| 10988 | int intentFlags, boolean whileRestarting) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10989 | //Slog.i(TAG, "Bring up service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10990 | //r.dump(" "); |
| 10991 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10992 | if (r.app != null && r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10993 | sendServiceArgsLocked(r, false); |
| 10994 | return true; |
| 10995 | } |
| 10996 | |
| 10997 | if (!whileRestarting && r.restartDelay > 0) { |
| 10998 | // If waiting for a restart, then do nothing. |
| 10999 | return true; |
| 11000 | } |
| 11001 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11002 | 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] | 11003 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 11004 | // We are now bringing the service up, so no longer in the |
| 11005 | // restarting state. |
| 11006 | mRestartingServices.remove(r); |
| 11007 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11008 | // Service is now being launched, its package can't be stopped. |
| 11009 | try { |
| 11010 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 11011 | r.packageName, false); |
| 11012 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 11013 | } catch (IllegalArgumentException e) { |
| 11014 | Slog.w(TAG, "Failed trying to unstop package " |
| 11015 | + r.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11016 | } |
| 11017 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11018 | final String appName = r.processName; |
| 11019 | ProcessRecord app = getProcessRecordLocked(appName, r.appInfo.uid); |
| 11020 | if (app != null && app.thread != null) { |
| 11021 | try { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11022 | app.addPackage(r.appInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11023 | realStartServiceLocked(r, app); |
| 11024 | return true; |
| 11025 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11026 | 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] | 11027 | } |
| 11028 | |
| 11029 | // If a dead object exception was thrown -- fall through to |
| 11030 | // restart the application. |
| 11031 | } |
| 11032 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 11033 | // Not running -- get it started, and enqueue this service record |
| 11034 | // to be executed when the app comes up. |
| 11035 | if (startProcessLocked(appName, r.appInfo, true, intentFlags, |
| 11036 | "service", r.name, false) == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11037 | Slog.w(TAG, "Unable to launch app " |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 11038 | + r.appInfo.packageName + "/" |
| 11039 | + r.appInfo.uid + " for service " |
| 11040 | + r.intent.getIntent() + ": process is bad"); |
| 11041 | bringDownServiceLocked(r, true); |
| 11042 | return false; |
| 11043 | } |
| 11044 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11045 | if (!mPendingServices.contains(r)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11046 | mPendingServices.add(r); |
| 11047 | } |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 11048 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11049 | return true; |
| 11050 | } |
| 11051 | |
| 11052 | private final void bringDownServiceLocked(ServiceRecord r, boolean force) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11053 | //Slog.i(TAG, "Bring down service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11054 | //r.dump(" "); |
| 11055 | |
| 11056 | // Does it still need to run? |
| 11057 | if (!force && r.startRequested) { |
| 11058 | return; |
| 11059 | } |
| 11060 | if (r.connections.size() > 0) { |
| 11061 | if (!force) { |
| 11062 | // XXX should probably keep a count of the number of auto-create |
| 11063 | // connections directly in the service. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11064 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11065 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11066 | ArrayList<ConnectionRecord> cr = it.next(); |
| 11067 | for (int i=0; i<cr.size(); i++) { |
| 11068 | if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) { |
| 11069 | return; |
| 11070 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11071 | } |
| 11072 | } |
| 11073 | } |
| 11074 | |
| 11075 | // Report to all of the connections that the service is no longer |
| 11076 | // available. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11077 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11078 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11079 | ArrayList<ConnectionRecord> c = it.next(); |
| 11080 | for (int i=0; i<c.size(); i++) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11081 | ConnectionRecord cr = c.get(i); |
| 11082 | // There is still a connection to the service that is |
| 11083 | // being brought down. Mark it as dead. |
| 11084 | cr.serviceDead = true; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11085 | try { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11086 | cr.conn.connected(r.name, null); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11087 | } catch (Exception e) { |
| 11088 | Slog.w(TAG, "Failure disconnecting service " + r.name + |
| 11089 | " to connection " + c.get(i).conn.asBinder() + |
| 11090 | " (in " + c.get(i).binding.client.processName + ")", e); |
| 11091 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11092 | } |
| 11093 | } |
| 11094 | } |
| 11095 | |
| 11096 | // Tell the service that it has been unbound. |
| 11097 | if (r.bindings.size() > 0 && r.app != null && r.app.thread != null) { |
| 11098 | Iterator<IntentBindRecord> it = r.bindings.values().iterator(); |
| 11099 | while (it.hasNext()) { |
| 11100 | IntentBindRecord ibr = it.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11101 | 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] | 11102 | + ": hasBound=" + ibr.hasBound); |
| 11103 | if (r.app != null && r.app.thread != null && ibr.hasBound) { |
| 11104 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11105 | bumpServiceExecutingLocked(r, "bring down unbind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11106 | updateOomAdjLocked(r.app); |
| 11107 | ibr.hasBound = false; |
| 11108 | r.app.thread.scheduleUnbindService(r, |
| 11109 | ibr.intent.getIntent()); |
| 11110 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11111 | Slog.w(TAG, "Exception when unbinding service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11112 | + r.shortName, e); |
| 11113 | serviceDoneExecutingLocked(r, true); |
| 11114 | } |
| 11115 | } |
| 11116 | } |
| 11117 | } |
| 11118 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11119 | if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down " + r + " " + r.intent); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 11120 | EventLog.writeEvent(EventLogTags.AM_DESTROY_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11121 | System.identityHashCode(r), r.shortName, |
| 11122 | (r.app != null) ? r.app.pid : -1); |
| 11123 | |
| 11124 | mServices.remove(r.name); |
| 11125 | mServicesByIntent.remove(r.intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11126 | r.totalRestartCount = 0; |
| 11127 | unscheduleServiceRestartLocked(r); |
| 11128 | |
| 11129 | // Also make sure it is not on the pending list. |
| 11130 | int N = mPendingServices.size(); |
| 11131 | for (int i=0; i<N; i++) { |
| 11132 | if (mPendingServices.get(i) == r) { |
| 11133 | mPendingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11134 | if (DEBUG_SERVICE) Slog.v(TAG, "Removed pending: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11135 | i--; |
| 11136 | N--; |
| 11137 | } |
| 11138 | } |
| 11139 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11140 | r.cancelNotification(); |
| 11141 | r.isForeground = false; |
| 11142 | r.foregroundId = 0; |
| 11143 | r.foregroundNoti = null; |
| 11144 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11145 | // Clear start entries. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11146 | r.clearDeliveredStartsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11147 | r.pendingStarts.clear(); |
| 11148 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11149 | if (r.app != null) { |
| 11150 | synchronized (r.stats.getBatteryStats()) { |
| 11151 | r.stats.stopLaunchedLocked(); |
| 11152 | } |
| 11153 | r.app.services.remove(r); |
| 11154 | if (r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11155 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11156 | bumpServiceExecutingLocked(r, "stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11157 | mStoppingServices.add(r); |
| 11158 | updateOomAdjLocked(r.app); |
| 11159 | r.app.thread.scheduleStopService(r); |
| 11160 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11161 | Slog.w(TAG, "Exception when stopping service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11162 | + r.shortName, e); |
| 11163 | serviceDoneExecutingLocked(r, true); |
| 11164 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11165 | updateServiceForegroundLocked(r.app, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11166 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11167 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11168 | TAG, "Removed service that has no process: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11169 | } |
| 11170 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11171 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11172 | TAG, "Removed service that is not running: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11173 | } |
Vairavan Srinivasan | a207ce2 | 2010-12-23 13:51:48 -0800 | [diff] [blame] | 11174 | |
| 11175 | if (r.bindings.size() > 0) { |
| 11176 | r.bindings.clear(); |
| 11177 | } |
| 11178 | |
| 11179 | if (r.restarter instanceof ServiceRestarter) { |
| 11180 | ((ServiceRestarter)r.restarter).setService(null); |
| 11181 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11182 | } |
| 11183 | |
| 11184 | ComponentName startServiceLocked(IApplicationThread caller, |
| 11185 | Intent service, String resolvedType, |
| 11186 | int callingPid, int callingUid) { |
| 11187 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11188 | if (DEBUG_SERVICE) Slog.v(TAG, "startService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11189 | + " type=" + resolvedType + " args=" + service.getExtras()); |
| 11190 | |
| 11191 | if (caller != null) { |
| 11192 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11193 | if (callerApp == null) { |
| 11194 | throw new SecurityException( |
| 11195 | "Unable to find app for caller " + caller |
| 11196 | + " (pid=" + Binder.getCallingPid() |
| 11197 | + ") when starting service " + service); |
| 11198 | } |
| 11199 | } |
| 11200 | |
| 11201 | ServiceLookupResult res = |
| 11202 | retrieveServiceLocked(service, resolvedType, |
| 11203 | callingPid, callingUid); |
| 11204 | if (res == null) { |
| 11205 | return null; |
| 11206 | } |
| 11207 | if (res.record == null) { |
| 11208 | return new ComponentName("!", res.permission != null |
| 11209 | ? res.permission : "private to package"); |
| 11210 | } |
| 11211 | ServiceRecord r = res.record; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11212 | int targetPermissionUid = checkGrantUriPermissionFromIntentLocked( |
| 11213 | callingUid, r.packageName, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11214 | if (unscheduleServiceRestartLocked(r)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11215 | 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] | 11216 | } |
| 11217 | r.startRequested = true; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11218 | r.callStart = false; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11219 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11220 | service, targetPermissionUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11221 | r.lastActivity = SystemClock.uptimeMillis(); |
| 11222 | synchronized (r.stats.getBatteryStats()) { |
| 11223 | r.stats.startRunningLocked(); |
| 11224 | } |
| 11225 | if (!bringUpServiceLocked(r, service.getFlags(), false)) { |
| 11226 | return new ComponentName("!", "Service process is bad"); |
| 11227 | } |
| 11228 | return r.name; |
| 11229 | } |
| 11230 | } |
| 11231 | |
| 11232 | public ComponentName startService(IApplicationThread caller, Intent service, |
| 11233 | String resolvedType) { |
| 11234 | // Refuse possible leaked file descriptors |
| 11235 | if (service != null && service.hasFileDescriptors() == true) { |
| 11236 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11237 | } |
| 11238 | |
| 11239 | synchronized(this) { |
| 11240 | final int callingPid = Binder.getCallingPid(); |
| 11241 | final int callingUid = Binder.getCallingUid(); |
| 11242 | final long origId = Binder.clearCallingIdentity(); |
| 11243 | ComponentName res = startServiceLocked(caller, service, |
| 11244 | resolvedType, callingPid, callingUid); |
| 11245 | Binder.restoreCallingIdentity(origId); |
| 11246 | return res; |
| 11247 | } |
| 11248 | } |
| 11249 | |
| 11250 | ComponentName startServiceInPackage(int uid, |
| 11251 | Intent service, String resolvedType) { |
| 11252 | synchronized(this) { |
| 11253 | final long origId = Binder.clearCallingIdentity(); |
| 11254 | ComponentName res = startServiceLocked(null, service, |
| 11255 | resolvedType, -1, uid); |
| 11256 | Binder.restoreCallingIdentity(origId); |
| 11257 | return res; |
| 11258 | } |
| 11259 | } |
| 11260 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11261 | private void stopServiceLocked(ServiceRecord service) { |
| 11262 | synchronized (service.stats.getBatteryStats()) { |
| 11263 | service.stats.stopRunningLocked(); |
| 11264 | } |
| 11265 | service.startRequested = false; |
| 11266 | service.callStart = false; |
| 11267 | bringDownServiceLocked(service, false); |
| 11268 | } |
| 11269 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11270 | public int stopService(IApplicationThread caller, Intent service, |
| 11271 | String resolvedType) { |
| 11272 | // Refuse possible leaked file descriptors |
| 11273 | if (service != null && service.hasFileDescriptors() == true) { |
| 11274 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11275 | } |
| 11276 | |
| 11277 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11278 | if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11279 | + " type=" + resolvedType); |
| 11280 | |
| 11281 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11282 | if (caller != null && callerApp == null) { |
| 11283 | throw new SecurityException( |
| 11284 | "Unable to find app for caller " + caller |
| 11285 | + " (pid=" + Binder.getCallingPid() |
| 11286 | + ") when stopping service " + service); |
| 11287 | } |
| 11288 | |
| 11289 | // If this service is active, make sure it is stopped. |
| 11290 | ServiceLookupResult r = findServiceLocked(service, resolvedType); |
| 11291 | if (r != null) { |
| 11292 | if (r.record != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11293 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11294 | try { |
| 11295 | stopServiceLocked(r.record); |
| 11296 | } finally { |
| 11297 | Binder.restoreCallingIdentity(origId); |
| 11298 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11299 | return 1; |
| 11300 | } |
| 11301 | return -1; |
| 11302 | } |
| 11303 | } |
| 11304 | |
| 11305 | return 0; |
| 11306 | } |
| 11307 | |
| 11308 | public IBinder peekService(Intent service, String resolvedType) { |
| 11309 | // Refuse possible leaked file descriptors |
| 11310 | if (service != null && service.hasFileDescriptors() == true) { |
| 11311 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11312 | } |
| 11313 | |
| 11314 | IBinder ret = null; |
| 11315 | |
| 11316 | synchronized(this) { |
| 11317 | ServiceLookupResult r = findServiceLocked(service, resolvedType); |
| 11318 | |
| 11319 | if (r != null) { |
| 11320 | // r.record is null if findServiceLocked() failed the caller permission check |
| 11321 | if (r.record == null) { |
| 11322 | throw new SecurityException( |
| 11323 | "Permission Denial: Accessing service " + r.record.name |
| 11324 | + " from pid=" + Binder.getCallingPid() |
| 11325 | + ", uid=" + Binder.getCallingUid() |
| 11326 | + " requires " + r.permission); |
| 11327 | } |
| 11328 | IntentBindRecord ib = r.record.bindings.get(r.record.intent); |
| 11329 | if (ib != null) { |
| 11330 | ret = ib.binder; |
| 11331 | } |
| 11332 | } |
| 11333 | } |
| 11334 | |
| 11335 | return ret; |
| 11336 | } |
| 11337 | |
| 11338 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 11339 | int startId) { |
| 11340 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11341 | if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11342 | + " " + token + " startId=" + startId); |
| 11343 | ServiceRecord r = findServiceLocked(className, token); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11344 | if (r != null) { |
| 11345 | if (startId >= 0) { |
| 11346 | // Asked to only stop if done with all work. Note that |
| 11347 | // to avoid leaks, we will take this as dropping all |
| 11348 | // start items up to and including this one. |
| 11349 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 11350 | if (si != null) { |
| 11351 | while (r.deliveredStarts.size() > 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11352 | ServiceRecord.StartItem cur = r.deliveredStarts.remove(0); |
| 11353 | cur.removeUriPermissionsLocked(); |
| 11354 | if (cur == si) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11355 | break; |
| 11356 | } |
| 11357 | } |
| 11358 | } |
| 11359 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11360 | if (r.getLastStartId() != startId) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11361 | return false; |
| 11362 | } |
| 11363 | |
| 11364 | if (r.deliveredStarts.size() > 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11365 | Slog.w(TAG, "stopServiceToken startId " + startId |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11366 | + " is last, but have " + r.deliveredStarts.size() |
| 11367 | + " remaining args"); |
| 11368 | } |
| 11369 | } |
| 11370 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11371 | synchronized (r.stats.getBatteryStats()) { |
| 11372 | r.stats.stopRunningLocked(); |
| 11373 | r.startRequested = false; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11374 | r.callStart = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11375 | } |
| 11376 | final long origId = Binder.clearCallingIdentity(); |
| 11377 | bringDownServiceLocked(r, false); |
| 11378 | Binder.restoreCallingIdentity(origId); |
| 11379 | return true; |
| 11380 | } |
| 11381 | } |
| 11382 | return false; |
| 11383 | } |
| 11384 | |
| 11385 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11386 | int id, Notification notification, boolean removeNotification) { |
| 11387 | final long origId = Binder.clearCallingIdentity(); |
| 11388 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11389 | synchronized(this) { |
| 11390 | ServiceRecord r = findServiceLocked(className, token); |
| 11391 | if (r != null) { |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11392 | if (id != 0) { |
| 11393 | if (notification == null) { |
| 11394 | throw new IllegalArgumentException("null notification"); |
| 11395 | } |
| 11396 | if (r.foregroundId != id) { |
| 11397 | r.cancelNotification(); |
| 11398 | r.foregroundId = id; |
| 11399 | } |
| 11400 | notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 11401 | r.foregroundNoti = notification; |
| 11402 | r.isForeground = true; |
| 11403 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11404 | if (r.app != null) { |
| 11405 | updateServiceForegroundLocked(r.app, true); |
| 11406 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11407 | } else { |
| 11408 | if (r.isForeground) { |
| 11409 | r.isForeground = false; |
| 11410 | if (r.app != null) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 11411 | updateLruProcessLocked(r.app, false, true); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11412 | updateServiceForegroundLocked(r.app, true); |
| 11413 | } |
| 11414 | } |
| 11415 | if (removeNotification) { |
| 11416 | r.cancelNotification(); |
| 11417 | r.foregroundId = 0; |
| 11418 | r.foregroundNoti = null; |
| 11419 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11420 | } |
| 11421 | } |
| 11422 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11423 | } finally { |
| 11424 | Binder.restoreCallingIdentity(origId); |
| 11425 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11426 | } |
| 11427 | |
| 11428 | public void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) { |
| 11429 | boolean anyForeground = false; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 11430 | for (ServiceRecord sr : proc.services) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11431 | if (sr.isForeground) { |
| 11432 | anyForeground = true; |
| 11433 | break; |
| 11434 | } |
| 11435 | } |
| 11436 | if (anyForeground != proc.foregroundServices) { |
| 11437 | proc.foregroundServices = anyForeground; |
| 11438 | if (oomAdj) { |
| 11439 | updateOomAdjLocked(); |
| 11440 | } |
| 11441 | } |
| 11442 | } |
| 11443 | |
| 11444 | public int bindService(IApplicationThread caller, IBinder token, |
| 11445 | Intent service, String resolvedType, |
| 11446 | IServiceConnection connection, int flags) { |
| 11447 | // Refuse possible leaked file descriptors |
| 11448 | if (service != null && service.hasFileDescriptors() == true) { |
| 11449 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11450 | } |
| 11451 | |
| 11452 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11453 | if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11454 | + " type=" + resolvedType + " conn=" + connection.asBinder() |
| 11455 | + " flags=0x" + Integer.toHexString(flags)); |
| 11456 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11457 | if (callerApp == null) { |
| 11458 | throw new SecurityException( |
| 11459 | "Unable to find app for caller " + caller |
| 11460 | + " (pid=" + Binder.getCallingPid() |
| 11461 | + ") when binding service " + service); |
| 11462 | } |
| 11463 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11464 | ActivityRecord activity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11465 | if (token != null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 11466 | activity = mMainStack.isInStackLocked(token); |
| 11467 | if (activity == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11468 | Slog.w(TAG, "Binding with unknown activity: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11469 | return 0; |
| 11470 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11471 | } |
| 11472 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11473 | int clientLabel = 0; |
| 11474 | PendingIntent clientIntent = null; |
| 11475 | |
| 11476 | if (callerApp.info.uid == Process.SYSTEM_UID) { |
| 11477 | // Hacky kind of thing -- allow system stuff to tell us |
| 11478 | // what they are, so we can report this elsewhere for |
| 11479 | // others to know why certain services are running. |
| 11480 | try { |
| 11481 | clientIntent = (PendingIntent)service.getParcelableExtra( |
| 11482 | Intent.EXTRA_CLIENT_INTENT); |
| 11483 | } catch (RuntimeException e) { |
| 11484 | } |
| 11485 | if (clientIntent != null) { |
| 11486 | clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0); |
| 11487 | if (clientLabel != 0) { |
| 11488 | // There are no useful extras in the intent, trash them. |
| 11489 | // System code calling with this stuff just needs to know |
| 11490 | // this will happen. |
| 11491 | service = service.cloneFilter(); |
| 11492 | } |
| 11493 | } |
| 11494 | } |
| 11495 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11496 | ServiceLookupResult res = |
| 11497 | retrieveServiceLocked(service, resolvedType, |
| 11498 | Binder.getCallingPid(), Binder.getCallingUid()); |
| 11499 | if (res == null) { |
| 11500 | return 0; |
| 11501 | } |
| 11502 | if (res.record == null) { |
| 11503 | return -1; |
| 11504 | } |
| 11505 | ServiceRecord s = res.record; |
| 11506 | |
| 11507 | final long origId = Binder.clearCallingIdentity(); |
| 11508 | |
| 11509 | if (unscheduleServiceRestartLocked(s)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11510 | if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: " |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11511 | + s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11512 | } |
| 11513 | |
| 11514 | AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp); |
| 11515 | ConnectionRecord c = new ConnectionRecord(b, activity, |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11516 | connection, flags, clientLabel, clientIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11517 | |
| 11518 | IBinder binder = connection.asBinder(); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11519 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 11520 | if (clist == null) { |
| 11521 | clist = new ArrayList<ConnectionRecord>(); |
| 11522 | s.connections.put(binder, clist); |
| 11523 | } |
| 11524 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11525 | b.connections.add(c); |
| 11526 | if (activity != null) { |
| 11527 | if (activity.connections == null) { |
| 11528 | activity.connections = new HashSet<ConnectionRecord>(); |
| 11529 | } |
| 11530 | activity.connections.add(c); |
| 11531 | } |
| 11532 | b.client.connections.add(c); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 11533 | if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { |
| 11534 | b.client.hasAboveClient = true; |
| 11535 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11536 | clist = mServiceConnections.get(binder); |
| 11537 | if (clist == null) { |
| 11538 | clist = new ArrayList<ConnectionRecord>(); |
| 11539 | mServiceConnections.put(binder, clist); |
| 11540 | } |
| 11541 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11542 | |
| 11543 | if ((flags&Context.BIND_AUTO_CREATE) != 0) { |
| 11544 | s.lastActivity = SystemClock.uptimeMillis(); |
| 11545 | if (!bringUpServiceLocked(s, service.getFlags(), false)) { |
| 11546 | return 0; |
| 11547 | } |
| 11548 | } |
| 11549 | |
| 11550 | if (s.app != null) { |
| 11551 | // This could have made the service more important. |
| 11552 | updateOomAdjLocked(s.app); |
| 11553 | } |
| 11554 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11555 | 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] | 11556 | + ": received=" + b.intent.received |
| 11557 | + " apps=" + b.intent.apps.size() |
| 11558 | + " doRebind=" + b.intent.doRebind); |
| 11559 | |
| 11560 | if (s.app != null && b.intent.received) { |
| 11561 | // Service is already running, so we can immediately |
| 11562 | // publish the connection. |
| 11563 | try { |
| 11564 | c.conn.connected(s.name, b.intent.binder); |
| 11565 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11566 | Slog.w(TAG, "Failure sending service " + s.shortName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11567 | + " to connection " + c.conn.asBinder() |
| 11568 | + " (in " + c.binding.client.processName + ")", e); |
| 11569 | } |
| 11570 | |
| 11571 | // If this is the first app connected back to this binding, |
| 11572 | // and the service had previously asked to be told when |
| 11573 | // rebound, then do so. |
| 11574 | if (b.intent.apps.size() == 1 && b.intent.doRebind) { |
| 11575 | requestServiceBindingLocked(s, b.intent, true); |
| 11576 | } |
| 11577 | } else if (!b.intent.requested) { |
| 11578 | requestServiceBindingLocked(s, b.intent, false); |
| 11579 | } |
| 11580 | |
| 11581 | Binder.restoreCallingIdentity(origId); |
| 11582 | } |
| 11583 | |
| 11584 | return 1; |
| 11585 | } |
| 11586 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 11587 | void removeConnectionLocked( |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11588 | ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11589 | IBinder binder = c.conn.asBinder(); |
| 11590 | AppBindRecord b = c.binding; |
| 11591 | ServiceRecord s = b.service; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11592 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 11593 | if (clist != null) { |
| 11594 | clist.remove(c); |
| 11595 | if (clist.size() == 0) { |
| 11596 | s.connections.remove(binder); |
| 11597 | } |
| 11598 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11599 | b.connections.remove(c); |
| 11600 | if (c.activity != null && c.activity != skipAct) { |
| 11601 | if (c.activity.connections != null) { |
| 11602 | c.activity.connections.remove(c); |
| 11603 | } |
| 11604 | } |
| 11605 | if (b.client != skipApp) { |
| 11606 | b.client.connections.remove(c); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 11607 | if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { |
| 11608 | b.client.updateHasAboveClientLocked(); |
| 11609 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11610 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11611 | clist = mServiceConnections.get(binder); |
| 11612 | if (clist != null) { |
| 11613 | clist.remove(c); |
| 11614 | if (clist.size() == 0) { |
| 11615 | mServiceConnections.remove(binder); |
| 11616 | } |
| 11617 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11618 | |
| 11619 | if (b.connections.size() == 0) { |
| 11620 | b.intent.apps.remove(b.client); |
| 11621 | } |
| 11622 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11623 | if (!c.serviceDead) { |
| 11624 | if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent |
| 11625 | + ": shouldUnbind=" + b.intent.hasBound); |
| 11626 | if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0 |
| 11627 | && b.intent.hasBound) { |
| 11628 | try { |
| 11629 | bumpServiceExecutingLocked(s, "unbind"); |
| 11630 | updateOomAdjLocked(s.app); |
| 11631 | b.intent.hasBound = false; |
| 11632 | // Assume the client doesn't want to know about a rebind; |
| 11633 | // we will deal with that later if it asks for one. |
| 11634 | b.intent.doRebind = false; |
| 11635 | s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent()); |
| 11636 | } catch (Exception e) { |
| 11637 | Slog.w(TAG, "Exception when unbinding service " + s.shortName, e); |
| 11638 | serviceDoneExecutingLocked(s, true); |
| 11639 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11640 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11641 | |
| 11642 | if ((c.flags&Context.BIND_AUTO_CREATE) != 0) { |
| 11643 | bringDownServiceLocked(s, false); |
| 11644 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11645 | } |
| 11646 | } |
| 11647 | |
| 11648 | public boolean unbindService(IServiceConnection connection) { |
| 11649 | synchronized (this) { |
| 11650 | IBinder binder = connection.asBinder(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11651 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11652 | ArrayList<ConnectionRecord> clist = mServiceConnections.get(binder); |
| 11653 | if (clist == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11654 | 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] | 11655 | + connection.asBinder()); |
| 11656 | return false; |
| 11657 | } |
| 11658 | |
| 11659 | final long origId = Binder.clearCallingIdentity(); |
| 11660 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11661 | while (clist.size() > 0) { |
| 11662 | ConnectionRecord r = clist.get(0); |
| 11663 | removeConnectionLocked(r, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11664 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11665 | if (r.binding.service.app != null) { |
| 11666 | // This could have made the service less important. |
| 11667 | updateOomAdjLocked(r.binding.service.app); |
| 11668 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11669 | } |
| 11670 | |
| 11671 | Binder.restoreCallingIdentity(origId); |
| 11672 | } |
| 11673 | |
| 11674 | return true; |
| 11675 | } |
| 11676 | |
| 11677 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 11678 | // Refuse possible leaked file descriptors |
| 11679 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11680 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11681 | } |
| 11682 | |
| 11683 | synchronized(this) { |
| 11684 | if (!(token instanceof ServiceRecord)) { |
| 11685 | throw new IllegalArgumentException("Invalid service token"); |
| 11686 | } |
| 11687 | ServiceRecord r = (ServiceRecord)token; |
| 11688 | |
| 11689 | final long origId = Binder.clearCallingIdentity(); |
| 11690 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11691 | if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11692 | + " " + intent + ": " + service); |
| 11693 | if (r != null) { |
| 11694 | Intent.FilterComparison filter |
| 11695 | = new Intent.FilterComparison(intent); |
| 11696 | IntentBindRecord b = r.bindings.get(filter); |
| 11697 | if (b != null && !b.received) { |
| 11698 | b.binder = service; |
| 11699 | b.requested = true; |
| 11700 | b.received = true; |
| 11701 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11702 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11703 | = r.connections.values().iterator(); |
| 11704 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11705 | ArrayList<ConnectionRecord> clist = it.next(); |
| 11706 | for (int i=0; i<clist.size(); i++) { |
| 11707 | ConnectionRecord c = clist.get(i); |
| 11708 | if (!filter.equals(c.binding.intent.intent)) { |
| 11709 | if (DEBUG_SERVICE) Slog.v( |
| 11710 | TAG, "Not publishing to: " + c); |
| 11711 | if (DEBUG_SERVICE) Slog.v( |
| 11712 | TAG, "Bound intent: " + c.binding.intent.intent); |
| 11713 | if (DEBUG_SERVICE) Slog.v( |
| 11714 | TAG, "Published intent: " + intent); |
| 11715 | continue; |
| 11716 | } |
| 11717 | if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c); |
| 11718 | try { |
| 11719 | c.conn.connected(r.name, service); |
| 11720 | } catch (Exception e) { |
| 11721 | Slog.w(TAG, "Failure sending service " + r.name + |
| 11722 | " to connection " + c.conn.asBinder() + |
| 11723 | " (in " + c.binding.client.processName + ")", e); |
| 11724 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11725 | } |
| 11726 | } |
| 11727 | } |
| 11728 | } |
| 11729 | |
| 11730 | serviceDoneExecutingLocked(r, mStoppingServices.contains(r)); |
| 11731 | |
| 11732 | Binder.restoreCallingIdentity(origId); |
| 11733 | } |
| 11734 | } |
| 11735 | } |
| 11736 | |
| 11737 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 11738 | // Refuse possible leaked file descriptors |
| 11739 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11740 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11741 | } |
| 11742 | |
| 11743 | synchronized(this) { |
| 11744 | if (!(token instanceof ServiceRecord)) { |
| 11745 | throw new IllegalArgumentException("Invalid service token"); |
| 11746 | } |
| 11747 | ServiceRecord r = (ServiceRecord)token; |
| 11748 | |
| 11749 | final long origId = Binder.clearCallingIdentity(); |
| 11750 | |
| 11751 | if (r != null) { |
| 11752 | Intent.FilterComparison filter |
| 11753 | = new Intent.FilterComparison(intent); |
| 11754 | IntentBindRecord b = r.bindings.get(filter); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11755 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11756 | + " at " + b + ": apps=" |
| 11757 | + (b != null ? b.apps.size() : 0)); |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11758 | |
| 11759 | boolean inStopping = mStoppingServices.contains(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11760 | if (b != null) { |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11761 | if (b.apps.size() > 0 && !inStopping) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11762 | // Applications have already bound since the last |
| 11763 | // unbind, so just rebind right here. |
| 11764 | requestServiceBindingLocked(r, b, true); |
| 11765 | } else { |
| 11766 | // Note to tell the service the next time there is |
| 11767 | // a new client. |
| 11768 | b.doRebind = true; |
| 11769 | } |
| 11770 | } |
| 11771 | |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11772 | serviceDoneExecutingLocked(r, inStopping); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11773 | |
| 11774 | Binder.restoreCallingIdentity(origId); |
| 11775 | } |
| 11776 | } |
| 11777 | } |
| 11778 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11779 | 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] | 11780 | synchronized(this) { |
| 11781 | if (!(token instanceof ServiceRecord)) { |
| 11782 | throw new IllegalArgumentException("Invalid service token"); |
| 11783 | } |
| 11784 | ServiceRecord r = (ServiceRecord)token; |
| 11785 | boolean inStopping = mStoppingServices.contains(token); |
| 11786 | if (r != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11787 | if (r != token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11788 | Slog.w(TAG, "Done executing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11789 | + " with incorrect token: given " + token |
| 11790 | + ", expected " + r); |
| 11791 | return; |
| 11792 | } |
| 11793 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11794 | if (type == 1) { |
| 11795 | // This is a call from a service start... take care of |
| 11796 | // book-keeping. |
| 11797 | r.callStart = true; |
| 11798 | switch (res) { |
| 11799 | case Service.START_STICKY_COMPATIBILITY: |
| 11800 | case Service.START_STICKY: { |
| 11801 | // We are done with the associated start arguments. |
| 11802 | r.findDeliveredStart(startId, true); |
| 11803 | // Don't stop if killed. |
| 11804 | r.stopIfKilled = false; |
| 11805 | break; |
| 11806 | } |
| 11807 | case Service.START_NOT_STICKY: { |
| 11808 | // We are done with the associated start arguments. |
| 11809 | r.findDeliveredStart(startId, true); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11810 | if (r.getLastStartId() == startId) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11811 | // There is no more work, and this service |
| 11812 | // doesn't want to hang around if killed. |
| 11813 | r.stopIfKilled = true; |
| 11814 | } |
| 11815 | break; |
| 11816 | } |
| 11817 | case Service.START_REDELIVER_INTENT: { |
| 11818 | // We'll keep this item until they explicitly |
| 11819 | // call stop for it, but keep track of the fact |
| 11820 | // that it was delivered. |
| 11821 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 11822 | if (si != null) { |
| 11823 | si.deliveryCount = 0; |
| 11824 | si.doneExecutingCount++; |
| 11825 | // Don't stop if killed. |
| 11826 | r.stopIfKilled = true; |
| 11827 | } |
| 11828 | break; |
| 11829 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11830 | case Service.START_TASK_REMOVED_COMPLETE: { |
| 11831 | // Special processing for onTaskRemoved(). Don't |
| 11832 | // impact normal onStartCommand() processing. |
| 11833 | r.findDeliveredStart(startId, true); |
| 11834 | break; |
| 11835 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11836 | default: |
| 11837 | throw new IllegalArgumentException( |
| 11838 | "Unknown service start result: " + res); |
| 11839 | } |
| 11840 | if (res == Service.START_STICKY_COMPATIBILITY) { |
| 11841 | r.callStart = false; |
| 11842 | } |
| 11843 | } |
| 11844 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11845 | final long origId = Binder.clearCallingIdentity(); |
| 11846 | serviceDoneExecutingLocked(r, inStopping); |
| 11847 | Binder.restoreCallingIdentity(origId); |
| 11848 | } else { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11849 | Slog.w(TAG, "Done executing unknown service from pid " |
| 11850 | + Binder.getCallingPid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11851 | } |
| 11852 | } |
| 11853 | } |
| 11854 | |
| 11855 | public void serviceDoneExecutingLocked(ServiceRecord r, boolean inStopping) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11856 | if (DEBUG_SERVICE) Slog.v(TAG, "<<< DONE EXECUTING " + r |
| 11857 | + ": nesting=" + r.executeNesting |
| 11858 | + ", inStopping=" + inStopping + ", app=" + r.app); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11859 | 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] | 11860 | r.executeNesting--; |
| 11861 | if (r.executeNesting <= 0 && r.app != null) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11862 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 11863 | "Nesting at 0 of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11864 | r.app.executingServices.remove(r); |
| 11865 | if (r.app.executingServices.size() == 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11866 | if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG, |
| 11867 | "No more executingServices of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11868 | mHandler.removeMessages(SERVICE_TIMEOUT_MSG, r.app); |
| 11869 | } |
| 11870 | if (inStopping) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11871 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 11872 | "doneExecuting remove stopping " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11873 | mStoppingServices.remove(r); |
Mattias Petersson | 3996b41 | 2010-10-27 09:32:51 +0200 | [diff] [blame] | 11874 | r.bindings.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11875 | } |
| 11876 | updateOomAdjLocked(r.app); |
| 11877 | } |
| 11878 | } |
| 11879 | |
| 11880 | void serviceTimeout(ProcessRecord proc) { |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11881 | String anrMessage = null; |
| 11882 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11883 | synchronized(this) { |
| 11884 | if (proc.executingServices.size() == 0 || proc.thread == null) { |
| 11885 | return; |
| 11886 | } |
| 11887 | long maxTime = SystemClock.uptimeMillis() - SERVICE_TIMEOUT; |
| 11888 | Iterator<ServiceRecord> it = proc.executingServices.iterator(); |
| 11889 | ServiceRecord timeout = null; |
| 11890 | long nextTime = 0; |
| 11891 | while (it.hasNext()) { |
| 11892 | ServiceRecord sr = it.next(); |
| 11893 | if (sr.executingStart < maxTime) { |
| 11894 | timeout = sr; |
| 11895 | break; |
| 11896 | } |
| 11897 | if (sr.executingStart > nextTime) { |
| 11898 | nextTime = sr.executingStart; |
| 11899 | } |
| 11900 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11901 | if (timeout != null && mLruProcesses.contains(proc)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11902 | Slog.w(TAG, "Timeout executing service: " + timeout); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11903 | anrMessage = "Executing service " + timeout.shortName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11904 | } else { |
| 11905 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 11906 | msg.obj = proc; |
| 11907 | mHandler.sendMessageAtTime(msg, nextTime+SERVICE_TIMEOUT); |
| 11908 | } |
| 11909 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11910 | |
| 11911 | if (anrMessage != null) { |
| 11912 | appNotResponding(proc, null, null, anrMessage); |
| 11913 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11914 | } |
| 11915 | |
| 11916 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11917 | // BACKUP AND RESTORE |
| 11918 | // ========================================================= |
| 11919 | |
| 11920 | // Cause the target app to be launched if necessary and its backup agent |
| 11921 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 11922 | // activity manager to announce its creation. |
| 11923 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11924 | if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11925 | enforceCallingPermission("android.permission.BACKUP", "startBackupAgent"); |
| 11926 | |
| 11927 | synchronized(this) { |
| 11928 | // !!! TODO: currently no check here that we're already bound |
| 11929 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 11930 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 11931 | synchronized (stats) { |
| 11932 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 11933 | } |
| 11934 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11935 | // Backup agent is now in use, its package can't be stopped. |
| 11936 | try { |
| 11937 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 11938 | app.packageName, false); |
| 11939 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 11940 | } catch (IllegalArgumentException e) { |
| 11941 | Slog.w(TAG, "Failed trying to unstop package " |
| 11942 | + app.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11943 | } |
| 11944 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11945 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 11946 | ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) |
| 11947 | ? new ComponentName(app.packageName, app.backupAgentName) |
| 11948 | : new ComponentName("android", "FullBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11949 | // startProcessLocked() returns existing proc's record if it's already running |
| 11950 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 11951 | false, 0, "backup", hostingName, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11952 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11953 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11954 | return false; |
| 11955 | } |
| 11956 | |
| 11957 | r.app = proc; |
| 11958 | mBackupTarget = r; |
| 11959 | mBackupAppName = app.packageName; |
| 11960 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11961 | // Try not to kill the process during backup |
| 11962 | updateOomAdjLocked(proc); |
| 11963 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11964 | // If the process is already attached, schedule the creation of the backup agent now. |
| 11965 | // If it is not yet live, this will be done when it attaches to the framework. |
| 11966 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11967 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11968 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11969 | proc.thread.scheduleCreateBackupAgent(app, |
| 11970 | compatibilityInfoForPackageLocked(app), backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11971 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 11972 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11973 | } |
| 11974 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11975 | 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] | 11976 | } |
| 11977 | // Invariants: at this point, the target app process exists and the application |
| 11978 | // is either already running or in the process of coming up. mBackupTarget and |
| 11979 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 11980 | // know that it's scheduled for a backup-agent operation. |
| 11981 | } |
| 11982 | |
| 11983 | return true; |
| 11984 | } |
| 11985 | |
| 11986 | // A backup agent has just come up |
| 11987 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11988 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11989 | + " = " + agent); |
| 11990 | |
| 11991 | synchronized(this) { |
| 11992 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11993 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11994 | return; |
| 11995 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11996 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11997 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11998 | long oldIdent = Binder.clearCallingIdentity(); |
| 11999 | try { |
| 12000 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 12001 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 12002 | bm.agentConnected(agentPackageName, agent); |
| 12003 | } catch (RemoteException e) { |
| 12004 | // can't happen; the backup manager service is local |
| 12005 | } catch (Exception e) { |
| 12006 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 12007 | e.printStackTrace(); |
| 12008 | } finally { |
| 12009 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12010 | } |
| 12011 | } |
| 12012 | |
| 12013 | // done with this agent |
| 12014 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12015 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 12016 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12017 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 12018 | return; |
| 12019 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12020 | |
| 12021 | synchronized(this) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 12022 | if (mBackupAppName == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12023 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 12024 | return; |
| 12025 | } |
| 12026 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12027 | if (!mBackupAppName.equals(appInfo.packageName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12028 | 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] | 12029 | return; |
| 12030 | } |
| 12031 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12032 | ProcessRecord proc = mBackupTarget.app; |
| 12033 | mBackupTarget = null; |
| 12034 | mBackupAppName = null; |
| 12035 | |
| 12036 | // Not backing this app up any more; reset its OOM adjustment |
| 12037 | updateOomAdjLocked(proc); |
| 12038 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 12039 | // If the app crashed during backup, 'thread' will be null here |
| 12040 | if (proc.thread != null) { |
| 12041 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 12042 | proc.thread.scheduleDestroyBackupAgent(appInfo, |
| 12043 | compatibilityInfoForPackageLocked(appInfo)); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 12044 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12045 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 12046 | e.printStackTrace(); |
| 12047 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12048 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12049 | } |
| 12050 | } |
| 12051 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12052 | // BROADCASTS |
| 12053 | // ========================================================= |
| 12054 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12055 | private final List getStickiesLocked(String action, IntentFilter filter, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12056 | List cur) { |
| 12057 | final ContentResolver resolver = mContext.getContentResolver(); |
| 12058 | final ArrayList<Intent> list = mStickyBroadcasts.get(action); |
| 12059 | if (list == null) { |
| 12060 | return cur; |
| 12061 | } |
| 12062 | int N = list.size(); |
| 12063 | for (int i=0; i<N; i++) { |
| 12064 | Intent intent = list.get(i); |
| 12065 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 12066 | if (cur == null) { |
| 12067 | cur = new ArrayList<Intent>(); |
| 12068 | } |
| 12069 | cur.add(intent); |
| 12070 | } |
| 12071 | } |
| 12072 | return cur; |
| 12073 | } |
| 12074 | |
| 12075 | private final void scheduleBroadcastsLocked() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12076 | if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts: current=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12077 | + mBroadcastsScheduled); |
| 12078 | |
| 12079 | if (mBroadcastsScheduled) { |
| 12080 | return; |
| 12081 | } |
| 12082 | mHandler.sendEmptyMessage(BROADCAST_INTENT_MSG); |
| 12083 | mBroadcastsScheduled = true; |
| 12084 | } |
| 12085 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 12086 | public Intent registerReceiver(IApplicationThread caller, String callerPackage, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12087 | IIntentReceiver receiver, IntentFilter filter, String permission) { |
| 12088 | synchronized(this) { |
| 12089 | ProcessRecord callerApp = null; |
| 12090 | if (caller != null) { |
| 12091 | callerApp = getRecordForAppLocked(caller); |
| 12092 | if (callerApp == null) { |
| 12093 | throw new SecurityException( |
| 12094 | "Unable to find app for caller " + caller |
| 12095 | + " (pid=" + Binder.getCallingPid() |
| 12096 | + ") when registering receiver " + receiver); |
| 12097 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 12098 | if (callerApp.info.uid != Process.SYSTEM_UID && |
| 12099 | !callerApp.pkgList.contains(callerPackage)) { |
| 12100 | throw new SecurityException("Given caller package " + callerPackage |
| 12101 | + " is not running in process " + callerApp); |
| 12102 | } |
| 12103 | } else { |
| 12104 | callerPackage = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12105 | } |
| 12106 | |
| 12107 | List allSticky = null; |
| 12108 | |
| 12109 | // Look for any matching sticky broadcasts... |
| 12110 | Iterator actions = filter.actionsIterator(); |
| 12111 | if (actions != null) { |
| 12112 | while (actions.hasNext()) { |
| 12113 | String action = (String)actions.next(); |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12114 | allSticky = getStickiesLocked(action, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12115 | } |
| 12116 | } else { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12117 | allSticky = getStickiesLocked(null, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12118 | } |
| 12119 | |
| 12120 | // The first sticky in the list is returned directly back to |
| 12121 | // the client. |
| 12122 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 12123 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12124 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12125 | + ": " + sticky); |
| 12126 | |
| 12127 | if (receiver == null) { |
| 12128 | return sticky; |
| 12129 | } |
| 12130 | |
| 12131 | ReceiverList rl |
| 12132 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 12133 | if (rl == null) { |
| 12134 | rl = new ReceiverList(this, callerApp, |
| 12135 | Binder.getCallingPid(), |
| 12136 | Binder.getCallingUid(), receiver); |
| 12137 | if (rl.app != null) { |
| 12138 | rl.app.receivers.add(rl); |
| 12139 | } else { |
| 12140 | try { |
| 12141 | receiver.asBinder().linkToDeath(rl, 0); |
| 12142 | } catch (RemoteException e) { |
| 12143 | return sticky; |
| 12144 | } |
| 12145 | rl.linkedToDeath = true; |
| 12146 | } |
| 12147 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
| 12148 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 12149 | BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, permission); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12150 | rl.add(bf); |
| 12151 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12152 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12153 | } |
| 12154 | mReceiverResolver.addFilter(bf); |
| 12155 | |
| 12156 | // Enqueue broadcasts for all existing stickies that match |
| 12157 | // this filter. |
| 12158 | if (allSticky != null) { |
| 12159 | ArrayList receivers = new ArrayList(); |
| 12160 | receivers.add(bf); |
| 12161 | |
| 12162 | int N = allSticky.size(); |
| 12163 | for (int i=0; i<N; i++) { |
| 12164 | Intent intent = (Intent)allSticky.get(i); |
| 12165 | BroadcastRecord r = new BroadcastRecord(intent, null, |
| 12166 | null, -1, -1, null, receivers, null, 0, null, null, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12167 | false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12168 | if (mParallelBroadcasts.size() == 0) { |
| 12169 | scheduleBroadcastsLocked(); |
| 12170 | } |
| 12171 | mParallelBroadcasts.add(r); |
| 12172 | } |
| 12173 | } |
| 12174 | |
| 12175 | return sticky; |
| 12176 | } |
| 12177 | } |
| 12178 | |
| 12179 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12180 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12181 | |
| 12182 | boolean doNext = false; |
| 12183 | |
| 12184 | synchronized(this) { |
| 12185 | ReceiverList rl |
| 12186 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 12187 | if (rl != null) { |
| 12188 | if (rl.curBroadcast != null) { |
| 12189 | BroadcastRecord r = rl.curBroadcast; |
| 12190 | doNext = finishReceiverLocked( |
| 12191 | receiver.asBinder(), r.resultCode, r.resultData, |
| 12192 | r.resultExtras, r.resultAbort, true); |
| 12193 | } |
| 12194 | |
| 12195 | if (rl.app != null) { |
| 12196 | rl.app.receivers.remove(rl); |
| 12197 | } |
| 12198 | removeReceiverLocked(rl); |
| 12199 | if (rl.linkedToDeath) { |
| 12200 | rl.linkedToDeath = false; |
| 12201 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 12202 | } |
| 12203 | } |
| 12204 | } |
| 12205 | |
| 12206 | if (!doNext) { |
| 12207 | return; |
| 12208 | } |
| 12209 | |
| 12210 | final long origId = Binder.clearCallingIdentity(); |
| 12211 | processNextBroadcast(false); |
| 12212 | trimApplications(); |
| 12213 | Binder.restoreCallingIdentity(origId); |
| 12214 | } |
| 12215 | |
| 12216 | void removeReceiverLocked(ReceiverList rl) { |
| 12217 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 12218 | int N = rl.size(); |
| 12219 | for (int i=0; i<N; i++) { |
| 12220 | mReceiverResolver.removeFilter(rl.get(i)); |
| 12221 | } |
| 12222 | } |
| 12223 | |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 12224 | private final void sendPackageBroadcastLocked(int cmd, String[] packages) { |
| 12225 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 12226 | ProcessRecord r = mLruProcesses.get(i); |
| 12227 | if (r.thread != null) { |
| 12228 | try { |
| 12229 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 12230 | } catch (RemoteException ex) { |
| 12231 | } |
| 12232 | } |
| 12233 | } |
| 12234 | } |
| 12235 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12236 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 12237 | String callerPackage, Intent intent, String resolvedType, |
| 12238 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 12239 | Bundle map, String requiredPermission, |
| 12240 | boolean ordered, boolean sticky, int callingPid, int callingUid) { |
| 12241 | intent = new Intent(intent); |
| 12242 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 12243 | // By default broadcasts do not go to stopped apps. |
| 12244 | intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES); |
| 12245 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12246 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12247 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
| 12248 | + " ordered=" + ordered); |
| 12249 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12250 | 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] | 12251 | } |
| 12252 | |
| 12253 | // Handle special intents: if this broadcast is from the package |
| 12254 | // manager about a package being removed, we need to remove all of |
| 12255 | // its activities from the history stack. |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12256 | final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12257 | intent.getAction()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12258 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 12259 | || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 12260 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12261 | || uidRemoved) { |
| 12262 | if (checkComponentPermission( |
| 12263 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12264 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12265 | == PackageManager.PERMISSION_GRANTED) { |
| 12266 | if (uidRemoved) { |
| 12267 | final Bundle intentExtras = intent.getExtras(); |
| 12268 | final int uid = intentExtras != null |
| 12269 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 12270 | if (uid >= 0) { |
| 12271 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 12272 | synchronized (bs) { |
| 12273 | bs.removeUidStatsLocked(uid); |
| 12274 | } |
| 12275 | } |
| 12276 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12277 | // If resources are unvailble just force stop all |
| 12278 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 12279 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12280 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 12281 | if (list != null && (list.length > 0)) { |
| 12282 | for (String pkg : list) { |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12283 | forceStopPackageLocked(pkg, -1, false, true, true, false); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12284 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 12285 | sendPackageBroadcastLocked( |
| 12286 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12287 | } |
| 12288 | } else { |
| 12289 | Uri data = intent.getData(); |
| 12290 | String ssp; |
| 12291 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 12292 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 12293 | forceStopPackageLocked(ssp, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12294 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true, false); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 12295 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12296 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 12297 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
| 12298 | new String[] {ssp}); |
| 12299 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12300 | } |
| 12301 | } |
| 12302 | } |
| 12303 | } else { |
| 12304 | String msg = "Permission Denial: " + intent.getAction() |
| 12305 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 12306 | + ", uid=" + callingUid + ")" |
| 12307 | + " requires " |
| 12308 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12309 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12310 | throw new SecurityException(msg); |
| 12311 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12312 | |
| 12313 | // Special case for adding a package: by default turn on compatibility |
| 12314 | // mode. |
| 12315 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 12316 | Uri data = intent.getData(); |
| 12317 | String ssp; |
| 12318 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 12319 | mCompatModePackages.handlePackageAddedLocked(ssp, |
| 12320 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12321 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12322 | } |
| 12323 | |
| 12324 | /* |
| 12325 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 12326 | * of all currently running processes. This message will get queued up before the broadcast |
| 12327 | * happens. |
| 12328 | */ |
| 12329 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 12330 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 12331 | } |
| 12332 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 12333 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 12334 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 12335 | } |
| 12336 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 12337 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 12338 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 12339 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 12340 | } |
| 12341 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12342 | /* |
| 12343 | * Prevent non-system code (defined here to be non-persistent |
| 12344 | * processes) from sending protected broadcasts. |
| 12345 | */ |
| 12346 | if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID |
| 12347 | || callingUid == Process.SHELL_UID || callingUid == 0) { |
| 12348 | // Always okay. |
| 12349 | } else if (callerApp == null || !callerApp.persistent) { |
| 12350 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12351 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12352 | intent.getAction())) { |
| 12353 | String msg = "Permission Denial: not allowed to send broadcast " |
| 12354 | + intent.getAction() + " from pid=" |
| 12355 | + callingPid + ", uid=" + callingUid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12356 | Slog.w(TAG, msg); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12357 | throw new SecurityException(msg); |
| 12358 | } |
| 12359 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12360 | Slog.w(TAG, "Remote exception", e); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12361 | return BROADCAST_SUCCESS; |
| 12362 | } |
| 12363 | } |
| 12364 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12365 | // Add to the sticky list if requested. |
| 12366 | if (sticky) { |
| 12367 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 12368 | callingPid, callingUid) |
| 12369 | != PackageManager.PERMISSION_GRANTED) { |
| 12370 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 12371 | + callingPid + ", uid=" + callingUid |
| 12372 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12373 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12374 | throw new SecurityException(msg); |
| 12375 | } |
| 12376 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12377 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12378 | + " and enforce permission " + requiredPermission); |
| 12379 | return BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
| 12380 | } |
| 12381 | if (intent.getComponent() != null) { |
| 12382 | throw new SecurityException( |
| 12383 | "Sticky broadcasts can't target a specific component"); |
| 12384 | } |
| 12385 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 12386 | if (list == null) { |
| 12387 | list = new ArrayList<Intent>(); |
| 12388 | mStickyBroadcasts.put(intent.getAction(), list); |
| 12389 | } |
| 12390 | int N = list.size(); |
| 12391 | int i; |
| 12392 | for (i=0; i<N; i++) { |
| 12393 | if (intent.filterEquals(list.get(i))) { |
| 12394 | // This sticky already exists, replace it. |
| 12395 | list.set(i, new Intent(intent)); |
| 12396 | break; |
| 12397 | } |
| 12398 | } |
| 12399 | if (i >= N) { |
| 12400 | list.add(new Intent(intent)); |
| 12401 | } |
| 12402 | } |
| 12403 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12404 | // Figure out who all will receive this broadcast. |
| 12405 | List receivers = null; |
| 12406 | List<BroadcastFilter> registeredReceivers = null; |
| 12407 | try { |
| 12408 | if (intent.getComponent() != null) { |
| 12409 | // Broadcast is going to one specific receiver class... |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12410 | ActivityInfo ai = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12411 | getReceiverInfo(intent.getComponent(), STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12412 | if (ai != null) { |
| 12413 | receivers = new ArrayList(); |
| 12414 | ResolveInfo ri = new ResolveInfo(); |
| 12415 | ri.activityInfo = ai; |
| 12416 | receivers.add(ri); |
| 12417 | } |
| 12418 | } else { |
| 12419 | // Need to resolve the intent to interested receivers... |
| 12420 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 12421 | == 0) { |
| 12422 | receivers = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12423 | AppGlobals.getPackageManager().queryIntentReceivers( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12424 | intent, resolvedType, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12425 | } |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 12426 | registeredReceivers = mReceiverResolver.queryIntent(intent, resolvedType, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12427 | } |
| 12428 | } catch (RemoteException ex) { |
| 12429 | // pm is in same process, this will never happen. |
| 12430 | } |
| 12431 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12432 | final boolean replacePending = |
| 12433 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 12434 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12435 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12436 | + " replacePending=" + replacePending); |
| 12437 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12438 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 12439 | if (!ordered && NR > 0) { |
| 12440 | // If we are not serializing this broadcast, then send the |
| 12441 | // registered receivers separately so they don't wait for the |
| 12442 | // components to be launched. |
| 12443 | BroadcastRecord r = new BroadcastRecord(intent, callerApp, |
| 12444 | callerPackage, callingPid, callingUid, requiredPermission, |
| 12445 | registeredReceivers, resultTo, resultCode, resultData, map, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12446 | ordered, sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12447 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12448 | TAG, "Enqueueing parallel broadcast " + r |
| 12449 | + ": prev had " + mParallelBroadcasts.size()); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12450 | boolean replaced = false; |
| 12451 | if (replacePending) { |
| 12452 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 12453 | if (intent.filterEquals(mParallelBroadcasts.get(i).intent)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12454 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12455 | "***** DROPPING PARALLEL: " + intent); |
| 12456 | mParallelBroadcasts.set(i, r); |
| 12457 | replaced = true; |
| 12458 | break; |
| 12459 | } |
| 12460 | } |
| 12461 | } |
| 12462 | if (!replaced) { |
| 12463 | mParallelBroadcasts.add(r); |
| 12464 | scheduleBroadcastsLocked(); |
| 12465 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12466 | registeredReceivers = null; |
| 12467 | NR = 0; |
| 12468 | } |
| 12469 | |
| 12470 | // Merge into one list. |
| 12471 | int ir = 0; |
| 12472 | if (receivers != null) { |
| 12473 | // A special case for PACKAGE_ADDED: do not allow the package |
| 12474 | // being added to see this broadcast. This prevents them from |
| 12475 | // using this as a back door to get run as soon as they are |
| 12476 | // installed. Maybe in the future we want to have a special install |
| 12477 | // broadcast or such for apps, but we'd like to deliberately make |
| 12478 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12479 | String skipPackages[] = null; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12480 | if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 12481 | || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 12482 | || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12483 | Uri data = intent.getData(); |
| 12484 | if (data != null) { |
| 12485 | String pkgName = data.getSchemeSpecificPart(); |
| 12486 | if (pkgName != null) { |
| 12487 | skipPackages = new String[] { pkgName }; |
| 12488 | } |
| 12489 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12490 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12491 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 12492 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12493 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 12494 | for (String skipPackage : skipPackages) { |
| 12495 | if (skipPackage != null) { |
| 12496 | int NT = receivers.size(); |
| 12497 | for (int it=0; it<NT; it++) { |
| 12498 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 12499 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 12500 | receivers.remove(it); |
| 12501 | it--; |
| 12502 | NT--; |
| 12503 | } |
| 12504 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12505 | } |
| 12506 | } |
| 12507 | } |
| 12508 | |
| 12509 | int NT = receivers != null ? receivers.size() : 0; |
| 12510 | int it = 0; |
| 12511 | ResolveInfo curt = null; |
| 12512 | BroadcastFilter curr = null; |
| 12513 | while (it < NT && ir < NR) { |
| 12514 | if (curt == null) { |
| 12515 | curt = (ResolveInfo)receivers.get(it); |
| 12516 | } |
| 12517 | if (curr == null) { |
| 12518 | curr = registeredReceivers.get(ir); |
| 12519 | } |
| 12520 | if (curr.getPriority() >= curt.priority) { |
| 12521 | // Insert this broadcast record into the final list. |
| 12522 | receivers.add(it, curr); |
| 12523 | ir++; |
| 12524 | curr = null; |
| 12525 | it++; |
| 12526 | NT++; |
| 12527 | } else { |
| 12528 | // Skip to the next ResolveInfo in the final list. |
| 12529 | it++; |
| 12530 | curt = null; |
| 12531 | } |
| 12532 | } |
| 12533 | } |
| 12534 | while (ir < NR) { |
| 12535 | if (receivers == null) { |
| 12536 | receivers = new ArrayList(); |
| 12537 | } |
| 12538 | receivers.add(registeredReceivers.get(ir)); |
| 12539 | ir++; |
| 12540 | } |
| 12541 | |
| 12542 | if ((receivers != null && receivers.size() > 0) |
| 12543 | || resultTo != null) { |
| 12544 | BroadcastRecord r = new BroadcastRecord(intent, callerApp, |
| 12545 | callerPackage, callingPid, callingUid, requiredPermission, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12546 | receivers, resultTo, resultCode, resultData, map, ordered, |
| 12547 | sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12548 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12549 | TAG, "Enqueueing ordered broadcast " + r |
| 12550 | + ": prev had " + mOrderedBroadcasts.size()); |
| 12551 | if (DEBUG_BROADCAST) { |
| 12552 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12553 | 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] | 12554 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12555 | boolean replaced = false; |
| 12556 | if (replacePending) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12557 | for (int i=mOrderedBroadcasts.size()-1; i>0; i--) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12558 | if (intent.filterEquals(mOrderedBroadcasts.get(i).intent)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12559 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12560 | "***** DROPPING ORDERED: " + intent); |
| 12561 | mOrderedBroadcasts.set(i, r); |
| 12562 | replaced = true; |
| 12563 | break; |
| 12564 | } |
| 12565 | } |
| 12566 | } |
| 12567 | if (!replaced) { |
| 12568 | mOrderedBroadcasts.add(r); |
| 12569 | scheduleBroadcastsLocked(); |
| 12570 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12571 | } |
| 12572 | |
| 12573 | return BROADCAST_SUCCESS; |
| 12574 | } |
| 12575 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12576 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12577 | // Refuse possible leaked file descriptors |
| 12578 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 12579 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12580 | } |
| 12581 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12582 | int flags = intent.getFlags(); |
| 12583 | |
| 12584 | if (!mProcessesReady) { |
| 12585 | // if the caller really truly claims to know what they're doing, go |
| 12586 | // ahead and allow the broadcast without launching any receivers |
| 12587 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 12588 | intent = new Intent(intent); |
| 12589 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 12590 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 12591 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 12592 | + " before boot completion"); |
| 12593 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 12594 | } |
| 12595 | } |
| 12596 | |
| 12597 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 12598 | throw new IllegalArgumentException( |
| 12599 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 12600 | } |
| 12601 | |
| 12602 | return intent; |
| 12603 | } |
| 12604 | |
| 12605 | public final int broadcastIntent(IApplicationThread caller, |
| 12606 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 12607 | int resultCode, String resultData, Bundle map, |
| 12608 | String requiredPermission, boolean serialized, boolean sticky) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12609 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12610 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 12611 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12612 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 12613 | final int callingPid = Binder.getCallingPid(); |
| 12614 | final int callingUid = Binder.getCallingUid(); |
| 12615 | final long origId = Binder.clearCallingIdentity(); |
| 12616 | int res = broadcastIntentLocked(callerApp, |
| 12617 | callerApp != null ? callerApp.info.packageName : null, |
| 12618 | intent, resolvedType, resultTo, |
| 12619 | resultCode, resultData, map, requiredPermission, serialized, |
| 12620 | sticky, callingPid, callingUid); |
| 12621 | Binder.restoreCallingIdentity(origId); |
| 12622 | return res; |
| 12623 | } |
| 12624 | } |
| 12625 | |
| 12626 | int broadcastIntentInPackage(String packageName, int uid, |
| 12627 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 12628 | int resultCode, String resultData, Bundle map, |
| 12629 | String requiredPermission, boolean serialized, boolean sticky) { |
| 12630 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12631 | intent = verifyBroadcastLocked(intent); |
| 12632 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12633 | final long origId = Binder.clearCallingIdentity(); |
| 12634 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 12635 | resultTo, resultCode, resultData, map, requiredPermission, |
| 12636 | serialized, sticky, -1, uid); |
| 12637 | Binder.restoreCallingIdentity(origId); |
| 12638 | return res; |
| 12639 | } |
| 12640 | } |
| 12641 | |
| 12642 | public final void unbroadcastIntent(IApplicationThread caller, |
| 12643 | Intent intent) { |
| 12644 | // Refuse possible leaked file descriptors |
| 12645 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 12646 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12647 | } |
| 12648 | |
| 12649 | synchronized(this) { |
| 12650 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 12651 | != PackageManager.PERMISSION_GRANTED) { |
| 12652 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 12653 | + Binder.getCallingPid() |
| 12654 | + ", uid=" + Binder.getCallingUid() |
| 12655 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12656 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12657 | throw new SecurityException(msg); |
| 12658 | } |
| 12659 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 12660 | if (list != null) { |
| 12661 | int N = list.size(); |
| 12662 | int i; |
| 12663 | for (i=0; i<N; i++) { |
| 12664 | if (intent.filterEquals(list.get(i))) { |
| 12665 | list.remove(i); |
| 12666 | break; |
| 12667 | } |
| 12668 | } |
| 12669 | } |
| 12670 | } |
| 12671 | } |
| 12672 | |
| 12673 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 12674 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 12675 | boolean explicit) { |
| 12676 | if (mOrderedBroadcasts.size() == 0) { |
| 12677 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12678 | Slog.w(TAG, "finishReceiver called but no pending broadcasts"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12679 | } |
| 12680 | return false; |
| 12681 | } |
| 12682 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 12683 | if (r.receiver == null) { |
| 12684 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12685 | Slog.w(TAG, "finishReceiver called but none active"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12686 | } |
| 12687 | return false; |
| 12688 | } |
| 12689 | if (r.receiver != receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12690 | 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] | 12691 | return false; |
| 12692 | } |
| 12693 | int state = r.state; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12694 | r.state = BroadcastRecord.IDLE; |
| 12695 | if (state == BroadcastRecord.IDLE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12696 | if (explicit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12697 | Slog.w(TAG, "finishReceiver called but state is IDLE"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12698 | } |
| 12699 | } |
| 12700 | r.receiver = null; |
| 12701 | r.intent.setComponent(null); |
| 12702 | if (r.curApp != null) { |
| 12703 | r.curApp.curReceiver = null; |
| 12704 | } |
| 12705 | if (r.curFilter != null) { |
| 12706 | r.curFilter.receiverList.curBroadcast = null; |
| 12707 | } |
| 12708 | r.curFilter = null; |
| 12709 | r.curApp = null; |
| 12710 | r.curComponent = null; |
| 12711 | r.curReceiver = null; |
| 12712 | mPendingBroadcast = null; |
| 12713 | |
| 12714 | r.resultCode = resultCode; |
| 12715 | r.resultData = resultData; |
| 12716 | r.resultExtras = resultExtras; |
| 12717 | r.resultAbort = resultAbort; |
| 12718 | |
| 12719 | // We will process the next receiver right now if this is finishing |
| 12720 | // an app receiver (which is always asynchronous) or after we have |
| 12721 | // come back from calling a receiver. |
| 12722 | return state == BroadcastRecord.APP_RECEIVE |
| 12723 | || state == BroadcastRecord.CALL_DONE_RECEIVE; |
| 12724 | } |
| 12725 | |
| 12726 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 12727 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12728 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12729 | |
| 12730 | // Refuse possible leaked file descriptors |
| 12731 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 12732 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12733 | } |
| 12734 | |
| 12735 | boolean doNext; |
| 12736 | |
| 12737 | final long origId = Binder.clearCallingIdentity(); |
| 12738 | |
| 12739 | synchronized(this) { |
| 12740 | doNext = finishReceiverLocked( |
| 12741 | who, resultCode, resultData, resultExtras, resultAbort, true); |
| 12742 | } |
| 12743 | |
| 12744 | if (doNext) { |
| 12745 | processNextBroadcast(false); |
| 12746 | } |
| 12747 | trimApplications(); |
| 12748 | |
| 12749 | Binder.restoreCallingIdentity(origId); |
| 12750 | } |
| 12751 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12752 | private final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12753 | if (r.nextReceiver > 0) { |
| 12754 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 12755 | if (curReceiver instanceof BroadcastFilter) { |
| 12756 | BroadcastFilter bf = (BroadcastFilter) curReceiver; |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12757 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12758 | System.identityHashCode(r), |
| 12759 | r.intent.getAction(), |
| 12760 | r.nextReceiver - 1, |
| 12761 | System.identityHashCode(bf)); |
| 12762 | } else { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12763 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12764 | System.identityHashCode(r), |
| 12765 | r.intent.getAction(), |
| 12766 | r.nextReceiver - 1, |
| 12767 | ((ResolveInfo)curReceiver).toString()); |
| 12768 | } |
| 12769 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12770 | 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] | 12771 | + r); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12772 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12773 | System.identityHashCode(r), |
| 12774 | r.intent.getAction(), |
| 12775 | r.nextReceiver, |
| 12776 | "NONE"); |
| 12777 | } |
| 12778 | } |
| 12779 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12780 | private final void setBroadcastTimeoutLocked(long timeoutTime) { |
| 12781 | if (! mPendingBroadcastTimeoutMessage) { |
| 12782 | Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG); |
| 12783 | mHandler.sendMessageAtTime(msg, timeoutTime); |
| 12784 | mPendingBroadcastTimeoutMessage = true; |
| 12785 | } |
| 12786 | } |
| 12787 | |
| 12788 | private final void cancelBroadcastTimeoutLocked() { |
| 12789 | if (mPendingBroadcastTimeoutMessage) { |
| 12790 | mHandler.removeMessages(BROADCAST_TIMEOUT_MSG); |
| 12791 | mPendingBroadcastTimeoutMessage = false; |
| 12792 | } |
| 12793 | } |
| 12794 | |
| 12795 | private final void broadcastTimeoutLocked(boolean fromMsg) { |
| 12796 | if (fromMsg) { |
| 12797 | mPendingBroadcastTimeoutMessage = false; |
| 12798 | } |
| 12799 | |
| 12800 | if (mOrderedBroadcasts.size() == 0) { |
| 12801 | return; |
| 12802 | } |
| 12803 | |
| 12804 | long now = SystemClock.uptimeMillis(); |
| 12805 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 12806 | if (fromMsg) { |
| 12807 | if (mDidDexOpt) { |
| 12808 | // Delay timeouts until dexopt finishes. |
| 12809 | mDidDexOpt = false; |
| 12810 | long timeoutTime = SystemClock.uptimeMillis() + BROADCAST_TIMEOUT; |
| 12811 | setBroadcastTimeoutLocked(timeoutTime); |
| 12812 | return; |
| 12813 | } |
| 12814 | if (! mProcessesReady) { |
| 12815 | // Only process broadcast timeouts if the system is ready. That way |
| 12816 | // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended |
| 12817 | // to do heavy lifting for system up. |
| 12818 | return; |
| 12819 | } |
| 12820 | |
| 12821 | long timeoutTime = r.receiverTime + BROADCAST_TIMEOUT; |
| 12822 | if (timeoutTime > now) { |
| 12823 | // We can observe premature timeouts because we do not cancel and reset the |
| 12824 | // broadcast timeout message after each receiver finishes. Instead, we set up |
| 12825 | // an initial timeout then kick it down the road a little further as needed |
| 12826 | // when it expires. |
| 12827 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12828 | "Premature timeout @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for " |
| 12829 | + timeoutTime); |
| 12830 | setBroadcastTimeoutLocked(timeoutTime); |
| 12831 | return; |
| 12832 | } |
| 12833 | } |
| 12834 | |
| 12835 | Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r.receiver |
| 12836 | + ", started " + (now - r.receiverTime) + "ms ago"); |
| 12837 | r.receiverTime = now; |
| 12838 | r.anrCount++; |
| 12839 | |
| 12840 | // Current receiver has passed its expiration date. |
| 12841 | if (r.nextReceiver <= 0) { |
| 12842 | Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0"); |
| 12843 | return; |
| 12844 | } |
| 12845 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12846 | ProcessRecord app = null; |
| 12847 | String anrMessage = null; |
| 12848 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12849 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 12850 | Slog.w(TAG, "Receiver during timeout: " + curReceiver); |
| 12851 | logBroadcastReceiverDiscardLocked(r); |
| 12852 | if (curReceiver instanceof BroadcastFilter) { |
| 12853 | BroadcastFilter bf = (BroadcastFilter)curReceiver; |
| 12854 | if (bf.receiverList.pid != 0 |
| 12855 | && bf.receiverList.pid != MY_PID) { |
| 12856 | synchronized (this.mPidsSelfLocked) { |
| 12857 | app = this.mPidsSelfLocked.get( |
| 12858 | bf.receiverList.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12859 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12860 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12861 | } else { |
| 12862 | app = r.curApp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12863 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12864 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12865 | if (app != null) { |
| 12866 | anrMessage = "Broadcast of " + r.intent.toString(); |
| 12867 | } |
| 12868 | |
| 12869 | if (mPendingBroadcast == r) { |
| 12870 | mPendingBroadcast = null; |
| 12871 | } |
| 12872 | |
| 12873 | // Move on to the next receiver. |
| 12874 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 12875 | r.resultExtras, r.resultAbort, true); |
| 12876 | scheduleBroadcastsLocked(); |
| 12877 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12878 | if (anrMessage != null) { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12879 | // Post the ANR to the handler since we do not want to process ANRs while |
| 12880 | // potentially holding our lock. |
| 12881 | mHandler.post(new AppNotResponding(app, anrMessage)); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12882 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12883 | } |
| 12884 | |
| 12885 | private final void processCurBroadcastLocked(BroadcastRecord r, |
| 12886 | ProcessRecord app) throws RemoteException { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12887 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12888 | "Process cur broadcast " + r + " for app " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12889 | if (app.thread == null) { |
| 12890 | throw new RemoteException(); |
| 12891 | } |
| 12892 | r.receiver = app.thread.asBinder(); |
| 12893 | r.curApp = app; |
| 12894 | app.curReceiver = r; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12895 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12896 | |
| 12897 | // Tell the application to launch this receiver. |
| 12898 | r.intent.setComponent(r.curComponent); |
| 12899 | |
| 12900 | boolean started = false; |
| 12901 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12902 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12903 | "Delivering to component " + r.curComponent |
| 12904 | + ": " + r); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 12905 | ensurePackageDexOpt(r.intent.getComponent().getPackageName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12906 | app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver, |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 12907 | compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12908 | r.resultCode, r.resultData, r.resultExtras, r.ordered); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12909 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12910 | "Process cur broadcast " + r + " DELIVERED for app " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12911 | started = true; |
| 12912 | } finally { |
| 12913 | if (!started) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12914 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 12915 | "Process cur broadcast " + r + ": NOT STARTED!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12916 | r.receiver = null; |
| 12917 | r.curApp = null; |
| 12918 | app.curReceiver = null; |
| 12919 | } |
| 12920 | } |
| 12921 | |
| 12922 | } |
| 12923 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12924 | static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 12925 | Intent intent, int resultCode, String data, Bundle extras, |
| 12926 | boolean ordered, boolean sticky) throws RemoteException { |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12927 | // 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] | 12928 | if (app != null && app.thread != null) { |
| 12929 | // If we have an app thread, do the call through that so it is |
| 12930 | // correctly ordered with other one-way calls. |
| 12931 | app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 12932 | data, extras, ordered, sticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12933 | } else { |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 12934 | receiver.performReceive(intent, resultCode, data, extras, ordered, sticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12935 | } |
| 12936 | } |
| 12937 | |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12938 | private final void deliverToRegisteredReceiverLocked(BroadcastRecord r, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12939 | BroadcastFilter filter, boolean ordered) { |
| 12940 | boolean skip = false; |
| 12941 | if (filter.requiredPermission != null) { |
| 12942 | int perm = checkComponentPermission(filter.requiredPermission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12943 | r.callingPid, r.callingUid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12944 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12945 | Slog.w(TAG, "Permission Denial: broadcasting " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12946 | + r.intent.toString() |
| 12947 | + " from " + r.callerPackage + " (pid=" |
| 12948 | + r.callingPid + ", uid=" + r.callingUid + ")" |
| 12949 | + " requires " + filter.requiredPermission |
| 12950 | + " due to registered receiver " + filter); |
| 12951 | skip = true; |
| 12952 | } |
| 12953 | } |
| 12954 | if (r.requiredPermission != null) { |
| 12955 | int perm = checkComponentPermission(r.requiredPermission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12956 | filter.receiverList.pid, filter.receiverList.uid, -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12957 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12958 | Slog.w(TAG, "Permission Denial: receiving " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12959 | + r.intent.toString() |
| 12960 | + " to " + filter.receiverList.app |
| 12961 | + " (pid=" + filter.receiverList.pid |
| 12962 | + ", uid=" + filter.receiverList.uid + ")" |
| 12963 | + " requires " + r.requiredPermission |
| 12964 | + " due to sender " + r.callerPackage |
| 12965 | + " (uid " + r.callingUid + ")"); |
| 12966 | skip = true; |
| 12967 | } |
| 12968 | } |
| 12969 | |
| 12970 | if (!skip) { |
| 12971 | // If this is not being sent as an ordered broadcast, then we |
| 12972 | // don't want to touch the fields that keep track of the current |
| 12973 | // state of ordered broadcasts. |
| 12974 | if (ordered) { |
| 12975 | r.receiver = filter.receiverList.receiver.asBinder(); |
| 12976 | r.curFilter = filter; |
| 12977 | filter.receiverList.curBroadcast = r; |
| 12978 | r.state = BroadcastRecord.CALL_IN_RECEIVE; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12979 | if (filter.receiverList.app != null) { |
| 12980 | // Bump hosting application to no longer be in background |
| 12981 | // scheduling class. Note that we can't do that if there |
| 12982 | // isn't an app... but we can only be in that case for |
| 12983 | // things that directly call the IActivityManager API, which |
| 12984 | // are already core system stuff so don't matter for this. |
| 12985 | r.curApp = filter.receiverList.app; |
| 12986 | filter.receiverList.app.curReceiver = r; |
| 12987 | updateOomAdjLocked(); |
| 12988 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12989 | } |
| 12990 | try { |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12991 | if (DEBUG_BROADCAST_LIGHT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12992 | int seq = r.intent.getIntExtra("seq", -1); |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 12993 | Slog.i(TAG, "Delivering to " + filter |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 12994 | + " (seq=" + seq + "): " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12995 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12996 | performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12997 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12998 | r.resultData, r.resultExtras, r.ordered, r.initialSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12999 | if (ordered) { |
| 13000 | r.state = BroadcastRecord.CALL_DONE_RECEIVE; |
| 13001 | } |
| 13002 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13003 | Slog.w(TAG, "Failure sending broadcast " + r.intent, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13004 | if (ordered) { |
| 13005 | r.receiver = null; |
| 13006 | r.curFilter = null; |
| 13007 | filter.receiverList.curBroadcast = null; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 13008 | if (filter.receiverList.app != null) { |
| 13009 | filter.receiverList.app.curReceiver = null; |
| 13010 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13011 | } |
| 13012 | } |
| 13013 | } |
| 13014 | } |
| 13015 | |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 13016 | private final void addBroadcastToHistoryLocked(BroadcastRecord r) { |
| 13017 | if (r.callingUid < 0) { |
| 13018 | // This was from a registerReceiver() call; ignore it. |
| 13019 | return; |
| 13020 | } |
| 13021 | System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1, |
| 13022 | MAX_BROADCAST_HISTORY-1); |
| 13023 | r.finishTime = SystemClock.uptimeMillis(); |
| 13024 | mBroadcastHistory[0] = r; |
| 13025 | } |
| 13026 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13027 | private final void processNextBroadcast(boolean fromMsg) { |
| 13028 | synchronized(this) { |
| 13029 | BroadcastRecord r; |
| 13030 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13031 | if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13032 | + mParallelBroadcasts.size() + " broadcasts, " |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 13033 | + mOrderedBroadcasts.size() + " ordered broadcasts"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13034 | |
| 13035 | updateCpuStats(); |
| 13036 | |
| 13037 | if (fromMsg) { |
| 13038 | mBroadcastsScheduled = false; |
| 13039 | } |
| 13040 | |
| 13041 | // First, deliver any non-serialized broadcasts right away. |
| 13042 | while (mParallelBroadcasts.size() > 0) { |
| 13043 | r = mParallelBroadcasts.remove(0); |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 13044 | r.dispatchTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 13045 | r.dispatchClockTime = System.currentTimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13046 | final int N = r.receivers.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13047 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 13048 | + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13049 | for (int i=0; i<N; i++) { |
| 13050 | Object target = r.receivers.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13051 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 13052 | "Delivering non-ordered to registered " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13053 | + target + ": " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 13054 | deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13055 | } |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 13056 | addBroadcastToHistoryLocked(r); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13057 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 13058 | + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13059 | } |
| 13060 | |
| 13061 | // Now take care of the next serialized one... |
| 13062 | |
| 13063 | // If we are waiting for a process to come up to handle the next |
| 13064 | // broadcast, then do nothing at this point. Just in case, we |
| 13065 | // check that the process we're waiting for still exists. |
| 13066 | if (mPendingBroadcast != null) { |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 13067 | if (DEBUG_BROADCAST_LIGHT) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13068 | Slog.v(TAG, "processNextBroadcast: waiting for " |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 13069 | + mPendingBroadcast.curApp); |
| 13070 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13071 | |
| 13072 | boolean isDead; |
| 13073 | synchronized (mPidsSelfLocked) { |
| 13074 | isDead = (mPidsSelfLocked.get(mPendingBroadcast.curApp.pid) == null); |
| 13075 | } |
| 13076 | if (!isDead) { |
| 13077 | // It's still alive, so keep waiting |
| 13078 | return; |
| 13079 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13080 | Slog.w(TAG, "pending app " + mPendingBroadcast.curApp |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13081 | + " died before responding to broadcast"); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 13082 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 13083 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13084 | mPendingBroadcast = null; |
| 13085 | } |
| 13086 | } |
| 13087 | |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 13088 | boolean looped = false; |
| 13089 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13090 | do { |
| 13091 | if (mOrderedBroadcasts.size() == 0) { |
| 13092 | // No more broadcasts pending, so all done! |
| 13093 | scheduleAppGcsLocked(); |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 13094 | if (looped) { |
| 13095 | // If we had finished the last ordered broadcast, then |
| 13096 | // make sure all processes have correct oom and sched |
| 13097 | // adjustments. |
| 13098 | updateOomAdjLocked(); |
| 13099 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13100 | return; |
| 13101 | } |
| 13102 | r = mOrderedBroadcasts.get(0); |
| 13103 | boolean forceReceive = false; |
| 13104 | |
| 13105 | // Ensure that even if something goes awry with the timeout |
| 13106 | // detection, we catch "hung" broadcasts here, discard them, |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 13107 | // and continue to make progress. |
| 13108 | // |
| 13109 | // 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] | 13110 | // receivers don't get executed with timeouts. They're intended for |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 13111 | // one time heavy lifting after system upgrades and can take |
| 13112 | // significant amounts of time. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13113 | int numReceivers = (r.receivers != null) ? r.receivers.size() : 0; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 13114 | if (mProcessesReady && r.dispatchTime > 0) { |
Jeff Hamilton | acf8474 | 2010-05-25 22:10:18 -0500 | [diff] [blame] | 13115 | long now = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13116 | if ((numReceivers > 0) && |
| 13117 | (now > r.dispatchTime + (2*BROADCAST_TIMEOUT*numReceivers))) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13118 | Slog.w(TAG, "Hung broadcast discarded after timeout failure:" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13119 | + " now=" + now |
| 13120 | + " dispatchTime=" + r.dispatchTime |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 13121 | + " startTime=" + r.receiverTime |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13122 | + " intent=" + r.intent |
| 13123 | + " numReceivers=" + numReceivers |
| 13124 | + " nextReceiver=" + r.nextReceiver |
| 13125 | + " state=" + r.state); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 13126 | broadcastTimeoutLocked(false); // forcibly finish this broadcast |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13127 | forceReceive = true; |
| 13128 | r.state = BroadcastRecord.IDLE; |
| 13129 | } |
| 13130 | } |
| 13131 | |
| 13132 | if (r.state != BroadcastRecord.IDLE) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13133 | if (DEBUG_BROADCAST) Slog.d(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13134 | "processNextBroadcast() called when not idle (state=" |
| 13135 | + r.state + ")"); |
| 13136 | return; |
| 13137 | } |
| 13138 | |
| 13139 | if (r.receivers == null || r.nextReceiver >= numReceivers |
| 13140 | || r.resultAbort || forceReceive) { |
| 13141 | // No more receivers for this broadcast! Send the final |
| 13142 | // result if requested... |
| 13143 | if (r.resultTo != null) { |
| 13144 | try { |
| 13145 | if (DEBUG_BROADCAST) { |
| 13146 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13147 | Slog.i(TAG, "Finishing broadcast " + r.intent.getAction() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13148 | + " seq=" + seq + " app=" + r.callerApp); |
| 13149 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 13150 | performReceiveLocked(r.callerApp, r.resultTo, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13151 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 13152 | r.resultData, r.resultExtras, false, false); |
Johannes Carlsson | b5a8654 | 2010-10-27 10:08:10 +0200 | [diff] [blame] | 13153 | // Set this to null so that the reference |
| 13154 | // (local and remote) isnt kept in the mBroadcastHistory. |
| 13155 | r.resultTo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13156 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13157 | 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] | 13158 | } |
| 13159 | } |
| 13160 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13161 | if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG"); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 13162 | cancelBroadcastTimeoutLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13163 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13164 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 13165 | + r); |
| 13166 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13167 | // ... and on to the next... |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 13168 | addBroadcastToHistoryLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13169 | mOrderedBroadcasts.remove(0); |
| 13170 | r = null; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 13171 | looped = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13172 | continue; |
| 13173 | } |
| 13174 | } while (r == null); |
| 13175 | |
| 13176 | // Get the next receiver... |
| 13177 | int recIdx = r.nextReceiver++; |
| 13178 | |
| 13179 | // Keep track of when this receiver started, and make sure there |
| 13180 | // is a timeout message pending to kill it if need be. |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 13181 | r.receiverTime = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13182 | if (recIdx == 0) { |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 13183 | r.dispatchTime = r.receiverTime; |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 13184 | r.dispatchClockTime = System.currentTimeMillis(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13185 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast " |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 13186 | + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 13187 | } |
| 13188 | if (! mPendingBroadcastTimeoutMessage) { |
| 13189 | long timeoutTime = r.receiverTime + BROADCAST_TIMEOUT; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13190 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 13191 | "Submitting BROADCAST_TIMEOUT_MSG for " + r + " at " + timeoutTime); |
| 13192 | setBroadcastTimeoutLocked(timeoutTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13193 | } |
| 13194 | |
| 13195 | Object nextReceiver = r.receivers.get(recIdx); |
| 13196 | if (nextReceiver instanceof BroadcastFilter) { |
| 13197 | // Simple case: this is a registered receiver who gets |
| 13198 | // a direct call. |
| 13199 | BroadcastFilter filter = (BroadcastFilter)nextReceiver; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13200 | if (DEBUG_BROADCAST) Slog.v(TAG, |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 13201 | "Delivering ordered to registered " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13202 | + filter + ": " + r); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 13203 | deliverToRegisteredReceiverLocked(r, filter, r.ordered); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13204 | if (r.receiver == null || !r.ordered) { |
| 13205 | // The receiver has already finished, so schedule to |
| 13206 | // process the next one. |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 13207 | if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing: ordered=" |
| 13208 | + r.ordered + " receiver=" + r.receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13209 | r.state = BroadcastRecord.IDLE; |
| 13210 | scheduleBroadcastsLocked(); |
| 13211 | } |
| 13212 | return; |
| 13213 | } |
| 13214 | |
| 13215 | // Hard case: need to instantiate the receiver, possibly |
| 13216 | // starting its application process to host it. |
| 13217 | |
| 13218 | ResolveInfo info = |
| 13219 | (ResolveInfo)nextReceiver; |
| 13220 | |
| 13221 | boolean skip = false; |
| 13222 | int perm = checkComponentPermission(info.activityInfo.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 13223 | r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid, |
| 13224 | info.activityInfo.exported); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13225 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 13226 | if (!info.activityInfo.exported) { |
| 13227 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 13228 | + r.intent.toString() |
| 13229 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 13230 | + ", uid=" + r.callingUid + ")" |
| 13231 | + " is not exported from uid " + info.activityInfo.applicationInfo.uid |
| 13232 | + " due to receiver " + info.activityInfo.packageName |
| 13233 | + "/" + info.activityInfo.name); |
| 13234 | } else { |
| 13235 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 13236 | + r.intent.toString() |
| 13237 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 13238 | + ", uid=" + r.callingUid + ")" |
| 13239 | + " requires " + info.activityInfo.permission |
| 13240 | + " due to receiver " + info.activityInfo.packageName |
| 13241 | + "/" + info.activityInfo.name); |
| 13242 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13243 | skip = true; |
| 13244 | } |
Dianne Hackborn | d99b293 | 2011-08-18 14:39:58 -0700 | [diff] [blame] | 13245 | if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID && |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13246 | r.requiredPermission != null) { |
| 13247 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13248 | perm = AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13249 | checkPermission(r.requiredPermission, |
| 13250 | info.activityInfo.applicationInfo.packageName); |
| 13251 | } catch (RemoteException e) { |
| 13252 | perm = PackageManager.PERMISSION_DENIED; |
| 13253 | } |
| 13254 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13255 | Slog.w(TAG, "Permission Denial: receiving " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13256 | + r.intent + " to " |
| 13257 | + info.activityInfo.applicationInfo.packageName |
| 13258 | + " requires " + r.requiredPermission |
| 13259 | + " due to sender " + r.callerPackage |
| 13260 | + " (uid " + r.callingUid + ")"); |
| 13261 | skip = true; |
| 13262 | } |
| 13263 | } |
| 13264 | if (r.curApp != null && r.curApp.crashing) { |
| 13265 | // If the target process is crashing, just skip it. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 13266 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 13267 | "Skipping deliver ordered " + r + " to " + r.curApp |
| 13268 | + ": process crashing"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13269 | skip = true; |
| 13270 | } |
| 13271 | |
| 13272 | if (skip) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 13273 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 13274 | "Skipping delivery of ordered " + r + " for whatever reason"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13275 | r.receiver = null; |
| 13276 | r.curFilter = null; |
| 13277 | r.state = BroadcastRecord.IDLE; |
| 13278 | scheduleBroadcastsLocked(); |
| 13279 | return; |
| 13280 | } |
| 13281 | |
| 13282 | r.state = BroadcastRecord.APP_RECEIVE; |
| 13283 | String targetProcess = info.activityInfo.processName; |
| 13284 | r.curComponent = new ComponentName( |
| 13285 | info.activityInfo.applicationInfo.packageName, |
| 13286 | info.activityInfo.name); |
| 13287 | r.curReceiver = info.activityInfo; |
| 13288 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 13289 | // Broadcast is being executed, its package can't be stopped. |
| 13290 | try { |
| 13291 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 13292 | r.curComponent.getPackageName(), false); |
| 13293 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 13294 | } catch (IllegalArgumentException e) { |
| 13295 | Slog.w(TAG, "Failed trying to unstop package " |
| 13296 | + r.curComponent.getPackageName() + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 13297 | } |
| 13298 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13299 | // Is this receiver's application already running? |
| 13300 | ProcessRecord app = getProcessRecordLocked(targetProcess, |
| 13301 | info.activityInfo.applicationInfo.uid); |
| 13302 | if (app != null && app.thread != null) { |
| 13303 | try { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 13304 | app.addPackage(info.activityInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13305 | processCurBroadcastLocked(r, app); |
| 13306 | return; |
| 13307 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13308 | Slog.w(TAG, "Exception when sending broadcast to " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13309 | + r.curComponent, e); |
| 13310 | } |
| 13311 | |
| 13312 | // If a dead object exception was thrown -- fall through to |
| 13313 | // restart the application. |
| 13314 | } |
| 13315 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 13316 | // 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] | 13317 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 13318 | "Need to start app " + targetProcess + " for broadcast " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13319 | if ((r.curApp=startProcessLocked(targetProcess, |
| 13320 | info.activityInfo.applicationInfo, true, |
| 13321 | r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 13322 | "broadcast", r.curComponent, |
| 13323 | (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0)) |
| 13324 | == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13325 | // Ah, this recipient is unavailable. Finish it if necessary, |
| 13326 | // and mark the broadcast record as ready for the next. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13327 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13328 | + info.activityInfo.applicationInfo.packageName + "/" |
| 13329 | + info.activityInfo.applicationInfo.uid + " for broadcast " |
| 13330 | + r.intent + ": process is bad"); |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 13331 | logBroadcastReceiverDiscardLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13332 | finishReceiverLocked(r.receiver, r.resultCode, r.resultData, |
| 13333 | r.resultExtras, r.resultAbort, true); |
| 13334 | scheduleBroadcastsLocked(); |
| 13335 | r.state = BroadcastRecord.IDLE; |
| 13336 | return; |
| 13337 | } |
| 13338 | |
| 13339 | mPendingBroadcast = r; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 13340 | mPendingBroadcastRecvIndex = recIdx; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13341 | } |
| 13342 | } |
| 13343 | |
| 13344 | // ========================================================= |
| 13345 | // INSTRUMENTATION |
| 13346 | // ========================================================= |
| 13347 | |
| 13348 | public boolean startInstrumentation(ComponentName className, |
| 13349 | String profileFile, int flags, Bundle arguments, |
| 13350 | IInstrumentationWatcher watcher) { |
| 13351 | // Refuse possible leaked file descriptors |
| 13352 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 13353 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 13354 | } |
| 13355 | |
| 13356 | synchronized(this) { |
| 13357 | InstrumentationInfo ii = null; |
| 13358 | ApplicationInfo ai = null; |
| 13359 | try { |
| 13360 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 13361 | className, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13362 | ai = mContext.getPackageManager().getApplicationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 13363 | ii.targetPackage, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13364 | } catch (PackageManager.NameNotFoundException e) { |
| 13365 | } |
| 13366 | if (ii == null) { |
| 13367 | reportStartInstrumentationFailure(watcher, className, |
| 13368 | "Unable to find instrumentation info for: " + className); |
| 13369 | return false; |
| 13370 | } |
| 13371 | if (ai == null) { |
| 13372 | reportStartInstrumentationFailure(watcher, className, |
| 13373 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 13374 | return false; |
| 13375 | } |
| 13376 | |
| 13377 | int match = mContext.getPackageManager().checkSignatures( |
| 13378 | ii.targetPackage, ii.packageName); |
| 13379 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 13380 | String msg = "Permission Denial: starting instrumentation " |
| 13381 | + className + " from pid=" |
| 13382 | + Binder.getCallingPid() |
| 13383 | + ", uid=" + Binder.getCallingPid() |
| 13384 | + " not allowed because package " + ii.packageName |
| 13385 | + " does not have a signature matching the target " |
| 13386 | + ii.targetPackage; |
| 13387 | reportStartInstrumentationFailure(watcher, className, msg); |
| 13388 | throw new SecurityException(msg); |
| 13389 | } |
| 13390 | |
| 13391 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 13392 | // Instrumentation can kill and relaunch even persistent processes |
| 13393 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13394 | ProcessRecord app = addAppLocked(ai); |
| 13395 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 13396 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13397 | app.instrumentationProfileFile = profileFile; |
| 13398 | app.instrumentationArguments = arguments; |
| 13399 | app.instrumentationWatcher = watcher; |
| 13400 | app.instrumentationResultClass = className; |
| 13401 | Binder.restoreCallingIdentity(origId); |
| 13402 | } |
| 13403 | |
| 13404 | return true; |
| 13405 | } |
| 13406 | |
| 13407 | /** |
| 13408 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 13409 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 13410 | * the "am" command to report errors with more information. |
| 13411 | * |
| 13412 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 13413 | * @param cn The component name of the instrumentation. |
| 13414 | * @param report The error report. |
| 13415 | */ |
| 13416 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 13417 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13418 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13419 | try { |
| 13420 | if (watcher != null) { |
| 13421 | Bundle results = new Bundle(); |
| 13422 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 13423 | results.putString("Error", report); |
| 13424 | watcher.instrumentationStatus(cn, -1, results); |
| 13425 | } |
| 13426 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13427 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13428 | } |
| 13429 | } |
| 13430 | |
| 13431 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 13432 | if (app.instrumentationWatcher != null) { |
| 13433 | try { |
| 13434 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 13435 | app.instrumentationWatcher.instrumentationFinished( |
| 13436 | app.instrumentationClass, |
| 13437 | resultCode, |
| 13438 | results); |
| 13439 | } catch (RemoteException e) { |
| 13440 | } |
| 13441 | } |
| 13442 | app.instrumentationWatcher = null; |
| 13443 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 13444 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13445 | app.instrumentationProfileFile = null; |
| 13446 | app.instrumentationArguments = null; |
| 13447 | |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 13448 | forceStopPackageLocked(app.processName, -1, false, false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13449 | } |
| 13450 | |
| 13451 | public void finishInstrumentation(IApplicationThread target, |
| 13452 | int resultCode, Bundle results) { |
| 13453 | // Refuse possible leaked file descriptors |
| 13454 | if (results != null && results.hasFileDescriptors()) { |
| 13455 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 13456 | } |
| 13457 | |
| 13458 | synchronized(this) { |
| 13459 | ProcessRecord app = getRecordForAppLocked(target); |
| 13460 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13461 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13462 | return; |
| 13463 | } |
| 13464 | final long origId = Binder.clearCallingIdentity(); |
| 13465 | finishInstrumentationLocked(app, resultCode, results); |
| 13466 | Binder.restoreCallingIdentity(origId); |
| 13467 | } |
| 13468 | } |
| 13469 | |
| 13470 | // ========================================================= |
| 13471 | // CONFIGURATION |
| 13472 | // ========================================================= |
| 13473 | |
| 13474 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 13475 | ConfigurationInfo config = new ConfigurationInfo(); |
| 13476 | synchronized (this) { |
| 13477 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 13478 | config.reqKeyboardType = mConfiguration.keyboard; |
| 13479 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 13480 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 13481 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13482 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 13483 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 13484 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 13485 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13486 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 13487 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 13488 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13489 | } |
| 13490 | return config; |
| 13491 | } |
| 13492 | |
| 13493 | public Configuration getConfiguration() { |
| 13494 | Configuration ci; |
| 13495 | synchronized(this) { |
| 13496 | ci = new Configuration(mConfiguration); |
| 13497 | } |
| 13498 | return ci; |
| 13499 | } |
| 13500 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13501 | public void updatePersistentConfiguration(Configuration values) { |
| 13502 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 13503 | "updateConfiguration()"); |
| 13504 | enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS, |
| 13505 | "updateConfiguration()"); |
| 13506 | if (values == null) { |
| 13507 | throw new NullPointerException("Configuration must not be null"); |
| 13508 | } |
| 13509 | |
| 13510 | synchronized(this) { |
| 13511 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13512 | updateConfigurationLocked(values, null, true, false); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13513 | Binder.restoreCallingIdentity(origId); |
| 13514 | } |
| 13515 | } |
| 13516 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13517 | public void updateConfiguration(Configuration values) { |
| 13518 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 13519 | "updateConfiguration()"); |
| 13520 | |
| 13521 | synchronized(this) { |
| 13522 | if (values == null && mWindowManager != null) { |
| 13523 | // sentinel: fetch the current configuration from the window manager |
| 13524 | values = mWindowManager.computeNewConfiguration(); |
| 13525 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13526 | |
| 13527 | if (mWindowManager != null) { |
| 13528 | mProcessList.applyDisplaySize(mWindowManager); |
| 13529 | } |
| 13530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13531 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13532 | if (values != null) { |
| 13533 | Settings.System.clearConfiguration(values); |
| 13534 | } |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13535 | updateConfigurationLocked(values, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13536 | Binder.restoreCallingIdentity(origId); |
| 13537 | } |
| 13538 | } |
| 13539 | |
| 13540 | /** |
| 13541 | * Do either or both things: (1) change the current configuration, and (2) |
| 13542 | * make sure the given activity is running with the (now) current |
| 13543 | * configuration. Returns true if the activity has been left running, or |
| 13544 | * false if <var>starting</var> is being destroyed to match the new |
| 13545 | * configuration. |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13546 | * @param persistent TODO |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13547 | */ |
| 13548 | public boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13549 | ActivityRecord starting, boolean persistent, boolean initLocale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13550 | int changes = 0; |
| 13551 | |
| 13552 | boolean kept = true; |
| 13553 | |
| 13554 | if (values != null) { |
| 13555 | Configuration newConfig = new Configuration(mConfiguration); |
| 13556 | changes = newConfig.updateFrom(values); |
| 13557 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 13558 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13559 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13560 | } |
| 13561 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 13562 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13563 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13564 | if (values.locale != null && !initLocale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13565 | saveLocaleLocked(values.locale, |
| 13566 | !values.locale.equals(mConfiguration.locale), |
| 13567 | values.userSetLocale); |
| 13568 | } |
| 13569 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 13570 | mConfigurationSeq++; |
| 13571 | if (mConfigurationSeq <= 0) { |
| 13572 | mConfigurationSeq = 1; |
| 13573 | } |
| 13574 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13575 | mConfiguration = newConfig; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13576 | Slog.i(TAG, "Config changed: " + newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13577 | |
| 13578 | final Configuration configCopy = new Configuration(mConfiguration); |
| 13579 | |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 13580 | AttributeCache ac = AttributeCache.instance(); |
| 13581 | if (ac != null) { |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13582 | ac.updateConfiguration(configCopy); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 13583 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13584 | |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 13585 | // Make sure all resources in our process are updated |
| 13586 | // right now, so that anyone who is going to retrieve |
| 13587 | // resource values after we return will be sure to get |
| 13588 | // the new ones. This is especially important during |
| 13589 | // boot, where the first config change needs to guarantee |
| 13590 | // all resources have that config before following boot |
| 13591 | // code is executed. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13592 | mSystemThread.applyConfigurationToResources(configCopy); |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 13593 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 13594 | if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 13595 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13596 | msg.obj = new Configuration(configCopy); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 13597 | mHandler.sendMessage(msg); |
| 13598 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13599 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13600 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 13601 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13602 | try { |
| 13603 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13604 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 13605 | + app.processName + " new config " + mConfiguration); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 13606 | app.thread.scheduleConfigurationChanged(configCopy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13607 | } |
| 13608 | } catch (Exception e) { |
| 13609 | } |
| 13610 | } |
| 13611 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 13612 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 13613 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13614 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
| 13615 | null, false, false, MY_PID, Process.SYSTEM_UID); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 13616 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
| 13617 | broadcastIntentLocked(null, null, |
| 13618 | new Intent(Intent.ACTION_LOCALE_CHANGED), |
| 13619 | null, null, 0, null, null, |
| 13620 | null, false, false, MY_PID, Process.SYSTEM_UID); |
| 13621 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13622 | } |
| 13623 | } |
| 13624 | |
| 13625 | if (changes != 0 && starting == null) { |
| 13626 | // If the configuration changed, and the caller is not already |
| 13627 | // in the process of starting an activity, then find the top |
| 13628 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13629 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13630 | } |
| 13631 | |
| 13632 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13633 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 13634 | // And we need to make sure at this point that all other activities |
| 13635 | // are made visible with the correct configuration. |
| 13636 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13637 | } |
| 13638 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 13639 | if (values != null && mWindowManager != null) { |
| 13640 | mWindowManager.setNewConfiguration(mConfiguration); |
| 13641 | } |
| 13642 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13643 | return kept; |
| 13644 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13645 | |
| 13646 | /** |
| 13647 | * Save the locale. You must be inside a synchronized (this) block. |
| 13648 | */ |
| 13649 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 13650 | if(isDiff) { |
| 13651 | SystemProperties.set("user.language", l.getLanguage()); |
| 13652 | SystemProperties.set("user.region", l.getCountry()); |
| 13653 | } |
| 13654 | |
| 13655 | if(isPersist) { |
| 13656 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 13657 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 13658 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 13659 | } |
| 13660 | } |
| 13661 | |
| 13662 | // ========================================================= |
| 13663 | // LIFETIME MANAGEMENT |
| 13664 | // ========================================================= |
| 13665 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13666 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13667 | ProcessRecord TOP_APP, boolean recursed, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13668 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13669 | // This adjustment has already been computed. If we are calling |
| 13670 | // from the top, we may have already computed our adjustment with |
| 13671 | // an earlier hidden adjustment that isn't really for us... if |
| 13672 | // so, use the new hidden adjustment. |
| 13673 | if (!recursed && app.hidden) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13674 | app.curAdj = app.curRawAdj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13675 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13676 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13677 | } |
| 13678 | |
| 13679 | if (app.thread == null) { |
| 13680 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13681 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13682 | return (app.curAdj=ProcessList.HIDDEN_APP_MAX_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13683 | } |
| 13684 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13685 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
| 13686 | app.adjSource = null; |
| 13687 | app.adjTarget = null; |
| 13688 | app.empty = false; |
| 13689 | app.hidden = false; |
| 13690 | |
| 13691 | final int activitiesSize = app.activities.size(); |
| 13692 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13693 | if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13694 | // The max adjustment doesn't allow this app to be anything |
| 13695 | // below foreground, so it is not worth doing work for it. |
| 13696 | app.adjType = "fixed"; |
| 13697 | app.adjSeq = mAdjSeq; |
| 13698 | app.curRawAdj = app.maxAdj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13699 | app.foregroundActivities = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13700 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13701 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13702 | // System process can do UI, and when they do we want to have |
| 13703 | // them trim their memory after the user leaves the UI. To |
| 13704 | // facilitate this, here we need to determine whether or not it |
| 13705 | // is currently showing UI. |
| 13706 | app.systemNoUi = true; |
| 13707 | if (app == TOP_APP) { |
| 13708 | app.systemNoUi = false; |
| 13709 | } else if (activitiesSize > 0) { |
| 13710 | for (int j = 0; j < activitiesSize; j++) { |
| 13711 | final ActivityRecord r = app.activities.get(j); |
| 13712 | if (r.visible) { |
| 13713 | app.systemNoUi = false; |
| 13714 | break; |
| 13715 | } |
| 13716 | } |
| 13717 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13718 | return (app.curAdj=app.maxAdj); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13719 | } |
| 13720 | |
| 13721 | final boolean hadForegroundActivities = app.foregroundActivities; |
| 13722 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13723 | app.foregroundActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13724 | app.keeping = false; |
| 13725 | app.systemNoUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13726 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 13727 | // Determine the importance of the process, starting with most |
| 13728 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13729 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13730 | int schedGroup; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13731 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13732 | // The last app on the list is the foreground app. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13733 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13734 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13735 | app.adjType = "top-activity"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13736 | app.foregroundActivities = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13737 | } else if (app.instrumentationClass != null) { |
| 13738 | // Don't want to kill running instrumentation. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13739 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13740 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13741 | app.adjType = "instrumentation"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13742 | } else if (app.curReceiver != null || |
| 13743 | (mPendingBroadcast != null && mPendingBroadcast.curApp == app)) { |
| 13744 | // An app that is currently receiving a broadcast also |
| 13745 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13746 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13747 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13748 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13749 | } else if (app.executingServices.size() > 0) { |
| 13750 | // An app that is currently executing a service callback also |
| 13751 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13752 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13753 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13754 | app.adjType = "exec-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13755 | } else if (activitiesSize > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13756 | // This app is in the background with paused activities. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13757 | // We inspect activities to potentially upgrade adjustment further below. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13758 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13759 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13760 | app.hidden = true; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13761 | app.adjType = "bg-activities"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13762 | } else { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13763 | // A very not-needed process. If this is lower in the lru list, |
| 13764 | // we will push it in to the empty bucket. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13765 | adj = hiddenAdj; |
| 13766 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13767 | app.hidden = true; |
| 13768 | app.empty = true; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13769 | app.adjType = "bg-empty"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13770 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13771 | |
| 13772 | // Examine all activities if not already foreground. |
| 13773 | if (!app.foregroundActivities && activitiesSize > 0) { |
| 13774 | for (int j = 0; j < activitiesSize; j++) { |
| 13775 | final ActivityRecord r = app.activities.get(j); |
| 13776 | if (r.visible) { |
| 13777 | // App has a visible activity; only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13778 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 13779 | adj = ProcessList.VISIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13780 | app.adjType = "visible"; |
| 13781 | } |
| 13782 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13783 | app.hidden = false; |
| 13784 | app.foregroundActivities = true; |
| 13785 | break; |
| 13786 | } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED |
| 13787 | || r.state == ActivityState.STOPPING) { |
| 13788 | // Only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13789 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13790 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13791 | app.adjType = "stopping"; |
| 13792 | } |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 13793 | app.hidden = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13794 | app.foregroundActivities = true; |
| 13795 | } |
| 13796 | } |
| 13797 | } |
| 13798 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13799 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13800 | if (app.foregroundServices) { |
| 13801 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13802 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13803 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13804 | app.adjType = "foreground-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13805 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13806 | } else if (app.forcingToForeground != null) { |
| 13807 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13808 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13809 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13810 | app.adjType = "force-foreground"; |
| 13811 | app.adjSource = app.forcingToForeground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13812 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13813 | } |
| 13814 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13815 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13816 | if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13817 | // We don't want to kill the current heavy-weight process. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13818 | adj = ProcessList.HEAVY_WEIGHT_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13819 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13820 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13821 | app.adjType = "heavy"; |
| 13822 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13823 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13824 | if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13825 | // This process is hosting what we currently consider to be the |
| 13826 | // home app, so we don't want to let it go into the background. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13827 | adj = ProcessList.HOME_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13828 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13829 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13830 | app.adjType = "home"; |
| 13831 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13832 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 13833 | if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess |
| 13834 | && app.activities.size() > 0) { |
| 13835 | // This was the previous process that showed UI to the user. |
| 13836 | // We want to try to keep it around more aggressively, to give |
| 13837 | // a good experience around switching between two apps. |
| 13838 | adj = ProcessList.PREVIOUS_APP_ADJ; |
| 13839 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 13840 | app.hidden = false; |
| 13841 | app.adjType = "previous"; |
| 13842 | } |
| 13843 | |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13844 | if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj |
| 13845 | + " reason=" + app.adjType); |
| 13846 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 13847 | // 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] | 13848 | // there are applications dependent on our services or providers, but |
| 13849 | // this gives us a baseline and makes sure we don't get into an |
| 13850 | // infinite recursion. |
| 13851 | app.adjSeq = mAdjSeq; |
| 13852 | app.curRawAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13853 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 13854 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 13855 | // If possible we want to avoid killing apps while they're being backed up |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13856 | if (adj > ProcessList.BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13857 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13858 | adj = ProcessList.BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13859 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13860 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 13861 | } |
| 13862 | } |
| 13863 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13864 | if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13865 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13866 | final long now = SystemClock.uptimeMillis(); |
| 13867 | // This process is more important if the top activity is |
| 13868 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13869 | Iterator<ServiceRecord> jt = app.services.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13870 | while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13871 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13872 | if (s.startRequested) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13873 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13874 | // If this process has shown some UI, let it immediately |
| 13875 | // go to the LRU list because it may be pretty heavy with |
| 13876 | // UI stuff. We'll tag it with a label just to help |
| 13877 | // debug and understand what is going on. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13878 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13879 | app.adjType = "started-bg-ui-services"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13880 | } |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13881 | } else { |
| 13882 | if (now < (s.lastActivity+MAX_SERVICE_INACTIVITY)) { |
| 13883 | // This service has seen some activity within |
| 13884 | // recent memory, so we will keep its process ahead |
| 13885 | // of the background processes. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13886 | if (adj > ProcessList.SERVICE_ADJ) { |
| 13887 | adj = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13888 | app.adjType = "started-services"; |
| 13889 | app.hidden = false; |
| 13890 | } |
| 13891 | } |
| 13892 | // If we have let the service slide into the background |
| 13893 | // state, still have some text describing what it is doing |
| 13894 | // even though the service no longer has an impact. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13895 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13896 | app.adjType = "started-bg-services"; |
| 13897 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13898 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13899 | // Don't kill this process because it is doing work; it |
| 13900 | // has said it is doing work. |
| 13901 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13902 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13903 | if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13904 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13905 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13906 | = s.connections.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13907 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13908 | ArrayList<ConnectionRecord> clist = kt.next(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13909 | for (int i=0; i<clist.size() && adj > ProcessList.FOREGROUND_APP_ADJ; i++) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13910 | // XXX should compute this based on the max of |
| 13911 | // all connected clients. |
| 13912 | ConnectionRecord cr = clist.get(i); |
| 13913 | if (cr.binding.client == app) { |
| 13914 | // Binding to ourself is not interesting. |
| 13915 | continue; |
| 13916 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13917 | if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13918 | ProcessRecord client = cr.binding.client; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13919 | int clientAdj = adj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13920 | int myHiddenAdj = hiddenAdj; |
| 13921 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13922 | if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13923 | myHiddenAdj = client.hiddenAdj; |
| 13924 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13925 | myHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13926 | } |
| 13927 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13928 | clientAdj = computeOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13929 | client, myHiddenAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13930 | String adjType = null; |
| 13931 | if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) { |
| 13932 | // Not doing bind OOM management, so treat |
| 13933 | // this guy more like a started service. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13934 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13935 | // If this process has shown some UI, let it immediately |
| 13936 | // go to the LRU list because it may be pretty heavy with |
| 13937 | // UI stuff. We'll tag it with a label just to help |
| 13938 | // debug and understand what is going on. |
| 13939 | if (adj > clientAdj) { |
| 13940 | adjType = "bound-bg-ui-services"; |
| 13941 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13942 | app.hidden = false; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13943 | clientAdj = adj; |
| 13944 | } else { |
| 13945 | if (now >= (s.lastActivity+MAX_SERVICE_INACTIVITY)) { |
| 13946 | // This service has not seen activity within |
| 13947 | // recent memory, so allow it to drop to the |
| 13948 | // LRU list if there is no other reason to keep |
| 13949 | // it around. We'll also tag it with a label just |
| 13950 | // to help debug and undertand what is going on. |
| 13951 | if (adj > clientAdj) { |
| 13952 | adjType = "bound-bg-services"; |
| 13953 | } |
| 13954 | clientAdj = adj; |
| 13955 | } |
| 13956 | } |
| 13957 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13958 | if (adj > clientAdj) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13959 | // If this process has recently shown UI, and |
| 13960 | // the process that is binding to it is less |
| 13961 | // important than being visible, then we don't |
| 13962 | // care about the binding as much as we care |
| 13963 | // about letting this process get into the LRU |
| 13964 | // list to be killed and restarted if needed for |
| 13965 | // memory. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13966 | if (app.hasShownUi && app != mHomeProcess |
| 13967 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13968 | adjType = "bound-bg-ui-services"; |
| 13969 | } else { |
| 13970 | if ((cr.flags&(Context.BIND_ABOVE_CLIENT |
| 13971 | |Context.BIND_IMPORTANT)) != 0) { |
| 13972 | adj = clientAdj; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 13973 | } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0 |
| 13974 | && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ |
| 13975 | && adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13976 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13977 | } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13978 | adj = clientAdj; |
| 13979 | } else { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 13980 | app.pendingUiClean = true; |
| 13981 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 13982 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13983 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13984 | } |
| 13985 | if (!client.hidden) { |
| 13986 | app.hidden = false; |
| 13987 | } |
| 13988 | if (client.keeping) { |
| 13989 | app.keeping = true; |
| 13990 | } |
| 13991 | adjType = "service"; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13992 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13993 | } |
| 13994 | if (adjType != null) { |
| 13995 | app.adjType = adjType; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13996 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13997 | .REASON_SERVICE_IN_USE; |
| 13998 | app.adjSource = cr.binding.client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13999 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 14000 | app.adjTarget = s.name; |
| 14001 | } |
| 14002 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 14003 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 14004 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 14005 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14006 | } |
| 14007 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14008 | if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) { |
| 14009 | ActivityRecord a = cr.activity; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14010 | if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ && |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14011 | (a.visible || a.state == ActivityState.RESUMED |
| 14012 | || a.state == ActivityState.PAUSING)) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14013 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14014 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 14015 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 14016 | } |
| 14017 | app.hidden = false; |
| 14018 | app.adjType = "service"; |
| 14019 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 14020 | .REASON_SERVICE_IN_USE; |
| 14021 | app.adjSource = a; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 14022 | app.adjSourceOom = adj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14023 | app.adjTarget = s.name; |
| 14024 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14025 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14026 | } |
| 14027 | } |
| 14028 | } |
| 14029 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 14030 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14031 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 14032 | // would like to avoid killing it unless it would prevent the current |
| 14033 | // application from running. By default we put the process in |
| 14034 | // with the rest of the background processes; as we scan through |
| 14035 | // its services we may bump it up from there. |
| 14036 | if (adj > hiddenAdj) { |
| 14037 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14038 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 14039 | app.adjType = "bg-services"; |
| 14040 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14041 | } |
| 14042 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14043 | if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 14044 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 14045 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14046 | while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 14047 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 14048 | ContentProviderRecord cpr = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14049 | if (cpr.clients.size() != 0) { |
| 14050 | Iterator<ProcessRecord> kt = cpr.clients.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14051 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14052 | ProcessRecord client = kt.next(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 14053 | if (client == app) { |
| 14054 | // Being our own client is not interesting. |
| 14055 | continue; |
| 14056 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14057 | int myHiddenAdj = hiddenAdj; |
| 14058 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14059 | if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14060 | myHiddenAdj = client.hiddenAdj; |
| 14061 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14062 | myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14063 | } |
| 14064 | } |
| 14065 | int clientAdj = computeOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14066 | client, myHiddenAdj, TOP_APP, true, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14067 | if (adj > clientAdj) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 14068 | if (app.hasShownUi && app != mHomeProcess |
| 14069 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14070 | app.adjType = "bg-ui-provider"; |
| 14071 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14072 | adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ |
| 14073 | ? clientAdj : ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14074 | app.adjType = "provider"; |
| 14075 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14076 | if (!client.hidden) { |
| 14077 | app.hidden = false; |
| 14078 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14079 | if (client.keeping) { |
| 14080 | app.keeping = true; |
| 14081 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 14082 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 14083 | .REASON_PROVIDER_IN_USE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 14084 | app.adjSource = client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 14085 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 14086 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14087 | } |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 14088 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 14089 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 14090 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14091 | } |
| 14092 | } |
| 14093 | // If the provider has external (non-framework) process |
| 14094 | // dependencies, ensure that its adjustment is at least |
| 14095 | // FOREGROUND_APP_ADJ. |
| 14096 | if (cpr.externals != 0) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14097 | if (adj > ProcessList.FOREGROUND_APP_ADJ) { |
| 14098 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 14099 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14100 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14101 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 14102 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 14103 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14104 | } |
| 14105 | } |
| 14106 | } |
| 14107 | } |
| 14108 | |
| 14109 | app.curRawAdj = adj; |
| 14110 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 14111 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14112 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 14113 | if (adj > app.maxAdj) { |
| 14114 | adj = app.maxAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14115 | if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 14116 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 14117 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14118 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14119 | if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14120 | app.keeping = true; |
| 14121 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14122 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14123 | if (app.hasAboveClient) { |
| 14124 | // If this process has bound to any services with BIND_ABOVE_CLIENT, |
| 14125 | // then we need to drop its adjustment to be lower than the service's |
| 14126 | // in order to honor the request. We want to drop it by one adjustment |
| 14127 | // level... but there is special meaning applied to various levels so |
| 14128 | // we will skip some of them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14129 | if (adj < ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14130 | // System process will not get dropped, ever |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14131 | } else if (adj < ProcessList.VISIBLE_APP_ADJ) { |
| 14132 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 14133 | } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 14134 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 14135 | } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 14136 | adj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14137 | } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14138 | adj++; |
| 14139 | } |
| 14140 | } |
| 14141 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14142 | if (adj == ProcessList.SERVICE_ADJ) { |
| 14143 | if (doingAll) { |
| 14144 | app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3); |
| 14145 | mNewNumServiceProcs++; |
| 14146 | } |
| 14147 | if (app.serviceb) { |
| 14148 | adj = ProcessList.SERVICE_B_ADJ; |
| 14149 | } |
| 14150 | } else { |
| 14151 | app.serviceb = false; |
| 14152 | } |
| 14153 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14154 | app.curAdj = adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 14155 | app.curSchedGroup = schedGroup; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 14156 | |
| 14157 | if (hadForegroundActivities != app.foregroundActivities) { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 14158 | mHandler.obtainMessage(DISPATCH_FOREGROUND_ACTIVITIES_CHANGED, app.pid, app.info.uid, |
| 14159 | app.foregroundActivities).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 14160 | } |
| 14161 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14162 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14163 | } |
| 14164 | |
| 14165 | /** |
| 14166 | * Ask a given process to GC right now. |
| 14167 | */ |
| 14168 | final void performAppGcLocked(ProcessRecord app) { |
| 14169 | try { |
| 14170 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 14171 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14172 | if (app.reportLowMemory) { |
| 14173 | app.reportLowMemory = false; |
| 14174 | app.thread.scheduleLowMemory(); |
| 14175 | } else { |
| 14176 | app.thread.processInBackground(); |
| 14177 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14178 | } |
| 14179 | } catch (Exception e) { |
| 14180 | // whatever. |
| 14181 | } |
| 14182 | } |
| 14183 | |
| 14184 | /** |
| 14185 | * Returns true if things are idle enough to perform GCs. |
| 14186 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 14187 | private final boolean canGcNowLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14188 | return mParallelBroadcasts.size() == 0 |
| 14189 | && mOrderedBroadcasts.size() == 0 |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14190 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 14191 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14192 | } |
| 14193 | |
| 14194 | /** |
| 14195 | * Perform GCs on all processes that are waiting for it, but only |
| 14196 | * if things are idle. |
| 14197 | */ |
| 14198 | final void performAppGcsLocked() { |
| 14199 | final int N = mProcessesToGc.size(); |
| 14200 | if (N <= 0) { |
| 14201 | return; |
| 14202 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 14203 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14204 | while (mProcessesToGc.size() > 0) { |
| 14205 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14206 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14207 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 14208 | <= SystemClock.uptimeMillis()) { |
| 14209 | // To avoid spamming the system, we will GC processes one |
| 14210 | // at a time, waiting a few seconds between each. |
| 14211 | performAppGcLocked(proc); |
| 14212 | scheduleAppGcsLocked(); |
| 14213 | return; |
| 14214 | } else { |
| 14215 | // It hasn't been long enough since we last GCed this |
| 14216 | // process... put it in the list to wait for its time. |
| 14217 | addProcessToGcListLocked(proc); |
| 14218 | break; |
| 14219 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14220 | } |
| 14221 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14222 | |
| 14223 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14224 | } |
| 14225 | } |
| 14226 | |
| 14227 | /** |
| 14228 | * If all looks good, perform GCs on all processes waiting for them. |
| 14229 | */ |
| 14230 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 14231 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14232 | performAppGcsLocked(); |
| 14233 | return; |
| 14234 | } |
| 14235 | // Still not idle, wait some more. |
| 14236 | scheduleAppGcsLocked(); |
| 14237 | } |
| 14238 | |
| 14239 | /** |
| 14240 | * Schedule the execution of all pending app GCs. |
| 14241 | */ |
| 14242 | final void scheduleAppGcsLocked() { |
| 14243 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14244 | |
| 14245 | if (mProcessesToGc.size() > 0) { |
| 14246 | // Schedule a GC for the time to the next process. |
| 14247 | ProcessRecord proc = mProcessesToGc.get(0); |
| 14248 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 14249 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14250 | long when = proc.lastRequestedGc + GC_MIN_INTERVAL; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14251 | long now = SystemClock.uptimeMillis(); |
| 14252 | if (when < (now+GC_TIMEOUT)) { |
| 14253 | when = now + GC_TIMEOUT; |
| 14254 | } |
| 14255 | mHandler.sendMessageAtTime(msg, when); |
| 14256 | } |
| 14257 | } |
| 14258 | |
| 14259 | /** |
| 14260 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 14261 | * list in sorted order by the last GC time. The process can't already be |
| 14262 | * on the list. |
| 14263 | */ |
| 14264 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 14265 | boolean added = false; |
| 14266 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 14267 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 14268 | proc.lastRequestedGc) { |
| 14269 | added = true; |
| 14270 | mProcessesToGc.add(i+1, proc); |
| 14271 | break; |
| 14272 | } |
| 14273 | } |
| 14274 | if (!added) { |
| 14275 | mProcessesToGc.add(0, proc); |
| 14276 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14277 | } |
| 14278 | |
| 14279 | /** |
| 14280 | * Set up to ask a process to GC itself. This will either do it |
| 14281 | * immediately, or put it on the list of processes to gc the next |
| 14282 | * time things are idle. |
| 14283 | */ |
| 14284 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 14285 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14286 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14287 | return; |
| 14288 | } |
| 14289 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 14290 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14291 | scheduleAppGcsLocked(); |
| 14292 | } |
| 14293 | } |
| 14294 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14295 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 14296 | updateCpuStatsNow(); |
| 14297 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14298 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14299 | boolean doWakeKills = doKills; |
| 14300 | boolean doCpuKills = doKills; |
| 14301 | if (mLastPowerCheckRealtime == 0) { |
| 14302 | doWakeKills = false; |
| 14303 | } |
| 14304 | if (mLastPowerCheckUptime == 0) { |
| 14305 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14306 | } |
| 14307 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14308 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14309 | } |
| 14310 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14311 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 14312 | final long curUptime = SystemClock.uptimeMillis(); |
| 14313 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 14314 | mLastPowerCheckRealtime = curRealtime; |
| 14315 | mLastPowerCheckUptime = curUptime; |
| 14316 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 14317 | doWakeKills = false; |
| 14318 | } |
| 14319 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 14320 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14321 | } |
| 14322 | int i = mLruProcesses.size(); |
| 14323 | while (i > 0) { |
| 14324 | i--; |
| 14325 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14326 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14327 | long wtime; |
| 14328 | synchronized (stats) { |
| 14329 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 14330 | app.pid, curRealtime); |
| 14331 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14332 | long wtimeUsed = wtime - app.lastWakeTime; |
| 14333 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 14334 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 14335 | StringBuilder sb = new StringBuilder(128); |
| 14336 | sb.append("Wake for "); |
| 14337 | app.toShortString(sb); |
| 14338 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14339 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 14340 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14341 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 14342 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14343 | sb.append((wtimeUsed*100)/realtimeSince); |
| 14344 | sb.append("%)"); |
| 14345 | Slog.i(TAG, sb.toString()); |
| 14346 | sb.setLength(0); |
| 14347 | sb.append("CPU for "); |
| 14348 | app.toShortString(sb); |
| 14349 | sb.append(": over "); |
| 14350 | TimeUtils.formatDuration(uptimeSince, sb); |
| 14351 | sb.append(" used "); |
| 14352 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 14353 | sb.append(" ("); |
| 14354 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 14355 | sb.append("%)"); |
| 14356 | Slog.i(TAG, sb.toString()); |
| 14357 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14358 | // If a process has held a wake lock for more |
| 14359 | // than 50% of the time during this period, |
| 14360 | // that sounds pad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14361 | if (doWakeKills && realtimeSince > 0 |
| 14362 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 14363 | synchronized (stats) { |
| 14364 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 14365 | realtimeSince, wtimeUsed); |
| 14366 | } |
| 14367 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 14368 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 14369 | + " during " + realtimeSince); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14370 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14371 | app.processName, app.setAdj, "excessive wake lock"); |
| 14372 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14373 | } else if (doCpuKills && uptimeSince > 0 |
| 14374 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 14375 | synchronized (stats) { |
| 14376 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 14377 | uptimeSince, cputimeUsed); |
| 14378 | } |
| 14379 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 14380 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 14381 | + " during " + uptimeSince); |
| 14382 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14383 | app.processName, app.setAdj, "excessive cpu"); |
| 14384 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14385 | } else { |
| 14386 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14387 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14388 | } |
| 14389 | } |
| 14390 | } |
| 14391 | } |
| 14392 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14393 | private final boolean updateOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14394 | ProcessRecord app, int hiddenAdj, ProcessRecord TOP_APP, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14395 | app.hiddenAdj = hiddenAdj; |
| 14396 | |
| 14397 | if (app.thread == null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14398 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14399 | } |
| 14400 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14401 | final boolean wasKeeping = app.keeping; |
| 14402 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14403 | boolean success = true; |
| 14404 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14405 | computeOomAdjLocked(app, hiddenAdj, TOP_APP, false, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14406 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14407 | if (app.curRawAdj != app.setRawAdj) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 14408 | if (false) { |
| 14409 | // Removing for now. Forcing GCs is not so useful anymore |
| 14410 | // with Dalvik, and the new memory level hint facility is |
| 14411 | // better for what we need to do these days. |
| 14412 | if (app.curRawAdj > ProcessList.FOREGROUND_APP_ADJ |
| 14413 | && app.setRawAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
| 14414 | // If this app is transitioning from foreground to |
| 14415 | // non-foreground, have it do a gc. |
| 14416 | scheduleAppGcLocked(app); |
| 14417 | } else if (app.curRawAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 14418 | && app.setRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 14419 | // Likewise do a gc when an app is moving in to the |
| 14420 | // background (such as a service stopping). |
| 14421 | scheduleAppGcLocked(app); |
| 14422 | } |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14423 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 14424 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14425 | if (wasKeeping && !app.keeping) { |
| 14426 | // This app is no longer something we want to keep. Note |
| 14427 | // its current wake lock time to later know to kill it if |
| 14428 | // it is not behaving well. |
| 14429 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 14430 | synchronized (stats) { |
| 14431 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 14432 | app.pid, SystemClock.elapsedRealtime()); |
| 14433 | } |
| 14434 | app.lastCpuTime = app.curCpuTime; |
| 14435 | } |
| 14436 | |
| 14437 | app.setRawAdj = app.curRawAdj; |
| 14438 | } |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14439 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14440 | if (app.curAdj != app.setAdj) { |
| 14441 | if (Process.setOomAdj(app.pid, app.curAdj)) { |
Dianne Hackborn | bbb09ac | 2011-11-30 11:31:29 -0800 | [diff] [blame] | 14442 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14443 | TAG, "Set " + app.pid + " " + app.processName + |
| 14444 | " adj " + app.curAdj + ": " + app.adjType); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14445 | app.setAdj = app.curAdj; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14446 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14447 | success = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14448 | Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14449 | } |
| 14450 | } |
| 14451 | if (app.setSchedGroup != app.curSchedGroup) { |
| 14452 | app.setSchedGroup = app.curSchedGroup; |
| 14453 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
| 14454 | "Setting process group of " + app.processName |
| 14455 | + " to " + app.curSchedGroup); |
| 14456 | if (app.waitingToKill != null && |
| 14457 | app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 14458 | Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill); |
| 14459 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14460 | app.processName, app.setAdj, app.waitingToKill); |
| 14461 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14462 | success = false; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14463 | } else { |
| 14464 | if (true) { |
| 14465 | long oldId = Binder.clearCallingIdentity(); |
| 14466 | try { |
| 14467 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 14468 | } catch (Exception e) { |
| 14469 | Slog.w(TAG, "Failed setting process group of " + app.pid |
| 14470 | + " to " + app.curSchedGroup); |
| 14471 | e.printStackTrace(); |
| 14472 | } finally { |
| 14473 | Binder.restoreCallingIdentity(oldId); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 14474 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14475 | } else { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14476 | if (app.thread != null) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 14477 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 14478 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 14479 | } catch (RemoteException e) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 14480 | } |
| 14481 | } |
| 14482 | } |
| 14483 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14484 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14485 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14486 | } |
| 14487 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 14488 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14489 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14490 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14491 | resumedActivity = mMainStack.mPausingActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14492 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14493 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14494 | } |
| 14495 | } |
| 14496 | return resumedActivity; |
| 14497 | } |
| 14498 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14499 | private final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 14500 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14501 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 14502 | int curAdj = app.curAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14503 | final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 14504 | && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14505 | |
| 14506 | mAdjSeq++; |
| 14507 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14508 | boolean success = updateOomAdjLocked(app, app.hiddenAdj, TOP_APP, false); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14509 | final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 14510 | && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14511 | if (nowHidden != wasHidden) { |
| 14512 | // Changed to/from hidden state, so apps after it in the LRU |
| 14513 | // list may also be changed. |
| 14514 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14515 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 14516 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14517 | } |
| 14518 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14519 | final void updateOomAdjLocked() { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 14520 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14521 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 14522 | |
| 14523 | if (false) { |
| 14524 | RuntimeException e = new RuntimeException(); |
| 14525 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 14526 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14527 | } |
| 14528 | |
| 14529 | mAdjSeq++; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14530 | mNewNumServiceProcs = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14531 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 14532 | // Let's determine how many processes we have running vs. |
| 14533 | // how many slots we have for background processes; we may want |
| 14534 | // to put multiple processes in a slot of there are enough of |
| 14535 | // them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14536 | int numSlots = ProcessList.HIDDEN_APP_MAX_ADJ - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 14537 | int factor = (mLruProcesses.size()-4)/numSlots; |
| 14538 | if (factor < 1) factor = 1; |
| 14539 | int step = 0; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 14540 | int numHidden = 0; |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 14541 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14542 | // First update the OOM adjustment for each of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14543 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14544 | int i = mLruProcesses.size(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14545 | int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14546 | while (i > 0) { |
| 14547 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14548 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 14549 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14550 | updateOomAdjLocked(app, curHiddenAdj, TOP_APP, true); |
| 14551 | if (curHiddenAdj < ProcessList.HIDDEN_APP_MAX_ADJ |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14552 | && app.curAdj == curHiddenAdj) { |
| 14553 | step++; |
| 14554 | if (step >= factor) { |
| 14555 | step = 0; |
| 14556 | curHiddenAdj++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14557 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14558 | } |
| 14559 | if (!app.killedBackground) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14560 | if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14561 | numHidden++; |
| 14562 | if (numHidden > mProcessLimit) { |
| 14563 | Slog.i(TAG, "No longer want " + app.processName |
| 14564 | + " (pid " + app.pid + "): hidden #" + numHidden); |
| 14565 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14566 | app.processName, app.setAdj, "too many background"); |
| 14567 | app.killedBackground = true; |
| 14568 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 14569 | } |
| 14570 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14571 | } |
| 14572 | } |
| 14573 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 14574 | mNumServiceProcs = mNewNumServiceProcs; |
| 14575 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14576 | // Now determine the memory trimming level of background processes. |
| 14577 | // Unfortunately we need to start at the back of the list to do this |
| 14578 | // properly. We only do this if the number of background apps we |
| 14579 | // are managing to keep around is less than half the maximum we desire; |
| 14580 | // if we are keeping a good number around, we'll let them use whatever |
| 14581 | // memory they want. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14582 | if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/2)) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14583 | final int N = mLruProcesses.size(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 14584 | factor = numHidden/3; |
| 14585 | int minFactor = 2; |
| 14586 | if (mHomeProcess != null) minFactor++; |
| 14587 | if (mPreviousProcess != null) minFactor++; |
| 14588 | if (factor < minFactor) factor = minFactor; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14589 | step = 0; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14590 | int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14591 | for (i=0; i<N; i++) { |
| 14592 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 14593 | if (app.curAdj >= ProcessList.HOME_APP_ADJ |
| 14594 | && app.curAdj != ProcessList.SERVICE_B_ADJ |
| 14595 | && !app.killedBackground) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14596 | if (app.trimMemoryLevel < curLevel && app.thread != null) { |
| 14597 | try { |
| 14598 | app.thread.scheduleTrimMemory(curLevel); |
| 14599 | } catch (RemoteException e) { |
| 14600 | } |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 14601 | if (false) { |
| 14602 | // For now we won't do this; our memory trimming seems |
| 14603 | // to be good enough at this point that destroying |
| 14604 | // activities causes more harm than good. |
| 14605 | if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE |
| 14606 | && app != mHomeProcess && app != mPreviousProcess) { |
| 14607 | // For these apps we will also finish their activities |
| 14608 | // to help them free memory. |
| 14609 | mMainStack.destroyActivitiesLocked(app, false, "trim"); |
| 14610 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14611 | } |
| 14612 | } |
| 14613 | app.trimMemoryLevel = curLevel; |
| 14614 | step++; |
| 14615 | if (step >= factor) { |
| 14616 | switch (curLevel) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14617 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 14618 | curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14619 | break; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14620 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 14621 | curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14622 | break; |
| 14623 | } |
| 14624 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14625 | } else if (app.curAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14626 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14627 | && app.thread != null) { |
| 14628 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14629 | app.thread.scheduleTrimMemory( |
| 14630 | ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14631 | } catch (RemoteException e) { |
| 14632 | } |
| 14633 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14634 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14635 | } else if ((app.curAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14636 | && app.pendingUiClean) { |
| 14637 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 14638 | && app.thread != null) { |
| 14639 | try { |
| 14640 | app.thread.scheduleTrimMemory( |
| 14641 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 14642 | } catch (RemoteException e) { |
| 14643 | } |
| 14644 | } |
| 14645 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 14646 | app.pendingUiClean = false; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14647 | } else { |
| 14648 | app.trimMemoryLevel = 0; |
| 14649 | } |
| 14650 | } |
| 14651 | } else { |
| 14652 | final int N = mLruProcesses.size(); |
| 14653 | for (i=0; i<N; i++) { |
| 14654 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14655 | if ((app.curAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14656 | && app.pendingUiClean) { |
| 14657 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 14658 | && app.thread != null) { |
| 14659 | try { |
| 14660 | app.thread.scheduleTrimMemory( |
| 14661 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 14662 | } catch (RemoteException e) { |
| 14663 | } |
| 14664 | } |
| 14665 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 14666 | app.pendingUiClean = false; |
| 14667 | } else { |
| 14668 | app.trimMemoryLevel = 0; |
| 14669 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14670 | } |
| 14671 | } |
| 14672 | |
| 14673 | if (mAlwaysFinishActivities) { |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 14674 | mMainStack.destroyActivitiesLocked(null, false, "always-finish"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14675 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14676 | } |
| 14677 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14678 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14679 | synchronized (this) { |
| 14680 | int i; |
| 14681 | |
| 14682 | // First remove any unused application processes whose package |
| 14683 | // has been removed. |
| 14684 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 14685 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 14686 | if (app.activities.size() == 0 |
| 14687 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 14688 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14689 | TAG, "Exiting empty application process " |
| 14690 | + app.processName + " (" |
| 14691 | + (app.thread != null ? app.thread.asBinder() : null) |
| 14692 | + ")\n"); |
| 14693 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 14694 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14695 | app.processName, app.setAdj, "empty"); |
| 14696 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14697 | } else { |
| 14698 | try { |
| 14699 | app.thread.scheduleExit(); |
| 14700 | } catch (Exception e) { |
| 14701 | // Ignore exceptions. |
| 14702 | } |
| 14703 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 14704 | cleanUpApplicationRecordLocked(app, false, true, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14705 | mRemovedProcesses.remove(i); |
| 14706 | |
| 14707 | if (app.persistent) { |
| 14708 | if (app.persistent) { |
| 14709 | addAppLocked(app.info); |
| 14710 | } |
| 14711 | } |
| 14712 | } |
| 14713 | } |
| 14714 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14715 | // Now update the oom adj for all processes. |
| 14716 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14717 | } |
| 14718 | } |
| 14719 | |
| 14720 | /** This method sends the specified signal to each of the persistent apps */ |
| 14721 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 14722 | if (sig != Process.SIGNAL_USR1) { |
| 14723 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 14724 | } |
| 14725 | |
| 14726 | synchronized (this) { |
| 14727 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 14728 | != PackageManager.PERMISSION_GRANTED) { |
| 14729 | throw new SecurityException("Requires permission " |
| 14730 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 14731 | } |
| 14732 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14733 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 14734 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14735 | if (r.thread != null && r.persistent) { |
| 14736 | Process.sendSignal(r.pid, sig); |
| 14737 | } |
| 14738 | } |
| 14739 | } |
| 14740 | } |
| 14741 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14742 | private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) { |
| 14743 | if (proc == null || proc == mProfileProc) { |
| 14744 | proc = mProfileProc; |
| 14745 | path = mProfileFile; |
| 14746 | profileType = mProfileType; |
| 14747 | clearProfilerLocked(); |
| 14748 | } |
| 14749 | if (proc == null) { |
| 14750 | return; |
| 14751 | } |
| 14752 | try { |
| 14753 | proc.thread.profilerControl(false, path, null, profileType); |
| 14754 | } catch (RemoteException e) { |
| 14755 | throw new IllegalStateException("Process disappeared"); |
| 14756 | } |
| 14757 | } |
| 14758 | |
| 14759 | private void clearProfilerLocked() { |
| 14760 | if (mProfileFd != null) { |
| 14761 | try { |
| 14762 | mProfileFd.close(); |
| 14763 | } catch (IOException e) { |
| 14764 | } |
| 14765 | } |
| 14766 | mProfileApp = null; |
| 14767 | mProfileProc = null; |
| 14768 | mProfileFile = null; |
| 14769 | mProfileType = 0; |
| 14770 | mAutoStopProfiler = false; |
| 14771 | } |
| 14772 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14773 | public boolean profileControl(String process, boolean start, |
Romain Guy | 7eabe55 | 2011-07-21 14:56:34 -0700 | [diff] [blame] | 14774 | String path, ParcelFileDescriptor fd, int profileType) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14775 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14776 | try { |
| 14777 | synchronized (this) { |
| 14778 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14779 | // its own permission. |
| 14780 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14781 | != PackageManager.PERMISSION_GRANTED) { |
| 14782 | throw new SecurityException("Requires permission " |
| 14783 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14784 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14785 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14786 | if (start && fd == null) { |
| 14787 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14788 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14789 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14790 | ProcessRecord proc = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14791 | if (process != null) { |
| 14792 | try { |
| 14793 | int pid = Integer.parseInt(process); |
| 14794 | synchronized (mPidsSelfLocked) { |
| 14795 | proc = mPidsSelfLocked.get(pid); |
| 14796 | } |
| 14797 | } catch (NumberFormatException e) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14798 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14799 | |
| 14800 | if (proc == null) { |
| 14801 | HashMap<String, SparseArray<ProcessRecord>> all |
| 14802 | = mProcessNames.getMap(); |
| 14803 | SparseArray<ProcessRecord> procs = all.get(process); |
| 14804 | if (procs != null && procs.size() > 0) { |
| 14805 | proc = procs.valueAt(0); |
| 14806 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14807 | } |
| 14808 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14809 | |
| 14810 | if (start && (proc == null || proc.thread == null)) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14811 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14812 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14813 | |
| 14814 | if (start) { |
| 14815 | stopProfilerLocked(null, null, 0); |
| 14816 | setProfileApp(proc.info, proc.processName, path, fd, false); |
| 14817 | mProfileProc = proc; |
| 14818 | mProfileType = profileType; |
| 14819 | try { |
| 14820 | fd = fd.dup(); |
| 14821 | } catch (IOException e) { |
| 14822 | fd = null; |
| 14823 | } |
| 14824 | proc.thread.profilerControl(start, path, fd, profileType); |
| 14825 | fd = null; |
| 14826 | mProfileFd = null; |
| 14827 | } else { |
| 14828 | stopProfilerLocked(proc, path, profileType); |
| 14829 | if (fd != null) { |
| 14830 | try { |
| 14831 | fd.close(); |
| 14832 | } catch (IOException e) { |
| 14833 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14834 | } |
| 14835 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14836 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14837 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14838 | } |
| 14839 | } catch (RemoteException e) { |
| 14840 | throw new IllegalStateException("Process disappeared"); |
| 14841 | } finally { |
| 14842 | if (fd != null) { |
| 14843 | try { |
| 14844 | fd.close(); |
| 14845 | } catch (IOException e) { |
| 14846 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14847 | } |
| 14848 | } |
| 14849 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14850 | |
| 14851 | public boolean dumpHeap(String process, boolean managed, |
| 14852 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 14853 | |
| 14854 | try { |
| 14855 | synchronized (this) { |
| 14856 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14857 | // its own permission (same as profileControl). |
| 14858 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14859 | != PackageManager.PERMISSION_GRANTED) { |
| 14860 | throw new SecurityException("Requires permission " |
| 14861 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 14862 | } |
| 14863 | |
| 14864 | if (fd == null) { |
| 14865 | throw new IllegalArgumentException("null fd"); |
| 14866 | } |
| 14867 | |
| 14868 | ProcessRecord proc = null; |
| 14869 | try { |
| 14870 | int pid = Integer.parseInt(process); |
| 14871 | synchronized (mPidsSelfLocked) { |
| 14872 | proc = mPidsSelfLocked.get(pid); |
| 14873 | } |
| 14874 | } catch (NumberFormatException e) { |
| 14875 | } |
| 14876 | |
| 14877 | if (proc == null) { |
| 14878 | HashMap<String, SparseArray<ProcessRecord>> all |
| 14879 | = mProcessNames.getMap(); |
| 14880 | SparseArray<ProcessRecord> procs = all.get(process); |
| 14881 | if (procs != null && procs.size() > 0) { |
| 14882 | proc = procs.valueAt(0); |
| 14883 | } |
| 14884 | } |
| 14885 | |
| 14886 | if (proc == null || proc.thread == null) { |
| 14887 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14888 | } |
| 14889 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 14890 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 14891 | if (!isDebuggable) { |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14892 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 14893 | throw new SecurityException("Process not debuggable: " + proc); |
| 14894 | } |
| 14895 | } |
| 14896 | |
| 14897 | proc.thread.dumpHeap(managed, path, fd); |
| 14898 | fd = null; |
| 14899 | return true; |
| 14900 | } |
| 14901 | } catch (RemoteException e) { |
| 14902 | throw new IllegalStateException("Process disappeared"); |
| 14903 | } finally { |
| 14904 | if (fd != null) { |
| 14905 | try { |
| 14906 | fd.close(); |
| 14907 | } catch (IOException e) { |
| 14908 | } |
| 14909 | } |
| 14910 | } |
| 14911 | } |
| 14912 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14913 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 14914 | public void monitor() { |
| 14915 | synchronized (this) { } |
| 14916 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 14917 | |
| 14918 | public void onCoreSettingsChange(Bundle settings) { |
| 14919 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 14920 | ProcessRecord processRecord = mLruProcesses.get(i); |
| 14921 | try { |
| 14922 | if (processRecord.thread != null) { |
| 14923 | processRecord.thread.setCoreSettings(settings); |
| 14924 | } |
| 14925 | } catch (RemoteException re) { |
| 14926 | /* ignore */ |
| 14927 | } |
| 14928 | } |
| 14929 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14930 | |
| 14931 | // Multi-user methods |
| 14932 | |
| 14933 | public boolean switchUser(int userid) { |
| 14934 | // TODO |
| 14935 | return true; |
| 14936 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14937 | } |