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.IApplicationThread; |
| 42 | import android.app.IInstrumentationWatcher; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 43 | import android.app.INotificationManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 44 | import android.app.IProcessObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.app.IServiceConnection; |
| 46 | import android.app.IThumbnailReceiver; |
| 47 | import android.app.Instrumentation; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 48 | import android.app.Notification; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 49 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.app.PendingIntent; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 51 | import android.app.Service; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 52 | import android.app.backup.IBackupManager; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 53 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 54 | import android.content.BroadcastReceiver; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 55 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.content.ComponentName; |
| 57 | import android.content.ContentResolver; |
| 58 | import android.content.Context; |
Christian Mehlmauer | 7664e20 | 2010-07-20 08:46:17 +0200 | [diff] [blame] | 59 | import android.content.DialogInterface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.content.Intent; |
| 61 | import android.content.IntentFilter; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 62 | import android.content.IIntentReceiver; |
| 63 | import android.content.IIntentSender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 64 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.content.pm.ActivityInfo; |
| 66 | import android.content.pm.ApplicationInfo; |
| 67 | import android.content.pm.ConfigurationInfo; |
| 68 | import android.content.pm.IPackageDataObserver; |
| 69 | import android.content.pm.IPackageManager; |
| 70 | import android.content.pm.InstrumentationInfo; |
Dan Egnor | 66c40e7 | 2010-01-26 16:23:11 -0800 | [diff] [blame] | 71 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import android.content.pm.PackageManager; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 73 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | import android.content.pm.ProviderInfo; |
| 75 | import android.content.pm.ResolveInfo; |
| 76 | import android.content.pm.ServiceInfo; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 77 | import android.content.pm.UserInfo; |
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; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 107 | import android.os.UserId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | import android.util.EventLog; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 110 | import android.util.Pair; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 111 | import android.util.Slog; |
Joe Onorato | 5d3bea6 | 2010-03-01 13:44:29 -0800 | [diff] [blame] | 112 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | import android.util.PrintWriterPrinter; |
| 114 | import android.util.SparseArray; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 115 | import android.util.SparseIntArray; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 116 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | import android.view.Gravity; |
| 118 | import android.view.LayoutInflater; |
| 119 | import android.view.View; |
| 120 | import android.view.WindowManager; |
| 121 | import android.view.WindowManagerPolicy; |
| 122 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 123 | import java.io.BufferedInputStream; |
| 124 | import java.io.BufferedOutputStream; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 125 | import java.io.BufferedReader; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 126 | import java.io.DataInputStream; |
| 127 | import java.io.DataOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | import java.io.File; |
| 129 | import java.io.FileDescriptor; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 130 | import java.io.FileInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | import java.io.FileNotFoundException; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 132 | import java.io.FileOutputStream; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 133 | import java.io.IOException; |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 134 | import java.io.InputStreamReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | import java.io.PrintWriter; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 136 | import java.io.StringWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | import java.lang.IllegalStateException; |
| 138 | import java.lang.ref.WeakReference; |
| 139 | import java.util.ArrayList; |
Dianne Hackborn | cbb722e | 2012-02-07 18:33:49 -0800 | [diff] [blame] | 140 | import java.util.Arrays; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 141 | import java.util.Collection; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 142 | import java.util.Collections; |
| 143 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | import java.util.HashMap; |
| 145 | import java.util.HashSet; |
| 146 | import java.util.Iterator; |
| 147 | import java.util.List; |
| 148 | import java.util.Locale; |
| 149 | import java.util.Map; |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 150 | import java.util.Set; |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 151 | import java.util.concurrent.atomic.AtomicBoolean; |
| 152 | import java.util.concurrent.atomic.AtomicLong; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 154 | public final class ActivityManagerService extends ActivityManagerNative |
| 155 | implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 156 | private static final String USER_DATA_DIR = "/data/user/"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | static final String TAG = "ActivityManager"; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 158 | static final String TAG_MU = "ActivityManagerServiceMU"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | static final boolean DEBUG = false; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 160 | static final boolean localLOGV = DEBUG; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | static final boolean DEBUG_SWITCH = localLOGV || false; |
| 162 | static final boolean DEBUG_TASKS = localLOGV || false; |
| 163 | static final boolean DEBUG_PAUSE = localLOGV || false; |
| 164 | static final boolean DEBUG_OOM_ADJ = localLOGV || false; |
| 165 | static final boolean DEBUG_TRANSITION = localLOGV || false; |
| 166 | static final boolean DEBUG_BROADCAST = localLOGV || false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 167 | static final boolean DEBUG_BACKGROUND_BROADCAST = DEBUG_BROADCAST || false; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 168 | static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | static final boolean DEBUG_SERVICE = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 170 | static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | static final boolean DEBUG_VISBILITY = localLOGV || false; |
| 172 | static final boolean DEBUG_PROCESSES = localLOGV || false; |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 173 | static final boolean DEBUG_PROVIDER = localLOGV || false; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 174 | static final boolean DEBUG_URI_PERMISSION = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | static final boolean DEBUG_USER_LEAVING = localLOGV || false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 176 | static final boolean DEBUG_RESULTS = localLOGV || false; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 177 | static final boolean DEBUG_BACKUP = localLOGV || false; |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 178 | static final boolean DEBUG_CONFIGURATION = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 179 | static final boolean DEBUG_POWER = localLOGV || false; |
| 180 | static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 181 | static final boolean DEBUG_MU = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | static final boolean VALIDATE_TOKENS = false; |
| 183 | static final boolean SHOW_ACTIVITY_START_TIME = true; |
| 184 | |
| 185 | // Control over CPU and battery monitoring. |
| 186 | static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes. |
| 187 | static final boolean MONITOR_CPU_USAGE = true; |
| 188 | static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds. |
| 189 | static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample. |
| 190 | static final boolean MONITOR_THREAD_CPU_USAGE = false; |
| 191 | |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 192 | // 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] | 193 | static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 194 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 195 | private static final String SYSTEM_DEBUGGABLE = "ro.debuggable"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | // Maximum number of recent tasks that we can remember. |
| 198 | static final int MAX_RECENT_TASKS = 20; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 199 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 200 | // Amount of time after a call to stopAppSwitches() during which we will |
| 201 | // prevent further untrusted switches from happening. |
| 202 | static final long APP_SWITCH_DELAY_TIME = 5*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | |
| 204 | // How long we wait for a launched process to attach to the activity manager |
| 205 | // before we decide it's never going to come up for real. |
| 206 | static final int PROC_START_TIMEOUT = 10*1000; |
| 207 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 208 | // How long we wait for a launched process to attach to the activity manager |
| 209 | // before we decide it's never going to come up for real, when the process was |
| 210 | // started with a wrapper for instrumentation (such as Valgrind) because it |
| 211 | // could take much longer than usual. |
| 212 | static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000; |
| 213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | // How long to wait after going idle before forcing apps to GC. |
| 215 | static final int GC_TIMEOUT = 5*1000; |
| 216 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 217 | // The minimum amount of time between successive GC requests for a process. |
| 218 | static final int GC_MIN_INTERVAL = 60*1000; |
| 219 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 220 | // The rate at which we check for apps using excessive power -- 15 mins. |
| 221 | static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000; |
| 222 | |
| 223 | // The minimum sample duration we will allow before deciding we have |
| 224 | // enough data on wake locks to start killing things. |
| 225 | static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
| 226 | |
| 227 | // The minimum sample duration we will allow before deciding we have |
| 228 | // enough data on CPU usage to start killing things. |
| 229 | 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] | 230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | // How long we allow a receiver to run before giving up on it. |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 232 | static final int BROADCAST_FG_TIMEOUT = 10*1000; |
| 233 | static final int BROADCAST_BG_TIMEOUT = 60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | |
| 235 | // How long we wait for a service to finish executing. |
| 236 | static final int SERVICE_TIMEOUT = 20*1000; |
| 237 | |
| 238 | // How long a service needs to be running until restarting its process |
| 239 | // is no longer considered to be a relaunch of the service. |
| 240 | static final int SERVICE_RESTART_DURATION = 5*1000; |
| 241 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 242 | // How long a service needs to be running until it will start back at |
| 243 | // SERVICE_RESTART_DURATION after being killed. |
| 244 | static final int SERVICE_RESET_RUN_DURATION = 60*1000; |
| 245 | |
| 246 | // Multiplying factor to increase restart duration time by, for each time |
| 247 | // a service is killed before it has run for SERVICE_RESET_RUN_DURATION. |
| 248 | static final int SERVICE_RESTART_DURATION_FACTOR = 4; |
| 249 | |
| 250 | // The minimum amount of time between restarting services that we allow. |
| 251 | // That is, when multiple services are restarting, we won't allow each |
| 252 | // to restart less than this amount of time from the last one. |
| 253 | static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000; |
| 254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | // Maximum amount of time for there to be no activity on a service before |
| 256 | // we consider it non-essential and allow its process to go on the |
| 257 | // LRU background list. |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 258 | static final int MAX_SERVICE_INACTIVITY = 30*60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | |
| 260 | // How long we wait until we timeout on key dispatching. |
| 261 | static final int KEY_DISPATCHING_TIMEOUT = 5*1000; |
| 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | // How long we wait until we timeout on key dispatching during instrumentation. |
| 264 | static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000; |
| 265 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 266 | static final int MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | |
| 268 | static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 269 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 270 | public ActivityStack mMainStack; |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 271 | |
| 272 | private final boolean mHeadless; |
| 273 | |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 274 | // Whether we should show our dialogs (ANR, crash, etc) or just perform their |
| 275 | // default actuion automatically. Important for devices without direct input |
| 276 | // devices. |
| 277 | private boolean mShowDialogs = true; |
| 278 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 280 | * Description of a request to start a new activity, which has been held |
| 281 | * due to app switches being disabled. |
| 282 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 283 | static class PendingActivityLaunch { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 284 | ActivityRecord r; |
| 285 | ActivityRecord sourceRecord; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 286 | Uri[] grantedUriPermissions; |
| 287 | int grantedMode; |
| 288 | boolean onlyIfNeeded; |
| 289 | } |
| 290 | |
| 291 | final ArrayList<PendingActivityLaunch> mPendingActivityLaunches |
| 292 | = new ArrayList<PendingActivityLaunch>(); |
| 293 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame^] | 295 | BroadcastQueue mFgBroadcastQueue; |
| 296 | BroadcastQueue mBgBroadcastQueue; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 297 | // Convenient for easy iteration over the queues. Foreground is first |
| 298 | // so that dispatch of foreground broadcasts gets precedence. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame^] | 299 | final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2]; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 300 | |
| 301 | BroadcastQueue broadcastQueueForIntent(Intent intent) { |
| 302 | final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0; |
| 303 | if (DEBUG_BACKGROUND_BROADCAST) { |
| 304 | Slog.i(TAG, "Broadcast intent " + intent + " on " |
| 305 | + (isFg ? "foreground" : "background") |
| 306 | + " queue"); |
| 307 | } |
| 308 | return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue; |
| 309 | } |
| 310 | |
| 311 | BroadcastRecord broadcastRecordForReceiverLocked(IBinder receiver) { |
| 312 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 313 | BroadcastRecord r = queue.getMatchingOrderedReceiver(receiver); |
| 314 | if (r != null) { |
| 315 | return r; |
| 316 | } |
| 317 | } |
| 318 | return null; |
| 319 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | |
| 321 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | * Activity we have told the window manager to have key focus. |
| 323 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 324 | ActivityRecord mFocusedActivity = null; |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 325 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | * List of intents that were used to start the most recent tasks. |
| 327 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 328 | final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | |
| 330 | /** |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 331 | * Process management. |
| 332 | */ |
| 333 | final ProcessList mProcessList = new ProcessList(); |
| 334 | |
| 335 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | * All of the applications we currently have running organized by name. |
| 337 | * The keys are strings of the application package name (as |
| 338 | * returned by the package manager), and the keys are ApplicationRecord |
| 339 | * objects. |
| 340 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 341 | final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | |
| 343 | /** |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 344 | * The currently running isolated processes. |
| 345 | */ |
| 346 | final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>(); |
| 347 | |
| 348 | /** |
| 349 | * Counter for assigning isolated process uids, to avoid frequently reusing the |
| 350 | * same ones. |
| 351 | */ |
| 352 | int mNextIsolatedProcessUid = 0; |
| 353 | |
| 354 | /** |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 355 | * The currently running heavy-weight process, if any. |
| 356 | */ |
| 357 | ProcessRecord mHeavyWeightProcess = null; |
| 358 | |
| 359 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | * The last time that various processes have crashed. |
| 361 | */ |
| 362 | final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>(); |
| 363 | |
| 364 | /** |
| 365 | * Set of applications that we consider to be bad, and will reject |
| 366 | * incoming broadcasts from (which the user has no control over). |
| 367 | * Processes are added to this set when they have crashed twice within |
| 368 | * a minimum amount of time; they are removed from it when they are |
| 369 | * later restarted (hopefully due to some user action). The value is the |
| 370 | * time it was added to the list. |
| 371 | */ |
| 372 | final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>(); |
| 373 | |
| 374 | /** |
| 375 | * All of the processes we currently have running organized by pid. |
| 376 | * The keys are the pid running the application. |
| 377 | * |
| 378 | * <p>NOTE: This object is protected by its own lock, NOT the global |
| 379 | * activity manager lock! |
| 380 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 381 | final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | |
| 383 | /** |
| 384 | * All of the processes that have been forced to be foreground. The key |
| 385 | * is the pid of the caller who requested it (we hold a death |
| 386 | * link on it). |
| 387 | */ |
| 388 | abstract class ForegroundToken implements IBinder.DeathRecipient { |
| 389 | int pid; |
| 390 | IBinder token; |
| 391 | } |
| 392 | final SparseArray<ForegroundToken> mForegroundProcesses |
| 393 | = new SparseArray<ForegroundToken>(); |
| 394 | |
| 395 | /** |
| 396 | * List of records for processes that someone had tried to start before the |
| 397 | * system was ready. We don't start them at that point, but ensure they |
| 398 | * are started by the time booting is complete. |
| 399 | */ |
| 400 | final ArrayList<ProcessRecord> mProcessesOnHold |
| 401 | = new ArrayList<ProcessRecord>(); |
| 402 | |
| 403 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | * List of persistent applications that are in the process |
| 405 | * of being started. |
| 406 | */ |
| 407 | final ArrayList<ProcessRecord> mPersistentStartingProcesses |
| 408 | = new ArrayList<ProcessRecord>(); |
| 409 | |
| 410 | /** |
| 411 | * Processes that are being forcibly torn down. |
| 412 | */ |
| 413 | final ArrayList<ProcessRecord> mRemovedProcesses |
| 414 | = new ArrayList<ProcessRecord>(); |
| 415 | |
| 416 | /** |
| 417 | * List of running applications, sorted by recent usage. |
| 418 | * The first entry in the list is the least recently used. |
| 419 | * It contains ApplicationRecord objects. This list does NOT include |
| 420 | * any persistent application records (since we never want to exit them). |
| 421 | */ |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 422 | final ArrayList<ProcessRecord> mLruProcesses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | = new ArrayList<ProcessRecord>(); |
| 424 | |
| 425 | /** |
| 426 | * List of processes that should gc as soon as things are idle. |
| 427 | */ |
| 428 | final ArrayList<ProcessRecord> mProcessesToGc |
| 429 | = new ArrayList<ProcessRecord>(); |
| 430 | |
| 431 | /** |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 432 | * This is the process holding what we currently consider to be |
| 433 | * the "home" activity. |
| 434 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 435 | ProcessRecord mHomeProcess; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 436 | |
| 437 | /** |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 438 | * This is the process holding the activity the user last visited that |
| 439 | * is in a different process from the one they are currently in. |
| 440 | */ |
| 441 | ProcessRecord mPreviousProcess; |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 442 | |
| 443 | /** |
| 444 | * The time at which the previous process was last visible. |
| 445 | */ |
| 446 | long mPreviousProcessVisibleTime; |
| 447 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 448 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 449 | * Packages that the user has asked to have run in screen size |
| 450 | * compatibility mode instead of filling the screen. |
| 451 | */ |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 452 | final CompatModePackages mCompatModePackages; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 453 | |
| 454 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | * Set of PendingResultRecord objects that are currently active. |
| 456 | */ |
| 457 | final HashSet mPendingResultRecords = new HashSet(); |
| 458 | |
| 459 | /** |
| 460 | * Set of IntentSenderRecord objects that are currently active. |
| 461 | */ |
| 462 | final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords |
| 463 | = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>(); |
| 464 | |
| 465 | /** |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 466 | * Fingerprints (hashCode()) of stack traces that we've |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 467 | * already logged DropBox entries for. Guarded by itself. If |
| 468 | * something (rogue user app) forces this over |
| 469 | * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared. |
| 470 | */ |
| 471 | private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>(); |
| 472 | private static final int MAX_DUP_SUPPRESSED_STACKS = 5000; |
| 473 | |
| 474 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 475 | * Strict Mode background batched logging state. |
| 476 | * |
| 477 | * The string buffer is guarded by itself, and its lock is also |
| 478 | * used to determine if another batched write is already |
| 479 | * in-flight. |
| 480 | */ |
| 481 | private final StringBuilder mStrictModeBuffer = new StringBuilder(); |
| 482 | |
| 483 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | * Keeps track of all IIntentReceivers that have been registered for |
| 485 | * broadcasts. Hash keys are the receiver IBinder, hash value is |
| 486 | * a ReceiverList. |
| 487 | */ |
| 488 | final HashMap mRegisteredReceivers = new HashMap(); |
| 489 | |
| 490 | /** |
| 491 | * Resolver for broadcast intents to registered receivers. |
| 492 | * Holds BroadcastFilter (subclass of IntentFilter). |
| 493 | */ |
| 494 | final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver |
| 495 | = new IntentResolver<BroadcastFilter, BroadcastFilter>() { |
| 496 | @Override |
| 497 | protected boolean allowFilterResult( |
| 498 | BroadcastFilter filter, List<BroadcastFilter> dest) { |
| 499 | IBinder target = filter.receiverList.receiver.asBinder(); |
| 500 | for (int i=dest.size()-1; i>=0; i--) { |
| 501 | if (dest.get(i).receiverList.receiver.asBinder() == target) { |
| 502 | return false; |
| 503 | } |
| 504 | } |
| 505 | return true; |
| 506 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 507 | |
| 508 | @Override |
| 509 | protected String packageForFilter(BroadcastFilter filter) { |
| 510 | return filter.packageName; |
| 511 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | }; |
| 513 | |
| 514 | /** |
| 515 | * State of all active sticky broadcasts. Keys are the action of the |
| 516 | * sticky Intent, values are an ArrayList of all broadcasted intents with |
| 517 | * that action (which should usually be one). |
| 518 | */ |
| 519 | final HashMap<String, ArrayList<Intent>> mStickyBroadcasts = |
| 520 | new HashMap<String, ArrayList<Intent>>(); |
| 521 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 522 | final ServiceMap mServiceMap = new ServiceMap(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | |
| 524 | /** |
| 525 | * All currently bound service connections. Keys are the IBinder of |
| 526 | * the client's IServiceConnection. |
| 527 | */ |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 528 | final HashMap<IBinder, ArrayList<ConnectionRecord>> mServiceConnections |
| 529 | = new HashMap<IBinder, ArrayList<ConnectionRecord>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | |
| 531 | /** |
| 532 | * List of services that we have been asked to start, |
| 533 | * but haven't yet been able to. It is used to hold start requests |
| 534 | * while waiting for their corresponding application thread to get |
| 535 | * going. |
| 536 | */ |
| 537 | final ArrayList<ServiceRecord> mPendingServices |
| 538 | = new ArrayList<ServiceRecord>(); |
| 539 | |
| 540 | /** |
| 541 | * List of services that are scheduled to restart following a crash. |
| 542 | */ |
| 543 | final ArrayList<ServiceRecord> mRestartingServices |
| 544 | = new ArrayList<ServiceRecord>(); |
| 545 | |
| 546 | /** |
| 547 | * List of services that are in the process of being stopped. |
| 548 | */ |
| 549 | final ArrayList<ServiceRecord> mStoppingServices |
| 550 | = new ArrayList<ServiceRecord>(); |
| 551 | |
| 552 | /** |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 553 | * Backup/restore process management |
| 554 | */ |
| 555 | String mBackupAppName = null; |
| 556 | BackupRecord mBackupTarget = null; |
| 557 | |
| 558 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | * List of PendingThumbnailsRecord objects of clients who are still |
| 560 | * waiting to receive all of the thumbnails for a task. |
| 561 | */ |
| 562 | final ArrayList mPendingThumbnails = new ArrayList(); |
| 563 | |
| 564 | /** |
| 565 | * List of HistoryRecord objects that have been finished and must |
| 566 | * still report back to a pending thumbnail receiver. |
| 567 | */ |
| 568 | final ArrayList mCancelledThumbnails = new ArrayList(); |
| 569 | |
| 570 | /** |
| 571 | * All of the currently running global content providers. Keys are a |
| 572 | * string containing the provider name and values are a |
| 573 | * ContentProviderRecord object containing the data about it. Note |
| 574 | * that a single provider may be published under multiple names, so |
| 575 | * there may be multiple entries here for a single one in mProvidersByClass. |
| 576 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 577 | final HashMap<String, ContentProviderRecord> mProvidersByName |
| 578 | = new HashMap<String, ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | |
| 580 | /** |
| 581 | * All of the currently running global content providers. Keys are a |
| 582 | * string containing the provider's implementation class and values are a |
| 583 | * ContentProviderRecord object containing the data about it. |
| 584 | */ |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 585 | final HashMap<ComponentName, ContentProviderRecord> mProvidersByClass |
| 586 | = new HashMap<ComponentName, ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 588 | final ProviderMap mProviderMap = new ProviderMap(); |
| 589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | /** |
| 591 | * List of content providers who have clients waiting for them. The |
| 592 | * application is currently being launched and the provider will be |
| 593 | * removed from this list once it is published. |
| 594 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 595 | final ArrayList<ContentProviderRecord> mLaunchingProviders |
| 596 | = new ArrayList<ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | |
| 598 | /** |
| 599 | * Global set of specific Uri permissions that have been granted. |
| 600 | */ |
| 601 | final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions |
| 602 | = new SparseArray<HashMap<Uri, UriPermission>>(); |
| 603 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 604 | CoreSettingsObserver mCoreSettingsObserver; |
| 605 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | /** |
| 607 | * Thread-local storage used to carry caller permissions over through |
| 608 | * indirect content-provider access. |
| 609 | * @see #ActivityManagerService.openContentUri() |
| 610 | */ |
| 611 | private class Identity { |
| 612 | public int pid; |
| 613 | public int uid; |
| 614 | |
| 615 | Identity(int _pid, int _uid) { |
| 616 | pid = _pid; |
| 617 | uid = _uid; |
| 618 | } |
| 619 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 620 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>(); |
| 622 | |
| 623 | /** |
| 624 | * All information we have collected about the runtime performance of |
| 625 | * any user id that can impact battery performance. |
| 626 | */ |
| 627 | final BatteryStatsService mBatteryStatsService; |
| 628 | |
| 629 | /** |
| 630 | * information about component usage |
| 631 | */ |
| 632 | final UsageStatsService mUsageStatsService; |
| 633 | |
| 634 | /** |
| 635 | * Current configuration information. HistoryRecord objects are given |
| 636 | * a reference to this object to indicate which configuration they are |
| 637 | * currently running in, so this object must be kept immutable. |
| 638 | */ |
| 639 | Configuration mConfiguration = new Configuration(); |
| 640 | |
| 641 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 642 | * Current sequencing integer of the configuration, for skipping old |
| 643 | * configurations. |
| 644 | */ |
| 645 | int mConfigurationSeq = 0; |
| 646 | |
| 647 | /** |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 648 | * Hardware-reported OpenGLES version. |
| 649 | */ |
| 650 | final int GL_ES_VERSION; |
| 651 | |
| 652 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | * List of initialization arguments to pass to all processes when binding applications to them. |
| 654 | * For example, references to the commonly used services. |
| 655 | */ |
| 656 | HashMap<String, IBinder> mAppBindArgs; |
| 657 | |
| 658 | /** |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 659 | * Temporary to avoid allocations. Protected by main lock. |
| 660 | */ |
| 661 | final StringBuilder mStringBuilder = new StringBuilder(256); |
| 662 | |
| 663 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | * Used to control how we initialize the service. |
| 665 | */ |
| 666 | boolean mStartRunning = false; |
| 667 | ComponentName mTopComponent; |
| 668 | String mTopAction; |
| 669 | String mTopData; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 670 | boolean mProcessesReady = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 671 | boolean mSystemReady = false; |
| 672 | boolean mBooting = false; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 673 | boolean mWaitingUpdate = false; |
| 674 | boolean mDidUpdate = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 675 | boolean mOnBattery = false; |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 676 | boolean mLaunchWarningShown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | |
| 678 | Context mContext; |
| 679 | |
| 680 | int mFactoryTest; |
| 681 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 682 | boolean mCheckedForSetup; |
| 683 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 685 | * The time at which we will allow normal application switches again, |
| 686 | * after a call to {@link #stopAppSwitches()}. |
| 687 | */ |
| 688 | long mAppSwitchesAllowedTime; |
| 689 | |
| 690 | /** |
| 691 | * This is set to true after the first switch after mAppSwitchesAllowedTime |
| 692 | * is set; any switches after that will clear the time. |
| 693 | */ |
| 694 | boolean mDidAppSwitch; |
| 695 | |
| 696 | /** |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 697 | * Last time (in realtime) at which we checked for power usage. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 698 | */ |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 699 | long mLastPowerCheckRealtime; |
| 700 | |
| 701 | /** |
| 702 | * Last time (in uptime) at which we checked for power usage. |
| 703 | */ |
| 704 | long mLastPowerCheckUptime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 705 | |
| 706 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | * Set while we are wanting to sleep, to prevent any |
| 708 | * activities from being started/resumed. |
| 709 | */ |
| 710 | boolean mSleeping = false; |
| 711 | |
| 712 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 713 | * Set if we are shutting down the system, similar to sleeping. |
| 714 | */ |
| 715 | boolean mShuttingDown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | |
| 717 | /** |
| 718 | * Task identifier that activities are currently being started |
| 719 | * in. Incremented each time a new task is created. |
| 720 | * todo: Replace this with a TokenSpace class that generates non-repeating |
| 721 | * integers that won't wrap. |
| 722 | */ |
| 723 | int mCurTask = 1; |
| 724 | |
| 725 | /** |
| 726 | * Current sequence id for oom_adj computation traversal. |
| 727 | */ |
| 728 | int mAdjSeq = 0; |
| 729 | |
| 730 | /** |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 731 | * Current sequence id for process LRU updating. |
| 732 | */ |
| 733 | int mLruSeq = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | |
| 735 | /** |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 736 | * Keep track of the number of service processes we last found, to |
| 737 | * determine on the next iteration which should be B services. |
| 738 | */ |
| 739 | int mNumServiceProcs = 0; |
| 740 | int mNewNumServiceProcs = 0; |
| 741 | |
| 742 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 743 | * System monitoring: number of processes that died since the last |
| 744 | * N procs were started. |
| 745 | */ |
| 746 | int[] mProcDeaths = new int[20]; |
| 747 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 748 | /** |
| 749 | * This is set if we had to do a delayed dexopt of an app before launching |
| 750 | * it, to increasing the ANR timeouts in that case. |
| 751 | */ |
| 752 | boolean mDidDexOpt; |
| 753 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | String mDebugApp = null; |
| 755 | boolean mWaitForDebugger = false; |
| 756 | boolean mDebugTransient = false; |
| 757 | String mOrigDebugApp = null; |
| 758 | boolean mOrigWaitForDebugger = false; |
| 759 | boolean mAlwaysFinishActivities = false; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 760 | IActivityController mController = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 761 | String mProfileApp = null; |
| 762 | ProcessRecord mProfileProc = null; |
| 763 | String mProfileFile; |
| 764 | ParcelFileDescriptor mProfileFd; |
| 765 | int mProfileType = 0; |
| 766 | boolean mAutoStopProfiler = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 768 | final RemoteCallbackList<IProcessObserver> mProcessObservers |
| 769 | = new RemoteCallbackList<IProcessObserver>(); |
| 770 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 771 | /** |
| 772 | * Callback of last caller to {@link #requestPss}. |
| 773 | */ |
| 774 | Runnable mRequestPssCallback; |
| 775 | |
| 776 | /** |
| 777 | * Remaining processes for which we are waiting results from the last |
| 778 | * call to {@link #requestPss}. |
| 779 | */ |
| 780 | final ArrayList<ProcessRecord> mRequestPssList |
| 781 | = new ArrayList<ProcessRecord>(); |
| 782 | |
| 783 | /** |
| 784 | * Runtime statistics collection thread. This object's lock is used to |
| 785 | * protect all related state. |
| 786 | */ |
| 787 | final Thread mProcessStatsThread; |
| 788 | |
| 789 | /** |
| 790 | * Used to collect process stats when showing not responding dialog. |
| 791 | * Protected by mProcessStatsThread. |
| 792 | */ |
| 793 | final ProcessStats mProcessStats = new ProcessStats( |
| 794 | MONITOR_THREAD_CPU_USAGE); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 795 | final AtomicLong mLastCpuTime = new AtomicLong(0); |
| 796 | final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true); |
| 797 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | long mLastWriteTime = 0; |
| 799 | |
| 800 | /** |
| 801 | * Set to true after the system has finished booting. |
| 802 | */ |
| 803 | boolean mBooted = false; |
| 804 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 805 | int mProcessLimit = ProcessList.MAX_HIDDEN_APPS; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 806 | int mProcessLimitOverride = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | |
| 808 | WindowManagerService mWindowManager; |
| 809 | |
| 810 | static ActivityManagerService mSelf; |
| 811 | static ActivityThread mSystemThread; |
| 812 | |
| 813 | private final class AppDeathRecipient implements IBinder.DeathRecipient { |
| 814 | final ProcessRecord mApp; |
| 815 | final int mPid; |
| 816 | final IApplicationThread mAppThread; |
| 817 | |
| 818 | AppDeathRecipient(ProcessRecord app, int pid, |
| 819 | IApplicationThread thread) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 820 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | TAG, "New death recipient " + this |
| 822 | + " for thread " + thread.asBinder()); |
| 823 | mApp = app; |
| 824 | mPid = pid; |
| 825 | mAppThread = thread; |
| 826 | } |
| 827 | |
| 828 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 829 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 830 | TAG, "Death received in " + this |
| 831 | + " for thread " + mAppThread.asBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | synchronized(ActivityManagerService.this) { |
| 833 | appDiedLocked(mApp, mPid, mAppThread); |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | static final int SHOW_ERROR_MSG = 1; |
| 839 | static final int SHOW_NOT_RESPONDING_MSG = 2; |
| 840 | static final int SHOW_FACTORY_ERROR_MSG = 3; |
| 841 | static final int UPDATE_CONFIGURATION_MSG = 4; |
| 842 | static final int GC_BACKGROUND_PROCESSES_MSG = 5; |
| 843 | static final int WAIT_FOR_DEBUGGER_MSG = 6; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | static final int SERVICE_TIMEOUT_MSG = 12; |
| 845 | static final int UPDATE_TIME_ZONE = 13; |
| 846 | static final int SHOW_UID_ERROR_MSG = 14; |
| 847 | static final int IM_FEELING_LUCKY_MSG = 15; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | static final int PROC_START_TIMEOUT_MSG = 20; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 849 | static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 850 | static final int KILL_APPLICATION_MSG = 22; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 851 | static final int FINALIZE_PENDING_INTENT_MSG = 23; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 852 | static final int POST_HEAVY_NOTIFICATION_MSG = 24; |
| 853 | static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 854 | static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 855 | static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 856 | static final int CLEAR_DNS_CACHE = 28; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 857 | static final int UPDATE_HTTP_PROXY = 29; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 858 | static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30; |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 859 | static final int DISPATCH_FOREGROUND_ACTIVITIES_CHANGED = 31; |
| 860 | static final int DISPATCH_PROCESS_DIED = 32; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 861 | static final int REPORT_MEM_USAGE = 33; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame^] | 863 | static final int FIRST_ACTIVITY_STACK_MSG = 100; |
| 864 | static final int FIRST_BROADCAST_QUEUE_MSG = 200; |
| 865 | static final int FIRST_COMPAT_MODE_MSG = 300; |
| 866 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | AlertDialog mUidAlert; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 868 | CompatModeDialog mCompatModeDialog; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 869 | long mLastMemUsageReportTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | |
| 871 | final Handler mHandler = new Handler() { |
| 872 | //public Handler() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 873 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | //} |
| 875 | |
| 876 | public void handleMessage(Message msg) { |
| 877 | switch (msg.what) { |
| 878 | case SHOW_ERROR_MSG: { |
| 879 | HashMap data = (HashMap) msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | synchronized (ActivityManagerService.this) { |
| 881 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 882 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 883 | Slog.e(TAG, "App already has crash dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | return; |
| 885 | } |
| 886 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 887 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 888 | Dialog d = new AppErrorDialog(mContext, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | d.show(); |
| 890 | proc.crashDialog = d; |
| 891 | } else { |
| 892 | // The device is asleep, so just pretend that the user |
| 893 | // saw a crash dialog and hit "force quit". |
| 894 | res.set(0); |
| 895 | } |
| 896 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 897 | |
| 898 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | } break; |
| 900 | case SHOW_NOT_RESPONDING_MSG: { |
| 901 | synchronized (ActivityManagerService.this) { |
| 902 | HashMap data = (HashMap) msg.obj; |
| 903 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 904 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 905 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 906 | return; |
| 907 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 908 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 909 | Intent intent = new Intent("android.intent.action.ANR"); |
| 910 | if (!mProcessesReady) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 911 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 912 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 913 | } |
| 914 | broadcastIntentLocked(null, null, intent, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 915 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 916 | false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 917 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 919 | mContext, proc, (ActivityRecord)data.get("activity")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 | d.show(); |
| 921 | proc.anrDialog = d; |
| 922 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 923 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 924 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 926 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 927 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 928 | synchronized (ActivityManagerService.this) { |
| 929 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 930 | if (proc == null) { |
| 931 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 932 | break; |
| 933 | } |
| 934 | if (proc.crashDialog != null) { |
| 935 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 936 | return; |
| 937 | } |
| 938 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 939 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 940 | Dialog d = new StrictModeViolationDialog(mContext, res, proc); |
| 941 | d.show(); |
| 942 | proc.crashDialog = d; |
| 943 | } else { |
| 944 | // The device is asleep, so just pretend that the user |
| 945 | // saw a crash dialog and hit "force quit". |
| 946 | res.set(0); |
| 947 | } |
| 948 | } |
| 949 | ensureBootCompleted(); |
| 950 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | case SHOW_FACTORY_ERROR_MSG: { |
| 952 | Dialog d = new FactoryErrorDialog( |
| 953 | mContext, msg.getData().getCharSequence("msg")); |
| 954 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 955 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | } break; |
| 957 | case UPDATE_CONFIGURATION_MSG: { |
| 958 | final ContentResolver resolver = mContext.getContentResolver(); |
| 959 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 960 | } break; |
| 961 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 962 | synchronized (ActivityManagerService.this) { |
| 963 | performAppGcsIfAppropriateLocked(); |
| 964 | } |
| 965 | } break; |
| 966 | case WAIT_FOR_DEBUGGER_MSG: { |
| 967 | synchronized (ActivityManagerService.this) { |
| 968 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 969 | if (msg.arg1 != 0) { |
| 970 | if (!app.waitedForDebugger) { |
| 971 | Dialog d = new AppWaitingForDebuggerDialog( |
| 972 | ActivityManagerService.this, |
| 973 | mContext, app); |
| 974 | app.waitDialog = d; |
| 975 | app.waitedForDebugger = true; |
| 976 | d.show(); |
| 977 | } |
| 978 | } else { |
| 979 | if (app.waitDialog != null) { |
| 980 | app.waitDialog.dismiss(); |
| 981 | app.waitDialog = null; |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 987 | if (mDidDexOpt) { |
| 988 | mDidDexOpt = false; |
| 989 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 990 | nmsg.obj = msg.obj; |
| 991 | mHandler.sendMessageDelayed(nmsg, SERVICE_TIMEOUT); |
| 992 | return; |
| 993 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 994 | serviceTimeout((ProcessRecord)msg.obj); |
| 995 | } break; |
| 996 | case UPDATE_TIME_ZONE: { |
| 997 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 998 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 999 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | if (r.thread != null) { |
| 1001 | try { |
| 1002 | r.thread.updateTimeZone(); |
| 1003 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1004 | 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] | 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1009 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1010 | case CLEAR_DNS_CACHE: { |
| 1011 | synchronized (ActivityManagerService.this) { |
| 1012 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1013 | ProcessRecord r = mLruProcesses.get(i); |
| 1014 | if (r.thread != null) { |
| 1015 | try { |
| 1016 | r.thread.clearDnsCache(); |
| 1017 | } catch (RemoteException ex) { |
| 1018 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1024 | case UPDATE_HTTP_PROXY: { |
| 1025 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 1026 | String host = ""; |
| 1027 | String port = ""; |
| 1028 | String exclList = ""; |
| 1029 | if (proxy != null) { |
| 1030 | host = proxy.getHost(); |
| 1031 | port = Integer.toString(proxy.getPort()); |
| 1032 | exclList = proxy.getExclusionList(); |
| 1033 | } |
| 1034 | synchronized (ActivityManagerService.this) { |
| 1035 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1036 | ProcessRecord r = mLruProcesses.get(i); |
| 1037 | if (r.thread != null) { |
| 1038 | try { |
| 1039 | r.thread.setHttpProxy(host, port, exclList); |
| 1040 | } catch (RemoteException ex) { |
| 1041 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1042 | r.info.processName); |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | case SHOW_UID_ERROR_MSG: { |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 1049 | String title = "System UIDs Inconsistent"; |
| 1050 | String text = "UIDs on the system are inconsistent, you need to wipe your" |
| 1051 | + " data partition or your device will be unstable."; |
| 1052 | Log.e(TAG, title + ": " + text); |
| 1053 | if (mShowDialogs) { |
| 1054 | // XXX This is a temporary dialog, no need to localize. |
| 1055 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1056 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1057 | d.setCancelable(false); |
| 1058 | d.setTitle(title); |
| 1059 | d.setMessage(text); |
| 1060 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
| 1061 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1062 | mUidAlert = d; |
| 1063 | d.show(); |
| 1064 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1065 | } break; |
| 1066 | case IM_FEELING_LUCKY_MSG: { |
| 1067 | if (mUidAlert != null) { |
| 1068 | mUidAlert.dismiss(); |
| 1069 | mUidAlert = null; |
| 1070 | } |
| 1071 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1073 | if (mDidDexOpt) { |
| 1074 | mDidDexOpt = false; |
| 1075 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1076 | nmsg.obj = msg.obj; |
| 1077 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1078 | return; |
| 1079 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1081 | synchronized (ActivityManagerService.this) { |
| 1082 | processStartTimedOutLocked(app); |
| 1083 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1084 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1085 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1086 | synchronized (ActivityManagerService.this) { |
| 1087 | doPendingActivityLaunchesLocked(true); |
| 1088 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1089 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1090 | case KILL_APPLICATION_MSG: { |
| 1091 | synchronized (ActivityManagerService.this) { |
| 1092 | int uid = msg.arg1; |
| 1093 | boolean restart = (msg.arg2 == 1); |
| 1094 | String pkg = (String) msg.obj; |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 1095 | forceStopPackageLocked(pkg, uid, restart, false, true, false); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1096 | } |
| 1097 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1098 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1099 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1100 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1101 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1102 | INotificationManager inm = NotificationManager.getService(); |
| 1103 | if (inm == null) { |
| 1104 | return; |
| 1105 | } |
| 1106 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1107 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1108 | ProcessRecord process = root.app; |
| 1109 | if (process == null) { |
| 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | try { |
| 1114 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1115 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1116 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1117 | Notification notification = new Notification(); |
| 1118 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1119 | notification.when = 0; |
| 1120 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1121 | notification.tickerText = text; |
| 1122 | notification.defaults = 0; // please be quiet |
| 1123 | notification.sound = null; |
| 1124 | notification.vibrate = null; |
| 1125 | notification.setLatestEventInfo(context, text, |
| 1126 | mContext.getText(R.string.heavy_weight_notification_detail), |
| 1127 | PendingIntent.getActivity(mContext, 0, root.intent, |
| 1128 | PendingIntent.FLAG_CANCEL_CURRENT)); |
| 1129 | |
| 1130 | try { |
| 1131 | int[] outId = new int[1]; |
| 1132 | inm.enqueueNotification("android", R.string.heavy_weight_notification, |
| 1133 | notification, outId); |
| 1134 | } catch (RuntimeException e) { |
| 1135 | Slog.w(ActivityManagerService.TAG, |
| 1136 | "Error showing notification for heavy-weight app", e); |
| 1137 | } catch (RemoteException e) { |
| 1138 | } |
| 1139 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1140 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1141 | } |
| 1142 | } break; |
| 1143 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1144 | INotificationManager inm = NotificationManager.getService(); |
| 1145 | if (inm == null) { |
| 1146 | return; |
| 1147 | } |
| 1148 | try { |
| 1149 | inm.cancelNotification("android", |
| 1150 | R.string.heavy_weight_notification); |
| 1151 | } catch (RuntimeException e) { |
| 1152 | Slog.w(ActivityManagerService.TAG, |
| 1153 | "Error canceling notification for service", e); |
| 1154 | } catch (RemoteException e) { |
| 1155 | } |
| 1156 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1157 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1158 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1159 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1160 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1161 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1162 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1163 | } |
| 1164 | } break; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1165 | case SHOW_COMPAT_MODE_DIALOG_MSG: { |
| 1166 | synchronized (ActivityManagerService.this) { |
| 1167 | ActivityRecord ar = (ActivityRecord)msg.obj; |
| 1168 | if (mCompatModeDialog != null) { |
| 1169 | if (mCompatModeDialog.mAppInfo.packageName.equals( |
| 1170 | ar.info.applicationInfo.packageName)) { |
| 1171 | return; |
| 1172 | } |
| 1173 | mCompatModeDialog.dismiss(); |
| 1174 | mCompatModeDialog = null; |
| 1175 | } |
Dianne Hackborn | 2947826 | 2011-06-07 15:44:22 -0700 | [diff] [blame] | 1176 | if (ar != null && false) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1177 | if (mCompatModePackages.getPackageAskCompatModeLocked( |
| 1178 | ar.packageName)) { |
| 1179 | int mode = mCompatModePackages.computeCompatModeLocked( |
| 1180 | ar.info.applicationInfo); |
| 1181 | if (mode == ActivityManager.COMPAT_MODE_DISABLED |
| 1182 | || mode == ActivityManager.COMPAT_MODE_ENABLED) { |
| 1183 | mCompatModeDialog = new CompatModeDialog( |
| 1184 | ActivityManagerService.this, mContext, |
| 1185 | ar.info.applicationInfo); |
| 1186 | mCompatModeDialog.show(); |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | } |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 1191 | break; |
| 1192 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1193 | case DISPATCH_FOREGROUND_ACTIVITIES_CHANGED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1194 | final int pid = msg.arg1; |
| 1195 | final int uid = msg.arg2; |
| 1196 | final boolean foregroundActivities = (Boolean) msg.obj; |
| 1197 | dispatchForegroundActivitiesChanged(pid, uid, foregroundActivities); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1198 | break; |
| 1199 | } |
| 1200 | case DISPATCH_PROCESS_DIED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1201 | final int pid = msg.arg1; |
| 1202 | final int uid = msg.arg2; |
| 1203 | dispatchProcessDied(pid, uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1204 | break; |
| 1205 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1206 | case REPORT_MEM_USAGE: { |
| 1207 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 1208 | if (!isDebuggable) { |
| 1209 | return; |
| 1210 | } |
| 1211 | synchronized (ActivityManagerService.this) { |
| 1212 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1213 | if (now < (mLastMemUsageReportTime+5*60*1000)) { |
| 1214 | // Don't report more than every 5 minutes to somewhat |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1215 | // avoid spamming. |
| 1216 | return; |
| 1217 | } |
| 1218 | mLastMemUsageReportTime = now; |
| 1219 | } |
| 1220 | Thread thread = new Thread() { |
| 1221 | @Override public void run() { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1222 | StringBuilder dropBuilder = new StringBuilder(1024); |
| 1223 | StringBuilder logBuilder = new StringBuilder(1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1224 | StringWriter oomSw = new StringWriter(); |
| 1225 | PrintWriter oomPw = new PrintWriter(oomSw); |
| 1226 | StringWriter catSw = new StringWriter(); |
| 1227 | PrintWriter catPw = new PrintWriter(catSw); |
| 1228 | String[] emptyArgs = new String[] { }; |
| 1229 | StringBuilder tag = new StringBuilder(128); |
| 1230 | StringBuilder stack = new StringBuilder(128); |
| 1231 | tag.append("Low on memory -- "); |
| 1232 | dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw, |
| 1233 | tag, stack); |
| 1234 | dropBuilder.append(stack); |
| 1235 | dropBuilder.append('\n'); |
| 1236 | dropBuilder.append('\n'); |
| 1237 | String oomString = oomSw.toString(); |
| 1238 | dropBuilder.append(oomString); |
| 1239 | dropBuilder.append('\n'); |
| 1240 | logBuilder.append(oomString); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1241 | try { |
| 1242 | java.lang.Process proc = Runtime.getRuntime().exec(new String[] { |
| 1243 | "procrank", }); |
| 1244 | final InputStreamReader converter = new InputStreamReader( |
| 1245 | proc.getInputStream()); |
| 1246 | BufferedReader in = new BufferedReader(converter); |
| 1247 | String line; |
| 1248 | while (true) { |
| 1249 | line = in.readLine(); |
| 1250 | if (line == null) { |
| 1251 | break; |
| 1252 | } |
| 1253 | if (line.length() > 0) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1254 | logBuilder.append(line); |
| 1255 | logBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1256 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1257 | dropBuilder.append(line); |
| 1258 | dropBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1259 | } |
| 1260 | converter.close(); |
| 1261 | } catch (IOException e) { |
| 1262 | } |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1263 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1264 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1265 | dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1266 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1267 | dumpServicesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1268 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1269 | dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1270 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1271 | dropBuilder.append(catSw.toString()); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1272 | addErrorToDropBox("lowmem", null, "system_server", null, |
| 1273 | null, tag.toString(), dropBuilder.toString(), null, null); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1274 | Slog.i(TAG, logBuilder.toString()); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1275 | synchronized (ActivityManagerService.this) { |
| 1276 | long now = SystemClock.uptimeMillis(); |
| 1277 | if (mLastMemUsageReportTime < now) { |
| 1278 | mLastMemUsageReportTime = now; |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | }; |
| 1283 | thread.start(); |
| 1284 | break; |
| 1285 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | }; |
| 1289 | |
| 1290 | public static void setSystemProcess() { |
| 1291 | try { |
| 1292 | ActivityManagerService m = mSelf; |
| 1293 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 1294 | ServiceManager.addService("activity", m, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | ServiceManager.addService("meminfo", new MemBinder(m)); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1296 | ServiceManager.addService("gfxinfo", new GraphicsBinder(m)); |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1297 | ServiceManager.addService("dbinfo", new DbBinder(m)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | if (MONITOR_CPU_USAGE) { |
| 1299 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1300 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1302 | |
| 1303 | ApplicationInfo info = |
| 1304 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 1305 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1306 | mSystemThread.installSystemApplicationInfo(info); |
| 1307 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1308 | synchronized (mSelf) { |
| 1309 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1310 | mSystemThread.getApplicationThread(), info, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1311 | info.processName, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1312 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1313 | app.pid = MY_PID; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1314 | app.maxAdj = ProcessList.SYSTEM_ADJ; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1315 | mSelf.mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1316 | synchronized (mSelf.mPidsSelfLocked) { |
| 1317 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1318 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1319 | mSelf.updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1320 | } |
| 1321 | } catch (PackageManager.NameNotFoundException e) { |
| 1322 | throw new RuntimeException( |
| 1323 | "Unable to find android system package", e); |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | public void setWindowManager(WindowManagerService wm) { |
| 1328 | mWindowManager = wm; |
| 1329 | } |
| 1330 | |
| 1331 | public static final Context main(int factoryTest) { |
| 1332 | AThread thr = new AThread(); |
| 1333 | thr.start(); |
| 1334 | |
| 1335 | synchronized (thr) { |
| 1336 | while (thr.mService == null) { |
| 1337 | try { |
| 1338 | thr.wait(); |
| 1339 | } catch (InterruptedException e) { |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | ActivityManagerService m = thr.mService; |
| 1345 | mSelf = m; |
| 1346 | ActivityThread at = ActivityThread.systemMain(); |
| 1347 | mSystemThread = at; |
| 1348 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1349 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1350 | m.mContext = context; |
| 1351 | m.mFactoryTest = factoryTest; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1352 | m.mMainStack = new ActivityStack(m, context, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | |
| 1354 | m.mBatteryStatsService.publish(context); |
| 1355 | m.mUsageStatsService.publish(context); |
| 1356 | |
| 1357 | synchronized (thr) { |
| 1358 | thr.mReady = true; |
| 1359 | thr.notifyAll(); |
| 1360 | } |
| 1361 | |
| 1362 | m.startRunning(null, null, null, null); |
| 1363 | |
| 1364 | return context; |
| 1365 | } |
| 1366 | |
| 1367 | public static ActivityManagerService self() { |
| 1368 | return mSelf; |
| 1369 | } |
| 1370 | |
| 1371 | static class AThread extends Thread { |
| 1372 | ActivityManagerService mService; |
| 1373 | boolean mReady = false; |
| 1374 | |
| 1375 | public AThread() { |
| 1376 | super("ActivityManager"); |
| 1377 | } |
| 1378 | |
| 1379 | public void run() { |
| 1380 | Looper.prepare(); |
| 1381 | |
| 1382 | android.os.Process.setThreadPriority( |
| 1383 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1384 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1385 | |
| 1386 | ActivityManagerService m = new ActivityManagerService(); |
| 1387 | |
| 1388 | synchronized (this) { |
| 1389 | mService = m; |
| 1390 | notifyAll(); |
| 1391 | } |
| 1392 | |
| 1393 | synchronized (this) { |
| 1394 | while (!mReady) { |
| 1395 | try { |
| 1396 | wait(); |
| 1397 | } catch (InterruptedException e) { |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1402 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1403 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1404 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1405 | } |
| 1406 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1407 | Looper.loop(); |
| 1408 | } |
| 1409 | } |
| 1410 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1411 | static class MemBinder extends Binder { |
| 1412 | ActivityManagerService mActivityManagerService; |
| 1413 | MemBinder(ActivityManagerService activityManagerService) { |
| 1414 | mActivityManagerService = activityManagerService; |
| 1415 | } |
| 1416 | |
| 1417 | @Override |
| 1418 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1419 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1420 | != PackageManager.PERMISSION_GRANTED) { |
| 1421 | pw.println("Permission Denial: can't dump meminfo from from pid=" |
| 1422 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1423 | + " without permission " + android.Manifest.permission.DUMP); |
| 1424 | return; |
| 1425 | } |
| 1426 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1427 | mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1428 | false, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1432 | static class GraphicsBinder extends Binder { |
| 1433 | ActivityManagerService mActivityManagerService; |
| 1434 | GraphicsBinder(ActivityManagerService activityManagerService) { |
| 1435 | mActivityManagerService = activityManagerService; |
| 1436 | } |
| 1437 | |
| 1438 | @Override |
| 1439 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1440 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1441 | != PackageManager.PERMISSION_GRANTED) { |
| 1442 | pw.println("Permission Denial: can't dump gfxinfo from from pid=" |
| 1443 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1444 | + " without permission " + android.Manifest.permission.DUMP); |
| 1445 | return; |
| 1446 | } |
| 1447 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1448 | mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1452 | static class DbBinder extends Binder { |
| 1453 | ActivityManagerService mActivityManagerService; |
| 1454 | DbBinder(ActivityManagerService activityManagerService) { |
| 1455 | mActivityManagerService = activityManagerService; |
| 1456 | } |
| 1457 | |
| 1458 | @Override |
| 1459 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1460 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1461 | != PackageManager.PERMISSION_GRANTED) { |
| 1462 | pw.println("Permission Denial: can't dump dbinfo from from pid=" |
| 1463 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1464 | + " without permission " + android.Manifest.permission.DUMP); |
| 1465 | return; |
| 1466 | } |
| 1467 | |
| 1468 | mActivityManagerService.dumpDbInfo(fd, pw, args); |
| 1469 | } |
| 1470 | } |
| 1471 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1472 | static class CpuBinder extends Binder { |
| 1473 | ActivityManagerService mActivityManagerService; |
| 1474 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1475 | mActivityManagerService = activityManagerService; |
| 1476 | } |
| 1477 | |
| 1478 | @Override |
| 1479 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1480 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1481 | != PackageManager.PERMISSION_GRANTED) { |
| 1482 | pw.println("Permission Denial: can't dump cpuinfo from from pid=" |
| 1483 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1484 | + " without permission " + android.Manifest.permission.DUMP); |
| 1485 | return; |
| 1486 | } |
| 1487 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1488 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1489 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1490 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1491 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1492 | } |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | private ActivityManagerService() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1497 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1498 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame^] | 1499 | mFgBroadcastQueue = new BroadcastQueue(this, "foreground", BROADCAST_FG_TIMEOUT); |
| 1500 | mBgBroadcastQueue = new BroadcastQueue(this, "background", BROADCAST_BG_TIMEOUT); |
| 1501 | mBroadcastQueues[0] = mFgBroadcastQueue; |
| 1502 | mBroadcastQueues[1] = mBgBroadcastQueue; |
| 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()); |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 1517 | mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1518 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1519 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1520 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1521 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1522 | mConfiguration.setToDefaults(); |
| 1523 | mConfiguration.locale = Locale.getDefault(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 1524 | mConfigurationSeq = mConfiguration.seq = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1525 | mProcessStats.init(); |
| 1526 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 1527 | mCompatModePackages = new CompatModePackages(this, systemDir); |
| 1528 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1529 | // Add ourself to the Watchdog monitors. |
| 1530 | Watchdog.getInstance().addMonitor(this); |
| 1531 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1532 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1533 | public void run() { |
| 1534 | while (true) { |
| 1535 | try { |
| 1536 | try { |
| 1537 | synchronized(this) { |
| 1538 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1539 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1540 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1541 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1542 | // + ", write delay=" + nextWriteDelay); |
| 1543 | if (nextWriteDelay < nextCpuDelay) { |
| 1544 | nextCpuDelay = nextWriteDelay; |
| 1545 | } |
| 1546 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1547 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1548 | this.wait(nextCpuDelay); |
| 1549 | } |
| 1550 | } |
| 1551 | } catch (InterruptedException e) { |
| 1552 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1553 | updateCpuStatsNow(); |
| 1554 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1555 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | }; |
| 1560 | mProcessStatsThread.start(); |
| 1561 | } |
| 1562 | |
| 1563 | @Override |
| 1564 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1565 | throws RemoteException { |
| 1566 | try { |
| 1567 | return super.onTransact(code, data, reply, flags); |
| 1568 | } catch (RuntimeException e) { |
| 1569 | // The activity manager only throws security exceptions, so let's |
| 1570 | // log all others. |
| 1571 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1572 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1573 | } |
| 1574 | throw e; |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1579 | final long now = SystemClock.uptimeMillis(); |
| 1580 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1581 | return; |
| 1582 | } |
| 1583 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1584 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1585 | mProcessStatsThread.notify(); |
| 1586 | } |
| 1587 | } |
| 1588 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1590 | void updateCpuStatsNow() { |
| 1591 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1592 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1593 | final long now = SystemClock.uptimeMillis(); |
| 1594 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1595 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1596 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1597 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1598 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1599 | haveNewCpuStats = true; |
| 1600 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1601 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1602 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1603 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1604 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1605 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1607 | int user = mProcessStats.getLastUserTime(); |
| 1608 | int system = mProcessStats.getLastSystemTime(); |
| 1609 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1610 | int irq = mProcessStats.getLastIrqTime(); |
| 1611 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1612 | int idle = mProcessStats.getLastIdleTime(); |
| 1613 | |
| 1614 | int total = user + system + iowait + irq + softIrq + idle; |
| 1615 | if (total == 0) total = 1; |
| 1616 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1617 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1619 | (user * 100) / total, |
| 1620 | (system * 100) / total, |
| 1621 | (iowait * 100) / total, |
| 1622 | (irq * 100) / total, |
| 1623 | (softIrq * 100) / total); |
| 1624 | } |
| 1625 | } |
| 1626 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1627 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1628 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1629 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1630 | synchronized(mPidsSelfLocked) { |
| 1631 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1632 | if (mOnBattery) { |
| 1633 | int perc = bstats.startAddingCpuLocked(); |
| 1634 | int totalUTime = 0; |
| 1635 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1636 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1637 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1638 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1639 | if (!st.working) { |
| 1640 | continue; |
| 1641 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1642 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1643 | int otherUTime = (st.rel_utime*perc)/100; |
| 1644 | int otherSTime = (st.rel_stime*perc)/100; |
| 1645 | totalUTime += otherUTime; |
| 1646 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1647 | if (pr != null) { |
| 1648 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1649 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1650 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1651 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1652 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1653 | } else { |
| 1654 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1655 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1656 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1657 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1658 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1659 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1660 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1661 | } |
| 1662 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1663 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1664 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1665 | } |
| 1666 | } |
| 1667 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1668 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1669 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1670 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1671 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1672 | } |
| 1673 | } |
| 1674 | } |
| 1675 | } |
| 1676 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1677 | @Override |
| 1678 | public void batteryNeedsCpuUpdate() { |
| 1679 | updateCpuStatsNow(); |
| 1680 | } |
| 1681 | |
| 1682 | @Override |
| 1683 | public void batteryPowerChanged(boolean onBattery) { |
| 1684 | // When plugging in, update the CPU stats first before changing |
| 1685 | // the plug state. |
| 1686 | updateCpuStatsNow(); |
| 1687 | synchronized (this) { |
| 1688 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1689 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1690 | } |
| 1691 | } |
| 1692 | } |
| 1693 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1694 | /** |
| 1695 | * Initialize the application bind args. These are passed to each |
| 1696 | * process when the bindApplication() IPC is sent to the process. They're |
| 1697 | * lazily setup to make sure the services are running when they're asked for. |
| 1698 | */ |
| 1699 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1700 | if (mAppBindArgs == null) { |
| 1701 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1702 | |
| 1703 | // Setup the application init args |
| 1704 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1705 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1706 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1707 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1708 | } |
| 1709 | return mAppBindArgs; |
| 1710 | } |
| 1711 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1712 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1713 | if (mFocusedActivity != r) { |
| 1714 | mFocusedActivity = r; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1715 | if (r != null) { |
| 1716 | mWindowManager.setFocusedApp(r.appToken, true); |
| 1717 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1718 | } |
| 1719 | } |
| 1720 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1721 | private final void updateLruProcessInternalLocked(ProcessRecord app, |
| 1722 | boolean oomAdj, boolean updateActivityTime, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1723 | // 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] | 1724 | int lrui = mLruProcesses.indexOf(app); |
| 1725 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1726 | |
| 1727 | int i = mLruProcesses.size()-1; |
| 1728 | int skipTop = 0; |
| 1729 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1730 | app.lruSeq = mLruSeq; |
| 1731 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1732 | // compute the new weight for this process. |
| 1733 | if (updateActivityTime) { |
| 1734 | app.lastActivityTime = SystemClock.uptimeMillis(); |
| 1735 | } |
| 1736 | if (app.activities.size() > 0) { |
| 1737 | // If this process has activities, we more strongly want to keep |
| 1738 | // it around. |
| 1739 | app.lruWeight = app.lastActivityTime; |
| 1740 | } else if (app.pubProviders.size() > 0) { |
| 1741 | // If this process contains content providers, we want to keep |
| 1742 | // it a little more strongly. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1743 | app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1744 | // 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] | 1745 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1746 | } else { |
| 1747 | // If this process doesn't have activities, we less strongly |
| 1748 | // want to keep it around, and generally want to avoid getting |
| 1749 | // in front of any very recently used activities. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1750 | app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1751 | // 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] | 1752 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1753 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1754 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1755 | while (i >= 0) { |
| 1756 | ProcessRecord p = mLruProcesses.get(i); |
| 1757 | // If this app shouldn't be in front of the first N background |
| 1758 | // apps, then skip over that many that are currently hidden. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1759 | if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1760 | skipTop--; |
| 1761 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1762 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1763 | mLruProcesses.add(i+1, app); |
| 1764 | break; |
| 1765 | } |
| 1766 | i--; |
| 1767 | } |
| 1768 | if (i < 0) { |
| 1769 | mLruProcesses.add(0, app); |
| 1770 | } |
| 1771 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1772 | // If the app is currently using a content provider or service, |
| 1773 | // bump those processes as well. |
| 1774 | if (app.connections.size() > 0) { |
| 1775 | for (ConnectionRecord cr : app.connections) { |
| 1776 | if (cr.binding != null && cr.binding.service != null |
| 1777 | && cr.binding.service.app != null |
| 1778 | && cr.binding.service.app.lruSeq != mLruSeq) { |
| 1779 | updateLruProcessInternalLocked(cr.binding.service.app, oomAdj, |
| 1780 | updateActivityTime, i+1); |
| 1781 | } |
| 1782 | } |
| 1783 | } |
| 1784 | if (app.conProviders.size() > 0) { |
| 1785 | for (ContentProviderRecord cpr : app.conProviders.keySet()) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 1786 | if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) { |
| 1787 | updateLruProcessInternalLocked(cpr.proc, oomAdj, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1788 | updateActivityTime, i+1); |
| 1789 | } |
| 1790 | } |
| 1791 | } |
| 1792 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1793 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1794 | if (oomAdj) { |
| 1795 | updateOomAdjLocked(); |
| 1796 | } |
| 1797 | } |
| 1798 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1799 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1800 | boolean oomAdj, boolean updateActivityTime) { |
| 1801 | mLruSeq++; |
| 1802 | updateLruProcessInternalLocked(app, oomAdj, updateActivityTime, 0); |
| 1803 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1804 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1805 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1806 | String processName, int uid) { |
| 1807 | if (uid == Process.SYSTEM_UID) { |
| 1808 | // The system gets to run in any process. If there are multiple |
| 1809 | // processes with the same uid, just pick the first (this |
| 1810 | // should never happen). |
| 1811 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1812 | processName); |
| 1813 | return procs != null ? procs.valueAt(0) : null; |
| 1814 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1815 | // uid = applyUserId(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1816 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1817 | return proc; |
| 1818 | } |
| 1819 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1820 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1821 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1822 | try { |
| 1823 | if (pm.performDexOpt(packageName)) { |
| 1824 | mDidDexOpt = true; |
| 1825 | } |
| 1826 | } catch (RemoteException e) { |
| 1827 | } |
| 1828 | } |
| 1829 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1830 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1831 | int transit = mWindowManager.getPendingAppTransition(); |
| 1832 | return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN |
| 1833 | || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN |
| 1834 | || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT; |
| 1835 | } |
| 1836 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1837 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1838 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1839 | String hostingType, ComponentName hostingName, boolean allowWhileBooting, |
| 1840 | boolean isolated) { |
| 1841 | ProcessRecord app; |
| 1842 | if (!isolated) { |
| 1843 | app = getProcessRecordLocked(processName, info.uid); |
| 1844 | } else { |
| 1845 | // If this is an isolated process, it can't re-use an existing process. |
| 1846 | app = null; |
| 1847 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1848 | // We don't have to do anything more if: |
| 1849 | // (1) There is an existing application record; and |
| 1850 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 1851 | // object attached to it so we know it couldn't have crashed; and |
| 1852 | // (3) There is a pid assigned to it, so it is either starting or |
| 1853 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1854 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1855 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 1856 | + " thread=" + (app != null ? app.thread : null) |
| 1857 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1858 | if (app != null && app.pid > 0) { |
| 1859 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1860 | // We already have the app running, or are waiting for it to |
| 1861 | // 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] | 1862 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1863 | // If this is a new package in the process, add the package to the list |
| 1864 | app.addPackage(info.packageName); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1865 | return app; |
| 1866 | } else { |
| 1867 | // An application record is attached to a previous process, |
| 1868 | // clean it up now. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1869 | if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 1870 | handleAppDiedLocked(app, true, true); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1871 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1872 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1873 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1874 | String hostingNameStr = hostingName != null |
| 1875 | ? hostingName.flattenToShortString() : null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1876 | |
| 1877 | if (!isolated) { |
| 1878 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 1879 | // If we are in the background, then check to see if this process |
| 1880 | // is bad. If so, we will just silently fail. |
| 1881 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 1882 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 1883 | + "/" + info.processName); |
| 1884 | return null; |
| 1885 | } |
| 1886 | } else { |
| 1887 | // When the user is explicitly starting a process, then clear its |
| 1888 | // crash count so that we won't make it bad until they see at |
| 1889 | // least one crash dialog again, and make the process good again |
| 1890 | // if it had been bad. |
| 1891 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1892 | + "/" + info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1893 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 1894 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 1895 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid, |
| 1896 | info.processName); |
| 1897 | mBadProcesses.remove(info.processName, info.uid); |
| 1898 | if (app != null) { |
| 1899 | app.bad = false; |
| 1900 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1901 | } |
| 1902 | } |
| 1903 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1904 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1905 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1906 | app = newProcessRecordLocked(null, info, processName, isolated); |
| 1907 | if (app == null) { |
| 1908 | Slog.w(TAG, "Failed making new process record for " |
| 1909 | + processName + "/" + info.uid + " isolated=" + isolated); |
| 1910 | return null; |
| 1911 | } |
| 1912 | mProcessNames.put(processName, app.uid, app); |
| 1913 | if (isolated) { |
| 1914 | mIsolatedProcesses.put(app.uid, app); |
| 1915 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1916 | } else { |
| 1917 | // If this is a new package in the process, add the package to the list |
| 1918 | app.addPackage(info.packageName); |
| 1919 | } |
| 1920 | |
| 1921 | // If the system is not ready yet, then hold off on starting this |
| 1922 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1923 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1924 | && !isAllowedWhileBooting(info) |
| 1925 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1926 | if (!mProcessesOnHold.contains(app)) { |
| 1927 | mProcessesOnHold.add(app); |
| 1928 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1929 | 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] | 1930 | return app; |
| 1931 | } |
| 1932 | |
| 1933 | startProcessLocked(app, hostingType, hostingNameStr); |
| 1934 | return (app.pid != 0) ? app : null; |
| 1935 | } |
| 1936 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1937 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 1938 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 1939 | } |
| 1940 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1941 | private final void startProcessLocked(ProcessRecord app, |
| 1942 | String hostingType, String hostingNameStr) { |
| 1943 | if (app.pid > 0 && app.pid != MY_PID) { |
| 1944 | synchronized (mPidsSelfLocked) { |
| 1945 | mPidsSelfLocked.remove(app.pid); |
| 1946 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 1947 | } |
| 1948 | app.pid = 0; |
| 1949 | } |
| 1950 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1951 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 1952 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1953 | mProcessesOnHold.remove(app); |
| 1954 | |
| 1955 | updateCpuStats(); |
| 1956 | |
| 1957 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 1958 | mProcDeaths[0] = 0; |
| 1959 | |
| 1960 | try { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1961 | int uid = app.uid; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1962 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1963 | int[] gids = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1964 | if (!app.isolated) { |
| 1965 | try { |
| 1966 | gids = mContext.getPackageManager().getPackageGids( |
| 1967 | app.info.packageName); |
| 1968 | } catch (PackageManager.NameNotFoundException e) { |
| 1969 | Slog.w(TAG, "Unable to retrieve gids", e); |
| 1970 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1971 | } |
| 1972 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 1973 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 1974 | && mTopComponent != null |
| 1975 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 1976 | uid = 0; |
| 1977 | } |
| 1978 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 1979 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 1980 | uid = 0; |
| 1981 | } |
| 1982 | } |
| 1983 | int debugFlags = 0; |
| 1984 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 1985 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
Elliott Hughes | fa36aee | 2011-06-17 14:39:41 -0700 | [diff] [blame] | 1986 | // Also turn on CheckJNI for debuggable apps. It's quite |
| 1987 | // awkward to turn on otherwise. |
| 1988 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1989 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 1990 | // Run the app in safe mode if its manifest requests so or the |
| 1991 | // system is booted in safe mode. |
| 1992 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 1993 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 1994 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 1995 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1996 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 1997 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 1998 | } |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 1999 | if ("1".equals(SystemProperties.get("debug.jni.logging"))) { |
| 2000 | debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING; |
| 2001 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2002 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 2003 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 2004 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2005 | |
| 2006 | // Start the process. It will either succeed and return a result containing |
| 2007 | // the PID of the new process, or else throw a RuntimeException. |
| 2008 | Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread", |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 2009 | app.processName, uid, uid, gids, debugFlags, |
| 2010 | app.info.targetSdkVersion, null); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2011 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2012 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 2013 | synchronized (bs) { |
| 2014 | if (bs.isOnBattery()) { |
| 2015 | app.batteryStats.incStartsLocked(); |
| 2016 | } |
| 2017 | } |
| 2018 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2019 | EventLog.writeEvent(EventLogTags.AM_PROC_START, startResult.pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2020 | app.processName, hostingType, |
| 2021 | hostingNameStr != null ? hostingNameStr : ""); |
| 2022 | |
| 2023 | if (app.persistent) { |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2024 | Watchdog.getInstance().processStarted(app.processName, startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2025 | } |
| 2026 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 2027 | StringBuilder buf = mStringBuilder; |
| 2028 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2029 | buf.append("Start proc "); |
| 2030 | buf.append(app.processName); |
| 2031 | buf.append(" for "); |
| 2032 | buf.append(hostingType); |
| 2033 | if (hostingNameStr != null) { |
| 2034 | buf.append(" "); |
| 2035 | buf.append(hostingNameStr); |
| 2036 | } |
| 2037 | buf.append(": pid="); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2038 | buf.append(startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2039 | buf.append(" uid="); |
| 2040 | buf.append(uid); |
| 2041 | buf.append(" gids={"); |
| 2042 | if (gids != null) { |
| 2043 | for (int gi=0; gi<gids.length; gi++) { |
| 2044 | if (gi != 0) buf.append(", "); |
| 2045 | buf.append(gids[gi]); |
| 2046 | |
| 2047 | } |
| 2048 | } |
| 2049 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2050 | Slog.i(TAG, buf.toString()); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2051 | app.pid = startResult.pid; |
| 2052 | app.usingWrapper = startResult.usingWrapper; |
| 2053 | app.removed = false; |
| 2054 | synchronized (mPidsSelfLocked) { |
| 2055 | this.mPidsSelfLocked.put(startResult.pid, app); |
| 2056 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 2057 | msg.obj = app; |
| 2058 | mHandler.sendMessageDelayed(msg, startResult.usingWrapper |
| 2059 | ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2060 | } |
| 2061 | } catch (RuntimeException e) { |
| 2062 | // XXX do better error recovery. |
| 2063 | app.pid = 0; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2064 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2065 | } |
| 2066 | } |
| 2067 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2068 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2069 | if (resumed) { |
| 2070 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 2071 | } else { |
| 2072 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 2073 | } |
| 2074 | } |
| 2075 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2076 | boolean startHomeActivityLocked(int userId) { |
Mike Lockwood | a8f767a | 2011-08-31 14:32:37 -0400 | [diff] [blame] | 2077 | if (mHeadless) { |
| 2078 | // Added because none of the other calls to ensureBootCompleted seem to fire |
| 2079 | // when running headless. |
| 2080 | ensureBootCompleted(); |
| 2081 | return false; |
| 2082 | } |
| 2083 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2084 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2085 | && mTopAction == null) { |
| 2086 | // We are running in factory test mode, but unable to find |
| 2087 | // the factory test app, so just sit around displaying the |
| 2088 | // error message and don't try to start anything. |
| 2089 | return false; |
| 2090 | } |
| 2091 | Intent intent = new Intent( |
| 2092 | mTopAction, |
| 2093 | mTopData != null ? Uri.parse(mTopData) : null); |
| 2094 | intent.setComponent(mTopComponent); |
| 2095 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 2096 | intent.addCategory(Intent.CATEGORY_HOME); |
| 2097 | } |
| 2098 | ActivityInfo aInfo = |
| 2099 | intent.resolveActivityInfo(mContext.getPackageManager(), |
| 2100 | STOCK_PM_FLAGS); |
| 2101 | if (aInfo != null) { |
| 2102 | intent.setComponent(new ComponentName( |
| 2103 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2104 | // Don't do this if the home app is currently being |
| 2105 | // instrumented. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2106 | aInfo = new ActivityInfo(aInfo); |
| 2107 | aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2108 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 2109 | aInfo.applicationInfo.uid); |
| 2110 | if (app == null || app.instrumentationClass == null) { |
| 2111 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2112 | mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2113 | null, null, 0, 0, 0, false, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2114 | } |
| 2115 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2116 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2117 | return true; |
| 2118 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2119 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2120 | /** |
| 2121 | * Starts the "new version setup screen" if appropriate. |
| 2122 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2123 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2124 | // Only do this once per boot. |
| 2125 | if (mCheckedForSetup) { |
| 2126 | return; |
| 2127 | } |
| 2128 | |
| 2129 | // We will show this screen if the current one is a different |
| 2130 | // version than the last one shown, and we are not running in |
| 2131 | // low-level factory test mode. |
| 2132 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2133 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
| 2134 | Settings.Secure.getInt(resolver, |
| 2135 | Settings.Secure.DEVICE_PROVISIONED, 0) != 0) { |
| 2136 | mCheckedForSetup = true; |
| 2137 | |
| 2138 | // See if we should be showing the platform update setup UI. |
| 2139 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 2140 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 2141 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 2142 | |
| 2143 | // We don't allow third party apps to replace this. |
| 2144 | ResolveInfo ri = null; |
| 2145 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 2146 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 2147 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2148 | ri = ris.get(i); |
| 2149 | break; |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | if (ri != null) { |
| 2154 | String vers = ri.activityInfo.metaData != null |
| 2155 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 2156 | : null; |
| 2157 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2158 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2159 | Intent.METADATA_SETUP_VERSION); |
| 2160 | } |
| 2161 | String lastVers = Settings.Secure.getString( |
| 2162 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2163 | if (vers != null && !vers.equals(lastVers)) { |
| 2164 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2165 | intent.setComponent(new ComponentName( |
| 2166 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2167 | mMainStack.startActivityLocked(null, intent, null, null, 0, ri.activityInfo, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2168 | null, null, 0, 0, 0, false, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2169 | } |
| 2170 | } |
| 2171 | } |
| 2172 | } |
| 2173 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2174 | CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2175 | return mCompatModePackages.compatibilityInfoForPackageLocked(ai); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2176 | } |
| 2177 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2178 | void enforceNotIsolatedCaller(String caller) { |
| 2179 | if (UserId.isIsolated(Binder.getCallingUid())) { |
| 2180 | throw new SecurityException("Isolated process not allowed to call " + caller); |
| 2181 | } |
| 2182 | } |
| 2183 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2184 | public int getFrontActivityScreenCompatMode() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2185 | enforceNotIsolatedCaller("getFrontActivityScreenCompatMode"); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2186 | synchronized (this) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2187 | return mCompatModePackages.getFrontActivityScreenCompatModeLocked(); |
| 2188 | } |
| 2189 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2190 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2191 | public void setFrontActivityScreenCompatMode(int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2192 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2193 | "setFrontActivityScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2194 | synchronized (this) { |
| 2195 | mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode); |
| 2196 | } |
| 2197 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2198 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2199 | public int getPackageScreenCompatMode(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2200 | enforceNotIsolatedCaller("getPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2201 | synchronized (this) { |
| 2202 | return mCompatModePackages.getPackageScreenCompatModeLocked(packageName); |
| 2203 | } |
| 2204 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2205 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2206 | public void setPackageScreenCompatMode(String packageName, int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2207 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2208 | "setPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2209 | synchronized (this) { |
| 2210 | mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2211 | } |
| 2212 | } |
| 2213 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2214 | public boolean getPackageAskScreenCompat(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2215 | enforceNotIsolatedCaller("getPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2216 | synchronized (this) { |
| 2217 | return mCompatModePackages.getPackageAskCompatModeLocked(packageName); |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | public void setPackageAskScreenCompat(String packageName, boolean ask) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2222 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2223 | "setPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2224 | synchronized (this) { |
| 2225 | mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask); |
| 2226 | } |
| 2227 | } |
| 2228 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2229 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2230 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2231 | |
| 2232 | final int identHash = System.identityHashCode(r); |
| 2233 | updateUsageStats(r, true); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2234 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2235 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2236 | private void dispatchForegroundActivitiesChanged(int pid, int uid, boolean foregroundActivities) { |
| 2237 | int i = mProcessObservers.beginBroadcast(); |
| 2238 | while (i > 0) { |
| 2239 | i--; |
| 2240 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2241 | if (observer != null) { |
| 2242 | try { |
| 2243 | observer.onForegroundActivitiesChanged(pid, uid, foregroundActivities); |
| 2244 | } catch (RemoteException e) { |
| 2245 | } |
| 2246 | } |
| 2247 | } |
| 2248 | mProcessObservers.finishBroadcast(); |
| 2249 | } |
| 2250 | |
| 2251 | private void dispatchProcessDied(int pid, int uid) { |
| 2252 | int i = mProcessObservers.beginBroadcast(); |
| 2253 | while (i > 0) { |
| 2254 | i--; |
| 2255 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2256 | if (observer != null) { |
| 2257 | try { |
| 2258 | observer.onProcessDied(pid, uid); |
| 2259 | } catch (RemoteException e) { |
| 2260 | } |
| 2261 | } |
| 2262 | } |
| 2263 | mProcessObservers.finishBroadcast(); |
| 2264 | } |
| 2265 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2266 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2267 | final int N = mPendingActivityLaunches.size(); |
| 2268 | if (N <= 0) { |
| 2269 | return; |
| 2270 | } |
| 2271 | for (int i=0; i<N; i++) { |
| 2272 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2273 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2274 | pal.grantedUriPermissions, pal.grantedMode, pal.onlyIfNeeded, |
| 2275 | doResume && i == (N-1)); |
| 2276 | } |
| 2277 | mPendingActivityLaunches.clear(); |
| 2278 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2279 | |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2280 | public final int startActivity(IApplicationThread caller, |
| 2281 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2282 | int grantedMode, IBinder resultTo, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2283 | String resultWho, int requestCode, boolean onlyIfNeeded, boolean debug, |
| 2284 | String profileFile, ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2285 | enforceNotIsolatedCaller("startActivity"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2286 | int userId = 0; |
| 2287 | if (intent.getCategories() != null && intent.getCategories().contains(Intent.CATEGORY_HOME)) { |
| 2288 | // Requesting home, set the identity to the current user |
| 2289 | // HACK! |
| 2290 | userId = mCurrentUserId; |
| 2291 | } else { |
| 2292 | // TODO: Fix this in a better way - calls coming from SystemUI should probably carry |
| 2293 | // the current user's userId |
| 2294 | if (Binder.getCallingUid() < Process.FIRST_APPLICATION_UID) { |
| 2295 | userId = 0; |
| 2296 | } else { |
| 2297 | userId = Binder.getOrigCallingUser(); |
| 2298 | } |
| 2299 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2300 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2301 | grantedUriPermissions, grantedMode, resultTo, resultWho, requestCode, onlyIfNeeded, |
| 2302 | debug, profileFile, profileFd, autoStopProfiler, null, null, userId); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | public final WaitResult startActivityAndWait(IApplicationThread caller, |
| 2306 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2307 | int grantedMode, IBinder resultTo, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2308 | String resultWho, int requestCode, boolean onlyIfNeeded, boolean debug, |
| 2309 | String profileFile, ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2310 | enforceNotIsolatedCaller("startActivityAndWait"); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2311 | WaitResult res = new WaitResult(); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2312 | int userId = Binder.getOrigCallingUser(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2313 | mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2314 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2315 | requestCode, onlyIfNeeded, debug, profileFile, profileFd, autoStopProfiler, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2316 | res, null, userId); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2317 | return res; |
| 2318 | } |
| 2319 | |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2320 | public final int startActivityWithConfig(IApplicationThread caller, |
| 2321 | Intent intent, String resolvedType, Uri[] grantedUriPermissions, |
| 2322 | int grantedMode, IBinder resultTo, |
| 2323 | String resultWho, int requestCode, boolean onlyIfNeeded, |
| 2324 | boolean debug, Configuration config) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2325 | enforceNotIsolatedCaller("startActivityWithConfig"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2326 | int ret = mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2327 | grantedUriPermissions, grantedMode, resultTo, resultWho, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2328 | requestCode, onlyIfNeeded, |
| 2329 | debug, null, null, false, null, config, Binder.getOrigCallingUser()); |
| 2330 | return ret; |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2331 | } |
| 2332 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2333 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2334 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2335 | IBinder resultTo, String resultWho, int requestCode, |
| 2336 | int flagsMask, int flagsValues) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2337 | enforceNotIsolatedCaller("startActivityIntentSender"); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2338 | // Refuse possible leaked file descriptors |
| 2339 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2340 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2341 | } |
| 2342 | |
| 2343 | IIntentSender sender = intent.getTarget(); |
| 2344 | if (!(sender instanceof PendingIntentRecord)) { |
| 2345 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2346 | } |
| 2347 | |
| 2348 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2349 | |
| 2350 | synchronized (this) { |
| 2351 | // If this is coming from the currently resumed activity, it is |
| 2352 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2353 | if (mMainStack.mResumedActivity != null |
| 2354 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2355 | Binder.getCallingUid()) { |
| 2356 | mAppSwitchesAllowedTime = 0; |
| 2357 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2358 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2359 | int ret = pir.sendInner(0, fillInIntent, resolvedType, null, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2360 | null, resultTo, resultWho, requestCode, flagsMask, flagsValues); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2361 | return ret; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2362 | } |
| 2363 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2364 | public boolean startNextMatchingActivity(IBinder callingActivity, |
| 2365 | Intent intent) { |
| 2366 | // Refuse possible leaked file descriptors |
| 2367 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2368 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2369 | } |
| 2370 | |
| 2371 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2372 | ActivityRecord r = mMainStack.isInStackLocked(callingActivity); |
| 2373 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2374 | return false; |
| 2375 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2376 | if (r.app == null || r.app.thread == null) { |
| 2377 | // The caller is not running... d'oh! |
| 2378 | return false; |
| 2379 | } |
| 2380 | intent = new Intent(intent); |
| 2381 | // The caller is not allowed to change the data. |
| 2382 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2383 | // And we are resetting to find the next component... |
| 2384 | intent.setComponent(null); |
| 2385 | |
| 2386 | ActivityInfo aInfo = null; |
| 2387 | try { |
| 2388 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2389 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2390 | intent, r.resolvedType, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 2391 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2392 | |
| 2393 | // Look for the original activity in the list... |
| 2394 | final int N = resolves != null ? resolves.size() : 0; |
| 2395 | for (int i=0; i<N; i++) { |
| 2396 | ResolveInfo rInfo = resolves.get(i); |
| 2397 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2398 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2399 | // We found the current one... the next matching is |
| 2400 | // after it. |
| 2401 | i++; |
| 2402 | if (i<N) { |
| 2403 | aInfo = resolves.get(i).activityInfo; |
| 2404 | } |
| 2405 | break; |
| 2406 | } |
| 2407 | } |
| 2408 | } catch (RemoteException e) { |
| 2409 | } |
| 2410 | |
| 2411 | if (aInfo == null) { |
| 2412 | // Nobody who is next! |
| 2413 | return false; |
| 2414 | } |
| 2415 | |
| 2416 | intent.setComponent(new ComponentName( |
| 2417 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2418 | intent.setFlags(intent.getFlags()&~( |
| 2419 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2420 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2421 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2422 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2423 | |
| 2424 | // Okay now we need to start the new activity, replacing the |
| 2425 | // currently running activity. This is a little tricky because |
| 2426 | // we want to start the new one as if the current one is finished, |
| 2427 | // but not finish the current one first so that there is no flicker. |
| 2428 | // And thus... |
| 2429 | final boolean wasFinishing = r.finishing; |
| 2430 | r.finishing = true; |
| 2431 | |
| 2432 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2433 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2434 | final String resultWho = r.resultWho; |
| 2435 | final int requestCode = r.requestCode; |
| 2436 | r.resultTo = null; |
| 2437 | if (resultTo != null) { |
| 2438 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2439 | } |
| 2440 | |
| 2441 | final long origId = Binder.clearCallingIdentity(); |
| 2442 | // XXX we are not dealing with propagating grantedUriPermissions... |
| 2443 | // 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] | 2444 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2445 | r.resolvedType, null, 0, aInfo, |
| 2446 | resultTo != null ? resultTo.appToken : null, resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2447 | requestCode, -1, r.launchedFromUid, false, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2448 | Binder.restoreCallingIdentity(origId); |
| 2449 | |
| 2450 | r.finishing = wasFinishing; |
| 2451 | if (res != START_SUCCESS) { |
| 2452 | return false; |
| 2453 | } |
| 2454 | return true; |
| 2455 | } |
| 2456 | } |
| 2457 | |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2458 | public final int startActivityInPackage(int uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2459 | Intent intent, String resolvedType, IBinder resultTo, |
| 2460 | String resultWho, int requestCode, boolean onlyIfNeeded) { |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2461 | |
| 2462 | // This is so super not safe, that only the system (or okay root) |
| 2463 | // can do it. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2464 | int userId = Binder.getOrigCallingUser(); |
Dianne Hackborn | 2d91af0 | 2009-07-16 13:34:33 -0700 | [diff] [blame] | 2465 | final int callingUid = Binder.getCallingUid(); |
| 2466 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2467 | throw new SecurityException( |
| 2468 | "startActivityInPackage only available to the system"); |
| 2469 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2470 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2471 | int ret = mMainStack.startActivityMayWait(null, uid, intent, resolvedType, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2472 | null, 0, resultTo, resultWho, requestCode, onlyIfNeeded, false, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2473 | null, null, false, null, null, userId); |
| 2474 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | public final int startActivities(IApplicationThread caller, |
| 2478 | Intent[] intents, String[] resolvedTypes, IBinder resultTo) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2479 | enforceNotIsolatedCaller("startActivities"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2480 | int ret = mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo, |
| 2481 | Binder.getOrigCallingUser()); |
| 2482 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2483 | } |
| 2484 | |
| 2485 | public final int startActivitiesInPackage(int uid, |
| 2486 | Intent[] intents, String[] resolvedTypes, IBinder resultTo) { |
| 2487 | |
| 2488 | // This is so super not safe, that only the system (or okay root) |
| 2489 | // can do it. |
| 2490 | final int callingUid = Binder.getCallingUid(); |
| 2491 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 2492 | throw new SecurityException( |
| 2493 | "startActivityInPackage only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2494 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2495 | int ret = mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo, |
| 2496 | UserId.getUserId(uid)); |
| 2497 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2498 | } |
| 2499 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2500 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2501 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2502 | // Quick case: check if the top-most recent task is the same. |
| 2503 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2504 | return; |
| 2505 | } |
| 2506 | // 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] | 2507 | for (int i=0; i<N; i++) { |
| 2508 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2509 | if (task.userId == tr.userId |
| 2510 | && ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2511 | || (task.intent != null && task.intent.filterEquals(tr.intent)))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2512 | mRecentTasks.remove(i); |
| 2513 | i--; |
| 2514 | N--; |
| 2515 | if (task.intent == null) { |
| 2516 | // If the new recent task we are adding is not fully |
| 2517 | // specified, then replace it with the existing recent task. |
| 2518 | task = tr; |
| 2519 | } |
| 2520 | } |
| 2521 | } |
| 2522 | if (N >= MAX_RECENT_TASKS) { |
| 2523 | mRecentTasks.remove(N-1); |
| 2524 | } |
| 2525 | mRecentTasks.add(0, task); |
| 2526 | } |
| 2527 | |
| 2528 | public void setRequestedOrientation(IBinder token, |
| 2529 | int requestedOrientation) { |
| 2530 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2531 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2532 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2533 | return; |
| 2534 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2535 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2536 | mWindowManager.setAppOrientation(r.appToken, requestedOrientation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2537 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2538 | mConfiguration, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2539 | r.mayFreezeScreenLocked(r.app) ? r.appToken : null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2540 | if (config != null) { |
| 2541 | r.frozenBeforeDestroy = true; |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 2542 | if (!updateConfigurationLocked(config, r, false, false)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2543 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2544 | } |
| 2545 | } |
| 2546 | Binder.restoreCallingIdentity(origId); |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | public int getRequestedOrientation(IBinder token) { |
| 2551 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2552 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2553 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2554 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2555 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2556 | return mWindowManager.getAppOrientation(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2557 | } |
| 2558 | } |
| 2559 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2560 | /** |
| 2561 | * This is the internal entry point for handling Activity.finish(). |
| 2562 | * |
| 2563 | * @param token The Binder token referencing the Activity we want to finish. |
| 2564 | * @param resultCode Result code, if any, from this Activity. |
| 2565 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2566 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2567 | * @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] | 2568 | */ |
| 2569 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2570 | // Refuse possible leaked file descriptors |
| 2571 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2572 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2573 | } |
| 2574 | |
| 2575 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2576 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2577 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2578 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2579 | if (next != null) { |
| 2580 | // ask watcher if this is allowed |
| 2581 | boolean resumeOK = true; |
| 2582 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2583 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2584 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2585 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | if (!resumeOK) { |
| 2589 | return false; |
| 2590 | } |
| 2591 | } |
| 2592 | } |
| 2593 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2594 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2595 | resultData, "app-request"); |
| 2596 | Binder.restoreCallingIdentity(origId); |
| 2597 | return res; |
| 2598 | } |
| 2599 | } |
| 2600 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2601 | public final void finishHeavyWeightApp() { |
| 2602 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2603 | != PackageManager.PERMISSION_GRANTED) { |
| 2604 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2605 | + Binder.getCallingPid() |
| 2606 | + ", uid=" + Binder.getCallingUid() |
| 2607 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2608 | Slog.w(TAG, msg); |
| 2609 | throw new SecurityException(msg); |
| 2610 | } |
| 2611 | |
| 2612 | synchronized(this) { |
| 2613 | if (mHeavyWeightProcess == null) { |
| 2614 | return; |
| 2615 | } |
| 2616 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2617 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2618 | mHeavyWeightProcess.activities); |
| 2619 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2620 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2621 | if (!r.finishing) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2622 | int index = mMainStack.indexOfTokenLocked(r.appToken); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2623 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2624 | mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED, |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2625 | null, "finish-heavy"); |
| 2626 | } |
| 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | mHeavyWeightProcess = null; |
| 2631 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 2632 | } |
| 2633 | } |
| 2634 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2635 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2636 | String message) { |
| 2637 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2638 | != PackageManager.PERMISSION_GRANTED) { |
| 2639 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2640 | + Binder.getCallingPid() |
| 2641 | + ", uid=" + Binder.getCallingUid() |
| 2642 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2643 | Slog.w(TAG, msg); |
| 2644 | throw new SecurityException(msg); |
| 2645 | } |
| 2646 | |
| 2647 | synchronized(this) { |
| 2648 | ProcessRecord proc = null; |
| 2649 | |
| 2650 | // Figure out which process to kill. We don't trust that initialPid |
| 2651 | // still has any relation to current pids, so must scan through the |
| 2652 | // list. |
| 2653 | synchronized (mPidsSelfLocked) { |
| 2654 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2655 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2656 | if (p.uid != uid) { |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2657 | continue; |
| 2658 | } |
| 2659 | if (p.pid == initialPid) { |
| 2660 | proc = p; |
| 2661 | break; |
| 2662 | } |
| 2663 | for (String str : p.pkgList) { |
| 2664 | if (str.equals(packageName)) { |
| 2665 | proc = p; |
| 2666 | } |
| 2667 | } |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2672 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2673 | + " initialPid=" + initialPid |
| 2674 | + " packageName=" + packageName); |
| 2675 | return; |
| 2676 | } |
| 2677 | |
| 2678 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2679 | if (proc.pid == Process.myPid()) { |
| 2680 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2681 | return; |
| 2682 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2683 | long ident = Binder.clearCallingIdentity(); |
| 2684 | try { |
| 2685 | proc.thread.scheduleCrash(message); |
| 2686 | } catch (RemoteException e) { |
| 2687 | } |
| 2688 | Binder.restoreCallingIdentity(ident); |
| 2689 | } |
| 2690 | } |
| 2691 | } |
| 2692 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2693 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2694 | int requestCode) { |
| 2695 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2696 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2697 | if (self == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2698 | return; |
| 2699 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2700 | |
| 2701 | final long origId = Binder.clearCallingIdentity(); |
| 2702 | |
| 2703 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2704 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2705 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2706 | if (r.resultTo == self && r.requestCode == requestCode) { |
| 2707 | if ((r.resultWho == null && resultWho == null) || |
| 2708 | (r.resultWho != null && r.resultWho.equals(resultWho))) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2709 | mMainStack.finishActivityLocked(r, i, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2710 | Activity.RESULT_CANCELED, null, "request-sub"); |
| 2711 | } |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | Binder.restoreCallingIdentity(origId); |
| 2716 | } |
| 2717 | } |
| 2718 | |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2719 | public boolean willActivityBeVisible(IBinder token) { |
| 2720 | synchronized(this) { |
| 2721 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2722 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2723 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2724 | if (r.appToken == token) { |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2725 | return true; |
| 2726 | } |
| 2727 | if (r.fullscreen && !r.finishing) { |
| 2728 | return false; |
| 2729 | } |
| 2730 | } |
| 2731 | return true; |
| 2732 | } |
| 2733 | } |
| 2734 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2735 | public void overridePendingTransition(IBinder token, String packageName, |
| 2736 | int enterAnim, int exitAnim) { |
| 2737 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2738 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2739 | if (self == null) { |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2740 | return; |
| 2741 | } |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2742 | |
| 2743 | final long origId = Binder.clearCallingIdentity(); |
| 2744 | |
| 2745 | if (self.state == ActivityState.RESUMED |
| 2746 | || self.state == ActivityState.PAUSING) { |
| 2747 | mWindowManager.overridePendingAppTransition(packageName, |
| 2748 | enterAnim, exitAnim); |
| 2749 | } |
| 2750 | |
| 2751 | Binder.restoreCallingIdentity(origId); |
| 2752 | } |
| 2753 | } |
| 2754 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2755 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2756 | * Main function for removing an existing process from the activity manager |
| 2757 | * as a result of that process going away. Clears out all connections |
| 2758 | * to the process. |
| 2759 | */ |
| 2760 | private final void handleAppDiedLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2761 | boolean restarting, boolean allowRestart) { |
| 2762 | cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2763 | if (!restarting) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2764 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2765 | } |
| 2766 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2767 | if (mProfileProc == app) { |
| 2768 | clearProfilerLocked(); |
| 2769 | } |
| 2770 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2771 | // Just in case... |
Dianne Hackborn | cbb722e | 2012-02-07 18:33:49 -0800 | [diff] [blame] | 2772 | mMainStack.appDiedLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2773 | |
| 2774 | // Remove this application's activities from active lists. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2775 | mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2776 | |
| 2777 | boolean atTop = true; |
| 2778 | boolean hasVisibleActivities = false; |
| 2779 | |
| 2780 | // Clean out the history list. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2781 | int i = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2782 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2783 | TAG, "Removing app " + app + " from history with " + i + " entries"); |
| 2784 | while (i > 0) { |
| 2785 | i--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2786 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2787 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2788 | TAG, "Record #" + i + " " + r + ": app=" + r.app); |
| 2789 | if (r.app == app) { |
| 2790 | if ((!r.haveState && !r.stateNotNeeded) || r.finishing) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2791 | if (ActivityStack.DEBUG_ADD_REMOVE) { |
| 2792 | RuntimeException here = new RuntimeException("here"); |
| 2793 | here.fillInStackTrace(); |
| 2794 | Slog.i(TAG, "Removing activity " + r + " from stack at " + i |
| 2795 | + ": haveState=" + r.haveState |
| 2796 | + " stateNotNeeded=" + r.stateNotNeeded |
| 2797 | + " finishing=" + r.finishing |
| 2798 | + " state=" + r.state, here); |
| 2799 | } |
| 2800 | if (!r.finishing) { |
| 2801 | Slog.w(TAG, "Force removing " + r + ": app died, no saved state"); |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 2802 | EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY, |
| 2803 | System.identityHashCode(r), |
| 2804 | r.task.taskId, r.shortComponentName, |
| 2805 | "proc died without state saved"); |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2806 | } |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 2807 | r.makeFinishing(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2808 | mMainStack.mHistory.remove(i); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 2809 | r.takeFromHistory(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2810 | mWindowManager.removeAppToken(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2811 | if (VALIDATE_TOKENS) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2812 | mMainStack.validateAppTokensLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2813 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2814 | r.removeUriPermissionsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2815 | |
| 2816 | } else { |
| 2817 | // We have the current state for this activity, so |
| 2818 | // it can be restarted later when needed. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2819 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2820 | TAG, "Keeping entry, setting app to null"); |
| 2821 | if (r.visible) { |
| 2822 | hasVisibleActivities = true; |
| 2823 | } |
| 2824 | r.app = null; |
| 2825 | r.nowVisible = false; |
| 2826 | if (!r.haveState) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2827 | if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG, |
| 2828 | "App died, clearing saved state of " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2829 | r.icicle = null; |
| 2830 | } |
| 2831 | } |
| 2832 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2833 | r.stack.cleanUpActivityLocked(r, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2834 | } |
| 2835 | atTop = false; |
| 2836 | } |
| 2837 | |
| 2838 | app.activities.clear(); |
| 2839 | |
| 2840 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2841 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2842 | + " running instrumentation " + app.instrumentationClass); |
| 2843 | Bundle info = new Bundle(); |
| 2844 | info.putString("shortMsg", "Process crashed."); |
| 2845 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 2846 | } |
| 2847 | |
| 2848 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2849 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2850 | // If there was nothing to resume, and we are not already |
| 2851 | // restarting this process, but there is a visible activity that |
| 2852 | // is hosted by the process... then make sure all visible |
| 2853 | // activities are running, taking care of restarting this |
| 2854 | // process. |
| 2855 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2856 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2857 | } |
| 2858 | } |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 2863 | IBinder threadBinder = thread.asBinder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2864 | // Find the application record. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2865 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2866 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2867 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 2868 | return i; |
| 2869 | } |
| 2870 | } |
| 2871 | return -1; |
| 2872 | } |
| 2873 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2874 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2875 | IApplicationThread thread) { |
| 2876 | if (thread == null) { |
| 2877 | return null; |
| 2878 | } |
| 2879 | |
| 2880 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2881 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2882 | } |
| 2883 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2884 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2885 | IApplicationThread thread) { |
| 2886 | |
| 2887 | mProcDeaths[0]++; |
| 2888 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2889 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 2890 | synchronized (stats) { |
| 2891 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 2892 | } |
| 2893 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2894 | // Clean up already done if the process has been re-started. |
| 2895 | if (app.pid == pid && app.thread != null && |
| 2896 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 2897 | if (!app.killedBackground) { |
| 2898 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 2899 | + ") has died."); |
| 2900 | } |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 2901 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2902 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2903 | TAG, "Dying app: " + app + ", pid: " + pid |
| 2904 | + ", thread: " + thread.asBinder()); |
| 2905 | boolean doLowMem = app.instrumentationClass == null; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2906 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2907 | |
| 2908 | if (doLowMem) { |
| 2909 | // If there are no longer any background processes running, |
| 2910 | // and the app that died was not running instrumentation, |
| 2911 | // then tell everyone we are now low on memory. |
| 2912 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2913 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2914 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2915 | 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] | 2916 | haveBg = true; |
| 2917 | break; |
| 2918 | } |
| 2919 | } |
| 2920 | |
| 2921 | if (!haveBg) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2922 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2923 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2924 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2925 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 2926 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2927 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 2928 | // The low memory report is overriding any current |
| 2929 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2930 | // heavy/important/visible/foreground processes first. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2931 | if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2932 | rec.lastRequestedGc = 0; |
| 2933 | } else { |
| 2934 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2935 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2936 | rec.reportLowMemory = true; |
| 2937 | rec.lastLowMemory = now; |
| 2938 | mProcessesToGc.remove(rec); |
| 2939 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2940 | } |
| 2941 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 2942 | mHandler.sendEmptyMessage(REPORT_MEM_USAGE); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2943 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2944 | } |
| 2945 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2946 | } else if (app.pid != pid) { |
| 2947 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2948 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2949 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | 28a8c2b | 2010-03-01 11:30:02 -0800 | [diff] [blame] | 2950 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 2951 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2952 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2953 | + thread.asBinder()); |
| 2954 | } |
| 2955 | } |
| 2956 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2957 | /** |
| 2958 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2959 | * @param clearTraces causes the dump file to be erased prior to the new |
| 2960 | * traces being written, if true; when false, the new traces will be |
| 2961 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2962 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 2963 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2964 | * @return file containing stack traces, or null if no dump file is configured |
| 2965 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2966 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
| 2967 | ProcessStats processStats, SparseArray<Boolean> lastPids) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2968 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 2969 | if (tracesPath == null || tracesPath.length() == 0) { |
| 2970 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2971 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2972 | |
| 2973 | File tracesFile = new File(tracesPath); |
| 2974 | try { |
| 2975 | File tracesDir = tracesFile.getParentFile(); |
| 2976 | if (!tracesDir.exists()) tracesFile.mkdirs(); |
| 2977 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 2978 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 2979 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2980 | tracesFile.createNewFile(); |
| 2981 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 2982 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2983 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 2984 | return null; |
| 2985 | } |
| 2986 | |
| 2987 | // Use a FileObserver to detect when traces finish writing. |
| 2988 | // The order of traces is considered important to maintain for legibility. |
| 2989 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 2990 | public synchronized void onEvent(int event, String path) { notify(); } |
| 2991 | }; |
| 2992 | |
| 2993 | try { |
| 2994 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 2995 | |
| 2996 | // First collect all of the stacks of the most important pids. |
| 2997 | try { |
| 2998 | int num = firstPids.size(); |
| 2999 | for (int i = 0; i < num; i++) { |
| 3000 | synchronized (observer) { |
| 3001 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 3002 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3003 | } |
| 3004 | } |
| 3005 | } catch (InterruptedException e) { |
| 3006 | Log.wtf(TAG, e); |
| 3007 | } |
| 3008 | |
| 3009 | // Next measure CPU usage. |
| 3010 | if (processStats != null) { |
| 3011 | processStats.init(); |
| 3012 | System.gc(); |
| 3013 | processStats.update(); |
| 3014 | try { |
| 3015 | synchronized (processStats) { |
| 3016 | processStats.wait(500); // measure over 1/2 second. |
| 3017 | } |
| 3018 | } catch (InterruptedException e) { |
| 3019 | } |
| 3020 | processStats.update(); |
| 3021 | |
| 3022 | // We'll take the stack crawls of just the top apps using CPU. |
| 3023 | final int N = processStats.countWorkingStats(); |
| 3024 | int numProcs = 0; |
| 3025 | for (int i=0; i<N && numProcs<5; i++) { |
| 3026 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 3027 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 3028 | numProcs++; |
| 3029 | try { |
| 3030 | synchronized (observer) { |
| 3031 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 3032 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3033 | } |
| 3034 | } catch (InterruptedException e) { |
| 3035 | Log.wtf(TAG, e); |
| 3036 | } |
| 3037 | |
| 3038 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3039 | } |
| 3040 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3041 | |
| 3042 | return tracesFile; |
| 3043 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3044 | } finally { |
| 3045 | observer.stopWatching(); |
| 3046 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3047 | } |
| 3048 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3049 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
| 3050 | ActivityRecord parent, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3051 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 3052 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 3053 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3054 | if (mController != null) { |
| 3055 | try { |
| 3056 | // 0 == continue, -1 = kill process immediately |
| 3057 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 3058 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3059 | } catch (RemoteException e) { |
| 3060 | mController = null; |
| 3061 | } |
| 3062 | } |
| 3063 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3064 | long anrTime = SystemClock.uptimeMillis(); |
| 3065 | if (MONITOR_CPU_USAGE) { |
| 3066 | updateCpuStatsNow(); |
| 3067 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3068 | |
| 3069 | synchronized (this) { |
| 3070 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 3071 | if (mShuttingDown) { |
| 3072 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 3073 | return; |
| 3074 | } else if (app.notResponding) { |
| 3075 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 3076 | return; |
| 3077 | } else if (app.crashing) { |
| 3078 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 3079 | return; |
| 3080 | } |
| 3081 | |
| 3082 | // In case we come through here for the same app before completing |
| 3083 | // this one, mark as anring now so we will bail out. |
| 3084 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3085 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3086 | // Log the ANR to the event log. |
| 3087 | EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags, |
| 3088 | annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3089 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3090 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3091 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3092 | |
| 3093 | int parentPid = app.pid; |
| 3094 | 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] | 3095 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3096 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3097 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3098 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3099 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 3100 | ProcessRecord r = mLruProcesses.get(i); |
| 3101 | if (r != null && r.thread != null) { |
| 3102 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3103 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 3104 | if (r.persistent) { |
| 3105 | firstPids.add(pid); |
| 3106 | } else { |
| 3107 | lastPids.put(pid, Boolean.TRUE); |
| 3108 | } |
| 3109 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3110 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3111 | } |
| 3112 | } |
| 3113 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3114 | // Log the ANR to the main log. |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3115 | StringBuilder info = mStringBuilder; |
| 3116 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3117 | info.append("ANR in ").append(app.processName); |
| 3118 | if (activity != null && activity.shortComponentName != null) { |
| 3119 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 3120 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3121 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3122 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3123 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3124 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3125 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3126 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3127 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3128 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3129 | final ProcessStats processStats = new ProcessStats(true); |
| 3130 | |
| 3131 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids); |
| 3132 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3133 | String cpuInfo = null; |
| 3134 | if (MONITOR_CPU_USAGE) { |
| 3135 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3136 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3137 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3138 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3139 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3140 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3141 | } |
| 3142 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3143 | info.append(processStats.printCurrentState(anrTime)); |
| 3144 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3145 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3146 | if (tracesFile == null) { |
| 3147 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 3148 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 3149 | } |
| 3150 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 3151 | addErrorToDropBox("anr", app, app.processName, activity, parent, annotation, |
| 3152 | cpuInfo, tracesFile, null); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3153 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3154 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3155 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3156 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 3157 | 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] | 3158 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3159 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3160 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3161 | } |
| 3162 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3163 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3164 | } |
| 3165 | } |
| 3166 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3167 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 3168 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 3169 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3170 | |
| 3171 | synchronized (this) { |
| 3172 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3173 | Slog.w(TAG, "Killing " + app + ": background ANR"); |
| 3174 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 3175 | app.processName, app.setAdj, "background ANR"); |
| 3176 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3177 | return; |
| 3178 | } |
| 3179 | |
| 3180 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 3181 | makeAppNotRespondingLocked(app, |
| 3182 | activity != null ? activity.shortComponentName : null, |
| 3183 | annotation != null ? "ANR " + annotation : "ANR", |
| 3184 | info.toString()); |
| 3185 | |
| 3186 | // Bring up the infamous App Not Responding dialog |
| 3187 | Message msg = Message.obtain(); |
| 3188 | HashMap map = new HashMap(); |
| 3189 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3190 | msg.obj = map; |
| 3191 | map.put("app", app); |
| 3192 | if (activity != null) { |
| 3193 | map.put("activity", activity); |
| 3194 | } |
| 3195 | |
| 3196 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3197 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3198 | } |
| 3199 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3200 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3201 | if (!mLaunchWarningShown) { |
| 3202 | mLaunchWarningShown = true; |
| 3203 | mHandler.post(new Runnable() { |
| 3204 | @Override |
| 3205 | public void run() { |
| 3206 | synchronized (ActivityManagerService.this) { |
| 3207 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3208 | d.show(); |
| 3209 | mHandler.postDelayed(new Runnable() { |
| 3210 | @Override |
| 3211 | public void run() { |
| 3212 | synchronized (ActivityManagerService.this) { |
| 3213 | d.dismiss(); |
| 3214 | mLaunchWarningShown = false; |
| 3215 | } |
| 3216 | } |
| 3217 | }, 4000); |
| 3218 | } |
| 3219 | } |
| 3220 | }); |
| 3221 | } |
| 3222 | } |
| 3223 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3224 | public boolean clearApplicationUserData(final String packageName, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3225 | final IPackageDataObserver observer, final int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3226 | enforceNotIsolatedCaller("clearApplicationUserData"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3227 | int uid = Binder.getCallingUid(); |
| 3228 | int pid = Binder.getCallingPid(); |
| 3229 | long callingId = Binder.clearCallingIdentity(); |
| 3230 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3231 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3232 | int pkgUid = -1; |
| 3233 | synchronized(this) { |
| 3234 | try { |
| 3235 | pkgUid = pm.getPackageUid(packageName); |
| 3236 | } catch (RemoteException e) { |
| 3237 | } |
| 3238 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3239 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3240 | return false; |
| 3241 | } |
| 3242 | if (uid == pkgUid || checkComponentPermission( |
| 3243 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3244 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3245 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3246 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3247 | } else { |
| 3248 | throw new SecurityException(pid+" does not have permission:"+ |
| 3249 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3250 | "for process:"+packageName); |
| 3251 | } |
| 3252 | } |
| 3253 | |
| 3254 | try { |
| 3255 | //clear application user data |
| 3256 | pm.clearApplicationUserData(packageName, observer); |
| 3257 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3258 | Uri.fromParts("package", packageName, null)); |
| 3259 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3260 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3261 | null, null, 0, null, null, null, false, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3262 | } catch (RemoteException e) { |
| 3263 | } |
| 3264 | } finally { |
| 3265 | Binder.restoreCallingIdentity(callingId); |
| 3266 | } |
| 3267 | return true; |
| 3268 | } |
| 3269 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3270 | public void killBackgroundProcesses(final String packageName) { |
| 3271 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3272 | != PackageManager.PERMISSION_GRANTED && |
| 3273 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3274 | != PackageManager.PERMISSION_GRANTED) { |
| 3275 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3276 | + Binder.getCallingPid() |
| 3277 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3278 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3279 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3280 | throw new SecurityException(msg); |
| 3281 | } |
| 3282 | |
| 3283 | long callingId = Binder.clearCallingIdentity(); |
| 3284 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3285 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3286 | int pkgUid = -1; |
| 3287 | synchronized(this) { |
| 3288 | try { |
| 3289 | pkgUid = pm.getPackageUid(packageName); |
| 3290 | } catch (RemoteException e) { |
| 3291 | } |
| 3292 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3293 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3294 | return; |
| 3295 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3296 | killPackageProcessesLocked(packageName, pkgUid, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3297 | ProcessList.SERVICE_ADJ, false, true, true, false, "kill background"); |
| 3298 | } |
| 3299 | } finally { |
| 3300 | Binder.restoreCallingIdentity(callingId); |
| 3301 | } |
| 3302 | } |
| 3303 | |
| 3304 | public void killAllBackgroundProcesses() { |
| 3305 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3306 | != PackageManager.PERMISSION_GRANTED) { |
| 3307 | String msg = "Permission Denial: killAllBackgroundProcesses() from pid=" |
| 3308 | + Binder.getCallingPid() |
| 3309 | + ", uid=" + Binder.getCallingUid() |
| 3310 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
| 3311 | Slog.w(TAG, msg); |
| 3312 | throw new SecurityException(msg); |
| 3313 | } |
| 3314 | |
| 3315 | long callingId = Binder.clearCallingIdentity(); |
| 3316 | try { |
| 3317 | synchronized(this) { |
| 3318 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 3319 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3320 | final int NA = apps.size(); |
| 3321 | for (int ia=0; ia<NA; ia++) { |
| 3322 | ProcessRecord app = apps.valueAt(ia); |
| 3323 | if (app.persistent) { |
| 3324 | // we don't kill persistent processes |
| 3325 | continue; |
| 3326 | } |
| 3327 | if (app.removed) { |
| 3328 | procs.add(app); |
| 3329 | } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 3330 | app.removed = true; |
| 3331 | procs.add(app); |
| 3332 | } |
| 3333 | } |
| 3334 | } |
| 3335 | |
| 3336 | int N = procs.size(); |
| 3337 | for (int i=0; i<N; i++) { |
| 3338 | removeProcessLocked(procs.get(i), false, true, "kill all background"); |
| 3339 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3340 | } |
| 3341 | } finally { |
| 3342 | Binder.restoreCallingIdentity(callingId); |
| 3343 | } |
| 3344 | } |
| 3345 | |
| 3346 | public void forceStopPackage(final String packageName) { |
| 3347 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3348 | != PackageManager.PERMISSION_GRANTED) { |
| 3349 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3350 | + Binder.getCallingPid() |
| 3351 | + ", uid=" + Binder.getCallingUid() |
| 3352 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3353 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3354 | throw new SecurityException(msg); |
| 3355 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3356 | final int userId = Binder.getOrigCallingUser(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3357 | long callingId = Binder.clearCallingIdentity(); |
| 3358 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3359 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3360 | int pkgUid = -1; |
| 3361 | synchronized(this) { |
| 3362 | try { |
| 3363 | pkgUid = pm.getPackageUid(packageName); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3364 | // Convert the uid to the one for the calling user |
| 3365 | pkgUid = UserId.getUid(userId, pkgUid); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3366 | } catch (RemoteException e) { |
| 3367 | } |
| 3368 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3369 | Slog.w(TAG, "Invalid packageName: " + packageName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3370 | return; |
| 3371 | } |
| 3372 | forceStopPackageLocked(packageName, pkgUid); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3373 | try { |
| 3374 | pm.setPackageStoppedState(packageName, true); |
| 3375 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 3376 | } catch (IllegalArgumentException e) { |
| 3377 | Slog.w(TAG, "Failed trying to unstop package " |
| 3378 | + packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3379 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3380 | } |
| 3381 | } finally { |
| 3382 | Binder.restoreCallingIdentity(callingId); |
| 3383 | } |
| 3384 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3385 | |
| 3386 | /* |
| 3387 | * The pkg name and uid have to be specified. |
| 3388 | * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int) |
| 3389 | */ |
| 3390 | public void killApplicationWithUid(String pkg, int uid) { |
| 3391 | if (pkg == null) { |
| 3392 | return; |
| 3393 | } |
| 3394 | // Make sure the uid is valid. |
| 3395 | if (uid < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3396 | Slog.w(TAG, "Invalid uid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3397 | return; |
| 3398 | } |
| 3399 | int callerUid = Binder.getCallingUid(); |
| 3400 | // Only the system server can kill an application |
| 3401 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3402 | // Post an aysnc message to kill the application |
| 3403 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
| 3404 | msg.arg1 = uid; |
| 3405 | msg.arg2 = 0; |
| 3406 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3407 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3408 | } else { |
| 3409 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3410 | pkg); |
| 3411 | } |
| 3412 | } |
| 3413 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3414 | public void closeSystemDialogs(String reason) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3415 | enforceNotIsolatedCaller("closeSystemDialogs"); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3416 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3417 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3418 | if (reason != null) { |
| 3419 | intent.putExtra("reason", reason); |
| 3420 | } |
| 3421 | |
| 3422 | final int uid = Binder.getCallingUid(); |
| 3423 | final long origId = Binder.clearCallingIdentity(); |
| 3424 | synchronized (this) { |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3425 | mWindowManager.closeSystemDialogs(reason); |
| 3426 | |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 3427 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3428 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3429 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3430 | r.stack.finishActivityLocked(r, i, |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3431 | Activity.RESULT_CANCELED, null, "close-sys"); |
| 3432 | } |
| 3433 | } |
| 3434 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3435 | broadcastIntentLocked(null, null, intent, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3436 | null, 0, null, null, null, false, false, -1, uid, 0 /* TODO: Verify */); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3437 | } |
| 3438 | Binder.restoreCallingIdentity(origId); |
| 3439 | } |
| 3440 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3441 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3442 | throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3443 | enforceNotIsolatedCaller("getProcessMemoryInfo"); |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3444 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3445 | for (int i=pids.length-1; i>=0; i--) { |
| 3446 | infos[i] = new Debug.MemoryInfo(); |
| 3447 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3448 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3449 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3450 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3451 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3452 | public long[] getProcessPss(int[] pids) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3453 | enforceNotIsolatedCaller("getProcessPss"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3454 | long[] pss = new long[pids.length]; |
| 3455 | for (int i=pids.length-1; i>=0; i--) { |
| 3456 | pss[i] = Debug.getPss(pids[i]); |
| 3457 | } |
| 3458 | return pss; |
| 3459 | } |
| 3460 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3461 | public void killApplicationProcess(String processName, int uid) { |
| 3462 | if (processName == null) { |
| 3463 | return; |
| 3464 | } |
| 3465 | |
| 3466 | int callerUid = Binder.getCallingUid(); |
| 3467 | // Only the system server can kill an application |
| 3468 | if (callerUid == Process.SYSTEM_UID) { |
| 3469 | synchronized (this) { |
| 3470 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3471 | if (app != null && app.thread != null) { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3472 | try { |
| 3473 | app.thread.scheduleSuicide(); |
| 3474 | } catch (RemoteException e) { |
| 3475 | // If the other end already died, then our work here is done. |
| 3476 | } |
| 3477 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3478 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3479 | + processName + " / " + uid); |
| 3480 | } |
| 3481 | } |
| 3482 | } else { |
| 3483 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3484 | processName); |
| 3485 | } |
| 3486 | } |
| 3487 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3488 | private void forceStopPackageLocked(final String packageName, int uid) { |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3489 | forceStopPackageLocked(packageName, uid, false, false, true, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3490 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3491 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3492 | if (!mProcessesReady) { |
| 3493 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3494 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3495 | intent.putExtra(Intent.EXTRA_UID, uid); |
| 3496 | broadcastIntentLocked(null, null, intent, |
| 3497 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3498 | false, false, |
| 3499 | MY_PID, Process.SYSTEM_UID, UserId.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3500 | } |
| 3501 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3502 | private final boolean killPackageProcessesLocked(String packageName, int uid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3503 | int minOomAdj, boolean callerWillRestart, boolean allowRestart, boolean doit, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3504 | boolean evenPersistent, String reason) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3505 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3506 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3507 | // Remove all processes this package may have touched: all with the |
| 3508 | // same UID (except for the system or root user), and all whose name |
| 3509 | // matches the package name. |
| 3510 | final String procNamePrefix = packageName + ":"; |
| 3511 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3512 | final int NA = apps.size(); |
| 3513 | for (int ia=0; ia<NA; ia++) { |
| 3514 | ProcessRecord app = apps.valueAt(ia); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3515 | if (app.persistent && !evenPersistent) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3516 | // we don't kill persistent processes |
| 3517 | continue; |
| 3518 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3519 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3520 | if (doit) { |
| 3521 | procs.add(app); |
| 3522 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3523 | } else if ((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid) |
| 3524 | || app.processName.equals(packageName) |
| 3525 | || app.processName.startsWith(procNamePrefix)) { |
| 3526 | if (app.setAdj >= minOomAdj) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3527 | if (!doit) { |
| 3528 | return true; |
| 3529 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3530 | app.removed = true; |
| 3531 | procs.add(app); |
| 3532 | } |
| 3533 | } |
| 3534 | } |
| 3535 | } |
| 3536 | |
| 3537 | int N = procs.size(); |
| 3538 | for (int i=0; i<N; i++) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3539 | removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3540 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3541 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3542 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3543 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3544 | private final boolean forceStopPackageLocked(String name, int uid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3545 | boolean callerWillRestart, boolean purgeCache, boolean doit, |
| 3546 | boolean evenPersistent) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3547 | int i; |
| 3548 | int N; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3549 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3550 | if (uid < 0) { |
| 3551 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3552 | uid = AppGlobals.getPackageManager().getPackageUid(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3553 | } catch (RemoteException e) { |
| 3554 | } |
| 3555 | } |
| 3556 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3557 | if (doit) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3558 | Slog.i(TAG, "Force stopping package " + name + " uid=" + uid); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3559 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3560 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3561 | while (badApps.hasNext()) { |
| 3562 | SparseArray<Long> ba = badApps.next(); |
| 3563 | if (ba.get(uid) != null) { |
| 3564 | badApps.remove(); |
| 3565 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3566 | } |
| 3567 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3568 | |
| 3569 | boolean didSomething = killPackageProcessesLocked(name, uid, -100, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3570 | callerWillRestart, false, doit, evenPersistent, "force stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3571 | |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3572 | TaskRecord lastTask = null; |
| 3573 | for (i=0; i<mMainStack.mHistory.size(); i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3574 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3575 | final boolean samePackage = r.packageName.equals(name); |
| 3576 | if ((samePackage || r.task == lastTask) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3577 | && (r.app == null || evenPersistent || !r.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3578 | if (!doit) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 3579 | if (r.finishing) { |
| 3580 | // If this activity is just finishing, then it is not |
| 3581 | // interesting as far as something to stop. |
| 3582 | continue; |
| 3583 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3584 | return true; |
| 3585 | } |
| 3586 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3587 | Slog.i(TAG, " Force finishing activity " + r); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3588 | if (samePackage) { |
| 3589 | if (r.app != null) { |
| 3590 | r.app.removed = true; |
| 3591 | } |
| 3592 | r.app = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3593 | } |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3594 | lastTask = r.task; |
| 3595 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
| 3596 | null, "force-stop")) { |
| 3597 | i--; |
| 3598 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3599 | } |
| 3600 | } |
| 3601 | |
| 3602 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3603 | int userId = UserId.getUserId(uid); |
| 3604 | for (ServiceRecord service : mServiceMap.getAllServices(userId)) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3605 | if (service.packageName.equals(name) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3606 | && (service.app == null || evenPersistent || !service.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3607 | if (!doit) { |
| 3608 | return true; |
| 3609 | } |
| 3610 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3611 | Slog.i(TAG, " Force stopping service " + service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3612 | if (service.app != null) { |
| 3613 | service.app.removed = true; |
| 3614 | } |
| 3615 | service.app = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3616 | service.isolatedProc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3617 | services.add(service); |
| 3618 | } |
| 3619 | } |
| 3620 | |
| 3621 | N = services.size(); |
| 3622 | for (i=0; i<N; i++) { |
| 3623 | bringDownServiceLocked(services.get(i), true); |
| 3624 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3625 | |
| 3626 | ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); |
| 3627 | for (ContentProviderRecord provider : mProvidersByClass.values()) { |
| 3628 | if (provider.info.packageName.equals(name) |
| 3629 | && (provider.proc == null || evenPersistent || !provider.proc.persistent)) { |
| 3630 | if (!doit) { |
| 3631 | return true; |
| 3632 | } |
| 3633 | didSomething = true; |
| 3634 | providers.add(provider); |
| 3635 | } |
| 3636 | } |
| 3637 | |
| 3638 | N = providers.size(); |
| 3639 | for (i=0; i<N; i++) { |
| 3640 | removeDyingProviderLocked(null, providers.get(i)); |
| 3641 | } |
| 3642 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3643 | if (doit) { |
| 3644 | if (purgeCache) { |
| 3645 | AttributeCache ac = AttributeCache.instance(); |
| 3646 | if (ac != null) { |
| 3647 | ac.removePackage(name); |
| 3648 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3649 | } |
Dianne Hackborn | 38cc896 | 2011-10-13 11:33:55 -0700 | [diff] [blame] | 3650 | if (mBooted) { |
| 3651 | mMainStack.resumeTopActivityLocked(null); |
| 3652 | mMainStack.scheduleIdleLocked(); |
| 3653 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3654 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3655 | |
| 3656 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3657 | } |
| 3658 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3659 | private final boolean removeProcessLocked(ProcessRecord app, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3660 | boolean callerWillRestart, boolean allowRestart, String reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3661 | final String name = app.processName; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3662 | final int uid = app.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3663 | if (DEBUG_PROCESSES) Slog.d( |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3664 | TAG, "Force removing proc " + app.toShortString() + " (" + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3665 | + "/" + uid + ")"); |
| 3666 | |
| 3667 | mProcessNames.remove(name, uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3668 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3669 | if (mHeavyWeightProcess == app) { |
| 3670 | mHeavyWeightProcess = null; |
| 3671 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3672 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3673 | boolean needRestart = false; |
| 3674 | if (app.pid > 0 && app.pid != MY_PID) { |
| 3675 | int pid = app.pid; |
| 3676 | synchronized (mPidsSelfLocked) { |
| 3677 | mPidsSelfLocked.remove(pid); |
| 3678 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3679 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3680 | Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3681 | handleAppDiedLocked(app, true, allowRestart); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3682 | mLruProcesses.remove(app); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3683 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3684 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3685 | if (app.persistent && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3686 | if (!callerWillRestart) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3687 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3688 | } else { |
| 3689 | needRestart = true; |
| 3690 | } |
| 3691 | } |
| 3692 | } else { |
| 3693 | mRemovedProcesses.add(app); |
| 3694 | } |
| 3695 | |
| 3696 | return needRestart; |
| 3697 | } |
| 3698 | |
| 3699 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 3700 | final int pid = app.pid; |
| 3701 | boolean gone = false; |
| 3702 | synchronized (mPidsSelfLocked) { |
| 3703 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 3704 | if (knownApp != null && knownApp.thread == null) { |
| 3705 | mPidsSelfLocked.remove(pid); |
| 3706 | gone = true; |
| 3707 | } |
| 3708 | } |
| 3709 | |
| 3710 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3711 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3712 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.uid, |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3713 | app.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3714 | mProcessNames.remove(app.processName, app.uid); |
| 3715 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3716 | if (mHeavyWeightProcess == app) { |
| 3717 | mHeavyWeightProcess = null; |
| 3718 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 3719 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3720 | // Take care of any launching providers waiting for this process. |
| 3721 | checkAppInLaunchingProvidersLocked(app, true); |
| 3722 | // Take care of any services that are waiting for the process. |
| 3723 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3724 | ServiceRecord sr = mPendingServices.get(i); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3725 | if ((app.uid == sr.appInfo.uid |
| 3726 | && app.processName.equals(sr.processName)) |
| 3727 | || sr.isolatedProc == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3728 | Slog.w(TAG, "Forcing bringing down service: " + sr); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3729 | sr.isolatedProc = null; |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3730 | mPendingServices.remove(i); |
| 3731 | i--; |
| 3732 | bringDownServiceLocked(sr, true); |
| 3733 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3734 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3735 | EventLog.writeEvent(EventLogTags.AM_KILL, pid, |
| 3736 | app.processName, app.setAdj, "start timeout"); |
| 3737 | Process.killProcessQuiet(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3738 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3739 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3740 | try { |
| 3741 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 3742 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 3743 | bm.agentDisconnected(app.info.packageName); |
| 3744 | } catch (RemoteException e) { |
| 3745 | // Can't happen; the backup manager is local |
| 3746 | } |
| 3747 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 3748 | if (isPendingBroadcastProcessLocked(pid)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3749 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 3750 | skipPendingBroadcastLocked(pid); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3751 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3752 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3753 | 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] | 3754 | } |
| 3755 | } |
| 3756 | |
| 3757 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 3758 | int pid) { |
| 3759 | |
| 3760 | // Find the application record that is being attached... either via |
| 3761 | // the pid if we are running in multiple processes, or just pull the |
| 3762 | // next app record if we are emulating process with anonymous threads. |
| 3763 | ProcessRecord app; |
| 3764 | if (pid != MY_PID && pid >= 0) { |
| 3765 | synchronized (mPidsSelfLocked) { |
| 3766 | app = mPidsSelfLocked.get(pid); |
| 3767 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3768 | } else { |
| 3769 | app = null; |
| 3770 | } |
| 3771 | |
| 3772 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3773 | 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] | 3774 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3775 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3776 | if (pid > 0 && pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3777 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3778 | } else { |
| 3779 | try { |
| 3780 | thread.scheduleExit(); |
| 3781 | } catch (Exception e) { |
| 3782 | // Ignore exceptions. |
| 3783 | } |
| 3784 | } |
| 3785 | return false; |
| 3786 | } |
| 3787 | |
| 3788 | // If this application record is still attached to a previous |
| 3789 | // process, clean it up now. |
| 3790 | if (app.thread != null) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3791 | handleAppDiedLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | // Tell the process all about itself. |
| 3795 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3796 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3797 | TAG, "Binding process pid " + pid + " to record " + app); |
| 3798 | |
| 3799 | String processName = app.processName; |
| 3800 | try { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 3801 | AppDeathRecipient adr = new AppDeathRecipient( |
| 3802 | app, pid, thread); |
| 3803 | thread.asBinder().linkToDeath(adr, 0); |
| 3804 | app.deathRecipient = adr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3805 | } catch (RemoteException e) { |
| 3806 | app.resetPackageList(); |
| 3807 | startProcessLocked(app, "link fail", processName); |
| 3808 | return false; |
| 3809 | } |
| 3810 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3811 | 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] | 3812 | |
| 3813 | app.thread = thread; |
| 3814 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 3815 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 3816 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3817 | app.forcingToForeground = null; |
| 3818 | app.foregroundServices = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 3819 | app.hasShownUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3820 | app.debugging = false; |
| 3821 | |
| 3822 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3823 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3824 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3825 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3826 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3827 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3828 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3829 | } |
| 3830 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3831 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3832 | TAG, "New app record " + app |
| 3833 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 3834 | try { |
| 3835 | int testMode = IApplicationThread.DEBUG_OFF; |
| 3836 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 3837 | testMode = mWaitForDebugger |
| 3838 | ? IApplicationThread.DEBUG_WAIT |
| 3839 | : IApplicationThread.DEBUG_ON; |
| 3840 | app.debugging = true; |
| 3841 | if (mDebugTransient) { |
| 3842 | mDebugApp = mOrigDebugApp; |
| 3843 | mWaitForDebugger = mOrigWaitForDebugger; |
| 3844 | } |
| 3845 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3846 | String profileFile = app.instrumentationProfileFile; |
| 3847 | ParcelFileDescriptor profileFd = null; |
| 3848 | boolean profileAutoStop = false; |
| 3849 | if (mProfileApp != null && mProfileApp.equals(processName)) { |
| 3850 | mProfileProc = app; |
| 3851 | profileFile = mProfileFile; |
| 3852 | profileFd = mProfileFd; |
| 3853 | profileAutoStop = mAutoStopProfiler; |
| 3854 | } |
| 3855 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3856 | // If the app is being launched for restore or full backup, set it up specially |
| 3857 | boolean isRestrictedBackupMode = false; |
| 3858 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 3859 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 3860 | || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3861 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 3862 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 3863 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 3864 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 3865 | ? app.instrumentationInfo.packageName |
| 3866 | : app.info.packageName); |
| 3867 | if (app.instrumentationClass != null) { |
| 3868 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 3869 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3870 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 3871 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3872 | ApplicationInfo appInfo = app.instrumentationInfo != null |
| 3873 | ? app.instrumentationInfo : app.info; |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 3874 | app.compat = compatibilityInfoForPackageLocked(appInfo); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3875 | if (profileFd != null) { |
| 3876 | profileFd = profileFd.dup(); |
| 3877 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3878 | thread.bindApplication(processName, appInfo, providers, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3879 | app.instrumentationClass, profileFile, profileFd, profileAutoStop, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3880 | app.instrumentationArguments, app.instrumentationWatcher, testMode, |
Dianne Hackborn | 5d927c2 | 2011-09-02 12:22:18 -0700 | [diff] [blame] | 3881 | isRestrictedBackupMode || !normalMode, app.persistent, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 3882 | new Configuration(mConfiguration), app.compat, getCommonServicesLocked(), |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 3883 | mCoreSettingsObserver.getCoreSettingsLocked()); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3884 | updateLruProcessLocked(app, false, true); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3885 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3886 | } catch (Exception e) { |
| 3887 | // todo: Yikes! What should we do? For now we will try to |
| 3888 | // start another process, but that could easily get us in |
| 3889 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3890 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3891 | |
| 3892 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 3893 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3894 | startProcessLocked(app, "bind fail", processName); |
| 3895 | return false; |
| 3896 | } |
| 3897 | |
| 3898 | // Remove this record from the list of starting applications. |
| 3899 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3900 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 3901 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3902 | mProcessesOnHold.remove(app); |
| 3903 | |
| 3904 | boolean badApp = false; |
| 3905 | boolean didSomething = false; |
| 3906 | |
| 3907 | // 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] | 3908 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 3909 | if (hr != null && normalMode) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3910 | if (hr.app == null && app.uid == hr.info.applicationInfo.uid |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3911 | && processName.equals(hr.processName)) { |
| 3912 | try { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 3913 | if (mHeadless) { |
| 3914 | Slog.e(TAG, "Starting activities not supported on headless device: " + hr); |
| 3915 | } else if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3916 | didSomething = true; |
| 3917 | } |
| 3918 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3919 | 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] | 3920 | + hr.intent.getComponent().flattenToShortString(), e); |
| 3921 | badApp = true; |
| 3922 | } |
| 3923 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3924 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3925 | } |
| 3926 | } |
| 3927 | |
| 3928 | // Find any services that should be running in this process... |
| 3929 | if (!badApp && mPendingServices.size() > 0) { |
| 3930 | ServiceRecord sr = null; |
| 3931 | try { |
| 3932 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3933 | sr = mPendingServices.get(i); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3934 | if (app != sr.isolatedProc && (app.uid != sr.appInfo.uid |
| 3935 | || !processName.equals(sr.processName))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3936 | continue; |
| 3937 | } |
| 3938 | |
| 3939 | mPendingServices.remove(i); |
| 3940 | i--; |
| 3941 | realStartServiceLocked(sr, app); |
| 3942 | didSomething = true; |
| 3943 | } |
| 3944 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3945 | 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] | 3946 | + sr.shortName, e); |
| 3947 | badApp = true; |
| 3948 | } |
| 3949 | } |
| 3950 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 3951 | // Check if a next-broadcast receiver is in this process... |
| 3952 | if (!badApp && isPendingBroadcastProcessLocked(pid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3953 | try { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 3954 | didSomething = sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3955 | } catch (Exception e) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 3956 | // If the app died trying to launch the receiver we declare it 'bad' |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3957 | badApp = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3958 | } |
| 3959 | } |
| 3960 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3961 | // Check whether the next backup agent is in this process... |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3962 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3963 | 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] | 3964 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3965 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3966 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, |
| 3967 | compatibilityInfoForPackageLocked(mBackupTarget.appInfo), |
| 3968 | mBackupTarget.backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3969 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3970 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3971 | e.printStackTrace(); |
| 3972 | } |
| 3973 | } |
| 3974 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3975 | if (badApp) { |
| 3976 | // todo: Also need to kill application to deal with all |
| 3977 | // kinds of exceptions. |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3978 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3979 | return false; |
| 3980 | } |
| 3981 | |
| 3982 | if (!didSomething) { |
| 3983 | updateOomAdjLocked(); |
| 3984 | } |
| 3985 | |
| 3986 | return true; |
| 3987 | } |
| 3988 | |
| 3989 | public final void attachApplication(IApplicationThread thread) { |
| 3990 | synchronized (this) { |
| 3991 | int callingPid = Binder.getCallingPid(); |
| 3992 | final long origId = Binder.clearCallingIdentity(); |
| 3993 | attachApplicationLocked(thread, callingPid); |
| 3994 | Binder.restoreCallingIdentity(origId); |
| 3995 | } |
| 3996 | } |
| 3997 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 3998 | 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] | 3999 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4000 | ActivityRecord r = mMainStack.activityIdleInternal(token, false, config); |
| 4001 | if (stopProfiling) { |
| 4002 | synchronized (this) { |
| 4003 | if (mProfileProc == r.app) { |
| 4004 | if (mProfileFd != null) { |
| 4005 | try { |
| 4006 | mProfileFd.close(); |
| 4007 | } catch (IOException e) { |
| 4008 | } |
| 4009 | clearProfilerLocked(); |
| 4010 | } |
| 4011 | } |
| 4012 | } |
| 4013 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4014 | Binder.restoreCallingIdentity(origId); |
| 4015 | } |
| 4016 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4017 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4018 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4019 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4020 | mWindowManager.enableScreenAfterBoot(); |
| 4021 | } |
| 4022 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4023 | public void showBootMessage(final CharSequence msg, final boolean always) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4024 | enforceNotIsolatedCaller("showBootMessage"); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4025 | mWindowManager.showBootMessage(msg, always); |
| 4026 | } |
| 4027 | |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4028 | public void dismissKeyguardOnNextActivity() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4029 | enforceNotIsolatedCaller("dismissKeyguardOnNextActivity"); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4030 | synchronized (this) { |
| 4031 | mMainStack.dismissKeyguardOnNextActivityLocked(); |
| 4032 | } |
| 4033 | } |
| 4034 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4035 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4036 | IntentFilter pkgFilter = new IntentFilter(); |
| 4037 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 4038 | pkgFilter.addDataScheme("package"); |
| 4039 | mContext.registerReceiver(new BroadcastReceiver() { |
| 4040 | @Override |
| 4041 | public void onReceive(Context context, Intent intent) { |
| 4042 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 4043 | if (pkgs != null) { |
| 4044 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 4045 | synchronized (ActivityManagerService.this) { |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 4046 | if (forceStopPackageLocked(pkg, -1, false, false, false, false)) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 4047 | setResultCode(Activity.RESULT_OK); |
| 4048 | return; |
| 4049 | } |
| 4050 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4051 | } |
| 4052 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4053 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4054 | }, pkgFilter); |
| 4055 | |
| 4056 | synchronized (this) { |
| 4057 | // Ensure that any processes we had put on hold are now started |
| 4058 | // up. |
| 4059 | final int NP = mProcessesOnHold.size(); |
| 4060 | if (NP > 0) { |
| 4061 | ArrayList<ProcessRecord> procs = |
| 4062 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 4063 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4064 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 4065 | + procs.get(ip)); |
| 4066 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4067 | } |
| 4068 | } |
| 4069 | |
| 4070 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4071 | // Start looking for apps that are abusing wake locks. |
| 4072 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 4073 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4074 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 4075 | SystemProperties.set("sys.boot_completed", "1"); |
Guang Zhu | 191713a | 2012-01-12 12:02:22 -0800 | [diff] [blame] | 4076 | SystemProperties.set("dev.bootcomplete", "1"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4077 | /* TODO: Send this to all users that are to be logged in on startup */ |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4078 | broadcastIntentLocked(null, null, |
| 4079 | new Intent(Intent.ACTION_BOOT_COMPLETED, null), |
| 4080 | null, null, 0, null, null, |
| 4081 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4082 | false, false, MY_PID, Process.SYSTEM_UID, Binder.getOrigCallingUser()); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4083 | } |
| 4084 | } |
| 4085 | } |
| 4086 | |
| 4087 | final void ensureBootCompleted() { |
| 4088 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4089 | boolean enableScreen; |
| 4090 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4091 | booting = mBooting; |
| 4092 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4093 | enableScreen = !mBooted; |
| 4094 | mBooted = true; |
| 4095 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4096 | |
| 4097 | if (booting) { |
| 4098 | finishBooting(); |
| 4099 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4100 | |
| 4101 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4102 | enableScreenAfterBoot(); |
| 4103 | } |
| 4104 | } |
| 4105 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4106 | public final void activityPaused(IBinder token) { |
| 4107 | final long origId = Binder.clearCallingIdentity(); |
| 4108 | mMainStack.activityPaused(token, false); |
| 4109 | Binder.restoreCallingIdentity(origId); |
| 4110 | } |
| 4111 | |
| 4112 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 4113 | CharSequence description) { |
| 4114 | if (localLOGV) Slog.v( |
| 4115 | TAG, "Activity stopped: token=" + token); |
| 4116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4117 | // Refuse possible leaked file descriptors |
| 4118 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 4119 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 4120 | } |
| 4121 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4122 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4123 | |
| 4124 | final long origId = Binder.clearCallingIdentity(); |
| 4125 | |
| 4126 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4127 | r = mMainStack.isInStackLocked(token); |
| 4128 | if (r != null) { |
| 4129 | r.stack.activityStoppedLocked(r, icicle, thumbnail, description); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | if (r != null) { |
| 4134 | sendPendingThumbnail(r, null, null, null, false); |
| 4135 | } |
| 4136 | |
| 4137 | trimApplications(); |
| 4138 | |
| 4139 | Binder.restoreCallingIdentity(origId); |
| 4140 | } |
| 4141 | |
| 4142 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4143 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4144 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | public String getCallingPackage(IBinder token) { |
| 4148 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4149 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 4150 | 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] | 4151 | } |
| 4152 | } |
| 4153 | |
| 4154 | public ComponentName getCallingActivity(IBinder token) { |
| 4155 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4156 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4157 | return r != null ? r.intent.getComponent() : null; |
| 4158 | } |
| 4159 | } |
| 4160 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4161 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4162 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4163 | if (r == null) { |
| 4164 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4165 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4166 | return r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | public ComponentName getActivityClassForToken(IBinder token) { |
| 4170 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4171 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4172 | if (r == null) { |
| 4173 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4174 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4175 | return r.intent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4176 | } |
| 4177 | } |
| 4178 | |
| 4179 | public String getPackageForToken(IBinder token) { |
| 4180 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4181 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4182 | if (r == null) { |
| 4183 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4184 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4185 | return r.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4186 | } |
| 4187 | } |
| 4188 | |
| 4189 | public IIntentSender getIntentSender(int type, |
| 4190 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4191 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4192 | enforceNotIsolatedCaller("getIntentSender"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4193 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4194 | if (intents != null) { |
| 4195 | if (intents.length < 1) { |
| 4196 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 4197 | } |
| 4198 | for (int i=0; i<intents.length; i++) { |
| 4199 | Intent intent = intents[i]; |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4200 | if (intent != null) { |
| 4201 | if (intent.hasFileDescriptors()) { |
| 4202 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 4203 | } |
| 4204 | if (type == INTENT_SENDER_BROADCAST && |
| 4205 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 4206 | throw new IllegalArgumentException( |
| 4207 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 4208 | } |
| 4209 | intents[i] = new Intent(intent); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4210 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4211 | } |
| 4212 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4213 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4214 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4215 | } |
| 4216 | } |
| 4217 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4218 | synchronized(this) { |
| 4219 | int callingUid = Binder.getCallingUid(); |
| 4220 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4221 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4222 | int uid = AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4223 | .getPackageUid(packageName); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4224 | if (UserId.getAppId(callingUid) != uid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4225 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 4226 | + Binder.getCallingPid() |
| 4227 | + ", uid=" + Binder.getCallingUid() |
| 4228 | + ", (need uid=" + uid + ")" |
| 4229 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4230 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4231 | throw new SecurityException(msg); |
| 4232 | } |
| 4233 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4234 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4235 | if (DEBUG_MU) |
| 4236 | Slog.i(TAG_MU, "Getting intent sender for origCallingUid=" |
| 4237 | + Binder.getOrigCallingUid()); |
| 4238 | return getIntentSenderLocked(type, packageName, Binder.getOrigCallingUid(), |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4239 | token, resultWho, requestCode, intents, resolvedTypes, flags); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4240 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4241 | } catch (RemoteException e) { |
| 4242 | throw new SecurityException(e); |
| 4243 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4244 | } |
| 4245 | } |
| 4246 | |
| 4247 | IIntentSender getIntentSenderLocked(int type, |
| 4248 | String packageName, int callingUid, IBinder token, String resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4249 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4250 | if (DEBUG_MU) |
| 4251 | Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4252 | ActivityRecord activity = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4253 | if (type == INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4254 | activity = mMainStack.isInStackLocked(token); |
| 4255 | if (activity == null) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4256 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4257 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4258 | if (activity.finishing) { |
| 4259 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4260 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4261 | } |
| 4262 | |
| 4263 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 4264 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 4265 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 4266 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 4267 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 4268 | |
| 4269 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 4270 | type, packageName, activity, resultWho, |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4271 | requestCode, intents, resolvedTypes, flags); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4272 | WeakReference<PendingIntentRecord> ref; |
| 4273 | ref = mIntentSenderRecords.get(key); |
| 4274 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 4275 | if (rec != null) { |
| 4276 | if (!cancelCurrent) { |
| 4277 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4278 | if (rec.key.requestIntent != null) { |
| 4279 | rec.key.requestIntent.replaceExtras(intents != null ? intents[0] : null); |
| 4280 | } |
| 4281 | if (intents != null) { |
| 4282 | intents[intents.length-1] = rec.key.requestIntent; |
| 4283 | rec.key.allIntents = intents; |
| 4284 | rec.key.allResolvedTypes = resolvedTypes; |
| 4285 | } else { |
| 4286 | rec.key.allIntents = null; |
| 4287 | rec.key.allResolvedTypes = null; |
| 4288 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4289 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4290 | return rec; |
| 4291 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4292 | rec.canceled = true; |
| 4293 | mIntentSenderRecords.remove(key); |
| 4294 | } |
| 4295 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4296 | return rec; |
| 4297 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4298 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4299 | mIntentSenderRecords.put(key, rec.ref); |
| 4300 | if (type == INTENT_SENDER_ACTIVITY_RESULT) { |
| 4301 | if (activity.pendingResults == null) { |
| 4302 | activity.pendingResults |
| 4303 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4304 | } |
| 4305 | activity.pendingResults.add(rec.ref); |
| 4306 | } |
| 4307 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | public void cancelIntentSender(IIntentSender sender) { |
| 4311 | if (!(sender instanceof PendingIntentRecord)) { |
| 4312 | return; |
| 4313 | } |
| 4314 | synchronized(this) { |
| 4315 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4316 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4317 | int uid = AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4318 | .getPackageUid(rec.key.packageName); |
| 4319 | if (uid != Binder.getCallingUid()) { |
| 4320 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4321 | + Binder.getCallingPid() |
| 4322 | + ", uid=" + Binder.getCallingUid() |
| 4323 | + " is not allowed to cancel packges " |
| 4324 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4325 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4326 | throw new SecurityException(msg); |
| 4327 | } |
| 4328 | } catch (RemoteException e) { |
| 4329 | throw new SecurityException(e); |
| 4330 | } |
| 4331 | cancelIntentSenderLocked(rec, true); |
| 4332 | } |
| 4333 | } |
| 4334 | |
| 4335 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4336 | rec.canceled = true; |
| 4337 | mIntentSenderRecords.remove(rec.key); |
| 4338 | if (cleanActivity && rec.key.activity != null) { |
| 4339 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4340 | } |
| 4341 | } |
| 4342 | |
| 4343 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4344 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4345 | return null; |
| 4346 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4347 | try { |
| 4348 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4349 | return res.key.packageName; |
| 4350 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4351 | } |
| 4352 | return null; |
| 4353 | } |
| 4354 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 4355 | public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) { |
| 4356 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4357 | return false; |
| 4358 | } |
| 4359 | try { |
| 4360 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4361 | if (res.key.allIntents == null) { |
| 4362 | return false; |
| 4363 | } |
| 4364 | for (int i=0; i<res.key.allIntents.length; i++) { |
| 4365 | Intent intent = res.key.allIntents[i]; |
| 4366 | if (intent.getPackage() != null && intent.getComponent() != null) { |
| 4367 | return false; |
| 4368 | } |
| 4369 | } |
| 4370 | return true; |
| 4371 | } catch (ClassCastException e) { |
| 4372 | } |
| 4373 | return false; |
| 4374 | } |
| 4375 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4376 | public void setProcessLimit(int max) { |
| 4377 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4378 | "setProcessLimit()"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4379 | synchronized (this) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4380 | mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4381 | mProcessLimitOverride = max; |
| 4382 | } |
| 4383 | trimApplications(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4384 | } |
| 4385 | |
| 4386 | public int getProcessLimit() { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4387 | synchronized (this) { |
| 4388 | return mProcessLimitOverride; |
| 4389 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | void foregroundTokenDied(ForegroundToken token) { |
| 4393 | synchronized (ActivityManagerService.this) { |
| 4394 | synchronized (mPidsSelfLocked) { |
| 4395 | ForegroundToken cur |
| 4396 | = mForegroundProcesses.get(token.pid); |
| 4397 | if (cur != token) { |
| 4398 | return; |
| 4399 | } |
| 4400 | mForegroundProcesses.remove(token.pid); |
| 4401 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4402 | if (pr == null) { |
| 4403 | return; |
| 4404 | } |
| 4405 | pr.forcingToForeground = null; |
| 4406 | pr.foregroundServices = false; |
| 4407 | } |
| 4408 | updateOomAdjLocked(); |
| 4409 | } |
| 4410 | } |
| 4411 | |
| 4412 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4413 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4414 | "setProcessForeground()"); |
| 4415 | synchronized(this) { |
| 4416 | boolean changed = false; |
| 4417 | |
| 4418 | synchronized (mPidsSelfLocked) { |
| 4419 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4420 | if (pr == null && isForeground) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4421 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4422 | return; |
| 4423 | } |
| 4424 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4425 | if (oldToken != null) { |
| 4426 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4427 | mForegroundProcesses.remove(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4428 | if (pr != null) { |
| 4429 | pr.forcingToForeground = null; |
| 4430 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4431 | changed = true; |
| 4432 | } |
| 4433 | if (isForeground && token != null) { |
| 4434 | ForegroundToken newToken = new ForegroundToken() { |
| 4435 | public void binderDied() { |
| 4436 | foregroundTokenDied(this); |
| 4437 | } |
| 4438 | }; |
| 4439 | newToken.pid = pid; |
| 4440 | newToken.token = token; |
| 4441 | try { |
| 4442 | token.linkToDeath(newToken, 0); |
| 4443 | mForegroundProcesses.put(pid, newToken); |
| 4444 | pr.forcingToForeground = token; |
| 4445 | changed = true; |
| 4446 | } catch (RemoteException e) { |
| 4447 | // If the process died while doing this, we will later |
| 4448 | // do the cleanup with the process death link. |
| 4449 | } |
| 4450 | } |
| 4451 | } |
| 4452 | |
| 4453 | if (changed) { |
| 4454 | updateOomAdjLocked(); |
| 4455 | } |
| 4456 | } |
| 4457 | } |
| 4458 | |
| 4459 | // ========================================================= |
| 4460 | // PERMISSIONS |
| 4461 | // ========================================================= |
| 4462 | |
| 4463 | static class PermissionController extends IPermissionController.Stub { |
| 4464 | ActivityManagerService mActivityManagerService; |
| 4465 | PermissionController(ActivityManagerService activityManagerService) { |
| 4466 | mActivityManagerService = activityManagerService; |
| 4467 | } |
| 4468 | |
| 4469 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4470 | return mActivityManagerService.checkPermission(permission, pid, |
| 4471 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4472 | } |
| 4473 | } |
| 4474 | |
| 4475 | /** |
| 4476 | * This can be called with or without the global lock held. |
| 4477 | */ |
| 4478 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4479 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4480 | // We might be performing an operation on behalf of an indirect binder |
| 4481 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4482 | // client identity accordingly before proceeding. |
| 4483 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4484 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4485 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4486 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4487 | uid = tlsIdentity.uid; |
| 4488 | pid = tlsIdentity.pid; |
| 4489 | } |
| 4490 | |
| 4491 | // Root, system server and our own process get to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4492 | 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] | 4493 | return PackageManager.PERMISSION_GRANTED; |
| 4494 | } |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4495 | // Isolated processes don't get any permissions. |
| 4496 | if (UserId.isIsolated(uid)) { |
| 4497 | return PackageManager.PERMISSION_DENIED; |
| 4498 | } |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4499 | // If there is a uid that owns whatever is being accessed, it has |
| 4500 | // blanket access to it regardless of the permissions it requires. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4501 | if (owningUid >= 0 && UserId.isSameApp(uid, owningUid)) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4502 | return PackageManager.PERMISSION_GRANTED; |
| 4503 | } |
| 4504 | // If the target is not exported, then nobody else can get to it. |
| 4505 | if (!exported) { |
| 4506 | Slog.w(TAG, "Permission denied: checkComponentPermission() owningUid=" + owningUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4507 | return PackageManager.PERMISSION_DENIED; |
| 4508 | } |
| 4509 | if (permission == null) { |
| 4510 | return PackageManager.PERMISSION_GRANTED; |
| 4511 | } |
| 4512 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4513 | return AppGlobals.getPackageManager() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4514 | .checkUidPermission(permission, uid); |
| 4515 | } catch (RemoteException e) { |
| 4516 | // Should never happen, but if it does... deny! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4517 | Slog.e(TAG, "PackageManager is dead?!?", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4518 | } |
| 4519 | return PackageManager.PERMISSION_DENIED; |
| 4520 | } |
| 4521 | |
| 4522 | /** |
| 4523 | * As the only public entry point for permissions checking, this method |
| 4524 | * can enforce the semantic that requesting a check on a null global |
| 4525 | * permission is automatically denied. (Internally a null permission |
| 4526 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4527 | * when only uid-based security is needed.) |
| 4528 | * |
| 4529 | * This can be called with or without the global lock held. |
| 4530 | */ |
| 4531 | public int checkPermission(String permission, int pid, int uid) { |
| 4532 | if (permission == null) { |
| 4533 | return PackageManager.PERMISSION_DENIED; |
| 4534 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4535 | return checkComponentPermission(permission, pid, UserId.getAppId(uid), -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4536 | } |
| 4537 | |
| 4538 | /** |
| 4539 | * Binder IPC calls go through the public entry point. |
| 4540 | * This can be called with or without the global lock held. |
| 4541 | */ |
| 4542 | int checkCallingPermission(String permission) { |
| 4543 | return checkPermission(permission, |
| 4544 | Binder.getCallingPid(), |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4545 | UserId.getAppId(Binder.getCallingUid())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | /** |
| 4549 | * This can be called with or without the global lock held. |
| 4550 | */ |
| 4551 | void enforceCallingPermission(String permission, String func) { |
| 4552 | if (checkCallingPermission(permission) |
| 4553 | == PackageManager.PERMISSION_GRANTED) { |
| 4554 | return; |
| 4555 | } |
| 4556 | |
| 4557 | String msg = "Permission Denial: " + func + " from pid=" |
| 4558 | + Binder.getCallingPid() |
| 4559 | + ", uid=" + Binder.getCallingUid() |
| 4560 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4561 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4562 | throw new SecurityException(msg); |
| 4563 | } |
| 4564 | |
| 4565 | private final boolean checkHoldingPermissionsLocked(IPackageManager pm, |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4566 | ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
| 4567 | boolean readPerm = (modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 4568 | boolean writePerm = (modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0; |
| 4569 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4570 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4571 | try { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4572 | // Is the component private from the target uid? |
| 4573 | final boolean prv = !pi.exported && pi.applicationInfo.uid != uid; |
| 4574 | |
| 4575 | // Acceptable if the there is no read permission needed from the |
| 4576 | // target or the target is holding the read permission. |
| 4577 | if (!readPerm) { |
| 4578 | if ((!prv && pi.readPermission == null) || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4579 | (pm.checkUidPermission(pi.readPermission, uid) |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4580 | == PackageManager.PERMISSION_GRANTED)) { |
| 4581 | readPerm = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4582 | } |
| 4583 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4584 | |
| 4585 | // Acceptable if the there is no write permission needed from the |
| 4586 | // target or the target is holding the read permission. |
| 4587 | if (!writePerm) { |
| 4588 | if (!prv && (pi.writePermission == null) || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4589 | (pm.checkUidPermission(pi.writePermission, uid) |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4590 | == PackageManager.PERMISSION_GRANTED)) { |
| 4591 | writePerm = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4592 | } |
| 4593 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4594 | |
| 4595 | // Acceptable if there is a path permission matching the URI that |
| 4596 | // the target holds the permission on. |
| 4597 | PathPermission[] pps = pi.pathPermissions; |
| 4598 | if (pps != null && (!readPerm || !writePerm)) { |
| 4599 | final String path = uri.getPath(); |
| 4600 | int i = pps.length; |
| 4601 | while (i > 0 && (!readPerm || !writePerm)) { |
| 4602 | i--; |
| 4603 | PathPermission pp = pps[i]; |
| 4604 | if (!readPerm) { |
| 4605 | final String pprperm = pp.getReadPermission(); |
| 4606 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 4607 | + pprperm + " for " + pp.getPath() |
| 4608 | + ": match=" + pp.match(path) |
| 4609 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 4610 | if (pprperm != null && pp.match(path) && |
| 4611 | (pm.checkUidPermission(pprperm, uid) |
| 4612 | == PackageManager.PERMISSION_GRANTED)) { |
| 4613 | readPerm = true; |
| 4614 | } |
| 4615 | } |
| 4616 | if (!writePerm) { |
| 4617 | final String ppwperm = pp.getWritePermission(); |
| 4618 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 4619 | + ppwperm + " for " + pp.getPath() |
| 4620 | + ": match=" + pp.match(path) |
| 4621 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 4622 | if (ppwperm != null && pp.match(path) && |
| 4623 | (pm.checkUidPermission(ppwperm, uid) |
| 4624 | == PackageManager.PERMISSION_GRANTED)) { |
| 4625 | writePerm = true; |
| 4626 | } |
| 4627 | } |
| 4628 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4629 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4630 | } catch (RemoteException e) { |
| 4631 | return false; |
| 4632 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4633 | |
| 4634 | return readPerm && writePerm; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 4638 | int modeFlags) { |
| 4639 | // Root gets to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4640 | if (uid == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4641 | return true; |
| 4642 | } |
| 4643 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 4644 | if (perms == null) return false; |
| 4645 | UriPermission perm = perms.get(uri); |
| 4646 | if (perm == null) return false; |
| 4647 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 4648 | } |
| 4649 | |
| 4650 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4651 | enforceNotIsolatedCaller("checkUriPermission"); |
| 4652 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4653 | // Another redirected-binder-call permissions check as in |
| 4654 | // {@link checkComponentPermission}. |
| 4655 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4656 | if (tlsIdentity != null) { |
| 4657 | uid = tlsIdentity.uid; |
| 4658 | pid = tlsIdentity.pid; |
| 4659 | } |
| 4660 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4661 | uid = UserId.getAppId(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4662 | // Our own process gets to do everything. |
| 4663 | if (pid == MY_PID) { |
| 4664 | return PackageManager.PERMISSION_GRANTED; |
| 4665 | } |
| 4666 | synchronized(this) { |
| 4667 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 4668 | ? PackageManager.PERMISSION_GRANTED |
| 4669 | : PackageManager.PERMISSION_DENIED; |
| 4670 | } |
| 4671 | } |
| 4672 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4673 | /** |
| 4674 | * Check if the targetPkg can be granted permission to access uri by |
| 4675 | * the callingUid using the given modeFlags. Throws a security exception |
| 4676 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 4677 | * if the URI permission grant should be performed; returns -1 if it is not |
| 4678 | * needed (for example targetPkg already has permission to access the URI). |
| 4679 | */ |
| 4680 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
| 4681 | Uri uri, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4682 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4683 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4684 | if (modeFlags == 0) { |
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 | } |
| 4687 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4688 | if (targetPkg != null) { |
| 4689 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4690 | "Checking grant " + targetPkg + " permission to " + uri); |
| 4691 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4692 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4693 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4694 | |
| 4695 | // If this is not a content: uri, we can't do anything with it. |
| 4696 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4697 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4698 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4699 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4700 | } |
| 4701 | |
| 4702 | String name = uri.getAuthority(); |
| 4703 | ProviderInfo pi = null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4704 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, |
| 4705 | UserId.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4706 | if (cpr != null) { |
| 4707 | pi = cpr.info; |
| 4708 | } else { |
| 4709 | try { |
| 4710 | pi = pm.resolveContentProvider(name, |
| 4711 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4712 | } catch (RemoteException ex) { |
| 4713 | } |
| 4714 | } |
| 4715 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 4716 | Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4717 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4718 | } |
| 4719 | |
| 4720 | int targetUid; |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4721 | if (targetPkg != null) { |
| 4722 | try { |
| 4723 | targetUid = pm.getPackageUid(targetPkg); |
| 4724 | if (targetUid < 0) { |
| 4725 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4726 | "Can't grant URI permission no uid for: " + targetPkg); |
| 4727 | return -1; |
| 4728 | } |
| 4729 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4730 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4731 | } |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4732 | } else { |
| 4733 | targetUid = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4734 | } |
| 4735 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4736 | if (targetUid >= 0) { |
| 4737 | // First... does the target actually need this permission? |
| 4738 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 4739 | // No need to grant the target this permission. |
| 4740 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4741 | "Target " + targetPkg + " already has full permission to " + uri); |
| 4742 | return -1; |
| 4743 | } |
| 4744 | } else { |
| 4745 | // First... there is no target package, so can anyone access it? |
| 4746 | boolean allowed = pi.exported; |
| 4747 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4748 | if (pi.readPermission != null) { |
| 4749 | allowed = false; |
| 4750 | } |
| 4751 | } |
| 4752 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4753 | if (pi.writePermission != null) { |
| 4754 | allowed = false; |
| 4755 | } |
| 4756 | } |
| 4757 | if (allowed) { |
| 4758 | return -1; |
| 4759 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4760 | } |
| 4761 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4762 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4763 | if (!pi.grantUriPermissions) { |
| 4764 | throw new SecurityException("Provider " + pi.packageName |
| 4765 | + "/" + pi.name |
| 4766 | + " does not allow granting of Uri permissions (uri " |
| 4767 | + uri + ")"); |
| 4768 | } |
| 4769 | if (pi.uriPermissionPatterns != null) { |
| 4770 | final int N = pi.uriPermissionPatterns.length; |
| 4771 | boolean allowed = false; |
| 4772 | for (int i=0; i<N; i++) { |
| 4773 | if (pi.uriPermissionPatterns[i] != null |
| 4774 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 4775 | allowed = true; |
| 4776 | break; |
| 4777 | } |
| 4778 | } |
| 4779 | if (!allowed) { |
| 4780 | throw new SecurityException("Provider " + pi.packageName |
| 4781 | + "/" + pi.name |
| 4782 | + " does not allow granting of permission to path of Uri " |
| 4783 | + uri); |
| 4784 | } |
| 4785 | } |
| 4786 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4787 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4788 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4789 | if (callingUid != Process.myUid()) { |
| 4790 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 4791 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4792 | throw new SecurityException("Uid " + callingUid |
| 4793 | + " does not have permission to uri " + uri); |
| 4794 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4795 | } |
| 4796 | } |
| 4797 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4798 | return targetUid; |
| 4799 | } |
| 4800 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4801 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 4802 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4803 | enforceNotIsolatedCaller("checkGrantUriPermission"); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4804 | synchronized(this) { |
| 4805 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags); |
| 4806 | } |
| 4807 | } |
| 4808 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4809 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 4810 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 4811 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4812 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4813 | if (modeFlags == 0) { |
| 4814 | return; |
| 4815 | } |
| 4816 | |
| 4817 | // 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] | 4818 | // to the uri, and the target doesn't. Let's now give this to |
| 4819 | // the target. |
| 4820 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4821 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4822 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4823 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4824 | HashMap<Uri, UriPermission> targetUris |
| 4825 | = mGrantedUriPermissions.get(targetUid); |
| 4826 | if (targetUris == null) { |
| 4827 | targetUris = new HashMap<Uri, UriPermission>(); |
| 4828 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 4829 | } |
| 4830 | |
| 4831 | UriPermission perm = targetUris.get(uri); |
| 4832 | if (perm == null) { |
| 4833 | perm = new UriPermission(targetUid, uri); |
| 4834 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4835 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4836 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4837 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4838 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4839 | perm.globalModeFlags |= modeFlags; |
Vairavan Srinivasan | 91c12c2 | 2011-01-21 18:26:06 -0800 | [diff] [blame] | 4840 | } else { |
| 4841 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4842 | perm.readOwners.add(owner); |
| 4843 | owner.addReadPermission(perm); |
| 4844 | } |
| 4845 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4846 | perm.writeOwners.add(owner); |
| 4847 | owner.addWritePermission(perm); |
| 4848 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4849 | } |
| 4850 | } |
| 4851 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4852 | void grantUriPermissionLocked(int callingUid, |
| 4853 | String targetPkg, Uri uri, int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4854 | if (targetPkg == null) { |
| 4855 | throw new NullPointerException("targetPkg"); |
| 4856 | } |
| 4857 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4858 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags); |
| 4859 | if (targetUid < 0) { |
| 4860 | return; |
| 4861 | } |
| 4862 | |
| 4863 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 4864 | } |
| 4865 | |
| 4866 | /** |
| 4867 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 4868 | */ |
| 4869 | int checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 4870 | String targetPkg, Intent intent) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4871 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4872 | "Checking URI perm to " + (intent != null ? intent.getData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4873 | + " from " + intent + "; flags=0x" |
| 4874 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 4875 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4876 | if (targetPkg == null) { |
| 4877 | throw new NullPointerException("targetPkg"); |
| 4878 | } |
| 4879 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4880 | if (intent == null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4881 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4882 | } |
| 4883 | Uri data = intent.getData(); |
| 4884 | if (data == null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4885 | return -1; |
| 4886 | } |
| 4887 | return checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 4888 | intent.getFlags()); |
| 4889 | } |
| 4890 | |
| 4891 | /** |
| 4892 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 4893 | */ |
| 4894 | void grantUriPermissionUncheckedFromIntentLocked(int targetUid, |
| 4895 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
| 4896 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, intent.getData(), |
| 4897 | intent.getFlags(), owner); |
| 4898 | } |
| 4899 | |
| 4900 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 4901 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
| 4902 | int targetUid = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, intent); |
| 4903 | if (targetUid < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4904 | return; |
| 4905 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4906 | |
| 4907 | grantUriPermissionUncheckedFromIntentLocked(targetUid, targetPkg, intent, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 4911 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4912 | enforceNotIsolatedCaller("grantUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4913 | synchronized(this) { |
| 4914 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 4915 | if (r == null) { |
| 4916 | throw new SecurityException("Unable to find app for caller " |
| 4917 | + caller |
| 4918 | + " when granting permission to uri " + uri); |
| 4919 | } |
| 4920 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4921 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4922 | } |
| 4923 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 4924 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4925 | } |
| 4926 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4927 | grantUriPermissionLocked(r.uid, targetPkg, uri, modeFlags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4928 | null); |
| 4929 | } |
| 4930 | } |
| 4931 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4932 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4933 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4934 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 4935 | HashMap<Uri, UriPermission> perms |
| 4936 | = mGrantedUriPermissions.get(perm.uid); |
| 4937 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4938 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4939 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4940 | perms.remove(perm.uri); |
| 4941 | if (perms.size() == 0) { |
| 4942 | mGrantedUriPermissions.remove(perm.uid); |
| 4943 | } |
| 4944 | } |
| 4945 | } |
| 4946 | } |
| 4947 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4948 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 4949 | int modeFlags) { |
| 4950 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4951 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4952 | if (modeFlags == 0) { |
| 4953 | return; |
| 4954 | } |
| 4955 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4956 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4957 | "Revoking all granted permissions to " + uri); |
| 4958 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4959 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4960 | |
| 4961 | final String authority = uri.getAuthority(); |
| 4962 | ProviderInfo pi = null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4963 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, |
| 4964 | UserId.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4965 | if (cpr != null) { |
| 4966 | pi = cpr.info; |
| 4967 | } else { |
| 4968 | try { |
| 4969 | pi = pm.resolveContentProvider(authority, |
| 4970 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 4971 | } catch (RemoteException ex) { |
| 4972 | } |
| 4973 | } |
| 4974 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 4975 | 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] | 4976 | return; |
| 4977 | } |
| 4978 | |
| 4979 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4980 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4981 | // Right now, if you are not the original owner of the permission, |
| 4982 | // you are not allowed to revoke it. |
| 4983 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 4984 | throw new SecurityException("Uid " + callingUid |
| 4985 | + " does not have permission to uri " + uri); |
| 4986 | //} |
| 4987 | } |
| 4988 | |
| 4989 | // Go through all of the permissions and remove any that match. |
| 4990 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 4991 | if (SEGMENTS != null) { |
| 4992 | final int NS = SEGMENTS.size(); |
| 4993 | int N = mGrantedUriPermissions.size(); |
| 4994 | for (int i=0; i<N; i++) { |
| 4995 | HashMap<Uri, UriPermission> perms |
| 4996 | = mGrantedUriPermissions.valueAt(i); |
| 4997 | Iterator<UriPermission> it = perms.values().iterator(); |
| 4998 | toploop: |
| 4999 | while (it.hasNext()) { |
| 5000 | UriPermission perm = it.next(); |
| 5001 | Uri targetUri = perm.uri; |
| 5002 | if (!authority.equals(targetUri.getAuthority())) { |
| 5003 | continue; |
| 5004 | } |
| 5005 | List<String> targetSegments = targetUri.getPathSegments(); |
| 5006 | if (targetSegments == null) { |
| 5007 | continue; |
| 5008 | } |
| 5009 | if (targetSegments.size() < NS) { |
| 5010 | continue; |
| 5011 | } |
| 5012 | for (int j=0; j<NS; j++) { |
| 5013 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 5014 | continue toploop; |
| 5015 | } |
| 5016 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5017 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5018 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5019 | perm.clearModes(modeFlags); |
| 5020 | if (perm.modeFlags == 0) { |
| 5021 | it.remove(); |
| 5022 | } |
| 5023 | } |
| 5024 | if (perms.size() == 0) { |
| 5025 | mGrantedUriPermissions.remove( |
| 5026 | mGrantedUriPermissions.keyAt(i)); |
| 5027 | N--; |
| 5028 | i--; |
| 5029 | } |
| 5030 | } |
| 5031 | } |
| 5032 | } |
| 5033 | |
| 5034 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 5035 | int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5036 | enforceNotIsolatedCaller("revokeUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5037 | synchronized(this) { |
| 5038 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5039 | if (r == null) { |
| 5040 | throw new SecurityException("Unable to find app for caller " |
| 5041 | + caller |
| 5042 | + " when revoking permission to uri " + uri); |
| 5043 | } |
| 5044 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5045 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5046 | return; |
| 5047 | } |
| 5048 | |
| 5049 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5050 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5051 | if (modeFlags == 0) { |
| 5052 | return; |
| 5053 | } |
| 5054 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5055 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5056 | |
| 5057 | final String authority = uri.getAuthority(); |
| 5058 | ProviderInfo pi = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5059 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5060 | if (cpr != null) { |
| 5061 | pi = cpr.info; |
| 5062 | } else { |
| 5063 | try { |
| 5064 | pi = pm.resolveContentProvider(authority, |
| 5065 | PackageManager.GET_URI_PERMISSION_PATTERNS); |
| 5066 | } catch (RemoteException ex) { |
| 5067 | } |
| 5068 | } |
| 5069 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5070 | Slog.w(TAG, "No content provider found for permission revoke: " |
| 5071 | + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5072 | return; |
| 5073 | } |
| 5074 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5075 | revokeUriPermissionLocked(r.uid, uri, modeFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5076 | } |
| 5077 | } |
| 5078 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5079 | @Override |
| 5080 | public IBinder newUriPermissionOwner(String name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5081 | enforceNotIsolatedCaller("newUriPermissionOwner"); |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5082 | synchronized(this) { |
| 5083 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 5084 | return owner.getExternalTokenLocked(); |
| 5085 | } |
| 5086 | } |
| 5087 | |
| 5088 | @Override |
| 5089 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 5090 | Uri uri, int modeFlags) { |
| 5091 | synchronized(this) { |
| 5092 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5093 | if (owner == null) { |
| 5094 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5095 | } |
| 5096 | if (fromUid != Binder.getCallingUid()) { |
| 5097 | if (Binder.getCallingUid() != Process.myUid()) { |
| 5098 | // Only system code can grant URI permissions on behalf |
| 5099 | // of other users. |
| 5100 | throw new SecurityException("nice try"); |
| 5101 | } |
| 5102 | } |
| 5103 | if (targetPkg == null) { |
| 5104 | throw new IllegalArgumentException("null target"); |
| 5105 | } |
| 5106 | if (uri == null) { |
| 5107 | throw new IllegalArgumentException("null uri"); |
| 5108 | } |
| 5109 | |
| 5110 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 5111 | } |
| 5112 | } |
| 5113 | |
| 5114 | @Override |
| 5115 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 5116 | synchronized(this) { |
| 5117 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5118 | if (owner == null) { |
| 5119 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5120 | } |
| 5121 | |
| 5122 | if (uri == null) { |
| 5123 | owner.removeUriPermissionsLocked(mode); |
| 5124 | } else { |
| 5125 | owner.removeUriPermissionLocked(uri, mode); |
| 5126 | } |
| 5127 | } |
| 5128 | } |
| 5129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5130 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 5131 | synchronized (this) { |
| 5132 | ProcessRecord app = |
| 5133 | who != null ? getRecordForAppLocked(who) : null; |
| 5134 | if (app == null) return; |
| 5135 | |
| 5136 | Message msg = Message.obtain(); |
| 5137 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 5138 | msg.obj = app; |
| 5139 | msg.arg1 = waiting ? 1 : 0; |
| 5140 | mHandler.sendMessage(msg); |
| 5141 | } |
| 5142 | } |
| 5143 | |
| 5144 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5145 | final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ); |
| 5146 | 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] | 5147 | outInfo.availMem = Process.getFreeMemory(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5148 | outInfo.threshold = homeAppMem; |
| 5149 | outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2)); |
| 5150 | outInfo.hiddenAppThreshold = hiddenAppMem; |
| 5151 | outInfo.secondaryServerThreshold = mProcessList.getMemLevel( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 5152 | ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5153 | outInfo.visibleAppThreshold = mProcessList.getMemLevel( |
| 5154 | ProcessList.VISIBLE_APP_ADJ); |
| 5155 | outInfo.foregroundAppThreshold = mProcessList.getMemLevel( |
| 5156 | ProcessList.FOREGROUND_APP_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | // ========================================================= |
| 5160 | // TASK MANAGEMENT |
| 5161 | // ========================================================= |
| 5162 | |
| 5163 | public List getTasks(int maxNum, int flags, |
| 5164 | IThumbnailReceiver receiver) { |
| 5165 | ArrayList list = new ArrayList(); |
| 5166 | |
| 5167 | PendingThumbnailsRecord pending = null; |
| 5168 | IApplicationThread topThumbnail = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5169 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5170 | |
| 5171 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5172 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5173 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 5174 | + ", receiver=" + receiver); |
| 5175 | |
| 5176 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 5177 | != PackageManager.PERMISSION_GRANTED) { |
| 5178 | if (receiver != null) { |
| 5179 | // If the caller wants to wait for pending thumbnails, |
| 5180 | // it ain't gonna get them. |
| 5181 | try { |
| 5182 | receiver.finished(); |
| 5183 | } catch (RemoteException ex) { |
| 5184 | } |
| 5185 | } |
| 5186 | String msg = "Permission Denial: getTasks() from pid=" |
| 5187 | + Binder.getCallingPid() |
| 5188 | + ", uid=" + Binder.getCallingUid() |
| 5189 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5190 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5191 | throw new SecurityException(msg); |
| 5192 | } |
| 5193 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5194 | int pos = mMainStack.mHistory.size()-1; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5195 | ActivityRecord next = |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5196 | pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5197 | ActivityRecord top = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5198 | TaskRecord curTask = null; |
| 5199 | int numActivities = 0; |
| 5200 | int numRunning = 0; |
| 5201 | while (pos >= 0 && maxNum > 0) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5202 | final ActivityRecord r = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5203 | pos--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5204 | next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5205 | |
| 5206 | // Initialize state for next task if needed. |
| 5207 | if (top == null || |
| 5208 | (top.state == ActivityState.INITIALIZING |
| 5209 | && top.task == r.task)) { |
| 5210 | top = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5211 | curTask = r.task; |
| 5212 | numActivities = numRunning = 0; |
| 5213 | } |
| 5214 | |
| 5215 | // Add 'r' into the current task. |
| 5216 | numActivities++; |
| 5217 | if (r.app != null && r.app.thread != null) { |
| 5218 | numRunning++; |
| 5219 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5220 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5221 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5222 | TAG, r.intent.getComponent().flattenToShortString() |
| 5223 | + ": task=" + r.task); |
| 5224 | |
| 5225 | // If the next one is a different task, generate a new |
| 5226 | // TaskInfo entry for what we have. |
| 5227 | if (next == null || next.task != curTask) { |
| 5228 | ActivityManager.RunningTaskInfo ci |
| 5229 | = new ActivityManager.RunningTaskInfo(); |
| 5230 | ci.id = curTask.taskId; |
| 5231 | ci.baseActivity = r.intent.getComponent(); |
| 5232 | ci.topActivity = top.intent.getComponent(); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5233 | if (top.thumbHolder != null) { |
| 5234 | ci.description = top.thumbHolder.lastDescription; |
| 5235 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5236 | ci.numActivities = numActivities; |
| 5237 | ci.numRunning = numRunning; |
| 5238 | //System.out.println( |
| 5239 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 5240 | if (ci.thumbnail == null && receiver != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5241 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5242 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 5243 | + " app=" + top.app |
| 5244 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 5245 | if (top.state == ActivityState.RESUMED |
| 5246 | || top.state == ActivityState.PAUSING) { |
| 5247 | if (top.idle && top.app != null |
| 5248 | && top.app.thread != null) { |
| 5249 | topRecord = top; |
| 5250 | topThumbnail = top.app.thread; |
| 5251 | } else { |
| 5252 | top.thumbnailNeeded = true; |
| 5253 | } |
| 5254 | } |
| 5255 | if (pending == null) { |
| 5256 | pending = new PendingThumbnailsRecord(receiver); |
| 5257 | } |
| 5258 | pending.pendingRecords.add(top); |
| 5259 | } |
| 5260 | list.add(ci); |
| 5261 | maxNum--; |
| 5262 | top = null; |
| 5263 | } |
| 5264 | } |
| 5265 | |
| 5266 | if (pending != null) { |
| 5267 | mPendingThumbnails.add(pending); |
| 5268 | } |
| 5269 | } |
| 5270 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5271 | 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] | 5272 | |
| 5273 | if (topThumbnail != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5274 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5275 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5276 | topThumbnail.requestThumbnail(topRecord.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5277 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5278 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5279 | sendPendingThumbnail(null, topRecord.appToken, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5280 | } |
| 5281 | } |
| 5282 | |
| 5283 | if (pending == null && receiver != null) { |
| 5284 | // In this case all thumbnails were available and the client |
| 5285 | // is being asked to be told when the remaining ones come in... |
| 5286 | // which is unusually, since the top-most currently running |
| 5287 | // activity should never have a canned thumbnail! Oh well. |
| 5288 | try { |
| 5289 | receiver.finished(); |
| 5290 | } catch (RemoteException ex) { |
| 5291 | } |
| 5292 | } |
| 5293 | |
| 5294 | return list; |
| 5295 | } |
| 5296 | |
| 5297 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
| 5298 | int flags) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5299 | final int callingUid = Binder.getCallingUid(); |
| 5300 | // If it's the system uid asking, then use the current user id. |
| 5301 | // TODO: Make sure that there aren't any other legitimate calls from the system uid that |
| 5302 | // require the entire list. |
| 5303 | final int callingUserId = callingUid == Process.SYSTEM_UID |
| 5304 | ? mCurrentUserId : UserId.getUserId(callingUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5305 | synchronized (this) { |
| 5306 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 5307 | "getRecentTasks()"); |
| 5308 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5309 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5311 | final int N = mRecentTasks.size(); |
| 5312 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 5313 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 5314 | maxNum < N ? maxNum : N); |
| 5315 | for (int i=0; i<N && maxNum > 0; i++) { |
| 5316 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5317 | // Only add calling user's recent tasks |
| 5318 | if (tr.userId != callingUserId) continue; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5319 | // Return the entry if desired by the caller. We always return |
| 5320 | // the first entry, because callers always expect this to be the |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5321 | // foreground app. We may filter others if the caller has |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5322 | // not supplied RECENT_WITH_EXCLUDED and there is some reason |
| 5323 | // we should exclude the entry. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5324 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5325 | if (i == 0 |
| 5326 | || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5327 | || (tr.intent == null) |
| 5328 | || ((tr.intent.getFlags() |
| 5329 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 5330 | ActivityManager.RecentTaskInfo rti |
| 5331 | = new ActivityManager.RecentTaskInfo(); |
| 5332 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5333 | rti.persistentId = tr.taskId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5334 | rti.baseIntent = new Intent( |
| 5335 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5336 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 5337 | rti.description = tr.lastDescription; |
| 5338 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5339 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5340 | // Check whether this activity is currently available. |
| 5341 | try { |
| 5342 | if (rti.origActivity != null) { |
| 5343 | if (pm.getActivityInfo(rti.origActivity, 0) == null) { |
| 5344 | continue; |
| 5345 | } |
| 5346 | } else if (rti.baseIntent != null) { |
| 5347 | if (pm.queryIntentActivities(rti.baseIntent, |
| 5348 | null, 0) == null) { |
| 5349 | continue; |
| 5350 | } |
| 5351 | } |
| 5352 | } catch (RemoteException e) { |
| 5353 | // Will never happen. |
| 5354 | } |
| 5355 | } |
| 5356 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5357 | res.add(rti); |
| 5358 | maxNum--; |
| 5359 | } |
| 5360 | } |
| 5361 | return res; |
| 5362 | } |
| 5363 | } |
| 5364 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5365 | private TaskRecord taskForIdLocked(int id) { |
| 5366 | final int N = mRecentTasks.size(); |
| 5367 | for (int i=0; i<N; i++) { |
| 5368 | TaskRecord tr = mRecentTasks.get(i); |
| 5369 | if (tr.taskId == id) { |
| 5370 | return tr; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5371 | } |
| 5372 | } |
| 5373 | return null; |
| 5374 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5375 | |
| 5376 | public ActivityManager.TaskThumbnails getTaskThumbnails(int id) { |
| 5377 | synchronized (this) { |
| 5378 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5379 | "getTaskThumbnails()"); |
| 5380 | TaskRecord tr = taskForIdLocked(id); |
| 5381 | if (tr != null) { |
| 5382 | return mMainStack.getTaskThumbnailsLocked(tr); |
| 5383 | } |
| 5384 | } |
| 5385 | return null; |
| 5386 | } |
| 5387 | |
| 5388 | public boolean removeSubTask(int taskId, int subTaskIndex) { |
| 5389 | synchronized (this) { |
| 5390 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5391 | "removeSubTask()"); |
| 5392 | long ident = Binder.clearCallingIdentity(); |
| 5393 | try { |
| 5394 | return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex) != null; |
| 5395 | } finally { |
| 5396 | Binder.restoreCallingIdentity(ident); |
| 5397 | } |
| 5398 | } |
| 5399 | } |
| 5400 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5401 | private void cleanUpRemovedTaskLocked(ActivityRecord root, boolean killProcesses) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5402 | TaskRecord tr = root.task; |
| 5403 | Intent baseIntent = new Intent( |
| 5404 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5405 | ComponentName component = baseIntent.getComponent(); |
| 5406 | if (component == null) { |
| 5407 | Slog.w(TAG, "Now component for base intent of task: " + tr); |
| 5408 | return; |
| 5409 | } |
| 5410 | |
| 5411 | // Find any running services associated with this app. |
| 5412 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5413 | for (ServiceRecord sr : mServiceMap.getAllServices(root.userId)) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5414 | if (sr.packageName.equals(component.getPackageName())) { |
| 5415 | services.add(sr); |
| 5416 | } |
| 5417 | } |
| 5418 | |
| 5419 | // Take care of any running services associated with the app. |
| 5420 | for (int i=0; i<services.size(); i++) { |
| 5421 | ServiceRecord sr = services.get(i); |
| 5422 | if (sr.startRequested) { |
| 5423 | if ((sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0) { |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5424 | Slog.i(TAG, "Stopping service " + sr.shortName + ": remove task"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5425 | stopServiceLocked(sr); |
| 5426 | } else { |
| 5427 | sr.pendingStarts.add(new ServiceRecord.StartItem(sr, true, |
| 5428 | sr.makeNextStartId(), baseIntent, -1)); |
| 5429 | if (sr.app != null && sr.app.thread != null) { |
| 5430 | sendServiceArgsLocked(sr, false); |
| 5431 | } |
| 5432 | } |
| 5433 | } |
| 5434 | } |
| 5435 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5436 | if (killProcesses) { |
| 5437 | // Find any running processes associated with this app. |
| 5438 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 5439 | SparseArray<ProcessRecord> appProcs |
| 5440 | = mProcessNames.getMap().get(component.getPackageName()); |
| 5441 | if (appProcs != null) { |
| 5442 | for (int i=0; i<appProcs.size(); i++) { |
| 5443 | procs.add(appProcs.valueAt(i)); |
| 5444 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5445 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5446 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5447 | // Kill the running processes. |
| 5448 | for (int i=0; i<procs.size(); i++) { |
| 5449 | ProcessRecord pr = procs.get(i); |
| 5450 | if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 5451 | Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task"); |
| 5452 | EventLog.writeEvent(EventLogTags.AM_KILL, pr.pid, |
| 5453 | pr.processName, pr.setAdj, "remove task"); |
| 5454 | Process.killProcessQuiet(pr.pid); |
| 5455 | } else { |
| 5456 | pr.waitingToKill = "remove task"; |
| 5457 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5458 | } |
| 5459 | } |
| 5460 | } |
| 5461 | |
| 5462 | public boolean removeTask(int taskId, int flags) { |
| 5463 | synchronized (this) { |
| 5464 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5465 | "removeTask()"); |
| 5466 | long ident = Binder.clearCallingIdentity(); |
| 5467 | try { |
| 5468 | ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1); |
| 5469 | if (r != null) { |
| 5470 | mRecentTasks.remove(r.task); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5471 | cleanUpRemovedTaskLocked(r, |
| 5472 | (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5473 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5474 | } else { |
| 5475 | TaskRecord tr = null; |
| 5476 | int i=0; |
| 5477 | while (i < mRecentTasks.size()) { |
| 5478 | TaskRecord t = mRecentTasks.get(i); |
| 5479 | if (t.taskId == taskId) { |
| 5480 | tr = t; |
| 5481 | break; |
| 5482 | } |
| 5483 | i++; |
| 5484 | } |
| 5485 | if (tr != null) { |
| 5486 | if (tr.numActivities <= 0) { |
| 5487 | // Caller is just removing a recent task that is |
| 5488 | // not actively running. That is easy! |
| 5489 | mRecentTasks.remove(i); |
| 5490 | } else { |
| 5491 | Slog.w(TAG, "removeTask: task " + taskId |
| 5492 | + " does not have activities to remove, " |
| 5493 | + " but numActivities=" + tr.numActivities |
| 5494 | + ": " + tr); |
| 5495 | } |
| 5496 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5497 | } |
| 5498 | } finally { |
| 5499 | Binder.restoreCallingIdentity(ident); |
| 5500 | } |
| 5501 | } |
| 5502 | return false; |
| 5503 | } |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5504 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5505 | private final int findAffinityTaskTopLocked(int startIndex, String affinity) { |
| 5506 | int j; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5507 | TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5508 | TaskRecord jt = startTask; |
| 5509 | |
| 5510 | // First look backwards |
| 5511 | for (j=startIndex-1; j>=0; j--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5512 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5513 | if (r.task != jt) { |
| 5514 | jt = r.task; |
| 5515 | if (affinity.equals(jt.affinity)) { |
| 5516 | return j; |
| 5517 | } |
| 5518 | } |
| 5519 | } |
| 5520 | |
| 5521 | // Now look forwards |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5522 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5523 | jt = startTask; |
| 5524 | for (j=startIndex+1; j<N; j++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5525 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5526 | if (r.task != jt) { |
| 5527 | if (affinity.equals(jt.affinity)) { |
| 5528 | return j; |
| 5529 | } |
| 5530 | jt = r.task; |
| 5531 | } |
| 5532 | } |
| 5533 | |
| 5534 | // Might it be at the top? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5535 | 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] | 5536 | return N-1; |
| 5537 | } |
| 5538 | |
| 5539 | return -1; |
| 5540 | } |
| 5541 | |
| 5542 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5543 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5544 | */ |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5545 | public void moveTaskToFront(int task, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5546 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5547 | "moveTaskToFront()"); |
| 5548 | |
| 5549 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5550 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5551 | Binder.getCallingUid(), "Task to front")) { |
| 5552 | return; |
| 5553 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5554 | final long origId = Binder.clearCallingIdentity(); |
| 5555 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5556 | TaskRecord tr = taskForIdLocked(task); |
| 5557 | if (tr != null) { |
| 5558 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5559 | mMainStack.mUserLeaving = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5560 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5561 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5562 | // Caller wants the home activity moved with it. To accomplish this, |
| 5563 | // we'll just move the home task to the top first. |
| 5564 | mMainStack.moveHomeToFrontLocked(); |
| 5565 | } |
| 5566 | mMainStack.moveTaskToFrontLocked(tr, null); |
| 5567 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5568 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5569 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 5570 | ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5571 | if (hr.task.taskId == task) { |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5572 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5573 | mMainStack.mUserLeaving = true; |
| 5574 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5575 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5576 | // Caller wants the home activity moved with it. To accomplish this, |
| 5577 | // we'll just move the home task to the top first. |
| 5578 | mMainStack.moveHomeToFrontLocked(); |
| 5579 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5580 | mMainStack.moveTaskToFrontLocked(hr.task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5581 | return; |
| 5582 | } |
| 5583 | } |
| 5584 | } finally { |
| 5585 | Binder.restoreCallingIdentity(origId); |
| 5586 | } |
| 5587 | } |
| 5588 | } |
| 5589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5590 | public void moveTaskToBack(int task) { |
| 5591 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5592 | "moveTaskToBack()"); |
| 5593 | |
| 5594 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5595 | if (mMainStack.mResumedActivity != null |
| 5596 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5597 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5598 | Binder.getCallingUid(), "Task to back")) { |
| 5599 | return; |
| 5600 | } |
| 5601 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5602 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5603 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5604 | Binder.restoreCallingIdentity(origId); |
| 5605 | } |
| 5606 | } |
| 5607 | |
| 5608 | /** |
| 5609 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 5610 | * of the history stack. The activity's order within the task is unchanged. |
| 5611 | * |
| 5612 | * @param token A reference to the activity we wish to move |
| 5613 | * @param nonRoot If false then this only works if the activity is the root |
| 5614 | * of a task; if true it will work for any activity in a task. |
| 5615 | * @return Returns true if the move completed, false if not. |
| 5616 | */ |
| 5617 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5618 | enforceNotIsolatedCaller("moveActivityTaskToBack"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5619 | synchronized(this) { |
| 5620 | final long origId = Binder.clearCallingIdentity(); |
| 5621 | int taskId = getTaskForActivityLocked(token, !nonRoot); |
| 5622 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5623 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5624 | } |
| 5625 | Binder.restoreCallingIdentity(origId); |
| 5626 | } |
| 5627 | return false; |
| 5628 | } |
| 5629 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5630 | public void moveTaskBackwards(int task) { |
| 5631 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5632 | "moveTaskBackwards()"); |
| 5633 | |
| 5634 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5635 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5636 | Binder.getCallingUid(), "Task backwards")) { |
| 5637 | return; |
| 5638 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5639 | final long origId = Binder.clearCallingIdentity(); |
| 5640 | moveTaskBackwardsLocked(task); |
| 5641 | Binder.restoreCallingIdentity(origId); |
| 5642 | } |
| 5643 | } |
| 5644 | |
| 5645 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5646 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 5650 | synchronized(this) { |
| 5651 | return getTaskForActivityLocked(token, onlyRoot); |
| 5652 | } |
| 5653 | } |
| 5654 | |
| 5655 | int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5656 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5657 | TaskRecord lastTask = null; |
| 5658 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5659 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5660 | if (r.appToken == token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5661 | if (!onlyRoot || lastTask != r.task) { |
| 5662 | return r.task.taskId; |
| 5663 | } |
| 5664 | return -1; |
| 5665 | } |
| 5666 | lastTask = r.task; |
| 5667 | } |
| 5668 | |
| 5669 | return -1; |
| 5670 | } |
| 5671 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5672 | // ========================================================= |
| 5673 | // THUMBNAILS |
| 5674 | // ========================================================= |
| 5675 | |
| 5676 | public void reportThumbnail(IBinder token, |
| 5677 | Bitmap thumbnail, CharSequence description) { |
| 5678 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 5679 | final long origId = Binder.clearCallingIdentity(); |
| 5680 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 5681 | Binder.restoreCallingIdentity(origId); |
| 5682 | } |
| 5683 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5684 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5685 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 5686 | TaskRecord task = null; |
| 5687 | ArrayList receivers = null; |
| 5688 | |
| 5689 | //System.out.println("Send pending thumbnail: " + r); |
| 5690 | |
| 5691 | synchronized(this) { |
| 5692 | if (r == null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 5693 | r = mMainStack.isInStackLocked(token); |
| 5694 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5695 | return; |
| 5696 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5697 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5698 | if (thumbnail == null && r.thumbHolder != null) { |
| 5699 | thumbnail = r.thumbHolder.lastThumbnail; |
| 5700 | description = r.thumbHolder.lastDescription; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5701 | } |
| 5702 | if (thumbnail == null && !always) { |
| 5703 | // If there is no thumbnail, and this entry is not actually |
| 5704 | // going away, then abort for now and pick up the next |
| 5705 | // thumbnail we get. |
| 5706 | return; |
| 5707 | } |
| 5708 | task = r.task; |
| 5709 | |
| 5710 | int N = mPendingThumbnails.size(); |
| 5711 | int i=0; |
| 5712 | while (i<N) { |
| 5713 | PendingThumbnailsRecord pr = |
| 5714 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 5715 | //System.out.println("Looking in " + pr.pendingRecords); |
| 5716 | if (pr.pendingRecords.remove(r)) { |
| 5717 | if (receivers == null) { |
| 5718 | receivers = new ArrayList(); |
| 5719 | } |
| 5720 | receivers.add(pr); |
| 5721 | if (pr.pendingRecords.size() == 0) { |
| 5722 | pr.finished = true; |
| 5723 | mPendingThumbnails.remove(i); |
| 5724 | N--; |
| 5725 | continue; |
| 5726 | } |
| 5727 | } |
| 5728 | i++; |
| 5729 | } |
| 5730 | } |
| 5731 | |
| 5732 | if (receivers != null) { |
| 5733 | final int N = receivers.size(); |
| 5734 | for (int i=0; i<N; i++) { |
| 5735 | try { |
| 5736 | PendingThumbnailsRecord pr = |
| 5737 | (PendingThumbnailsRecord)receivers.get(i); |
| 5738 | pr.receiver.newThumbnail( |
| 5739 | task != null ? task.taskId : -1, thumbnail, description); |
| 5740 | if (pr.finished) { |
| 5741 | pr.receiver.finished(); |
| 5742 | } |
| 5743 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5744 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5745 | } |
| 5746 | } |
| 5747 | } |
| 5748 | } |
| 5749 | |
| 5750 | // ========================================================= |
| 5751 | // CONTENT PROVIDERS |
| 5752 | // ========================================================= |
| 5753 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 5754 | private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) { |
| 5755 | List<ProviderInfo> providers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5756 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5757 | providers = AppGlobals.getPackageManager(). |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5758 | queryContentProviders(app.processName, app.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5759 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5760 | } catch (RemoteException ex) { |
| 5761 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5762 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5763 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid); |
| 5764 | int userId = app.userId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5765 | if (providers != null) { |
| 5766 | final int N = providers.size(); |
| 5767 | for (int i=0; i<N; i++) { |
| 5768 | ProviderInfo cpi = |
| 5769 | (ProviderInfo)providers.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5770 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 5771 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5772 | ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5773 | if (cpr == null) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5774 | cpr = new ContentProviderRecord(cpi, app.info, comp); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5775 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5776 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5777 | if (DEBUG_MU) |
| 5778 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5779 | app.pubProviders.put(cpi.name, cpr); |
| 5780 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 5781 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5782 | } |
| 5783 | } |
| 5784 | return providers; |
| 5785 | } |
| 5786 | |
| 5787 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5788 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5789 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5790 | final int callingUid = (r != null) ? r.uid : Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5791 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5792 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5793 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5794 | return null; |
| 5795 | } |
| 5796 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5797 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5798 | == PackageManager.PERMISSION_GRANTED) { |
| 5799 | return null; |
| 5800 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5801 | |
| 5802 | PathPermission[] pps = cpi.pathPermissions; |
| 5803 | if (pps != null) { |
| 5804 | int i = pps.length; |
| 5805 | while (i > 0) { |
| 5806 | i--; |
| 5807 | PathPermission pp = pps[i]; |
| 5808 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5809 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5810 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5811 | return null; |
| 5812 | } |
| 5813 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5814 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 5815 | == PackageManager.PERMISSION_GRANTED) { |
| 5816 | return null; |
| 5817 | } |
| 5818 | } |
| 5819 | } |
| 5820 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5821 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 5822 | if (perms != null) { |
| 5823 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 5824 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 5825 | return null; |
| 5826 | } |
| 5827 | } |
| 5828 | } |
| 5829 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 5830 | String msg; |
| 5831 | if (!cpi.exported) { |
| 5832 | msg = "Permission Denial: opening provider " + cpi.name |
| 5833 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 5834 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 5835 | + cpi.applicationInfo.uid; |
| 5836 | } else { |
| 5837 | msg = "Permission Denial: opening provider " + cpi.name |
| 5838 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 5839 | + ", uid=" + callingUid + ") requires " |
| 5840 | + cpi.readPermission + " or " + cpi.writePermission; |
| 5841 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5842 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5843 | return msg; |
| 5844 | } |
| 5845 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5846 | boolean incProviderCount(ProcessRecord r, ContentProviderRecord cpr) { |
| 5847 | if (r != null) { |
| 5848 | Integer cnt = r.conProviders.get(cpr); |
| 5849 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 5850 | "Adding provider requested by " |
| 5851 | + r.processName + " from process " |
| 5852 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 5853 | + " cnt=" + (cnt == null ? 1 : cnt)); |
| 5854 | if (cnt == null) { |
| 5855 | cpr.clients.add(r); |
| 5856 | r.conProviders.put(cpr, new Integer(1)); |
| 5857 | return true; |
| 5858 | } else { |
| 5859 | r.conProviders.put(cpr, new Integer(cnt.intValue()+1)); |
| 5860 | } |
| 5861 | } else { |
| 5862 | cpr.externals++; |
| 5863 | } |
| 5864 | return false; |
| 5865 | } |
| 5866 | |
| 5867 | boolean decProviderCount(ProcessRecord r, ContentProviderRecord cpr) { |
| 5868 | if (r != null) { |
| 5869 | Integer cnt = r.conProviders.get(cpr); |
| 5870 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 5871 | "Removing provider requested by " |
| 5872 | + r.processName + " from process " |
| 5873 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 5874 | + " cnt=" + cnt); |
| 5875 | if (cnt == null || cnt.intValue() <= 1) { |
| 5876 | cpr.clients.remove(r); |
| 5877 | r.conProviders.remove(cpr); |
| 5878 | return true; |
| 5879 | } else { |
| 5880 | r.conProviders.put(cpr, new Integer(cnt.intValue()-1)); |
| 5881 | } |
| 5882 | } else { |
| 5883 | cpr.externals++; |
| 5884 | } |
| 5885 | return false; |
| 5886 | } |
| 5887 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5888 | private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller, |
| 5889 | String name) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5890 | ContentProviderRecord cpr; |
| 5891 | ProviderInfo cpi = null; |
| 5892 | |
| 5893 | synchronized(this) { |
| 5894 | ProcessRecord r = null; |
| 5895 | if (caller != null) { |
| 5896 | r = getRecordForAppLocked(caller); |
| 5897 | if (r == null) { |
| 5898 | throw new SecurityException( |
| 5899 | "Unable to find app for caller " + caller |
| 5900 | + " (pid=" + Binder.getCallingPid() |
| 5901 | + ") when getting content provider " + name); |
| 5902 | } |
| 5903 | } |
| 5904 | |
| 5905 | // First check if this content provider has been published... |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5906 | int userId = UserId.getUserId(r != null ? r.uid : Binder.getCallingUid()); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5907 | cpr = mProviderMap.getProviderByName(name, userId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5908 | boolean providerRunning = cpr != null; |
| 5909 | if (providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5910 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5911 | String msg; |
| 5912 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 5913 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5914 | } |
| 5915 | |
| 5916 | if (r != null && cpr.canRunHere(r)) { |
| 5917 | // This provider has been published or is in the process |
| 5918 | // of being published... but it is also allowed to run |
| 5919 | // in the caller's process, so don't make a connection |
| 5920 | // and just let the caller instantiate its own instance. |
| 5921 | if (cpr.provider != null) { |
| 5922 | // don't give caller the provider object, it needs |
| 5923 | // to make its own. |
| 5924 | cpr = new ContentProviderRecord(cpr); |
| 5925 | } |
| 5926 | return cpr; |
| 5927 | } |
| 5928 | |
| 5929 | final long origId = Binder.clearCallingIdentity(); |
| 5930 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 5931 | // 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] | 5932 | // return it right away. |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5933 | final boolean countChanged = incProviderCount(r, cpr); |
| 5934 | if (countChanged) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5935 | if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 5936 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 5937 | // make sure to count it as being accessed and thus |
| 5938 | // back up on the LRU list. This is good because |
| 5939 | // content providers are often expensive to start. |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5940 | updateLruProcessLocked(cpr.proc, false, true); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 5941 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 5942 | } |
| 5943 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5944 | if (cpr.proc != null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5945 | if (false) { |
| 5946 | if (cpr.name.flattenToShortString().equals( |
| 5947 | "com.android.providers.calendar/.CalendarProvider2")) { |
| 5948 | Slog.v(TAG, "****************** KILLING " |
| 5949 | + cpr.name.flattenToShortString()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5950 | Process.killProcess(cpr.proc.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5951 | } |
| 5952 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5953 | boolean success = updateOomAdjLocked(cpr.proc); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5954 | if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success); |
| 5955 | // NOTE: there is still a race here where a signal could be |
| 5956 | // pending on the process even though we managed to update its |
| 5957 | // adj level. Not sure what to do about this, but at least |
| 5958 | // the race is now smaller. |
| 5959 | if (!success) { |
| 5960 | // Uh oh... it looks like the provider's process |
| 5961 | // has been killed on us. We need to wait for a new |
| 5962 | // process to be started, and make sure its death |
| 5963 | // doesn't kill our process. |
| 5964 | Slog.i(TAG, |
| 5965 | "Existing provider " + cpr.name.flattenToShortString() |
| 5966 | + " is crashing; detaching " + r); |
| 5967 | boolean lastRef = decProviderCount(r, cpr); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 5968 | appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5969 | if (!lastRef) { |
| 5970 | // This wasn't the last ref our process had on |
| 5971 | // the provider... we have now been killed, bail. |
| 5972 | return null; |
| 5973 | } |
| 5974 | providerRunning = false; |
| 5975 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5976 | } |
| 5977 | |
| 5978 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5979 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5980 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 5981 | if (!providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5982 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5983 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 5984 | resolveContentProvider(name, |
| 5985 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5986 | } catch (RemoteException ex) { |
| 5987 | } |
| 5988 | if (cpi == null) { |
| 5989 | return null; |
| 5990 | } |
| 5991 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5992 | cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, |
| 5993 | Binder.getOrigCallingUser()); |
| 5994 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5995 | String msg; |
| 5996 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 5997 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5998 | } |
| 5999 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 6000 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6001 | && !cpi.processName.equals("system")) { |
| 6002 | // If this content provider does not run in the system |
| 6003 | // process, and the system is not yet ready to run other |
| 6004 | // processes, then fail fast instead of hanging. |
| 6005 | throw new IllegalArgumentException( |
| 6006 | "Attempt to launch content provider before system ready"); |
| 6007 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6008 | |
| 6009 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6010 | cpr = mProviderMap.getProviderByClass(comp, Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6011 | final boolean firstClass = cpr == null; |
| 6012 | if (firstClass) { |
| 6013 | try { |
| 6014 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6015 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6016 | getApplicationInfo( |
| 6017 | cpi.applicationInfo.packageName, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6018 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6019 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6020 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6021 | + cpi.name); |
| 6022 | return null; |
| 6023 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6024 | ai = getAppInfoForUser(ai, Binder.getOrigCallingUser()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6025 | cpr = new ContentProviderRecord(cpi, ai, comp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6026 | } catch (RemoteException ex) { |
| 6027 | // pm is in same process, this will never happen. |
| 6028 | } |
| 6029 | } |
| 6030 | |
| 6031 | if (r != null && cpr.canRunHere(r)) { |
| 6032 | // If this is a multiprocess provider, then just return its |
| 6033 | // info and allow the caller to instantiate it. Only do |
| 6034 | // this if the provider is the same user as the caller's |
| 6035 | // process, or can run as root (so can be in any process). |
| 6036 | return cpr; |
| 6037 | } |
| 6038 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6039 | if (DEBUG_PROVIDER) { |
| 6040 | RuntimeException e = new RuntimeException("here"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6041 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6042 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6043 | } |
| 6044 | |
| 6045 | // This is single process, and our app is now connecting to it. |
| 6046 | // See if we are already in the process of launching this |
| 6047 | // provider. |
| 6048 | final int N = mLaunchingProviders.size(); |
| 6049 | int i; |
| 6050 | for (i=0; i<N; i++) { |
| 6051 | if (mLaunchingProviders.get(i) == cpr) { |
| 6052 | break; |
| 6053 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6054 | } |
| 6055 | |
| 6056 | // If the provider is not already being launched, then get it |
| 6057 | // started. |
| 6058 | if (i >= N) { |
| 6059 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6060 | |
| 6061 | try { |
| 6062 | // Content provider is now in use, its package can't be stopped. |
| 6063 | try { |
| 6064 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 6065 | cpr.appInfo.packageName, false); |
| 6066 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6067 | } catch (IllegalArgumentException e) { |
| 6068 | Slog.w(TAG, "Failed trying to unstop package " |
| 6069 | + cpr.appInfo.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6070 | } |
| 6071 | |
| 6072 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 6073 | cpr.appInfo, false, 0, "content provider", |
| 6074 | new ComponentName(cpi.applicationInfo.packageName, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6075 | cpi.name), false, false); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6076 | if (proc == null) { |
| 6077 | Slog.w(TAG, "Unable to launch app " |
| 6078 | + cpi.applicationInfo.packageName + "/" |
| 6079 | + cpi.applicationInfo.uid + " for provider " |
| 6080 | + name + ": process is bad"); |
| 6081 | return null; |
| 6082 | } |
| 6083 | cpr.launchingApp = proc; |
| 6084 | mLaunchingProviders.add(cpr); |
| 6085 | } finally { |
| 6086 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6087 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6088 | } |
| 6089 | |
| 6090 | // Make sure the provider is published (the same provider class |
| 6091 | // may be published under multiple names). |
| 6092 | if (firstClass) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6093 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6094 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6095 | mProviderMap.putProviderByName(name, cpr); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6096 | incProviderCount(r, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6097 | } |
| 6098 | } |
| 6099 | |
| 6100 | // Wait for the provider to be published... |
| 6101 | synchronized (cpr) { |
| 6102 | while (cpr.provider == null) { |
| 6103 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6104 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6105 | + cpi.applicationInfo.packageName + "/" |
| 6106 | + cpi.applicationInfo.uid + " for provider " |
| 6107 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6108 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6109 | cpi.applicationInfo.packageName, |
| 6110 | cpi.applicationInfo.uid, name); |
| 6111 | return null; |
| 6112 | } |
| 6113 | try { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6114 | if (DEBUG_MU) { |
| 6115 | Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp=" |
| 6116 | + cpr.launchingApp); |
| 6117 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6118 | cpr.wait(); |
| 6119 | } catch (InterruptedException ex) { |
| 6120 | } |
| 6121 | } |
| 6122 | } |
| 6123 | return cpr; |
| 6124 | } |
| 6125 | |
| 6126 | public final ContentProviderHolder getContentProvider( |
| 6127 | IApplicationThread caller, String name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6128 | enforceNotIsolatedCaller("getContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6129 | if (caller == null) { |
| 6130 | String msg = "null IApplicationThread when getting content provider " |
| 6131 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6132 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6133 | throw new SecurityException(msg); |
| 6134 | } |
| 6135 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6136 | ContentProviderHolder contentProvider = getContentProviderImpl(caller, name); |
| 6137 | return contentProvider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6138 | } |
| 6139 | |
| 6140 | private ContentProviderHolder getContentProviderExternal(String name) { |
| 6141 | return getContentProviderImpl(null, name); |
| 6142 | } |
| 6143 | |
| 6144 | /** |
| 6145 | * Drop a content provider from a ProcessRecord's bookkeeping |
| 6146 | * @param cpr |
| 6147 | */ |
| 6148 | public void removeContentProvider(IApplicationThread caller, String name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6149 | enforceNotIsolatedCaller("removeContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6150 | synchronized (this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6151 | int userId = UserId.getUserId(Binder.getCallingUid()); |
| 6152 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6153 | if(cpr == null) { |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6154 | // remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6155 | if (DEBUG_PROVIDER) Slog.v(TAG, name + |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6156 | " provider not found in providers list"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6157 | return; |
| 6158 | } |
| 6159 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 6160 | if (r == null) { |
| 6161 | throw new SecurityException( |
| 6162 | "Unable to find app for caller " + caller + |
| 6163 | " when removing content provider " + name); |
| 6164 | } |
| 6165 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6166 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6167 | ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, userId); |
| 6168 | if (DEBUG_PROVIDER) Slog.v(TAG, "Removing provider requested by " |
| 6169 | + r.info.processName + " from process " |
| 6170 | + localCpr.appInfo.processName); |
| 6171 | if (localCpr.launchingApp == r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6172 | //should not happen. taken care of as a local provider |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6173 | Slog.w(TAG, "removeContentProvider called on local provider: " |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6174 | + cpr.info.name + " in process " + r.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6175 | return; |
| 6176 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6177 | if (decProviderCount(r, localCpr)) { |
| 6178 | updateOomAdjLocked(); |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 6179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6180 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6181 | } |
| 6182 | } |
| 6183 | |
| 6184 | private void removeContentProviderExternal(String name) { |
| 6185 | synchronized (this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6186 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, |
| 6187 | Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6188 | if(cpr == null) { |
| 6189 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6190 | 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] | 6191 | return; |
| 6192 | } |
| 6193 | |
| 6194 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6195 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6196 | ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, |
| 6197 | Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6198 | localCpr.externals--; |
| 6199 | if (localCpr.externals < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6200 | Slog.e(TAG, "Externals < 0 for content provider " + localCpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6201 | } |
| 6202 | updateOomAdjLocked(); |
| 6203 | } |
| 6204 | } |
| 6205 | |
| 6206 | public final void publishContentProviders(IApplicationThread caller, |
| 6207 | List<ContentProviderHolder> providers) { |
| 6208 | if (providers == null) { |
| 6209 | return; |
| 6210 | } |
| 6211 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6212 | enforceNotIsolatedCaller("publishContentProviders"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6213 | synchronized(this) { |
| 6214 | final ProcessRecord r = getRecordForAppLocked(caller); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6215 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6216 | Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6217 | if (r == null) { |
| 6218 | throw new SecurityException( |
| 6219 | "Unable to find app for caller " + caller |
| 6220 | + " (pid=" + Binder.getCallingPid() |
| 6221 | + ") when publishing content providers"); |
| 6222 | } |
| 6223 | |
| 6224 | final long origId = Binder.clearCallingIdentity(); |
| 6225 | |
| 6226 | final int N = providers.size(); |
| 6227 | for (int i=0; i<N; i++) { |
| 6228 | ContentProviderHolder src = providers.get(i); |
| 6229 | if (src == null || src.info == null || src.provider == null) { |
| 6230 | continue; |
| 6231 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6232 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6233 | if (DEBUG_MU) |
| 6234 | Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6235 | if (dst != null) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6236 | ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6237 | mProviderMap.putProviderByClass(comp, dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6238 | String names[] = dst.info.authority.split(";"); |
| 6239 | for (int j = 0; j < names.length; j++) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6240 | mProviderMap.putProviderByName(names[j], dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6241 | } |
| 6242 | |
| 6243 | int NL = mLaunchingProviders.size(); |
| 6244 | int j; |
| 6245 | for (j=0; j<NL; j++) { |
| 6246 | if (mLaunchingProviders.get(j) == dst) { |
| 6247 | mLaunchingProviders.remove(j); |
| 6248 | j--; |
| 6249 | NL--; |
| 6250 | } |
| 6251 | } |
| 6252 | synchronized (dst) { |
| 6253 | dst.provider = src.provider; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6254 | dst.proc = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6255 | dst.notifyAll(); |
| 6256 | } |
| 6257 | updateOomAdjLocked(r); |
| 6258 | } |
| 6259 | } |
| 6260 | |
| 6261 | Binder.restoreCallingIdentity(origId); |
| 6262 | } |
| 6263 | } |
| 6264 | |
| 6265 | public static final void installSystemProviders() { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6266 | List<ProviderInfo> providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6267 | synchronized (mSelf) { |
| 6268 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 6269 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 6270 | if (providers != null) { |
| 6271 | for (int i=providers.size()-1; i>=0; i--) { |
| 6272 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 6273 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6274 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 6275 | + ": not system .apk"); |
| 6276 | providers.remove(i); |
| 6277 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6278 | } |
| 6279 | } |
| 6280 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6281 | if (providers != null) { |
| 6282 | mSystemThread.installSystemProviders(providers); |
| 6283 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 6284 | |
| 6285 | mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 6286 | |
| 6287 | mSelf.mUsageStatsService.monitorPackages(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6288 | } |
| 6289 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6290 | /** |
| 6291 | * Allows app to retrieve the MIME type of a URI without having permission |
| 6292 | * to access its content provider. |
| 6293 | * |
| 6294 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 6295 | * |
| 6296 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 6297 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 6298 | */ |
| 6299 | public String getProviderMimeType(Uri uri) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6300 | enforceNotIsolatedCaller("getProviderMimeType"); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6301 | final String name = uri.getAuthority(); |
| 6302 | final long ident = Binder.clearCallingIdentity(); |
| 6303 | ContentProviderHolder holder = null; |
| 6304 | |
| 6305 | try { |
| 6306 | holder = getContentProviderExternal(name); |
| 6307 | if (holder != null) { |
| 6308 | return holder.provider.getType(uri); |
| 6309 | } |
| 6310 | } catch (RemoteException e) { |
| 6311 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 6312 | return null; |
| 6313 | } finally { |
| 6314 | if (holder != null) { |
| 6315 | removeContentProviderExternal(name); |
| 6316 | } |
| 6317 | Binder.restoreCallingIdentity(ident); |
| 6318 | } |
| 6319 | |
| 6320 | return null; |
| 6321 | } |
| 6322 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6323 | // ========================================================= |
| 6324 | // GLOBAL MANAGEMENT |
| 6325 | // ========================================================= |
| 6326 | |
| 6327 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6328 | ApplicationInfo info, String customProcess, boolean isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6329 | String proc = customProcess != null ? customProcess : info.processName; |
| 6330 | BatteryStatsImpl.Uid.Proc ps = null; |
| 6331 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6332 | int uid = info.uid; |
| 6333 | if (isolated) { |
| 6334 | int userId = UserId.getUserId(uid); |
| 6335 | int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1; |
| 6336 | uid = 0; |
| 6337 | while (true) { |
| 6338 | if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID |
| 6339 | || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) { |
| 6340 | mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID; |
| 6341 | } |
| 6342 | uid = UserId.getUid(userId, mNextIsolatedProcessUid); |
| 6343 | mNextIsolatedProcessUid++; |
| 6344 | if (mIsolatedProcesses.indexOfKey(uid) < 0) { |
| 6345 | // No process for this uid, use it. |
| 6346 | break; |
| 6347 | } |
| 6348 | stepsLeft--; |
| 6349 | if (stepsLeft <= 0) { |
| 6350 | return null; |
| 6351 | } |
| 6352 | } |
| 6353 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6354 | synchronized (stats) { |
| 6355 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 6356 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6357 | return new ProcessRecord(ps, thread, info, proc, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6358 | } |
| 6359 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6360 | final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated) { |
| 6361 | ProcessRecord app; |
| 6362 | if (!isolated) { |
| 6363 | app = getProcessRecordLocked(info.processName, info.uid); |
| 6364 | } else { |
| 6365 | app = null; |
| 6366 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6367 | |
| 6368 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6369 | app = newProcessRecordLocked(null, info, null, isolated); |
| 6370 | mProcessNames.put(info.processName, app.uid, app); |
| 6371 | if (isolated) { |
| 6372 | mIsolatedProcesses.put(app.uid, app); |
| 6373 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 6374 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6375 | } |
| 6376 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6377 | // This package really, really can not be stopped. |
| 6378 | try { |
| 6379 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 6380 | info.packageName, false); |
| 6381 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6382 | } catch (IllegalArgumentException e) { |
| 6383 | Slog.w(TAG, "Failed trying to unstop package " |
| 6384 | + info.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6385 | } |
| 6386 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6387 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 6388 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 6389 | app.persistent = true; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 6390 | app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6391 | } |
| 6392 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 6393 | mPersistentStartingProcesses.add(app); |
| 6394 | startProcessLocked(app, "added application", app.processName); |
| 6395 | } |
| 6396 | |
| 6397 | return app; |
| 6398 | } |
| 6399 | |
| 6400 | public void unhandledBack() { |
| 6401 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 6402 | "unhandledBack()"); |
| 6403 | |
| 6404 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6405 | int count = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6406 | if (DEBUG_SWITCH) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6407 | TAG, "Performing unhandledBack(): stack size = " + count); |
| 6408 | if (count > 1) { |
| 6409 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6410 | mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6411 | count-1, Activity.RESULT_CANCELED, null, "unhandled-back"); |
| 6412 | Binder.restoreCallingIdentity(origId); |
| 6413 | } |
| 6414 | } |
| 6415 | } |
| 6416 | |
| 6417 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6418 | enforceNotIsolatedCaller("openContentUri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6419 | String name = uri.getAuthority(); |
| 6420 | ContentProviderHolder cph = getContentProviderExternal(name); |
| 6421 | ParcelFileDescriptor pfd = null; |
| 6422 | if (cph != null) { |
| 6423 | // We record the binder invoker's uid in thread-local storage before |
| 6424 | // going to the content provider to open the file. Later, in the code |
| 6425 | // that handles all permissions checks, we look for this uid and use |
| 6426 | // that rather than the Activity Manager's own uid. The effect is that |
| 6427 | // we do the check against the caller's permissions even though it looks |
| 6428 | // to the content provider like the Activity Manager itself is making |
| 6429 | // the request. |
| 6430 | sCallerIdentity.set(new Identity( |
| 6431 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 6432 | try { |
| 6433 | pfd = cph.provider.openFile(uri, "r"); |
| 6434 | } catch (FileNotFoundException e) { |
| 6435 | // do nothing; pfd will be returned null |
| 6436 | } finally { |
| 6437 | // Ensure that whatever happens, we clean up the identity state |
| 6438 | sCallerIdentity.remove(); |
| 6439 | } |
| 6440 | |
| 6441 | // We've got the fd now, so we're done with the provider. |
| 6442 | removeContentProviderExternal(name); |
| 6443 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6444 | 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] | 6445 | } |
| 6446 | return pfd; |
| 6447 | } |
| 6448 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6449 | // Actually is sleeping or shutting down or whatever else in the future |
| 6450 | // is an inactive state. |
| 6451 | public boolean isSleeping() { |
| 6452 | return mSleeping || mShuttingDown; |
| 6453 | } |
| 6454 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6455 | public void goingToSleep() { |
| 6456 | synchronized(this) { |
| 6457 | mSleeping = true; |
| 6458 | mWindowManager.setEventDispatching(false); |
| 6459 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6460 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6461 | |
| 6462 | // Initialize the wake times of all processes. |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 6463 | checkExcessivePowerUsageLocked(false); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6464 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 6465 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 6466 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6467 | } |
| 6468 | } |
| 6469 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6470 | public boolean shutdown(int timeout) { |
| 6471 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 6472 | != PackageManager.PERMISSION_GRANTED) { |
| 6473 | throw new SecurityException("Requires permission " |
| 6474 | + android.Manifest.permission.SHUTDOWN); |
| 6475 | } |
| 6476 | |
| 6477 | boolean timedout = false; |
| 6478 | |
| 6479 | synchronized(this) { |
| 6480 | mShuttingDown = true; |
| 6481 | mWindowManager.setEventDispatching(false); |
| 6482 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6483 | if (mMainStack.mResumedActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6484 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6485 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6486 | while (mMainStack.mResumedActivity != null |
Dianne Hackborn | cbb722e | 2012-02-07 18:33:49 -0800 | [diff] [blame] | 6487 | || mMainStack.mPausingActivities.size() > 0) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6488 | long delay = endTime - System.currentTimeMillis(); |
| 6489 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6490 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6491 | timedout = true; |
| 6492 | break; |
| 6493 | } |
| 6494 | try { |
| 6495 | this.wait(); |
| 6496 | } catch (InterruptedException e) { |
| 6497 | } |
| 6498 | } |
| 6499 | } |
| 6500 | } |
| 6501 | |
| 6502 | mUsageStatsService.shutdown(); |
| 6503 | mBatteryStatsService.shutdown(); |
| 6504 | |
| 6505 | return timedout; |
| 6506 | } |
| 6507 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6508 | public final void activitySlept(IBinder token) { |
| 6509 | if (localLOGV) Slog.v( |
| 6510 | TAG, "Activity slept: token=" + token); |
| 6511 | |
| 6512 | ActivityRecord r = null; |
| 6513 | |
| 6514 | final long origId = Binder.clearCallingIdentity(); |
| 6515 | |
| 6516 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6517 | r = mMainStack.isInStackLocked(token); |
| 6518 | if (r != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6519 | mMainStack.activitySleptLocked(r); |
| 6520 | } |
| 6521 | } |
| 6522 | |
| 6523 | Binder.restoreCallingIdentity(origId); |
| 6524 | } |
| 6525 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6526 | public void wakingUp() { |
| 6527 | synchronized(this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6528 | mWindowManager.setEventDispatching(true); |
| 6529 | mSleeping = false; |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6530 | mMainStack.awakeFromSleepingLocked(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6531 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6532 | } |
| 6533 | } |
| 6534 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6535 | public void stopAppSwitches() { |
| 6536 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6537 | != PackageManager.PERMISSION_GRANTED) { |
| 6538 | throw new SecurityException("Requires permission " |
| 6539 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6540 | } |
| 6541 | |
| 6542 | synchronized(this) { |
| 6543 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 6544 | + APP_SWITCH_DELAY_TIME; |
| 6545 | mDidAppSwitch = false; |
| 6546 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6547 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 6548 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 6549 | } |
| 6550 | } |
| 6551 | |
| 6552 | public void resumeAppSwitches() { |
| 6553 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 6554 | != PackageManager.PERMISSION_GRANTED) { |
| 6555 | throw new SecurityException("Requires permission " |
| 6556 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 6557 | } |
| 6558 | |
| 6559 | synchronized(this) { |
| 6560 | // Note that we don't execute any pending app switches... we will |
| 6561 | // let those wait until either the timeout, or the next start |
| 6562 | // activity request. |
| 6563 | mAppSwitchesAllowedTime = 0; |
| 6564 | } |
| 6565 | } |
| 6566 | |
| 6567 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 6568 | String name) { |
| 6569 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 6570 | return true; |
| 6571 | } |
| 6572 | |
| 6573 | final int perm = checkComponentPermission( |
| 6574 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6575 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6576 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 6577 | return true; |
| 6578 | } |
| 6579 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6580 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6581 | return false; |
| 6582 | } |
| 6583 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6584 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 6585 | boolean persistent) { |
| 6586 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 6587 | "setDebugApp()"); |
| 6588 | |
| 6589 | // Note that this is not really thread safe if there are multiple |
| 6590 | // callers into it at the same time, but that's not a situation we |
| 6591 | // care about. |
| 6592 | if (persistent) { |
| 6593 | final ContentResolver resolver = mContext.getContentResolver(); |
| 6594 | Settings.System.putString( |
| 6595 | resolver, Settings.System.DEBUG_APP, |
| 6596 | packageName); |
| 6597 | Settings.System.putInt( |
| 6598 | resolver, Settings.System.WAIT_FOR_DEBUGGER, |
| 6599 | waitForDebugger ? 1 : 0); |
| 6600 | } |
| 6601 | |
| 6602 | synchronized (this) { |
| 6603 | if (!persistent) { |
| 6604 | mOrigDebugApp = mDebugApp; |
| 6605 | mOrigWaitForDebugger = mWaitForDebugger; |
| 6606 | } |
| 6607 | mDebugApp = packageName; |
| 6608 | mWaitForDebugger = waitForDebugger; |
| 6609 | mDebugTransient = !persistent; |
| 6610 | if (packageName != null) { |
| 6611 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 6612 | forceStopPackageLocked(packageName, -1, false, false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6613 | Binder.restoreCallingIdentity(origId); |
| 6614 | } |
| 6615 | } |
| 6616 | } |
| 6617 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 6618 | void setProfileApp(ApplicationInfo app, String processName, String profileFile, |
| 6619 | ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
| 6620 | synchronized (this) { |
| 6621 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 6622 | if (!isDebuggable) { |
| 6623 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 6624 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 6625 | } |
| 6626 | } |
| 6627 | mProfileApp = processName; |
| 6628 | mProfileFile = profileFile; |
| 6629 | if (mProfileFd != null) { |
| 6630 | try { |
| 6631 | mProfileFd.close(); |
| 6632 | } catch (IOException e) { |
| 6633 | } |
| 6634 | mProfileFd = null; |
| 6635 | } |
| 6636 | mProfileFd = profileFd; |
| 6637 | mProfileType = 0; |
| 6638 | mAutoStopProfiler = autoStopProfiler; |
| 6639 | } |
| 6640 | } |
| 6641 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6642 | public void setAlwaysFinish(boolean enabled) { |
| 6643 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 6644 | "setAlwaysFinish()"); |
| 6645 | |
| 6646 | Settings.System.putInt( |
| 6647 | mContext.getContentResolver(), |
| 6648 | Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
| 6649 | |
| 6650 | synchronized (this) { |
| 6651 | mAlwaysFinishActivities = enabled; |
| 6652 | } |
| 6653 | } |
| 6654 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6655 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6656 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6657 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6658 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6659 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6660 | } |
| 6661 | } |
| 6662 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 6663 | public boolean isUserAMonkey() { |
| 6664 | // For now the fact that there is a controller implies |
| 6665 | // we have a monkey. |
| 6666 | synchronized (this) { |
| 6667 | return mController != null; |
| 6668 | } |
| 6669 | } |
| 6670 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 6671 | public void registerProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 6672 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
| 6673 | "registerProcessObserver()"); |
| 6674 | synchronized (this) { |
| 6675 | mProcessObservers.register(observer); |
| 6676 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 6677 | } |
| 6678 | |
| 6679 | public void unregisterProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 6680 | synchronized (this) { |
| 6681 | mProcessObservers.unregister(observer); |
| 6682 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 6683 | } |
| 6684 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6685 | public void setImmersive(IBinder token, boolean immersive) { |
| 6686 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6687 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 6688 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6689 | throw new IllegalArgumentException(); |
| 6690 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6691 | r.immersive = immersive; |
| 6692 | } |
| 6693 | } |
| 6694 | |
| 6695 | public boolean isImmersive(IBinder token) { |
| 6696 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6697 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 6698 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6699 | throw new IllegalArgumentException(); |
| 6700 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6701 | return r.immersive; |
| 6702 | } |
| 6703 | } |
| 6704 | |
| 6705 | public boolean isTopActivityImmersive() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6706 | enforceNotIsolatedCaller("startActivity"); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6707 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6708 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 6709 | return (r != null) ? r.immersive : false; |
| 6710 | } |
| 6711 | } |
| 6712 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6713 | public final void enterSafeMode() { |
| 6714 | synchronized(this) { |
| 6715 | // It only makes sense to do this before the system is ready |
| 6716 | // and started launching other packages. |
| 6717 | if (!mSystemReady) { |
| 6718 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6719 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6720 | } catch (RemoteException e) { |
| 6721 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6722 | } |
| 6723 | } |
| 6724 | } |
| 6725 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 6726 | public final void showSafeModeOverlay() { |
| 6727 | View v = LayoutInflater.from(mContext).inflate( |
| 6728 | com.android.internal.R.layout.safe_mode, null); |
| 6729 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 6730 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 6731 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6732 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
| 6733 | lp.gravity = Gravity.BOTTOM | Gravity.LEFT; |
| 6734 | lp.format = v.getBackground().getOpacity(); |
| 6735 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 6736 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 6737 | ((WindowManager)mContext.getSystemService( |
| 6738 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 6739 | } |
| 6740 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6741 | public void noteWakeupAlarm(IIntentSender sender) { |
| 6742 | if (!(sender instanceof PendingIntentRecord)) { |
| 6743 | return; |
| 6744 | } |
| 6745 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 6746 | synchronized (stats) { |
| 6747 | if (mBatteryStatsService.isOnBattery()) { |
| 6748 | mBatteryStatsService.enforceCallingPermission(); |
| 6749 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 6750 | int MY_UID = Binder.getCallingUid(); |
| 6751 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 6752 | BatteryStatsImpl.Uid.Pkg pkg = |
| 6753 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 6754 | pkg.incWakeupsLocked(); |
| 6755 | } |
| 6756 | } |
| 6757 | } |
| 6758 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6759 | public boolean killPids(int[] pids, String pReason, boolean secure) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6760 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6761 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6762 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6763 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6764 | // XXX Note: don't acquire main activity lock here, because the window |
| 6765 | // manager calls in with its locks held. |
| 6766 | |
| 6767 | boolean killed = false; |
| 6768 | synchronized (mPidsSelfLocked) { |
| 6769 | int[] types = new int[pids.length]; |
| 6770 | int worstType = 0; |
| 6771 | for (int i=0; i<pids.length; i++) { |
| 6772 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 6773 | if (proc != null) { |
| 6774 | int type = proc.setAdj; |
| 6775 | types[i] = type; |
| 6776 | if (type > worstType) { |
| 6777 | worstType = type; |
| 6778 | } |
| 6779 | } |
| 6780 | } |
| 6781 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6782 | // 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] | 6783 | // then constrain it so we will kill all hidden procs. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 6784 | if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ |
| 6785 | && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 6786 | worstType = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6787 | } |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6788 | |
| 6789 | // If this is not a secure call, don't let it kill processes that |
| 6790 | // are important. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 6791 | if (!secure && worstType < ProcessList.SERVICE_ADJ) { |
| 6792 | worstType = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 6793 | } |
| 6794 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6795 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6796 | for (int i=0; i<pids.length; i++) { |
| 6797 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 6798 | if (proc == null) { |
| 6799 | continue; |
| 6800 | } |
| 6801 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6802 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 6803 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 6804 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid, |
| 6805 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6806 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6807 | proc.killedBackground = true; |
| 6808 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6809 | } |
| 6810 | } |
| 6811 | } |
| 6812 | return killed; |
| 6813 | } |
| 6814 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6815 | public final void startRunning(String pkg, String cls, String action, |
| 6816 | String data) { |
| 6817 | synchronized(this) { |
| 6818 | if (mStartRunning) { |
| 6819 | return; |
| 6820 | } |
| 6821 | mStartRunning = true; |
| 6822 | mTopComponent = pkg != null && cls != null |
| 6823 | ? new ComponentName(pkg, cls) : null; |
| 6824 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 6825 | mTopData = data; |
| 6826 | if (!mSystemReady) { |
| 6827 | return; |
| 6828 | } |
| 6829 | } |
| 6830 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6831 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6832 | } |
| 6833 | |
| 6834 | private void retrieveSettings() { |
| 6835 | final ContentResolver resolver = mContext.getContentResolver(); |
| 6836 | String debugApp = Settings.System.getString( |
| 6837 | resolver, Settings.System.DEBUG_APP); |
| 6838 | boolean waitForDebugger = Settings.System.getInt( |
| 6839 | resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0; |
| 6840 | boolean alwaysFinishActivities = Settings.System.getInt( |
| 6841 | resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
| 6842 | |
| 6843 | Configuration configuration = new Configuration(); |
| 6844 | Settings.System.getConfiguration(resolver, configuration); |
| 6845 | |
| 6846 | synchronized (this) { |
| 6847 | mDebugApp = mOrigDebugApp = debugApp; |
| 6848 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 6849 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 6850 | // This happens before any activities are started, so we can |
| 6851 | // change mConfiguration in-place. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 6852 | updateConfigurationLocked(configuration, null, false, true); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6853 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6854 | } |
| 6855 | } |
| 6856 | |
| 6857 | public boolean testIsSystemReady() { |
| 6858 | // no need to synchronize(this) just to read & return the value |
| 6859 | return mSystemReady; |
| 6860 | } |
| 6861 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6862 | private static File getCalledPreBootReceiversFile() { |
| 6863 | File dataDir = Environment.getDataDirectory(); |
| 6864 | File systemDir = new File(dataDir, "system"); |
| 6865 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 6866 | return fname; |
| 6867 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6868 | |
| 6869 | static final int LAST_DONE_VERSION = 10000; |
| 6870 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6871 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 6872 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 6873 | File file = getCalledPreBootReceiversFile(); |
| 6874 | FileInputStream fis = null; |
| 6875 | try { |
| 6876 | fis = new FileInputStream(file); |
| 6877 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6878 | int fvers = dis.readInt(); |
| 6879 | if (fvers == LAST_DONE_VERSION) { |
| 6880 | String vers = dis.readUTF(); |
| 6881 | String codename = dis.readUTF(); |
| 6882 | String build = dis.readUTF(); |
| 6883 | if (android.os.Build.VERSION.RELEASE.equals(vers) |
| 6884 | && android.os.Build.VERSION.CODENAME.equals(codename) |
| 6885 | && android.os.Build.VERSION.INCREMENTAL.equals(build)) { |
| 6886 | int num = dis.readInt(); |
| 6887 | while (num > 0) { |
| 6888 | num--; |
| 6889 | String pkg = dis.readUTF(); |
| 6890 | String cls = dis.readUTF(); |
| 6891 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 6892 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6893 | } |
| 6894 | } |
| 6895 | } catch (FileNotFoundException e) { |
| 6896 | } catch (IOException e) { |
| 6897 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 6898 | } finally { |
| 6899 | if (fis != null) { |
| 6900 | try { |
| 6901 | fis.close(); |
| 6902 | } catch (IOException e) { |
| 6903 | } |
| 6904 | } |
| 6905 | } |
| 6906 | return lastDoneReceivers; |
| 6907 | } |
| 6908 | |
| 6909 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 6910 | File file = getCalledPreBootReceiversFile(); |
| 6911 | FileOutputStream fos = null; |
| 6912 | DataOutputStream dos = null; |
| 6913 | try { |
| 6914 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 6915 | fos = new FileOutputStream(file); |
| 6916 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6917 | dos.writeInt(LAST_DONE_VERSION); |
| 6918 | dos.writeUTF(android.os.Build.VERSION.RELEASE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6919 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 6920 | dos.writeUTF(android.os.Build.VERSION.INCREMENTAL); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6921 | dos.writeInt(list.size()); |
| 6922 | for (int i=0; i<list.size(); i++) { |
| 6923 | dos.writeUTF(list.get(i).getPackageName()); |
| 6924 | dos.writeUTF(list.get(i).getClassName()); |
| 6925 | } |
| 6926 | } catch (IOException e) { |
| 6927 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 6928 | file.delete(); |
| 6929 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 6930 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6931 | if (dos != null) { |
| 6932 | try { |
| 6933 | dos.close(); |
| 6934 | } catch (IOException e) { |
| 6935 | // TODO Auto-generated catch block |
| 6936 | e.printStackTrace(); |
| 6937 | } |
| 6938 | } |
| 6939 | } |
| 6940 | } |
| 6941 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6942 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6943 | synchronized(this) { |
| 6944 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 6945 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6946 | return; |
| 6947 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6948 | |
| 6949 | // Check to see if there are any update receivers to run. |
| 6950 | if (!mDidUpdate) { |
| 6951 | if (mWaitingUpdate) { |
| 6952 | return; |
| 6953 | } |
| 6954 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 6955 | List<ResolveInfo> ris = null; |
| 6956 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6957 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6958 | intent, null, 0); |
| 6959 | } catch (RemoteException e) { |
| 6960 | } |
| 6961 | if (ris != null) { |
| 6962 | for (int i=ris.size()-1; i>=0; i--) { |
| 6963 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 6964 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6965 | ris.remove(i); |
| 6966 | } |
| 6967 | } |
| 6968 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6969 | |
| 6970 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
| 6971 | |
| 6972 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6973 | for (int i=0; i<ris.size(); i++) { |
| 6974 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6975 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 6976 | if (lastDoneReceivers.contains(comp)) { |
| 6977 | ris.remove(i); |
| 6978 | i--; |
| 6979 | } |
| 6980 | } |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 6981 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 6982 | for (int i=0; i<ris.size(); i++) { |
| 6983 | ActivityInfo ai = ris.get(i).activityInfo; |
| 6984 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 6985 | doneReceivers.add(comp); |
| 6986 | intent.setComponent(comp); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6987 | IIntentReceiver finisher = null; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 6988 | if (i == ris.size()-1) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 6989 | finisher = new IIntentReceiver.Stub() { |
| 6990 | public void performReceive(Intent intent, int resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 6991 | String data, Bundle extras, boolean ordered, |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 6992 | boolean sticky) { |
| 6993 | // The raw IIntentReceiver interface is called |
| 6994 | // with the AM lock held, so redispatch to |
| 6995 | // execute our code without the lock. |
| 6996 | mHandler.post(new Runnable() { |
| 6997 | public void run() { |
| 6998 | synchronized (ActivityManagerService.this) { |
| 6999 | mDidUpdate = true; |
| 7000 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7001 | writeLastDonePreBootReceivers(doneReceivers); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7002 | showBootMessage(mContext.getText( |
| 7003 | R.string.android_upgrading_complete), |
| 7004 | false); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 7005 | systemReady(goingCallback); |
| 7006 | } |
| 7007 | }); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7008 | } |
| 7009 | }; |
| 7010 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7011 | Slog.i(TAG, "Sending system update to: " + intent.getComponent()); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 7012 | /* TODO: Send this to all users */ |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7013 | broadcastIntentLocked(null, null, intent, null, finisher, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 7014 | 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID, |
| 7015 | Process.SYSTEM_UID); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 7016 | if (finisher != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7017 | mWaitingUpdate = true; |
| 7018 | } |
| 7019 | } |
| 7020 | } |
| 7021 | if (mWaitingUpdate) { |
| 7022 | return; |
| 7023 | } |
| 7024 | mDidUpdate = true; |
| 7025 | } |
| 7026 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7027 | mSystemReady = true; |
| 7028 | if (!mStartRunning) { |
| 7029 | return; |
| 7030 | } |
| 7031 | } |
| 7032 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7033 | ArrayList<ProcessRecord> procsToKill = null; |
| 7034 | synchronized(mPidsSelfLocked) { |
| 7035 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 7036 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7037 | if (!isAllowedWhileBooting(proc.info)){ |
| 7038 | if (procsToKill == null) { |
| 7039 | procsToKill = new ArrayList<ProcessRecord>(); |
| 7040 | } |
| 7041 | procsToKill.add(proc); |
| 7042 | } |
| 7043 | } |
| 7044 | } |
| 7045 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7046 | synchronized(this) { |
| 7047 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7048 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 7049 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7050 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7051 | removeProcessLocked(proc, true, false, "system update done"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7052 | } |
| 7053 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7054 | |
| 7055 | // Now that we have cleaned up any update processes, we |
| 7056 | // are ready to start launching real processes and know that |
| 7057 | // we won't trample on them any more. |
| 7058 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7059 | } |
| 7060 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7061 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7062 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7063 | SystemClock.uptimeMillis()); |
| 7064 | |
| 7065 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7066 | // Make sure we have no pre-ready processes sitting around. |
| 7067 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7068 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7069 | ResolveInfo ri = mContext.getPackageManager() |
| 7070 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7071 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7072 | CharSequence errorMsg = null; |
| 7073 | if (ri != null) { |
| 7074 | ActivityInfo ai = ri.activityInfo; |
| 7075 | ApplicationInfo app = ai.applicationInfo; |
| 7076 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7077 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 7078 | mTopData = null; |
| 7079 | mTopComponent = new ComponentName(app.packageName, |
| 7080 | ai.name); |
| 7081 | } else { |
| 7082 | errorMsg = mContext.getResources().getText( |
| 7083 | com.android.internal.R.string.factorytest_not_system); |
| 7084 | } |
| 7085 | } else { |
| 7086 | errorMsg = mContext.getResources().getText( |
| 7087 | com.android.internal.R.string.factorytest_no_action); |
| 7088 | } |
| 7089 | if (errorMsg != null) { |
| 7090 | mTopAction = null; |
| 7091 | mTopData = null; |
| 7092 | mTopComponent = null; |
| 7093 | Message msg = Message.obtain(); |
| 7094 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 7095 | msg.getData().putCharSequence("msg", errorMsg); |
| 7096 | mHandler.sendMessage(msg); |
| 7097 | } |
| 7098 | } |
| 7099 | } |
| 7100 | |
| 7101 | retrieveSettings(); |
| 7102 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7103 | if (goingCallback != null) goingCallback.run(); |
| 7104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7105 | synchronized (this) { |
| 7106 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7107 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7108 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7109 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7110 | if (apps != null) { |
| 7111 | int N = apps.size(); |
| 7112 | int i; |
| 7113 | for (i=0; i<N; i++) { |
| 7114 | ApplicationInfo info |
| 7115 | = (ApplicationInfo)apps.get(i); |
| 7116 | if (info != null && |
| 7117 | !info.packageName.equals("android")) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7118 | addAppLocked(info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7119 | } |
| 7120 | } |
| 7121 | } |
| 7122 | } catch (RemoteException ex) { |
| 7123 | // pm is in same process, this will never happen. |
| 7124 | } |
| 7125 | } |
| 7126 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7127 | // Start up initial activity. |
| 7128 | mBooting = true; |
| 7129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7130 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7131 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7132 | Message msg = Message.obtain(); |
| 7133 | msg.what = SHOW_UID_ERROR_MSG; |
| 7134 | mHandler.sendMessage(msg); |
| 7135 | } |
| 7136 | } catch (RemoteException e) { |
| 7137 | } |
| 7138 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7139 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7140 | } |
| 7141 | } |
| 7142 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7143 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7144 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7145 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7146 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7147 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7148 | startAppProblemLocked(app); |
| 7149 | app.stopFreezingAllLocked(); |
| 7150 | return handleAppCrashLocked(app); |
| 7151 | } |
| 7152 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7153 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7154 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7155 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7156 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7157 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 7158 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7159 | startAppProblemLocked(app); |
| 7160 | app.stopFreezingAllLocked(); |
| 7161 | } |
| 7162 | |
| 7163 | /** |
| 7164 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 7165 | * |
| 7166 | * @param app The ProcessRecord in which the error occurred. |
| 7167 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 7168 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7169 | * @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] | 7170 | * @param shortMsg Short message describing the crash. |
| 7171 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7172 | * @param stackTrace Full crash stack trace, may be null. |
| 7173 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7174 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 7175 | */ |
| 7176 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7177 | 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] | 7178 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7179 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7180 | report.condition = condition; |
| 7181 | report.processName = app.processName; |
| 7182 | report.pid = app.pid; |
| 7183 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7184 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7185 | report.shortMsg = shortMsg; |
| 7186 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7187 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7188 | |
| 7189 | return report; |
| 7190 | } |
| 7191 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7192 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7193 | synchronized (this) { |
| 7194 | app.crashing = false; |
| 7195 | app.crashingReport = null; |
| 7196 | app.notResponding = false; |
| 7197 | app.notRespondingReport = null; |
| 7198 | if (app.anrDialog == fromDialog) { |
| 7199 | app.anrDialog = null; |
| 7200 | } |
| 7201 | if (app.waitDialog == fromDialog) { |
| 7202 | app.waitDialog = null; |
| 7203 | } |
| 7204 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7205 | handleAppCrashLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7206 | Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request"); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7207 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 7208 | app.processName, app.setAdj, "user's request after error"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7209 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7210 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7211 | } |
| 7212 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7213 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7214 | private boolean handleAppCrashLocked(ProcessRecord app) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7215 | long now = SystemClock.uptimeMillis(); |
| 7216 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7217 | Long crashTime; |
| 7218 | if (!app.isolated) { |
| 7219 | crashTime = mProcessCrashTimes.get(app.info.processName, app.uid); |
| 7220 | } else { |
| 7221 | crashTime = null; |
| 7222 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7223 | if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7224 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7225 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7226 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7227 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7228 | app.info.processName, app.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7229 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 7230 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7231 | if (r.app == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7232 | Slog.w(TAG, " Force finishing activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7233 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7234 | 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] | 7235 | } |
| 7236 | } |
| 7237 | if (!app.persistent) { |
| 7238 | // We don't want to start this process again until the user |
| 7239 | // explicitly does so... but for persistent process, we really |
| 7240 | // need to keep it running. If a persistent process is actually |
| 7241 | // repeatedly crashing, then badness for everyone. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7242 | EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7243 | app.info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7244 | if (!app.isolated) { |
| 7245 | // XXX We don't have a way to mark isolated processes |
| 7246 | // as bad, since they don't have a peristent identity. |
| 7247 | mBadProcesses.put(app.info.processName, app.uid, now); |
| 7248 | mProcessCrashTimes.remove(app.info.processName, app.uid); |
| 7249 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7250 | app.bad = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7251 | app.removed = true; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 7252 | // Don't let services in this process be restarted and potentially |
| 7253 | // annoy the user repeatedly. Unless it is persistent, since those |
| 7254 | // processes run critical code. |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7255 | removeProcessLocked(app, false, false, "crash"); |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7256 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7257 | return false; |
| 7258 | } |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7259 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7260 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7261 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7262 | if (r.app == app) { |
| 7263 | // If the top running activity is from this crashing |
| 7264 | // process, then terminate it to avoid getting in a loop. |
| 7265 | Slog.w(TAG, " Force finishing activity " |
| 7266 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 7267 | int index = mMainStack.indexOfActivityLocked(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7268 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7269 | Activity.RESULT_CANCELED, null, "crashed"); |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 7270 | // Also terminate any activities below it that aren't yet |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7271 | // stopped, to avoid a situation where one will get |
| 7272 | // re-start our crashing activity once it gets resumed again. |
| 7273 | index--; |
| 7274 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7275 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7276 | if (r.state == ActivityState.RESUMED |
| 7277 | || r.state == ActivityState.PAUSING |
| 7278 | || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 7279 | if (!r.isHomeActivity || mHomeProcess != r.app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7280 | Slog.w(TAG, " Force finishing activity " |
| 7281 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7282 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7283 | Activity.RESULT_CANCELED, null, "crashed"); |
| 7284 | } |
| 7285 | } |
| 7286 | } |
| 7287 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7288 | } |
| 7289 | |
| 7290 | // Bump up the crash count of any services currently running in the proc. |
| 7291 | if (app.services.size() != 0) { |
| 7292 | // Any services running in the application need to be placed |
| 7293 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7294 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7295 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7296 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7297 | sr.crashCount++; |
| 7298 | } |
| 7299 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7300 | |
| 7301 | // If the crashing process is what we consider to be the "home process" and it has been |
| 7302 | // replaced by a third-party app, clear the package preferred activities from packages |
| 7303 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 7304 | // from blocking the user to manually clear the list. |
| 7305 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 7306 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7307 | Iterator it = mHomeProcess.activities.iterator(); |
| 7308 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 7309 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7310 | if (r.isHomeActivity) { |
| 7311 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 7312 | try { |
| 7313 | ActivityThread.getPackageManager() |
| 7314 | .clearPackagePreferredActivities(r.packageName); |
| 7315 | } catch (RemoteException c) { |
| 7316 | // pm is in same process, this will never happen. |
| 7317 | } |
| 7318 | } |
| 7319 | } |
| 7320 | } |
| 7321 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7322 | if (!app.isolated) { |
| 7323 | // XXX Can't keep track of crash times for isolated processes, |
| 7324 | // because they don't have a perisistent identity. |
| 7325 | mProcessCrashTimes.put(app.info.processName, app.uid, now); |
| 7326 | } |
| 7327 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7328 | return true; |
| 7329 | } |
| 7330 | |
| 7331 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 7332 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 7333 | mContext, app.info.packageName, app.info.flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7334 | skipCurrentReceiverLocked(app); |
| 7335 | } |
| 7336 | |
| 7337 | void skipCurrentReceiverLocked(ProcessRecord app) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 7338 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 7339 | queue.skipCurrentReceiverLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7340 | } |
| 7341 | } |
| 7342 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7343 | /** |
| 7344 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 7345 | * The application process will exit immediately after this call returns. |
| 7346 | * @param app object of the crashing app, null for the system server |
| 7347 | * @param crashInfo describing the exception |
| 7348 | */ |
| 7349 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7350 | ProcessRecord r = findAppProcess(app, "Crash"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7351 | final String processName = app == null ? "system_server" |
| 7352 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7353 | |
| 7354 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7355 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7356 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7357 | crashInfo.exceptionClassName, |
| 7358 | crashInfo.exceptionMessage, |
| 7359 | crashInfo.throwFileName, |
| 7360 | crashInfo.throwLineNumber); |
| 7361 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7362 | addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7363 | |
| 7364 | crashApplication(r, crashInfo); |
| 7365 | } |
| 7366 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7367 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7368 | IBinder app, |
| 7369 | int violationMask, |
| 7370 | StrictMode.ViolationInfo info) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7371 | ProcessRecord r = findAppProcess(app, "StrictMode"); |
| 7372 | if (r == null) { |
| 7373 | return; |
| 7374 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7375 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7376 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 7377 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7378 | boolean logIt = true; |
| 7379 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 7380 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 7381 | logIt = false; |
| 7382 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7383 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7384 | // the relative pain numbers, without logging all |
| 7385 | // the stack traces repeatedly. We'd want to do |
| 7386 | // likewise in the client code, which also does |
| 7387 | // dup suppression, before the Binder call. |
| 7388 | } else { |
| 7389 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 7390 | mAlreadyLoggedViolatedStacks.clear(); |
| 7391 | } |
| 7392 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 7393 | } |
| 7394 | } |
| 7395 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7396 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7397 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7398 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7399 | |
| 7400 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 7401 | AppErrorResult result = new AppErrorResult(); |
| 7402 | synchronized (this) { |
| 7403 | final long origId = Binder.clearCallingIdentity(); |
| 7404 | |
| 7405 | Message msg = Message.obtain(); |
| 7406 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 7407 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 7408 | data.put("result", result); |
| 7409 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7410 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7411 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7412 | msg.obj = data; |
| 7413 | mHandler.sendMessage(msg); |
| 7414 | |
| 7415 | Binder.restoreCallingIdentity(origId); |
| 7416 | } |
| 7417 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 7418 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7419 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7420 | } |
| 7421 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7422 | // Depending on the policy in effect, there could be a bunch of |
| 7423 | // these in quick succession so we try to batch these together to |
| 7424 | // minimize disk writes, number of dropbox entries, and maximize |
| 7425 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7426 | private void logStrictModeViolationToDropBox( |
| 7427 | ProcessRecord process, |
| 7428 | StrictMode.ViolationInfo info) { |
| 7429 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7430 | return; |
| 7431 | } |
| 7432 | final boolean isSystemApp = process == null || |
| 7433 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 7434 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7435 | final String processName = process == null ? "unknown" : process.processName; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7436 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 7437 | final DropBoxManager dbox = (DropBoxManager) |
| 7438 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7439 | |
| 7440 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7441 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7442 | |
| 7443 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7444 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7445 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 7446 | synchronized (sb) { |
| 7447 | bufferWasEmpty = sb.length() == 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7448 | appendDropBoxProcessHeaders(process, processName, sb); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7449 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 7450 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7451 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 7452 | if (info.violationNumThisLoop != 0) { |
| 7453 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 7454 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 7455 | if (info.numAnimationsRunning != 0) { |
| 7456 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 7457 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 7458 | if (info.broadcastIntentAction != null) { |
| 7459 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 7460 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7461 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7462 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7463 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 7464 | if (info.numInstances != -1) { |
| 7465 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 7466 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 7467 | if (info.tags != null) { |
| 7468 | for (String tag : info.tags) { |
| 7469 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 7470 | } |
| 7471 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7472 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7473 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 7474 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7475 | } |
| 7476 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7477 | |
| 7478 | // Only buffer up to ~64k. Various logging bits truncate |
| 7479 | // things at 128k. |
| 7480 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7481 | } |
| 7482 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7483 | // Flush immediately if the buffer's grown too large, or this |
| 7484 | // is a non-system app. Non-system apps are isolated with a |
| 7485 | // different tag & policy and not batched. |
| 7486 | // |
| 7487 | // Batching is useful during internal testing with |
| 7488 | // StrictMode settings turned up high. Without batching, |
| 7489 | // thousands of separate files could be created on boot. |
| 7490 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7491 | new Thread("Error dump: " + dropboxTag) { |
| 7492 | @Override |
| 7493 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7494 | String report; |
| 7495 | synchronized (sb) { |
| 7496 | report = sb.toString(); |
| 7497 | sb.delete(0, sb.length()); |
| 7498 | sb.trimToSize(); |
| 7499 | } |
| 7500 | if (report.length() != 0) { |
| 7501 | dbox.addText(dropboxTag, report); |
| 7502 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7503 | } |
| 7504 | }.start(); |
| 7505 | return; |
| 7506 | } |
| 7507 | |
| 7508 | // System app batching: |
| 7509 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 7510 | // An existing dropbox-writing thread is outstanding, so |
| 7511 | // we don't need to start it up. The existing thread will |
| 7512 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7513 | return; |
| 7514 | } |
| 7515 | |
| 7516 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 7517 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7518 | new Thread("Error dump: " + dropboxTag) { |
| 7519 | @Override |
| 7520 | public void run() { |
| 7521 | // 5 second sleep to let stacks arrive and be batched together |
| 7522 | try { |
| 7523 | Thread.sleep(5000); // 5 seconds |
| 7524 | } catch (InterruptedException e) {} |
| 7525 | |
| 7526 | String errorReport; |
| 7527 | synchronized (mStrictModeBuffer) { |
| 7528 | errorReport = mStrictModeBuffer.toString(); |
| 7529 | if (errorReport.length() == 0) { |
| 7530 | return; |
| 7531 | } |
| 7532 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 7533 | mStrictModeBuffer.trimToSize(); |
| 7534 | } |
| 7535 | dbox.addText(dropboxTag, errorReport); |
| 7536 | } |
| 7537 | }.start(); |
| 7538 | } |
| 7539 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7540 | /** |
| 7541 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 7542 | * @param app object of the crashing app, null for the system server |
| 7543 | * @param tag reported by the caller |
| 7544 | * @param crashInfo describing the context of the error |
| 7545 | * @return true if the process should exit immediately (WTF is fatal) |
| 7546 | */ |
| 7547 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7548 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7549 | ProcessRecord r = findAppProcess(app, "WTF"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7550 | final String processName = app == null ? "system_server" |
| 7551 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7552 | |
| 7553 | EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7554 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7555 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7556 | tag, crashInfo.exceptionMessage); |
| 7557 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7558 | addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7559 | |
Dianne Hackborn | 1ab4377 | 2011-03-15 14:38:02 -0700 | [diff] [blame] | 7560 | if (r != null && r.pid != Process.myPid() && |
| 7561 | Settings.Secure.getInt(mContext.getContentResolver(), |
| 7562 | Settings.Secure.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7563 | crashApplication(r, crashInfo); |
| 7564 | return true; |
| 7565 | } else { |
| 7566 | return false; |
| 7567 | } |
| 7568 | } |
| 7569 | |
| 7570 | /** |
| 7571 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 7572 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 7573 | */ |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7574 | private ProcessRecord findAppProcess(IBinder app, String reason) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7575 | if (app == null) { |
| 7576 | return null; |
| 7577 | } |
| 7578 | |
| 7579 | synchronized (this) { |
| 7580 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 7581 | final int NA = apps.size(); |
| 7582 | for (int ia=0; ia<NA; ia++) { |
| 7583 | ProcessRecord p = apps.valueAt(ia); |
| 7584 | if (p.thread != null && p.thread.asBinder() == app) { |
| 7585 | return p; |
| 7586 | } |
| 7587 | } |
| 7588 | } |
| 7589 | |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7590 | Slog.w(TAG, "Can't find mystery application for " + reason |
| 7591 | + " from pid=" + Binder.getCallingPid() |
| 7592 | + " uid=" + Binder.getCallingUid() + ": " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7593 | return null; |
| 7594 | } |
| 7595 | } |
| 7596 | |
| 7597 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7598 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 7599 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7600 | */ |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7601 | private void appendDropBoxProcessHeaders(ProcessRecord process, String processName, |
| 7602 | StringBuilder sb) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 7603 | // Watchdog thread ends up invoking this function (with |
| 7604 | // a null ProcessRecord) to add the stack file to dropbox. |
| 7605 | // Do not acquire a lock on this (am) in such cases, as it |
| 7606 | // could cause a potential deadlock, if and when watchdog |
| 7607 | // is invoked due to unavailability of lock on am and it |
| 7608 | // would prevent watchdog from killing system_server. |
| 7609 | if (process == null) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7610 | sb.append("Process: ").append(processName).append("\n"); |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 7611 | return; |
| 7612 | } |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 7613 | // Note: ProcessRecord 'process' is guarded by the service |
| 7614 | // instance. (notably process.pkgList, which could otherwise change |
| 7615 | // concurrently during execution of this method) |
| 7616 | synchronized (this) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7617 | sb.append("Process: ").append(processName).append("\n"); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7618 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7619 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7620 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 7621 | for (String pkg : process.pkgList) { |
| 7622 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7623 | try { |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7624 | PackageInfo pi = pm.getPackageInfo(pkg, 0); |
| 7625 | if (pi != null) { |
| 7626 | sb.append(" v").append(pi.versionCode); |
| 7627 | if (pi.versionName != null) { |
| 7628 | sb.append(" (").append(pi.versionName).append(")"); |
| 7629 | } |
| 7630 | } |
| 7631 | } catch (RemoteException e) { |
| 7632 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7633 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7634 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7635 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7636 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7637 | } |
| 7638 | |
| 7639 | private static String processClass(ProcessRecord process) { |
| 7640 | if (process == null || process.pid == MY_PID) { |
| 7641 | return "system_server"; |
| 7642 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7643 | return "system_app"; |
| 7644 | } else { |
| 7645 | return "data_app"; |
| 7646 | } |
| 7647 | } |
| 7648 | |
| 7649 | /** |
| 7650 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 7651 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 7652 | * @param process which caused the error, null means the system server |
| 7653 | * @param activity which triggered the error, null if unknown |
| 7654 | * @param parent activity related to the error, null if unknown |
| 7655 | * @param subject line related to the error, null if absent |
| 7656 | * @param report in long form describing the error, null if absent |
| 7657 | * @param logFile to include in the report, null if none |
| 7658 | * @param crashInfo giving an application stack trace, null if absent |
| 7659 | */ |
| 7660 | public void addErrorToDropBox(String eventType, |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7661 | ProcessRecord process, String processName, ActivityRecord activity, |
| 7662 | ActivityRecord parent, String subject, |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7663 | final String report, final File logFile, |
| 7664 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 7665 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 7666 | // otherwise the watchdog may be prevented from resetting the system. |
| 7667 | |
| 7668 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 7669 | final DropBoxManager dbox = (DropBoxManager) |
| 7670 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 7671 | |
| 7672 | // Exit early if the dropbox isn't configured to accept this report type. |
| 7673 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 7674 | |
| 7675 | final StringBuilder sb = new StringBuilder(1024); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7676 | appendDropBoxProcessHeaders(process, processName, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7677 | if (activity != null) { |
| 7678 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 7679 | } |
| 7680 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 7681 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 7682 | } |
| 7683 | if (parent != null && parent != activity) { |
| 7684 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 7685 | } |
| 7686 | if (subject != null) { |
| 7687 | sb.append("Subject: ").append(subject).append("\n"); |
| 7688 | } |
| 7689 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 7690 | if (Debug.isDebuggerConnected()) { |
| 7691 | sb.append("Debugger: Connected\n"); |
| 7692 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7693 | sb.append("\n"); |
| 7694 | |
| 7695 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 7696 | // (After this point, we shouldn't access AMS internal data structures.) |
| 7697 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 7698 | @Override |
| 7699 | public void run() { |
| 7700 | if (report != null) { |
| 7701 | sb.append(report); |
| 7702 | } |
| 7703 | if (logFile != null) { |
| 7704 | try { |
| 7705 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 7706 | } catch (IOException e) { |
| 7707 | Slog.e(TAG, "Error reading " + logFile, e); |
| 7708 | } |
| 7709 | } |
| 7710 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 7711 | sb.append(crashInfo.stackTrace); |
| 7712 | } |
| 7713 | |
| 7714 | String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 7715 | int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0); |
| 7716 | if (lines > 0) { |
| 7717 | sb.append("\n"); |
| 7718 | |
| 7719 | // Merge several logcat streams, and take the last N lines |
| 7720 | InputStreamReader input = null; |
| 7721 | try { |
| 7722 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 7723 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 7724 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 7725 | |
| 7726 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 7727 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 7728 | input = new InputStreamReader(logcat.getInputStream()); |
| 7729 | |
| 7730 | int num; |
| 7731 | char[] buf = new char[8192]; |
| 7732 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 7733 | } catch (IOException e) { |
| 7734 | Slog.e(TAG, "Error running logcat", e); |
| 7735 | } finally { |
| 7736 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 7737 | } |
| 7738 | } |
| 7739 | |
| 7740 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7741 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 7742 | }; |
| 7743 | |
| 7744 | if (process == null || process.pid == MY_PID) { |
| 7745 | worker.run(); // We may be about to die -- need to run this synchronously |
| 7746 | } else { |
| 7747 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7748 | } |
| 7749 | } |
| 7750 | |
| 7751 | /** |
| 7752 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 7753 | * Deal with edge cases (intercepts from instrumented applications, |
| 7754 | * ActivityController, error intent receivers, that sort of thing). |
| 7755 | * @param r the application crashing |
| 7756 | * @param crashInfo describing the failure |
| 7757 | */ |
| 7758 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7759 | long timeMillis = System.currentTimeMillis(); |
| 7760 | String shortMsg = crashInfo.exceptionClassName; |
| 7761 | String longMsg = crashInfo.exceptionMessage; |
| 7762 | String stackTrace = crashInfo.stackTrace; |
| 7763 | if (shortMsg != null && longMsg != null) { |
| 7764 | longMsg = shortMsg + ": " + longMsg; |
| 7765 | } else if (shortMsg != null) { |
| 7766 | longMsg = shortMsg; |
| 7767 | } |
| 7768 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7769 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7770 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7771 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7772 | try { |
| 7773 | String name = r != null ? r.processName : null; |
| 7774 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7775 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7776 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7777 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7778 | + " at watcher's request"); |
| 7779 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7780 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7781 | } |
| 7782 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7783 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7784 | } |
| 7785 | } |
| 7786 | |
| 7787 | final long origId = Binder.clearCallingIdentity(); |
| 7788 | |
| 7789 | // If this process is running instrumentation, finish it. |
| 7790 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7791 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7792 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7793 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 7794 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7795 | Bundle info = new Bundle(); |
| 7796 | info.putString("shortMsg", shortMsg); |
| 7797 | info.putString("longMsg", longMsg); |
| 7798 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 7799 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7800 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7801 | } |
| 7802 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7803 | // If we can't identify the process or it's already exceeded its crash quota, |
| 7804 | // quit right away without showing a crash dialog. |
| 7805 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7806 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7807 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7808 | } |
| 7809 | |
| 7810 | Message msg = Message.obtain(); |
| 7811 | msg.what = SHOW_ERROR_MSG; |
| 7812 | HashMap data = new HashMap(); |
| 7813 | data.put("result", result); |
| 7814 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7815 | msg.obj = data; |
| 7816 | mHandler.sendMessage(msg); |
| 7817 | |
| 7818 | Binder.restoreCallingIdentity(origId); |
| 7819 | } |
| 7820 | |
| 7821 | int res = result.get(); |
| 7822 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7823 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7824 | synchronized (this) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7825 | if (r != null && !r.isolated) { |
| 7826 | // XXX Can't keep track of crash time for isolated processes, |
| 7827 | // since they don't have a persistent identity. |
| 7828 | mProcessCrashTimes.put(r.info.processName, r.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7829 | SystemClock.uptimeMillis()); |
| 7830 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7831 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7832 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7833 | } |
| 7834 | } |
| 7835 | |
| 7836 | if (appErrorIntent != null) { |
| 7837 | try { |
| 7838 | mContext.startActivity(appErrorIntent); |
| 7839 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7840 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7841 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7842 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7843 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7844 | |
| 7845 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 7846 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 7847 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7848 | if (report == null) { |
| 7849 | return null; |
| 7850 | } |
| 7851 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 7852 | result.setComponent(r.errorReportReceiver); |
| 7853 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 7854 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 7855 | return result; |
| 7856 | } |
| 7857 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7858 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 7859 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7860 | if (r.errorReportReceiver == null) { |
| 7861 | return null; |
| 7862 | } |
| 7863 | |
| 7864 | if (!r.crashing && !r.notResponding) { |
| 7865 | return null; |
| 7866 | } |
| 7867 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7868 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 7869 | report.packageName = r.info.packageName; |
| 7870 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 7871 | report.processName = r.processName; |
| 7872 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 7873 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7874 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7875 | if (r.crashing) { |
| 7876 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 7877 | report.crashInfo = crashInfo; |
| 7878 | } else if (r.notResponding) { |
| 7879 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 7880 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7881 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7882 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 7883 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 7884 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7885 | } |
| 7886 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7887 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 7888 | } |
| 7889 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7890 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 7891 | enforceNotIsolatedCaller("getProcessesInErrorState"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7892 | // assume our apps are happy - lazy create the list |
| 7893 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 7894 | |
| 7895 | synchronized (this) { |
| 7896 | |
| 7897 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7898 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 7899 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7900 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 7901 | // This one's in trouble, so we'll generate a report for it |
| 7902 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 7903 | ActivityManager.ProcessErrorStateInfo report = null; |
| 7904 | if (app.crashing) { |
| 7905 | report = app.crashingReport; |
| 7906 | } else if (app.notResponding) { |
| 7907 | report = app.notRespondingReport; |
| 7908 | } |
| 7909 | |
| 7910 | if (report != null) { |
| 7911 | if (errList == null) { |
| 7912 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 7913 | } |
| 7914 | errList.add(report); |
| 7915 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7916 | 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] | 7917 | " crashing = " + app.crashing + |
| 7918 | " notResponding = " + app.notResponding); |
| 7919 | } |
| 7920 | } |
| 7921 | } |
| 7922 | } |
| 7923 | |
| 7924 | return errList; |
| 7925 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7926 | |
| 7927 | static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) { |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7928 | if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7929 | if (currApp != null) { |
| 7930 | currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
| 7931 | } |
| 7932 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7933 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 7934 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7935 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 7936 | if (currApp != null) { |
| 7937 | currApp.lru = 0; |
| 7938 | } |
| 7939 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7940 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7941 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 7942 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 7943 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 7944 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 7945 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 7946 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 7947 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 7948 | } else { |
| 7949 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 7950 | } |
| 7951 | } |
| 7952 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7953 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 7954 | enforceNotIsolatedCaller("getRunningAppProcesses"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7955 | // Lazy instantiation of list |
| 7956 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
| 7957 | synchronized (this) { |
| 7958 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 7959 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 7960 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7961 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 7962 | // Generate process state info for running application |
| 7963 | ActivityManager.RunningAppProcessInfo currApp = |
| 7964 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 7965 | app.pid, app.getPackageList()); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 7966 | currApp.uid = app.info.uid; |
Dianne Hackborn | baf42c6 | 2010-06-24 11:23:39 -0700 | [diff] [blame] | 7967 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 482566e | 2010-09-03 12:51:28 -0700 | [diff] [blame] | 7968 | currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
Dianne Hackborn | baf42c6 | 2010-06-24 11:23:39 -0700 | [diff] [blame] | 7969 | } |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 7970 | if (app.persistent) { |
| 7971 | currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 7972 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7973 | int adj = app.curAdj; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7974 | currApp.importance = oomAdjToImportance(adj, currApp); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 7975 | currApp.importanceReasonCode = app.adjTypeCode; |
| 7976 | if (app.adjSource instanceof ProcessRecord) { |
| 7977 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 7978 | currApp.importanceReasonImportance = oomAdjToImportance( |
| 7979 | app.adjSourceOom, null); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7980 | } else if (app.adjSource instanceof ActivityRecord) { |
| 7981 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 7982 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 7983 | } |
| 7984 | if (app.adjTarget instanceof ComponentName) { |
| 7985 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 7986 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7987 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7988 | // + " lru=" + currApp.lru); |
| 7989 | if (runList == null) { |
| 7990 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 7991 | } |
| 7992 | runList.add(currApp); |
| 7993 | } |
| 7994 | } |
| 7995 | } |
| 7996 | return runList; |
| 7997 | } |
| 7998 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 7999 | public List<ApplicationInfo> getRunningExternalApplications() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8000 | enforceNotIsolatedCaller("getRunningExternalApplications"); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8001 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 8002 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 8003 | if (runningApps != null && runningApps.size() > 0) { |
| 8004 | Set<String> extList = new HashSet<String>(); |
| 8005 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 8006 | if (app.pkgList != null) { |
| 8007 | for (String pkg : app.pkgList) { |
| 8008 | extList.add(pkg); |
| 8009 | } |
| 8010 | } |
| 8011 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8012 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8013 | for (String pkg : extList) { |
| 8014 | try { |
| 8015 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0); |
| 8016 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 8017 | retList.add(info); |
| 8018 | } |
| 8019 | } catch (RemoteException e) { |
| 8020 | } |
| 8021 | } |
| 8022 | } |
| 8023 | return retList; |
| 8024 | } |
| 8025 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8026 | @Override |
| 8027 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8028 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 8029 | != PackageManager.PERMISSION_GRANTED) { |
| 8030 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 8031 | + Binder.getCallingPid() |
| 8032 | + ", uid=" + Binder.getCallingUid() |
| 8033 | + " without permission " |
| 8034 | + android.Manifest.permission.DUMP); |
| 8035 | return; |
| 8036 | } |
| 8037 | |
| 8038 | boolean dumpAll = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8039 | boolean dumpClient = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8040 | String dumpPackage = null; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8041 | |
| 8042 | int opti = 0; |
| 8043 | while (opti < args.length) { |
| 8044 | String opt = args[opti]; |
| 8045 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 8046 | break; |
| 8047 | } |
| 8048 | opti++; |
| 8049 | if ("-a".equals(opt)) { |
| 8050 | dumpAll = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8051 | } else if ("-c".equals(opt)) { |
| 8052 | dumpClient = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8053 | } else if ("-h".equals(opt)) { |
| 8054 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8055 | pw.println(" [-a] [-c] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8056 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8057 | pw.println(" a[ctivities]: activity stack state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8058 | pw.println(" b[roadcasts] [PACKAGE_NAME]: broadcast state"); |
| 8059 | pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state"); |
| 8060 | pw.println(" p[rocesses] [PACKAGE_NAME]: process state"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8061 | pw.println(" o[om]: out of memory management"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8062 | pw.println(" prov[iders] [COMP_SPEC ...]: content provider state"); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8063 | pw.println(" provider [COMP_SPEC]: provider client-side state"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8064 | pw.println(" s[ervices] [COMP_SPEC ...]: service state"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8065 | pw.println(" service [COMP_SPEC]: service client-side state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8066 | pw.println(" package [PACKAGE_NAME]: all state related to given package"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8067 | pw.println(" all: dump all activities"); |
| 8068 | pw.println(" top: dump the top activity"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8069 | pw.println(" cmd may also be a COMP_SPEC to dump activities."); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8070 | pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),"); |
| 8071 | pw.println(" a partial substring in a component name, a"); |
| 8072 | pw.println(" hex object identifier."); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8073 | pw.println(" -a: include all available server state."); |
| 8074 | pw.println(" -c: include client state."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8075 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8076 | } else { |
| 8077 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8078 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8079 | } |
| 8080 | |
| 8081 | // Is the caller requesting to dump a particular piece of data? |
| 8082 | if (opti < args.length) { |
| 8083 | String cmd = args[opti]; |
| 8084 | opti++; |
| 8085 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 8086 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8087 | dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8088 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8089 | return; |
| 8090 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8091 | String[] newArgs; |
| 8092 | String name; |
| 8093 | if (opti >= args.length) { |
| 8094 | name = null; |
| 8095 | newArgs = EMPTY_STRING_ARRAY; |
| 8096 | } else { |
| 8097 | name = args[opti]; |
| 8098 | opti++; |
| 8099 | newArgs = new String[args.length - opti]; |
| 8100 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8101 | args.length - opti); |
| 8102 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8103 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8104 | dumpBroadcastsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8105 | } |
| 8106 | return; |
| 8107 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8108 | String[] newArgs; |
| 8109 | String name; |
| 8110 | if (opti >= args.length) { |
| 8111 | name = null; |
| 8112 | newArgs = EMPTY_STRING_ARRAY; |
| 8113 | } else { |
| 8114 | name = args[opti]; |
| 8115 | opti++; |
| 8116 | newArgs = new String[args.length - opti]; |
| 8117 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8118 | args.length - opti); |
| 8119 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8120 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8121 | dumpPendingIntentsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8122 | } |
| 8123 | return; |
| 8124 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8125 | String[] newArgs; |
| 8126 | String name; |
| 8127 | if (opti >= args.length) { |
| 8128 | name = null; |
| 8129 | newArgs = EMPTY_STRING_ARRAY; |
| 8130 | } else { |
| 8131 | name = args[opti]; |
| 8132 | opti++; |
| 8133 | newArgs = new String[args.length - opti]; |
| 8134 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8135 | args.length - opti); |
| 8136 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8137 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8138 | dumpProcessesLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8139 | } |
| 8140 | return; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8141 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 8142 | synchronized (this) { |
| 8143 | dumpOomLocked(fd, pw, args, opti, true); |
| 8144 | } |
| 8145 | return; |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8146 | } else if ("provider".equals(cmd)) { |
| 8147 | String[] newArgs; |
| 8148 | String name; |
| 8149 | if (opti >= args.length) { |
| 8150 | name = null; |
| 8151 | newArgs = EMPTY_STRING_ARRAY; |
| 8152 | } else { |
| 8153 | name = args[opti]; |
| 8154 | opti++; |
| 8155 | newArgs = new String[args.length - opti]; |
| 8156 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 8157 | } |
| 8158 | if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) { |
| 8159 | pw.println("No providers match: " + name); |
| 8160 | pw.println("Use -h for help."); |
| 8161 | } |
| 8162 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8163 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 8164 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8165 | dumpProvidersLocked(fd, pw, args, opti, true, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8166 | } |
| 8167 | return; |
| 8168 | } else if ("service".equals(cmd)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8169 | String[] newArgs; |
| 8170 | String name; |
| 8171 | if (opti >= args.length) { |
| 8172 | name = null; |
| 8173 | newArgs = EMPTY_STRING_ARRAY; |
| 8174 | } else { |
| 8175 | name = args[opti]; |
| 8176 | opti++; |
| 8177 | newArgs = new String[args.length - opti]; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8178 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8179 | args.length - opti); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8180 | } |
| 8181 | if (!dumpService(fd, pw, name, newArgs, 0, dumpAll)) { |
| 8182 | pw.println("No services match: " + name); |
| 8183 | pw.println("Use -h for help."); |
| 8184 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8185 | return; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8186 | } else if ("package".equals(cmd)) { |
| 8187 | String[] newArgs; |
| 8188 | if (opti >= args.length) { |
| 8189 | pw.println("package: no package name specified"); |
| 8190 | pw.println("Use -h for help."); |
| 8191 | return; |
| 8192 | } else { |
| 8193 | dumpPackage = args[opti]; |
| 8194 | opti++; |
| 8195 | newArgs = new String[args.length - opti]; |
| 8196 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8197 | args.length - opti); |
| 8198 | args = newArgs; |
| 8199 | opti = 0; |
| 8200 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8201 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 8202 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8203 | dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8204 | } |
| 8205 | return; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8206 | } else { |
| 8207 | // Dumping a single activity? |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8208 | if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 8209 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 8210 | pw.println("Use -h for help."); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8211 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8212 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8213 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8214 | } |
| 8215 | |
| 8216 | // No piece of data specified, dump everything. |
| 8217 | synchronized (this) { |
| 8218 | boolean needSep; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8219 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8220 | if (needSep) { |
| 8221 | pw.println(" "); |
| 8222 | } |
| 8223 | if (dumpAll) { |
| 8224 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8225 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8226 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8227 | if (needSep) { |
| 8228 | pw.println(" "); |
| 8229 | } |
| 8230 | if (dumpAll) { |
| 8231 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8232 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8233 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8234 | if (needSep) { |
| 8235 | pw.println(" "); |
| 8236 | } |
| 8237 | if (dumpAll) { |
| 8238 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8239 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8240 | needSep = dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8241 | if (needSep) { |
| 8242 | pw.println(" "); |
| 8243 | } |
| 8244 | if (dumpAll) { |
| 8245 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8246 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8247 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8248 | if (needSep) { |
| 8249 | pw.println(" "); |
| 8250 | } |
| 8251 | if (dumpAll) { |
| 8252 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8253 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8254 | dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8255 | } |
| 8256 | } |
| 8257 | |
| 8258 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8259 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8260 | pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)"); |
| 8261 | pw.println(" Main stack:"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8262 | dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient, |
| 8263 | dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8264 | pw.println(" "); |
| 8265 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8266 | dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false, |
| 8267 | dumpPackage); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8268 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8269 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8270 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8271 | dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8272 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8273 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8274 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8275 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8276 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8277 | dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8278 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8279 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 8280 | if (mMainStack.mGoingToSleepActivities.size() > 0) { |
| 8281 | pw.println(" "); |
| 8282 | pw.println(" Activities waiting to sleep:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8283 | dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8284 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 8285 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8286 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8287 | pw.println(" "); |
| 8288 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8289 | dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8290 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8291 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8292 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8293 | pw.println(" "); |
Dianne Hackborn | cbb722e | 2012-02-07 18:33:49 -0800 | [diff] [blame] | 8294 | if (mMainStack.mPausingActivities.size() > 0) { |
| 8295 | pw.println(" mPausingActivities: " + Arrays.toString( |
| 8296 | mMainStack.mPausingActivities.toArray())); |
| 8297 | } |
| 8298 | if (mMainStack.mInputPausedActivities.size() > 0) { |
| 8299 | pw.println(" mInputPausedActivities: " + Arrays.toString( |
| 8300 | mMainStack.mInputPausedActivities.toArray())); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8301 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8302 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8303 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8304 | if (dumpAll) { |
| 8305 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
| 8306 | pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 8307 | pw.println(" mDismissKeyguardOnNextActivity: " |
| 8308 | + mMainStack.mDismissKeyguardOnNextActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8309 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8310 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8311 | if (mRecentTasks.size() > 0) { |
| 8312 | pw.println(); |
| 8313 | pw.println(" Recent tasks:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8314 | |
| 8315 | final int N = mRecentTasks.size(); |
| 8316 | for (int i=0; i<N; i++) { |
| 8317 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8318 | if (dumpPackage != null) { |
| 8319 | if (tr.realActivity == null || |
| 8320 | !dumpPackage.equals(tr.realActivity)) { |
| 8321 | continue; |
| 8322 | } |
| 8323 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8324 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 8325 | pw.println(tr); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8326 | if (dumpAll) { |
| 8327 | mRecentTasks.get(i).dump(pw, " "); |
| 8328 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8329 | } |
| 8330 | } |
| 8331 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8332 | if (dumpAll) { |
| 8333 | pw.println(" "); |
| 8334 | pw.println(" mCurTask: " + mCurTask); |
| 8335 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8336 | |
| 8337 | return true; |
| 8338 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8339 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8340 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8341 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8342 | boolean needSep = false; |
| 8343 | int numPers = 0; |
| 8344 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8345 | pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)"); |
| 8346 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8347 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8348 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 8349 | final int NA = procs.size(); |
| 8350 | for (int ia=0; ia<NA; ia++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8351 | ProcessRecord r = procs.valueAt(ia); |
| 8352 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8353 | continue; |
| 8354 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8355 | if (!needSep) { |
| 8356 | pw.println(" All known processes:"); |
| 8357 | needSep = true; |
| 8358 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8359 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 8360 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 8361 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8362 | r.dump(pw, " "); |
| 8363 | if (r.persistent) { |
| 8364 | numPers++; |
| 8365 | } |
| 8366 | } |
| 8367 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8368 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8369 | |
| 8370 | if (mIsolatedProcesses.size() > 0) { |
| 8371 | if (needSep) pw.println(" "); |
| 8372 | needSep = true; |
| 8373 | pw.println(" Isolated process list (sorted by uid):"); |
| 8374 | for (int i=0; i<mIsolatedProcesses.size(); i++) { |
| 8375 | ProcessRecord r = mIsolatedProcesses.valueAt(i); |
| 8376 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8377 | continue; |
| 8378 | } |
| 8379 | pw.println(String.format("%sIsolated #%2d: %s", |
| 8380 | " ", i, r.toString())); |
| 8381 | } |
| 8382 | } |
| 8383 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8384 | if (mLruProcesses.size() > 0) { |
| 8385 | if (needSep) pw.println(" "); |
| 8386 | needSep = true; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8387 | pw.println(" Process LRU list (sorted by oom_adj):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8388 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8389 | "Proc", "PERS", false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8390 | needSep = true; |
| 8391 | } |
| 8392 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8393 | if (dumpAll) { |
| 8394 | synchronized (mPidsSelfLocked) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8395 | boolean printed = false; |
| 8396 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 8397 | ProcessRecord r = mPidsSelfLocked.valueAt(i); |
| 8398 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8399 | continue; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8400 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8401 | if (!printed) { |
| 8402 | if (needSep) pw.println(" "); |
| 8403 | needSep = true; |
| 8404 | pw.println(" PID mappings:"); |
| 8405 | printed = true; |
| 8406 | } |
| 8407 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 8408 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8409 | } |
| 8410 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8411 | } |
| 8412 | |
| 8413 | if (mForegroundProcesses.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8414 | synchronized (mPidsSelfLocked) { |
| 8415 | boolean printed = false; |
| 8416 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 8417 | ProcessRecord r = mPidsSelfLocked.get( |
| 8418 | mForegroundProcesses.valueAt(i).pid); |
| 8419 | if (dumpPackage != null && (r == null |
| 8420 | || !dumpPackage.equals(r.info.packageName))) { |
| 8421 | continue; |
| 8422 | } |
| 8423 | if (!printed) { |
| 8424 | if (needSep) pw.println(" "); |
| 8425 | needSep = true; |
| 8426 | pw.println(" Foreground Processes:"); |
| 8427 | printed = true; |
| 8428 | } |
| 8429 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 8430 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
| 8431 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8432 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8433 | } |
| 8434 | |
| 8435 | if (mPersistentStartingProcesses.size() > 0) { |
| 8436 | if (needSep) pw.println(" "); |
| 8437 | needSep = true; |
| 8438 | pw.println(" Persisent processes that are starting:"); |
| 8439 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8440 | "Starting Norm", "Restarting PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8441 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8442 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8443 | if (mRemovedProcesses.size() > 0) { |
| 8444 | if (needSep) pw.println(" "); |
| 8445 | needSep = true; |
| 8446 | pw.println(" Processes that are being removed:"); |
| 8447 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8448 | "Removed Norm", "Removed PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8449 | } |
| 8450 | |
| 8451 | if (mProcessesOnHold.size() > 0) { |
| 8452 | if (needSep) pw.println(" "); |
| 8453 | needSep = true; |
| 8454 | pw.println(" Processes that are on old until the system is ready:"); |
| 8455 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8456 | "OnHold Norm", "OnHold PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8457 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8458 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8459 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8460 | |
| 8461 | if (mProcessCrashTimes.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8462 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8463 | long now = SystemClock.uptimeMillis(); |
| 8464 | for (Map.Entry<String, SparseArray<Long>> procs |
| 8465 | : mProcessCrashTimes.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8466 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8467 | SparseArray<Long> uids = procs.getValue(); |
| 8468 | final int N = uids.size(); |
| 8469 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8470 | int puid = uids.keyAt(i); |
| 8471 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 8472 | if (dumpPackage != null && (r == null |
| 8473 | || !dumpPackage.equals(r.info.packageName))) { |
| 8474 | continue; |
| 8475 | } |
| 8476 | if (!printed) { |
| 8477 | if (needSep) pw.println(" "); |
| 8478 | needSep = true; |
| 8479 | pw.println(" Time since processes crashed:"); |
| 8480 | printed = true; |
| 8481 | } |
| 8482 | pw.print(" Process "); pw.print(pname); |
| 8483 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8484 | pw.print(": last crashed "); |
| 8485 | pw.print((now-uids.valueAt(i))); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 8486 | pw.println(" ms ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 8487 | } |
| 8488 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8489 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8490 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8491 | if (mBadProcesses.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8492 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8493 | for (Map.Entry<String, SparseArray<Long>> procs |
| 8494 | : mBadProcesses.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8495 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8496 | SparseArray<Long> uids = procs.getValue(); |
| 8497 | final int N = uids.size(); |
| 8498 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8499 | int puid = uids.keyAt(i); |
| 8500 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 8501 | if (dumpPackage != null && (r == null |
| 8502 | || !dumpPackage.equals(r.info.packageName))) { |
| 8503 | continue; |
| 8504 | } |
| 8505 | if (!printed) { |
| 8506 | if (needSep) pw.println(" "); |
| 8507 | needSep = true; |
| 8508 | pw.println(" Bad processes:"); |
| 8509 | } |
| 8510 | pw.print(" Bad process "); pw.print(pname); |
| 8511 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8512 | pw.print(": crashed at time "); |
| 8513 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8514 | } |
| 8515 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8516 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8517 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8518 | pw.println(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8519 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 8520 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 8521 | if (dumpAll) { |
| 8522 | StringBuilder sb = new StringBuilder(128); |
| 8523 | sb.append(" mPreviousProcessVisibleTime: "); |
| 8524 | TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb); |
| 8525 | pw.println(sb); |
| 8526 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8527 | if (mHeavyWeightProcess != null) { |
| 8528 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 8529 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8530 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8531 | if (dumpAll) { |
| 8532 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | 3d0724d | 2011-05-12 15:39:41 -0700 | [diff] [blame] | 8533 | if (mCompatModePackages.getPackages().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8534 | boolean printed = false; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 8535 | for (Map.Entry<String, Integer> entry |
| 8536 | : mCompatModePackages.getPackages().entrySet()) { |
| 8537 | String pkg = entry.getKey(); |
| 8538 | int mode = entry.getValue(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8539 | if (dumpPackage != null && !dumpPackage.equals(pkg)) { |
| 8540 | continue; |
| 8541 | } |
| 8542 | if (!printed) { |
| 8543 | pw.println(" mScreenCompatPackages:"); |
| 8544 | printed = true; |
| 8545 | } |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 8546 | pw.print(" "); pw.print(pkg); pw.print(": "); |
| 8547 | pw.print(mode); pw.println(); |
| 8548 | } |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 8549 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8550 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8551 | pw.println(" mSleeping=" + mSleeping + " mShuttingDown=" + mShuttingDown); |
| 8552 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 8553 | || mOrigWaitForDebugger) { |
| 8554 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 8555 | + " mDebugTransient=" + mDebugTransient |
| 8556 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 8557 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 8558 | if (mProfileApp != null || mProfileProc != null || mProfileFile != null |
| 8559 | || mProfileFd != null) { |
| 8560 | pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc); |
| 8561 | pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd); |
| 8562 | pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler=" |
| 8563 | + mAutoStopProfiler); |
| 8564 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8565 | if (mAlwaysFinishActivities || mController != null) { |
| 8566 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 8567 | + " mController=" + mController); |
| 8568 | } |
| 8569 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8570 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8571 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 8572 | + " mProcessesReady=" + mProcessesReady |
| 8573 | + " mSystemReady=" + mSystemReady); |
| 8574 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8575 | + " mBooted=" + mBooted |
| 8576 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8577 | pw.print(" mLastPowerCheckRealtime="); |
| 8578 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 8579 | pw.println(""); |
| 8580 | pw.print(" mLastPowerCheckUptime="); |
| 8581 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 8582 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8583 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 8584 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 8585 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8586 | pw.println(" mNumServiceProcs=" + mNumServiceProcs |
| 8587 | + " mNewNumServiceProcs=" + mNewNumServiceProcs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8588 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8589 | |
| 8590 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8591 | } |
| 8592 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8593 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8594 | int opti, boolean needSep, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8595 | if (mProcessesToGc.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8596 | boolean printed = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8597 | long now = SystemClock.uptimeMillis(); |
| 8598 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 8599 | ProcessRecord proc = mProcessesToGc.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8600 | if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) { |
| 8601 | continue; |
| 8602 | } |
| 8603 | if (!printed) { |
| 8604 | if (needSep) pw.println(" "); |
| 8605 | needSep = true; |
| 8606 | pw.println(" Processes that are waiting to GC:"); |
| 8607 | printed = true; |
| 8608 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8609 | pw.print(" Process "); pw.println(proc); |
| 8610 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 8611 | pw.print(", last gced="); |
| 8612 | pw.print(now-proc.lastRequestedGc); |
| 8613 | pw.print(" ms ago, last lowMem="); |
| 8614 | pw.print(now-proc.lastLowMemory); |
| 8615 | pw.println(" ms ago"); |
| 8616 | |
| 8617 | } |
| 8618 | } |
| 8619 | return needSep; |
| 8620 | } |
| 8621 | |
| 8622 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 8623 | int opti, boolean dumpAll) { |
| 8624 | boolean needSep = false; |
| 8625 | |
| 8626 | if (mLruProcesses.size() > 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8627 | if (needSep) pw.println(" "); |
| 8628 | needSep = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 8629 | pw.println(" OOM levels:"); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8630 | pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8631 | pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8632 | pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ); |
| 8633 | pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ); |
| 8634 | pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ); |
| 8635 | pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 8636 | pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8637 | pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8638 | pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 8639 | pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8640 | pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8641 | 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] | 8642 | 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] | 8643 | |
| 8644 | if (needSep) pw.println(" "); |
| 8645 | needSep = true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8646 | pw.println(" Process OOM control:"); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8647 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8648 | "Proc", "PERS", true, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8649 | needSep = true; |
| 8650 | } |
| 8651 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8652 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8653 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8654 | pw.println(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8655 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 8656 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8657 | if (mHeavyWeightProcess != null) { |
| 8658 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 8659 | } |
| 8660 | |
| 8661 | return true; |
| 8662 | } |
| 8663 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8664 | /** |
| 8665 | * There are three ways to call this: |
| 8666 | * - no service specified: dump all the services |
| 8667 | * - a flattened component name that matched an existing service was specified as the |
| 8668 | * first arg: dump that one service |
| 8669 | * - the first arg isn't the flattened component name of an existing service: |
| 8670 | * dump all services whose component contains the first arg as a substring |
| 8671 | */ |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8672 | protected boolean dumpService(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 8673 | int opti, boolean dumpAll) { |
| 8674 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8675 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8676 | if ("all".equals(name)) { |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 8677 | synchronized (this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 8678 | try { |
| 8679 | List<UserInfo> users = AppGlobals.getPackageManager().getUsers(); |
| 8680 | for (UserInfo user : users) { |
| 8681 | for (ServiceRecord r1 : mServiceMap.getAllServices(user.id)) { |
| 8682 | services.add(r1); |
| 8683 | } |
| 8684 | } |
| 8685 | } catch (RemoteException re) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8686 | } |
| 8687 | } |
| 8688 | } else { |
| 8689 | ComponentName componentName = name != null |
| 8690 | ? ComponentName.unflattenFromString(name) : null; |
| 8691 | int objectId = 0; |
| 8692 | if (componentName == null) { |
| 8693 | // Not a '/' separated full component name; maybe an object ID? |
| 8694 | try { |
| 8695 | objectId = Integer.parseInt(name, 16); |
| 8696 | name = null; |
| 8697 | componentName = null; |
| 8698 | } catch (RuntimeException e) { |
| 8699 | } |
| 8700 | } |
| 8701 | |
| 8702 | synchronized (this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 8703 | try { |
| 8704 | List<UserInfo> users = AppGlobals.getPackageManager().getUsers(); |
| 8705 | for (UserInfo user : users) { |
| 8706 | for (ServiceRecord r1 : mServiceMap.getAllServices(user.id)) { |
| 8707 | if (componentName != null) { |
| 8708 | if (r1.name.equals(componentName)) { |
| 8709 | services.add(r1); |
| 8710 | } |
| 8711 | } else if (name != null) { |
| 8712 | if (r1.name.flattenToString().contains(name)) { |
| 8713 | services.add(r1); |
| 8714 | } |
| 8715 | } else if (System.identityHashCode(r1) == objectId) { |
| 8716 | services.add(r1); |
| 8717 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8718 | } |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 8719 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 8720 | } catch (RemoteException re) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8721 | } |
| 8722 | } |
| 8723 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8724 | |
| 8725 | if (services.size() <= 0) { |
| 8726 | return false; |
| 8727 | } |
| 8728 | |
| 8729 | boolean needSep = false; |
| 8730 | for (int i=0; i<services.size(); i++) { |
| 8731 | if (needSep) { |
| 8732 | pw.println(); |
| 8733 | } |
| 8734 | needSep = true; |
| 8735 | dumpService("", fd, pw, services.get(i), args, dumpAll); |
| 8736 | } |
| 8737 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8738 | } |
| 8739 | |
| 8740 | /** |
| 8741 | * Invokes IApplicationThread.dumpService() on the thread of the specified service if |
| 8742 | * there is a thread associated with the service. |
| 8743 | */ |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8744 | private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw, |
| 8745 | final ServiceRecord r, String[] args, boolean dumpAll) { |
| 8746 | String innerPrefix = prefix + " "; |
| 8747 | synchronized (this) { |
| 8748 | pw.print(prefix); pw.print("SERVICE "); |
| 8749 | pw.print(r.shortName); pw.print(" "); |
| 8750 | pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 8751 | pw.print(" pid="); |
| 8752 | if (r.app != null) pw.println(r.app.pid); |
| 8753 | else pw.println("(not running)"); |
| 8754 | if (dumpAll) { |
| 8755 | r.dump(pw, innerPrefix); |
| 8756 | } |
| 8757 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8758 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8759 | pw.print(prefix); pw.println(" Client:"); |
| 8760 | pw.flush(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8761 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8762 | TransferPipe tp = new TransferPipe(); |
| 8763 | try { |
| 8764 | r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), r, args); |
| 8765 | tp.setBufferPrefix(prefix + " "); |
| 8766 | tp.go(fd); |
| 8767 | } finally { |
| 8768 | tp.kill(); |
| 8769 | } |
| 8770 | } catch (IOException e) { |
| 8771 | pw.println(prefix + " Failure while dumping the service: " + e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8772 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8773 | 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] | 8774 | } |
| 8775 | } |
| 8776 | } |
| 8777 | |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8778 | /** |
| 8779 | * There are three ways to call this: |
| 8780 | * - no provider specified: dump all the providers |
| 8781 | * - a flattened component name that matched an existing provider was specified as the |
| 8782 | * first arg: dump that one provider |
| 8783 | * - the first arg isn't the flattened component name of an existing provider: |
| 8784 | * dump all providers whose component contains the first arg as a substring |
| 8785 | */ |
| 8786 | protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 8787 | int opti, boolean dumpAll) { |
Marco Nelissen | de7408c | 2012-02-08 14:57:38 -0800 | [diff] [blame] | 8788 | return mProviderMap.dumpProvider(fd, pw, name, args, opti, dumpAll); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8789 | } |
| 8790 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8791 | static class ItemMatcher { |
| 8792 | ArrayList<ComponentName> components; |
| 8793 | ArrayList<String> strings; |
| 8794 | ArrayList<Integer> objects; |
| 8795 | boolean all; |
| 8796 | |
| 8797 | ItemMatcher() { |
| 8798 | all = true; |
| 8799 | } |
| 8800 | |
| 8801 | void build(String name) { |
| 8802 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 8803 | if (componentName != null) { |
| 8804 | if (components == null) { |
| 8805 | components = new ArrayList<ComponentName>(); |
| 8806 | } |
| 8807 | components.add(componentName); |
| 8808 | all = false; |
| 8809 | } else { |
| 8810 | int objectId = 0; |
| 8811 | // Not a '/' separated full component name; maybe an object ID? |
| 8812 | try { |
| 8813 | objectId = Integer.parseInt(name, 16); |
| 8814 | if (objects == null) { |
| 8815 | objects = new ArrayList<Integer>(); |
| 8816 | } |
| 8817 | objects.add(objectId); |
| 8818 | all = false; |
| 8819 | } catch (RuntimeException e) { |
| 8820 | // Not an integer; just do string match. |
| 8821 | if (strings == null) { |
| 8822 | strings = new ArrayList<String>(); |
| 8823 | } |
| 8824 | strings.add(name); |
| 8825 | all = false; |
| 8826 | } |
| 8827 | } |
| 8828 | } |
| 8829 | |
| 8830 | int build(String[] args, int opti) { |
| 8831 | for (; opti<args.length; opti++) { |
| 8832 | String name = args[opti]; |
| 8833 | if ("--".equals(name)) { |
| 8834 | return opti+1; |
| 8835 | } |
| 8836 | build(name); |
| 8837 | } |
| 8838 | return opti; |
| 8839 | } |
| 8840 | |
| 8841 | boolean match(Object object, ComponentName comp) { |
| 8842 | if (all) { |
| 8843 | return true; |
| 8844 | } |
| 8845 | if (components != null) { |
| 8846 | for (int i=0; i<components.size(); i++) { |
| 8847 | if (components.get(i).equals(comp)) { |
| 8848 | return true; |
| 8849 | } |
| 8850 | } |
| 8851 | } |
| 8852 | if (objects != null) { |
| 8853 | for (int i=0; i<objects.size(); i++) { |
| 8854 | if (System.identityHashCode(object) == objects.get(i)) { |
| 8855 | return true; |
| 8856 | } |
| 8857 | } |
| 8858 | } |
| 8859 | if (strings != null) { |
| 8860 | String flat = comp.flattenToString(); |
| 8861 | for (int i=0; i<strings.size(); i++) { |
| 8862 | if (flat.contains(strings.get(i))) { |
| 8863 | return true; |
| 8864 | } |
| 8865 | } |
| 8866 | } |
| 8867 | return false; |
| 8868 | } |
| 8869 | } |
| 8870 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8871 | /** |
| 8872 | * There are three things that cmd can be: |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8873 | * - a flattened component name that matches an existing activity |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8874 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 8875 | * dump all activity whose component contains the cmd as a substring |
| 8876 | * - A hex number of the ActivityRecord object instance. |
| 8877 | */ |
| 8878 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 8879 | int opti, boolean dumpAll) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8880 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8881 | |
| 8882 | if ("all".equals(name)) { |
| 8883 | synchronized (this) { |
| 8884 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8885 | activities.add(r1); |
| 8886 | } |
| 8887 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 8888 | } else if ("top".equals(name)) { |
| 8889 | synchronized (this) { |
| 8890 | final int N = mMainStack.mHistory.size(); |
| 8891 | if (N > 0) { |
| 8892 | activities.add((ActivityRecord)mMainStack.mHistory.get(N-1)); |
| 8893 | } |
| 8894 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8895 | } else { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8896 | ItemMatcher matcher = new ItemMatcher(); |
| 8897 | matcher.build(name); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8898 | |
| 8899 | synchronized (this) { |
| 8900 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8901 | if (matcher.match(r1, r1.intent.getComponent())) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8902 | activities.add(r1); |
| 8903 | } |
| 8904 | } |
| 8905 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8906 | } |
| 8907 | |
| 8908 | if (activities.size() <= 0) { |
| 8909 | return false; |
| 8910 | } |
| 8911 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8912 | String[] newArgs = new String[args.length - opti]; |
| 8913 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 8914 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8915 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8916 | boolean needSep = false; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8917 | for (int i=activities.size()-1; i>=0; i--) { |
| 8918 | ActivityRecord r = (ActivityRecord)activities.get(i); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8919 | if (needSep) { |
| 8920 | pw.println(); |
| 8921 | } |
| 8922 | needSep = true; |
| 8923 | synchronized (this) { |
| 8924 | if (lastTask != r.task) { |
| 8925 | lastTask = r.task; |
| 8926 | pw.print("TASK "); pw.print(lastTask.affinity); |
| 8927 | pw.print(" id="); pw.println(lastTask.taskId); |
| 8928 | if (dumpAll) { |
| 8929 | lastTask.dump(pw, " "); |
| 8930 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8931 | } |
| 8932 | } |
| 8933 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8934 | } |
| 8935 | return true; |
| 8936 | } |
| 8937 | |
| 8938 | /** |
| 8939 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 8940 | * there is a thread associated with the activity. |
| 8941 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8942 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8943 | final ActivityRecord r, String[] args, boolean dumpAll) { |
| 8944 | String innerPrefix = prefix + " "; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8945 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8946 | pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName); |
| 8947 | pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 8948 | pw.print(" pid="); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8949 | if (r.app != null) pw.println(r.app.pid); |
| 8950 | else pw.println("(not running)"); |
| 8951 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8952 | r.dump(pw, innerPrefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8953 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8954 | } |
| 8955 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8956 | // flush anything that is already in the PrintWriter since the thread is going |
| 8957 | // to write to the file descriptor directly |
| 8958 | pw.flush(); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8959 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8960 | TransferPipe tp = new TransferPipe(); |
| 8961 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 8962 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 8963 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8964 | tp.go(fd); |
| 8965 | } finally { |
| 8966 | tp.kill(); |
| 8967 | } |
| 8968 | } catch (IOException e) { |
| 8969 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8970 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8971 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8972 | } |
| 8973 | } |
| 8974 | } |
| 8975 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8976 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8977 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8978 | boolean needSep = false; |
| 8979 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8980 | pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8981 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8982 | if (mRegisteredReceivers.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8983 | boolean printed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8984 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 8985 | while (it.hasNext()) { |
| 8986 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8987 | if (dumpPackage != null && (r.app == null || |
| 8988 | !dumpPackage.equals(r.app.info.packageName))) { |
| 8989 | continue; |
| 8990 | } |
| 8991 | if (!printed) { |
| 8992 | pw.println(" Registered Receivers:"); |
| 8993 | needSep = true; |
| 8994 | printed = true; |
| 8995 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8996 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8997 | r.dump(pw, " "); |
| 8998 | } |
| 8999 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9000 | |
| 9001 | if (mReceiverResolver.dump(pw, needSep ? |
| 9002 | "\n Receiver Resolver Table:" : " Receiver Resolver Table:", |
| 9003 | " ", dumpPackage, false)) { |
| 9004 | needSep = true; |
| 9005 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9006 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9007 | |
| 9008 | for (BroadcastQueue q : mBroadcastQueues) { |
| 9009 | needSep = q.dumpLocked(fd, pw, args, opti, dumpAll, dumpPackage, needSep); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9010 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9011 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9012 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9013 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9014 | if (mStickyBroadcasts != null && dumpPackage == null) { |
| 9015 | if (needSep) { |
| 9016 | pw.println(); |
| 9017 | } |
| 9018 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9019 | pw.println(" Sticky broadcasts:"); |
| 9020 | StringBuilder sb = new StringBuilder(128); |
| 9021 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 9022 | : mStickyBroadcasts.entrySet()) { |
| 9023 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9024 | if (dumpAll) { |
| 9025 | pw.println(":"); |
| 9026 | ArrayList<Intent> intents = ent.getValue(); |
| 9027 | final int N = intents.size(); |
| 9028 | for (int i=0; i<N; i++) { |
| 9029 | sb.setLength(0); |
| 9030 | sb.append(" Intent: "); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9031 | intents.get(i).toShortString(sb, false, true, false); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9032 | pw.println(sb.toString()); |
| 9033 | Bundle bundle = intents.get(i).getExtras(); |
| 9034 | if (bundle != null) { |
| 9035 | pw.print(" "); |
| 9036 | pw.println(bundle.toString()); |
| 9037 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9038 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9039 | } else { |
| 9040 | pw.println(""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9041 | } |
| 9042 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9043 | needSep = true; |
| 9044 | } |
| 9045 | |
| 9046 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9047 | pw.println(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9048 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 9049 | pw.println(" mBroadcastsScheduled [" + queue.mQueueName + "]=" |
| 9050 | + queue.mBroadcastsScheduled); |
| 9051 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9052 | pw.println(" mHandler:"); |
| 9053 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9054 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9055 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9056 | |
| 9057 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9058 | } |
| 9059 | |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9060 | /** |
| 9061 | * Prints a list of ServiceRecords (dumpsys activity services) |
| 9062 | */ |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9063 | boolean dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9064 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9065 | boolean needSep = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9066 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9067 | ItemMatcher matcher = new ItemMatcher(); |
| 9068 | matcher.build(args, opti); |
| 9069 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9070 | pw.println("ACTIVITY MANAGER SERVICES (dumpsys activity services)"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9071 | try { |
| 9072 | List<UserInfo> users = AppGlobals.getPackageManager().getUsers(); |
| 9073 | for (UserInfo user : users) { |
| 9074 | if (mServiceMap.getAllServices(user.id).size() > 0) { |
| 9075 | boolean printed = false; |
| 9076 | long nowReal = SystemClock.elapsedRealtime(); |
| 9077 | Iterator<ServiceRecord> it = mServiceMap.getAllServices( |
| 9078 | user.id).iterator(); |
| 9079 | needSep = false; |
| 9080 | while (it.hasNext()) { |
| 9081 | ServiceRecord r = it.next(); |
| 9082 | if (!matcher.match(r, r.name)) { |
| 9083 | continue; |
| 9084 | } |
| 9085 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9086 | continue; |
| 9087 | } |
| 9088 | if (!printed) { |
| 9089 | pw.println(" Active services:"); |
| 9090 | printed = true; |
| 9091 | } |
| 9092 | if (needSep) { |
| 9093 | pw.println(); |
| 9094 | } |
| 9095 | pw.print(" * "); |
| 9096 | pw.println(r); |
| 9097 | if (dumpAll) { |
| 9098 | r.dump(pw, " "); |
| 9099 | needSep = true; |
| 9100 | } else { |
| 9101 | pw.print(" app="); |
| 9102 | pw.println(r.app); |
| 9103 | pw.print(" created="); |
| 9104 | TimeUtils.formatDuration(r.createTime, nowReal, pw); |
| 9105 | pw.print(" started="); |
| 9106 | pw.print(r.startRequested); |
| 9107 | pw.print(" connections="); |
| 9108 | pw.println(r.connections.size()); |
| 9109 | if (r.connections.size() > 0) { |
| 9110 | pw.println(" Connections:"); |
| 9111 | for (ArrayList<ConnectionRecord> clist : r.connections.values()) { |
| 9112 | for (int i = 0; i < clist.size(); i++) { |
| 9113 | ConnectionRecord conn = clist.get(i); |
| 9114 | pw.print(" "); |
| 9115 | pw.print(conn.binding.intent.intent.getIntent() |
| 9116 | .toShortString(false, false, false)); |
| 9117 | pw.print(" -> "); |
| 9118 | ProcessRecord proc = conn.binding.client; |
| 9119 | pw.println(proc != null ? proc.toShortString() : "null"); |
| 9120 | } |
| 9121 | } |
Dianne Hackborn | 8ec8d41 | 2011-11-14 18:27:24 -0800 | [diff] [blame] | 9122 | } |
| 9123 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9124 | if (dumpClient && r.app != null && r.app.thread != null) { |
| 9125 | pw.println(" Client:"); |
| 9126 | pw.flush(); |
| 9127 | try { |
| 9128 | TransferPipe tp = new TransferPipe(); |
| 9129 | try { |
| 9130 | r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), |
| 9131 | r, args); |
| 9132 | tp.setBufferPrefix(" "); |
| 9133 | // Short timeout, since blocking here can |
| 9134 | // deadlock with the application. |
| 9135 | tp.go(fd, 2000); |
| 9136 | } finally { |
| 9137 | tp.kill(); |
| 9138 | } |
| 9139 | } catch (IOException e) { |
| 9140 | pw.println(" Failure while dumping the service: " + e); |
| 9141 | } catch (RemoteException e) { |
| 9142 | pw.println(" Got a RemoteException while dumping the service"); |
| 9143 | } |
| 9144 | needSep = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9145 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9146 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9147 | needSep = printed; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9148 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9149 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9150 | } catch (RemoteException re) { |
| 9151 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9152 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9153 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9154 | if (mPendingServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9155 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9156 | for (int i=0; i<mPendingServices.size(); i++) { |
| 9157 | ServiceRecord r = mPendingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9158 | if (!matcher.match(r, r.name)) { |
| 9159 | continue; |
| 9160 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9161 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9162 | continue; |
| 9163 | } |
| 9164 | if (!printed) { |
| 9165 | if (needSep) pw.println(" "); |
| 9166 | needSep = true; |
| 9167 | pw.println(" Pending services:"); |
| 9168 | printed = true; |
| 9169 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9170 | pw.print(" * Pending "); pw.println(r); |
| 9171 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9172 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9173 | needSep = true; |
| 9174 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9175 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9176 | if (mRestartingServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9177 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9178 | for (int i=0; i<mRestartingServices.size(); i++) { |
| 9179 | ServiceRecord r = mRestartingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9180 | if (!matcher.match(r, r.name)) { |
| 9181 | continue; |
| 9182 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9183 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9184 | continue; |
| 9185 | } |
| 9186 | if (!printed) { |
| 9187 | if (needSep) pw.println(" "); |
| 9188 | needSep = true; |
| 9189 | pw.println(" Restarting services:"); |
| 9190 | printed = true; |
| 9191 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9192 | pw.print(" * Restarting "); pw.println(r); |
| 9193 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9194 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9195 | needSep = true; |
| 9196 | } |
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 | if (mStoppingServices.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9199 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9200 | for (int i=0; i<mStoppingServices.size(); i++) { |
| 9201 | ServiceRecord r = mStoppingServices.get(i); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9202 | if (!matcher.match(r, r.name)) { |
| 9203 | continue; |
| 9204 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9205 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 9206 | continue; |
| 9207 | } |
| 9208 | if (!printed) { |
| 9209 | if (needSep) pw.println(" "); |
| 9210 | needSep = true; |
| 9211 | pw.println(" Stopping services:"); |
| 9212 | printed = true; |
| 9213 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9214 | pw.print(" * Stopping "); pw.println(r); |
| 9215 | r.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9216 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9217 | needSep = true; |
| 9218 | } |
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 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9221 | if (mServiceConnections.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9222 | boolean printed = false; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9223 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9224 | = mServiceConnections.values().iterator(); |
| 9225 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9226 | ArrayList<ConnectionRecord> r = it.next(); |
| 9227 | for (int i=0; i<r.size(); i++) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9228 | ConnectionRecord cr = r.get(i); |
| 9229 | if (!matcher.match(cr.binding.service, cr.binding.service.name)) { |
| 9230 | continue; |
| 9231 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9232 | if (dumpPackage != null && (cr.binding.client == null |
| 9233 | || !dumpPackage.equals(cr.binding.client.info.packageName))) { |
| 9234 | continue; |
| 9235 | } |
| 9236 | if (!printed) { |
| 9237 | if (needSep) pw.println(" "); |
| 9238 | needSep = true; |
| 9239 | pw.println(" Connection bindings to services:"); |
| 9240 | printed = true; |
| 9241 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9242 | pw.print(" * "); pw.println(cr); |
| 9243 | cr.dump(pw, " "); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 9244 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9245 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9246 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9247 | } |
| 9248 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9249 | |
| 9250 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9251 | } |
| 9252 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9253 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9254 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9255 | boolean needSep = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9256 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9257 | ItemMatcher matcher = new ItemMatcher(); |
| 9258 | matcher.build(args, opti); |
| 9259 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9260 | pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9261 | |
| 9262 | mProviderMap.dumpProvidersLocked(pw, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9263 | |
| 9264 | if (mLaunchingProviders.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9265 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9266 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9267 | ContentProviderRecord r = mLaunchingProviders.get(i); |
| 9268 | if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) { |
| 9269 | continue; |
| 9270 | } |
| 9271 | if (!printed) { |
| 9272 | if (needSep) pw.println(" "); |
| 9273 | needSep = true; |
| 9274 | pw.println(" Launching content providers:"); |
| 9275 | printed = true; |
| 9276 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9277 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9278 | pw.println(r); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9279 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9280 | } |
| 9281 | |
| 9282 | if (mGrantedUriPermissions.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9283 | if (needSep) pw.println(); |
| 9284 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9285 | pw.println("Granted Uri Permissions:"); |
| 9286 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 9287 | int uid = mGrantedUriPermissions.keyAt(i); |
| 9288 | HashMap<Uri, UriPermission> perms |
| 9289 | = mGrantedUriPermissions.valueAt(i); |
| 9290 | pw.print(" * UID "); pw.print(uid); |
| 9291 | pw.println(" holds:"); |
| 9292 | for (UriPermission perm : perms.values()) { |
| 9293 | pw.print(" "); pw.println(perm); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9294 | if (dumpAll) { |
| 9295 | perm.dump(pw, " "); |
| 9296 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9297 | } |
| 9298 | } |
| 9299 | needSep = true; |
| 9300 | } |
| 9301 | |
| 9302 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9303 | } |
| 9304 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9305 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9306 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9307 | boolean needSep = false; |
| 9308 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9309 | if (mIntentSenderRecords.size() > 0) { |
| 9310 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9311 | Iterator<WeakReference<PendingIntentRecord>> it |
| 9312 | = mIntentSenderRecords.values().iterator(); |
| 9313 | while (it.hasNext()) { |
| 9314 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 9315 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9316 | if (dumpPackage != null && (rec == null |
| 9317 | || !dumpPackage.equals(rec.key.packageName))) { |
| 9318 | continue; |
| 9319 | } |
| 9320 | if (!printed) { |
| 9321 | pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)"); |
| 9322 | printed = true; |
| 9323 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9324 | needSep = true; |
| 9325 | if (rec != null) { |
| 9326 | pw.print(" * "); pw.println(rec); |
| 9327 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9328 | rec.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9329 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9330 | } else { |
| 9331 | pw.print(" * "); pw.println(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9332 | } |
| 9333 | } |
| 9334 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9335 | |
| 9336 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9337 | } |
| 9338 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9339 | private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9340 | String prefix, String label, boolean complete, boolean brief, boolean client, |
| 9341 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9342 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9343 | boolean needNL = false; |
| 9344 | final String innerPrefix = prefix + " "; |
| 9345 | final String[] args = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9346 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9347 | final ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9348 | if (dumpPackage != null && !dumpPackage.equals(r.packageName)) { |
| 9349 | continue; |
| 9350 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 9351 | final boolean full = !brief && (complete || !r.isInHistory()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9352 | if (needNL) { |
| 9353 | pw.println(" "); |
| 9354 | needNL = false; |
| 9355 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9356 | if (lastTask != r.task) { |
| 9357 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9358 | pw.print(prefix); |
| 9359 | pw.print(full ? "* " : " "); |
| 9360 | pw.println(lastTask); |
| 9361 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9362 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9363 | } else if (complete) { |
| 9364 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 9365 | if (lastTask.intent != null) { |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9366 | pw.print(prefix); pw.print(" "); |
| 9367 | pw.println(lastTask.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9368 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9369 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9370 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9371 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 9372 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 9373 | pw.println(r); |
| 9374 | if (full) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9375 | r.dump(pw, innerPrefix); |
| 9376 | } else if (complete) { |
| 9377 | // Complete + brief == give a summary. Isn't that obvious?!? |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9378 | pw.print(innerPrefix); pw.println(r.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9379 | if (r.app != null) { |
| 9380 | pw.print(innerPrefix); pw.println(r.app); |
| 9381 | } |
| 9382 | } |
| 9383 | if (client && r.app != null && r.app.thread != null) { |
| 9384 | // flush anything that is already in the PrintWriter since the thread is going |
| 9385 | // to write to the file descriptor directly |
| 9386 | pw.flush(); |
| 9387 | try { |
| 9388 | TransferPipe tp = new TransferPipe(); |
| 9389 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9390 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9391 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9392 | // Short timeout, since blocking here can |
| 9393 | // deadlock with the application. |
| 9394 | tp.go(fd, 2000); |
| 9395 | } finally { |
| 9396 | tp.kill(); |
| 9397 | } |
| 9398 | } catch (IOException e) { |
| 9399 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
| 9400 | } catch (RemoteException e) { |
| 9401 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
| 9402 | } |
| 9403 | needNL = true; |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9404 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9405 | } |
| 9406 | } |
| 9407 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 9408 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 9409 | if (val == base) { |
| 9410 | if (space == null) return prefix; |
| 9411 | return prefix + " "; |
| 9412 | } |
| 9413 | return prefix + "+" + Integer.toString(val-base); |
| 9414 | } |
| 9415 | |
| 9416 | private static final int dumpProcessList(PrintWriter pw, |
| 9417 | ActivityManagerService service, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9418 | String prefix, String normalLabel, String persistentLabel, |
| 9419 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9420 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 9421 | final int N = list.size()-1; |
| 9422 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9423 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9424 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9425 | continue; |
| 9426 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9427 | pw.println(String.format("%s%s #%2d: %s", |
| 9428 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 9429 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9430 | if (r.persistent) { |
| 9431 | numPers++; |
| 9432 | } |
| 9433 | } |
| 9434 | return numPers; |
| 9435 | } |
| 9436 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9437 | private static final boolean dumpProcessOomList(PrintWriter pw, |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9438 | ActivityManagerService service, List<ProcessRecord> origList, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9439 | String prefix, String normalLabel, String persistentLabel, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9440 | boolean inclDetails, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9441 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9442 | ArrayList<Pair<ProcessRecord, Integer>> list |
| 9443 | = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size()); |
| 9444 | for (int i=0; i<origList.size(); i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9445 | ProcessRecord r = origList.get(i); |
| 9446 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9447 | continue; |
| 9448 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9449 | list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i)); |
| 9450 | } |
| 9451 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9452 | if (list.size() <= 0) { |
| 9453 | return false; |
| 9454 | } |
| 9455 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9456 | Comparator<Pair<ProcessRecord, Integer>> comparator |
| 9457 | = new Comparator<Pair<ProcessRecord, Integer>>() { |
| 9458 | @Override |
| 9459 | public int compare(Pair<ProcessRecord, Integer> object1, |
| 9460 | Pair<ProcessRecord, Integer> object2) { |
| 9461 | if (object1.first.setAdj != object2.first.setAdj) { |
| 9462 | return object1.first.setAdj > object2.first.setAdj ? -1 : 1; |
| 9463 | } |
| 9464 | if (object1.second.intValue() != object2.second.intValue()) { |
| 9465 | return object1.second.intValue() > object2.second.intValue() ? -1 : 1; |
| 9466 | } |
| 9467 | return 0; |
| 9468 | } |
| 9469 | }; |
| 9470 | |
| 9471 | Collections.sort(list, comparator); |
| 9472 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9473 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 9474 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 9475 | final long curUptime = SystemClock.uptimeMillis(); |
| 9476 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 9477 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9478 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9479 | ProcessRecord r = list.get(i).first; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9480 | String oomAdj; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9481 | if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9482 | oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9483 | } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) { |
| 9484 | oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9485 | } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) { |
| 9486 | oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9487 | } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) { |
| 9488 | oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9489 | } else if (r.setAdj >= ProcessList.SERVICE_ADJ) { |
| 9490 | oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9491 | } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9492 | oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9493 | } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 9494 | oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9495 | } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 9496 | oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9497 | } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 9498 | oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ); |
| 9499 | } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 9500 | oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9501 | } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) { |
| 9502 | oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9503 | } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) { |
| 9504 | oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9505 | } else { |
| 9506 | oomAdj = Integer.toString(r.setAdj); |
| 9507 | } |
| 9508 | String schedGroup; |
| 9509 | switch (r.setSchedGroup) { |
| 9510 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 9511 | schedGroup = "B"; |
| 9512 | break; |
| 9513 | case Process.THREAD_GROUP_DEFAULT: |
| 9514 | schedGroup = "F"; |
| 9515 | break; |
| 9516 | default: |
| 9517 | schedGroup = Integer.toString(r.setSchedGroup); |
| 9518 | break; |
| 9519 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 9520 | String foreground; |
| 9521 | if (r.foregroundActivities) { |
| 9522 | foreground = "A"; |
| 9523 | } else if (r.foregroundServices) { |
| 9524 | foreground = "S"; |
| 9525 | } else { |
| 9526 | foreground = " "; |
| 9527 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 9528 | 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] | 9529 | prefix, (r.persistent ? persistentLabel : normalLabel), |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9530 | (origList.size()-1)-list.get(i).second, oomAdj, schedGroup, |
| 9531 | foreground, r.trimMemoryLevel, r.toShortString(), r.adjType)); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9532 | if (r.adjSource != null || r.adjTarget != null) { |
| 9533 | pw.print(prefix); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9534 | pw.print(" "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9535 | if (r.adjTarget instanceof ComponentName) { |
| 9536 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 9537 | } else if (r.adjTarget != null) { |
| 9538 | pw.print(r.adjTarget.toString()); |
| 9539 | } else { |
| 9540 | pw.print("{null}"); |
| 9541 | } |
| 9542 | pw.print("<="); |
| 9543 | if (r.adjSource instanceof ProcessRecord) { |
| 9544 | pw.print("Proc{"); |
| 9545 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 9546 | pw.println("}"); |
| 9547 | } else if (r.adjSource != null) { |
| 9548 | pw.println(r.adjSource.toString()); |
| 9549 | } else { |
| 9550 | pw.println("{null}"); |
| 9551 | } |
| 9552 | } |
| 9553 | if (inclDetails) { |
| 9554 | pw.print(prefix); |
| 9555 | pw.print(" "); |
| 9556 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 9557 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
| 9558 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 9559 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 9560 | pw.print(" cur="); pw.print(r.curAdj); |
| 9561 | pw.print(" set="); pw.println(r.setAdj); |
| 9562 | pw.print(prefix); |
| 9563 | pw.print(" "); |
| 9564 | pw.print("keeping="); pw.print(r.keeping); |
| 9565 | pw.print(" hidden="); pw.print(r.hidden); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9566 | pw.print(" empty="); pw.print(r.empty); |
| 9567 | pw.print(" hasAboveClient="); pw.println(r.hasAboveClient); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9568 | |
| 9569 | if (!r.keeping) { |
| 9570 | if (r.lastWakeTime != 0) { |
| 9571 | long wtime; |
| 9572 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 9573 | synchronized (stats) { |
| 9574 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 9575 | r.pid, curRealtime); |
| 9576 | } |
| 9577 | long timeUsed = wtime - r.lastWakeTime; |
| 9578 | pw.print(prefix); |
| 9579 | pw.print(" "); |
| 9580 | pw.print("keep awake over "); |
| 9581 | TimeUtils.formatDuration(realtimeSince, pw); |
| 9582 | pw.print(" used "); |
| 9583 | TimeUtils.formatDuration(timeUsed, pw); |
| 9584 | pw.print(" ("); |
| 9585 | pw.print((timeUsed*100)/realtimeSince); |
| 9586 | pw.println("%)"); |
| 9587 | } |
| 9588 | if (r.lastCpuTime != 0) { |
| 9589 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 9590 | pw.print(prefix); |
| 9591 | pw.print(" "); |
| 9592 | pw.print("run cpu over "); |
| 9593 | TimeUtils.formatDuration(uptimeSince, pw); |
| 9594 | pw.print(" used "); |
| 9595 | TimeUtils.formatDuration(timeUsed, pw); |
| 9596 | pw.print(" ("); |
| 9597 | pw.print((timeUsed*100)/uptimeSince); |
| 9598 | pw.println("%)"); |
| 9599 | } |
| 9600 | } |
| 9601 | } |
| 9602 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9603 | return true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9604 | } |
| 9605 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9606 | ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9607 | ArrayList<ProcessRecord> procs; |
| 9608 | synchronized (this) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9609 | if (args != null && args.length > start |
| 9610 | && args[start].charAt(0) != '-') { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9611 | procs = new ArrayList<ProcessRecord>(); |
| 9612 | int pid = -1; |
| 9613 | try { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9614 | pid = Integer.parseInt(args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9615 | } catch (NumberFormatException e) { |
| 9616 | |
| 9617 | } |
| 9618 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 9619 | ProcessRecord proc = mLruProcesses.get(i); |
| 9620 | if (proc.pid == pid) { |
| 9621 | procs.add(proc); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9622 | } else if (proc.processName.equals(args[start])) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9623 | procs.add(proc); |
| 9624 | } |
| 9625 | } |
| 9626 | if (procs.size() <= 0) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9627 | pw.println("No process found for: " + args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9628 | return null; |
| 9629 | } |
| 9630 | } else { |
| 9631 | procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 9632 | } |
| 9633 | } |
| 9634 | return procs; |
| 9635 | } |
| 9636 | |
| 9637 | final void dumpGraphicsHardwareUsage(FileDescriptor fd, |
| 9638 | PrintWriter pw, String[] args) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9639 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9640 | if (procs == null) { |
| 9641 | return; |
| 9642 | } |
| 9643 | |
| 9644 | long uptime = SystemClock.uptimeMillis(); |
| 9645 | long realtime = SystemClock.elapsedRealtime(); |
| 9646 | pw.println("Applications Graphics Acceleration Info:"); |
| 9647 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 9648 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9649 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9650 | ProcessRecord r = procs.get(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9651 | if (r.thread != null) { |
| 9652 | pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **"); |
| 9653 | pw.flush(); |
| 9654 | try { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9655 | TransferPipe tp = new TransferPipe(); |
| 9656 | try { |
| 9657 | r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 9658 | tp.go(fd); |
| 9659 | } finally { |
| 9660 | tp.kill(); |
| 9661 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9662 | } catch (IOException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9663 | pw.println("Failure while dumping the app: " + r); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9664 | pw.flush(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9665 | } catch (RemoteException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9666 | pw.println("Got a RemoteException while dumping the app " + r); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9667 | pw.flush(); |
| 9668 | } |
| 9669 | } |
| 9670 | } |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9671 | } |
| 9672 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 9673 | final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 9674 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
| 9675 | if (procs == null) { |
| 9676 | return; |
| 9677 | } |
| 9678 | |
| 9679 | pw.println("Applications Database Info:"); |
| 9680 | |
| 9681 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9682 | ProcessRecord r = procs.get(i); |
| 9683 | if (r.thread != null) { |
| 9684 | pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **"); |
| 9685 | pw.flush(); |
| 9686 | try { |
| 9687 | TransferPipe tp = new TransferPipe(); |
| 9688 | try { |
| 9689 | r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 9690 | tp.go(fd); |
| 9691 | } finally { |
| 9692 | tp.kill(); |
| 9693 | } |
| 9694 | } catch (IOException e) { |
| 9695 | pw.println("Failure while dumping the app: " + r); |
| 9696 | pw.flush(); |
| 9697 | } catch (RemoteException e) { |
| 9698 | pw.println("Got a RemoteException while dumping the app " + r); |
| 9699 | pw.flush(); |
| 9700 | } |
| 9701 | } |
| 9702 | } |
| 9703 | } |
| 9704 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9705 | final static class MemItem { |
| 9706 | final String label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9707 | final String shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9708 | final long pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9709 | final int id; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9710 | ArrayList<MemItem> subitems; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9711 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9712 | public MemItem(String _label, String _shortLabel, long _pss, int _id) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9713 | label = _label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9714 | shortLabel = _shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9715 | pss = _pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9716 | id = _id; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9717 | } |
| 9718 | } |
| 9719 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9720 | static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9721 | boolean sort) { |
| 9722 | if (sort) { |
| 9723 | Collections.sort(items, new Comparator<MemItem>() { |
| 9724 | @Override |
| 9725 | public int compare(MemItem lhs, MemItem rhs) { |
| 9726 | if (lhs.pss < rhs.pss) { |
| 9727 | return 1; |
| 9728 | } else if (lhs.pss > rhs.pss) { |
| 9729 | return -1; |
| 9730 | } |
| 9731 | return 0; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9732 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9733 | }); |
| 9734 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9735 | |
| 9736 | for (int i=0; i<items.size(); i++) { |
| 9737 | MemItem mi = items.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9738 | 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] | 9739 | if (mi.subitems != null) { |
| 9740 | dumpMemItems(pw, prefix + " ", mi.subitems, true); |
| 9741 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9742 | } |
| 9743 | } |
| 9744 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9745 | // These are in KB. |
| 9746 | static final long[] DUMP_MEM_BUCKETS = new long[] { |
| 9747 | 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024, |
| 9748 | 120*1024, 160*1024, 200*1024, |
| 9749 | 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024, |
| 9750 | 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024 |
| 9751 | }; |
| 9752 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9753 | static final void appendMemBucket(StringBuilder out, long memKB, String label, |
| 9754 | boolean stackLike) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9755 | int start = label.lastIndexOf('.'); |
| 9756 | if (start >= 0) start++; |
| 9757 | else start = 0; |
| 9758 | int end = label.length(); |
| 9759 | for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) { |
| 9760 | if (DUMP_MEM_BUCKETS[i] >= memKB) { |
| 9761 | long bucket = DUMP_MEM_BUCKETS[i]/1024; |
| 9762 | out.append(bucket); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9763 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9764 | out.append(label, start, end); |
| 9765 | return; |
| 9766 | } |
| 9767 | } |
| 9768 | out.append(memKB/1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9769 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9770 | out.append(label, start, end); |
| 9771 | } |
| 9772 | |
| 9773 | static final int[] DUMP_MEM_OOM_ADJ = new int[] { |
| 9774 | ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ, |
| 9775 | ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ, |
| 9776 | ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ, |
| 9777 | ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ |
| 9778 | }; |
| 9779 | static final String[] DUMP_MEM_OOM_LABEL = new String[] { |
| 9780 | "System", "Persistent", "Foreground", |
| 9781 | "Visible", "Perceptible", "Heavy Weight", |
| 9782 | "Backup", "A Services", "Home", "Previous", |
| 9783 | "B Services", "Background" |
| 9784 | }; |
| 9785 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9786 | final void dumpApplicationMemoryUsage(FileDescriptor fd, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9787 | PrintWriter pw, String prefix, String[] args, boolean brief, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9788 | PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9789 | boolean dumpAll = false; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9790 | boolean oomOnly = false; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9791 | |
| 9792 | int opti = 0; |
| 9793 | while (opti < args.length) { |
| 9794 | String opt = args[opti]; |
| 9795 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 9796 | break; |
| 9797 | } |
| 9798 | opti++; |
| 9799 | if ("-a".equals(opt)) { |
| 9800 | dumpAll = true; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9801 | } else if ("--oom".equals(opt)) { |
| 9802 | oomOnly = true; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9803 | } else if ("-h".equals(opt)) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9804 | pw.println("meminfo dump options: [-a] [--oom] [process]"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9805 | pw.println(" -a: include all available information for each process."); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9806 | pw.println(" --oom: only show processes organized by oom adj."); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9807 | pw.println("If [process] is specified it can be the name or "); |
| 9808 | pw.println("pid of a specific process to dump."); |
| 9809 | return; |
| 9810 | } else { |
| 9811 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 9812 | } |
| 9813 | } |
| 9814 | |
| 9815 | ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9816 | if (procs == null) { |
| 9817 | return; |
| 9818 | } |
| 9819 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 9820 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9821 | long uptime = SystemClock.uptimeMillis(); |
| 9822 | long realtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9823 | |
| 9824 | if (procs.size() == 1 || isCheckinRequest) { |
| 9825 | dumpAll = true; |
| 9826 | } |
| 9827 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9828 | if (isCheckinRequest) { |
| 9829 | // short checkin version |
| 9830 | pw.println(uptime + "," + realtime); |
| 9831 | pw.flush(); |
| 9832 | } else { |
| 9833 | pw.println("Applications Memory Usage (kB):"); |
| 9834 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 9835 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9836 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9837 | String[] innerArgs = new String[args.length-opti]; |
| 9838 | System.arraycopy(args, opti, innerArgs, 0, args.length-opti); |
| 9839 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9840 | ArrayList<MemItem> procMems = new ArrayList<MemItem>(); |
| 9841 | long nativePss=0, dalvikPss=0, otherPss=0; |
| 9842 | long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS]; |
| 9843 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9844 | long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length]; |
| 9845 | ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[]) |
| 9846 | new ArrayList[DUMP_MEM_OOM_LABEL.length]; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9847 | |
| 9848 | long totalPss = 0; |
| 9849 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9850 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9851 | ProcessRecord r = procs.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9852 | if (r.thread != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9853 | if (!isCheckinRequest && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9854 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 9855 | pw.flush(); |
| 9856 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9857 | Debug.MemoryInfo mi = null; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9858 | if (dumpAll) { |
| 9859 | try { |
| 9860 | mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs); |
| 9861 | } catch (RemoteException e) { |
| 9862 | if (!isCheckinRequest) { |
| 9863 | pw.println("Got RemoteException!"); |
| 9864 | pw.flush(); |
| 9865 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9866 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9867 | } else { |
| 9868 | mi = new Debug.MemoryInfo(); |
| 9869 | Debug.getMemoryInfo(r.pid, mi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9870 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9871 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9872 | if (!isCheckinRequest && mi != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9873 | long myTotalPss = mi.getTotalPss(); |
| 9874 | totalPss += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9875 | MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")", |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9876 | r.processName, myTotalPss, 0); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9877 | procMems.add(pssItem); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9878 | |
| 9879 | nativePss += mi.nativePss; |
| 9880 | dalvikPss += mi.dalvikPss; |
| 9881 | otherPss += mi.otherPss; |
| 9882 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
| 9883 | long mem = mi.getOtherPss(j); |
| 9884 | miscPss[j] += mem; |
| 9885 | otherPss -= mem; |
| 9886 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9887 | |
| 9888 | for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9889 | if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex] |
| 9890 | || oomIndex == (oomPss.length-1)) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9891 | oomPss[oomIndex] += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9892 | if (oomProcs[oomIndex] == null) { |
| 9893 | oomProcs[oomIndex] = new ArrayList<MemItem>(); |
| 9894 | } |
| 9895 | oomProcs[oomIndex].add(pssItem); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9896 | break; |
| 9897 | } |
| 9898 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9899 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9900 | } |
| 9901 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9902 | |
| 9903 | if (!isCheckinRequest && procs.size() > 1) { |
| 9904 | ArrayList<MemItem> catMems = new ArrayList<MemItem>(); |
| 9905 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9906 | catMems.add(new MemItem("Native", "Native", nativePss, -1)); |
| 9907 | catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2)); |
| 9908 | catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9909 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9910 | String label = Debug.MemoryInfo.getOtherLabel(j); |
| 9911 | catMems.add(new MemItem(label, label, miscPss[j], j)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9912 | } |
| 9913 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9914 | ArrayList<MemItem> oomMems = new ArrayList<MemItem>(); |
| 9915 | for (int j=0; j<oomPss.length; j++) { |
| 9916 | if (oomPss[j] != 0) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9917 | String label = DUMP_MEM_OOM_LABEL[j]; |
| 9918 | MemItem item = new MemItem(label, label, oomPss[j], |
| 9919 | DUMP_MEM_OOM_ADJ[j]); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 9920 | item.subitems = oomProcs[j]; |
| 9921 | oomMems.add(item); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9922 | } |
| 9923 | } |
| 9924 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9925 | if (outTag != null || outStack != null) { |
| 9926 | if (outTag != null) { |
| 9927 | appendMemBucket(outTag, totalPss, "total", false); |
| 9928 | } |
| 9929 | if (outStack != null) { |
| 9930 | appendMemBucket(outStack, totalPss, "total", true); |
| 9931 | } |
| 9932 | boolean firstLine = true; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9933 | for (int i=0; i<oomMems.size(); i++) { |
| 9934 | MemItem miCat = oomMems.get(i); |
| 9935 | if (miCat.subitems == null || miCat.subitems.size() < 1) { |
| 9936 | continue; |
| 9937 | } |
| 9938 | if (miCat.id < ProcessList.SERVICE_ADJ |
| 9939 | || miCat.id == ProcessList.HOME_APP_ADJ |
| 9940 | || miCat.id == ProcessList.PREVIOUS_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9941 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 9942 | outTag.append(" / "); |
| 9943 | } |
| 9944 | if (outStack != null) { |
| 9945 | if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 9946 | if (firstLine) { |
| 9947 | outStack.append(":"); |
| 9948 | firstLine = false; |
| 9949 | } |
| 9950 | outStack.append("\n\t at "); |
| 9951 | } else { |
| 9952 | outStack.append("$"); |
| 9953 | } |
| 9954 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9955 | for (int j=0; j<miCat.subitems.size(); j++) { |
| 9956 | MemItem mi = miCat.subitems.get(j); |
| 9957 | if (j > 0) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9958 | if (outTag != null) { |
| 9959 | outTag.append(" "); |
| 9960 | } |
| 9961 | if (outStack != null) { |
| 9962 | outStack.append("$"); |
| 9963 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9964 | } |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9965 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 9966 | appendMemBucket(outTag, mi.pss, mi.shortLabel, false); |
| 9967 | } |
| 9968 | if (outStack != null) { |
| 9969 | appendMemBucket(outStack, mi.pss, mi.shortLabel, true); |
| 9970 | } |
| 9971 | } |
| 9972 | if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 9973 | outStack.append("("); |
| 9974 | for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) { |
| 9975 | if (DUMP_MEM_OOM_ADJ[k] == miCat.id) { |
| 9976 | outStack.append(DUMP_MEM_OOM_LABEL[k]); |
| 9977 | outStack.append(":"); |
| 9978 | outStack.append(DUMP_MEM_OOM_ADJ[k]); |
| 9979 | } |
| 9980 | } |
| 9981 | outStack.append(")"); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 9982 | } |
| 9983 | } |
| 9984 | } |
| 9985 | } |
| 9986 | |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9987 | if (!brief && !oomOnly) { |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 9988 | pw.println(); |
| 9989 | pw.println("Total PSS by process:"); |
| 9990 | dumpMemItems(pw, " ", procMems, true); |
| 9991 | pw.println(); |
| 9992 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9993 | pw.println("Total PSS by OOM adjustment:"); |
| 9994 | dumpMemItems(pw, " ", oomMems, false); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 9995 | if (!oomOnly) { |
| 9996 | PrintWriter out = categoryPw != null ? categoryPw : pw; |
| 9997 | out.println(); |
| 9998 | out.println("Total PSS by category:"); |
| 9999 | dumpMemItems(out, " ", catMems, true); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10000 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10001 | pw.println(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10002 | pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10003 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10004 | } |
| 10005 | |
| 10006 | /** |
| 10007 | * Searches array of arguments for the specified string |
| 10008 | * @param args array of argument strings |
| 10009 | * @param value value to search for |
| 10010 | * @return true if the value is contained in the array |
| 10011 | */ |
| 10012 | private static boolean scanArgs(String[] args, String value) { |
| 10013 | if (args != null) { |
| 10014 | for (String arg : args) { |
| 10015 | if (value.equals(arg)) { |
| 10016 | return true; |
| 10017 | } |
| 10018 | } |
| 10019 | } |
| 10020 | return false; |
| 10021 | } |
| 10022 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10023 | private final void killServicesLocked(ProcessRecord app, |
| 10024 | boolean allowRestart) { |
| 10025 | // Report disconnected services. |
| 10026 | if (false) { |
| 10027 | // XXX we are letting the client link to the service for |
| 10028 | // death notifications. |
| 10029 | if (app.services.size() > 0) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10030 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10031 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10032 | ServiceRecord r = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10033 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10034 | Iterator<ArrayList<ConnectionRecord>> jt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10035 | = r.connections.values().iterator(); |
| 10036 | while (jt.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10037 | ArrayList<ConnectionRecord> cl = jt.next(); |
| 10038 | for (int i=0; i<cl.size(); i++) { |
| 10039 | ConnectionRecord c = cl.get(i); |
| 10040 | if (c.binding.client != app) { |
| 10041 | try { |
| 10042 | //c.conn.connected(r.className, null); |
| 10043 | } catch (Exception e) { |
| 10044 | // todo: this should be asynchronous! |
| 10045 | Slog.w(TAG, "Exception thrown disconnected servce " |
| 10046 | + r.shortName |
| 10047 | + " from app " + app.processName, e); |
| 10048 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10049 | } |
| 10050 | } |
| 10051 | } |
| 10052 | } |
| 10053 | } |
| 10054 | } |
| 10055 | } |
| 10056 | |
| 10057 | // Clean up any connections this application has to other services. |
| 10058 | if (app.connections.size() > 0) { |
| 10059 | Iterator<ConnectionRecord> it = app.connections.iterator(); |
| 10060 | while (it.hasNext()) { |
| 10061 | ConnectionRecord r = it.next(); |
| 10062 | removeConnectionLocked(r, app, null); |
| 10063 | } |
| 10064 | } |
| 10065 | app.connections.clear(); |
| 10066 | |
| 10067 | if (app.services.size() != 0) { |
| 10068 | // Any services running in the application need to be placed |
| 10069 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10070 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10071 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10072 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10073 | synchronized (sr.stats.getBatteryStats()) { |
| 10074 | sr.stats.stopLaunchedLocked(); |
| 10075 | } |
| 10076 | sr.app = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10077 | sr.isolatedProc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10078 | sr.executeNesting = 0; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10079 | if (mStoppingServices.remove(sr)) { |
| 10080 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
| 10081 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10082 | |
| 10083 | boolean hasClients = sr.bindings.size() > 0; |
| 10084 | if (hasClients) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10085 | Iterator<IntentBindRecord> bindings |
| 10086 | = sr.bindings.values().iterator(); |
| 10087 | while (bindings.hasNext()) { |
| 10088 | IntentBindRecord b = bindings.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10089 | if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10090 | + ": shouldUnbind=" + b.hasBound); |
| 10091 | b.binder = null; |
| 10092 | b.requested = b.received = b.hasBound = false; |
| 10093 | } |
| 10094 | } |
| 10095 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 10096 | if (sr.crashCount >= 2 && (sr.serviceInfo.applicationInfo.flags |
| 10097 | &ApplicationInfo.FLAG_PERSISTENT) == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10098 | Slog.w(TAG, "Service crashed " + sr.crashCount |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10099 | + " times, stopping: " + sr); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10100 | EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10101 | sr.crashCount, sr.shortName, app.pid); |
| 10102 | bringDownServiceLocked(sr, true); |
| 10103 | } else if (!allowRestart) { |
| 10104 | bringDownServiceLocked(sr, true); |
| 10105 | } else { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10106 | boolean canceled = scheduleServiceRestartLocked(sr, true); |
| 10107 | |
| 10108 | // Should the service remain running? Note that in the |
| 10109 | // extreme case of so many attempts to deliver a command |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10110 | // that it failed we also will stop it here. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10111 | if (sr.startRequested && (sr.stopIfKilled || canceled)) { |
| 10112 | if (sr.pendingStarts.size() == 0) { |
| 10113 | sr.startRequested = false; |
| 10114 | if (!hasClients) { |
| 10115 | // Whoops, no reason to restart! |
| 10116 | bringDownServiceLocked(sr, true); |
| 10117 | } |
| 10118 | } |
| 10119 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10120 | } |
| 10121 | } |
| 10122 | |
| 10123 | if (!allowRestart) { |
| 10124 | app.services.clear(); |
| 10125 | } |
| 10126 | } |
| 10127 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10128 | // Make sure we have no more records on the stopping list. |
| 10129 | int i = mStoppingServices.size(); |
| 10130 | while (i > 0) { |
| 10131 | i--; |
| 10132 | ServiceRecord sr = mStoppingServices.get(i); |
| 10133 | if (sr.app == app) { |
| 10134 | mStoppingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10135 | if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10136 | } |
| 10137 | } |
| 10138 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10139 | app.executingServices.clear(); |
| 10140 | } |
| 10141 | |
| 10142 | private final void removeDyingProviderLocked(ProcessRecord proc, |
| 10143 | ContentProviderRecord cpr) { |
| 10144 | synchronized (cpr) { |
| 10145 | cpr.launchingApp = null; |
| 10146 | cpr.notifyAll(); |
| 10147 | } |
| 10148 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10149 | mProviderMap.removeProviderByClass(cpr.name, UserId.getUserId(cpr.uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10150 | String names[] = cpr.info.authority.split(";"); |
| 10151 | for (int j = 0; j < names.length; j++) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10152 | mProviderMap.removeProviderByName(names[j], UserId.getUserId(cpr.uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10153 | } |
| 10154 | |
| 10155 | Iterator<ProcessRecord> cit = cpr.clients.iterator(); |
| 10156 | while (cit.hasNext()) { |
| 10157 | ProcessRecord capp = cit.next(); |
| 10158 | if (!capp.persistent && capp.thread != null |
| 10159 | && capp.pid != 0 |
| 10160 | && capp.pid != MY_PID) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 10161 | Slog.i(TAG, "Kill " + capp.processName |
| 10162 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10163 | + " in dying process " + (proc != null ? proc.processName : "??")); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 10164 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid, |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10165 | capp.processName, capp.setAdj, "dying provider " |
| 10166 | + cpr.name.toShortString()); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10167 | Process.killProcessQuiet(capp.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10168 | } |
| 10169 | } |
| 10170 | |
| 10171 | mLaunchingProviders.remove(cpr); |
| 10172 | } |
| 10173 | |
| 10174 | /** |
| 10175 | * Main code for cleaning up a process when it has gone away. This is |
| 10176 | * called both as a result of the process dying, or directly when stopping |
| 10177 | * a process when running in single process mode. |
| 10178 | */ |
| 10179 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10180 | boolean restarting, boolean allowRestart, int index) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10181 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10182 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10183 | } |
| 10184 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10185 | mProcessesToGc.remove(app); |
| 10186 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10187 | // Dismiss any open dialogs. |
| 10188 | if (app.crashDialog != null) { |
| 10189 | app.crashDialog.dismiss(); |
| 10190 | app.crashDialog = null; |
| 10191 | } |
| 10192 | if (app.anrDialog != null) { |
| 10193 | app.anrDialog.dismiss(); |
| 10194 | app.anrDialog = null; |
| 10195 | } |
| 10196 | if (app.waitDialog != null) { |
| 10197 | app.waitDialog.dismiss(); |
| 10198 | app.waitDialog = null; |
| 10199 | } |
| 10200 | |
| 10201 | app.crashing = false; |
| 10202 | app.notResponding = false; |
| 10203 | |
| 10204 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 10205 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10206 | app.thread = null; |
| 10207 | app.forcingToForeground = null; |
| 10208 | app.foregroundServices = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10209 | app.foregroundActivities = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 10210 | app.hasShownUi = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10211 | app.hasAboveClient = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10212 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10213 | killServicesLocked(app, allowRestart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10214 | |
| 10215 | boolean restart = false; |
| 10216 | |
| 10217 | int NL = mLaunchingProviders.size(); |
| 10218 | |
| 10219 | // Remove published content providers. |
| 10220 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10221 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10222 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10223 | ContentProviderRecord cpr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10224 | cpr.provider = null; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10225 | cpr.proc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10226 | |
| 10227 | // See if someone is waiting for this provider... in which |
| 10228 | // case we don't remove it, but just let it restart. |
| 10229 | int i = 0; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10230 | if (!app.bad && allowRestart) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10231 | for (; i<NL; i++) { |
| 10232 | if (mLaunchingProviders.get(i) == cpr) { |
| 10233 | restart = true; |
| 10234 | break; |
| 10235 | } |
| 10236 | } |
| 10237 | } else { |
| 10238 | i = NL; |
| 10239 | } |
| 10240 | |
| 10241 | if (i >= NL) { |
| 10242 | removeDyingProviderLocked(app, cpr); |
| 10243 | NL = mLaunchingProviders.size(); |
| 10244 | } |
| 10245 | } |
| 10246 | app.pubProviders.clear(); |
| 10247 | } |
| 10248 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10249 | // Take care of any launching providers waiting for this process. |
| 10250 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 10251 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10252 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10253 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10254 | // Unregister from connected content providers. |
| 10255 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 0c3154d | 2009-10-06 17:18:05 -0700 | [diff] [blame] | 10256 | Iterator it = app.conProviders.keySet().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10257 | while (it.hasNext()) { |
| 10258 | ContentProviderRecord cpr = (ContentProviderRecord)it.next(); |
| 10259 | cpr.clients.remove(app); |
| 10260 | } |
| 10261 | app.conProviders.clear(); |
| 10262 | } |
| 10263 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10264 | // At this point there may be remaining entries in mLaunchingProviders |
| 10265 | // where we were the only one waiting, so they are no longer of use. |
| 10266 | // Look for these and clean up if found. |
| 10267 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 10268 | // the actual situation to identify what is actually going on. |
| 10269 | if (false) { |
| 10270 | for (int i=0; i<NL; i++) { |
| 10271 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 10272 | mLaunchingProviders.get(i); |
| 10273 | if (cpr.clients.size() <= 0 && cpr.externals <= 0) { |
| 10274 | synchronized (cpr) { |
| 10275 | cpr.launchingApp = null; |
| 10276 | cpr.notifyAll(); |
| 10277 | } |
| 10278 | } |
| 10279 | } |
| 10280 | } |
| 10281 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10282 | skipCurrentReceiverLocked(app); |
| 10283 | |
| 10284 | // Unregister any receivers. |
| 10285 | if (app.receivers.size() > 0) { |
| 10286 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 10287 | while (it.hasNext()) { |
| 10288 | removeReceiverLocked(it.next()); |
| 10289 | } |
| 10290 | app.receivers.clear(); |
| 10291 | } |
| 10292 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10293 | // If the app is undergoing backup, tell the backup manager about it |
| 10294 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10295 | if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10296 | try { |
| 10297 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10298 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10299 | bm.agentDisconnected(app.info.packageName); |
| 10300 | } catch (RemoteException e) { |
| 10301 | // can't happen; backup manager is local |
| 10302 | } |
| 10303 | } |
| 10304 | |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 10305 | mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10306 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10307 | // If the caller is restarting this app, then leave it in its |
| 10308 | // current lists and let the caller take care of it. |
| 10309 | if (restarting) { |
| 10310 | return; |
| 10311 | } |
| 10312 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10313 | if (!app.persistent || app.isolated) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10314 | if (DEBUG_PROCESSES) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10315 | "Removing non-persistent process during cleanup: " + app); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10316 | mProcessNames.remove(app.processName, app.uid); |
| 10317 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10318 | if (mHeavyWeightProcess == app) { |
| 10319 | mHeavyWeightProcess = null; |
| 10320 | mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG); |
| 10321 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10322 | } else if (!app.removed) { |
| 10323 | // This app is persistent, so we need to keep its record around. |
| 10324 | // If it is not already on the pending app list, add it there |
| 10325 | // and start a new process for it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10326 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 10327 | mPersistentStartingProcesses.add(app); |
| 10328 | restart = true; |
| 10329 | } |
| 10330 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10331 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 10332 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10333 | mProcessesOnHold.remove(app); |
| 10334 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 10335 | if (app == mHomeProcess) { |
| 10336 | mHomeProcess = null; |
| 10337 | } |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10338 | if (app == mPreviousProcess) { |
| 10339 | mPreviousProcess = null; |
| 10340 | } |
| 10341 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10342 | if (restart && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10343 | // We have components that still need to be running in the |
| 10344 | // process, so re-launch it. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10345 | mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10346 | startProcessLocked(app, "restart", app.processName); |
| 10347 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 10348 | // Goodbye! |
| 10349 | synchronized (mPidsSelfLocked) { |
| 10350 | mPidsSelfLocked.remove(app.pid); |
| 10351 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 10352 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10353 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10354 | } |
| 10355 | } |
| 10356 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10357 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 10358 | // Look through the content providers we are waiting to have launched, |
| 10359 | // and if any run in this process then either schedule a restart of |
| 10360 | // the process or kill the client waiting for it if this process has |
| 10361 | // gone bad. |
| 10362 | int NL = mLaunchingProviders.size(); |
| 10363 | boolean restart = false; |
| 10364 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10365 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10366 | if (cpr.launchingApp == app) { |
| 10367 | if (!alwaysBad && !app.bad) { |
| 10368 | restart = true; |
| 10369 | } else { |
| 10370 | removeDyingProviderLocked(app, cpr); |
| 10371 | NL = mLaunchingProviders.size(); |
| 10372 | } |
| 10373 | } |
| 10374 | } |
| 10375 | return restart; |
| 10376 | } |
| 10377 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10378 | // ========================================================= |
| 10379 | // SERVICES |
| 10380 | // ========================================================= |
| 10381 | |
| 10382 | ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) { |
| 10383 | ActivityManager.RunningServiceInfo info = |
| 10384 | new ActivityManager.RunningServiceInfo(); |
| 10385 | info.service = r.name; |
| 10386 | if (r.app != null) { |
| 10387 | info.pid = r.app.pid; |
| 10388 | } |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 10389 | info.uid = r.appInfo.uid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10390 | info.process = r.processName; |
| 10391 | info.foreground = r.isForeground; |
| 10392 | info.activeSince = r.createTime; |
| 10393 | info.started = r.startRequested; |
| 10394 | info.clientCount = r.connections.size(); |
| 10395 | info.crashCount = r.crashCount; |
| 10396 | info.lastActivityTime = r.lastActivity; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 10397 | if (r.isForeground) { |
| 10398 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND; |
| 10399 | } |
| 10400 | if (r.startRequested) { |
| 10401 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED; |
| 10402 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 10403 | if (r.app != null && r.app.pid == MY_PID) { |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 10404 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS; |
| 10405 | } |
| 10406 | if (r.app != null && r.app.persistent) { |
| 10407 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS; |
| 10408 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10409 | |
| 10410 | for (ArrayList<ConnectionRecord> connl : r.connections.values()) { |
| 10411 | for (int i=0; i<connl.size(); i++) { |
| 10412 | ConnectionRecord conn = connl.get(i); |
| 10413 | if (conn.clientLabel != 0) { |
| 10414 | info.clientPackage = conn.binding.client.info.packageName; |
| 10415 | info.clientLabel = conn.clientLabel; |
| 10416 | return info; |
| 10417 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10418 | } |
| 10419 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10420 | return info; |
| 10421 | } |
| 10422 | |
| 10423 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 10424 | int flags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10425 | enforceNotIsolatedCaller("getServices"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10426 | synchronized (this) { |
| 10427 | ArrayList<ActivityManager.RunningServiceInfo> res |
| 10428 | = new ArrayList<ActivityManager.RunningServiceInfo>(); |
| 10429 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10430 | int userId = UserId.getUserId(Binder.getCallingUid()); |
| 10431 | if (mServiceMap.getAllServices(userId).size() > 0) { |
| 10432 | Iterator<ServiceRecord> it |
| 10433 | = mServiceMap.getAllServices(userId).iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10434 | while (it.hasNext() && res.size() < maxNum) { |
| 10435 | res.add(makeRunningServiceInfoLocked(it.next())); |
| 10436 | } |
| 10437 | } |
| 10438 | |
| 10439 | for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { |
| 10440 | ServiceRecord r = mRestartingServices.get(i); |
| 10441 | ActivityManager.RunningServiceInfo info = |
| 10442 | makeRunningServiceInfoLocked(r); |
| 10443 | info.restarting = r.nextRestartTime; |
| 10444 | res.add(info); |
| 10445 | } |
| 10446 | |
| 10447 | return res; |
| 10448 | } |
| 10449 | } |
| 10450 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10451 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10452 | enforceNotIsolatedCaller("getRunningServiceControlPanel"); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10453 | synchronized (this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10454 | int userId = UserId.getUserId(Binder.getCallingUid()); |
| 10455 | ServiceRecord r = mServiceMap.getServiceByName(name, userId); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10456 | if (r != null) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10457 | for (ArrayList<ConnectionRecord> conn : r.connections.values()) { |
| 10458 | for (int i=0; i<conn.size(); i++) { |
| 10459 | if (conn.get(i).clientIntent != null) { |
| 10460 | return conn.get(i).clientIntent; |
| 10461 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10462 | } |
| 10463 | } |
| 10464 | } |
| 10465 | } |
| 10466 | return null; |
| 10467 | } |
| 10468 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10469 | private final ServiceRecord findServiceLocked(ComponentName name, |
| 10470 | IBinder token) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10471 | ServiceRecord r = mServiceMap.getServiceByName(name, Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10472 | return r == token ? r : null; |
| 10473 | } |
| 10474 | |
| 10475 | private final class ServiceLookupResult { |
| 10476 | final ServiceRecord record; |
| 10477 | final String permission; |
| 10478 | |
| 10479 | ServiceLookupResult(ServiceRecord _record, String _permission) { |
| 10480 | record = _record; |
| 10481 | permission = _permission; |
| 10482 | } |
| 10483 | }; |
| 10484 | |
| 10485 | private ServiceLookupResult findServiceLocked(Intent service, |
| 10486 | String resolvedType) { |
| 10487 | ServiceRecord r = null; |
| 10488 | if (service.getComponent() != null) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10489 | r = mServiceMap.getServiceByName(service.getComponent(), Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10490 | } |
| 10491 | if (r == null) { |
| 10492 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10493 | r = mServiceMap.getServiceByIntent(filter, Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10494 | } |
| 10495 | |
| 10496 | if (r == null) { |
| 10497 | try { |
| 10498 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10499 | AppGlobals.getPackageManager().resolveService( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10500 | service, resolvedType, 0); |
| 10501 | ServiceInfo sInfo = |
| 10502 | rInfo != null ? rInfo.serviceInfo : null; |
| 10503 | if (sInfo == null) { |
| 10504 | return null; |
| 10505 | } |
| 10506 | |
| 10507 | ComponentName name = new ComponentName( |
| 10508 | sInfo.applicationInfo.packageName, sInfo.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10509 | r = mServiceMap.getServiceByName(name, Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10510 | } catch (RemoteException ex) { |
| 10511 | // pm is in same process, this will never happen. |
| 10512 | } |
| 10513 | } |
| 10514 | if (r != null) { |
| 10515 | int callingPid = Binder.getCallingPid(); |
| 10516 | int callingUid = Binder.getCallingUid(); |
| 10517 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10518 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10519 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10520 | if (!r.exported) { |
| 10521 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 10522 | + " from pid=" + callingPid |
| 10523 | + ", uid=" + callingUid |
| 10524 | + " that is not exported from uid " + r.appInfo.uid); |
| 10525 | return new ServiceLookupResult(null, "not exported from uid " |
| 10526 | + r.appInfo.uid); |
| 10527 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10528 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10529 | + " from pid=" + callingPid |
| 10530 | + ", uid=" + callingUid |
| 10531 | + " requires " + r.permission); |
| 10532 | return new ServiceLookupResult(null, r.permission); |
| 10533 | } |
| 10534 | return new ServiceLookupResult(r, null); |
| 10535 | } |
| 10536 | return null; |
| 10537 | } |
| 10538 | |
| 10539 | private class ServiceRestarter implements Runnable { |
| 10540 | private ServiceRecord mService; |
| 10541 | |
| 10542 | void setService(ServiceRecord service) { |
| 10543 | mService = service; |
| 10544 | } |
| 10545 | |
| 10546 | public void run() { |
| 10547 | synchronized(ActivityManagerService.this) { |
| 10548 | performServiceRestartLocked(mService); |
| 10549 | } |
| 10550 | } |
| 10551 | } |
| 10552 | |
| 10553 | private ServiceLookupResult retrieveServiceLocked(Intent service, |
| 10554 | String resolvedType, int callingPid, int callingUid) { |
| 10555 | ServiceRecord r = null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10556 | if (DEBUG_SERVICE) |
| 10557 | Slog.v(TAG, "retrieveServiceLocked: " + service + " type=" + resolvedType |
| 10558 | + " origCallingUid=" + callingUid); |
| 10559 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10560 | if (service.getComponent() != null) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10561 | r = mServiceMap.getServiceByName(service.getComponent(), Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10562 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10563 | if (r == null) { |
| 10564 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
| 10565 | r = mServiceMap.getServiceByIntent(filter, Binder.getOrigCallingUser()); |
| 10566 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10567 | if (r == null) { |
| 10568 | try { |
| 10569 | ResolveInfo rInfo = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 10570 | AppGlobals.getPackageManager().resolveService( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 10571 | service, resolvedType, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10572 | ServiceInfo sInfo = |
| 10573 | rInfo != null ? rInfo.serviceInfo : null; |
| 10574 | if (sInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10575 | Slog.w(TAG, "Unable to start service " + service + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10576 | ": not found"); |
| 10577 | return null; |
| 10578 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10579 | if (Binder.getOrigCallingUser() > 0) { |
| 10580 | sInfo.applicationInfo = getAppInfoForUser(sInfo.applicationInfo, |
| 10581 | Binder.getOrigCallingUser()); |
| 10582 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10583 | ComponentName name = new ComponentName( |
| 10584 | sInfo.applicationInfo.packageName, sInfo.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10585 | r = mServiceMap.getServiceByName(name, Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10586 | if (r == null) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10587 | Intent.FilterComparison filter = new Intent.FilterComparison( |
| 10588 | service.cloneFilter()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10589 | ServiceRestarter res = new ServiceRestarter(); |
| 10590 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 10591 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 10592 | synchronized (stats) { |
| 10593 | ss = stats.getServiceStatsLocked( |
| 10594 | sInfo.applicationInfo.uid, sInfo.packageName, |
| 10595 | sInfo.name); |
| 10596 | } |
Dianne Hackborn | b1c4a2a | 2010-01-19 15:36:42 -0800 | [diff] [blame] | 10597 | r = new ServiceRecord(this, ss, name, filter, sInfo, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10598 | res.setService(r); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10599 | mServiceMap.putServiceByName(name, UserId.getUserId(r.appInfo.uid), r); |
| 10600 | mServiceMap.putServiceByIntent(filter, UserId.getUserId(r.appInfo.uid), r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10601 | |
| 10602 | // Make sure this component isn't in the pending list. |
| 10603 | int N = mPendingServices.size(); |
| 10604 | for (int i=0; i<N; i++) { |
| 10605 | ServiceRecord pr = mPendingServices.get(i); |
| 10606 | if (pr.name.equals(name)) { |
| 10607 | mPendingServices.remove(i); |
| 10608 | i--; |
| 10609 | N--; |
| 10610 | } |
| 10611 | } |
| 10612 | } |
| 10613 | } catch (RemoteException ex) { |
| 10614 | // pm is in same process, this will never happen. |
| 10615 | } |
| 10616 | } |
| 10617 | if (r != null) { |
| 10618 | if (checkComponentPermission(r.permission, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10619 | callingPid, callingUid, r.appInfo.uid, r.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10620 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10621 | if (!r.exported) { |
| 10622 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 10623 | + " from pid=" + callingPid |
| 10624 | + ", uid=" + callingUid |
| 10625 | + " that is not exported from uid " + r.appInfo.uid); |
| 10626 | return new ServiceLookupResult(null, "not exported from uid " |
| 10627 | + r.appInfo.uid); |
| 10628 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10629 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 10630 | + " from pid=" + callingPid |
| 10631 | + ", uid=" + callingUid |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10632 | + " requires " + r.permission); |
| 10633 | return new ServiceLookupResult(null, r.permission); |
| 10634 | } |
| 10635 | return new ServiceLookupResult(r, null); |
| 10636 | } |
| 10637 | return null; |
| 10638 | } |
| 10639 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10640 | private final void bumpServiceExecutingLocked(ServiceRecord r, String why) { |
| 10641 | if (DEBUG_SERVICE) Log.v(TAG, ">>> EXECUTING " |
| 10642 | + why + " of " + r + " in app " + r.app); |
| 10643 | else if (DEBUG_SERVICE_EXECUTING) Log.v(TAG, ">>> EXECUTING " |
| 10644 | + why + " of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10645 | long now = SystemClock.uptimeMillis(); |
| 10646 | if (r.executeNesting == 0 && r.app != null) { |
| 10647 | if (r.app.executingServices.size() == 0) { |
| 10648 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 10649 | msg.obj = r.app; |
| 10650 | mHandler.sendMessageAtTime(msg, now+SERVICE_TIMEOUT); |
| 10651 | } |
| 10652 | r.app.executingServices.add(r); |
| 10653 | } |
| 10654 | r.executeNesting++; |
| 10655 | r.executingStart = now; |
| 10656 | } |
| 10657 | |
| 10658 | private final void sendServiceArgsLocked(ServiceRecord r, |
| 10659 | boolean oomAdjusted) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10660 | final int N = r.pendingStarts.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10661 | if (N == 0) { |
| 10662 | return; |
| 10663 | } |
| 10664 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10665 | while (r.pendingStarts.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10666 | try { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10667 | ServiceRecord.StartItem si = r.pendingStarts.remove(0); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10668 | if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to: " |
| 10669 | + r + " " + r.intent + " args=" + si.intent); |
Dianne Hackborn | 3a28f22 | 2011-03-01 12:25:54 -0800 | [diff] [blame] | 10670 | if (si.intent == null && N > 1) { |
| 10671 | // If somehow we got a dummy null intent in the middle, |
| 10672 | // then skip it. DO NOT skip a null intent when it is |
| 10673 | // the only one in the list -- this is to support the |
| 10674 | // onStartCommand(null) case. |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10675 | continue; |
| 10676 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10677 | si.deliveredTime = SystemClock.uptimeMillis(); |
| 10678 | r.deliveredStarts.add(si); |
| 10679 | si.deliveryCount++; |
Dianne Hackborn | 3a28f22 | 2011-03-01 12:25:54 -0800 | [diff] [blame] | 10680 | if (si.targetPermissionUid >= 0 && si.intent != null) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10681 | grantUriPermissionUncheckedFromIntentLocked(si.targetPermissionUid, |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 10682 | r.packageName, si.intent, si.getUriPermissionsLocked()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10683 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10684 | bumpServiceExecutingLocked(r, "start"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10685 | if (!oomAdjusted) { |
| 10686 | oomAdjusted = true; |
| 10687 | updateOomAdjLocked(r.app); |
| 10688 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10689 | int flags = 0; |
| 10690 | if (si.deliveryCount > 0) { |
| 10691 | flags |= Service.START_FLAG_RETRY; |
| 10692 | } |
| 10693 | if (si.doneExecutingCount > 0) { |
| 10694 | flags |= Service.START_FLAG_REDELIVERY; |
| 10695 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10696 | r.app.thread.scheduleServiceArgs(r, si.taskRemoved, si.id, flags, si.intent); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10697 | } catch (RemoteException e) { |
| 10698 | // Remote process gone... we'll let the normal cleanup take |
| 10699 | // care of this. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10700 | if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while scheduling start: " + r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10701 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10702 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10703 | Slog.w(TAG, "Unexpected exception", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10704 | break; |
| 10705 | } |
| 10706 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10707 | } |
| 10708 | |
| 10709 | private final boolean requestServiceBindingLocked(ServiceRecord r, |
| 10710 | IntentBindRecord i, boolean rebind) { |
| 10711 | if (r.app == null || r.app.thread == null) { |
| 10712 | // If service is not currently running, can't yet bind. |
| 10713 | return false; |
| 10714 | } |
| 10715 | if ((!i.requested || rebind) && i.apps.size() > 0) { |
| 10716 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10717 | bumpServiceExecutingLocked(r, "bind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10718 | r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind); |
| 10719 | if (!rebind) { |
| 10720 | i.requested = true; |
| 10721 | } |
| 10722 | i.hasBound = true; |
| 10723 | i.doRebind = false; |
| 10724 | } catch (RemoteException e) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10725 | 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] | 10726 | return false; |
| 10727 | } |
| 10728 | } |
| 10729 | return true; |
| 10730 | } |
| 10731 | |
| 10732 | private final void requestServiceBindingsLocked(ServiceRecord r) { |
| 10733 | Iterator<IntentBindRecord> bindings = r.bindings.values().iterator(); |
| 10734 | while (bindings.hasNext()) { |
| 10735 | IntentBindRecord i = bindings.next(); |
| 10736 | if (!requestServiceBindingLocked(r, i, false)) { |
| 10737 | break; |
| 10738 | } |
| 10739 | } |
| 10740 | } |
| 10741 | |
| 10742 | private final void realStartServiceLocked(ServiceRecord r, |
| 10743 | ProcessRecord app) throws RemoteException { |
| 10744 | if (app.thread == null) { |
| 10745 | throw new RemoteException(); |
| 10746 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10747 | if (DEBUG_MU) |
| 10748 | Slog.v(TAG_MU, "realStartServiceLocked, ServiceRecord.uid = " + r.appInfo.uid |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10749 | + ", ProcessRecord.uid = " + app.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10750 | r.app = app; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 10751 | r.restartTime = r.lastActivity = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10752 | |
| 10753 | app.services.add(r); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10754 | bumpServiceExecutingLocked(r, "create"); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10755 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10756 | |
| 10757 | boolean created = false; |
| 10758 | try { |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 10759 | mStringBuilder.setLength(0); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 10760 | r.intent.getIntent().toShortString(mStringBuilder, true, false, true); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10761 | EventLog.writeEvent(EventLogTags.AM_CREATE_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10762 | System.identityHashCode(r), r.shortName, |
Dianne Hackborn | a33e3f7 | 2009-09-29 17:28:24 -0700 | [diff] [blame] | 10763 | mStringBuilder.toString(), r.app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10764 | synchronized (r.stats.getBatteryStats()) { |
| 10765 | r.stats.startLaunchedLocked(); |
| 10766 | } |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 10767 | ensurePackageDexOpt(r.serviceInfo.packageName); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 10768 | app.thread.scheduleCreateService(r, r.serviceInfo, |
| 10769 | compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo)); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10770 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10771 | created = true; |
| 10772 | } finally { |
| 10773 | if (!created) { |
| 10774 | app.services.remove(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10775 | scheduleServiceRestartLocked(r, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10776 | } |
| 10777 | } |
| 10778 | |
| 10779 | requestServiceBindingsLocked(r); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10780 | |
| 10781 | // If the service is in the started state, and there are no |
| 10782 | // pending arguments, then fake up one so its onStartCommand() will |
| 10783 | // be called. |
| 10784 | if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 10785 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
| 10786 | null, -1)); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10787 | } |
| 10788 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10789 | sendServiceArgsLocked(r, true); |
| 10790 | } |
| 10791 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10792 | private final boolean scheduleServiceRestartLocked(ServiceRecord r, |
| 10793 | boolean allowCancel) { |
| 10794 | boolean canceled = false; |
| 10795 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10796 | final long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10797 | long minDuration = SERVICE_RESTART_DURATION; |
Dianne Hackborn | 6ccd2af | 2009-08-27 12:26:44 -0700 | [diff] [blame] | 10798 | long resetTime = SERVICE_RESET_RUN_DURATION; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10799 | |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 10800 | if ((r.serviceInfo.applicationInfo.flags |
| 10801 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 10802 | minDuration /= 4; |
| 10803 | } |
| 10804 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10805 | // Any delivered but not yet finished starts should be put back |
| 10806 | // on the pending list. |
| 10807 | final int N = r.deliveredStarts.size(); |
| 10808 | if (N > 0) { |
| 10809 | for (int i=N-1; i>=0; i--) { |
| 10810 | ServiceRecord.StartItem si = r.deliveredStarts.get(i); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 10811 | si.removeUriPermissionsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10812 | if (si.intent == null) { |
| 10813 | // We'll generate this again if needed. |
| 10814 | } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT |
| 10815 | && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) { |
| 10816 | r.pendingStarts.add(0, si); |
| 10817 | long dur = SystemClock.uptimeMillis() - si.deliveredTime; |
| 10818 | dur *= 2; |
| 10819 | if (minDuration < dur) minDuration = dur; |
| 10820 | if (resetTime < dur) resetTime = dur; |
| 10821 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10822 | Slog.w(TAG, "Canceling start item " + si.intent + " in service " |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10823 | + r.name); |
| 10824 | canceled = true; |
| 10825 | } |
| 10826 | } |
| 10827 | r.deliveredStarts.clear(); |
| 10828 | } |
| 10829 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10830 | r.totalRestartCount++; |
| 10831 | if (r.restartDelay == 0) { |
| 10832 | r.restartCount++; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10833 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10834 | } else { |
| 10835 | // If it has been a "reasonably long time" since the service |
| 10836 | // was started, then reset our restart duration back to |
| 10837 | // the beginning, so we don't infinitely increase the duration |
| 10838 | // on a service that just occasionally gets killed (which is |
| 10839 | // a normal case, due to process being killed to reclaim memory). |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10840 | if (now > (r.restartTime+resetTime)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10841 | r.restartCount = 1; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10842 | r.restartDelay = minDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10843 | } else { |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 10844 | if ((r.serviceInfo.applicationInfo.flags |
| 10845 | &ApplicationInfo.FLAG_PERSISTENT) != 0) { |
| 10846 | // Services in peristent processes will restart much more |
| 10847 | // quickly, since they are pretty important. (Think SystemUI). |
| 10848 | r.restartDelay += minDuration/2; |
| 10849 | } else { |
| 10850 | r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR; |
| 10851 | if (r.restartDelay < minDuration) { |
| 10852 | r.restartDelay = minDuration; |
| 10853 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10854 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10855 | } |
| 10856 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10857 | |
| 10858 | r.nextRestartTime = now + r.restartDelay; |
| 10859 | |
| 10860 | // Make sure that we don't end up restarting a bunch of services |
| 10861 | // all at the same time. |
| 10862 | boolean repeat; |
| 10863 | do { |
| 10864 | repeat = false; |
| 10865 | for (int i=mRestartingServices.size()-1; i>=0; i--) { |
| 10866 | ServiceRecord r2 = mRestartingServices.get(i); |
| 10867 | if (r2 != r && r.nextRestartTime |
| 10868 | >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN) |
| 10869 | && r.nextRestartTime |
| 10870 | < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) { |
| 10871 | r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN; |
| 10872 | r.restartDelay = r.nextRestartTime - now; |
| 10873 | repeat = true; |
| 10874 | break; |
| 10875 | } |
| 10876 | } |
| 10877 | } while (repeat); |
| 10878 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10879 | if (!mRestartingServices.contains(r)) { |
| 10880 | mRestartingServices.add(r); |
| 10881 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10882 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10883 | r.cancelNotification(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10884 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10885 | mHandler.removeCallbacks(r.restarter); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 10886 | mHandler.postAtTime(r.restarter, r.nextRestartTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10887 | r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10888 | Slog.w(TAG, "Scheduling restart of crashed service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10889 | + r.shortName + " in " + r.restartDelay + "ms"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 10890 | EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10891 | r.shortName, r.restartDelay); |
| 10892 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10893 | return canceled; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10894 | } |
| 10895 | |
| 10896 | final void performServiceRestartLocked(ServiceRecord r) { |
| 10897 | if (!mRestartingServices.contains(r)) { |
| 10898 | return; |
| 10899 | } |
| 10900 | bringUpServiceLocked(r, r.intent.getIntent().getFlags(), true); |
| 10901 | } |
| 10902 | |
| 10903 | private final boolean unscheduleServiceRestartLocked(ServiceRecord r) { |
| 10904 | if (r.restartDelay == 0) { |
| 10905 | return false; |
| 10906 | } |
| 10907 | r.resetRestartCounter(); |
| 10908 | mRestartingServices.remove(r); |
| 10909 | mHandler.removeCallbacks(r.restarter); |
| 10910 | return true; |
| 10911 | } |
| 10912 | |
| 10913 | private final boolean bringUpServiceLocked(ServiceRecord r, |
| 10914 | int intentFlags, boolean whileRestarting) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10915 | //Slog.i(TAG, "Bring up service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10916 | //r.dump(" "); |
| 10917 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10918 | if (r.app != null && r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10919 | sendServiceArgsLocked(r, false); |
| 10920 | return true; |
| 10921 | } |
| 10922 | |
| 10923 | if (!whileRestarting && r.restartDelay > 0) { |
| 10924 | // If waiting for a restart, then do nothing. |
| 10925 | return true; |
| 10926 | } |
| 10927 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 10928 | 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] | 10929 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10930 | // We are now bringing the service up, so no longer in the |
| 10931 | // restarting state. |
| 10932 | mRestartingServices.remove(r); |
| 10933 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10934 | // Service is now being launched, its package can't be stopped. |
| 10935 | try { |
| 10936 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 10937 | r.packageName, false); |
| 10938 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 10939 | } catch (IllegalArgumentException e) { |
| 10940 | Slog.w(TAG, "Failed trying to unstop package " |
| 10941 | + r.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10942 | } |
| 10943 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10944 | final boolean isolated = (r.serviceInfo.flags&ServiceInfo.FLAG_ISOLATED_PROCESS) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10945 | final String appName = r.processName; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10946 | ProcessRecord app; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10947 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10948 | if (!isolated) { |
| 10949 | app = getProcessRecordLocked(appName, r.appInfo.uid); |
| 10950 | if (DEBUG_MU) |
| 10951 | Slog.v(TAG_MU, "bringUpServiceLocked: appInfo.uid=" + r.appInfo.uid + " app=" + app); |
| 10952 | if (app != null && app.thread != null) { |
| 10953 | try { |
| 10954 | app.addPackage(r.appInfo.packageName); |
| 10955 | realStartServiceLocked(r, app); |
| 10956 | return true; |
| 10957 | } catch (RemoteException e) { |
| 10958 | Slog.w(TAG, "Exception when starting service " + r.shortName, e); |
| 10959 | } |
| 10960 | |
| 10961 | // If a dead object exception was thrown -- fall through to |
| 10962 | // restart the application. |
| 10963 | } |
| 10964 | } else { |
| 10965 | // If this service runs in an isolated process, then each time |
| 10966 | // we call startProcessLocked() we will get a new isolated |
| 10967 | // process, starting another process if we are currently waiting |
| 10968 | // for a previous process to come up. To deal with this, we store |
| 10969 | // in the service any current isolated process it is running in or |
| 10970 | // waiting to have come up. |
| 10971 | app = r.isolatedProc; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10972 | } |
| 10973 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10974 | // Not running -- get it started, and enqueue this service record |
| 10975 | // to be executed when the app comes up. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10976 | if (app == null) { |
| 10977 | if ((app=startProcessLocked(appName, r.appInfo, true, intentFlags, |
| 10978 | "service", r.name, false, isolated)) == null) { |
| 10979 | Slog.w(TAG, "Unable to launch app " |
| 10980 | + r.appInfo.packageName + "/" |
| 10981 | + r.appInfo.uid + " for service " |
| 10982 | + r.intent.getIntent() + ": process is bad"); |
| 10983 | bringDownServiceLocked(r, true); |
| 10984 | return false; |
| 10985 | } |
| 10986 | if (isolated) { |
| 10987 | r.isolatedProc = app; |
| 10988 | } |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10989 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10990 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10991 | if (!mPendingServices.contains(r)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10992 | mPendingServices.add(r); |
| 10993 | } |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10994 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10995 | return true; |
| 10996 | } |
| 10997 | |
| 10998 | private final void bringDownServiceLocked(ServiceRecord r, boolean force) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10999 | //Slog.i(TAG, "Bring down service:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11000 | //r.dump(" "); |
| 11001 | |
| 11002 | // Does it still need to run? |
| 11003 | if (!force && r.startRequested) { |
| 11004 | return; |
| 11005 | } |
| 11006 | if (r.connections.size() > 0) { |
| 11007 | if (!force) { |
| 11008 | // XXX should probably keep a count of the number of auto-create |
| 11009 | // connections directly in the service. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11010 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11011 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11012 | ArrayList<ConnectionRecord> cr = it.next(); |
| 11013 | for (int i=0; i<cr.size(); i++) { |
| 11014 | if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) { |
| 11015 | return; |
| 11016 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11017 | } |
| 11018 | } |
| 11019 | } |
| 11020 | |
| 11021 | // Report to all of the connections that the service is no longer |
| 11022 | // available. |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11023 | Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11024 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11025 | ArrayList<ConnectionRecord> c = it.next(); |
| 11026 | for (int i=0; i<c.size(); i++) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11027 | ConnectionRecord cr = c.get(i); |
| 11028 | // There is still a connection to the service that is |
| 11029 | // being brought down. Mark it as dead. |
| 11030 | cr.serviceDead = true; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11031 | try { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11032 | cr.conn.connected(r.name, null); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11033 | } catch (Exception e) { |
| 11034 | Slog.w(TAG, "Failure disconnecting service " + r.name + |
| 11035 | " to connection " + c.get(i).conn.asBinder() + |
| 11036 | " (in " + c.get(i).binding.client.processName + ")", e); |
| 11037 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11038 | } |
| 11039 | } |
| 11040 | } |
| 11041 | |
| 11042 | // Tell the service that it has been unbound. |
| 11043 | if (r.bindings.size() > 0 && r.app != null && r.app.thread != null) { |
| 11044 | Iterator<IntentBindRecord> it = r.bindings.values().iterator(); |
| 11045 | while (it.hasNext()) { |
| 11046 | IntentBindRecord ibr = it.next(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11047 | 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] | 11048 | + ": hasBound=" + ibr.hasBound); |
| 11049 | if (r.app != null && r.app.thread != null && ibr.hasBound) { |
| 11050 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11051 | bumpServiceExecutingLocked(r, "bring down unbind"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11052 | updateOomAdjLocked(r.app); |
| 11053 | ibr.hasBound = false; |
| 11054 | r.app.thread.scheduleUnbindService(r, |
| 11055 | ibr.intent.getIntent()); |
| 11056 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11057 | Slog.w(TAG, "Exception when unbinding service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11058 | + r.shortName, e); |
| 11059 | serviceDoneExecutingLocked(r, true); |
| 11060 | } |
| 11061 | } |
| 11062 | } |
| 11063 | } |
| 11064 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11065 | if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down " + r + " " + r.intent); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 11066 | EventLog.writeEvent(EventLogTags.AM_DESTROY_SERVICE, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11067 | System.identityHashCode(r), r.shortName, |
| 11068 | (r.app != null) ? r.app.pid : -1); |
| 11069 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11070 | mServiceMap.removeServiceByName(r.name, r.userId); |
| 11071 | mServiceMap.removeServiceByIntent(r.intent, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11072 | r.totalRestartCount = 0; |
| 11073 | unscheduleServiceRestartLocked(r); |
| 11074 | |
| 11075 | // Also make sure it is not on the pending list. |
| 11076 | int N = mPendingServices.size(); |
| 11077 | for (int i=0; i<N; i++) { |
| 11078 | if (mPendingServices.get(i) == r) { |
| 11079 | mPendingServices.remove(i); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11080 | if (DEBUG_SERVICE) Slog.v(TAG, "Removed pending: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11081 | i--; |
| 11082 | N--; |
| 11083 | } |
| 11084 | } |
| 11085 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11086 | r.cancelNotification(); |
| 11087 | r.isForeground = false; |
| 11088 | r.foregroundId = 0; |
| 11089 | r.foregroundNoti = null; |
| 11090 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11091 | // Clear start entries. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11092 | r.clearDeliveredStartsLocked(); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11093 | r.pendingStarts.clear(); |
| 11094 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11095 | if (r.app != null) { |
| 11096 | synchronized (r.stats.getBatteryStats()) { |
| 11097 | r.stats.stopLaunchedLocked(); |
| 11098 | } |
| 11099 | r.app.services.remove(r); |
| 11100 | if (r.app.thread != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11101 | try { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11102 | bumpServiceExecutingLocked(r, "stop"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11103 | mStoppingServices.add(r); |
| 11104 | updateOomAdjLocked(r.app); |
| 11105 | r.app.thread.scheduleStopService(r); |
| 11106 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11107 | Slog.w(TAG, "Exception when stopping service " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11108 | + r.shortName, e); |
| 11109 | serviceDoneExecutingLocked(r, true); |
| 11110 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11111 | updateServiceForegroundLocked(r.app, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11112 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11113 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11114 | TAG, "Removed service that has no process: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11115 | } |
| 11116 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11117 | if (DEBUG_SERVICE) Slog.v( |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11118 | TAG, "Removed service that is not running: " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11119 | } |
Vairavan Srinivasan | a207ce2 | 2010-12-23 13:51:48 -0800 | [diff] [blame] | 11120 | |
| 11121 | if (r.bindings.size() > 0) { |
| 11122 | r.bindings.clear(); |
| 11123 | } |
| 11124 | |
| 11125 | if (r.restarter instanceof ServiceRestarter) { |
| 11126 | ((ServiceRestarter)r.restarter).setService(null); |
| 11127 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11128 | } |
| 11129 | |
| 11130 | ComponentName startServiceLocked(IApplicationThread caller, |
| 11131 | Intent service, String resolvedType, |
| 11132 | int callingPid, int callingUid) { |
| 11133 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11134 | if (DEBUG_SERVICE) Slog.v(TAG, "startService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11135 | + " type=" + resolvedType + " args=" + service.getExtras()); |
| 11136 | |
| 11137 | if (caller != null) { |
| 11138 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11139 | if (callerApp == null) { |
| 11140 | throw new SecurityException( |
| 11141 | "Unable to find app for caller " + caller |
| 11142 | + " (pid=" + Binder.getCallingPid() |
| 11143 | + ") when starting service " + service); |
| 11144 | } |
| 11145 | } |
| 11146 | |
| 11147 | ServiceLookupResult res = |
| 11148 | retrieveServiceLocked(service, resolvedType, |
| 11149 | callingPid, callingUid); |
| 11150 | if (res == null) { |
| 11151 | return null; |
| 11152 | } |
| 11153 | if (res.record == null) { |
| 11154 | return new ComponentName("!", res.permission != null |
| 11155 | ? res.permission : "private to package"); |
| 11156 | } |
| 11157 | ServiceRecord r = res.record; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11158 | int targetPermissionUid = checkGrantUriPermissionFromIntentLocked( |
| 11159 | callingUid, r.packageName, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11160 | if (unscheduleServiceRestartLocked(r)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11161 | 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] | 11162 | } |
| 11163 | r.startRequested = true; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11164 | r.callStart = false; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11165 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11166 | service, targetPermissionUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11167 | r.lastActivity = SystemClock.uptimeMillis(); |
| 11168 | synchronized (r.stats.getBatteryStats()) { |
| 11169 | r.stats.startRunningLocked(); |
| 11170 | } |
| 11171 | if (!bringUpServiceLocked(r, service.getFlags(), false)) { |
| 11172 | return new ComponentName("!", "Service process is bad"); |
| 11173 | } |
| 11174 | return r.name; |
| 11175 | } |
| 11176 | } |
| 11177 | |
| 11178 | public ComponentName startService(IApplicationThread caller, Intent service, |
| 11179 | String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11180 | enforceNotIsolatedCaller("startService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11181 | // Refuse possible leaked file descriptors |
| 11182 | if (service != null && service.hasFileDescriptors() == true) { |
| 11183 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11184 | } |
| 11185 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11186 | if (DEBUG_SERVICE) |
| 11187 | Slog.v(TAG, "startService: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11188 | synchronized(this) { |
| 11189 | final int callingPid = Binder.getCallingPid(); |
| 11190 | final int callingUid = Binder.getCallingUid(); |
| 11191 | final long origId = Binder.clearCallingIdentity(); |
| 11192 | ComponentName res = startServiceLocked(caller, service, |
| 11193 | resolvedType, callingPid, callingUid); |
| 11194 | Binder.restoreCallingIdentity(origId); |
| 11195 | return res; |
| 11196 | } |
| 11197 | } |
| 11198 | |
| 11199 | ComponentName startServiceInPackage(int uid, |
| 11200 | Intent service, String resolvedType) { |
| 11201 | synchronized(this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11202 | if (DEBUG_SERVICE) |
| 11203 | Slog.v(TAG, "startServiceInPackage: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11204 | final long origId = Binder.clearCallingIdentity(); |
| 11205 | ComponentName res = startServiceLocked(null, service, |
| 11206 | resolvedType, -1, uid); |
| 11207 | Binder.restoreCallingIdentity(origId); |
| 11208 | return res; |
| 11209 | } |
| 11210 | } |
| 11211 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11212 | private void stopServiceLocked(ServiceRecord service) { |
| 11213 | synchronized (service.stats.getBatteryStats()) { |
| 11214 | service.stats.stopRunningLocked(); |
| 11215 | } |
| 11216 | service.startRequested = false; |
| 11217 | service.callStart = false; |
| 11218 | bringDownServiceLocked(service, false); |
| 11219 | } |
| 11220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11221 | public int stopService(IApplicationThread caller, Intent service, |
| 11222 | String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11223 | enforceNotIsolatedCaller("stopService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11224 | // Refuse possible leaked file descriptors |
| 11225 | if (service != null && service.hasFileDescriptors() == true) { |
| 11226 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11227 | } |
| 11228 | |
| 11229 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11230 | if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11231 | + " type=" + resolvedType); |
| 11232 | |
| 11233 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11234 | if (caller != null && callerApp == null) { |
| 11235 | throw new SecurityException( |
| 11236 | "Unable to find app for caller " + caller |
| 11237 | + " (pid=" + Binder.getCallingPid() |
| 11238 | + ") when stopping service " + service); |
| 11239 | } |
| 11240 | |
| 11241 | // If this service is active, make sure it is stopped. |
| 11242 | ServiceLookupResult r = findServiceLocked(service, resolvedType); |
| 11243 | if (r != null) { |
| 11244 | if (r.record != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11245 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11246 | try { |
| 11247 | stopServiceLocked(r.record); |
| 11248 | } finally { |
| 11249 | Binder.restoreCallingIdentity(origId); |
| 11250 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11251 | return 1; |
| 11252 | } |
| 11253 | return -1; |
| 11254 | } |
| 11255 | } |
| 11256 | |
| 11257 | return 0; |
| 11258 | } |
| 11259 | |
| 11260 | public IBinder peekService(Intent service, String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11261 | enforceNotIsolatedCaller("peekService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11262 | // Refuse possible leaked file descriptors |
| 11263 | if (service != null && service.hasFileDescriptors() == true) { |
| 11264 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11265 | } |
| 11266 | |
| 11267 | IBinder ret = null; |
| 11268 | |
| 11269 | synchronized(this) { |
| 11270 | ServiceLookupResult r = findServiceLocked(service, resolvedType); |
| 11271 | |
| 11272 | if (r != null) { |
| 11273 | // r.record is null if findServiceLocked() failed the caller permission check |
| 11274 | if (r.record == null) { |
| 11275 | throw new SecurityException( |
| 11276 | "Permission Denial: Accessing service " + r.record.name |
| 11277 | + " from pid=" + Binder.getCallingPid() |
| 11278 | + ", uid=" + Binder.getCallingUid() |
| 11279 | + " requires " + r.permission); |
| 11280 | } |
| 11281 | IntentBindRecord ib = r.record.bindings.get(r.record.intent); |
| 11282 | if (ib != null) { |
| 11283 | ret = ib.binder; |
| 11284 | } |
| 11285 | } |
| 11286 | } |
| 11287 | |
| 11288 | return ret; |
| 11289 | } |
| 11290 | |
| 11291 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 11292 | int startId) { |
| 11293 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11294 | if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11295 | + " " + token + " startId=" + startId); |
| 11296 | ServiceRecord r = findServiceLocked(className, token); |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11297 | if (r != null) { |
| 11298 | if (startId >= 0) { |
| 11299 | // Asked to only stop if done with all work. Note that |
| 11300 | // to avoid leaks, we will take this as dropping all |
| 11301 | // start items up to and including this one. |
| 11302 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 11303 | if (si != null) { |
| 11304 | while (r.deliveredStarts.size() > 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 11305 | ServiceRecord.StartItem cur = r.deliveredStarts.remove(0); |
| 11306 | cur.removeUriPermissionsLocked(); |
| 11307 | if (cur == si) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11308 | break; |
| 11309 | } |
| 11310 | } |
| 11311 | } |
| 11312 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11313 | if (r.getLastStartId() != startId) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11314 | return false; |
| 11315 | } |
| 11316 | |
| 11317 | if (r.deliveredStarts.size() > 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11318 | Slog.w(TAG, "stopServiceToken startId " + startId |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11319 | + " is last, but have " + r.deliveredStarts.size() |
| 11320 | + " remaining args"); |
| 11321 | } |
| 11322 | } |
| 11323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11324 | synchronized (r.stats.getBatteryStats()) { |
| 11325 | r.stats.stopRunningLocked(); |
| 11326 | r.startRequested = false; |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11327 | r.callStart = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11328 | } |
| 11329 | final long origId = Binder.clearCallingIdentity(); |
| 11330 | bringDownServiceLocked(r, false); |
| 11331 | Binder.restoreCallingIdentity(origId); |
| 11332 | return true; |
| 11333 | } |
| 11334 | } |
| 11335 | return false; |
| 11336 | } |
| 11337 | |
| 11338 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11339 | int id, Notification notification, boolean removeNotification) { |
| 11340 | final long origId = Binder.clearCallingIdentity(); |
| 11341 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11342 | synchronized(this) { |
| 11343 | ServiceRecord r = findServiceLocked(className, token); |
| 11344 | if (r != null) { |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11345 | if (id != 0) { |
| 11346 | if (notification == null) { |
| 11347 | throw new IllegalArgumentException("null notification"); |
| 11348 | } |
| 11349 | if (r.foregroundId != id) { |
| 11350 | r.cancelNotification(); |
| 11351 | r.foregroundId = id; |
| 11352 | } |
| 11353 | notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 11354 | r.foregroundNoti = notification; |
| 11355 | r.isForeground = true; |
| 11356 | r.postNotification(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11357 | if (r.app != null) { |
| 11358 | updateServiceForegroundLocked(r.app, true); |
| 11359 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11360 | } else { |
| 11361 | if (r.isForeground) { |
| 11362 | r.isForeground = false; |
| 11363 | if (r.app != null) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 11364 | updateLruProcessLocked(r.app, false, true); |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11365 | updateServiceForegroundLocked(r.app, true); |
| 11366 | } |
| 11367 | } |
| 11368 | if (removeNotification) { |
| 11369 | r.cancelNotification(); |
| 11370 | r.foregroundId = 0; |
| 11371 | r.foregroundNoti = null; |
| 11372 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11373 | } |
| 11374 | } |
| 11375 | } |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 11376 | } finally { |
| 11377 | Binder.restoreCallingIdentity(origId); |
| 11378 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11379 | } |
| 11380 | |
| 11381 | public void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) { |
| 11382 | boolean anyForeground = false; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 11383 | for (ServiceRecord sr : proc.services) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11384 | if (sr.isForeground) { |
| 11385 | anyForeground = true; |
| 11386 | break; |
| 11387 | } |
| 11388 | } |
| 11389 | if (anyForeground != proc.foregroundServices) { |
| 11390 | proc.foregroundServices = anyForeground; |
| 11391 | if (oomAdj) { |
| 11392 | updateOomAdjLocked(); |
| 11393 | } |
| 11394 | } |
| 11395 | } |
| 11396 | |
| 11397 | public int bindService(IApplicationThread caller, IBinder token, |
| 11398 | Intent service, String resolvedType, |
| 11399 | IServiceConnection connection, int flags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11400 | enforceNotIsolatedCaller("bindService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11401 | // Refuse possible leaked file descriptors |
| 11402 | if (service != null && service.hasFileDescriptors() == true) { |
| 11403 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11404 | } |
| 11405 | |
| 11406 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11407 | if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11408 | + " type=" + resolvedType + " conn=" + connection.asBinder() |
| 11409 | + " flags=0x" + Integer.toHexString(flags)); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11410 | if (DEBUG_MU) |
| 11411 | Slog.i(TAG_MU, "bindService uid=" + Binder.getCallingUid() + " origUid=" |
| 11412 | + Binder.getOrigCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11413 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11414 | if (callerApp == null) { |
| 11415 | throw new SecurityException( |
| 11416 | "Unable to find app for caller " + caller |
| 11417 | + " (pid=" + Binder.getCallingPid() |
| 11418 | + ") when binding service " + service); |
| 11419 | } |
| 11420 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11421 | ActivityRecord activity = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11422 | if (token != null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 11423 | activity = mMainStack.isInStackLocked(token); |
| 11424 | if (activity == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11425 | Slog.w(TAG, "Binding with unknown activity: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11426 | return 0; |
| 11427 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11428 | } |
| 11429 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11430 | int clientLabel = 0; |
| 11431 | PendingIntent clientIntent = null; |
| 11432 | |
| 11433 | if (callerApp.info.uid == Process.SYSTEM_UID) { |
| 11434 | // Hacky kind of thing -- allow system stuff to tell us |
| 11435 | // what they are, so we can report this elsewhere for |
| 11436 | // others to know why certain services are running. |
| 11437 | try { |
| 11438 | clientIntent = (PendingIntent)service.getParcelableExtra( |
| 11439 | Intent.EXTRA_CLIENT_INTENT); |
| 11440 | } catch (RuntimeException e) { |
| 11441 | } |
| 11442 | if (clientIntent != null) { |
| 11443 | clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0); |
| 11444 | if (clientLabel != 0) { |
| 11445 | // There are no useful extras in the intent, trash them. |
| 11446 | // System code calling with this stuff just needs to know |
| 11447 | // this will happen. |
| 11448 | service = service.cloneFilter(); |
| 11449 | } |
| 11450 | } |
| 11451 | } |
| 11452 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11453 | ServiceLookupResult res = |
| 11454 | retrieveServiceLocked(service, resolvedType, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11455 | Binder.getCallingPid(), Binder.getOrigCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11456 | if (res == null) { |
| 11457 | return 0; |
| 11458 | } |
| 11459 | if (res.record == null) { |
| 11460 | return -1; |
| 11461 | } |
| 11462 | ServiceRecord s = res.record; |
| 11463 | |
| 11464 | final long origId = Binder.clearCallingIdentity(); |
| 11465 | |
| 11466 | if (unscheduleServiceRestartLocked(s)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11467 | if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: " |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11468 | + s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11469 | } |
| 11470 | |
| 11471 | AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp); |
| 11472 | ConnectionRecord c = new ConnectionRecord(b, activity, |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 11473 | connection, flags, clientLabel, clientIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11474 | |
| 11475 | IBinder binder = connection.asBinder(); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11476 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 11477 | if (clist == null) { |
| 11478 | clist = new ArrayList<ConnectionRecord>(); |
| 11479 | s.connections.put(binder, clist); |
| 11480 | } |
| 11481 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11482 | b.connections.add(c); |
| 11483 | if (activity != null) { |
| 11484 | if (activity.connections == null) { |
| 11485 | activity.connections = new HashSet<ConnectionRecord>(); |
| 11486 | } |
| 11487 | activity.connections.add(c); |
| 11488 | } |
| 11489 | b.client.connections.add(c); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 11490 | if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { |
| 11491 | b.client.hasAboveClient = true; |
| 11492 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11493 | clist = mServiceConnections.get(binder); |
| 11494 | if (clist == null) { |
| 11495 | clist = new ArrayList<ConnectionRecord>(); |
| 11496 | mServiceConnections.put(binder, clist); |
| 11497 | } |
| 11498 | clist.add(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11499 | |
| 11500 | if ((flags&Context.BIND_AUTO_CREATE) != 0) { |
| 11501 | s.lastActivity = SystemClock.uptimeMillis(); |
| 11502 | if (!bringUpServiceLocked(s, service.getFlags(), false)) { |
| 11503 | return 0; |
| 11504 | } |
| 11505 | } |
| 11506 | |
| 11507 | if (s.app != null) { |
| 11508 | // This could have made the service more important. |
| 11509 | updateOomAdjLocked(s.app); |
| 11510 | } |
| 11511 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11512 | 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] | 11513 | + ": received=" + b.intent.received |
| 11514 | + " apps=" + b.intent.apps.size() |
| 11515 | + " doRebind=" + b.intent.doRebind); |
| 11516 | |
| 11517 | if (s.app != null && b.intent.received) { |
| 11518 | // Service is already running, so we can immediately |
| 11519 | // publish the connection. |
| 11520 | try { |
| 11521 | c.conn.connected(s.name, b.intent.binder); |
| 11522 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11523 | Slog.w(TAG, "Failure sending service " + s.shortName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11524 | + " to connection " + c.conn.asBinder() |
| 11525 | + " (in " + c.binding.client.processName + ")", e); |
| 11526 | } |
| 11527 | |
| 11528 | // If this is the first app connected back to this binding, |
| 11529 | // and the service had previously asked to be told when |
| 11530 | // rebound, then do so. |
| 11531 | if (b.intent.apps.size() == 1 && b.intent.doRebind) { |
| 11532 | requestServiceBindingLocked(s, b.intent, true); |
| 11533 | } |
| 11534 | } else if (!b.intent.requested) { |
| 11535 | requestServiceBindingLocked(s, b.intent, false); |
| 11536 | } |
| 11537 | |
| 11538 | Binder.restoreCallingIdentity(origId); |
| 11539 | } |
| 11540 | |
| 11541 | return 1; |
| 11542 | } |
| 11543 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 11544 | void removeConnectionLocked( |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 11545 | ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11546 | IBinder binder = c.conn.asBinder(); |
| 11547 | AppBindRecord b = c.binding; |
| 11548 | ServiceRecord s = b.service; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11549 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 11550 | if (clist != null) { |
| 11551 | clist.remove(c); |
| 11552 | if (clist.size() == 0) { |
| 11553 | s.connections.remove(binder); |
| 11554 | } |
| 11555 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11556 | b.connections.remove(c); |
| 11557 | if (c.activity != null && c.activity != skipAct) { |
| 11558 | if (c.activity.connections != null) { |
| 11559 | c.activity.connections.remove(c); |
| 11560 | } |
| 11561 | } |
| 11562 | if (b.client != skipApp) { |
| 11563 | b.client.connections.remove(c); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 11564 | if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { |
| 11565 | b.client.updateHasAboveClientLocked(); |
| 11566 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11567 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11568 | clist = mServiceConnections.get(binder); |
| 11569 | if (clist != null) { |
| 11570 | clist.remove(c); |
| 11571 | if (clist.size() == 0) { |
| 11572 | mServiceConnections.remove(binder); |
| 11573 | } |
| 11574 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11575 | |
| 11576 | if (b.connections.size() == 0) { |
| 11577 | b.intent.apps.remove(b.client); |
| 11578 | } |
| 11579 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11580 | if (!c.serviceDead) { |
| 11581 | if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent |
| 11582 | + ": shouldUnbind=" + b.intent.hasBound); |
| 11583 | if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0 |
| 11584 | && b.intent.hasBound) { |
| 11585 | try { |
| 11586 | bumpServiceExecutingLocked(s, "unbind"); |
| 11587 | updateOomAdjLocked(s.app); |
| 11588 | b.intent.hasBound = false; |
| 11589 | // Assume the client doesn't want to know about a rebind; |
| 11590 | // we will deal with that later if it asks for one. |
| 11591 | b.intent.doRebind = false; |
| 11592 | s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent()); |
| 11593 | } catch (Exception e) { |
| 11594 | Slog.w(TAG, "Exception when unbinding service " + s.shortName, e); |
| 11595 | serviceDoneExecutingLocked(s, true); |
| 11596 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11597 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 11598 | |
| 11599 | if ((c.flags&Context.BIND_AUTO_CREATE) != 0) { |
| 11600 | bringDownServiceLocked(s, false); |
| 11601 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11602 | } |
| 11603 | } |
| 11604 | |
| 11605 | public boolean unbindService(IServiceConnection connection) { |
| 11606 | synchronized (this) { |
| 11607 | IBinder binder = connection.asBinder(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11608 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder); |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11609 | ArrayList<ConnectionRecord> clist = mServiceConnections.get(binder); |
| 11610 | if (clist == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11611 | 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] | 11612 | + connection.asBinder()); |
| 11613 | return false; |
| 11614 | } |
| 11615 | |
| 11616 | final long origId = Binder.clearCallingIdentity(); |
| 11617 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11618 | while (clist.size() > 0) { |
| 11619 | ConnectionRecord r = clist.get(0); |
| 11620 | removeConnectionLocked(r, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11621 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11622 | if (r.binding.service.app != null) { |
| 11623 | // This could have made the service less important. |
| 11624 | updateOomAdjLocked(r.binding.service.app); |
| 11625 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11626 | } |
| 11627 | |
| 11628 | Binder.restoreCallingIdentity(origId); |
| 11629 | } |
| 11630 | |
| 11631 | return true; |
| 11632 | } |
| 11633 | |
| 11634 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 11635 | // Refuse possible leaked file descriptors |
| 11636 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11637 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11638 | } |
| 11639 | |
| 11640 | synchronized(this) { |
| 11641 | if (!(token instanceof ServiceRecord)) { |
| 11642 | throw new IllegalArgumentException("Invalid service token"); |
| 11643 | } |
| 11644 | ServiceRecord r = (ServiceRecord)token; |
| 11645 | |
| 11646 | final long origId = Binder.clearCallingIdentity(); |
| 11647 | |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11648 | if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11649 | + " " + intent + ": " + service); |
| 11650 | if (r != null) { |
| 11651 | Intent.FilterComparison filter |
| 11652 | = new Intent.FilterComparison(intent); |
| 11653 | IntentBindRecord b = r.bindings.get(filter); |
| 11654 | if (b != null && !b.received) { |
| 11655 | b.binder = service; |
| 11656 | b.requested = true; |
| 11657 | b.received = true; |
| 11658 | if (r.connections.size() > 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11659 | Iterator<ArrayList<ConnectionRecord>> it |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11660 | = r.connections.values().iterator(); |
| 11661 | while (it.hasNext()) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11662 | ArrayList<ConnectionRecord> clist = it.next(); |
| 11663 | for (int i=0; i<clist.size(); i++) { |
| 11664 | ConnectionRecord c = clist.get(i); |
| 11665 | if (!filter.equals(c.binding.intent.intent)) { |
| 11666 | if (DEBUG_SERVICE) Slog.v( |
| 11667 | TAG, "Not publishing to: " + c); |
| 11668 | if (DEBUG_SERVICE) Slog.v( |
| 11669 | TAG, "Bound intent: " + c.binding.intent.intent); |
| 11670 | if (DEBUG_SERVICE) Slog.v( |
| 11671 | TAG, "Published intent: " + intent); |
| 11672 | continue; |
| 11673 | } |
| 11674 | if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c); |
| 11675 | try { |
| 11676 | c.conn.connected(r.name, service); |
| 11677 | } catch (Exception e) { |
| 11678 | Slog.w(TAG, "Failure sending service " + r.name + |
| 11679 | " to connection " + c.conn.asBinder() + |
| 11680 | " (in " + c.binding.client.processName + ")", e); |
| 11681 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11682 | } |
| 11683 | } |
| 11684 | } |
| 11685 | } |
| 11686 | |
| 11687 | serviceDoneExecutingLocked(r, mStoppingServices.contains(r)); |
| 11688 | |
| 11689 | Binder.restoreCallingIdentity(origId); |
| 11690 | } |
| 11691 | } |
| 11692 | } |
| 11693 | |
| 11694 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 11695 | // Refuse possible leaked file descriptors |
| 11696 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11697 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11698 | } |
| 11699 | |
| 11700 | synchronized(this) { |
| 11701 | if (!(token instanceof ServiceRecord)) { |
| 11702 | throw new IllegalArgumentException("Invalid service token"); |
| 11703 | } |
| 11704 | ServiceRecord r = (ServiceRecord)token; |
| 11705 | |
| 11706 | final long origId = Binder.clearCallingIdentity(); |
| 11707 | |
| 11708 | if (r != null) { |
| 11709 | Intent.FilterComparison filter |
| 11710 | = new Intent.FilterComparison(intent); |
| 11711 | IntentBindRecord b = r.bindings.get(filter); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11712 | if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11713 | + " at " + b + ": apps=" |
| 11714 | + (b != null ? b.apps.size() : 0)); |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11715 | |
| 11716 | boolean inStopping = mStoppingServices.contains(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11717 | if (b != null) { |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11718 | if (b.apps.size() > 0 && !inStopping) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11719 | // Applications have already bound since the last |
| 11720 | // unbind, so just rebind right here. |
| 11721 | requestServiceBindingLocked(r, b, true); |
| 11722 | } else { |
| 11723 | // Note to tell the service the next time there is |
| 11724 | // a new client. |
| 11725 | b.doRebind = true; |
| 11726 | } |
| 11727 | } |
| 11728 | |
Per Edelberg | 78f9fff | 2010-08-30 20:01:35 +0200 | [diff] [blame] | 11729 | serviceDoneExecutingLocked(r, inStopping); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11730 | |
| 11731 | Binder.restoreCallingIdentity(origId); |
| 11732 | } |
| 11733 | } |
| 11734 | } |
| 11735 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11736 | 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] | 11737 | synchronized(this) { |
| 11738 | if (!(token instanceof ServiceRecord)) { |
| 11739 | throw new IllegalArgumentException("Invalid service token"); |
| 11740 | } |
| 11741 | ServiceRecord r = (ServiceRecord)token; |
| 11742 | boolean inStopping = mStoppingServices.contains(token); |
| 11743 | if (r != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11744 | if (r != token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11745 | Slog.w(TAG, "Done executing service " + r.name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11746 | + " with incorrect token: given " + token |
| 11747 | + ", expected " + r); |
| 11748 | return; |
| 11749 | } |
| 11750 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11751 | if (type == 1) { |
| 11752 | // This is a call from a service start... take care of |
| 11753 | // book-keeping. |
| 11754 | r.callStart = true; |
| 11755 | switch (res) { |
| 11756 | case Service.START_STICKY_COMPATIBILITY: |
| 11757 | case Service.START_STICKY: { |
| 11758 | // We are done with the associated start arguments. |
| 11759 | r.findDeliveredStart(startId, true); |
| 11760 | // Don't stop if killed. |
| 11761 | r.stopIfKilled = false; |
| 11762 | break; |
| 11763 | } |
| 11764 | case Service.START_NOT_STICKY: { |
| 11765 | // We are done with the associated start arguments. |
| 11766 | r.findDeliveredStart(startId, true); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11767 | if (r.getLastStartId() == startId) { |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11768 | // There is no more work, and this service |
| 11769 | // doesn't want to hang around if killed. |
| 11770 | r.stopIfKilled = true; |
| 11771 | } |
| 11772 | break; |
| 11773 | } |
| 11774 | case Service.START_REDELIVER_INTENT: { |
| 11775 | // We'll keep this item until they explicitly |
| 11776 | // call stop for it, but keep track of the fact |
| 11777 | // that it was delivered. |
| 11778 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 11779 | if (si != null) { |
| 11780 | si.deliveryCount = 0; |
| 11781 | si.doneExecutingCount++; |
| 11782 | // Don't stop if killed. |
| 11783 | r.stopIfKilled = true; |
| 11784 | } |
| 11785 | break; |
| 11786 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 11787 | case Service.START_TASK_REMOVED_COMPLETE: { |
| 11788 | // Special processing for onTaskRemoved(). Don't |
| 11789 | // impact normal onStartCommand() processing. |
| 11790 | r.findDeliveredStart(startId, true); |
| 11791 | break; |
| 11792 | } |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11793 | default: |
| 11794 | throw new IllegalArgumentException( |
| 11795 | "Unknown service start result: " + res); |
| 11796 | } |
| 11797 | if (res == Service.START_STICKY_COMPATIBILITY) { |
| 11798 | r.callStart = false; |
| 11799 | } |
| 11800 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11801 | if (DEBUG_MU) |
| 11802 | Slog.v(TAG_MU, "before serviceDontExecutingLocked, uid=" |
| 11803 | + Binder.getOrigCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11804 | final long origId = Binder.clearCallingIdentity(); |
| 11805 | serviceDoneExecutingLocked(r, inStopping); |
| 11806 | Binder.restoreCallingIdentity(origId); |
| 11807 | } else { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11808 | Slog.w(TAG, "Done executing unknown service from pid " |
| 11809 | + Binder.getCallingPid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11810 | } |
| 11811 | } |
| 11812 | } |
| 11813 | |
| 11814 | public void serviceDoneExecutingLocked(ServiceRecord r, boolean inStopping) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 11815 | if (DEBUG_SERVICE) Slog.v(TAG, "<<< DONE EXECUTING " + r |
| 11816 | + ": nesting=" + r.executeNesting |
| 11817 | + ", inStopping=" + inStopping + ", app=" + r.app); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11818 | 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] | 11819 | r.executeNesting--; |
| 11820 | if (r.executeNesting <= 0 && r.app != null) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11821 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 11822 | "Nesting at 0 of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11823 | r.app.executingServices.remove(r); |
| 11824 | if (r.app.executingServices.size() == 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11825 | if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG, |
| 11826 | "No more executingServices of " + r.shortName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11827 | mHandler.removeMessages(SERVICE_TIMEOUT_MSG, r.app); |
| 11828 | } |
| 11829 | if (inStopping) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 11830 | if (DEBUG_SERVICE) Slog.v(TAG, |
| 11831 | "doneExecuting remove stopping " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11832 | mStoppingServices.remove(r); |
Mattias Petersson | 3996b41 | 2010-10-27 09:32:51 +0200 | [diff] [blame] | 11833 | r.bindings.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11834 | } |
| 11835 | updateOomAdjLocked(r.app); |
| 11836 | } |
| 11837 | } |
| 11838 | |
| 11839 | void serviceTimeout(ProcessRecord proc) { |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11840 | String anrMessage = null; |
| 11841 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11842 | synchronized(this) { |
| 11843 | if (proc.executingServices.size() == 0 || proc.thread == null) { |
| 11844 | return; |
| 11845 | } |
| 11846 | long maxTime = SystemClock.uptimeMillis() - SERVICE_TIMEOUT; |
| 11847 | Iterator<ServiceRecord> it = proc.executingServices.iterator(); |
| 11848 | ServiceRecord timeout = null; |
| 11849 | long nextTime = 0; |
| 11850 | while (it.hasNext()) { |
| 11851 | ServiceRecord sr = it.next(); |
| 11852 | if (sr.executingStart < maxTime) { |
| 11853 | timeout = sr; |
| 11854 | break; |
| 11855 | } |
| 11856 | if (sr.executingStart > nextTime) { |
| 11857 | nextTime = sr.executingStart; |
| 11858 | } |
| 11859 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 11860 | if (timeout != null && mLruProcesses.contains(proc)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11861 | Slog.w(TAG, "Timeout executing service: " + timeout); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11862 | anrMessage = "Executing service " + timeout.shortName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11863 | } else { |
| 11864 | Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 11865 | msg.obj = proc; |
| 11866 | mHandler.sendMessageAtTime(msg, nextTime+SERVICE_TIMEOUT); |
| 11867 | } |
| 11868 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11869 | |
| 11870 | if (anrMessage != null) { |
| 11871 | appNotResponding(proc, null, null, anrMessage); |
| 11872 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11873 | } |
| 11874 | |
| 11875 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11876 | // BACKUP AND RESTORE |
| 11877 | // ========================================================= |
| 11878 | |
| 11879 | // Cause the target app to be launched if necessary and its backup agent |
| 11880 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 11881 | // activity manager to announce its creation. |
| 11882 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11883 | if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11884 | enforceCallingPermission("android.permission.BACKUP", "startBackupAgent"); |
| 11885 | |
| 11886 | synchronized(this) { |
| 11887 | // !!! TODO: currently no check here that we're already bound |
| 11888 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 11889 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 11890 | synchronized (stats) { |
| 11891 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 11892 | } |
| 11893 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11894 | // Backup agent is now in use, its package can't be stopped. |
| 11895 | try { |
| 11896 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 11897 | app.packageName, false); |
| 11898 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 11899 | } catch (IllegalArgumentException e) { |
| 11900 | Slog.w(TAG, "Failed trying to unstop package " |
| 11901 | + app.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11902 | } |
| 11903 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11904 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 11905 | ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) |
| 11906 | ? new ComponentName(app.packageName, app.backupAgentName) |
| 11907 | : new ComponentName("android", "FullBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11908 | // startProcessLocked() returns existing proc's record if it's already running |
| 11909 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11910 | false, 0, "backup", hostingName, false, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11911 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11912 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11913 | return false; |
| 11914 | } |
| 11915 | |
| 11916 | r.app = proc; |
| 11917 | mBackupTarget = r; |
| 11918 | mBackupAppName = app.packageName; |
| 11919 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11920 | // Try not to kill the process during backup |
| 11921 | updateOomAdjLocked(proc); |
| 11922 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11923 | // If the process is already attached, schedule the creation of the backup agent now. |
| 11924 | // If it is not yet live, this will be done when it attaches to the framework. |
| 11925 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11926 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11927 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11928 | proc.thread.scheduleCreateBackupAgent(app, |
| 11929 | compatibilityInfoForPackageLocked(app), backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11930 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 11931 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11932 | } |
| 11933 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11934 | 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] | 11935 | } |
| 11936 | // Invariants: at this point, the target app process exists and the application |
| 11937 | // is either already running or in the process of coming up. mBackupTarget and |
| 11938 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 11939 | // know that it's scheduled for a backup-agent operation. |
| 11940 | } |
| 11941 | |
| 11942 | return true; |
| 11943 | } |
| 11944 | |
| 11945 | // A backup agent has just come up |
| 11946 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11947 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11948 | + " = " + agent); |
| 11949 | |
| 11950 | synchronized(this) { |
| 11951 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11952 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11953 | return; |
| 11954 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11955 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11956 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11957 | long oldIdent = Binder.clearCallingIdentity(); |
| 11958 | try { |
| 11959 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 11960 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 11961 | bm.agentConnected(agentPackageName, agent); |
| 11962 | } catch (RemoteException e) { |
| 11963 | // can't happen; the backup manager service is local |
| 11964 | } catch (Exception e) { |
| 11965 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 11966 | e.printStackTrace(); |
| 11967 | } finally { |
| 11968 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11969 | } |
| 11970 | } |
| 11971 | |
| 11972 | // done with this agent |
| 11973 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11974 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11975 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11976 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11977 | return; |
| 11978 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11979 | |
| 11980 | synchronized(this) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11981 | if (mBackupAppName == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11982 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11983 | return; |
| 11984 | } |
| 11985 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11986 | if (!mBackupAppName.equals(appInfo.packageName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11987 | 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] | 11988 | return; |
| 11989 | } |
| 11990 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11991 | ProcessRecord proc = mBackupTarget.app; |
| 11992 | mBackupTarget = null; |
| 11993 | mBackupAppName = null; |
| 11994 | |
| 11995 | // Not backing this app up any more; reset its OOM adjustment |
| 11996 | updateOomAdjLocked(proc); |
| 11997 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11998 | // If the app crashed during backup, 'thread' will be null here |
| 11999 | if (proc.thread != null) { |
| 12000 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 12001 | proc.thread.scheduleDestroyBackupAgent(appInfo, |
| 12002 | compatibilityInfoForPackageLocked(appInfo)); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 12003 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12004 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 12005 | e.printStackTrace(); |
| 12006 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12007 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 12008 | } |
| 12009 | } |
| 12010 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12011 | // BROADCASTS |
| 12012 | // ========================================================= |
| 12013 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12014 | private final List getStickiesLocked(String action, IntentFilter filter, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12015 | List cur) { |
| 12016 | final ContentResolver resolver = mContext.getContentResolver(); |
| 12017 | final ArrayList<Intent> list = mStickyBroadcasts.get(action); |
| 12018 | if (list == null) { |
| 12019 | return cur; |
| 12020 | } |
| 12021 | int N = list.size(); |
| 12022 | for (int i=0; i<N; i++) { |
| 12023 | Intent intent = list.get(i); |
| 12024 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 12025 | if (cur == null) { |
| 12026 | cur = new ArrayList<Intent>(); |
| 12027 | } |
| 12028 | cur.add(intent); |
| 12029 | } |
| 12030 | } |
| 12031 | return cur; |
| 12032 | } |
| 12033 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12034 | boolean isPendingBroadcastProcessLocked(int pid) { |
| 12035 | return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid) |
| 12036 | || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid); |
| 12037 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12038 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12039 | void skipPendingBroadcastLocked(int pid) { |
| 12040 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
| 12041 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 12042 | queue.skipPendingBroadcastLocked(pid); |
| 12043 | } |
| 12044 | } |
| 12045 | |
| 12046 | // The app just attached; send any pending broadcasts that it should receive |
| 12047 | boolean sendPendingBroadcastsLocked(ProcessRecord app) { |
| 12048 | boolean didSomething = false; |
| 12049 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 12050 | didSomething |= queue.sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12051 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12052 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12053 | } |
| 12054 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 12055 | public Intent registerReceiver(IApplicationThread caller, String callerPackage, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12056 | IIntentReceiver receiver, IntentFilter filter, String permission) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12057 | enforceNotIsolatedCaller("registerReceiver"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12058 | synchronized(this) { |
| 12059 | ProcessRecord callerApp = null; |
| 12060 | if (caller != null) { |
| 12061 | callerApp = getRecordForAppLocked(caller); |
| 12062 | if (callerApp == null) { |
| 12063 | throw new SecurityException( |
| 12064 | "Unable to find app for caller " + caller |
| 12065 | + " (pid=" + Binder.getCallingPid() |
| 12066 | + ") when registering receiver " + receiver); |
| 12067 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 12068 | if (callerApp.info.uid != Process.SYSTEM_UID && |
| 12069 | !callerApp.pkgList.contains(callerPackage)) { |
| 12070 | throw new SecurityException("Given caller package " + callerPackage |
| 12071 | + " is not running in process " + callerApp); |
| 12072 | } |
| 12073 | } else { |
| 12074 | callerPackage = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12075 | } |
| 12076 | |
| 12077 | List allSticky = null; |
| 12078 | |
| 12079 | // Look for any matching sticky broadcasts... |
| 12080 | Iterator actions = filter.actionsIterator(); |
| 12081 | if (actions != null) { |
| 12082 | while (actions.hasNext()) { |
| 12083 | String action = (String)actions.next(); |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12084 | allSticky = getStickiesLocked(action, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12085 | } |
| 12086 | } else { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12087 | allSticky = getStickiesLocked(null, filter, allSticky); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12088 | } |
| 12089 | |
| 12090 | // The first sticky in the list is returned directly back to |
| 12091 | // the client. |
| 12092 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 12093 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12094 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12095 | + ": " + sticky); |
| 12096 | |
| 12097 | if (receiver == null) { |
| 12098 | return sticky; |
| 12099 | } |
| 12100 | |
| 12101 | ReceiverList rl |
| 12102 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 12103 | if (rl == null) { |
| 12104 | rl = new ReceiverList(this, callerApp, |
| 12105 | Binder.getCallingPid(), |
| 12106 | Binder.getCallingUid(), receiver); |
| 12107 | if (rl.app != null) { |
| 12108 | rl.app.receivers.add(rl); |
| 12109 | } else { |
| 12110 | try { |
| 12111 | receiver.asBinder().linkToDeath(rl, 0); |
| 12112 | } catch (RemoteException e) { |
| 12113 | return sticky; |
| 12114 | } |
| 12115 | rl.linkedToDeath = true; |
| 12116 | } |
| 12117 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
| 12118 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 12119 | BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, permission); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12120 | rl.add(bf); |
| 12121 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12122 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12123 | } |
| 12124 | mReceiverResolver.addFilter(bf); |
| 12125 | |
| 12126 | // Enqueue broadcasts for all existing stickies that match |
| 12127 | // this filter. |
| 12128 | if (allSticky != null) { |
| 12129 | ArrayList receivers = new ArrayList(); |
| 12130 | receivers.add(bf); |
| 12131 | |
| 12132 | int N = allSticky.size(); |
| 12133 | for (int i=0; i<N; i++) { |
| 12134 | Intent intent = (Intent)allSticky.get(i); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12135 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 12136 | BroadcastRecord r = new BroadcastRecord(queue, intent, null, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12137 | null, -1, -1, null, receivers, null, 0, null, null, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12138 | false, true, true); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12139 | queue.enqueueParallelBroadcastLocked(r); |
| 12140 | queue.scheduleBroadcastsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12141 | } |
| 12142 | } |
| 12143 | |
| 12144 | return sticky; |
| 12145 | } |
| 12146 | } |
| 12147 | |
| 12148 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12149 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12150 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12151 | final long origId = Binder.clearCallingIdentity(); |
| 12152 | try { |
| 12153 | boolean doTrim = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12154 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12155 | synchronized(this) { |
| 12156 | ReceiverList rl |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12157 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12158 | if (rl != null) { |
| 12159 | if (rl.curBroadcast != null) { |
| 12160 | BroadcastRecord r = rl.curBroadcast; |
| 12161 | final boolean doNext = finishReceiverLocked( |
| 12162 | receiver.asBinder(), r.resultCode, r.resultData, |
| 12163 | r.resultExtras, r.resultAbort, true); |
| 12164 | if (doNext) { |
| 12165 | doTrim = true; |
| 12166 | r.queue.processNextBroadcast(false); |
| 12167 | } |
| 12168 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12169 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12170 | if (rl.app != null) { |
| 12171 | rl.app.receivers.remove(rl); |
| 12172 | } |
| 12173 | removeReceiverLocked(rl); |
| 12174 | if (rl.linkedToDeath) { |
| 12175 | rl.linkedToDeath = false; |
| 12176 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 12177 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12178 | } |
| 12179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12180 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12181 | // If we actually concluded any broadcasts, we might now be able |
| 12182 | // to trim the recipients' apps from our working set |
| 12183 | if (doTrim) { |
| 12184 | trimApplications(); |
| 12185 | return; |
| 12186 | } |
| 12187 | |
| 12188 | } finally { |
| 12189 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12190 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12191 | } |
| 12192 | |
| 12193 | void removeReceiverLocked(ReceiverList rl) { |
| 12194 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 12195 | int N = rl.size(); |
| 12196 | for (int i=0; i<N; i++) { |
| 12197 | mReceiverResolver.removeFilter(rl.get(i)); |
| 12198 | } |
| 12199 | } |
| 12200 | |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 12201 | private final void sendPackageBroadcastLocked(int cmd, String[] packages) { |
| 12202 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 12203 | ProcessRecord r = mLruProcesses.get(i); |
| 12204 | if (r.thread != null) { |
| 12205 | try { |
| 12206 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 12207 | } catch (RemoteException ex) { |
| 12208 | } |
| 12209 | } |
| 12210 | } |
| 12211 | } |
| 12212 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12213 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 12214 | String callerPackage, Intent intent, String resolvedType, |
| 12215 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 12216 | Bundle map, String requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12217 | boolean ordered, boolean sticky, int callingPid, int callingUid, |
| 12218 | int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12219 | intent = new Intent(intent); |
| 12220 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 12221 | // By default broadcasts do not go to stopped apps. |
| 12222 | intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES); |
| 12223 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12224 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12225 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
| 12226 | + " ordered=" + ordered); |
| 12227 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12228 | 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] | 12229 | } |
| 12230 | |
| 12231 | // Handle special intents: if this broadcast is from the package |
| 12232 | // manager about a package being removed, we need to remove all of |
| 12233 | // its activities from the history stack. |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12234 | final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12235 | intent.getAction()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12236 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 12237 | || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 12238 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12239 | || uidRemoved) { |
| 12240 | if (checkComponentPermission( |
| 12241 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 12242 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12243 | == PackageManager.PERMISSION_GRANTED) { |
| 12244 | if (uidRemoved) { |
| 12245 | final Bundle intentExtras = intent.getExtras(); |
| 12246 | final int uid = intentExtras != null |
| 12247 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 12248 | if (uid >= 0) { |
| 12249 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 12250 | synchronized (bs) { |
| 12251 | bs.removeUidStatsLocked(uid); |
| 12252 | } |
| 12253 | } |
| 12254 | } else { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12255 | // If resources are unvailble just force stop all |
| 12256 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 12257 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12258 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 12259 | if (list != null && (list.length > 0)) { |
| 12260 | for (String pkg : list) { |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12261 | forceStopPackageLocked(pkg, -1, false, true, true, false); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12262 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 12263 | sendPackageBroadcastLocked( |
| 12264 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12265 | } |
| 12266 | } else { |
| 12267 | Uri data = intent.getData(); |
| 12268 | String ssp; |
| 12269 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 12270 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 12271 | forceStopPackageLocked(ssp, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12272 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true, false); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 12273 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12274 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 12275 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
| 12276 | new String[] {ssp}); |
| 12277 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12278 | } |
| 12279 | } |
| 12280 | } |
| 12281 | } else { |
| 12282 | String msg = "Permission Denial: " + intent.getAction() |
| 12283 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 12284 | + ", uid=" + callingUid + ")" |
| 12285 | + " requires " |
| 12286 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12287 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12288 | throw new SecurityException(msg); |
| 12289 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12290 | |
| 12291 | // Special case for adding a package: by default turn on compatibility |
| 12292 | // mode. |
| 12293 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 12294 | Uri data = intent.getData(); |
| 12295 | String ssp; |
| 12296 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 12297 | mCompatModePackages.handlePackageAddedLocked(ssp, |
| 12298 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12299 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12300 | } |
| 12301 | |
| 12302 | /* |
| 12303 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 12304 | * of all currently running processes. This message will get queued up before the broadcast |
| 12305 | * happens. |
| 12306 | */ |
| 12307 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 12308 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 12309 | } |
| 12310 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 12311 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 12312 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 12313 | } |
| 12314 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 12315 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 12316 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 12317 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 12318 | } |
| 12319 | |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12320 | /* |
| 12321 | * Prevent non-system code (defined here to be non-persistent |
| 12322 | * processes) from sending protected broadcasts. |
| 12323 | */ |
| 12324 | if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID |
| 12325 | || callingUid == Process.SHELL_UID || callingUid == 0) { |
| 12326 | // Always okay. |
| 12327 | } else if (callerApp == null || !callerApp.persistent) { |
| 12328 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12329 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12330 | intent.getAction())) { |
| 12331 | String msg = "Permission Denial: not allowed to send broadcast " |
| 12332 | + intent.getAction() + " from pid=" |
| 12333 | + callingPid + ", uid=" + callingUid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12334 | Slog.w(TAG, msg); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12335 | throw new SecurityException(msg); |
| 12336 | } |
| 12337 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12338 | Slog.w(TAG, "Remote exception", e); |
Dianne Hackborn | 854060af | 2009-07-09 18:14:31 -0700 | [diff] [blame] | 12339 | return BROADCAST_SUCCESS; |
| 12340 | } |
| 12341 | } |
| 12342 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12343 | // Add to the sticky list if requested. |
| 12344 | if (sticky) { |
| 12345 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 12346 | callingPid, callingUid) |
| 12347 | != PackageManager.PERMISSION_GRANTED) { |
| 12348 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 12349 | + callingPid + ", uid=" + callingUid |
| 12350 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12351 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12352 | throw new SecurityException(msg); |
| 12353 | } |
| 12354 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12355 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12356 | + " and enforce permission " + requiredPermission); |
| 12357 | return BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
| 12358 | } |
| 12359 | if (intent.getComponent() != null) { |
| 12360 | throw new SecurityException( |
| 12361 | "Sticky broadcasts can't target a specific component"); |
| 12362 | } |
| 12363 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 12364 | if (list == null) { |
| 12365 | list = new ArrayList<Intent>(); |
| 12366 | mStickyBroadcasts.put(intent.getAction(), list); |
| 12367 | } |
| 12368 | int N = list.size(); |
| 12369 | int i; |
| 12370 | for (i=0; i<N; i++) { |
| 12371 | if (intent.filterEquals(list.get(i))) { |
| 12372 | // This sticky already exists, replace it. |
| 12373 | list.set(i, new Intent(intent)); |
| 12374 | break; |
| 12375 | } |
| 12376 | } |
| 12377 | if (i >= N) { |
| 12378 | list.add(new Intent(intent)); |
| 12379 | } |
| 12380 | } |
| 12381 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12382 | // Figure out who all will receive this broadcast. |
| 12383 | List receivers = null; |
| 12384 | List<BroadcastFilter> registeredReceivers = null; |
| 12385 | try { |
| 12386 | if (intent.getComponent() != null) { |
| 12387 | // Broadcast is going to one specific receiver class... |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12388 | ActivityInfo ai = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12389 | getReceiverInfo(intent.getComponent(), STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12390 | if (ai != null) { |
| 12391 | receivers = new ArrayList(); |
| 12392 | ResolveInfo ri = new ResolveInfo(); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12393 | ri.activityInfo = getActivityInfoForUser(ai, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12394 | receivers.add(ri); |
| 12395 | } |
| 12396 | } else { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12397 | // TODO: Apply userId |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12398 | // Need to resolve the intent to interested receivers... |
| 12399 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 12400 | == 0) { |
| 12401 | receivers = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 12402 | AppGlobals.getPackageManager().queryIntentReceivers( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12403 | intent, resolvedType, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12404 | } |
Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 12405 | registeredReceivers = mReceiverResolver.queryIntent(intent, resolvedType, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12406 | } |
| 12407 | } catch (RemoteException ex) { |
| 12408 | // pm is in same process, this will never happen. |
| 12409 | } |
| 12410 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12411 | final boolean replacePending = |
| 12412 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 12413 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12414 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12415 | + " replacePending=" + replacePending); |
| 12416 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12417 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 12418 | if (!ordered && NR > 0) { |
| 12419 | // If we are not serializing this broadcast, then send the |
| 12420 | // registered receivers separately so they don't wait for the |
| 12421 | // components to be launched. |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12422 | final BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 12423 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12424 | callerPackage, callingPid, callingUid, requiredPermission, |
| 12425 | registeredReceivers, resultTo, resultCode, resultData, map, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12426 | ordered, sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12427 | if (DEBUG_BROADCAST) Slog.v( |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12428 | TAG, "Enqueueing parallel broadcast " + r); |
| 12429 | final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12430 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12431 | queue.enqueueParallelBroadcastLocked(r); |
| 12432 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12433 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12434 | registeredReceivers = null; |
| 12435 | NR = 0; |
| 12436 | } |
| 12437 | |
| 12438 | // Merge into one list. |
| 12439 | int ir = 0; |
| 12440 | if (receivers != null) { |
| 12441 | // A special case for PACKAGE_ADDED: do not allow the package |
| 12442 | // being added to see this broadcast. This prevents them from |
| 12443 | // using this as a back door to get run as soon as they are |
| 12444 | // installed. Maybe in the future we want to have a special install |
| 12445 | // broadcast or such for apps, but we'd like to deliberately make |
| 12446 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12447 | String skipPackages[] = null; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12448 | if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 12449 | || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 12450 | || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12451 | Uri data = intent.getData(); |
| 12452 | if (data != null) { |
| 12453 | String pkgName = data.getSchemeSpecificPart(); |
| 12454 | if (pkgName != null) { |
| 12455 | skipPackages = new String[] { pkgName }; |
| 12456 | } |
| 12457 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 12458 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12459 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 12460 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 12461 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 12462 | for (String skipPackage : skipPackages) { |
| 12463 | if (skipPackage != null) { |
| 12464 | int NT = receivers.size(); |
| 12465 | for (int it=0; it<NT; it++) { |
| 12466 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 12467 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 12468 | receivers.remove(it); |
| 12469 | it--; |
| 12470 | NT--; |
| 12471 | } |
| 12472 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12473 | } |
| 12474 | } |
| 12475 | } |
| 12476 | |
| 12477 | int NT = receivers != null ? receivers.size() : 0; |
| 12478 | int it = 0; |
| 12479 | ResolveInfo curt = null; |
| 12480 | BroadcastFilter curr = null; |
| 12481 | while (it < NT && ir < NR) { |
| 12482 | if (curt == null) { |
| 12483 | curt = (ResolveInfo)receivers.get(it); |
| 12484 | } |
| 12485 | if (curr == null) { |
| 12486 | curr = registeredReceivers.get(ir); |
| 12487 | } |
| 12488 | if (curr.getPriority() >= curt.priority) { |
| 12489 | // Insert this broadcast record into the final list. |
| 12490 | receivers.add(it, curr); |
| 12491 | ir++; |
| 12492 | curr = null; |
| 12493 | it++; |
| 12494 | NT++; |
| 12495 | } else { |
| 12496 | // Skip to the next ResolveInfo in the final list. |
| 12497 | it++; |
| 12498 | curt = null; |
| 12499 | } |
| 12500 | } |
| 12501 | } |
| 12502 | while (ir < NR) { |
| 12503 | if (receivers == null) { |
| 12504 | receivers = new ArrayList(); |
| 12505 | } |
| 12506 | receivers.add(registeredReceivers.get(ir)); |
| 12507 | ir++; |
| 12508 | } |
| 12509 | |
| 12510 | if ((receivers != null && receivers.size() > 0) |
| 12511 | || resultTo != null) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12512 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 12513 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12514 | callerPackage, callingPid, callingUid, requiredPermission, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 12515 | receivers, resultTo, resultCode, resultData, map, ordered, |
| 12516 | sticky, false); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12517 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12518 | TAG, "Enqueueing ordered broadcast " + r |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12519 | + ": prev had " + queue.mOrderedBroadcasts.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12520 | if (DEBUG_BROADCAST) { |
| 12521 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12522 | 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] | 12523 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12524 | boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12525 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12526 | queue.enqueueOrderedBroadcastLocked(r); |
| 12527 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12528 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12529 | } |
| 12530 | |
| 12531 | return BROADCAST_SUCCESS; |
| 12532 | } |
| 12533 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12534 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12535 | // Refuse possible leaked file descriptors |
| 12536 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 12537 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12538 | } |
| 12539 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12540 | int flags = intent.getFlags(); |
| 12541 | |
| 12542 | if (!mProcessesReady) { |
| 12543 | // if the caller really truly claims to know what they're doing, go |
| 12544 | // ahead and allow the broadcast without launching any receivers |
| 12545 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 12546 | intent = new Intent(intent); |
| 12547 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 12548 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 12549 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 12550 | + " before boot completion"); |
| 12551 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 12552 | } |
| 12553 | } |
| 12554 | |
| 12555 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 12556 | throw new IllegalArgumentException( |
| 12557 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 12558 | } |
| 12559 | |
| 12560 | return intent; |
| 12561 | } |
| 12562 | |
| 12563 | public final int broadcastIntent(IApplicationThread caller, |
| 12564 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 12565 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12566 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12567 | enforceNotIsolatedCaller("broadcastIntent"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12568 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12569 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 12570 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12571 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 12572 | final int callingPid = Binder.getCallingPid(); |
| 12573 | final int callingUid = Binder.getCallingUid(); |
| 12574 | final long origId = Binder.clearCallingIdentity(); |
| 12575 | int res = broadcastIntentLocked(callerApp, |
| 12576 | callerApp != null ? callerApp.info.packageName : null, |
| 12577 | intent, resolvedType, resultTo, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12578 | resultCode, resultData, map, requiredPermission, serialized, sticky, |
| 12579 | callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12580 | Binder.restoreCallingIdentity(origId); |
| 12581 | return res; |
| 12582 | } |
| 12583 | } |
| 12584 | |
| 12585 | int broadcastIntentInPackage(String packageName, int uid, |
| 12586 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 12587 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12588 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12589 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 12590 | intent = verifyBroadcastLocked(intent); |
| 12591 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12592 | final long origId = Binder.clearCallingIdentity(); |
| 12593 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 12594 | resultTo, resultCode, resultData, map, requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12595 | serialized, sticky, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12596 | Binder.restoreCallingIdentity(origId); |
| 12597 | return res; |
| 12598 | } |
| 12599 | } |
| 12600 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12601 | // TODO: Use the userId; maybe mStickyBroadcasts need to be tied to the user. |
| 12602 | public final void unbroadcastIntent(IApplicationThread caller, Intent intent, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12603 | // Refuse possible leaked file descriptors |
| 12604 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 12605 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12606 | } |
| 12607 | |
| 12608 | synchronized(this) { |
| 12609 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 12610 | != PackageManager.PERMISSION_GRANTED) { |
| 12611 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 12612 | + Binder.getCallingPid() |
| 12613 | + ", uid=" + Binder.getCallingUid() |
| 12614 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12615 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12616 | throw new SecurityException(msg); |
| 12617 | } |
| 12618 | ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction()); |
| 12619 | if (list != null) { |
| 12620 | int N = list.size(); |
| 12621 | int i; |
| 12622 | for (i=0; i<N; i++) { |
| 12623 | if (intent.filterEquals(list.get(i))) { |
| 12624 | list.remove(i); |
| 12625 | break; |
| 12626 | } |
| 12627 | } |
| 12628 | } |
| 12629 | } |
| 12630 | } |
| 12631 | |
| 12632 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 12633 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 12634 | boolean explicit) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12635 | final BroadcastRecord r = broadcastRecordForReceiverLocked(receiver); |
| 12636 | if (r == null) { |
| 12637 | Slog.w(TAG, "finishReceiver called but not found on queue"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12638 | return false; |
| 12639 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12640 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12641 | return r.queue.finishReceiverLocked(r, resultCode, resultData, resultExtras, resultAbort, |
| 12642 | explicit); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12643 | } |
| 12644 | |
| 12645 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 12646 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12647 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12648 | |
| 12649 | // Refuse possible leaked file descriptors |
| 12650 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 12651 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12652 | } |
| 12653 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12654 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12655 | try { |
| 12656 | boolean doNext = false; |
| 12657 | BroadcastRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12658 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12659 | synchronized(this) { |
| 12660 | r = broadcastRecordForReceiverLocked(who); |
| 12661 | if (r != null) { |
| 12662 | doNext = r.queue.finishReceiverLocked(r, resultCode, |
| 12663 | resultData, resultExtras, resultAbort, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12664 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12665 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12666 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12667 | if (doNext) { |
| 12668 | r.queue.processNextBroadcast(false); |
| 12669 | } |
| 12670 | trimApplications(); |
| 12671 | } finally { |
| 12672 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12673 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12674 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12675 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12676 | // ========================================================= |
| 12677 | // INSTRUMENTATION |
| 12678 | // ========================================================= |
| 12679 | |
| 12680 | public boolean startInstrumentation(ComponentName className, |
| 12681 | String profileFile, int flags, Bundle arguments, |
| 12682 | IInstrumentationWatcher watcher) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12683 | enforceNotIsolatedCaller("startInstrumentation"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12684 | // Refuse possible leaked file descriptors |
| 12685 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 12686 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12687 | } |
| 12688 | |
| 12689 | synchronized(this) { |
| 12690 | InstrumentationInfo ii = null; |
| 12691 | ApplicationInfo ai = null; |
| 12692 | try { |
| 12693 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12694 | className, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12695 | ai = mContext.getPackageManager().getApplicationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12696 | ii.targetPackage, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12697 | } catch (PackageManager.NameNotFoundException e) { |
| 12698 | } |
| 12699 | if (ii == null) { |
| 12700 | reportStartInstrumentationFailure(watcher, className, |
| 12701 | "Unable to find instrumentation info for: " + className); |
| 12702 | return false; |
| 12703 | } |
| 12704 | if (ai == null) { |
| 12705 | reportStartInstrumentationFailure(watcher, className, |
| 12706 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 12707 | return false; |
| 12708 | } |
| 12709 | |
| 12710 | int match = mContext.getPackageManager().checkSignatures( |
| 12711 | ii.targetPackage, ii.packageName); |
| 12712 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 12713 | String msg = "Permission Denial: starting instrumentation " |
| 12714 | + className + " from pid=" |
| 12715 | + Binder.getCallingPid() |
| 12716 | + ", uid=" + Binder.getCallingPid() |
| 12717 | + " not allowed because package " + ii.packageName |
| 12718 | + " does not have a signature matching the target " |
| 12719 | + ii.targetPackage; |
| 12720 | reportStartInstrumentationFailure(watcher, className, msg); |
| 12721 | throw new SecurityException(msg); |
| 12722 | } |
| 12723 | |
| 12724 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12725 | // Instrumentation can kill and relaunch even persistent processes |
| 12726 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 12727 | ProcessRecord app = addAppLocked(ai, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12728 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12729 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12730 | app.instrumentationProfileFile = profileFile; |
| 12731 | app.instrumentationArguments = arguments; |
| 12732 | app.instrumentationWatcher = watcher; |
| 12733 | app.instrumentationResultClass = className; |
| 12734 | Binder.restoreCallingIdentity(origId); |
| 12735 | } |
| 12736 | |
| 12737 | return true; |
| 12738 | } |
| 12739 | |
| 12740 | /** |
| 12741 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 12742 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 12743 | * the "am" command to report errors with more information. |
| 12744 | * |
| 12745 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 12746 | * @param cn The component name of the instrumentation. |
| 12747 | * @param report The error report. |
| 12748 | */ |
| 12749 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 12750 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12751 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12752 | try { |
| 12753 | if (watcher != null) { |
| 12754 | Bundle results = new Bundle(); |
| 12755 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 12756 | results.putString("Error", report); |
| 12757 | watcher.instrumentationStatus(cn, -1, results); |
| 12758 | } |
| 12759 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12760 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12761 | } |
| 12762 | } |
| 12763 | |
| 12764 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 12765 | if (app.instrumentationWatcher != null) { |
| 12766 | try { |
| 12767 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 12768 | app.instrumentationWatcher.instrumentationFinished( |
| 12769 | app.instrumentationClass, |
| 12770 | resultCode, |
| 12771 | results); |
| 12772 | } catch (RemoteException e) { |
| 12773 | } |
| 12774 | } |
| 12775 | app.instrumentationWatcher = null; |
| 12776 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12777 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12778 | app.instrumentationProfileFile = null; |
| 12779 | app.instrumentationArguments = null; |
| 12780 | |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12781 | forceStopPackageLocked(app.processName, -1, false, false, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12782 | } |
| 12783 | |
| 12784 | public void finishInstrumentation(IApplicationThread target, |
| 12785 | int resultCode, Bundle results) { |
| 12786 | // Refuse possible leaked file descriptors |
| 12787 | if (results != null && results.hasFileDescriptors()) { |
| 12788 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12789 | } |
| 12790 | |
| 12791 | synchronized(this) { |
| 12792 | ProcessRecord app = getRecordForAppLocked(target); |
| 12793 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12794 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12795 | return; |
| 12796 | } |
| 12797 | final long origId = Binder.clearCallingIdentity(); |
| 12798 | finishInstrumentationLocked(app, resultCode, results); |
| 12799 | Binder.restoreCallingIdentity(origId); |
| 12800 | } |
| 12801 | } |
| 12802 | |
| 12803 | // ========================================================= |
| 12804 | // CONFIGURATION |
| 12805 | // ========================================================= |
| 12806 | |
| 12807 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 12808 | ConfigurationInfo config = new ConfigurationInfo(); |
| 12809 | synchronized (this) { |
| 12810 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 12811 | config.reqKeyboardType = mConfiguration.keyboard; |
| 12812 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12813 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 12814 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12815 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 12816 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12817 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 12818 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12819 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 12820 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 12821 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12822 | } |
| 12823 | return config; |
| 12824 | } |
| 12825 | |
| 12826 | public Configuration getConfiguration() { |
| 12827 | Configuration ci; |
| 12828 | synchronized(this) { |
| 12829 | ci = new Configuration(mConfiguration); |
| 12830 | } |
| 12831 | return ci; |
| 12832 | } |
| 12833 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12834 | public void updatePersistentConfiguration(Configuration values) { |
| 12835 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12836 | "updateConfiguration()"); |
| 12837 | enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS, |
| 12838 | "updateConfiguration()"); |
| 12839 | if (values == null) { |
| 12840 | throw new NullPointerException("Configuration must not be null"); |
| 12841 | } |
| 12842 | |
| 12843 | synchronized(this) { |
| 12844 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12845 | updateConfigurationLocked(values, null, true, false); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12846 | Binder.restoreCallingIdentity(origId); |
| 12847 | } |
| 12848 | } |
| 12849 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12850 | public void updateConfiguration(Configuration values) { |
| 12851 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12852 | "updateConfiguration()"); |
| 12853 | |
| 12854 | synchronized(this) { |
| 12855 | if (values == null && mWindowManager != null) { |
| 12856 | // sentinel: fetch the current configuration from the window manager |
| 12857 | values = mWindowManager.computeNewConfiguration(); |
| 12858 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12859 | |
| 12860 | if (mWindowManager != null) { |
| 12861 | mProcessList.applyDisplaySize(mWindowManager); |
| 12862 | } |
| 12863 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12864 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12865 | if (values != null) { |
| 12866 | Settings.System.clearConfiguration(values); |
| 12867 | } |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12868 | updateConfigurationLocked(values, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12869 | Binder.restoreCallingIdentity(origId); |
| 12870 | } |
| 12871 | } |
| 12872 | |
| 12873 | /** |
| 12874 | * Do either or both things: (1) change the current configuration, and (2) |
| 12875 | * make sure the given activity is running with the (now) current |
| 12876 | * configuration. Returns true if the activity has been left running, or |
| 12877 | * false if <var>starting</var> is being destroyed to match the new |
| 12878 | * configuration. |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12879 | * @param persistent TODO |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12880 | */ |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12881 | boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12882 | ActivityRecord starting, boolean persistent, boolean initLocale) { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 12883 | // do nothing if we are headless |
| 12884 | if (mHeadless) return true; |
| 12885 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12886 | int changes = 0; |
| 12887 | |
| 12888 | boolean kept = true; |
| 12889 | |
| 12890 | if (values != null) { |
| 12891 | Configuration newConfig = new Configuration(mConfiguration); |
| 12892 | changes = newConfig.updateFrom(values); |
| 12893 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12894 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12895 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12896 | } |
| 12897 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12898 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12899 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12900 | if (values.locale != null && !initLocale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12901 | saveLocaleLocked(values.locale, |
| 12902 | !values.locale.equals(mConfiguration.locale), |
| 12903 | values.userSetLocale); |
| 12904 | } |
| 12905 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12906 | mConfigurationSeq++; |
| 12907 | if (mConfigurationSeq <= 0) { |
| 12908 | mConfigurationSeq = 1; |
| 12909 | } |
| 12910 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12911 | mConfiguration = newConfig; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12912 | Slog.i(TAG, "Config changed: " + newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12913 | |
| 12914 | final Configuration configCopy = new Configuration(mConfiguration); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12915 | |
| 12916 | // TODO: If our config changes, should we auto dismiss any currently |
| 12917 | // showing dialogs? |
| 12918 | mShowDialogs = shouldShowDialogs(newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12919 | |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12920 | AttributeCache ac = AttributeCache.instance(); |
| 12921 | if (ac != null) { |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12922 | ac.updateConfiguration(configCopy); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12923 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12924 | |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12925 | // Make sure all resources in our process are updated |
| 12926 | // right now, so that anyone who is going to retrieve |
| 12927 | // resource values after we return will be sure to get |
| 12928 | // the new ones. This is especially important during |
| 12929 | // boot, where the first config change needs to guarantee |
| 12930 | // all resources have that config before following boot |
| 12931 | // code is executed. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12932 | mSystemThread.applyConfigurationToResources(configCopy); |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12933 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12934 | if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12935 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12936 | msg.obj = new Configuration(configCopy); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12937 | mHandler.sendMessage(msg); |
| 12938 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12939 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12940 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 12941 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12942 | try { |
| 12943 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12944 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12945 | + app.processName + " new config " + mConfiguration); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12946 | app.thread.scheduleConfigurationChanged(configCopy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12947 | } |
| 12948 | } catch (Exception e) { |
| 12949 | } |
| 12950 | } |
| 12951 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12952 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 12953 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12954 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12955 | null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12956 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
| 12957 | broadcastIntentLocked(null, null, |
| 12958 | new Intent(Intent.ACTION_LOCALE_CHANGED), |
| 12959 | null, null, 0, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 12960 | null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12961 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12962 | } |
| 12963 | } |
| 12964 | |
| 12965 | if (changes != 0 && starting == null) { |
| 12966 | // If the configuration changed, and the caller is not already |
| 12967 | // in the process of starting an activity, then find the top |
| 12968 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12969 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12970 | } |
| 12971 | |
| 12972 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12973 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 12974 | // And we need to make sure at this point that all other activities |
| 12975 | // are made visible with the correct configuration. |
| 12976 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12977 | } |
| 12978 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12979 | if (values != null && mWindowManager != null) { |
| 12980 | mWindowManager.setNewConfiguration(mConfiguration); |
| 12981 | } |
| 12982 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12983 | return kept; |
| 12984 | } |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12985 | |
| 12986 | /** |
| 12987 | * Decide based on the configuration whether we should shouw the ANR, |
| 12988 | * crash, etc dialogs. The idea is that if there is no affordnace to |
| 12989 | * press the on-screen buttons, we shouldn't show the dialog. |
| 12990 | * |
| 12991 | * A thought: SystemUI might also want to get told about this, the Power |
| 12992 | * dialog / global actions also might want different behaviors. |
| 12993 | */ |
| 12994 | private static final boolean shouldShowDialogs(Configuration config) { |
| 12995 | return !(config.keyboard == Configuration.KEYBOARD_NOKEYS |
| 12996 | && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH); |
| 12997 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12998 | |
| 12999 | /** |
| 13000 | * Save the locale. You must be inside a synchronized (this) block. |
| 13001 | */ |
| 13002 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 13003 | if(isDiff) { |
| 13004 | SystemProperties.set("user.language", l.getLanguage()); |
| 13005 | SystemProperties.set("user.region", l.getCountry()); |
| 13006 | } |
| 13007 | |
| 13008 | if(isPersist) { |
| 13009 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 13010 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 13011 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 13012 | } |
| 13013 | } |
| 13014 | |
| 13015 | // ========================================================= |
| 13016 | // LIFETIME MANAGEMENT |
| 13017 | // ========================================================= |
| 13018 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13019 | // Returns which broadcast queue the app is the current [or imminent] receiver |
| 13020 | // on, or 'null' if the app is not an active broadcast recipient. |
| 13021 | private BroadcastQueue isReceivingBroadcast(ProcessRecord app) { |
| 13022 | BroadcastRecord r = app.curReceiver; |
| 13023 | if (r != null) { |
| 13024 | return r.queue; |
| 13025 | } |
| 13026 | |
| 13027 | // It's not the current receiver, but it might be starting up to become one |
| 13028 | synchronized (this) { |
| 13029 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 13030 | r = queue.mPendingBroadcast; |
| 13031 | if (r != null && r.curApp == app) { |
| 13032 | // found it; report which queue it's in |
| 13033 | return queue; |
| 13034 | } |
| 13035 | } |
| 13036 | } |
| 13037 | |
| 13038 | return null; |
| 13039 | } |
| 13040 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13041 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13042 | ProcessRecord TOP_APP, boolean recursed, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13043 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13044 | // This adjustment has already been computed. If we are calling |
| 13045 | // from the top, we may have already computed our adjustment with |
| 13046 | // an earlier hidden adjustment that isn't really for us... if |
| 13047 | // so, use the new hidden adjustment. |
| 13048 | if (!recursed && app.hidden) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13049 | app.curAdj = app.curRawAdj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13050 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13051 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13052 | } |
| 13053 | |
| 13054 | if (app.thread == null) { |
| 13055 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13056 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13057 | return (app.curAdj=ProcessList.HIDDEN_APP_MAX_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13058 | } |
| 13059 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13060 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
| 13061 | app.adjSource = null; |
| 13062 | app.adjTarget = null; |
| 13063 | app.empty = false; |
| 13064 | app.hidden = false; |
| 13065 | |
| 13066 | final int activitiesSize = app.activities.size(); |
| 13067 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13068 | if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13069 | // The max adjustment doesn't allow this app to be anything |
| 13070 | // below foreground, so it is not worth doing work for it. |
| 13071 | app.adjType = "fixed"; |
| 13072 | app.adjSeq = mAdjSeq; |
| 13073 | app.curRawAdj = app.maxAdj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13074 | app.foregroundActivities = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13075 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13076 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13077 | // System process can do UI, and when they do we want to have |
| 13078 | // them trim their memory after the user leaves the UI. To |
| 13079 | // facilitate this, here we need to determine whether or not it |
| 13080 | // is currently showing UI. |
| 13081 | app.systemNoUi = true; |
| 13082 | if (app == TOP_APP) { |
| 13083 | app.systemNoUi = false; |
| 13084 | } else if (activitiesSize > 0) { |
| 13085 | for (int j = 0; j < activitiesSize; j++) { |
| 13086 | final ActivityRecord r = app.activities.get(j); |
| 13087 | if (r.visible) { |
| 13088 | app.systemNoUi = false; |
| 13089 | break; |
| 13090 | } |
| 13091 | } |
| 13092 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13093 | return (app.curAdj=app.maxAdj); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13094 | } |
| 13095 | |
| 13096 | final boolean hadForegroundActivities = app.foregroundActivities; |
| 13097 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13098 | app.foregroundActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13099 | app.keeping = false; |
| 13100 | app.systemNoUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13101 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 13102 | // Determine the importance of the process, starting with most |
| 13103 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13104 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13105 | int schedGroup; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13106 | BroadcastQueue queue; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13107 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13108 | // The last app on the list is the foreground app. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13109 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13110 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13111 | app.adjType = "top-activity"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13112 | app.foregroundActivities = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13113 | } else if (app.instrumentationClass != null) { |
| 13114 | // Don't want to kill running instrumentation. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13115 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13116 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13117 | app.adjType = "instrumentation"; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13118 | } else if ((queue = isReceivingBroadcast(app)) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13119 | // An app that is currently receiving a broadcast also |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13120 | // counts as being in the foreground for OOM killer purposes. |
| 13121 | // It's placed in a sched group based on the nature of the |
| 13122 | // broadcast as reflected by which queue it's active in. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13123 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13124 | schedGroup = (queue == mFgBroadcastQueue) |
| 13125 | ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13126 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13127 | } else if (app.executingServices.size() > 0) { |
| 13128 | // An app that is currently executing a service callback also |
| 13129 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13130 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13131 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13132 | app.adjType = "exec-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13133 | } else if (activitiesSize > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13134 | // This app is in the background with paused activities. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13135 | // We inspect activities to potentially upgrade adjustment further below. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13136 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13137 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13138 | app.hidden = true; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13139 | app.adjType = "bg-activities"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13140 | } else { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13141 | // A very not-needed process. If this is lower in the lru list, |
| 13142 | // we will push it in to the empty bucket. |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13143 | adj = hiddenAdj; |
| 13144 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13145 | app.hidden = true; |
| 13146 | app.empty = true; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13147 | app.adjType = "bg-empty"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13148 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13149 | |
| 13150 | // Examine all activities if not already foreground. |
| 13151 | if (!app.foregroundActivities && activitiesSize > 0) { |
| 13152 | for (int j = 0; j < activitiesSize; j++) { |
| 13153 | final ActivityRecord r = app.activities.get(j); |
| 13154 | if (r.visible) { |
| 13155 | // App has a visible activity; only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13156 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 13157 | adj = ProcessList.VISIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13158 | app.adjType = "visible"; |
| 13159 | } |
| 13160 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13161 | app.hidden = false; |
| 13162 | app.foregroundActivities = true; |
| 13163 | break; |
| 13164 | } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED |
| 13165 | || r.state == ActivityState.STOPPING) { |
| 13166 | // Only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13167 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13168 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13169 | app.adjType = "stopping"; |
| 13170 | } |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 13171 | app.hidden = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13172 | app.foregroundActivities = true; |
| 13173 | } |
| 13174 | } |
| 13175 | } |
| 13176 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13177 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13178 | if (app.foregroundServices) { |
| 13179 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13180 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13181 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13182 | app.adjType = "foreground-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13183 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13184 | } else if (app.forcingToForeground != null) { |
| 13185 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13186 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13187 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13188 | app.adjType = "force-foreground"; |
| 13189 | app.adjSource = app.forcingToForeground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13190 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13191 | } |
| 13192 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13193 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13194 | if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13195 | // We don't want to kill the current heavy-weight process. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13196 | adj = ProcessList.HEAVY_WEIGHT_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13197 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13198 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13199 | app.adjType = "heavy"; |
| 13200 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13201 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13202 | if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13203 | // This process is hosting what we currently consider to be the |
| 13204 | // 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] | 13205 | adj = ProcessList.HOME_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13206 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13207 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 13208 | app.adjType = "home"; |
| 13209 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13210 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 13211 | if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess |
| 13212 | && app.activities.size() > 0) { |
| 13213 | // This was the previous process that showed UI to the user. |
| 13214 | // We want to try to keep it around more aggressively, to give |
| 13215 | // a good experience around switching between two apps. |
| 13216 | adj = ProcessList.PREVIOUS_APP_ADJ; |
| 13217 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 13218 | app.hidden = false; |
| 13219 | app.adjType = "previous"; |
| 13220 | } |
| 13221 | |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13222 | if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj |
| 13223 | + " reason=" + app.adjType); |
| 13224 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 13225 | // 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] | 13226 | // there are applications dependent on our services or providers, but |
| 13227 | // this gives us a baseline and makes sure we don't get into an |
| 13228 | // infinite recursion. |
| 13229 | app.adjSeq = mAdjSeq; |
| 13230 | app.curRawAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13231 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 13232 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 13233 | // 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] | 13234 | if (adj > ProcessList.BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13235 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13236 | adj = ProcessList.BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13237 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13238 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 13239 | } |
| 13240 | } |
| 13241 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13242 | if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13243 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13244 | final long now = SystemClock.uptimeMillis(); |
| 13245 | // This process is more important if the top activity is |
| 13246 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13247 | Iterator<ServiceRecord> jt = app.services.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13248 | while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13249 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13250 | if (s.startRequested) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13251 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13252 | // If this process has shown some UI, let it immediately |
| 13253 | // go to the LRU list because it may be pretty heavy with |
| 13254 | // UI stuff. We'll tag it with a label just to help |
| 13255 | // debug and understand what is going on. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13256 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13257 | app.adjType = "started-bg-ui-services"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13258 | } |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13259 | } else { |
| 13260 | if (now < (s.lastActivity+MAX_SERVICE_INACTIVITY)) { |
| 13261 | // This service has seen some activity within |
| 13262 | // recent memory, so we will keep its process ahead |
| 13263 | // of the background processes. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13264 | if (adj > ProcessList.SERVICE_ADJ) { |
| 13265 | adj = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13266 | app.adjType = "started-services"; |
| 13267 | app.hidden = false; |
| 13268 | } |
| 13269 | } |
| 13270 | // If we have let the service slide into the background |
| 13271 | // state, still have some text describing what it is doing |
| 13272 | // even though the service no longer has an impact. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13273 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 13274 | app.adjType = "started-bg-services"; |
| 13275 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13276 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13277 | // Don't kill this process because it is doing work; it |
| 13278 | // has said it is doing work. |
| 13279 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13280 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13281 | if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13282 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13283 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13284 | = s.connections.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13285 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13286 | ArrayList<ConnectionRecord> clist = kt.next(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13287 | 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] | 13288 | // XXX should compute this based on the max of |
| 13289 | // all connected clients. |
| 13290 | ConnectionRecord cr = clist.get(i); |
| 13291 | if (cr.binding.client == app) { |
| 13292 | // Binding to ourself is not interesting. |
| 13293 | continue; |
| 13294 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13295 | if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13296 | ProcessRecord client = cr.binding.client; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13297 | int clientAdj = adj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13298 | int myHiddenAdj = hiddenAdj; |
| 13299 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13300 | if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13301 | myHiddenAdj = client.hiddenAdj; |
| 13302 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13303 | myHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13304 | } |
| 13305 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13306 | clientAdj = computeOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13307 | client, myHiddenAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13308 | String adjType = null; |
| 13309 | if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) { |
| 13310 | // Not doing bind OOM management, so treat |
| 13311 | // this guy more like a started service. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13312 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13313 | // If this process has shown some UI, let it immediately |
| 13314 | // go to the LRU list because it may be pretty heavy with |
| 13315 | // UI stuff. We'll tag it with a label just to help |
| 13316 | // debug and understand what is going on. |
| 13317 | if (adj > clientAdj) { |
| 13318 | adjType = "bound-bg-ui-services"; |
| 13319 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13320 | app.hidden = false; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13321 | clientAdj = adj; |
| 13322 | } else { |
| 13323 | if (now >= (s.lastActivity+MAX_SERVICE_INACTIVITY)) { |
| 13324 | // This service has not seen activity within |
| 13325 | // recent memory, so allow it to drop to the |
| 13326 | // LRU list if there is no other reason to keep |
| 13327 | // it around. We'll also tag it with a label just |
| 13328 | // to help debug and undertand what is going on. |
| 13329 | if (adj > clientAdj) { |
| 13330 | adjType = "bound-bg-services"; |
| 13331 | } |
| 13332 | clientAdj = adj; |
| 13333 | } |
| 13334 | } |
| 13335 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13336 | if (adj > clientAdj) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13337 | // If this process has recently shown UI, and |
| 13338 | // the process that is binding to it is less |
| 13339 | // important than being visible, then we don't |
| 13340 | // care about the binding as much as we care |
| 13341 | // about letting this process get into the LRU |
| 13342 | // list to be killed and restarted if needed for |
| 13343 | // memory. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13344 | if (app.hasShownUi && app != mHomeProcess |
| 13345 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13346 | adjType = "bound-bg-ui-services"; |
| 13347 | } else { |
| 13348 | if ((cr.flags&(Context.BIND_ABOVE_CLIENT |
| 13349 | |Context.BIND_IMPORTANT)) != 0) { |
| 13350 | adj = clientAdj; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 13351 | } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0 |
| 13352 | && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ |
| 13353 | && adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13354 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13355 | } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13356 | adj = clientAdj; |
| 13357 | } else { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 13358 | app.pendingUiClean = true; |
| 13359 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 13360 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13361 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13362 | } |
| 13363 | if (!client.hidden) { |
| 13364 | app.hidden = false; |
| 13365 | } |
| 13366 | if (client.keeping) { |
| 13367 | app.keeping = true; |
| 13368 | } |
| 13369 | adjType = "service"; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13370 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13371 | } |
| 13372 | if (adjType != null) { |
| 13373 | app.adjType = adjType; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13374 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13375 | .REASON_SERVICE_IN_USE; |
| 13376 | app.adjSource = cr.binding.client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13377 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 13378 | app.adjTarget = s.name; |
| 13379 | } |
| 13380 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 13381 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 13382 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13383 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13384 | } |
| 13385 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13386 | if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) { |
| 13387 | ActivityRecord a = cr.activity; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13388 | if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ && |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13389 | (a.visible || a.state == ActivityState.RESUMED |
| 13390 | || a.state == ActivityState.PAUSING)) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13391 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13392 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 13393 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13394 | } |
| 13395 | app.hidden = false; |
| 13396 | app.adjType = "service"; |
| 13397 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13398 | .REASON_SERVICE_IN_USE; |
| 13399 | app.adjSource = a; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13400 | app.adjSourceOom = adj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13401 | app.adjTarget = s.name; |
| 13402 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13403 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13404 | } |
| 13405 | } |
| 13406 | } |
| 13407 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13408 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13409 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13410 | // would like to avoid killing it unless it would prevent the current |
| 13411 | // application from running. By default we put the process in |
| 13412 | // with the rest of the background processes; as we scan through |
| 13413 | // its services we may bump it up from there. |
| 13414 | if (adj > hiddenAdj) { |
| 13415 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13416 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 13417 | app.adjType = "bg-services"; |
| 13418 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13419 | } |
| 13420 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13421 | if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13422 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13423 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13424 | while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13425 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 13426 | ContentProviderRecord cpr = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13427 | if (cpr.clients.size() != 0) { |
| 13428 | Iterator<ProcessRecord> kt = cpr.clients.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13429 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13430 | ProcessRecord client = kt.next(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 13431 | if (client == app) { |
| 13432 | // Being our own client is not interesting. |
| 13433 | continue; |
| 13434 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13435 | int myHiddenAdj = hiddenAdj; |
| 13436 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13437 | if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13438 | myHiddenAdj = client.hiddenAdj; |
| 13439 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13440 | myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13441 | } |
| 13442 | } |
| 13443 | int clientAdj = computeOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13444 | client, myHiddenAdj, TOP_APP, true, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13445 | if (adj > clientAdj) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 13446 | if (app.hasShownUi && app != mHomeProcess |
| 13447 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13448 | app.adjType = "bg-ui-provider"; |
| 13449 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13450 | adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ |
| 13451 | ? clientAdj : ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13452 | app.adjType = "provider"; |
| 13453 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13454 | if (!client.hidden) { |
| 13455 | app.hidden = false; |
| 13456 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13457 | if (client.keeping) { |
| 13458 | app.keeping = true; |
| 13459 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 13460 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13461 | .REASON_PROVIDER_IN_USE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13462 | app.adjSource = client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13463 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 13464 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13465 | } |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13466 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 13467 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13468 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13469 | } |
| 13470 | } |
| 13471 | // If the provider has external (non-framework) process |
| 13472 | // dependencies, ensure that its adjustment is at least |
| 13473 | // FOREGROUND_APP_ADJ. |
| 13474 | if (cpr.externals != 0) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13475 | if (adj > ProcessList.FOREGROUND_APP_ADJ) { |
| 13476 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13477 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13478 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13479 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13480 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 13481 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13482 | } |
| 13483 | } |
| 13484 | } |
| 13485 | } |
| 13486 | |
| 13487 | app.curRawAdj = adj; |
| 13488 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13489 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13490 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 13491 | if (adj > app.maxAdj) { |
| 13492 | adj = app.maxAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13493 | if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13494 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13495 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13496 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13497 | if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13498 | app.keeping = true; |
| 13499 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13500 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13501 | if (app.hasAboveClient) { |
| 13502 | // If this process has bound to any services with BIND_ABOVE_CLIENT, |
| 13503 | // then we need to drop its adjustment to be lower than the service's |
| 13504 | // in order to honor the request. We want to drop it by one adjustment |
| 13505 | // level... but there is special meaning applied to various levels so |
| 13506 | // we will skip some of them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13507 | if (adj < ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13508 | // System process will not get dropped, ever |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13509 | } else if (adj < ProcessList.VISIBLE_APP_ADJ) { |
| 13510 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13511 | } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13512 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13513 | } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13514 | adj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13515 | } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13516 | adj++; |
| 13517 | } |
| 13518 | } |
| 13519 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13520 | if (adj == ProcessList.SERVICE_ADJ) { |
| 13521 | if (doingAll) { |
| 13522 | app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3); |
| 13523 | mNewNumServiceProcs++; |
| 13524 | } |
| 13525 | if (app.serviceb) { |
| 13526 | adj = ProcessList.SERVICE_B_ADJ; |
| 13527 | } |
| 13528 | } else { |
| 13529 | app.serviceb = false; |
| 13530 | } |
| 13531 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13532 | app.curAdj = adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13533 | app.curSchedGroup = schedGroup; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13534 | |
| 13535 | if (hadForegroundActivities != app.foregroundActivities) { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 13536 | mHandler.obtainMessage(DISPATCH_FOREGROUND_ACTIVITIES_CHANGED, app.pid, app.info.uid, |
| 13537 | app.foregroundActivities).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13538 | } |
| 13539 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13540 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13541 | } |
| 13542 | |
| 13543 | /** |
| 13544 | * Ask a given process to GC right now. |
| 13545 | */ |
| 13546 | final void performAppGcLocked(ProcessRecord app) { |
| 13547 | try { |
| 13548 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 13549 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13550 | if (app.reportLowMemory) { |
| 13551 | app.reportLowMemory = false; |
| 13552 | app.thread.scheduleLowMemory(); |
| 13553 | } else { |
| 13554 | app.thread.processInBackground(); |
| 13555 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13556 | } |
| 13557 | } catch (Exception e) { |
| 13558 | // whatever. |
| 13559 | } |
| 13560 | } |
| 13561 | |
| 13562 | /** |
| 13563 | * Returns true if things are idle enough to perform GCs. |
| 13564 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13565 | private final boolean canGcNowLocked() { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13566 | boolean processingBroadcasts = false; |
| 13567 | for (BroadcastQueue q : mBroadcastQueues) { |
| 13568 | if (q.mParallelBroadcasts.size() != 0 || q.mOrderedBroadcasts.size() != 0) { |
| 13569 | processingBroadcasts = true; |
| 13570 | } |
| 13571 | } |
| 13572 | return !processingBroadcasts |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13573 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 13574 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13575 | } |
| 13576 | |
| 13577 | /** |
| 13578 | * Perform GCs on all processes that are waiting for it, but only |
| 13579 | * if things are idle. |
| 13580 | */ |
| 13581 | final void performAppGcsLocked() { |
| 13582 | final int N = mProcessesToGc.size(); |
| 13583 | if (N <= 0) { |
| 13584 | return; |
| 13585 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13586 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13587 | while (mProcessesToGc.size() > 0) { |
| 13588 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13589 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13590 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 13591 | <= SystemClock.uptimeMillis()) { |
| 13592 | // To avoid spamming the system, we will GC processes one |
| 13593 | // at a time, waiting a few seconds between each. |
| 13594 | performAppGcLocked(proc); |
| 13595 | scheduleAppGcsLocked(); |
| 13596 | return; |
| 13597 | } else { |
| 13598 | // It hasn't been long enough since we last GCed this |
| 13599 | // process... put it in the list to wait for its time. |
| 13600 | addProcessToGcListLocked(proc); |
| 13601 | break; |
| 13602 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13603 | } |
| 13604 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13605 | |
| 13606 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13607 | } |
| 13608 | } |
| 13609 | |
| 13610 | /** |
| 13611 | * If all looks good, perform GCs on all processes waiting for them. |
| 13612 | */ |
| 13613 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13614 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13615 | performAppGcsLocked(); |
| 13616 | return; |
| 13617 | } |
| 13618 | // Still not idle, wait some more. |
| 13619 | scheduleAppGcsLocked(); |
| 13620 | } |
| 13621 | |
| 13622 | /** |
| 13623 | * Schedule the execution of all pending app GCs. |
| 13624 | */ |
| 13625 | final void scheduleAppGcsLocked() { |
| 13626 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13627 | |
| 13628 | if (mProcessesToGc.size() > 0) { |
| 13629 | // Schedule a GC for the time to the next process. |
| 13630 | ProcessRecord proc = mProcessesToGc.get(0); |
| 13631 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 13632 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13633 | long when = proc.lastRequestedGc + GC_MIN_INTERVAL; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13634 | long now = SystemClock.uptimeMillis(); |
| 13635 | if (when < (now+GC_TIMEOUT)) { |
| 13636 | when = now + GC_TIMEOUT; |
| 13637 | } |
| 13638 | mHandler.sendMessageAtTime(msg, when); |
| 13639 | } |
| 13640 | } |
| 13641 | |
| 13642 | /** |
| 13643 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 13644 | * list in sorted order by the last GC time. The process can't already be |
| 13645 | * on the list. |
| 13646 | */ |
| 13647 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 13648 | boolean added = false; |
| 13649 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 13650 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 13651 | proc.lastRequestedGc) { |
| 13652 | added = true; |
| 13653 | mProcessesToGc.add(i+1, proc); |
| 13654 | break; |
| 13655 | } |
| 13656 | } |
| 13657 | if (!added) { |
| 13658 | mProcessesToGc.add(0, proc); |
| 13659 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13660 | } |
| 13661 | |
| 13662 | /** |
| 13663 | * Set up to ask a process to GC itself. This will either do it |
| 13664 | * immediately, or put it on the list of processes to gc the next |
| 13665 | * time things are idle. |
| 13666 | */ |
| 13667 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 13668 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13669 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13670 | return; |
| 13671 | } |
| 13672 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13673 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13674 | scheduleAppGcsLocked(); |
| 13675 | } |
| 13676 | } |
| 13677 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13678 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 13679 | updateCpuStatsNow(); |
| 13680 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13681 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13682 | boolean doWakeKills = doKills; |
| 13683 | boolean doCpuKills = doKills; |
| 13684 | if (mLastPowerCheckRealtime == 0) { |
| 13685 | doWakeKills = false; |
| 13686 | } |
| 13687 | if (mLastPowerCheckUptime == 0) { |
| 13688 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13689 | } |
| 13690 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13691 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13692 | } |
| 13693 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13694 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 13695 | final long curUptime = SystemClock.uptimeMillis(); |
| 13696 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 13697 | mLastPowerCheckRealtime = curRealtime; |
| 13698 | mLastPowerCheckUptime = curUptime; |
| 13699 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 13700 | doWakeKills = false; |
| 13701 | } |
| 13702 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 13703 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13704 | } |
| 13705 | int i = mLruProcesses.size(); |
| 13706 | while (i > 0) { |
| 13707 | i--; |
| 13708 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13709 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13710 | long wtime; |
| 13711 | synchronized (stats) { |
| 13712 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 13713 | app.pid, curRealtime); |
| 13714 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13715 | long wtimeUsed = wtime - app.lastWakeTime; |
| 13716 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 13717 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13718 | StringBuilder sb = new StringBuilder(128); |
| 13719 | sb.append("Wake for "); |
| 13720 | app.toShortString(sb); |
| 13721 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13722 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13723 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13724 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13725 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13726 | sb.append((wtimeUsed*100)/realtimeSince); |
| 13727 | sb.append("%)"); |
| 13728 | Slog.i(TAG, sb.toString()); |
| 13729 | sb.setLength(0); |
| 13730 | sb.append("CPU for "); |
| 13731 | app.toShortString(sb); |
| 13732 | sb.append(": over "); |
| 13733 | TimeUtils.formatDuration(uptimeSince, sb); |
| 13734 | sb.append(" used "); |
| 13735 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 13736 | sb.append(" ("); |
| 13737 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13738 | sb.append("%)"); |
| 13739 | Slog.i(TAG, sb.toString()); |
| 13740 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13741 | // If a process has held a wake lock for more |
| 13742 | // than 50% of the time during this period, |
| 13743 | // that sounds pad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13744 | if (doWakeKills && realtimeSince > 0 |
| 13745 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 13746 | synchronized (stats) { |
| 13747 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 13748 | realtimeSince, wtimeUsed); |
| 13749 | } |
| 13750 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 13751 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 13752 | + " during " + realtimeSince); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13753 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13754 | app.processName, app.setAdj, "excessive wake lock"); |
| 13755 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13756 | } else if (doCpuKills && uptimeSince > 0 |
| 13757 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 13758 | synchronized (stats) { |
| 13759 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 13760 | uptimeSince, cputimeUsed); |
| 13761 | } |
| 13762 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 13763 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 13764 | + " during " + uptimeSince); |
| 13765 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13766 | app.processName, app.setAdj, "excessive cpu"); |
| 13767 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13768 | } else { |
| 13769 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13770 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13771 | } |
| 13772 | } |
| 13773 | } |
| 13774 | } |
| 13775 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13776 | private final boolean updateOomAdjLocked( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13777 | ProcessRecord app, int hiddenAdj, ProcessRecord TOP_APP, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13778 | app.hiddenAdj = hiddenAdj; |
| 13779 | |
| 13780 | if (app.thread == null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13781 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13782 | } |
| 13783 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13784 | final boolean wasKeeping = app.keeping; |
| 13785 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13786 | boolean success = true; |
| 13787 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13788 | computeOomAdjLocked(app, hiddenAdj, TOP_APP, false, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13789 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13790 | if (app.curRawAdj != app.setRawAdj) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 13791 | if (false) { |
| 13792 | // Removing for now. Forcing GCs is not so useful anymore |
| 13793 | // with Dalvik, and the new memory level hint facility is |
| 13794 | // better for what we need to do these days. |
| 13795 | if (app.curRawAdj > ProcessList.FOREGROUND_APP_ADJ |
| 13796 | && app.setRawAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
| 13797 | // If this app is transitioning from foreground to |
| 13798 | // non-foreground, have it do a gc. |
| 13799 | scheduleAppGcLocked(app); |
| 13800 | } else if (app.curRawAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13801 | && app.setRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13802 | // Likewise do a gc when an app is moving in to the |
| 13803 | // background (such as a service stopping). |
| 13804 | scheduleAppGcLocked(app); |
| 13805 | } |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13806 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13807 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13808 | if (wasKeeping && !app.keeping) { |
| 13809 | // This app is no longer something we want to keep. Note |
| 13810 | // its current wake lock time to later know to kill it if |
| 13811 | // it is not behaving well. |
| 13812 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 13813 | synchronized (stats) { |
| 13814 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 13815 | app.pid, SystemClock.elapsedRealtime()); |
| 13816 | } |
| 13817 | app.lastCpuTime = app.curCpuTime; |
| 13818 | } |
| 13819 | |
| 13820 | app.setRawAdj = app.curRawAdj; |
| 13821 | } |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13822 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13823 | if (app.curAdj != app.setAdj) { |
| 13824 | if (Process.setOomAdj(app.pid, app.curAdj)) { |
Dianne Hackborn | bbb09ac | 2011-11-30 11:31:29 -0800 | [diff] [blame] | 13825 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13826 | TAG, "Set " + app.pid + " " + app.processName + |
| 13827 | " adj " + app.curAdj + ": " + app.adjType); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13828 | app.setAdj = app.curAdj; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13829 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13830 | success = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13831 | Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13832 | } |
| 13833 | } |
| 13834 | if (app.setSchedGroup != app.curSchedGroup) { |
| 13835 | app.setSchedGroup = app.curSchedGroup; |
| 13836 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
| 13837 | "Setting process group of " + app.processName |
| 13838 | + " to " + app.curSchedGroup); |
| 13839 | if (app.waitingToKill != null && |
| 13840 | app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 13841 | Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill); |
| 13842 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13843 | app.processName, app.setAdj, app.waitingToKill); |
| 13844 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13845 | success = false; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13846 | } else { |
| 13847 | if (true) { |
| 13848 | long oldId = Binder.clearCallingIdentity(); |
| 13849 | try { |
| 13850 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 13851 | } catch (Exception e) { |
| 13852 | Slog.w(TAG, "Failed setting process group of " + app.pid |
| 13853 | + " to " + app.curSchedGroup); |
| 13854 | e.printStackTrace(); |
| 13855 | } finally { |
| 13856 | Binder.restoreCallingIdentity(oldId); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13857 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13858 | } else { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13859 | if (app.thread != null) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13860 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13861 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 13862 | } catch (RemoteException e) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13863 | } |
| 13864 | } |
| 13865 | } |
| 13866 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13867 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13868 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13869 | } |
| 13870 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13871 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13872 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13873 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | cbb722e | 2012-02-07 18:33:49 -0800 | [diff] [blame] | 13874 | for (int i=mMainStack.mPausingActivities.size()-1; i>=0; i--) { |
| 13875 | ActivityRecord r = mMainStack.mPausingActivities.get(i); |
| 13876 | if (r.app != null) { |
| 13877 | resumedActivity = r; |
| 13878 | break; |
| 13879 | } |
| 13880 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13881 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13882 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13883 | } |
| 13884 | } |
| 13885 | return resumedActivity; |
| 13886 | } |
| 13887 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13888 | private final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13889 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13890 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13891 | int curAdj = app.curAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13892 | final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13893 | && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13894 | |
| 13895 | mAdjSeq++; |
| 13896 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13897 | boolean success = updateOomAdjLocked(app, app.hiddenAdj, TOP_APP, false); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13898 | final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13899 | && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13900 | if (nowHidden != wasHidden) { |
| 13901 | // Changed to/from hidden state, so apps after it in the LRU |
| 13902 | // list may also be changed. |
| 13903 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13904 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13905 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13906 | } |
| 13907 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13908 | final void updateOomAdjLocked() { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13909 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13910 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13911 | |
| 13912 | if (false) { |
| 13913 | RuntimeException e = new RuntimeException(); |
| 13914 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13915 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13916 | } |
| 13917 | |
| 13918 | mAdjSeq++; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13919 | mNewNumServiceProcs = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13920 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13921 | // Let's determine how many processes we have running vs. |
| 13922 | // how many slots we have for background processes; we may want |
| 13923 | // to put multiple processes in a slot of there are enough of |
| 13924 | // them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13925 | 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] | 13926 | int factor = (mLruProcesses.size()-4)/numSlots; |
| 13927 | if (factor < 1) factor = 1; |
| 13928 | int step = 0; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13929 | int numHidden = 0; |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13930 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13931 | // First update the OOM adjustment for each of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13932 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13933 | int i = mLruProcesses.size(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13934 | int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13935 | while (i > 0) { |
| 13936 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13937 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13938 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13939 | updateOomAdjLocked(app, curHiddenAdj, TOP_APP, true); |
| 13940 | if (curHiddenAdj < ProcessList.HIDDEN_APP_MAX_ADJ |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13941 | && app.curAdj == curHiddenAdj) { |
| 13942 | step++; |
| 13943 | if (step >= factor) { |
| 13944 | step = 0; |
| 13945 | curHiddenAdj++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13946 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13947 | } |
| 13948 | if (!app.killedBackground) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13949 | if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13950 | numHidden++; |
| 13951 | if (numHidden > mProcessLimit) { |
| 13952 | Slog.i(TAG, "No longer want " + app.processName |
| 13953 | + " (pid " + app.pid + "): hidden #" + numHidden); |
| 13954 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13955 | app.processName, app.setAdj, "too many background"); |
| 13956 | app.killedBackground = true; |
| 13957 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13958 | } |
| 13959 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13960 | if (!app.killedBackground && app.isolated && app.services.size() <= 0) { |
| 13961 | // If this is an isolated process, and there are no |
| 13962 | // services running in it, then the process is no longer |
| 13963 | // needed. We agressively kill these because we can by |
| 13964 | // definition not re-use the same process again, and it is |
| 13965 | // good to avoid having whatever code was running in them |
| 13966 | // left sitting around after no longer needed. |
| 13967 | Slog.i(TAG, "Isolated process " + app.processName |
| 13968 | + " (pid " + app.pid + ") no longer needed"); |
| 13969 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13970 | app.processName, app.setAdj, "isolated not needed"); |
| 13971 | app.killedBackground = true; |
| 13972 | Process.killProcessQuiet(app.pid); |
| 13973 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13974 | } |
| 13975 | } |
| 13976 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13977 | mNumServiceProcs = mNewNumServiceProcs; |
| 13978 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13979 | // Now determine the memory trimming level of background processes. |
| 13980 | // Unfortunately we need to start at the back of the list to do this |
| 13981 | // properly. We only do this if the number of background apps we |
| 13982 | // are managing to keep around is less than half the maximum we desire; |
| 13983 | // if we are keeping a good number around, we'll let them use whatever |
| 13984 | // memory they want. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13985 | if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/2)) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13986 | final int N = mLruProcesses.size(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13987 | factor = numHidden/3; |
| 13988 | int minFactor = 2; |
| 13989 | if (mHomeProcess != null) minFactor++; |
| 13990 | if (mPreviousProcess != null) minFactor++; |
| 13991 | if (factor < minFactor) factor = minFactor; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13992 | step = 0; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13993 | int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13994 | for (i=0; i<N; i++) { |
| 13995 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13996 | if (app.curAdj >= ProcessList.HOME_APP_ADJ |
| 13997 | && app.curAdj != ProcessList.SERVICE_B_ADJ |
| 13998 | && !app.killedBackground) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13999 | if (app.trimMemoryLevel < curLevel && app.thread != null) { |
| 14000 | try { |
| 14001 | app.thread.scheduleTrimMemory(curLevel); |
| 14002 | } catch (RemoteException e) { |
| 14003 | } |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 14004 | if (false) { |
| 14005 | // For now we won't do this; our memory trimming seems |
| 14006 | // to be good enough at this point that destroying |
| 14007 | // activities causes more harm than good. |
| 14008 | if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE |
| 14009 | && app != mHomeProcess && app != mPreviousProcess) { |
| 14010 | // For these apps we will also finish their activities |
| 14011 | // to help them free memory. |
| 14012 | mMainStack.destroyActivitiesLocked(app, false, "trim"); |
| 14013 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14014 | } |
| 14015 | } |
| 14016 | app.trimMemoryLevel = curLevel; |
| 14017 | step++; |
| 14018 | if (step >= factor) { |
| 14019 | switch (curLevel) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14020 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 14021 | curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14022 | break; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14023 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 14024 | curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14025 | break; |
| 14026 | } |
| 14027 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14028 | } else if (app.curAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14029 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14030 | && app.thread != null) { |
| 14031 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14032 | app.thread.scheduleTrimMemory( |
| 14033 | ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14034 | } catch (RemoteException e) { |
| 14035 | } |
| 14036 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14037 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14038 | } else if ((app.curAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14039 | && app.pendingUiClean) { |
| 14040 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 14041 | && app.thread != null) { |
| 14042 | try { |
| 14043 | app.thread.scheduleTrimMemory( |
| 14044 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 14045 | } catch (RemoteException e) { |
| 14046 | } |
| 14047 | } |
| 14048 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 14049 | app.pendingUiClean = false; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14050 | } else { |
| 14051 | app.trimMemoryLevel = 0; |
| 14052 | } |
| 14053 | } |
| 14054 | } else { |
| 14055 | final int N = mLruProcesses.size(); |
| 14056 | for (i=0; i<N; i++) { |
| 14057 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 14058 | if ((app.curAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 14059 | && app.pendingUiClean) { |
| 14060 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 14061 | && app.thread != null) { |
| 14062 | try { |
| 14063 | app.thread.scheduleTrimMemory( |
| 14064 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 14065 | } catch (RemoteException e) { |
| 14066 | } |
| 14067 | } |
| 14068 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 14069 | app.pendingUiClean = false; |
| 14070 | } else { |
| 14071 | app.trimMemoryLevel = 0; |
| 14072 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14073 | } |
| 14074 | } |
| 14075 | |
| 14076 | if (mAlwaysFinishActivities) { |
Dianne Hackborn | 28695e0 | 2011-11-02 21:59:51 -0700 | [diff] [blame] | 14077 | mMainStack.destroyActivitiesLocked(null, false, "always-finish"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14078 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14079 | } |
| 14080 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 14081 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14082 | synchronized (this) { |
| 14083 | int i; |
| 14084 | |
| 14085 | // First remove any unused application processes whose package |
| 14086 | // has been removed. |
| 14087 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 14088 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 14089 | if (app.activities.size() == 0 |
| 14090 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 14091 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14092 | TAG, "Exiting empty application process " |
| 14093 | + app.processName + " (" |
| 14094 | + (app.thread != null ? app.thread.asBinder() : null) |
| 14095 | + ")\n"); |
| 14096 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 14097 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 14098 | app.processName, app.setAdj, "empty"); |
| 14099 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14100 | } else { |
| 14101 | try { |
| 14102 | app.thread.scheduleExit(); |
| 14103 | } catch (Exception e) { |
| 14104 | // Ignore exceptions. |
| 14105 | } |
| 14106 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 14107 | cleanUpApplicationRecordLocked(app, false, true, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14108 | mRemovedProcesses.remove(i); |
| 14109 | |
| 14110 | if (app.persistent) { |
| 14111 | if (app.persistent) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 14112 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14113 | } |
| 14114 | } |
| 14115 | } |
| 14116 | } |
| 14117 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 14118 | // Now update the oom adj for all processes. |
| 14119 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14120 | } |
| 14121 | } |
| 14122 | |
| 14123 | /** This method sends the specified signal to each of the persistent apps */ |
| 14124 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 14125 | if (sig != Process.SIGNAL_USR1) { |
| 14126 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 14127 | } |
| 14128 | |
| 14129 | synchronized (this) { |
| 14130 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 14131 | != PackageManager.PERMISSION_GRANTED) { |
| 14132 | throw new SecurityException("Requires permission " |
| 14133 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 14134 | } |
| 14135 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 14136 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 14137 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14138 | if (r.thread != null && r.persistent) { |
| 14139 | Process.sendSignal(r.pid, sig); |
| 14140 | } |
| 14141 | } |
| 14142 | } |
| 14143 | } |
| 14144 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14145 | private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) { |
| 14146 | if (proc == null || proc == mProfileProc) { |
| 14147 | proc = mProfileProc; |
| 14148 | path = mProfileFile; |
| 14149 | profileType = mProfileType; |
| 14150 | clearProfilerLocked(); |
| 14151 | } |
| 14152 | if (proc == null) { |
| 14153 | return; |
| 14154 | } |
| 14155 | try { |
| 14156 | proc.thread.profilerControl(false, path, null, profileType); |
| 14157 | } catch (RemoteException e) { |
| 14158 | throw new IllegalStateException("Process disappeared"); |
| 14159 | } |
| 14160 | } |
| 14161 | |
| 14162 | private void clearProfilerLocked() { |
| 14163 | if (mProfileFd != null) { |
| 14164 | try { |
| 14165 | mProfileFd.close(); |
| 14166 | } catch (IOException e) { |
| 14167 | } |
| 14168 | } |
| 14169 | mProfileApp = null; |
| 14170 | mProfileProc = null; |
| 14171 | mProfileFile = null; |
| 14172 | mProfileType = 0; |
| 14173 | mAutoStopProfiler = false; |
| 14174 | } |
| 14175 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14176 | public boolean profileControl(String process, boolean start, |
Romain Guy | 7eabe55 | 2011-07-21 14:56:34 -0700 | [diff] [blame] | 14177 | String path, ParcelFileDescriptor fd, int profileType) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14178 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14179 | try { |
| 14180 | synchronized (this) { |
| 14181 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14182 | // its own permission. |
| 14183 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14184 | != PackageManager.PERMISSION_GRANTED) { |
| 14185 | throw new SecurityException("Requires permission " |
| 14186 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14187 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14188 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14189 | if (start && fd == null) { |
| 14190 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14191 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14192 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14193 | ProcessRecord proc = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14194 | if (process != null) { |
| 14195 | try { |
| 14196 | int pid = Integer.parseInt(process); |
| 14197 | synchronized (mPidsSelfLocked) { |
| 14198 | proc = mPidsSelfLocked.get(pid); |
| 14199 | } |
| 14200 | } catch (NumberFormatException e) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14201 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14202 | |
| 14203 | if (proc == null) { |
| 14204 | HashMap<String, SparseArray<ProcessRecord>> all |
| 14205 | = mProcessNames.getMap(); |
| 14206 | SparseArray<ProcessRecord> procs = all.get(process); |
| 14207 | if (procs != null && procs.size() > 0) { |
| 14208 | proc = procs.valueAt(0); |
| 14209 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14210 | } |
| 14211 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14212 | |
| 14213 | if (start && (proc == null || proc.thread == null)) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14214 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14215 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14216 | |
| 14217 | if (start) { |
| 14218 | stopProfilerLocked(null, null, 0); |
| 14219 | setProfileApp(proc.info, proc.processName, path, fd, false); |
| 14220 | mProfileProc = proc; |
| 14221 | mProfileType = profileType; |
| 14222 | try { |
| 14223 | fd = fd.dup(); |
| 14224 | } catch (IOException e) { |
| 14225 | fd = null; |
| 14226 | } |
| 14227 | proc.thread.profilerControl(start, path, fd, profileType); |
| 14228 | fd = null; |
| 14229 | mProfileFd = null; |
| 14230 | } else { |
| 14231 | stopProfilerLocked(proc, path, profileType); |
| 14232 | if (fd != null) { |
| 14233 | try { |
| 14234 | fd.close(); |
| 14235 | } catch (IOException e) { |
| 14236 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14237 | } |
| 14238 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 14239 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14240 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 14241 | } |
| 14242 | } catch (RemoteException e) { |
| 14243 | throw new IllegalStateException("Process disappeared"); |
| 14244 | } finally { |
| 14245 | if (fd != null) { |
| 14246 | try { |
| 14247 | fd.close(); |
| 14248 | } catch (IOException e) { |
| 14249 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 14250 | } |
| 14251 | } |
| 14252 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14253 | |
| 14254 | public boolean dumpHeap(String process, boolean managed, |
| 14255 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 14256 | |
| 14257 | try { |
| 14258 | synchronized (this) { |
| 14259 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14260 | // its own permission (same as profileControl). |
| 14261 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14262 | != PackageManager.PERMISSION_GRANTED) { |
| 14263 | throw new SecurityException("Requires permission " |
| 14264 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 14265 | } |
| 14266 | |
| 14267 | if (fd == null) { |
| 14268 | throw new IllegalArgumentException("null fd"); |
| 14269 | } |
| 14270 | |
| 14271 | ProcessRecord proc = null; |
| 14272 | try { |
| 14273 | int pid = Integer.parseInt(process); |
| 14274 | synchronized (mPidsSelfLocked) { |
| 14275 | proc = mPidsSelfLocked.get(pid); |
| 14276 | } |
| 14277 | } catch (NumberFormatException e) { |
| 14278 | } |
| 14279 | |
| 14280 | if (proc == null) { |
| 14281 | HashMap<String, SparseArray<ProcessRecord>> all |
| 14282 | = mProcessNames.getMap(); |
| 14283 | SparseArray<ProcessRecord> procs = all.get(process); |
| 14284 | if (procs != null && procs.size() > 0) { |
| 14285 | proc = procs.valueAt(0); |
| 14286 | } |
| 14287 | } |
| 14288 | |
| 14289 | if (proc == null || proc.thread == null) { |
| 14290 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14291 | } |
| 14292 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 14293 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 14294 | if (!isDebuggable) { |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14295 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 14296 | throw new SecurityException("Process not debuggable: " + proc); |
| 14297 | } |
| 14298 | } |
| 14299 | |
| 14300 | proc.thread.dumpHeap(managed, path, fd); |
| 14301 | fd = null; |
| 14302 | return true; |
| 14303 | } |
| 14304 | } catch (RemoteException e) { |
| 14305 | throw new IllegalStateException("Process disappeared"); |
| 14306 | } finally { |
| 14307 | if (fd != null) { |
| 14308 | try { |
| 14309 | fd.close(); |
| 14310 | } catch (IOException e) { |
| 14311 | } |
| 14312 | } |
| 14313 | } |
| 14314 | } |
| 14315 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14316 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 14317 | public void monitor() { |
| 14318 | synchronized (this) { } |
| 14319 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 14320 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 14321 | void onCoreSettingsChange(Bundle settings) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 14322 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 14323 | ProcessRecord processRecord = mLruProcesses.get(i); |
| 14324 | try { |
| 14325 | if (processRecord.thread != null) { |
| 14326 | processRecord.thread.setCoreSettings(settings); |
| 14327 | } |
| 14328 | } catch (RemoteException re) { |
| 14329 | /* ignore */ |
| 14330 | } |
| 14331 | } |
| 14332 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14333 | |
| 14334 | // Multi-user methods |
| 14335 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14336 | private int mCurrentUserId; |
| 14337 | private SparseIntArray mLoggedInUsers = new SparseIntArray(5); |
| 14338 | |
| 14339 | public boolean switchUser(int userId) { |
| 14340 | final int callingUid = Binder.getCallingUid(); |
| 14341 | if (callingUid != 0 && callingUid != Process.myUid()) { |
| 14342 | Slog.e(TAG, "Trying to switch user from unauthorized app"); |
| 14343 | return false; |
| 14344 | } |
| 14345 | if (mCurrentUserId == userId) |
| 14346 | return true; |
| 14347 | |
| 14348 | synchronized (this) { |
| 14349 | // Check if user is already logged in, otherwise check if user exists first before |
| 14350 | // adding to the list of logged in users. |
| 14351 | if (mLoggedInUsers.indexOfKey(userId) < 0) { |
| 14352 | if (!userExists(userId)) { |
| 14353 | return false; |
| 14354 | } |
| 14355 | mLoggedInUsers.append(userId, userId); |
| 14356 | } |
| 14357 | |
| 14358 | mCurrentUserId = userId; |
| 14359 | boolean haveActivities = mMainStack.switchUser(userId); |
| 14360 | if (!haveActivities) { |
| 14361 | startHomeActivityLocked(userId); |
| 14362 | } |
| 14363 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14364 | return true; |
| 14365 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14366 | |
| 14367 | private boolean userExists(int userId) { |
| 14368 | try { |
| 14369 | List<UserInfo> users = AppGlobals.getPackageManager().getUsers(); |
| 14370 | for (UserInfo user : users) { |
| 14371 | if (user.id == userId) { |
| 14372 | return true; |
| 14373 | } |
| 14374 | } |
| 14375 | } catch (RemoteException re) { |
| 14376 | // Won't happen, in same process |
| 14377 | } |
| 14378 | |
| 14379 | return false; |
| 14380 | } |
| 14381 | |
| 14382 | |
| 14383 | private int applyUserId(int uid, int userId) { |
| 14384 | return UserId.getUid(userId, uid); |
| 14385 | } |
| 14386 | |
| 14387 | private ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) { |
Amith Yamasani | 2f6c9eb | 2012-02-06 15:31:35 -0800 | [diff] [blame] | 14388 | if (info == null) return null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14389 | ApplicationInfo newInfo = new ApplicationInfo(info); |
| 14390 | newInfo.uid = applyUserId(info.uid, userId); |
| 14391 | if (newInfo.uid >= Process.FIRST_APPLICATION_UID) { |
| 14392 | newInfo.dataDir = USER_DATA_DIR + userId + "/" |
| 14393 | + info.packageName; |
| 14394 | } |
| 14395 | return newInfo; |
| 14396 | } |
| 14397 | |
| 14398 | ActivityInfo getActivityInfoForUser(ActivityInfo aInfo, int userId) { |
Amith Yamasani | 2f6c9eb | 2012-02-06 15:31:35 -0800 | [diff] [blame] | 14399 | if (aInfo == null || aInfo.applicationInfo.uid < Process.FIRST_APPLICATION_UID |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14400 | || userId < 1) { |
| 14401 | return aInfo; |
| 14402 | } |
| 14403 | |
| 14404 | ActivityInfo info = new ActivityInfo(aInfo); |
| 14405 | info.applicationInfo = getAppInfoForUser(info.applicationInfo, userId); |
| 14406 | return info; |
| 14407 | } |
| 14408 | |
| 14409 | static class ServiceMap { |
| 14410 | |
| 14411 | private final SparseArray<HashMap<ComponentName, ServiceRecord>> mServicesByNamePerUser |
| 14412 | = new SparseArray<HashMap<ComponentName, ServiceRecord>>(); |
| 14413 | private final SparseArray<HashMap<Intent.FilterComparison, ServiceRecord>> |
| 14414 | mServicesByIntentPerUser = new SparseArray< |
| 14415 | HashMap<Intent.FilterComparison, ServiceRecord>>(); |
| 14416 | |
| 14417 | ServiceRecord getServiceByName(ComponentName name, int callingUser) { |
| 14418 | // TODO: Deal with global services |
| 14419 | if (DEBUG_MU) |
| 14420 | Slog.v(TAG_MU, "getServiceByName(" + name + "), callingUser = " + callingUser); |
| 14421 | return getServices(callingUser).get(name); |
| 14422 | } |
| 14423 | |
| 14424 | ServiceRecord getServiceByName(ComponentName name) { |
| 14425 | return getServiceByName(name, -1); |
| 14426 | } |
| 14427 | |
| 14428 | ServiceRecord getServiceByIntent(Intent.FilterComparison filter, int callingUser) { |
| 14429 | // TODO: Deal with global services |
| 14430 | if (DEBUG_MU) |
| 14431 | Slog.v(TAG_MU, "getServiceByIntent(" + filter + "), callingUser = " + callingUser); |
| 14432 | return getServicesByIntent(callingUser).get(filter); |
| 14433 | } |
| 14434 | |
| 14435 | ServiceRecord getServiceByIntent(Intent.FilterComparison filter) { |
| 14436 | return getServiceByIntent(filter, -1); |
| 14437 | } |
| 14438 | |
| 14439 | void putServiceByName(ComponentName name, int callingUser, ServiceRecord value) { |
| 14440 | // TODO: Deal with global services |
| 14441 | getServices(callingUser).put(name, value); |
| 14442 | } |
| 14443 | |
| 14444 | void putServiceByIntent(Intent.FilterComparison filter, int callingUser, |
| 14445 | ServiceRecord value) { |
| 14446 | // TODO: Deal with global services |
| 14447 | getServicesByIntent(callingUser).put(filter, value); |
| 14448 | } |
| 14449 | |
| 14450 | void removeServiceByName(ComponentName name, int callingUser) { |
| 14451 | // TODO: Deal with global services |
| 14452 | ServiceRecord removed = getServices(callingUser).remove(name); |
| 14453 | if (DEBUG_MU) |
| 14454 | Slog.v(TAG, "removeServiceByName user=" + callingUser + " name=" + name |
| 14455 | + " removed=" + removed); |
| 14456 | } |
| 14457 | |
| 14458 | void removeServiceByIntent(Intent.FilterComparison filter, int callingUser) { |
| 14459 | // TODO: Deal with global services |
| 14460 | ServiceRecord removed = getServicesByIntent(callingUser).remove(filter); |
| 14461 | if (DEBUG_MU) |
| 14462 | Slog.v(TAG_MU, "removeServiceByIntent user=" + callingUser + " intent=" + filter |
| 14463 | + " removed=" + removed); |
| 14464 | } |
| 14465 | |
| 14466 | Collection<ServiceRecord> getAllServices(int callingUser) { |
| 14467 | // TODO: Deal with global services |
| 14468 | return getServices(callingUser).values(); |
| 14469 | } |
| 14470 | |
| 14471 | private HashMap<ComponentName, ServiceRecord> getServices(int callingUser) { |
| 14472 | HashMap map = mServicesByNamePerUser.get(callingUser); |
| 14473 | if (map == null) { |
| 14474 | map = new HashMap<ComponentName, ServiceRecord>(); |
| 14475 | mServicesByNamePerUser.put(callingUser, map); |
| 14476 | } |
| 14477 | return map; |
| 14478 | } |
| 14479 | |
| 14480 | private HashMap<Intent.FilterComparison, ServiceRecord> getServicesByIntent( |
| 14481 | int callingUser) { |
| 14482 | HashMap map = mServicesByIntentPerUser.get(callingUser); |
| 14483 | if (map == null) { |
| 14484 | map = new HashMap<Intent.FilterComparison, ServiceRecord>(); |
| 14485 | mServicesByIntentPerUser.put(callingUser, map); |
| 14486 | } |
| 14487 | return map; |
| 14488 | } |
| 14489 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14490 | } |