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 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
| 20 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 21 | import com.android.internal.R; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import com.android.internal.os.BatteryStatsImpl; |
Dianne Hackborn | 45ce864 | 2011-07-14 16:10:16 -0700 | [diff] [blame] | 23 | import com.android.internal.os.ProcessStats; |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 24 | import com.android.server.AttributeCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import com.android.server.IntentResolver; |
| 26 | import com.android.server.ProcessMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import com.android.server.SystemServer; |
| 28 | import com.android.server.Watchdog; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 29 | import com.android.server.am.ActivityStack.ActivityState; |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 30 | import com.android.server.wm.WindowManagerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 32 | import dalvik.system.Zygote; |
| 33 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.app.Activity; |
| 35 | import android.app.ActivityManager; |
| 36 | import android.app.ActivityManagerNative; |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 37 | import android.app.ActivityOptions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.app.ActivityThread; |
| 39 | import android.app.AlertDialog; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 40 | import android.app.AppGlobals; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 41 | import android.app.ApplicationErrorReport; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.app.Dialog; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 43 | import android.app.IActivityController; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.app.IApplicationThread; |
| 45 | import android.app.IInstrumentationWatcher; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 46 | import android.app.INotificationManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 47 | import android.app.IProcessObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.app.IServiceConnection; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 49 | import android.app.IStopUserCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.app.IThumbnailReceiver; |
| 51 | import android.app.Instrumentation; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 52 | import android.app.Notification; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 53 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.app.PendingIntent; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 55 | import android.app.backup.IBackupManager; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 56 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 57 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 58 | import android.content.ClipData; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 59 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.content.ComponentName; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 61 | import android.content.ContentProvider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | import android.content.ContentResolver; |
| 63 | import android.content.Context; |
Christian Mehlmauer | 7664e20 | 2010-07-20 08:46:17 +0200 | [diff] [blame] | 64 | import android.content.DialogInterface; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 65 | import android.content.IContentProvider; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 66 | import android.content.IIntentReceiver; |
| 67 | import android.content.IIntentSender; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 68 | import android.content.Intent; |
| 69 | import android.content.IntentFilter; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 70 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | import android.content.pm.ActivityInfo; |
| 72 | import android.content.pm.ApplicationInfo; |
| 73 | import android.content.pm.ConfigurationInfo; |
| 74 | import android.content.pm.IPackageDataObserver; |
| 75 | import android.content.pm.IPackageManager; |
| 76 | import android.content.pm.InstrumentationInfo; |
Dan Egnor | 66c40e7 | 2010-01-26 16:23:11 -0800 | [diff] [blame] | 77 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | import android.content.pm.PackageManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 79 | import android.content.pm.UserInfo; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 80 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 81 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | import android.content.pm.ProviderInfo; |
| 83 | import android.content.pm.ResolveInfo; |
| 84 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 85 | import android.content.res.CompatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | import android.content.res.Configuration; |
| 87 | import android.graphics.Bitmap; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 88 | import android.net.Proxy; |
| 89 | import android.net.ProxyProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | import android.net.Uri; |
| 91 | import android.os.Binder; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 92 | import android.os.Build; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 93 | import android.os.Bundle; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 94 | import android.os.Debug; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 95 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | import android.os.Environment; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 97 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | import android.os.FileUtils; |
| 99 | import android.os.Handler; |
| 100 | import android.os.IBinder; |
| 101 | import android.os.IPermissionController; |
| 102 | import android.os.Looper; |
| 103 | import android.os.Message; |
| 104 | import android.os.Parcel; |
| 105 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | import android.os.Process; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 107 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.os.RemoteException; |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 109 | import android.os.SELinux; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import android.os.ServiceManager; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 111 | import android.os.StrictMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | import android.os.SystemClock; |
| 113 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 114 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 115 | import android.os.UserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | import android.provider.Settings; |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 117 | import android.text.format.Time; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | import android.util.EventLog; |
Joe Onorato | 5d3bea6 | 2010-03-01 13:44:29 -0800 | [diff] [blame] | 119 | import android.util.Log; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 120 | import android.util.Pair; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | import android.util.PrintWriterPrinter; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 122 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | import android.util.SparseArray; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 124 | import android.util.SparseIntArray; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 125 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | import android.view.Gravity; |
| 127 | import android.view.LayoutInflater; |
| 128 | import android.view.View; |
| 129 | import android.view.WindowManager; |
| 130 | import android.view.WindowManagerPolicy; |
| 131 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 132 | import java.io.BufferedInputStream; |
| 133 | import java.io.BufferedOutputStream; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 134 | import java.io.BufferedReader; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 135 | import java.io.DataInputStream; |
| 136 | import java.io.DataOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | import java.io.File; |
| 138 | import java.io.FileDescriptor; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 139 | import java.io.FileInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | import java.io.FileNotFoundException; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 141 | import java.io.FileOutputStream; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 142 | import java.io.IOException; |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 143 | import java.io.InputStreamReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | import java.io.PrintWriter; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 145 | import java.io.StringWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | import java.lang.ref.WeakReference; |
| 147 | import java.util.ArrayList; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 148 | import java.util.Collections; |
| 149 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | import java.util.HashMap; |
| 151 | import java.util.HashSet; |
| 152 | import java.util.Iterator; |
| 153 | import java.util.List; |
| 154 | import java.util.Locale; |
| 155 | import java.util.Map; |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 156 | import java.util.Set; |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 157 | import java.util.concurrent.atomic.AtomicBoolean; |
| 158 | import java.util.concurrent.atomic.AtomicLong; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 160 | public final class ActivityManagerService extends ActivityManagerNative |
| 161 | implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 162 | private static final String USER_DATA_DIR = "/data/user/"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | static final String TAG = "ActivityManager"; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 164 | static final String TAG_MU = "ActivityManagerServiceMU"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | static final boolean DEBUG = false; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 166 | static final boolean localLOGV = DEBUG; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | static final boolean DEBUG_SWITCH = localLOGV || false; |
| 168 | static final boolean DEBUG_TASKS = localLOGV || false; |
| 169 | static final boolean DEBUG_PAUSE = localLOGV || false; |
| 170 | static final boolean DEBUG_OOM_ADJ = localLOGV || false; |
| 171 | static final boolean DEBUG_TRANSITION = localLOGV || false; |
| 172 | static final boolean DEBUG_BROADCAST = localLOGV || false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 173 | static final boolean DEBUG_BACKGROUND_BROADCAST = DEBUG_BROADCAST || false; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 174 | static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | static final boolean DEBUG_SERVICE = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 176 | static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | static final boolean DEBUG_VISBILITY = localLOGV || false; |
| 178 | static final boolean DEBUG_PROCESSES = localLOGV || false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 179 | static final boolean DEBUG_PROCESS_OBSERVERS = localLOGV || false; |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 180 | static final boolean DEBUG_PROVIDER = localLOGV || false; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 181 | static final boolean DEBUG_URI_PERMISSION = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | static final boolean DEBUG_USER_LEAVING = localLOGV || false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 183 | static final boolean DEBUG_RESULTS = localLOGV || false; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 184 | static final boolean DEBUG_BACKUP = localLOGV || false; |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 185 | static final boolean DEBUG_CONFIGURATION = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 186 | static final boolean DEBUG_POWER = localLOGV || false; |
| 187 | static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 188 | static final boolean DEBUG_MU = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | static final boolean VALIDATE_TOKENS = false; |
| 190 | static final boolean SHOW_ACTIVITY_START_TIME = true; |
| 191 | |
| 192 | // Control over CPU and battery monitoring. |
| 193 | static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes. |
| 194 | static final boolean MONITOR_CPU_USAGE = true; |
| 195 | static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds. |
| 196 | static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample. |
| 197 | static final boolean MONITOR_THREAD_CPU_USAGE = false; |
| 198 | |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 199 | // 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] | 200 | static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 201 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 202 | private static final String SYSTEM_DEBUGGABLE = "ro.debuggable"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 204 | static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); |
| 205 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | // Maximum number of recent tasks that we can remember. |
| 207 | static final int MAX_RECENT_TASKS = 20; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 208 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 209 | // Amount of time after a call to stopAppSwitches() during which we will |
| 210 | // prevent further untrusted switches from happening. |
| 211 | static final long APP_SWITCH_DELAY_TIME = 5*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | |
| 213 | // How long we wait for a launched process to attach to the activity manager |
| 214 | // before we decide it's never going to come up for real. |
| 215 | static final int PROC_START_TIMEOUT = 10*1000; |
| 216 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 217 | // How long we wait for a launched process to attach to the activity manager |
| 218 | // before we decide it's never going to come up for real, when the process was |
| 219 | // started with a wrapper for instrumentation (such as Valgrind) because it |
| 220 | // could take much longer than usual. |
| 221 | static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000; |
| 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | // How long to wait after going idle before forcing apps to GC. |
| 224 | static final int GC_TIMEOUT = 5*1000; |
| 225 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 226 | // The minimum amount of time between successive GC requests for a process. |
| 227 | static final int GC_MIN_INTERVAL = 60*1000; |
| 228 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 229 | // The rate at which we check for apps using excessive power -- 15 mins. |
| 230 | static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000; |
| 231 | |
| 232 | // The minimum sample duration we will allow before deciding we have |
| 233 | // enough data on wake locks to start killing things. |
| 234 | static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
| 235 | |
| 236 | // The minimum sample duration we will allow before deciding we have |
| 237 | // enough data on CPU usage to start killing things. |
| 238 | 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] | 239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | // 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] | 241 | static final int BROADCAST_FG_TIMEOUT = 10*1000; |
| 242 | static final int BROADCAST_BG_TIMEOUT = 60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | // How long we wait until we timeout on key dispatching. |
| 245 | static final int KEY_DISPATCHING_TIMEOUT = 5*1000; |
| 246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | // How long we wait until we timeout on key dispatching during instrumentation. |
| 248 | static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000; |
| 249 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 250 | static final int MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | |
| 252 | static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 253 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 254 | public ActivityStack mMainStack; |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 255 | |
| 256 | private final boolean mHeadless; |
| 257 | |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 258 | // Whether we should show our dialogs (ANR, crash, etc) or just perform their |
| 259 | // default actuion automatically. Important for devices without direct input |
| 260 | // devices. |
| 261 | private boolean mShowDialogs = true; |
| 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 264 | * Description of a request to start a new activity, which has been held |
| 265 | * due to app switches being disabled. |
| 266 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 267 | static class PendingActivityLaunch { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 268 | ActivityRecord r; |
| 269 | ActivityRecord sourceRecord; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 270 | int startFlags; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | final ArrayList<PendingActivityLaunch> mPendingActivityLaunches |
| 274 | = new ArrayList<PendingActivityLaunch>(); |
| 275 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 277 | BroadcastQueue mFgBroadcastQueue; |
| 278 | BroadcastQueue mBgBroadcastQueue; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 279 | // Convenient for easy iteration over the queues. Foreground is first |
| 280 | // so that dispatch of foreground broadcasts gets precedence. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 281 | final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2]; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 282 | |
| 283 | BroadcastQueue broadcastQueueForIntent(Intent intent) { |
| 284 | final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0; |
| 285 | if (DEBUG_BACKGROUND_BROADCAST) { |
| 286 | Slog.i(TAG, "Broadcast intent " + intent + " on " |
| 287 | + (isFg ? "foreground" : "background") |
| 288 | + " queue"); |
| 289 | } |
| 290 | return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue; |
| 291 | } |
| 292 | |
| 293 | BroadcastRecord broadcastRecordForReceiverLocked(IBinder receiver) { |
| 294 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 295 | BroadcastRecord r = queue.getMatchingOrderedReceiver(receiver); |
| 296 | if (r != null) { |
| 297 | return r; |
| 298 | } |
| 299 | } |
| 300 | return null; |
| 301 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | |
| 303 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | * Activity we have told the window manager to have key focus. |
| 305 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 306 | ActivityRecord mFocusedActivity = null; |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 307 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | * List of intents that were used to start the most recent tasks. |
| 309 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 310 | final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | |
| 312 | /** |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 313 | * Process management. |
| 314 | */ |
| 315 | final ProcessList mProcessList = new ProcessList(); |
| 316 | |
| 317 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | * All of the applications we currently have running organized by name. |
| 319 | * The keys are strings of the application package name (as |
| 320 | * returned by the package manager), and the keys are ApplicationRecord |
| 321 | * objects. |
| 322 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 323 | final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | |
| 325 | /** |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 326 | * The currently running isolated processes. |
| 327 | */ |
| 328 | final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>(); |
| 329 | |
| 330 | /** |
| 331 | * Counter for assigning isolated process uids, to avoid frequently reusing the |
| 332 | * same ones. |
| 333 | */ |
| 334 | int mNextIsolatedProcessUid = 0; |
| 335 | |
| 336 | /** |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 337 | * The currently running heavy-weight process, if any. |
| 338 | */ |
| 339 | ProcessRecord mHeavyWeightProcess = null; |
| 340 | |
| 341 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | * The last time that various processes have crashed. |
| 343 | */ |
| 344 | final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>(); |
| 345 | |
| 346 | /** |
| 347 | * Set of applications that we consider to be bad, and will reject |
| 348 | * incoming broadcasts from (which the user has no control over). |
| 349 | * Processes are added to this set when they have crashed twice within |
| 350 | * a minimum amount of time; they are removed from it when they are |
| 351 | * later restarted (hopefully due to some user action). The value is the |
| 352 | * time it was added to the list. |
| 353 | */ |
| 354 | final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>(); |
| 355 | |
| 356 | /** |
| 357 | * All of the processes we currently have running organized by pid. |
| 358 | * The keys are the pid running the application. |
| 359 | * |
| 360 | * <p>NOTE: This object is protected by its own lock, NOT the global |
| 361 | * activity manager lock! |
| 362 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 363 | final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | |
| 365 | /** |
| 366 | * All of the processes that have been forced to be foreground. The key |
| 367 | * is the pid of the caller who requested it (we hold a death |
| 368 | * link on it). |
| 369 | */ |
| 370 | abstract class ForegroundToken implements IBinder.DeathRecipient { |
| 371 | int pid; |
| 372 | IBinder token; |
| 373 | } |
| 374 | final SparseArray<ForegroundToken> mForegroundProcesses |
| 375 | = new SparseArray<ForegroundToken>(); |
| 376 | |
| 377 | /** |
| 378 | * List of records for processes that someone had tried to start before the |
| 379 | * system was ready. We don't start them at that point, but ensure they |
| 380 | * are started by the time booting is complete. |
| 381 | */ |
| 382 | final ArrayList<ProcessRecord> mProcessesOnHold |
| 383 | = new ArrayList<ProcessRecord>(); |
| 384 | |
| 385 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | * List of persistent applications that are in the process |
| 387 | * of being started. |
| 388 | */ |
| 389 | final ArrayList<ProcessRecord> mPersistentStartingProcesses |
| 390 | = new ArrayList<ProcessRecord>(); |
| 391 | |
| 392 | /** |
| 393 | * Processes that are being forcibly torn down. |
| 394 | */ |
| 395 | final ArrayList<ProcessRecord> mRemovedProcesses |
| 396 | = new ArrayList<ProcessRecord>(); |
| 397 | |
| 398 | /** |
| 399 | * List of running applications, sorted by recent usage. |
| 400 | * The first entry in the list is the least recently used. |
| 401 | * It contains ApplicationRecord objects. This list does NOT include |
| 402 | * any persistent application records (since we never want to exit them). |
| 403 | */ |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 404 | final ArrayList<ProcessRecord> mLruProcesses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | = new ArrayList<ProcessRecord>(); |
| 406 | |
| 407 | /** |
| 408 | * List of processes that should gc as soon as things are idle. |
| 409 | */ |
| 410 | final ArrayList<ProcessRecord> mProcessesToGc |
| 411 | = new ArrayList<ProcessRecord>(); |
| 412 | |
| 413 | /** |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 414 | * This is the process holding what we currently consider to be |
| 415 | * the "home" activity. |
| 416 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 417 | ProcessRecord mHomeProcess; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 418 | |
| 419 | /** |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 420 | * This is the process holding the activity the user last visited that |
| 421 | * is in a different process from the one they are currently in. |
| 422 | */ |
| 423 | ProcessRecord mPreviousProcess; |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 424 | |
| 425 | /** |
| 426 | * The time at which the previous process was last visible. |
| 427 | */ |
| 428 | long mPreviousProcessVisibleTime; |
| 429 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 430 | /** |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 431 | * Which uses have been started, so are allowed to run code. |
| 432 | */ |
| 433 | final SparseArray<UserStartedState> mStartedUsers = new SparseArray<UserStartedState>(); |
| 434 | |
| 435 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 436 | * Packages that the user has asked to have run in screen size |
| 437 | * compatibility mode instead of filling the screen. |
| 438 | */ |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 439 | final CompatModePackages mCompatModePackages; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 440 | |
| 441 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | * Set of PendingResultRecord objects that are currently active. |
| 443 | */ |
| 444 | final HashSet mPendingResultRecords = new HashSet(); |
| 445 | |
| 446 | /** |
| 447 | * Set of IntentSenderRecord objects that are currently active. |
| 448 | */ |
| 449 | final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords |
| 450 | = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>(); |
| 451 | |
| 452 | /** |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 453 | * Fingerprints (hashCode()) of stack traces that we've |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 454 | * already logged DropBox entries for. Guarded by itself. If |
| 455 | * something (rogue user app) forces this over |
| 456 | * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared. |
| 457 | */ |
| 458 | private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>(); |
| 459 | private static final int MAX_DUP_SUPPRESSED_STACKS = 5000; |
| 460 | |
| 461 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 462 | * Strict Mode background batched logging state. |
| 463 | * |
| 464 | * The string buffer is guarded by itself, and its lock is also |
| 465 | * used to determine if another batched write is already |
| 466 | * in-flight. |
| 467 | */ |
| 468 | private final StringBuilder mStrictModeBuffer = new StringBuilder(); |
| 469 | |
| 470 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | * Keeps track of all IIntentReceivers that have been registered for |
| 472 | * broadcasts. Hash keys are the receiver IBinder, hash value is |
| 473 | * a ReceiverList. |
| 474 | */ |
| 475 | final HashMap mRegisteredReceivers = new HashMap(); |
| 476 | |
| 477 | /** |
| 478 | * Resolver for broadcast intents to registered receivers. |
| 479 | * Holds BroadcastFilter (subclass of IntentFilter). |
| 480 | */ |
| 481 | final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver |
| 482 | = new IntentResolver<BroadcastFilter, BroadcastFilter>() { |
| 483 | @Override |
| 484 | protected boolean allowFilterResult( |
| 485 | BroadcastFilter filter, List<BroadcastFilter> dest) { |
| 486 | IBinder target = filter.receiverList.receiver.asBinder(); |
| 487 | for (int i=dest.size()-1; i>=0; i--) { |
| 488 | if (dest.get(i).receiverList.receiver.asBinder() == target) { |
| 489 | return false; |
| 490 | } |
| 491 | } |
| 492 | return true; |
| 493 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 494 | |
| 495 | @Override |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 496 | protected BroadcastFilter newResult(BroadcastFilter filter, int match, int userId) { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 497 | if (userId == UserHandle.USER_ALL || filter.owningUserId == UserHandle.USER_ALL |
| 498 | || userId == filter.owningUserId) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 499 | return super.newResult(filter, match, userId); |
| 500 | } |
| 501 | return null; |
| 502 | } |
| 503 | |
| 504 | @Override |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame] | 505 | protected BroadcastFilter[] newArray(int size) { |
| 506 | return new BroadcastFilter[size]; |
| 507 | } |
| 508 | |
| 509 | @Override |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 510 | protected String packageForFilter(BroadcastFilter filter) { |
| 511 | return filter.packageName; |
| 512 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | }; |
| 514 | |
| 515 | /** |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 516 | * State of all active sticky broadcasts per user. Keys are the action of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | * sticky Intent, values are an ArrayList of all broadcasted intents with |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 518 | * that action (which should usually be one). The SparseArray is keyed |
| 519 | * by the user ID the sticky is for, and can include UserHandle.USER_ALL |
| 520 | * for stickies that are sent to all users. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | */ |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 522 | final SparseArray<HashMap<String, ArrayList<Intent>>> mStickyBroadcasts = |
| 523 | new SparseArray<HashMap<String, ArrayList<Intent>>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 525 | final ActiveServices mServices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | |
| 527 | /** |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 528 | * Backup/restore process management |
| 529 | */ |
| 530 | String mBackupAppName = null; |
| 531 | BackupRecord mBackupTarget = null; |
| 532 | |
| 533 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | * List of PendingThumbnailsRecord objects of clients who are still |
| 535 | * waiting to receive all of the thumbnails for a task. |
| 536 | */ |
| 537 | final ArrayList mPendingThumbnails = new ArrayList(); |
| 538 | |
| 539 | /** |
| 540 | * List of HistoryRecord objects that have been finished and must |
| 541 | * still report back to a pending thumbnail receiver. |
| 542 | */ |
| 543 | final ArrayList mCancelledThumbnails = new ArrayList(); |
| 544 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 545 | final ProviderMap mProviderMap = new ProviderMap(); |
| 546 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | /** |
| 548 | * List of content providers who have clients waiting for them. The |
| 549 | * application is currently being launched and the provider will be |
| 550 | * removed from this list once it is published. |
| 551 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 552 | final ArrayList<ContentProviderRecord> mLaunchingProviders |
| 553 | = new ArrayList<ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | |
| 555 | /** |
| 556 | * Global set of specific Uri permissions that have been granted. |
| 557 | */ |
| 558 | final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions |
| 559 | = new SparseArray<HashMap<Uri, UriPermission>>(); |
| 560 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 561 | CoreSettingsObserver mCoreSettingsObserver; |
| 562 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | /** |
| 564 | * Thread-local storage used to carry caller permissions over through |
| 565 | * indirect content-provider access. |
| 566 | * @see #ActivityManagerService.openContentUri() |
| 567 | */ |
| 568 | private class Identity { |
| 569 | public int pid; |
| 570 | public int uid; |
| 571 | |
| 572 | Identity(int _pid, int _uid) { |
| 573 | pid = _pid; |
| 574 | uid = _uid; |
| 575 | } |
| 576 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 577 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>(); |
| 579 | |
| 580 | /** |
| 581 | * All information we have collected about the runtime performance of |
| 582 | * any user id that can impact battery performance. |
| 583 | */ |
| 584 | final BatteryStatsService mBatteryStatsService; |
| 585 | |
| 586 | /** |
| 587 | * information about component usage |
| 588 | */ |
| 589 | final UsageStatsService mUsageStatsService; |
| 590 | |
| 591 | /** |
| 592 | * Current configuration information. HistoryRecord objects are given |
| 593 | * a reference to this object to indicate which configuration they are |
| 594 | * currently running in, so this object must be kept immutable. |
| 595 | */ |
| 596 | Configuration mConfiguration = new Configuration(); |
| 597 | |
| 598 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 599 | * Current sequencing integer of the configuration, for skipping old |
| 600 | * configurations. |
| 601 | */ |
| 602 | int mConfigurationSeq = 0; |
| 603 | |
| 604 | /** |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 605 | * Hardware-reported OpenGLES version. |
| 606 | */ |
| 607 | final int GL_ES_VERSION; |
| 608 | |
| 609 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | * List of initialization arguments to pass to all processes when binding applications to them. |
| 611 | * For example, references to the commonly used services. |
| 612 | */ |
| 613 | HashMap<String, IBinder> mAppBindArgs; |
| 614 | |
| 615 | /** |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 616 | * Temporary to avoid allocations. Protected by main lock. |
| 617 | */ |
| 618 | final StringBuilder mStringBuilder = new StringBuilder(256); |
| 619 | |
| 620 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | * Used to control how we initialize the service. |
| 622 | */ |
| 623 | boolean mStartRunning = false; |
| 624 | ComponentName mTopComponent; |
| 625 | String mTopAction; |
| 626 | String mTopData; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 627 | boolean mProcessesReady = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | boolean mSystemReady = false; |
| 629 | boolean mBooting = false; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 630 | boolean mWaitingUpdate = false; |
| 631 | boolean mDidUpdate = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 632 | boolean mOnBattery = false; |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 633 | boolean mLaunchWarningShown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | |
| 635 | Context mContext; |
| 636 | |
| 637 | int mFactoryTest; |
| 638 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 639 | boolean mCheckedForSetup; |
| 640 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 642 | * The time at which we will allow normal application switches again, |
| 643 | * after a call to {@link #stopAppSwitches()}. |
| 644 | */ |
| 645 | long mAppSwitchesAllowedTime; |
| 646 | |
| 647 | /** |
| 648 | * This is set to true after the first switch after mAppSwitchesAllowedTime |
| 649 | * is set; any switches after that will clear the time. |
| 650 | */ |
| 651 | boolean mDidAppSwitch; |
| 652 | |
| 653 | /** |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 654 | * Last time (in realtime) at which we checked for power usage. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 655 | */ |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 656 | long mLastPowerCheckRealtime; |
| 657 | |
| 658 | /** |
| 659 | * Last time (in uptime) at which we checked for power usage. |
| 660 | */ |
| 661 | long mLastPowerCheckUptime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 662 | |
| 663 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | * Set while we are wanting to sleep, to prevent any |
| 665 | * activities from being started/resumed. |
| 666 | */ |
| 667 | boolean mSleeping = false; |
| 668 | |
| 669 | /** |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 670 | * State of external calls telling us if the device is asleep. |
| 671 | */ |
| 672 | boolean mWentToSleep = false; |
| 673 | |
| 674 | /** |
| 675 | * State of external call telling us if the lock screen is shown. |
| 676 | */ |
| 677 | boolean mLockScreenShown = false; |
| 678 | |
| 679 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 680 | * Set if we are shutting down the system, similar to sleeping. |
| 681 | */ |
| 682 | boolean mShuttingDown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | |
| 684 | /** |
| 685 | * Task identifier that activities are currently being started |
| 686 | * in. Incremented each time a new task is created. |
| 687 | * todo: Replace this with a TokenSpace class that generates non-repeating |
| 688 | * integers that won't wrap. |
| 689 | */ |
| 690 | int mCurTask = 1; |
| 691 | |
| 692 | /** |
| 693 | * Current sequence id for oom_adj computation traversal. |
| 694 | */ |
| 695 | int mAdjSeq = 0; |
| 696 | |
| 697 | /** |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 698 | * Current sequence id for process LRU updating. |
| 699 | */ |
| 700 | int mLruSeq = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | |
| 702 | /** |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 703 | * Keep track of the non-hidden/empty process we last found, to help |
| 704 | * determine how to distribute hidden/empty processes next time. |
| 705 | */ |
| 706 | int mNumNonHiddenProcs = 0; |
| 707 | |
| 708 | /** |
| 709 | * Keep track of the number of hidden procs, to balance oom adj |
| 710 | * distribution between those and empty procs. |
| 711 | */ |
| 712 | int mNumHiddenProcs = 0; |
| 713 | |
| 714 | /** |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 715 | * Keep track of the number of service processes we last found, to |
| 716 | * determine on the next iteration which should be B services. |
| 717 | */ |
| 718 | int mNumServiceProcs = 0; |
| 719 | int mNewNumServiceProcs = 0; |
| 720 | |
| 721 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | * System monitoring: number of processes that died since the last |
| 723 | * N procs were started. |
| 724 | */ |
| 725 | int[] mProcDeaths = new int[20]; |
| 726 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 727 | /** |
| 728 | * This is set if we had to do a delayed dexopt of an app before launching |
| 729 | * it, to increasing the ANR timeouts in that case. |
| 730 | */ |
| 731 | boolean mDidDexOpt; |
| 732 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | String mDebugApp = null; |
| 734 | boolean mWaitForDebugger = false; |
| 735 | boolean mDebugTransient = false; |
| 736 | String mOrigDebugApp = null; |
| 737 | boolean mOrigWaitForDebugger = false; |
| 738 | boolean mAlwaysFinishActivities = false; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 739 | IActivityController mController = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 740 | String mProfileApp = null; |
| 741 | ProcessRecord mProfileProc = null; |
| 742 | String mProfileFile; |
| 743 | ParcelFileDescriptor mProfileFd; |
| 744 | int mProfileType = 0; |
| 745 | boolean mAutoStopProfiler = false; |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 746 | String mOpenGlTraceApp = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 748 | static class ProcessChangeItem { |
| 749 | static final int CHANGE_ACTIVITIES = 1<<0; |
| 750 | static final int CHANGE_IMPORTANCE= 1<<1; |
| 751 | int changes; |
| 752 | int uid; |
| 753 | int pid; |
| 754 | int importance; |
| 755 | boolean foregroundActivities; |
| 756 | } |
| 757 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 758 | final RemoteCallbackList<IProcessObserver> mProcessObservers |
| 759 | = new RemoteCallbackList<IProcessObserver>(); |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 760 | ProcessChangeItem[] mActiveProcessChanges = new ProcessChangeItem[5]; |
| 761 | |
| 762 | final ArrayList<ProcessChangeItem> mPendingProcessChanges |
| 763 | = new ArrayList<ProcessChangeItem>(); |
| 764 | final ArrayList<ProcessChangeItem> mAvailProcessChanges |
| 765 | = new ArrayList<ProcessChangeItem>(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 766 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | /** |
| 768 | * Callback of last caller to {@link #requestPss}. |
| 769 | */ |
| 770 | Runnable mRequestPssCallback; |
| 771 | |
| 772 | /** |
| 773 | * Remaining processes for which we are waiting results from the last |
| 774 | * call to {@link #requestPss}. |
| 775 | */ |
| 776 | final ArrayList<ProcessRecord> mRequestPssList |
| 777 | = new ArrayList<ProcessRecord>(); |
| 778 | |
| 779 | /** |
| 780 | * Runtime statistics collection thread. This object's lock is used to |
| 781 | * protect all related state. |
| 782 | */ |
| 783 | final Thread mProcessStatsThread; |
| 784 | |
| 785 | /** |
| 786 | * Used to collect process stats when showing not responding dialog. |
| 787 | * Protected by mProcessStatsThread. |
| 788 | */ |
| 789 | final ProcessStats mProcessStats = new ProcessStats( |
| 790 | MONITOR_THREAD_CPU_USAGE); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 791 | final AtomicLong mLastCpuTime = new AtomicLong(0); |
| 792 | final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true); |
| 793 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 794 | long mLastWriteTime = 0; |
| 795 | |
| 796 | /** |
| 797 | * Set to true after the system has finished booting. |
| 798 | */ |
| 799 | boolean mBooted = false; |
| 800 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 801 | int mProcessLimit = ProcessList.MAX_HIDDEN_APPS; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 802 | int mProcessLimitOverride = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | |
| 804 | WindowManagerService mWindowManager; |
| 805 | |
| 806 | static ActivityManagerService mSelf; |
| 807 | static ActivityThread mSystemThread; |
| 808 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 809 | private int mCurrentUserId; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 810 | private UserManager mUserManager; |
| 811 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | private final class AppDeathRecipient implements IBinder.DeathRecipient { |
| 813 | final ProcessRecord mApp; |
| 814 | final int mPid; |
| 815 | final IApplicationThread mAppThread; |
| 816 | |
| 817 | AppDeathRecipient(ProcessRecord app, int pid, |
| 818 | IApplicationThread thread) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 819 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | TAG, "New death recipient " + this |
| 821 | + " for thread " + thread.asBinder()); |
| 822 | mApp = app; |
| 823 | mPid = pid; |
| 824 | mAppThread = thread; |
| 825 | } |
| 826 | |
| 827 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 828 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | TAG, "Death received in " + this |
| 830 | + " for thread " + mAppThread.asBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | synchronized(ActivityManagerService.this) { |
| 832 | appDiedLocked(mApp, mPid, mAppThread); |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | static final int SHOW_ERROR_MSG = 1; |
| 838 | static final int SHOW_NOT_RESPONDING_MSG = 2; |
| 839 | static final int SHOW_FACTORY_ERROR_MSG = 3; |
| 840 | static final int UPDATE_CONFIGURATION_MSG = 4; |
| 841 | static final int GC_BACKGROUND_PROCESSES_MSG = 5; |
| 842 | static final int WAIT_FOR_DEBUGGER_MSG = 6; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | static final int SERVICE_TIMEOUT_MSG = 12; |
| 844 | static final int UPDATE_TIME_ZONE = 13; |
| 845 | static final int SHOW_UID_ERROR_MSG = 14; |
| 846 | static final int IM_FEELING_LUCKY_MSG = 15; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | static final int PROC_START_TIMEOUT_MSG = 20; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 848 | static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 849 | static final int KILL_APPLICATION_MSG = 22; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 850 | static final int FINALIZE_PENDING_INTENT_MSG = 23; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 851 | static final int POST_HEAVY_NOTIFICATION_MSG = 24; |
| 852 | static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 853 | static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 854 | static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 855 | static final int CLEAR_DNS_CACHE = 28; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 856 | static final int UPDATE_HTTP_PROXY = 29; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 857 | static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 858 | static final int DISPATCH_PROCESSES_CHANGED = 31; |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 859 | static final int DISPATCH_PROCESS_DIED = 32; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 860 | static final int REPORT_MEM_USAGE = 33; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 862 | static final int FIRST_ACTIVITY_STACK_MSG = 100; |
| 863 | static final int FIRST_BROADCAST_QUEUE_MSG = 200; |
| 864 | static final int FIRST_COMPAT_MODE_MSG = 300; |
| 865 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | AlertDialog mUidAlert; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 867 | CompatModeDialog mCompatModeDialog; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 868 | long mLastMemUsageReportTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | |
| 870 | final Handler mHandler = new Handler() { |
| 871 | //public Handler() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 872 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | //} |
| 874 | |
| 875 | public void handleMessage(Message msg) { |
| 876 | switch (msg.what) { |
| 877 | case SHOW_ERROR_MSG: { |
| 878 | HashMap data = (HashMap) msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | synchronized (ActivityManagerService.this) { |
| 880 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 881 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 882 | Slog.e(TAG, "App already has crash dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | return; |
| 884 | } |
| 885 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 886 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 887 | Dialog d = new AppErrorDialog(mContext, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | d.show(); |
| 889 | proc.crashDialog = d; |
| 890 | } else { |
| 891 | // The device is asleep, so just pretend that the user |
| 892 | // saw a crash dialog and hit "force quit". |
| 893 | res.set(0); |
| 894 | } |
| 895 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 896 | |
| 897 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | } break; |
| 899 | case SHOW_NOT_RESPONDING_MSG: { |
| 900 | synchronized (ActivityManagerService.this) { |
| 901 | HashMap data = (HashMap) msg.obj; |
| 902 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 903 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 904 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | return; |
| 906 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 907 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 908 | Intent intent = new Intent("android.intent.action.ANR"); |
| 909 | if (!mProcessesReady) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 910 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 911 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 912 | } |
| 913 | broadcastIntentLocked(null, null, intent, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 914 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 915 | 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] | 916 | |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 917 | if (mShowDialogs) { |
| 918 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Mike Lockwood | 69ccdbd | 2012-04-03 11:53:47 -0700 | [diff] [blame] | 919 | mContext, proc, (ActivityRecord)data.get("activity")); |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 920 | d.show(); |
| 921 | proc.anrDialog = d; |
| 922 | } else { |
| 923 | // Just kill the app if there is no dialog to be shown. |
| 924 | killAppAtUsersRequest(proc, null); |
| 925 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 927 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 928 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 930 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 931 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 932 | synchronized (ActivityManagerService.this) { |
| 933 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 934 | if (proc == null) { |
| 935 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 936 | break; |
| 937 | } |
| 938 | if (proc.crashDialog != null) { |
| 939 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 940 | return; |
| 941 | } |
| 942 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 943 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 944 | Dialog d = new StrictModeViolationDialog(mContext, res, proc); |
| 945 | d.show(); |
| 946 | proc.crashDialog = d; |
| 947 | } else { |
| 948 | // The device is asleep, so just pretend that the user |
| 949 | // saw a crash dialog and hit "force quit". |
| 950 | res.set(0); |
| 951 | } |
| 952 | } |
| 953 | ensureBootCompleted(); |
| 954 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | case SHOW_FACTORY_ERROR_MSG: { |
| 956 | Dialog d = new FactoryErrorDialog( |
| 957 | mContext, msg.getData().getCharSequence("msg")); |
| 958 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 959 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | } break; |
| 961 | case UPDATE_CONFIGURATION_MSG: { |
| 962 | final ContentResolver resolver = mContext.getContentResolver(); |
| 963 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 964 | } break; |
| 965 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 966 | synchronized (ActivityManagerService.this) { |
| 967 | performAppGcsIfAppropriateLocked(); |
| 968 | } |
| 969 | } break; |
| 970 | case WAIT_FOR_DEBUGGER_MSG: { |
| 971 | synchronized (ActivityManagerService.this) { |
| 972 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 973 | if (msg.arg1 != 0) { |
| 974 | if (!app.waitedForDebugger) { |
| 975 | Dialog d = new AppWaitingForDebuggerDialog( |
| 976 | ActivityManagerService.this, |
| 977 | mContext, app); |
| 978 | app.waitDialog = d; |
| 979 | app.waitedForDebugger = true; |
| 980 | d.show(); |
| 981 | } |
| 982 | } else { |
| 983 | if (app.waitDialog != null) { |
| 984 | app.waitDialog.dismiss(); |
| 985 | app.waitDialog = null; |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 991 | if (mDidDexOpt) { |
| 992 | mDidDexOpt = false; |
| 993 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 994 | nmsg.obj = msg.obj; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 995 | mHandler.sendMessageDelayed(nmsg, ActiveServices.SERVICE_TIMEOUT); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 996 | return; |
| 997 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 998 | mServices.serviceTimeout((ProcessRecord)msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 999 | } break; |
| 1000 | case UPDATE_TIME_ZONE: { |
| 1001 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1002 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1003 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | if (r.thread != null) { |
| 1005 | try { |
| 1006 | r.thread.updateTimeZone(); |
| 1007 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1008 | 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] | 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1013 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1014 | case CLEAR_DNS_CACHE: { |
| 1015 | synchronized (ActivityManagerService.this) { |
| 1016 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1017 | ProcessRecord r = mLruProcesses.get(i); |
| 1018 | if (r.thread != null) { |
| 1019 | try { |
| 1020 | r.thread.clearDnsCache(); |
| 1021 | } catch (RemoteException ex) { |
| 1022 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1028 | case UPDATE_HTTP_PROXY: { |
| 1029 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 1030 | String host = ""; |
| 1031 | String port = ""; |
| 1032 | String exclList = ""; |
| 1033 | if (proxy != null) { |
| 1034 | host = proxy.getHost(); |
| 1035 | port = Integer.toString(proxy.getPort()); |
| 1036 | exclList = proxy.getExclusionList(); |
| 1037 | } |
| 1038 | synchronized (ActivityManagerService.this) { |
| 1039 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1040 | ProcessRecord r = mLruProcesses.get(i); |
| 1041 | if (r.thread != null) { |
| 1042 | try { |
| 1043 | r.thread.setHttpProxy(host, port, exclList); |
| 1044 | } catch (RemoteException ex) { |
| 1045 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1046 | r.info.processName); |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1052 | case SHOW_UID_ERROR_MSG: { |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 1053 | String title = "System UIDs Inconsistent"; |
| 1054 | String text = "UIDs on the system are inconsistent, you need to wipe your" |
| 1055 | + " data partition or your device will be unstable."; |
| 1056 | Log.e(TAG, title + ": " + text); |
| 1057 | if (mShowDialogs) { |
| 1058 | // XXX This is a temporary dialog, no need to localize. |
| 1059 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1060 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1061 | d.setCancelable(false); |
| 1062 | d.setTitle(title); |
| 1063 | d.setMessage(text); |
| 1064 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
| 1065 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1066 | mUidAlert = d; |
| 1067 | d.show(); |
| 1068 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1069 | } break; |
| 1070 | case IM_FEELING_LUCKY_MSG: { |
| 1071 | if (mUidAlert != null) { |
| 1072 | mUidAlert.dismiss(); |
| 1073 | mUidAlert = null; |
| 1074 | } |
| 1075 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1076 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1077 | if (mDidDexOpt) { |
| 1078 | mDidDexOpt = false; |
| 1079 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1080 | nmsg.obj = msg.obj; |
| 1081 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1082 | return; |
| 1083 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1084 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1085 | synchronized (ActivityManagerService.this) { |
| 1086 | processStartTimedOutLocked(app); |
| 1087 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1088 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1089 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1090 | synchronized (ActivityManagerService.this) { |
| 1091 | doPendingActivityLaunchesLocked(true); |
| 1092 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1093 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1094 | case KILL_APPLICATION_MSG: { |
| 1095 | synchronized (ActivityManagerService.this) { |
| 1096 | int uid = msg.arg1; |
| 1097 | boolean restart = (msg.arg2 == 1); |
| 1098 | String pkg = (String) msg.obj; |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1099 | forceStopPackageLocked(pkg, uid, restart, false, true, false, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1100 | UserHandle.getUserId(uid)); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1101 | } |
| 1102 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1103 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1104 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1105 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1106 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1107 | INotificationManager inm = NotificationManager.getService(); |
| 1108 | if (inm == null) { |
| 1109 | return; |
| 1110 | } |
| 1111 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1112 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1113 | ProcessRecord process = root.app; |
| 1114 | if (process == null) { |
| 1115 | return; |
| 1116 | } |
| 1117 | |
| 1118 | try { |
| 1119 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1120 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1121 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1122 | Notification notification = new Notification(); |
| 1123 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1124 | notification.when = 0; |
| 1125 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1126 | notification.tickerText = text; |
| 1127 | notification.defaults = 0; // please be quiet |
| 1128 | notification.sound = null; |
| 1129 | notification.vibrate = null; |
| 1130 | notification.setLatestEventInfo(context, text, |
| 1131 | mContext.getText(R.string.heavy_weight_notification_detail), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1132 | PendingIntent.getActivityAsUser(mContext, 0, root.intent, |
| 1133 | PendingIntent.FLAG_CANCEL_CURRENT, null, |
| 1134 | new UserHandle(root.userId))); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1135 | |
| 1136 | try { |
| 1137 | int[] outId = new int[1]; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1138 | inm.enqueueNotificationWithTag("android", null, |
| 1139 | R.string.heavy_weight_notification, |
| 1140 | notification, outId, root.userId); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1141 | } catch (RuntimeException e) { |
| 1142 | Slog.w(ActivityManagerService.TAG, |
| 1143 | "Error showing notification for heavy-weight app", e); |
| 1144 | } catch (RemoteException e) { |
| 1145 | } |
| 1146 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1147 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1148 | } |
| 1149 | } break; |
| 1150 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1151 | INotificationManager inm = NotificationManager.getService(); |
| 1152 | if (inm == null) { |
| 1153 | return; |
| 1154 | } |
| 1155 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1156 | inm.cancelNotificationWithTag("android", null, |
| 1157 | R.string.heavy_weight_notification, msg.arg1); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1158 | } catch (RuntimeException e) { |
| 1159 | Slog.w(ActivityManagerService.TAG, |
| 1160 | "Error canceling notification for service", e); |
| 1161 | } catch (RemoteException e) { |
| 1162 | } |
| 1163 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1164 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1165 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1166 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1167 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1168 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1169 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1170 | } |
| 1171 | } break; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1172 | case SHOW_COMPAT_MODE_DIALOG_MSG: { |
| 1173 | synchronized (ActivityManagerService.this) { |
| 1174 | ActivityRecord ar = (ActivityRecord)msg.obj; |
| 1175 | if (mCompatModeDialog != null) { |
| 1176 | if (mCompatModeDialog.mAppInfo.packageName.equals( |
| 1177 | ar.info.applicationInfo.packageName)) { |
| 1178 | return; |
| 1179 | } |
| 1180 | mCompatModeDialog.dismiss(); |
| 1181 | mCompatModeDialog = null; |
| 1182 | } |
Dianne Hackborn | 2947826 | 2011-06-07 15:44:22 -0700 | [diff] [blame] | 1183 | if (ar != null && false) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1184 | if (mCompatModePackages.getPackageAskCompatModeLocked( |
| 1185 | ar.packageName)) { |
| 1186 | int mode = mCompatModePackages.computeCompatModeLocked( |
| 1187 | ar.info.applicationInfo); |
| 1188 | if (mode == ActivityManager.COMPAT_MODE_DISABLED |
| 1189 | || mode == ActivityManager.COMPAT_MODE_ENABLED) { |
| 1190 | mCompatModeDialog = new CompatModeDialog( |
| 1191 | ActivityManagerService.this, mContext, |
| 1192 | ar.info.applicationInfo); |
| 1193 | mCompatModeDialog.show(); |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | } |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 1198 | break; |
| 1199 | } |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 1200 | case DISPATCH_PROCESSES_CHANGED: { |
| 1201 | dispatchProcessesChanged(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1202 | break; |
| 1203 | } |
| 1204 | case DISPATCH_PROCESS_DIED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1205 | final int pid = msg.arg1; |
| 1206 | final int uid = msg.arg2; |
| 1207 | dispatchProcessDied(pid, uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1208 | break; |
| 1209 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1210 | case REPORT_MEM_USAGE: { |
| 1211 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 1212 | if (!isDebuggable) { |
| 1213 | return; |
| 1214 | } |
| 1215 | synchronized (ActivityManagerService.this) { |
| 1216 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1217 | if (now < (mLastMemUsageReportTime+5*60*1000)) { |
| 1218 | // Don't report more than every 5 minutes to somewhat |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1219 | // avoid spamming. |
| 1220 | return; |
| 1221 | } |
| 1222 | mLastMemUsageReportTime = now; |
| 1223 | } |
| 1224 | Thread thread = new Thread() { |
| 1225 | @Override public void run() { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1226 | StringBuilder dropBuilder = new StringBuilder(1024); |
| 1227 | StringBuilder logBuilder = new StringBuilder(1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1228 | StringWriter oomSw = new StringWriter(); |
| 1229 | PrintWriter oomPw = new PrintWriter(oomSw); |
| 1230 | StringWriter catSw = new StringWriter(); |
| 1231 | PrintWriter catPw = new PrintWriter(catSw); |
| 1232 | String[] emptyArgs = new String[] { }; |
| 1233 | StringBuilder tag = new StringBuilder(128); |
| 1234 | StringBuilder stack = new StringBuilder(128); |
| 1235 | tag.append("Low on memory -- "); |
| 1236 | dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw, |
| 1237 | tag, stack); |
| 1238 | dropBuilder.append(stack); |
| 1239 | dropBuilder.append('\n'); |
| 1240 | dropBuilder.append('\n'); |
| 1241 | String oomString = oomSw.toString(); |
| 1242 | dropBuilder.append(oomString); |
| 1243 | dropBuilder.append('\n'); |
| 1244 | logBuilder.append(oomString); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1245 | try { |
| 1246 | java.lang.Process proc = Runtime.getRuntime().exec(new String[] { |
| 1247 | "procrank", }); |
| 1248 | final InputStreamReader converter = new InputStreamReader( |
| 1249 | proc.getInputStream()); |
| 1250 | BufferedReader in = new BufferedReader(converter); |
| 1251 | String line; |
| 1252 | while (true) { |
| 1253 | line = in.readLine(); |
| 1254 | if (line == null) { |
| 1255 | break; |
| 1256 | } |
| 1257 | if (line.length() > 0) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1258 | logBuilder.append(line); |
| 1259 | logBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1260 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1261 | dropBuilder.append(line); |
| 1262 | dropBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1263 | } |
| 1264 | converter.close(); |
| 1265 | } catch (IOException e) { |
| 1266 | } |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1267 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1268 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1269 | dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1270 | catPw.println(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1271 | mServices.dumpServicesLocked(null, catPw, emptyArgs, 0, |
| 1272 | false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1273 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1274 | dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1275 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1276 | dropBuilder.append(catSw.toString()); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1277 | addErrorToDropBox("lowmem", null, "system_server", null, |
| 1278 | null, tag.toString(), dropBuilder.toString(), null, null); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1279 | Slog.i(TAG, logBuilder.toString()); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1280 | synchronized (ActivityManagerService.this) { |
| 1281 | long now = SystemClock.uptimeMillis(); |
| 1282 | if (mLastMemUsageReportTime < now) { |
| 1283 | mLastMemUsageReportTime = now; |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | }; |
| 1288 | thread.start(); |
| 1289 | break; |
| 1290 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | }; |
| 1294 | |
| 1295 | public static void setSystemProcess() { |
| 1296 | try { |
| 1297 | ActivityManagerService m = mSelf; |
| 1298 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 1299 | ServiceManager.addService("activity", m, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1300 | ServiceManager.addService("meminfo", new MemBinder(m)); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1301 | ServiceManager.addService("gfxinfo", new GraphicsBinder(m)); |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1302 | ServiceManager.addService("dbinfo", new DbBinder(m)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | if (MONITOR_CPU_USAGE) { |
| 1304 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1305 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1306 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1307 | |
| 1308 | ApplicationInfo info = |
| 1309 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1310 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1311 | mSystemThread.installSystemApplicationInfo(info); |
| 1312 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1313 | synchronized (mSelf) { |
| 1314 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1315 | mSystemThread.getApplicationThread(), info, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1316 | info.processName, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1317 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1318 | app.pid = MY_PID; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1319 | app.maxAdj = ProcessList.SYSTEM_ADJ; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1320 | mSelf.mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1321 | synchronized (mSelf.mPidsSelfLocked) { |
| 1322 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1323 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1324 | mSelf.updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1325 | } |
| 1326 | } catch (PackageManager.NameNotFoundException e) { |
| 1327 | throw new RuntimeException( |
| 1328 | "Unable to find android system package", e); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | public void setWindowManager(WindowManagerService wm) { |
| 1333 | mWindowManager = wm; |
| 1334 | } |
| 1335 | |
| 1336 | public static final Context main(int factoryTest) { |
| 1337 | AThread thr = new AThread(); |
| 1338 | thr.start(); |
| 1339 | |
| 1340 | synchronized (thr) { |
| 1341 | while (thr.mService == null) { |
| 1342 | try { |
| 1343 | thr.wait(); |
| 1344 | } catch (InterruptedException e) { |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | ActivityManagerService m = thr.mService; |
| 1350 | mSelf = m; |
| 1351 | ActivityThread at = ActivityThread.systemMain(); |
| 1352 | mSystemThread = at; |
| 1353 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1354 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1355 | m.mContext = context; |
| 1356 | m.mFactoryTest = factoryTest; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1357 | m.mMainStack = new ActivityStack(m, context, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | |
| 1359 | m.mBatteryStatsService.publish(context); |
| 1360 | m.mUsageStatsService.publish(context); |
| 1361 | |
| 1362 | synchronized (thr) { |
| 1363 | thr.mReady = true; |
| 1364 | thr.notifyAll(); |
| 1365 | } |
| 1366 | |
| 1367 | m.startRunning(null, null, null, null); |
| 1368 | |
| 1369 | return context; |
| 1370 | } |
| 1371 | |
| 1372 | public static ActivityManagerService self() { |
| 1373 | return mSelf; |
| 1374 | } |
| 1375 | |
| 1376 | static class AThread extends Thread { |
| 1377 | ActivityManagerService mService; |
| 1378 | boolean mReady = false; |
| 1379 | |
| 1380 | public AThread() { |
| 1381 | super("ActivityManager"); |
| 1382 | } |
| 1383 | |
| 1384 | public void run() { |
| 1385 | Looper.prepare(); |
| 1386 | |
| 1387 | android.os.Process.setThreadPriority( |
| 1388 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1389 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1390 | |
| 1391 | ActivityManagerService m = new ActivityManagerService(); |
| 1392 | |
| 1393 | synchronized (this) { |
| 1394 | mService = m; |
| 1395 | notifyAll(); |
| 1396 | } |
| 1397 | |
| 1398 | synchronized (this) { |
| 1399 | while (!mReady) { |
| 1400 | try { |
| 1401 | wait(); |
| 1402 | } catch (InterruptedException e) { |
| 1403 | } |
| 1404 | } |
| 1405 | } |
| 1406 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1407 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1408 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1409 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1410 | } |
| 1411 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1412 | Looper.loop(); |
| 1413 | } |
| 1414 | } |
| 1415 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1416 | static class MemBinder extends Binder { |
| 1417 | ActivityManagerService mActivityManagerService; |
| 1418 | MemBinder(ActivityManagerService activityManagerService) { |
| 1419 | mActivityManagerService = activityManagerService; |
| 1420 | } |
| 1421 | |
| 1422 | @Override |
| 1423 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1424 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1425 | != PackageManager.PERMISSION_GRANTED) { |
| 1426 | pw.println("Permission Denial: can't dump meminfo from from pid=" |
| 1427 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1428 | + " without permission " + android.Manifest.permission.DUMP); |
| 1429 | return; |
| 1430 | } |
| 1431 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1432 | mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1433 | false, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1434 | } |
| 1435 | } |
| 1436 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1437 | static class GraphicsBinder extends Binder { |
| 1438 | ActivityManagerService mActivityManagerService; |
| 1439 | GraphicsBinder(ActivityManagerService activityManagerService) { |
| 1440 | mActivityManagerService = activityManagerService; |
| 1441 | } |
| 1442 | |
| 1443 | @Override |
| 1444 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1445 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1446 | != PackageManager.PERMISSION_GRANTED) { |
| 1447 | pw.println("Permission Denial: can't dump gfxinfo from from pid=" |
| 1448 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1449 | + " without permission " + android.Manifest.permission.DUMP); |
| 1450 | return; |
| 1451 | } |
| 1452 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1453 | mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1454 | } |
| 1455 | } |
| 1456 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1457 | static class DbBinder extends Binder { |
| 1458 | ActivityManagerService mActivityManagerService; |
| 1459 | DbBinder(ActivityManagerService activityManagerService) { |
| 1460 | mActivityManagerService = activityManagerService; |
| 1461 | } |
| 1462 | |
| 1463 | @Override |
| 1464 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1465 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1466 | != PackageManager.PERMISSION_GRANTED) { |
| 1467 | pw.println("Permission Denial: can't dump dbinfo from from pid=" |
| 1468 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1469 | + " without permission " + android.Manifest.permission.DUMP); |
| 1470 | return; |
| 1471 | } |
| 1472 | |
| 1473 | mActivityManagerService.dumpDbInfo(fd, pw, args); |
| 1474 | } |
| 1475 | } |
| 1476 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1477 | static class CpuBinder extends Binder { |
| 1478 | ActivityManagerService mActivityManagerService; |
| 1479 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1480 | mActivityManagerService = activityManagerService; |
| 1481 | } |
| 1482 | |
| 1483 | @Override |
| 1484 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1485 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1486 | != PackageManager.PERMISSION_GRANTED) { |
| 1487 | pw.println("Permission Denial: can't dump cpuinfo from from pid=" |
| 1488 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1489 | + " without permission " + android.Manifest.permission.DUMP); |
| 1490 | return; |
| 1491 | } |
| 1492 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1493 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1494 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1495 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1496 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1497 | } |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | private ActivityManagerService() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1502 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1503 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1504 | mFgBroadcastQueue = new BroadcastQueue(this, "foreground", BROADCAST_FG_TIMEOUT); |
| 1505 | mBgBroadcastQueue = new BroadcastQueue(this, "background", BROADCAST_BG_TIMEOUT); |
| 1506 | mBroadcastQueues[0] = mFgBroadcastQueue; |
| 1507 | mBroadcastQueues[1] = mBgBroadcastQueue; |
| 1508 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1509 | mServices = new ActiveServices(this); |
| 1510 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1511 | File dataDir = Environment.getDataDirectory(); |
| 1512 | File systemDir = new File(dataDir, "system"); |
| 1513 | systemDir.mkdirs(); |
| 1514 | mBatteryStatsService = new BatteryStatsService(new File( |
| 1515 | systemDir, "batterystats.bin").toString()); |
| 1516 | mBatteryStatsService.getActiveStatistics().readLocked(); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1517 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1518 | mOnBattery = DEBUG_POWER ? true |
| 1519 | : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1520 | mBatteryStatsService.getActiveStatistics().setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1521 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1522 | mUsageStatsService = new UsageStatsService(new File( |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1523 | systemDir, "usagestats").toString()); |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 1524 | mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1525 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1526 | // User 0 is the first and only user that runs at boot. |
| 1527 | mStartedUsers.put(0, new UserStartedState(new UserHandle(0), true)); |
| 1528 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1529 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1530 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1531 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1532 | mConfiguration.setToDefaults(); |
| 1533 | mConfiguration.locale = Locale.getDefault(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 1534 | mConfigurationSeq = mConfiguration.seq = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1535 | mProcessStats.init(); |
| 1536 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 1537 | mCompatModePackages = new CompatModePackages(this, systemDir); |
| 1538 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1539 | // Add ourself to the Watchdog monitors. |
| 1540 | Watchdog.getInstance().addMonitor(this); |
| 1541 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1542 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1543 | public void run() { |
| 1544 | while (true) { |
| 1545 | try { |
| 1546 | try { |
| 1547 | synchronized(this) { |
| 1548 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1549 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1550 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1551 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1552 | // + ", write delay=" + nextWriteDelay); |
| 1553 | if (nextWriteDelay < nextCpuDelay) { |
| 1554 | nextCpuDelay = nextWriteDelay; |
| 1555 | } |
| 1556 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1557 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1558 | this.wait(nextCpuDelay); |
| 1559 | } |
| 1560 | } |
| 1561 | } catch (InterruptedException e) { |
| 1562 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1563 | updateCpuStatsNow(); |
| 1564 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1565 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | }; |
| 1570 | mProcessStatsThread.start(); |
| 1571 | } |
| 1572 | |
| 1573 | @Override |
| 1574 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1575 | throws RemoteException { |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 1576 | if (code == SYSPROPS_TRANSACTION) { |
| 1577 | // We need to tell all apps about the system property change. |
| 1578 | ArrayList<IBinder> procs = new ArrayList<IBinder>(); |
| 1579 | synchronized(this) { |
| 1580 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 1581 | final int NA = apps.size(); |
| 1582 | for (int ia=0; ia<NA; ia++) { |
| 1583 | ProcessRecord app = apps.valueAt(ia); |
| 1584 | if (app.thread != null) { |
| 1585 | procs.add(app.thread.asBinder()); |
| 1586 | } |
| 1587 | } |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | int N = procs.size(); |
| 1592 | for (int i=0; i<N; i++) { |
| 1593 | Parcel data2 = Parcel.obtain(); |
| 1594 | try { |
| 1595 | procs.get(i).transact(IBinder.SYSPROPS_TRANSACTION, data2, null, 0); |
| 1596 | } catch (RemoteException e) { |
| 1597 | } |
| 1598 | data2.recycle(); |
| 1599 | } |
| 1600 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1601 | try { |
| 1602 | return super.onTransact(code, data, reply, flags); |
| 1603 | } catch (RuntimeException e) { |
| 1604 | // The activity manager only throws security exceptions, so let's |
| 1605 | // log all others. |
| 1606 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1607 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1608 | } |
| 1609 | throw e; |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1614 | final long now = SystemClock.uptimeMillis(); |
| 1615 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1616 | return; |
| 1617 | } |
| 1618 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1619 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | mProcessStatsThread.notify(); |
| 1621 | } |
| 1622 | } |
| 1623 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1624 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1625 | void updateCpuStatsNow() { |
| 1626 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1627 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1628 | final long now = SystemClock.uptimeMillis(); |
| 1629 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1630 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1631 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1632 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1633 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1634 | haveNewCpuStats = true; |
| 1635 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1636 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1637 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1638 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1639 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1640 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1642 | int user = mProcessStats.getLastUserTime(); |
| 1643 | int system = mProcessStats.getLastSystemTime(); |
| 1644 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1645 | int irq = mProcessStats.getLastIrqTime(); |
| 1646 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1647 | int idle = mProcessStats.getLastIdleTime(); |
| 1648 | |
| 1649 | int total = user + system + iowait + irq + softIrq + idle; |
| 1650 | if (total == 0) total = 1; |
| 1651 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1652 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1653 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1654 | (user * 100) / total, |
| 1655 | (system * 100) / total, |
| 1656 | (iowait * 100) / total, |
| 1657 | (irq * 100) / total, |
| 1658 | (softIrq * 100) / total); |
| 1659 | } |
| 1660 | } |
| 1661 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1662 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1663 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1664 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1665 | synchronized(mPidsSelfLocked) { |
| 1666 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1667 | if (mOnBattery) { |
| 1668 | int perc = bstats.startAddingCpuLocked(); |
| 1669 | int totalUTime = 0; |
| 1670 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1671 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1672 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1673 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1674 | if (!st.working) { |
| 1675 | continue; |
| 1676 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1677 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1678 | int otherUTime = (st.rel_utime*perc)/100; |
| 1679 | int otherSTime = (st.rel_stime*perc)/100; |
| 1680 | totalUTime += otherUTime; |
| 1681 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | if (pr != null) { |
| 1683 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1684 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1685 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1686 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1687 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1688 | } else { |
| 1689 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1690 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1691 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1692 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1693 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1694 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1695 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1696 | } |
| 1697 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1698 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1699 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1700 | } |
| 1701 | } |
| 1702 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1703 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1704 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1705 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1706 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1707 | } |
| 1708 | } |
| 1709 | } |
| 1710 | } |
| 1711 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1712 | @Override |
| 1713 | public void batteryNeedsCpuUpdate() { |
| 1714 | updateCpuStatsNow(); |
| 1715 | } |
| 1716 | |
| 1717 | @Override |
| 1718 | public void batteryPowerChanged(boolean onBattery) { |
| 1719 | // When plugging in, update the CPU stats first before changing |
| 1720 | // the plug state. |
| 1721 | updateCpuStatsNow(); |
| 1722 | synchronized (this) { |
| 1723 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1724 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1725 | } |
| 1726 | } |
| 1727 | } |
| 1728 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1729 | /** |
| 1730 | * Initialize the application bind args. These are passed to each |
| 1731 | * process when the bindApplication() IPC is sent to the process. They're |
| 1732 | * lazily setup to make sure the services are running when they're asked for. |
| 1733 | */ |
| 1734 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1735 | if (mAppBindArgs == null) { |
| 1736 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1737 | |
| 1738 | // Setup the application init args |
| 1739 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1740 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1741 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1742 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1743 | } |
| 1744 | return mAppBindArgs; |
| 1745 | } |
| 1746 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1747 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1748 | if (mFocusedActivity != r) { |
| 1749 | mFocusedActivity = r; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1750 | if (r != null) { |
| 1751 | mWindowManager.setFocusedApp(r.appToken, true); |
| 1752 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1756 | private final void updateLruProcessInternalLocked(ProcessRecord app, |
| 1757 | boolean oomAdj, boolean updateActivityTime, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1758 | // 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] | 1759 | int lrui = mLruProcesses.indexOf(app); |
| 1760 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1761 | |
| 1762 | int i = mLruProcesses.size()-1; |
| 1763 | int skipTop = 0; |
| 1764 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1765 | app.lruSeq = mLruSeq; |
| 1766 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1767 | // compute the new weight for this process. |
| 1768 | if (updateActivityTime) { |
| 1769 | app.lastActivityTime = SystemClock.uptimeMillis(); |
| 1770 | } |
| 1771 | if (app.activities.size() > 0) { |
| 1772 | // If this process has activities, we more strongly want to keep |
| 1773 | // it around. |
| 1774 | app.lruWeight = app.lastActivityTime; |
| 1775 | } else if (app.pubProviders.size() > 0) { |
| 1776 | // If this process contains content providers, we want to keep |
| 1777 | // it a little more strongly. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1778 | app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1779 | // 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] | 1780 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1781 | } else { |
| 1782 | // If this process doesn't have activities, we less strongly |
| 1783 | // want to keep it around, and generally want to avoid getting |
| 1784 | // in front of any very recently used activities. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1785 | app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1786 | // 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] | 1787 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1788 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1789 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1790 | while (i >= 0) { |
| 1791 | ProcessRecord p = mLruProcesses.get(i); |
| 1792 | // If this app shouldn't be in front of the first N background |
| 1793 | // apps, then skip over that many that are currently hidden. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1794 | if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1795 | skipTop--; |
| 1796 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1797 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1798 | mLruProcesses.add(i+1, app); |
| 1799 | break; |
| 1800 | } |
| 1801 | i--; |
| 1802 | } |
| 1803 | if (i < 0) { |
| 1804 | mLruProcesses.add(0, app); |
| 1805 | } |
| 1806 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1807 | // If the app is currently using a content provider or service, |
| 1808 | // bump those processes as well. |
| 1809 | if (app.connections.size() > 0) { |
| 1810 | for (ConnectionRecord cr : app.connections) { |
| 1811 | if (cr.binding != null && cr.binding.service != null |
| 1812 | && cr.binding.service.app != null |
| 1813 | && cr.binding.service.app.lruSeq != mLruSeq) { |
Björn Davidsson | 90f9e31 | 2010-11-18 08:26:27 +0100 | [diff] [blame] | 1814 | updateLruProcessInternalLocked(cr.binding.service.app, false, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1815 | updateActivityTime, i+1); |
| 1816 | } |
| 1817 | } |
| 1818 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1819 | for (int j=app.conProviders.size()-1; j>=0; j--) { |
| 1820 | ContentProviderRecord cpr = app.conProviders.get(j).provider; |
| 1821 | if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) { |
Jean-Baptiste Queru | b8c6405 | 2012-06-11 11:01:19 -0700 | [diff] [blame] | 1822 | updateLruProcessInternalLocked(cpr.proc, false, |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1823 | updateActivityTime, i+1); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1827 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1828 | if (oomAdj) { |
| 1829 | updateOomAdjLocked(); |
| 1830 | } |
| 1831 | } |
| 1832 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1833 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1834 | boolean oomAdj, boolean updateActivityTime) { |
| 1835 | mLruSeq++; |
| 1836 | updateLruProcessInternalLocked(app, oomAdj, updateActivityTime, 0); |
| 1837 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1838 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1839 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1840 | String processName, int uid) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1841 | if (uid == Process.SYSTEM_UID) { |
Amith Yamasani | 0184ce9 | 2012-03-28 22:41:41 -0700 | [diff] [blame] | 1842 | // The system gets to run in any process. If there are multiple |
| 1843 | // processes with the same uid, just pick the first (this |
| 1844 | // should never happen). |
| 1845 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1846 | processName); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1847 | if (procs == null) return null; |
| 1848 | final int N = procs.size(); |
| 1849 | for (int i = 0; i < N; i++) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1850 | if (UserHandle.isSameUser(procs.keyAt(i), uid)) return procs.valueAt(i); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1851 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1852 | } |
| 1853 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1854 | return proc; |
| 1855 | } |
| 1856 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1857 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1858 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1859 | try { |
| 1860 | if (pm.performDexOpt(packageName)) { |
| 1861 | mDidDexOpt = true; |
| 1862 | } |
| 1863 | } catch (RemoteException e) { |
| 1864 | } |
| 1865 | } |
| 1866 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1867 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1868 | int transit = mWindowManager.getPendingAppTransition(); |
| 1869 | return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN |
| 1870 | || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN |
| 1871 | || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT; |
| 1872 | } |
| 1873 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1874 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1875 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1876 | String hostingType, ComponentName hostingName, boolean allowWhileBooting, |
| 1877 | boolean isolated) { |
| 1878 | ProcessRecord app; |
| 1879 | if (!isolated) { |
| 1880 | app = getProcessRecordLocked(processName, info.uid); |
| 1881 | } else { |
| 1882 | // If this is an isolated process, it can't re-use an existing process. |
| 1883 | app = null; |
| 1884 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1885 | // We don't have to do anything more if: |
| 1886 | // (1) There is an existing application record; and |
| 1887 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 1888 | // object attached to it so we know it couldn't have crashed; and |
| 1889 | // (3) There is a pid assigned to it, so it is either starting or |
| 1890 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1891 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1892 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 1893 | + " thread=" + (app != null ? app.thread : null) |
| 1894 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1895 | if (app != null && app.pid > 0) { |
| 1896 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1897 | // We already have the app running, or are waiting for it to |
| 1898 | // 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] | 1899 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1900 | // If this is a new package in the process, add the package to the list |
| 1901 | app.addPackage(info.packageName); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1902 | return app; |
| 1903 | } else { |
| 1904 | // An application record is attached to a previous process, |
| 1905 | // clean it up now. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1906 | if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 1907 | handleAppDiedLocked(app, true, true); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1908 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1909 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1910 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1911 | String hostingNameStr = hostingName != null |
| 1912 | ? hostingName.flattenToShortString() : null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1913 | |
| 1914 | if (!isolated) { |
| 1915 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 1916 | // If we are in the background, then check to see if this process |
| 1917 | // is bad. If so, we will just silently fail. |
| 1918 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 1919 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 1920 | + "/" + info.processName); |
| 1921 | return null; |
| 1922 | } |
| 1923 | } else { |
| 1924 | // When the user is explicitly starting a process, then clear its |
| 1925 | // crash count so that we won't make it bad until they see at |
| 1926 | // least one crash dialog again, and make the process good again |
| 1927 | // if it had been bad. |
| 1928 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1929 | + "/" + info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1930 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 1931 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 1932 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid, |
| 1933 | info.processName); |
| 1934 | mBadProcesses.remove(info.processName, info.uid); |
| 1935 | if (app != null) { |
| 1936 | app.bad = false; |
| 1937 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1941 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1942 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1943 | app = newProcessRecordLocked(null, info, processName, isolated); |
| 1944 | if (app == null) { |
| 1945 | Slog.w(TAG, "Failed making new process record for " |
| 1946 | + processName + "/" + info.uid + " isolated=" + isolated); |
| 1947 | return null; |
| 1948 | } |
| 1949 | mProcessNames.put(processName, app.uid, app); |
| 1950 | if (isolated) { |
| 1951 | mIsolatedProcesses.put(app.uid, app); |
| 1952 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1953 | } else { |
| 1954 | // If this is a new package in the process, add the package to the list |
| 1955 | app.addPackage(info.packageName); |
| 1956 | } |
| 1957 | |
| 1958 | // If the system is not ready yet, then hold off on starting this |
| 1959 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1960 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1961 | && !isAllowedWhileBooting(info) |
| 1962 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1963 | if (!mProcessesOnHold.contains(app)) { |
| 1964 | mProcessesOnHold.add(app); |
| 1965 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1966 | 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] | 1967 | return app; |
| 1968 | } |
| 1969 | |
| 1970 | startProcessLocked(app, hostingType, hostingNameStr); |
| 1971 | return (app.pid != 0) ? app : null; |
| 1972 | } |
| 1973 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1974 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 1975 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 1976 | } |
| 1977 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1978 | private final void startProcessLocked(ProcessRecord app, |
| 1979 | String hostingType, String hostingNameStr) { |
| 1980 | if (app.pid > 0 && app.pid != MY_PID) { |
| 1981 | synchronized (mPidsSelfLocked) { |
| 1982 | mPidsSelfLocked.remove(app.pid); |
| 1983 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 1984 | } |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 1985 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1986 | } |
| 1987 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1988 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 1989 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1990 | mProcessesOnHold.remove(app); |
| 1991 | |
| 1992 | updateCpuStats(); |
| 1993 | |
| 1994 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 1995 | mProcDeaths[0] = 0; |
| 1996 | |
| 1997 | try { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1998 | int uid = app.uid; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1999 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2000 | int[] gids = null; |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2001 | int mountExternal = Zygote.MOUNT_EXTERNAL_NONE; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2002 | if (!app.isolated) { |
| 2003 | try { |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2004 | final PackageManager pm = mContext.getPackageManager(); |
| 2005 | gids = pm.getPackageGids(app.info.packageName); |
Jeff Sharkey | e217ee4 | 2012-08-28 16:23:01 -0700 | [diff] [blame] | 2006 | |
| 2007 | if (Environment.isExternalStorageEmulated()) { |
| 2008 | if (pm.checkPermission( |
| 2009 | android.Manifest.permission.ACCESS_ALL_EXTERNAL_STORAGE, |
| 2010 | app.info.packageName) == PERMISSION_GRANTED) { |
| 2011 | mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL; |
| 2012 | } else { |
| 2013 | mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER; |
| 2014 | } |
| 2015 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2016 | } catch (PackageManager.NameNotFoundException e) { |
| 2017 | Slog.w(TAG, "Unable to retrieve gids", e); |
| 2018 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2019 | } |
| 2020 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 2021 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2022 | && mTopComponent != null |
| 2023 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 2024 | uid = 0; |
| 2025 | } |
| 2026 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 2027 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 2028 | uid = 0; |
| 2029 | } |
| 2030 | } |
| 2031 | int debugFlags = 0; |
| 2032 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 2033 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
Elliott Hughes | fa36aee | 2011-06-17 14:39:41 -0700 | [diff] [blame] | 2034 | // Also turn on CheckJNI for debuggable apps. It's quite |
| 2035 | // awkward to turn on otherwise. |
| 2036 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2037 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 2038 | // Run the app in safe mode if its manifest requests so or the |
| 2039 | // system is booted in safe mode. |
| 2040 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 2041 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 2042 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 2043 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2044 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 2045 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 2046 | } |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 2047 | if ("1".equals(SystemProperties.get("debug.jni.logging"))) { |
| 2048 | debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING; |
| 2049 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2050 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 2051 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 2052 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2053 | |
| 2054 | // Start the process. It will either succeed and return a result containing |
| 2055 | // the PID of the new process, or else throw a RuntimeException. |
| 2056 | Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread", |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2057 | app.processName, uid, uid, gids, debugFlags, mountExternal, |
Stephen Smalley | 83d9eda | 2012-01-13 08:34:17 -0500 | [diff] [blame] | 2058 | app.info.targetSdkVersion, null, null); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2059 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2060 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 2061 | synchronized (bs) { |
| 2062 | if (bs.isOnBattery()) { |
| 2063 | app.batteryStats.incStartsLocked(); |
| 2064 | } |
| 2065 | } |
| 2066 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2067 | EventLog.writeEvent(EventLogTags.AM_PROC_START, startResult.pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2068 | app.processName, hostingType, |
| 2069 | hostingNameStr != null ? hostingNameStr : ""); |
| 2070 | |
| 2071 | if (app.persistent) { |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2072 | Watchdog.getInstance().processStarted(app.processName, startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2073 | } |
| 2074 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 2075 | StringBuilder buf = mStringBuilder; |
| 2076 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2077 | buf.append("Start proc "); |
| 2078 | buf.append(app.processName); |
| 2079 | buf.append(" for "); |
| 2080 | buf.append(hostingType); |
| 2081 | if (hostingNameStr != null) { |
| 2082 | buf.append(" "); |
| 2083 | buf.append(hostingNameStr); |
| 2084 | } |
| 2085 | buf.append(": pid="); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2086 | buf.append(startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2087 | buf.append(" uid="); |
| 2088 | buf.append(uid); |
| 2089 | buf.append(" gids={"); |
| 2090 | if (gids != null) { |
| 2091 | for (int gi=0; gi<gids.length; gi++) { |
| 2092 | if (gi != 0) buf.append(", "); |
| 2093 | buf.append(gids[gi]); |
| 2094 | |
| 2095 | } |
| 2096 | } |
| 2097 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2098 | Slog.i(TAG, buf.toString()); |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2099 | app.setPid(startResult.pid); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2100 | app.usingWrapper = startResult.usingWrapper; |
| 2101 | app.removed = false; |
| 2102 | synchronized (mPidsSelfLocked) { |
| 2103 | this.mPidsSelfLocked.put(startResult.pid, app); |
| 2104 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 2105 | msg.obj = app; |
| 2106 | mHandler.sendMessageDelayed(msg, startResult.usingWrapper |
| 2107 | ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2108 | } |
| 2109 | } catch (RuntimeException e) { |
| 2110 | // XXX do better error recovery. |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2111 | app.setPid(0); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2112 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2113 | } |
| 2114 | } |
| 2115 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2116 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2117 | if (resumed) { |
| 2118 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 2119 | } else { |
| 2120 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 2121 | } |
| 2122 | } |
| 2123 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2124 | boolean startHomeActivityLocked(int userId, UserStartedState startingUser) { |
Mike Lockwood | a8f767a | 2011-08-31 14:32:37 -0400 | [diff] [blame] | 2125 | if (mHeadless) { |
| 2126 | // Added because none of the other calls to ensureBootCompleted seem to fire |
| 2127 | // when running headless. |
| 2128 | ensureBootCompleted(); |
| 2129 | return false; |
| 2130 | } |
| 2131 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2132 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2133 | && mTopAction == null) { |
| 2134 | // We are running in factory test mode, but unable to find |
| 2135 | // the factory test app, so just sit around displaying the |
| 2136 | // error message and don't try to start anything. |
| 2137 | return false; |
| 2138 | } |
| 2139 | Intent intent = new Intent( |
| 2140 | mTopAction, |
| 2141 | mTopData != null ? Uri.parse(mTopData) : null); |
| 2142 | intent.setComponent(mTopComponent); |
| 2143 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 2144 | intent.addCategory(Intent.CATEGORY_HOME); |
| 2145 | } |
| 2146 | ActivityInfo aInfo = |
| 2147 | intent.resolveActivityInfo(mContext.getPackageManager(), |
| 2148 | STOCK_PM_FLAGS); |
| 2149 | if (aInfo != null) { |
| 2150 | intent.setComponent(new ComponentName( |
| 2151 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2152 | // Don't do this if the home app is currently being |
| 2153 | // instrumented. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2154 | aInfo = new ActivityInfo(aInfo); |
| 2155 | aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2156 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 2157 | aInfo.applicationInfo.uid); |
| 2158 | if (app == null || app.instrumentationClass == null) { |
| 2159 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2160 | mMainStack.startActivityLocked(null, intent, null, aInfo, |
| 2161 | null, null, 0, 0, 0, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2162 | } |
| 2163 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2164 | if (startingUser != null) { |
| 2165 | mMainStack.addStartingUserLocked(startingUser); |
| 2166 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2167 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2168 | return true; |
| 2169 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2170 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2171 | /** |
| 2172 | * Starts the "new version setup screen" if appropriate. |
| 2173 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2174 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2175 | // Only do this once per boot. |
| 2176 | if (mCheckedForSetup) { |
| 2177 | return; |
| 2178 | } |
| 2179 | |
| 2180 | // We will show this screen if the current one is a different |
| 2181 | // version than the last one shown, and we are not running in |
| 2182 | // low-level factory test mode. |
| 2183 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2184 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
| 2185 | Settings.Secure.getInt(resolver, |
| 2186 | Settings.Secure.DEVICE_PROVISIONED, 0) != 0) { |
| 2187 | mCheckedForSetup = true; |
| 2188 | |
| 2189 | // See if we should be showing the platform update setup UI. |
| 2190 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 2191 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 2192 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 2193 | |
| 2194 | // We don't allow third party apps to replace this. |
| 2195 | ResolveInfo ri = null; |
| 2196 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 2197 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 2198 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2199 | ri = ris.get(i); |
| 2200 | break; |
| 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | if (ri != null) { |
| 2205 | String vers = ri.activityInfo.metaData != null |
| 2206 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 2207 | : null; |
| 2208 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2209 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2210 | Intent.METADATA_SETUP_VERSION); |
| 2211 | } |
| 2212 | String lastVers = Settings.Secure.getString( |
| 2213 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2214 | if (vers != null && !vers.equals(lastVers)) { |
| 2215 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2216 | intent.setComponent(new ComponentName( |
| 2217 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2218 | mMainStack.startActivityLocked(null, intent, null, ri.activityInfo, |
| 2219 | null, null, 0, 0, 0, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2220 | } |
| 2221 | } |
| 2222 | } |
| 2223 | } |
| 2224 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2225 | CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2226 | return mCompatModePackages.compatibilityInfoForPackageLocked(ai); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2227 | } |
| 2228 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2229 | void enforceNotIsolatedCaller(String caller) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2230 | if (UserHandle.isIsolated(Binder.getCallingUid())) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2231 | throw new SecurityException("Isolated process not allowed to call " + caller); |
| 2232 | } |
| 2233 | } |
| 2234 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2235 | public int getFrontActivityScreenCompatMode() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2236 | enforceNotIsolatedCaller("getFrontActivityScreenCompatMode"); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2237 | synchronized (this) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2238 | return mCompatModePackages.getFrontActivityScreenCompatModeLocked(); |
| 2239 | } |
| 2240 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2241 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2242 | public void setFrontActivityScreenCompatMode(int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2243 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2244 | "setFrontActivityScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2245 | synchronized (this) { |
| 2246 | mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode); |
| 2247 | } |
| 2248 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2249 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2250 | public int getPackageScreenCompatMode(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2251 | enforceNotIsolatedCaller("getPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2252 | synchronized (this) { |
| 2253 | return mCompatModePackages.getPackageScreenCompatModeLocked(packageName); |
| 2254 | } |
| 2255 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2256 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2257 | public void setPackageScreenCompatMode(String packageName, int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2258 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2259 | "setPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2260 | synchronized (this) { |
| 2261 | mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2262 | } |
| 2263 | } |
| 2264 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2265 | public boolean getPackageAskScreenCompat(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2266 | enforceNotIsolatedCaller("getPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2267 | synchronized (this) { |
| 2268 | return mCompatModePackages.getPackageAskCompatModeLocked(packageName); |
| 2269 | } |
| 2270 | } |
| 2271 | |
| 2272 | public void setPackageAskScreenCompat(String packageName, boolean ask) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2273 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2274 | "setPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2275 | synchronized (this) { |
| 2276 | mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask); |
| 2277 | } |
| 2278 | } |
| 2279 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2280 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2281 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2282 | updateUsageStats(r, true); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2283 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2284 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2285 | private void dispatchProcessesChanged() { |
| 2286 | int N; |
| 2287 | synchronized (this) { |
| 2288 | N = mPendingProcessChanges.size(); |
| 2289 | if (mActiveProcessChanges.length < N) { |
| 2290 | mActiveProcessChanges = new ProcessChangeItem[N]; |
| 2291 | } |
| 2292 | mPendingProcessChanges.toArray(mActiveProcessChanges); |
| 2293 | mAvailProcessChanges.addAll(mPendingProcessChanges); |
| 2294 | mPendingProcessChanges.clear(); |
| 2295 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "*** Delivering " + N + " process changes"); |
| 2296 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2297 | int i = mProcessObservers.beginBroadcast(); |
| 2298 | while (i > 0) { |
| 2299 | i--; |
| 2300 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2301 | if (observer != null) { |
| 2302 | try { |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2303 | for (int j=0; j<N; j++) { |
| 2304 | ProcessChangeItem item = mActiveProcessChanges[j]; |
| 2305 | if ((item.changes&ProcessChangeItem.CHANGE_ACTIVITIES) != 0) { |
| 2306 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "ACTIVITIES CHANGED pid=" |
| 2307 | + item.pid + " uid=" + item.uid + ": " |
| 2308 | + item.foregroundActivities); |
| 2309 | observer.onForegroundActivitiesChanged(item.pid, item.uid, |
| 2310 | item.foregroundActivities); |
| 2311 | } |
| 2312 | if ((item.changes&ProcessChangeItem.CHANGE_IMPORTANCE) != 0) { |
| 2313 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "IMPORTANCE CHANGED pid=" |
| 2314 | + item.pid + " uid=" + item.uid + ": " + item.importance); |
| 2315 | observer.onImportanceChanged(item.pid, item.uid, |
| 2316 | item.importance); |
| 2317 | } |
| 2318 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2319 | } catch (RemoteException e) { |
| 2320 | } |
| 2321 | } |
| 2322 | } |
| 2323 | mProcessObservers.finishBroadcast(); |
| 2324 | } |
| 2325 | |
| 2326 | private void dispatchProcessDied(int pid, int uid) { |
| 2327 | int i = mProcessObservers.beginBroadcast(); |
| 2328 | while (i > 0) { |
| 2329 | i--; |
| 2330 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2331 | if (observer != null) { |
| 2332 | try { |
| 2333 | observer.onProcessDied(pid, uid); |
| 2334 | } catch (RemoteException e) { |
| 2335 | } |
| 2336 | } |
| 2337 | } |
| 2338 | mProcessObservers.finishBroadcast(); |
| 2339 | } |
| 2340 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2341 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2342 | final int N = mPendingActivityLaunches.size(); |
| 2343 | if (N <= 0) { |
| 2344 | return; |
| 2345 | } |
| 2346 | for (int i=0; i<N; i++) { |
| 2347 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2348 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 2349 | pal.startFlags, doResume && i == (N-1), null); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2350 | } |
| 2351 | mPendingActivityLaunches.clear(); |
| 2352 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2353 | |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2354 | public final int startActivity(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2355 | Intent intent, String resolvedType, IBinder resultTo, |
| 2356 | String resultWho, int requestCode, int startFlags, |
| 2357 | String profileFile, ParcelFileDescriptor profileFd, Bundle options) { |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 2358 | return startActivityAsUser(caller, intent, resolvedType, resultTo, resultWho, requestCode, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2359 | startFlags, profileFile, profileFd, options, UserHandle.getCallingUserId()); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | public final int startActivityAsUser(IApplicationThread caller, |
| 2363 | Intent intent, String resolvedType, IBinder resultTo, |
| 2364 | String resultWho, int requestCode, int startFlags, |
| 2365 | String profileFile, ParcelFileDescriptor profileFd, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2366 | enforceNotIsolatedCaller("startActivity"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2367 | if (userId != UserHandle.getCallingUserId()) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2368 | userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
| 2369 | false, true, "startActivity", null); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 2370 | } else { |
| 2371 | if (intent.getCategories() != null |
| 2372 | && intent.getCategories().contains(Intent.CATEGORY_HOME)) { |
| 2373 | // Requesting home, set the identity to the current user |
| 2374 | // HACK! |
| 2375 | userId = mCurrentUserId; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2376 | } |
| 2377 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2378 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2379 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
| 2380 | null, null, options, userId); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | public final WaitResult startActivityAndWait(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2384 | Intent intent, String resolvedType, IBinder resultTo, |
| 2385 | String resultWho, int requestCode, int startFlags, String profileFile, |
| 2386 | ParcelFileDescriptor profileFd, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2387 | enforceNotIsolatedCaller("startActivityAndWait"); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2388 | WaitResult res = new WaitResult(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2389 | mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2390 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2391 | res, null, options, UserHandle.getCallingUserId()); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2392 | return res; |
| 2393 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 2394 | |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2395 | public final int startActivityWithConfig(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2396 | Intent intent, String resolvedType, IBinder resultTo, |
| 2397 | String resultWho, int requestCode, int startFlags, Configuration config, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2398 | Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2399 | enforceNotIsolatedCaller("startActivityWithConfig"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2400 | userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
| 2401 | false, true, "startActivityWithConfig", null); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2402 | int ret = mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2403 | resultTo, resultWho, requestCode, startFlags, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2404 | null, null, null, config, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2405 | return ret; |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2406 | } |
| 2407 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2408 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2409 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2410 | IBinder resultTo, String resultWho, int requestCode, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2411 | int flagsMask, int flagsValues, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2412 | enforceNotIsolatedCaller("startActivityIntentSender"); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2413 | // Refuse possible leaked file descriptors |
| 2414 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2415 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2416 | } |
| 2417 | |
| 2418 | IIntentSender sender = intent.getTarget(); |
| 2419 | if (!(sender instanceof PendingIntentRecord)) { |
| 2420 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2421 | } |
| 2422 | |
| 2423 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2424 | |
| 2425 | synchronized (this) { |
| 2426 | // If this is coming from the currently resumed activity, it is |
| 2427 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2428 | if (mMainStack.mResumedActivity != null |
| 2429 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2430 | Binder.getCallingUid()) { |
| 2431 | mAppSwitchesAllowedTime = 0; |
| 2432 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2433 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2434 | int ret = pir.sendInner(0, fillInIntent, resolvedType, null, null, |
| 2435 | resultTo, resultWho, requestCode, flagsMask, flagsValues, options); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2436 | return ret; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2437 | } |
| 2438 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2439 | public boolean startNextMatchingActivity(IBinder callingActivity, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2440 | Intent intent, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2441 | // Refuse possible leaked file descriptors |
| 2442 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2443 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2444 | } |
| 2445 | |
| 2446 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2447 | ActivityRecord r = mMainStack.isInStackLocked(callingActivity); |
| 2448 | if (r == null) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2449 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2450 | return false; |
| 2451 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2452 | if (r.app == null || r.app.thread == null) { |
| 2453 | // The caller is not running... d'oh! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2454 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2455 | return false; |
| 2456 | } |
| 2457 | intent = new Intent(intent); |
| 2458 | // The caller is not allowed to change the data. |
| 2459 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2460 | // And we are resetting to find the next component... |
| 2461 | intent.setComponent(null); |
| 2462 | |
| 2463 | ActivityInfo aInfo = null; |
| 2464 | try { |
| 2465 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2466 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2467 | intent, r.resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2468 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2469 | UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2470 | |
| 2471 | // Look for the original activity in the list... |
| 2472 | final int N = resolves != null ? resolves.size() : 0; |
| 2473 | for (int i=0; i<N; i++) { |
| 2474 | ResolveInfo rInfo = resolves.get(i); |
| 2475 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2476 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2477 | // We found the current one... the next matching is |
| 2478 | // after it. |
| 2479 | i++; |
| 2480 | if (i<N) { |
| 2481 | aInfo = resolves.get(i).activityInfo; |
| 2482 | } |
| 2483 | break; |
| 2484 | } |
| 2485 | } |
| 2486 | } catch (RemoteException e) { |
| 2487 | } |
| 2488 | |
| 2489 | if (aInfo == null) { |
| 2490 | // Nobody who is next! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2491 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2492 | return false; |
| 2493 | } |
| 2494 | |
| 2495 | intent.setComponent(new ComponentName( |
| 2496 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2497 | intent.setFlags(intent.getFlags()&~( |
| 2498 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2499 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2500 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2501 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2502 | |
| 2503 | // Okay now we need to start the new activity, replacing the |
| 2504 | // currently running activity. This is a little tricky because |
| 2505 | // we want to start the new one as if the current one is finished, |
| 2506 | // but not finish the current one first so that there is no flicker. |
| 2507 | // And thus... |
| 2508 | final boolean wasFinishing = r.finishing; |
| 2509 | r.finishing = true; |
| 2510 | |
| 2511 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2512 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2513 | final String resultWho = r.resultWho; |
| 2514 | final int requestCode = r.requestCode; |
| 2515 | r.resultTo = null; |
| 2516 | if (resultTo != null) { |
| 2517 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2518 | } |
| 2519 | |
| 2520 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2521 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2522 | r.resolvedType, aInfo, resultTo != null ? resultTo.appToken : null, |
| 2523 | resultWho, requestCode, -1, r.launchedFromUid, 0, |
| 2524 | options, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2525 | Binder.restoreCallingIdentity(origId); |
| 2526 | |
| 2527 | r.finishing = wasFinishing; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2528 | if (res != ActivityManager.START_SUCCESS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2529 | return false; |
| 2530 | } |
| 2531 | return true; |
| 2532 | } |
| 2533 | } |
| 2534 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2535 | final int startActivityInPackage(int uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2536 | Intent intent, String resolvedType, IBinder resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2537 | String resultWho, int requestCode, int startFlags, Bundle options, int userId) { |
| 2538 | |
| 2539 | userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
| 2540 | false, true, "startActivityInPackage", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2541 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2542 | int ret = mMainStack.startActivityMayWait(null, uid, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2543 | resultTo, resultWho, requestCode, startFlags, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2544 | null, null, null, null, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2545 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | public final int startActivities(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2549 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2550 | enforceNotIsolatedCaller("startActivities"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2551 | int ret = mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2552 | options, UserHandle.getCallingUserId()); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2553 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2554 | } |
| 2555 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2556 | final int startActivitiesInPackage(int uid, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2557 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2558 | Bundle options, int userId) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2559 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2560 | userId = handleIncomingUserLocked(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
| 2561 | false, true, "startActivityInPackage", null); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2562 | int ret = mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2563 | options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2564 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2565 | } |
| 2566 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2567 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2568 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2569 | // Quick case: check if the top-most recent task is the same. |
| 2570 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2571 | return; |
| 2572 | } |
| 2573 | // 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] | 2574 | for (int i=0; i<N; i++) { |
| 2575 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2576 | if (task.userId == tr.userId |
| 2577 | && ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2578 | || (task.intent != null && task.intent.filterEquals(tr.intent)))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2579 | mRecentTasks.remove(i); |
| 2580 | i--; |
| 2581 | N--; |
| 2582 | if (task.intent == null) { |
| 2583 | // If the new recent task we are adding is not fully |
| 2584 | // specified, then replace it with the existing recent task. |
| 2585 | task = tr; |
| 2586 | } |
| 2587 | } |
| 2588 | } |
| 2589 | if (N >= MAX_RECENT_TASKS) { |
| 2590 | mRecentTasks.remove(N-1); |
| 2591 | } |
| 2592 | mRecentTasks.add(0, task); |
| 2593 | } |
| 2594 | |
| 2595 | public void setRequestedOrientation(IBinder token, |
| 2596 | int requestedOrientation) { |
| 2597 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2598 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2599 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2600 | return; |
| 2601 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2602 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2603 | mWindowManager.setAppOrientation(r.appToken, requestedOrientation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2604 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2605 | mConfiguration, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2606 | r.mayFreezeScreenLocked(r.app) ? r.appToken : null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2607 | if (config != null) { |
| 2608 | r.frozenBeforeDestroy = true; |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 2609 | if (!updateConfigurationLocked(config, r, false, false)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2610 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2611 | } |
| 2612 | } |
| 2613 | Binder.restoreCallingIdentity(origId); |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | public int getRequestedOrientation(IBinder token) { |
| 2618 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2619 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2620 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2621 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2622 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2623 | return mWindowManager.getAppOrientation(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2624 | } |
| 2625 | } |
| 2626 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2627 | /** |
| 2628 | * This is the internal entry point for handling Activity.finish(). |
| 2629 | * |
| 2630 | * @param token The Binder token referencing the Activity we want to finish. |
| 2631 | * @param resultCode Result code, if any, from this Activity. |
| 2632 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2633 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2634 | * @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] | 2635 | */ |
| 2636 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2637 | // Refuse possible leaked file descriptors |
| 2638 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2639 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2640 | } |
| 2641 | |
| 2642 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2643 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2644 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2645 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2646 | if (next != null) { |
| 2647 | // ask watcher if this is allowed |
| 2648 | boolean resumeOK = true; |
| 2649 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2650 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2651 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2652 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | if (!resumeOK) { |
| 2656 | return false; |
| 2657 | } |
| 2658 | } |
| 2659 | } |
| 2660 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2661 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2662 | resultData, "app-request"); |
| 2663 | Binder.restoreCallingIdentity(origId); |
| 2664 | return res; |
| 2665 | } |
| 2666 | } |
| 2667 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2668 | public final void finishHeavyWeightApp() { |
| 2669 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2670 | != PackageManager.PERMISSION_GRANTED) { |
| 2671 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2672 | + Binder.getCallingPid() |
| 2673 | + ", uid=" + Binder.getCallingUid() |
| 2674 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2675 | Slog.w(TAG, msg); |
| 2676 | throw new SecurityException(msg); |
| 2677 | } |
| 2678 | |
| 2679 | synchronized(this) { |
| 2680 | if (mHeavyWeightProcess == null) { |
| 2681 | return; |
| 2682 | } |
| 2683 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2684 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2685 | mHeavyWeightProcess.activities); |
| 2686 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2687 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2688 | if (!r.finishing) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2689 | int index = mMainStack.indexOfTokenLocked(r.appToken); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2690 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2691 | mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED, |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2692 | null, "finish-heavy"); |
| 2693 | } |
| 2694 | } |
| 2695 | } |
| 2696 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2697 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 2698 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2699 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2700 | } |
| 2701 | } |
| 2702 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2703 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2704 | String message) { |
| 2705 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2706 | != PackageManager.PERMISSION_GRANTED) { |
| 2707 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2708 | + Binder.getCallingPid() |
| 2709 | + ", uid=" + Binder.getCallingUid() |
| 2710 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2711 | Slog.w(TAG, msg); |
| 2712 | throw new SecurityException(msg); |
| 2713 | } |
| 2714 | |
| 2715 | synchronized(this) { |
| 2716 | ProcessRecord proc = null; |
| 2717 | |
| 2718 | // Figure out which process to kill. We don't trust that initialPid |
| 2719 | // still has any relation to current pids, so must scan through the |
| 2720 | // list. |
| 2721 | synchronized (mPidsSelfLocked) { |
| 2722 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2723 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2724 | if (p.uid != uid) { |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2725 | continue; |
| 2726 | } |
| 2727 | if (p.pid == initialPid) { |
| 2728 | proc = p; |
| 2729 | break; |
| 2730 | } |
| 2731 | for (String str : p.pkgList) { |
| 2732 | if (str.equals(packageName)) { |
| 2733 | proc = p; |
| 2734 | } |
| 2735 | } |
| 2736 | } |
| 2737 | } |
| 2738 | |
| 2739 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2740 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2741 | + " initialPid=" + initialPid |
| 2742 | + " packageName=" + packageName); |
| 2743 | return; |
| 2744 | } |
| 2745 | |
| 2746 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2747 | if (proc.pid == Process.myPid()) { |
| 2748 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2749 | return; |
| 2750 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2751 | long ident = Binder.clearCallingIdentity(); |
| 2752 | try { |
| 2753 | proc.thread.scheduleCrash(message); |
| 2754 | } catch (RemoteException e) { |
| 2755 | } |
| 2756 | Binder.restoreCallingIdentity(ident); |
| 2757 | } |
| 2758 | } |
| 2759 | } |
| 2760 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2761 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2762 | int requestCode) { |
| 2763 | synchronized(this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2764 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2765 | mMainStack.finishSubActivityLocked(token, resultWho, requestCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2766 | Binder.restoreCallingIdentity(origId); |
| 2767 | } |
| 2768 | } |
| 2769 | |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2770 | public boolean finishActivityAffinity(IBinder token) { |
| 2771 | synchronized(this) { |
| 2772 | final long origId = Binder.clearCallingIdentity(); |
| 2773 | boolean res = mMainStack.finishActivityAffinityLocked(token); |
| 2774 | Binder.restoreCallingIdentity(origId); |
| 2775 | return res; |
| 2776 | } |
| 2777 | } |
| 2778 | |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2779 | public boolean willActivityBeVisible(IBinder token) { |
| 2780 | synchronized(this) { |
| 2781 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2782 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2783 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2784 | if (r.appToken == token) { |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2785 | return true; |
| 2786 | } |
| 2787 | if (r.fullscreen && !r.finishing) { |
| 2788 | return false; |
| 2789 | } |
| 2790 | } |
| 2791 | return true; |
| 2792 | } |
| 2793 | } |
| 2794 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2795 | public void overridePendingTransition(IBinder token, String packageName, |
| 2796 | int enterAnim, int exitAnim) { |
| 2797 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2798 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2799 | if (self == null) { |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2800 | return; |
| 2801 | } |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2802 | |
| 2803 | final long origId = Binder.clearCallingIdentity(); |
| 2804 | |
| 2805 | if (self.state == ActivityState.RESUMED |
| 2806 | || self.state == ActivityState.PAUSING) { |
| 2807 | mWindowManager.overridePendingAppTransition(packageName, |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 2808 | enterAnim, exitAnim, null); |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | Binder.restoreCallingIdentity(origId); |
| 2812 | } |
| 2813 | } |
| 2814 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2815 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2816 | * Main function for removing an existing process from the activity manager |
| 2817 | * as a result of that process going away. Clears out all connections |
| 2818 | * to the process. |
| 2819 | */ |
| 2820 | private final void handleAppDiedLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2821 | boolean restarting, boolean allowRestart) { |
| 2822 | cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2823 | if (!restarting) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2824 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2825 | } |
| 2826 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2827 | if (mProfileProc == app) { |
| 2828 | clearProfilerLocked(); |
| 2829 | } |
| 2830 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2831 | // Just in case... |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 2832 | if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) { |
| 2833 | if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity); |
| 2834 | mMainStack.mPausingActivity = null; |
| 2835 | } |
| 2836 | if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) { |
| 2837 | mMainStack.mLastPausedActivity = null; |
| 2838 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2839 | |
| 2840 | // Remove this application's activities from active lists. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2841 | mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2842 | |
| 2843 | boolean atTop = true; |
| 2844 | boolean hasVisibleActivities = false; |
| 2845 | |
| 2846 | // Clean out the history list. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2847 | int i = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2848 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2849 | TAG, "Removing app " + app + " from history with " + i + " entries"); |
| 2850 | while (i > 0) { |
| 2851 | i--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2852 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2853 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2854 | TAG, "Record #" + i + " " + r + ": app=" + r.app); |
| 2855 | if (r.app == app) { |
| 2856 | if ((!r.haveState && !r.stateNotNeeded) || r.finishing) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2857 | if (ActivityStack.DEBUG_ADD_REMOVE) { |
| 2858 | RuntimeException here = new RuntimeException("here"); |
| 2859 | here.fillInStackTrace(); |
| 2860 | Slog.i(TAG, "Removing activity " + r + " from stack at " + i |
| 2861 | + ": haveState=" + r.haveState |
| 2862 | + " stateNotNeeded=" + r.stateNotNeeded |
| 2863 | + " finishing=" + r.finishing |
| 2864 | + " state=" + r.state, here); |
| 2865 | } |
| 2866 | if (!r.finishing) { |
| 2867 | Slog.w(TAG, "Force removing " + r + ": app died, no saved state"); |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 2868 | EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY, |
| 2869 | System.identityHashCode(r), |
| 2870 | r.task.taskId, r.shortComponentName, |
| 2871 | "proc died without state saved"); |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2872 | } |
Dianne Hackborn | 5c60743 | 2012-02-28 14:44:19 -0800 | [diff] [blame] | 2873 | mMainStack.removeActivityFromHistoryLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2874 | |
| 2875 | } else { |
| 2876 | // We have the current state for this activity, so |
| 2877 | // it can be restarted later when needed. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2878 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2879 | TAG, "Keeping entry, setting app to null"); |
| 2880 | if (r.visible) { |
| 2881 | hasVisibleActivities = true; |
| 2882 | } |
| 2883 | r.app = null; |
| 2884 | r.nowVisible = false; |
| 2885 | if (!r.haveState) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2886 | if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG, |
| 2887 | "App died, clearing saved state of " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2888 | r.icicle = null; |
| 2889 | } |
| 2890 | } |
| 2891 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2892 | r.stack.cleanUpActivityLocked(r, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2893 | } |
| 2894 | atTop = false; |
| 2895 | } |
| 2896 | |
| 2897 | app.activities.clear(); |
| 2898 | |
| 2899 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2900 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2901 | + " running instrumentation " + app.instrumentationClass); |
| 2902 | Bundle info = new Bundle(); |
| 2903 | info.putString("shortMsg", "Process crashed."); |
| 2904 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 2905 | } |
| 2906 | |
| 2907 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2908 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2909 | // If there was nothing to resume, and we are not already |
| 2910 | // restarting this process, but there is a visible activity that |
| 2911 | // is hosted by the process... then make sure all visible |
| 2912 | // activities are running, taking care of restarting this |
| 2913 | // process. |
| 2914 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2915 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2916 | } |
| 2917 | } |
| 2918 | } |
| 2919 | } |
| 2920 | |
| 2921 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 2922 | IBinder threadBinder = thread.asBinder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2923 | // Find the application record. |
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); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2926 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 2927 | return i; |
| 2928 | } |
| 2929 | } |
| 2930 | return -1; |
| 2931 | } |
| 2932 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2933 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2934 | IApplicationThread thread) { |
| 2935 | if (thread == null) { |
| 2936 | return null; |
| 2937 | } |
| 2938 | |
| 2939 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2940 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2941 | } |
| 2942 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2943 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2944 | IApplicationThread thread) { |
| 2945 | |
| 2946 | mProcDeaths[0]++; |
| 2947 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 2948 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 2949 | synchronized (stats) { |
| 2950 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 2951 | } |
| 2952 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 2953 | // Clean up already done if the process has been re-started. |
| 2954 | if (app.pid == pid && app.thread != null && |
| 2955 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 2956 | if (!app.killedBackground) { |
| 2957 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 2958 | + ") has died."); |
| 2959 | } |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 2960 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2961 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2962 | TAG, "Dying app: " + app + ", pid: " + pid |
| 2963 | + ", thread: " + thread.asBinder()); |
| 2964 | boolean doLowMem = app.instrumentationClass == null; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2965 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2966 | |
| 2967 | if (doLowMem) { |
| 2968 | // If there are no longer any background processes running, |
| 2969 | // and the app that died was not running instrumentation, |
| 2970 | // then tell everyone we are now low on memory. |
| 2971 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2972 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2973 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2974 | 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] | 2975 | haveBg = true; |
| 2976 | break; |
| 2977 | } |
| 2978 | } |
| 2979 | |
| 2980 | if (!haveBg) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2981 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2982 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2983 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 2984 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 2985 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2986 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 2987 | // The low memory report is overriding any current |
| 2988 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 2989 | // heavy/important/visible/foreground processes first. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 2990 | if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2991 | rec.lastRequestedGc = 0; |
| 2992 | } else { |
| 2993 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2994 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 2995 | rec.reportLowMemory = true; |
| 2996 | rec.lastLowMemory = now; |
| 2997 | mProcessesToGc.remove(rec); |
| 2998 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2999 | } |
| 3000 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 3001 | mHandler.sendEmptyMessage(REPORT_MEM_USAGE); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3002 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3003 | } |
| 3004 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3005 | } else if (app.pid != pid) { |
| 3006 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3007 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3008 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | 28a8c2b | 2010-03-01 11:30:02 -0800 | [diff] [blame] | 3009 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3010 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3011 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3012 | + thread.asBinder()); |
| 3013 | } |
| 3014 | } |
| 3015 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3016 | /** |
| 3017 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 3018 | * @param clearTraces causes the dump file to be erased prior to the new |
| 3019 | * traces being written, if true; when false, the new traces will be |
| 3020 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3021 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 3022 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3023 | * @param nativeProcs optional list of native process names to dump stack crawls |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3024 | * @return file containing stack traces, or null if no dump file is configured |
| 3025 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3026 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3027 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3028 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3029 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3030 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3031 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3032 | |
| 3033 | File tracesFile = new File(tracesPath); |
| 3034 | try { |
| 3035 | File tracesDir = tracesFile.getParentFile(); |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 3036 | if (!tracesDir.exists()) { |
| 3037 | tracesFile.mkdirs(); |
| 3038 | if (!SELinux.restorecon(tracesDir)) { |
| 3039 | return null; |
| 3040 | } |
| 3041 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3042 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3043 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 3044 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3045 | tracesFile.createNewFile(); |
| 3046 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3047 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3048 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3049 | return null; |
| 3050 | } |
| 3051 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3052 | dumpStackTraces(tracesPath, firstPids, processStats, lastPids, nativeProcs); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3053 | return tracesFile; |
| 3054 | } |
| 3055 | |
| 3056 | private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3057 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3058 | // Use a FileObserver to detect when traces finish writing. |
| 3059 | // The order of traces is considered important to maintain for legibility. |
| 3060 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 3061 | public synchronized void onEvent(int event, String path) { notify(); } |
| 3062 | }; |
| 3063 | |
| 3064 | try { |
| 3065 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3066 | |
| 3067 | // First collect all of the stacks of the most important pids. |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3068 | if (firstPids != null) { |
| 3069 | try { |
| 3070 | int num = firstPids.size(); |
| 3071 | for (int i = 0; i < num; i++) { |
| 3072 | synchronized (observer) { |
| 3073 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 3074 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3075 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3076 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3077 | } catch (InterruptedException e) { |
| 3078 | Log.wtf(TAG, e); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3079 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | // Next measure CPU usage. |
| 3083 | if (processStats != null) { |
| 3084 | processStats.init(); |
| 3085 | System.gc(); |
| 3086 | processStats.update(); |
| 3087 | try { |
| 3088 | synchronized (processStats) { |
| 3089 | processStats.wait(500); // measure over 1/2 second. |
| 3090 | } |
| 3091 | } catch (InterruptedException e) { |
| 3092 | } |
| 3093 | processStats.update(); |
| 3094 | |
| 3095 | // We'll take the stack crawls of just the top apps using CPU. |
| 3096 | final int N = processStats.countWorkingStats(); |
| 3097 | int numProcs = 0; |
| 3098 | for (int i=0; i<N && numProcs<5; i++) { |
| 3099 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 3100 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 3101 | numProcs++; |
| 3102 | try { |
| 3103 | synchronized (observer) { |
| 3104 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 3105 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3106 | } |
| 3107 | } catch (InterruptedException e) { |
| 3108 | Log.wtf(TAG, e); |
| 3109 | } |
| 3110 | |
| 3111 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3112 | } |
| 3113 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3114 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3115 | } finally { |
| 3116 | observer.stopWatching(); |
| 3117 | } |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3118 | |
| 3119 | if (nativeProcs != null) { |
| 3120 | int[] pids = Process.getPidsForCommands(nativeProcs); |
| 3121 | if (pids != null) { |
| 3122 | for (int pid : pids) { |
| 3123 | Debug.dumpNativeBacktraceToFile(pid, tracesPath); |
| 3124 | } |
| 3125 | } |
| 3126 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3127 | } |
| 3128 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3129 | final void logAppTooSlow(ProcessRecord app, long startTime, String msg) { |
Dianne Hackborn | 69dc66e | 2012-03-26 10:50:54 -0700 | [diff] [blame] | 3130 | if (true || IS_USER_BUILD) { |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3131 | return; |
| 3132 | } |
| 3133 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3134 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3135 | return; |
| 3136 | } |
| 3137 | |
| 3138 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 3139 | StrictMode.allowThreadDiskWrites(); |
| 3140 | try { |
| 3141 | final File tracesFile = new File(tracesPath); |
| 3142 | final File tracesDir = tracesFile.getParentFile(); |
| 3143 | final File tracesTmp = new File(tracesDir, "__tmp__"); |
| 3144 | try { |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 3145 | if (!tracesDir.exists()) { |
| 3146 | tracesFile.mkdirs(); |
| 3147 | if (!SELinux.restorecon(tracesDir.getPath())) { |
| 3148 | return; |
| 3149 | } |
| 3150 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3151 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3152 | |
| 3153 | if (tracesFile.exists()) { |
| 3154 | tracesTmp.delete(); |
| 3155 | tracesFile.renameTo(tracesTmp); |
| 3156 | } |
| 3157 | StringBuilder sb = new StringBuilder(); |
| 3158 | Time tobj = new Time(); |
| 3159 | tobj.set(System.currentTimeMillis()); |
| 3160 | sb.append(tobj.format("%Y-%m-%d %H:%M:%S")); |
| 3161 | sb.append(": "); |
| 3162 | TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb); |
| 3163 | sb.append(" since "); |
| 3164 | sb.append(msg); |
| 3165 | FileOutputStream fos = new FileOutputStream(tracesFile); |
| 3166 | fos.write(sb.toString().getBytes()); |
| 3167 | if (app == null) { |
| 3168 | fos.write("\n*** No application process!".getBytes()); |
| 3169 | } |
| 3170 | fos.close(); |
| 3171 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3172 | } catch (IOException e) { |
| 3173 | Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesPath, e); |
| 3174 | return; |
| 3175 | } |
| 3176 | |
| 3177 | if (app != null) { |
| 3178 | ArrayList<Integer> firstPids = new ArrayList<Integer>(); |
| 3179 | firstPids.add(app.pid); |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3180 | dumpStackTraces(tracesPath, firstPids, null, null, null); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | File lastTracesFile = null; |
| 3184 | File curTracesFile = null; |
| 3185 | for (int i=9; i>=0; i--) { |
| 3186 | String name = String.format("slow%02d.txt", i); |
| 3187 | curTracesFile = new File(tracesDir, name); |
| 3188 | if (curTracesFile.exists()) { |
| 3189 | if (lastTracesFile != null) { |
| 3190 | curTracesFile.renameTo(lastTracesFile); |
| 3191 | } else { |
| 3192 | curTracesFile.delete(); |
| 3193 | } |
| 3194 | } |
| 3195 | lastTracesFile = curTracesFile; |
| 3196 | } |
| 3197 | tracesFile.renameTo(curTracesFile); |
| 3198 | if (tracesTmp.exists()) { |
| 3199 | tracesTmp.renameTo(tracesFile); |
| 3200 | } |
| 3201 | } finally { |
| 3202 | StrictMode.setThreadPolicy(oldPolicy); |
| 3203 | } |
| 3204 | } |
| 3205 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3206 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
| 3207 | ActivityRecord parent, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3208 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 3209 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 3210 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3211 | if (mController != null) { |
| 3212 | try { |
| 3213 | // 0 == continue, -1 = kill process immediately |
| 3214 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 3215 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3216 | } catch (RemoteException e) { |
| 3217 | mController = null; |
| 3218 | } |
| 3219 | } |
| 3220 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3221 | long anrTime = SystemClock.uptimeMillis(); |
| 3222 | if (MONITOR_CPU_USAGE) { |
| 3223 | updateCpuStatsNow(); |
| 3224 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3225 | |
| 3226 | synchronized (this) { |
| 3227 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 3228 | if (mShuttingDown) { |
| 3229 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 3230 | return; |
| 3231 | } else if (app.notResponding) { |
| 3232 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 3233 | return; |
| 3234 | } else if (app.crashing) { |
| 3235 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 3236 | return; |
| 3237 | } |
| 3238 | |
| 3239 | // In case we come through here for the same app before completing |
| 3240 | // this one, mark as anring now so we will bail out. |
| 3241 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3242 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3243 | // Log the ANR to the event log. |
| 3244 | EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags, |
| 3245 | annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3246 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3247 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3248 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3249 | |
| 3250 | int parentPid = app.pid; |
| 3251 | 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] | 3252 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3253 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3254 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3255 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3256 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 3257 | ProcessRecord r = mLruProcesses.get(i); |
| 3258 | if (r != null && r.thread != null) { |
| 3259 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3260 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 3261 | if (r.persistent) { |
| 3262 | firstPids.add(pid); |
| 3263 | } else { |
| 3264 | lastPids.put(pid, Boolean.TRUE); |
| 3265 | } |
| 3266 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3267 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3268 | } |
| 3269 | } |
| 3270 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3271 | // Log the ANR to the main log. |
Jeff Brown | deb6ed8 | 2012-04-10 14:26:26 -0700 | [diff] [blame] | 3272 | StringBuilder info = new StringBuilder(); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3273 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3274 | info.append("ANR in ").append(app.processName); |
| 3275 | if (activity != null && activity.shortComponentName != null) { |
| 3276 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 3277 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3278 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3279 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3280 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3281 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3282 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3283 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3284 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3285 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3286 | final ProcessStats processStats = new ProcessStats(true); |
| 3287 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3288 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3289 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3290 | String cpuInfo = null; |
| 3291 | if (MONITOR_CPU_USAGE) { |
| 3292 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3293 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3294 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3295 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3296 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3297 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3298 | } |
| 3299 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3300 | info.append(processStats.printCurrentState(anrTime)); |
| 3301 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3302 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3303 | if (tracesFile == null) { |
| 3304 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 3305 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 3306 | } |
| 3307 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 3308 | addErrorToDropBox("anr", app, app.processName, activity, parent, annotation, |
| 3309 | cpuInfo, tracesFile, null); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3310 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3311 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3312 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3313 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 3314 | 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] | 3315 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3316 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3317 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3318 | } |
| 3319 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3320 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3321 | } |
| 3322 | } |
| 3323 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3324 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 3325 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 3326 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3327 | |
| 3328 | synchronized (this) { |
| 3329 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3330 | Slog.w(TAG, "Killing " + app + ": background ANR"); |
| 3331 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 3332 | app.processName, app.setAdj, "background ANR"); |
| 3333 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3334 | return; |
| 3335 | } |
| 3336 | |
| 3337 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 3338 | makeAppNotRespondingLocked(app, |
| 3339 | activity != null ? activity.shortComponentName : null, |
| 3340 | annotation != null ? "ANR " + annotation : "ANR", |
| 3341 | info.toString()); |
| 3342 | |
| 3343 | // Bring up the infamous App Not Responding dialog |
| 3344 | Message msg = Message.obtain(); |
| 3345 | HashMap map = new HashMap(); |
| 3346 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3347 | msg.obj = map; |
| 3348 | map.put("app", app); |
| 3349 | if (activity != null) { |
| 3350 | map.put("activity", activity); |
| 3351 | } |
| 3352 | |
| 3353 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3354 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3355 | } |
| 3356 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3357 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3358 | if (!mLaunchWarningShown) { |
| 3359 | mLaunchWarningShown = true; |
| 3360 | mHandler.post(new Runnable() { |
| 3361 | @Override |
| 3362 | public void run() { |
| 3363 | synchronized (ActivityManagerService.this) { |
| 3364 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3365 | d.show(); |
| 3366 | mHandler.postDelayed(new Runnable() { |
| 3367 | @Override |
| 3368 | public void run() { |
| 3369 | synchronized (ActivityManagerService.this) { |
| 3370 | d.dismiss(); |
| 3371 | mLaunchWarningShown = false; |
| 3372 | } |
| 3373 | } |
| 3374 | }, 4000); |
| 3375 | } |
| 3376 | } |
| 3377 | }); |
| 3378 | } |
| 3379 | } |
| 3380 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3381 | public boolean clearApplicationUserData(final String packageName, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3382 | final IPackageDataObserver observer, final int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3383 | enforceNotIsolatedCaller("clearApplicationUserData"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3384 | int uid = Binder.getCallingUid(); |
| 3385 | int pid = Binder.getCallingPid(); |
| 3386 | long callingId = Binder.clearCallingIdentity(); |
| 3387 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3388 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3389 | int pkgUid = -1; |
| 3390 | synchronized(this) { |
| 3391 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3392 | pkgUid = pm.getPackageUid(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3393 | } catch (RemoteException e) { |
| 3394 | } |
| 3395 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3396 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3397 | return false; |
| 3398 | } |
| 3399 | if (uid == pkgUid || checkComponentPermission( |
| 3400 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3401 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3402 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3403 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3404 | } else { |
| 3405 | throw new SecurityException(pid+" does not have permission:"+ |
| 3406 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3407 | "for process:"+packageName); |
| 3408 | } |
| 3409 | } |
| 3410 | |
| 3411 | try { |
| 3412 | //clear application user data |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3413 | pm.clearApplicationUserData(packageName, observer, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3414 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3415 | Uri.fromParts("package", packageName, null)); |
| 3416 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3417 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3418 | null, null, 0, null, null, null, false, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3419 | } catch (RemoteException e) { |
| 3420 | } |
| 3421 | } finally { |
| 3422 | Binder.restoreCallingIdentity(callingId); |
| 3423 | } |
| 3424 | return true; |
| 3425 | } |
| 3426 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3427 | public void killBackgroundProcesses(final String packageName) { |
| 3428 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3429 | != PackageManager.PERMISSION_GRANTED && |
| 3430 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3431 | != PackageManager.PERMISSION_GRANTED) { |
| 3432 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3433 | + Binder.getCallingPid() |
| 3434 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3435 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3436 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3437 | throw new SecurityException(msg); |
| 3438 | } |
| 3439 | |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3440 | int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3441 | long callingId = Binder.clearCallingIdentity(); |
| 3442 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3443 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3444 | int pkgUid = -1; |
| 3445 | synchronized(this) { |
| 3446 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3447 | pkgUid = pm.getPackageUid(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3448 | } catch (RemoteException e) { |
| 3449 | } |
| 3450 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3451 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3452 | return; |
| 3453 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3454 | killPackageProcessesLocked(packageName, pkgUid, -1, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3455 | ProcessList.SERVICE_ADJ, false, true, true, false, "kill background"); |
| 3456 | } |
| 3457 | } finally { |
| 3458 | Binder.restoreCallingIdentity(callingId); |
| 3459 | } |
| 3460 | } |
| 3461 | |
| 3462 | public void killAllBackgroundProcesses() { |
| 3463 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3464 | != PackageManager.PERMISSION_GRANTED) { |
| 3465 | String msg = "Permission Denial: killAllBackgroundProcesses() from pid=" |
| 3466 | + Binder.getCallingPid() |
| 3467 | + ", uid=" + Binder.getCallingUid() |
| 3468 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
| 3469 | Slog.w(TAG, msg); |
| 3470 | throw new SecurityException(msg); |
| 3471 | } |
| 3472 | |
| 3473 | long callingId = Binder.clearCallingIdentity(); |
| 3474 | try { |
| 3475 | synchronized(this) { |
| 3476 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 3477 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3478 | final int NA = apps.size(); |
| 3479 | for (int ia=0; ia<NA; ia++) { |
| 3480 | ProcessRecord app = apps.valueAt(ia); |
| 3481 | if (app.persistent) { |
| 3482 | // we don't kill persistent processes |
| 3483 | continue; |
| 3484 | } |
| 3485 | if (app.removed) { |
| 3486 | procs.add(app); |
| 3487 | } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 3488 | app.removed = true; |
| 3489 | procs.add(app); |
| 3490 | } |
| 3491 | } |
| 3492 | } |
| 3493 | |
| 3494 | int N = procs.size(); |
| 3495 | for (int i=0; i<N; i++) { |
| 3496 | removeProcessLocked(procs.get(i), false, true, "kill all background"); |
| 3497 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3498 | } |
| 3499 | } finally { |
| 3500 | Binder.restoreCallingIdentity(callingId); |
| 3501 | } |
| 3502 | } |
| 3503 | |
| 3504 | public void forceStopPackage(final String packageName) { |
| 3505 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3506 | != PackageManager.PERMISSION_GRANTED) { |
| 3507 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3508 | + Binder.getCallingPid() |
| 3509 | + ", uid=" + Binder.getCallingUid() |
| 3510 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3511 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3512 | throw new SecurityException(msg); |
| 3513 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3514 | final int userId = UserHandle.getCallingUserId(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3515 | long callingId = Binder.clearCallingIdentity(); |
| 3516 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3517 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3518 | int pkgUid = -1; |
| 3519 | synchronized(this) { |
| 3520 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3521 | pkgUid = pm.getPackageUid(packageName, userId); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3522 | } catch (RemoteException e) { |
| 3523 | } |
| 3524 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3525 | Slog.w(TAG, "Invalid packageName: " + packageName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3526 | return; |
| 3527 | } |
| 3528 | forceStopPackageLocked(packageName, pkgUid); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3529 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3530 | pm.setPackageStoppedState(packageName, true, userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3531 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 3532 | } catch (IllegalArgumentException e) { |
| 3533 | Slog.w(TAG, "Failed trying to unstop package " |
| 3534 | + packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3535 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3536 | } |
| 3537 | } finally { |
| 3538 | Binder.restoreCallingIdentity(callingId); |
| 3539 | } |
| 3540 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3541 | |
| 3542 | /* |
| 3543 | * The pkg name and uid have to be specified. |
| 3544 | * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int) |
| 3545 | */ |
| 3546 | public void killApplicationWithUid(String pkg, int uid) { |
| 3547 | if (pkg == null) { |
| 3548 | return; |
| 3549 | } |
| 3550 | // Make sure the uid is valid. |
| 3551 | if (uid < 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3552 | Slog.w(TAG, "Invalid uid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3553 | return; |
| 3554 | } |
| 3555 | int callerUid = Binder.getCallingUid(); |
| 3556 | // Only the system server can kill an application |
| 3557 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3558 | // Post an aysnc message to kill the application |
| 3559 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
| 3560 | msg.arg1 = uid; |
| 3561 | msg.arg2 = 0; |
| 3562 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3563 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3564 | } else { |
| 3565 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3566 | pkg); |
| 3567 | } |
| 3568 | } |
| 3569 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3570 | public void closeSystemDialogs(String reason) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3571 | enforceNotIsolatedCaller("closeSystemDialogs"); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3572 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame^] | 3573 | final int pid = Binder.getCallingPid(); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3574 | final int uid = Binder.getCallingUid(); |
| 3575 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame^] | 3576 | try { |
| 3577 | synchronized (this) { |
| 3578 | // Only allow this from foreground processes, so that background |
| 3579 | // applications can't abuse it to prevent system UI from being shown. |
| 3580 | if (uid >= Process.FIRST_APPLICATION_UID) { |
| 3581 | ProcessRecord proc; |
| 3582 | synchronized (mPidsSelfLocked) { |
| 3583 | proc = mPidsSelfLocked.get(pid); |
| 3584 | } |
| 3585 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 3586 | Slog.w(TAG, "Ignoring closeSystemDialogs " + reason |
| 3587 | + " from background process " + proc); |
| 3588 | return; |
| 3589 | } |
| 3590 | } |
| 3591 | closeSystemDialogsLocked(reason); |
| 3592 | } |
| 3593 | } finally { |
| 3594 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3595 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3596 | } |
| 3597 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame^] | 3598 | void closeSystemDialogsLocked(String reason) { |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3599 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3600 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3601 | if (reason != null) { |
| 3602 | intent.putExtra("reason", reason); |
| 3603 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3604 | mWindowManager.closeSystemDialogs(reason); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3605 | |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3606 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 3607 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 3608 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3609 | r.stack.finishActivityLocked(r, i, |
| 3610 | Activity.RESULT_CANCELED, null, "close-sys"); |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3611 | } |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3612 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3613 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame^] | 3614 | broadcastIntentLocked(null, null, intent, null, |
| 3615 | null, 0, null, null, null, false, false, -1, |
| 3616 | Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3617 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3618 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3619 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3620 | throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3621 | enforceNotIsolatedCaller("getProcessMemoryInfo"); |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3622 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3623 | for (int i=pids.length-1; i>=0; i--) { |
| 3624 | infos[i] = new Debug.MemoryInfo(); |
| 3625 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3626 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3627 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3628 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3629 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3630 | public long[] getProcessPss(int[] pids) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3631 | enforceNotIsolatedCaller("getProcessPss"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3632 | long[] pss = new long[pids.length]; |
| 3633 | for (int i=pids.length-1; i>=0; i--) { |
| 3634 | pss[i] = Debug.getPss(pids[i]); |
| 3635 | } |
| 3636 | return pss; |
| 3637 | } |
| 3638 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3639 | public void killApplicationProcess(String processName, int uid) { |
| 3640 | if (processName == null) { |
| 3641 | return; |
| 3642 | } |
| 3643 | |
| 3644 | int callerUid = Binder.getCallingUid(); |
| 3645 | // Only the system server can kill an application |
| 3646 | if (callerUid == Process.SYSTEM_UID) { |
| 3647 | synchronized (this) { |
| 3648 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3649 | if (app != null && app.thread != null) { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3650 | try { |
| 3651 | app.thread.scheduleSuicide(); |
| 3652 | } catch (RemoteException e) { |
| 3653 | // If the other end already died, then our work here is done. |
| 3654 | } |
| 3655 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3656 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3657 | + processName + " / " + uid); |
| 3658 | } |
| 3659 | } |
| 3660 | } else { |
| 3661 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3662 | processName); |
| 3663 | } |
| 3664 | } |
| 3665 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3666 | private void forceStopPackageLocked(final String packageName, int uid) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3667 | forceStopPackageLocked(packageName, uid, false, false, true, false, |
| 3668 | UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3669 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3670 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3671 | if (!mProcessesReady) { |
| 3672 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3673 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3674 | intent.putExtra(Intent.EXTRA_UID, uid); |
| 3675 | broadcastIntentLocked(null, null, intent, |
| 3676 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3677 | false, false, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3678 | MY_PID, Process.SYSTEM_UID, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3679 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3680 | |
| 3681 | private void forceStopUserLocked(int userId) { |
| 3682 | forceStopPackageLocked(null, -1, false, false, true, false, userId); |
| 3683 | Intent intent = new Intent(Intent.ACTION_USER_STOPPED); |
| 3684 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 3685 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 3686 | broadcastIntentLocked(null, null, intent, |
| 3687 | null, null, 0, null, null, null, |
| 3688 | false, false, |
| 3689 | MY_PID, Process.SYSTEM_UID, userId); |
| 3690 | } |
| 3691 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3692 | private final boolean killPackageProcessesLocked(String packageName, int uid, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3693 | int userId, int minOomAdj, boolean callerWillRestart, boolean allowRestart, |
| 3694 | boolean doit, boolean evenPersistent, String reason) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3695 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3696 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3697 | // Remove all processes this package may have touched: all with the |
| 3698 | // same UID (except for the system or root user), and all whose name |
| 3699 | // matches the package name. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3700 | final String procNamePrefix = packageName != null ? (packageName + ":") : null; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3701 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3702 | final int NA = apps.size(); |
| 3703 | for (int ia=0; ia<NA; ia++) { |
| 3704 | ProcessRecord app = apps.valueAt(ia); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3705 | if (app.persistent && !evenPersistent) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3706 | // we don't kill persistent processes |
| 3707 | continue; |
| 3708 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3709 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3710 | if (doit) { |
| 3711 | procs.add(app); |
| 3712 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3713 | // If no package is specified, we call all processes under the |
| 3714 | // give user id. |
| 3715 | } else if (packageName == null) { |
| 3716 | if (app.userId == userId) { |
| 3717 | if (app.setAdj >= minOomAdj) { |
| 3718 | if (!doit) { |
| 3719 | return true; |
| 3720 | } |
| 3721 | app.removed = true; |
| 3722 | procs.add(app); |
| 3723 | } |
| 3724 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3725 | // If uid is specified and the uid and process name match |
| 3726 | // Or, the uid is not specified and the process name matches |
| 3727 | } else if (((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid) |
Amith Yamasani | 34db3d6 | 2012-04-02 16:35:19 -0700 | [diff] [blame] | 3728 | || ((app.processName.equals(packageName) |
| 3729 | || app.processName.startsWith(procNamePrefix)) |
| 3730 | && uid < 0))) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3731 | if (app.setAdj >= minOomAdj) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3732 | if (!doit) { |
| 3733 | return true; |
| 3734 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3735 | app.removed = true; |
| 3736 | procs.add(app); |
| 3737 | } |
| 3738 | } |
| 3739 | } |
| 3740 | } |
| 3741 | |
| 3742 | int N = procs.size(); |
| 3743 | for (int i=0; i<N; i++) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3744 | removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3745 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3746 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3747 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3748 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3749 | private final boolean forceStopPackageLocked(String name, int uid, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3750 | boolean callerWillRestart, boolean purgeCache, boolean doit, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3751 | boolean evenPersistent, int userId) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3752 | int i; |
| 3753 | int N; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3754 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3755 | if (uid < 0 && name != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3756 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3757 | uid = AppGlobals.getPackageManager().getPackageUid(name, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3758 | } catch (RemoteException e) { |
| 3759 | } |
| 3760 | } |
| 3761 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3762 | if (doit) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3763 | if (name != null) { |
| 3764 | Slog.i(TAG, "Force stopping package " + name + " uid=" + uid); |
| 3765 | } else { |
| 3766 | Slog.i(TAG, "Force stopping user " + userId); |
| 3767 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3768 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3769 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3770 | while (badApps.hasNext()) { |
| 3771 | SparseArray<Long> ba = badApps.next(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3772 | for (i=ba.size()-1; i>=0; i--) { |
| 3773 | boolean remove = false; |
| 3774 | final int entUid = ba.keyAt(i); |
| 3775 | if (name != null) { |
| 3776 | if (entUid == uid) { |
| 3777 | remove = true; |
| 3778 | } |
| 3779 | } else if (UserHandle.getUserId(entUid) == userId) { |
| 3780 | remove = true; |
| 3781 | } |
| 3782 | if (remove) { |
| 3783 | ba.removeAt(i); |
| 3784 | } |
| 3785 | } |
| 3786 | if (ba.size() == 0) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3787 | badApps.remove(); |
| 3788 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3789 | } |
| 3790 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3791 | |
| 3792 | boolean didSomething = killPackageProcessesLocked(name, uid, |
| 3793 | name == null ? userId : -1 , -100, callerWillRestart, false, |
| 3794 | doit, evenPersistent, |
| 3795 | name == null ? ("force stop user " + userId) : ("force stop " + name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3796 | |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3797 | TaskRecord lastTask = null; |
| 3798 | for (i=0; i<mMainStack.mHistory.size(); i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3799 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3800 | final boolean samePackage = r.packageName.equals(name) |
| 3801 | || (name == null && r.userId == userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3802 | if (r.userId == userId |
| 3803 | && (samePackage || r.task == lastTask) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3804 | && (r.app == null || evenPersistent || !r.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3805 | if (!doit) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 3806 | if (r.finishing) { |
| 3807 | // If this activity is just finishing, then it is not |
| 3808 | // interesting as far as something to stop. |
| 3809 | continue; |
| 3810 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3811 | return true; |
| 3812 | } |
| 3813 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3814 | Slog.i(TAG, " Force finishing activity " + r); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3815 | if (samePackage) { |
| 3816 | if (r.app != null) { |
| 3817 | r.app.removed = true; |
| 3818 | } |
| 3819 | r.app = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3820 | } |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3821 | lastTask = r.task; |
| 3822 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 3823 | null, "force-stop", true)) { |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3824 | i--; |
| 3825 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3826 | } |
| 3827 | } |
| 3828 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3829 | if (mServices.forceStopLocked(name, userId, evenPersistent, doit)) { |
| 3830 | if (!doit) { |
| 3831 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3832 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3833 | didSomething = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3834 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3835 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3836 | if (name == null) { |
| 3837 | // Remove all sticky broadcasts from this user. |
| 3838 | mStickyBroadcasts.remove(userId); |
| 3839 | } |
| 3840 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3841 | ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3842 | for (ContentProviderRecord provider : mProviderMap.getProvidersByClass(userId).values()) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3843 | if ((name == null || provider.info.packageName.equals(name)) |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3844 | && (provider.proc == null || evenPersistent || !provider.proc.persistent)) { |
| 3845 | if (!doit) { |
| 3846 | return true; |
| 3847 | } |
| 3848 | didSomething = true; |
| 3849 | providers.add(provider); |
| 3850 | } |
| 3851 | } |
| 3852 | |
| 3853 | N = providers.size(); |
| 3854 | for (i=0; i<N; i++) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 3855 | removeDyingProviderLocked(null, providers.get(i), true); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3856 | } |
| 3857 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3858 | if (doit) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3859 | if (purgeCache && name != null) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3860 | AttributeCache ac = AttributeCache.instance(); |
| 3861 | if (ac != null) { |
| 3862 | ac.removePackage(name); |
| 3863 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3864 | } |
Dianne Hackborn | 38cc896 | 2011-10-13 11:33:55 -0700 | [diff] [blame] | 3865 | if (mBooted) { |
| 3866 | mMainStack.resumeTopActivityLocked(null); |
| 3867 | mMainStack.scheduleIdleLocked(); |
| 3868 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3869 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3870 | |
| 3871 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3872 | } |
| 3873 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3874 | private final boolean removeProcessLocked(ProcessRecord app, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3875 | boolean callerWillRestart, boolean allowRestart, String reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3876 | final String name = app.processName; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3877 | final int uid = app.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3878 | if (DEBUG_PROCESSES) Slog.d( |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3879 | TAG, "Force removing proc " + app.toShortString() + " (" + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3880 | + "/" + uid + ")"); |
| 3881 | |
| 3882 | mProcessNames.remove(name, uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3883 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3884 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3885 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 3886 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3887 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3888 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3889 | boolean needRestart = false; |
| 3890 | if (app.pid > 0 && app.pid != MY_PID) { |
| 3891 | int pid = app.pid; |
| 3892 | synchronized (mPidsSelfLocked) { |
| 3893 | mPidsSelfLocked.remove(pid); |
| 3894 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 3895 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3896 | Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3897 | handleAppDiedLocked(app, true, allowRestart); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3898 | mLruProcesses.remove(app); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3899 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3900 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3901 | if (app.persistent && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3902 | if (!callerWillRestart) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3903 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3904 | } else { |
| 3905 | needRestart = true; |
| 3906 | } |
| 3907 | } |
| 3908 | } else { |
| 3909 | mRemovedProcesses.add(app); |
| 3910 | } |
| 3911 | |
| 3912 | return needRestart; |
| 3913 | } |
| 3914 | |
| 3915 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 3916 | final int pid = app.pid; |
| 3917 | boolean gone = false; |
| 3918 | synchronized (mPidsSelfLocked) { |
| 3919 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 3920 | if (knownApp != null && knownApp.thread == null) { |
| 3921 | mPidsSelfLocked.remove(pid); |
| 3922 | gone = true; |
| 3923 | } |
| 3924 | } |
| 3925 | |
| 3926 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3927 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3928 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.uid, |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3929 | app.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 3930 | mProcessNames.remove(app.processName, app.uid); |
| 3931 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3932 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3933 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 3934 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3935 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 3936 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3937 | // Take care of any launching providers waiting for this process. |
| 3938 | checkAppInLaunchingProvidersLocked(app, true); |
| 3939 | // Take care of any services that are waiting for the process. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3940 | mServices.processStartTimedOutLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3941 | EventLog.writeEvent(EventLogTags.AM_KILL, pid, |
| 3942 | app.processName, app.setAdj, "start timeout"); |
| 3943 | Process.killProcessQuiet(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3944 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3945 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 3946 | try { |
| 3947 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 3948 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 3949 | bm.agentDisconnected(app.info.packageName); |
| 3950 | } catch (RemoteException e) { |
| 3951 | // Can't happen; the backup manager is local |
| 3952 | } |
| 3953 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 3954 | if (isPendingBroadcastProcessLocked(pid)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3955 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 3956 | skipPendingBroadcastLocked(pid); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 3957 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3958 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3959 | 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] | 3960 | } |
| 3961 | } |
| 3962 | |
| 3963 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 3964 | int pid) { |
| 3965 | |
| 3966 | // Find the application record that is being attached... either via |
| 3967 | // the pid if we are running in multiple processes, or just pull the |
| 3968 | // next app record if we are emulating process with anonymous threads. |
| 3969 | ProcessRecord app; |
| 3970 | if (pid != MY_PID && pid >= 0) { |
| 3971 | synchronized (mPidsSelfLocked) { |
| 3972 | app = mPidsSelfLocked.get(pid); |
| 3973 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3974 | } else { |
| 3975 | app = null; |
| 3976 | } |
| 3977 | |
| 3978 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3979 | 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] | 3980 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 3981 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3982 | if (pid > 0 && pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3983 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3984 | } else { |
| 3985 | try { |
| 3986 | thread.scheduleExit(); |
| 3987 | } catch (Exception e) { |
| 3988 | // Ignore exceptions. |
| 3989 | } |
| 3990 | } |
| 3991 | return false; |
| 3992 | } |
| 3993 | |
| 3994 | // If this application record is still attached to a previous |
| 3995 | // process, clean it up now. |
| 3996 | if (app.thread != null) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3997 | handleAppDiedLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3998 | } |
| 3999 | |
| 4000 | // Tell the process all about itself. |
| 4001 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4002 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4003 | TAG, "Binding process pid " + pid + " to record " + app); |
| 4004 | |
| 4005 | String processName = app.processName; |
| 4006 | try { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 4007 | AppDeathRecipient adr = new AppDeathRecipient( |
| 4008 | app, pid, thread); |
| 4009 | thread.asBinder().linkToDeath(adr, 0); |
| 4010 | app.deathRecipient = adr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4011 | } catch (RemoteException e) { |
| 4012 | app.resetPackageList(); |
| 4013 | startProcessLocked(app, "link fail", processName); |
| 4014 | return false; |
| 4015 | } |
| 4016 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4017 | 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] | 4018 | |
| 4019 | app.thread = thread; |
| 4020 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 4021 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 4022 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4023 | app.forcingToForeground = null; |
| 4024 | app.foregroundServices = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 4025 | app.hasShownUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4026 | app.debugging = false; |
| 4027 | |
| 4028 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 4029 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4030 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4031 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4032 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4033 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4034 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4035 | } |
| 4036 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4037 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4038 | TAG, "New app record " + app |
| 4039 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 4040 | try { |
| 4041 | int testMode = IApplicationThread.DEBUG_OFF; |
| 4042 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 4043 | testMode = mWaitForDebugger |
| 4044 | ? IApplicationThread.DEBUG_WAIT |
| 4045 | : IApplicationThread.DEBUG_ON; |
| 4046 | app.debugging = true; |
| 4047 | if (mDebugTransient) { |
| 4048 | mDebugApp = mOrigDebugApp; |
| 4049 | mWaitForDebugger = mOrigWaitForDebugger; |
| 4050 | } |
| 4051 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4052 | String profileFile = app.instrumentationProfileFile; |
| 4053 | ParcelFileDescriptor profileFd = null; |
| 4054 | boolean profileAutoStop = false; |
| 4055 | if (mProfileApp != null && mProfileApp.equals(processName)) { |
| 4056 | mProfileProc = app; |
| 4057 | profileFile = mProfileFile; |
| 4058 | profileFd = mProfileFd; |
| 4059 | profileAutoStop = mAutoStopProfiler; |
| 4060 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4061 | boolean enableOpenGlTrace = false; |
| 4062 | if (mOpenGlTraceApp != null && mOpenGlTraceApp.equals(processName)) { |
| 4063 | enableOpenGlTrace = true; |
| 4064 | mOpenGlTraceApp = null; |
| 4065 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4066 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4067 | // If the app is being launched for restore or full backup, set it up specially |
| 4068 | boolean isRestrictedBackupMode = false; |
| 4069 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 4070 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4071 | || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4072 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 4073 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4074 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 4075 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 4076 | ? app.instrumentationInfo.packageName |
| 4077 | : app.info.packageName); |
| 4078 | if (app.instrumentationClass != null) { |
| 4079 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 4080 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4081 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 4082 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4083 | ApplicationInfo appInfo = app.instrumentationInfo != null |
| 4084 | ? app.instrumentationInfo : app.info; |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 4085 | app.compat = compatibilityInfoForPackageLocked(appInfo); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4086 | if (profileFd != null) { |
| 4087 | profileFd = profileFd.dup(); |
| 4088 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4089 | thread.bindApplication(processName, appInfo, providers, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4090 | app.instrumentationClass, profileFile, profileFd, profileAutoStop, |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4091 | app.instrumentationArguments, app.instrumentationWatcher, testMode, |
| 4092 | enableOpenGlTrace, isRestrictedBackupMode || !normalMode, app.persistent, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 4093 | new Configuration(mConfiguration), app.compat, getCommonServicesLocked(), |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 4094 | mCoreSettingsObserver.getCoreSettingsLocked()); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 4095 | updateLruProcessLocked(app, false, true); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 4096 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4097 | } catch (Exception e) { |
| 4098 | // todo: Yikes! What should we do? For now we will try to |
| 4099 | // start another process, but that could easily get us in |
| 4100 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4101 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4102 | |
| 4103 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 4104 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4105 | startProcessLocked(app, "bind fail", processName); |
| 4106 | return false; |
| 4107 | } |
| 4108 | |
| 4109 | // Remove this record from the list of starting applications. |
| 4110 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4111 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 4112 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4113 | mProcessesOnHold.remove(app); |
| 4114 | |
| 4115 | boolean badApp = false; |
| 4116 | boolean didSomething = false; |
| 4117 | |
| 4118 | // 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] | 4119 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 4120 | if (hr != null && normalMode) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4121 | 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] | 4122 | && processName.equals(hr.processName)) { |
| 4123 | try { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 4124 | if (mHeadless) { |
| 4125 | Slog.e(TAG, "Starting activities not supported on headless device: " + hr); |
| 4126 | } else if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4127 | didSomething = true; |
| 4128 | } |
| 4129 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4130 | 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] | 4131 | + hr.intent.getComponent().flattenToShortString(), e); |
| 4132 | badApp = true; |
| 4133 | } |
| 4134 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4135 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4136 | } |
| 4137 | } |
| 4138 | |
| 4139 | // Find any services that should be running in this process... |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4140 | if (!badApp) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4141 | try { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4142 | didSomething |= mServices.attachApplicationLocked(app, processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4143 | } catch (Exception e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4144 | badApp = true; |
| 4145 | } |
| 4146 | } |
| 4147 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4148 | // Check if a next-broadcast receiver is in this process... |
| 4149 | if (!badApp && isPendingBroadcastProcessLocked(pid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4150 | try { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4151 | didSomething = sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4152 | } catch (Exception e) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4153 | // 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] | 4154 | badApp = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4155 | } |
| 4156 | } |
| 4157 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4158 | // Check whether the next backup agent is in this process... |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4159 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4160 | 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] | 4161 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4162 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4163 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, |
| 4164 | compatibilityInfoForPackageLocked(mBackupTarget.appInfo), |
| 4165 | mBackupTarget.backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4166 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4167 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4168 | e.printStackTrace(); |
| 4169 | } |
| 4170 | } |
| 4171 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4172 | if (badApp) { |
| 4173 | // todo: Also need to kill application to deal with all |
| 4174 | // kinds of exceptions. |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4175 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4176 | return false; |
| 4177 | } |
| 4178 | |
| 4179 | if (!didSomething) { |
| 4180 | updateOomAdjLocked(); |
| 4181 | } |
| 4182 | |
| 4183 | return true; |
| 4184 | } |
| 4185 | |
| 4186 | public final void attachApplication(IApplicationThread thread) { |
| 4187 | synchronized (this) { |
| 4188 | int callingPid = Binder.getCallingPid(); |
| 4189 | final long origId = Binder.clearCallingIdentity(); |
| 4190 | attachApplicationLocked(thread, callingPid); |
| 4191 | Binder.restoreCallingIdentity(origId); |
| 4192 | } |
| 4193 | } |
| 4194 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4195 | 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] | 4196 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4197 | ActivityRecord r = mMainStack.activityIdleInternal(token, false, config); |
| 4198 | if (stopProfiling) { |
| 4199 | synchronized (this) { |
| 4200 | if (mProfileProc == r.app) { |
| 4201 | if (mProfileFd != null) { |
| 4202 | try { |
| 4203 | mProfileFd.close(); |
| 4204 | } catch (IOException e) { |
| 4205 | } |
| 4206 | clearProfilerLocked(); |
| 4207 | } |
| 4208 | } |
| 4209 | } |
| 4210 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4211 | Binder.restoreCallingIdentity(origId); |
| 4212 | } |
| 4213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4214 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4215 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4216 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4217 | mWindowManager.enableScreenAfterBoot(); |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 4218 | |
| 4219 | synchronized (this) { |
| 4220 | updateEventDispatchingLocked(); |
| 4221 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4222 | } |
| 4223 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4224 | public void showBootMessage(final CharSequence msg, final boolean always) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4225 | enforceNotIsolatedCaller("showBootMessage"); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4226 | mWindowManager.showBootMessage(msg, always); |
| 4227 | } |
| 4228 | |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4229 | public void dismissKeyguardOnNextActivity() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4230 | enforceNotIsolatedCaller("dismissKeyguardOnNextActivity"); |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4231 | final long token = Binder.clearCallingIdentity(); |
| 4232 | try { |
| 4233 | synchronized (this) { |
| 4234 | if (mLockScreenShown) { |
| 4235 | mLockScreenShown = false; |
| 4236 | comeOutOfSleepIfNeededLocked(); |
| 4237 | } |
| 4238 | mMainStack.dismissKeyguardOnNextActivityLocked(); |
Dianne Hackborn | 1e88e98 | 2012-04-24 18:35:55 -0700 | [diff] [blame] | 4239 | } |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4240 | } finally { |
| 4241 | Binder.restoreCallingIdentity(token); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4242 | } |
| 4243 | } |
| 4244 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4245 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4246 | IntentFilter pkgFilter = new IntentFilter(); |
| 4247 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 4248 | pkgFilter.addDataScheme("package"); |
| 4249 | mContext.registerReceiver(new BroadcastReceiver() { |
| 4250 | @Override |
| 4251 | public void onReceive(Context context, Intent intent) { |
| 4252 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 4253 | if (pkgs != null) { |
| 4254 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 4255 | synchronized (ActivityManagerService.this) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4256 | if (forceStopPackageLocked(pkg, -1, false, false, false, false, 0)) { |
| 4257 | setResultCode(Activity.RESULT_OK); |
| 4258 | return; |
| 4259 | } |
| 4260 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4261 | } |
| 4262 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4263 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4264 | }, pkgFilter); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4265 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4266 | synchronized (this) { |
| 4267 | // Ensure that any processes we had put on hold are now started |
| 4268 | // up. |
| 4269 | final int NP = mProcessesOnHold.size(); |
| 4270 | if (NP > 0) { |
| 4271 | ArrayList<ProcessRecord> procs = |
| 4272 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 4273 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4274 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 4275 | + procs.get(ip)); |
| 4276 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4277 | } |
| 4278 | } |
| 4279 | |
| 4280 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4281 | // Start looking for apps that are abusing wake locks. |
| 4282 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 4283 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4284 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 4285 | SystemProperties.set("sys.boot_completed", "1"); |
Guang Zhu | 191713a | 2012-01-12 12:02:22 -0800 | [diff] [blame] | 4286 | SystemProperties.set("dev.bootcomplete", "1"); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4287 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 4288 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 4289 | if (uss.mState == UserStartedState.STATE_BOOTING) { |
| 4290 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4291 | final int userId = mStartedUsers.keyAt(i); |
| 4292 | Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 4293 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 4294 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4295 | null, null, 0, null, null, |
| 4296 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4297 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4298 | } |
Amith Yamasani | 4860cfc | 2012-08-08 19:14:18 -0700 | [diff] [blame] | 4299 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4300 | } |
| 4301 | } |
| 4302 | } |
| 4303 | |
| 4304 | final void ensureBootCompleted() { |
| 4305 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4306 | boolean enableScreen; |
| 4307 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4308 | booting = mBooting; |
| 4309 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4310 | enableScreen = !mBooted; |
| 4311 | mBooted = true; |
| 4312 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4313 | |
| 4314 | if (booting) { |
| 4315 | finishBooting(); |
| 4316 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4317 | |
| 4318 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4319 | enableScreenAfterBoot(); |
| 4320 | } |
| 4321 | } |
| 4322 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4323 | public final void activityPaused(IBinder token) { |
| 4324 | final long origId = Binder.clearCallingIdentity(); |
| 4325 | mMainStack.activityPaused(token, false); |
| 4326 | Binder.restoreCallingIdentity(origId); |
| 4327 | } |
| 4328 | |
| 4329 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 4330 | CharSequence description) { |
| 4331 | if (localLOGV) Slog.v( |
| 4332 | TAG, "Activity stopped: token=" + token); |
| 4333 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4334 | // Refuse possible leaked file descriptors |
| 4335 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 4336 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 4337 | } |
| 4338 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4339 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4340 | |
| 4341 | final long origId = Binder.clearCallingIdentity(); |
| 4342 | |
| 4343 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4344 | r = mMainStack.isInStackLocked(token); |
| 4345 | if (r != null) { |
| 4346 | r.stack.activityStoppedLocked(r, icicle, thumbnail, description); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4347 | } |
| 4348 | } |
| 4349 | |
| 4350 | if (r != null) { |
| 4351 | sendPendingThumbnail(r, null, null, null, false); |
| 4352 | } |
| 4353 | |
| 4354 | trimApplications(); |
| 4355 | |
| 4356 | Binder.restoreCallingIdentity(origId); |
| 4357 | } |
| 4358 | |
| 4359 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4360 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4361 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | public String getCallingPackage(IBinder token) { |
| 4365 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4366 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 4367 | 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] | 4368 | } |
| 4369 | } |
| 4370 | |
| 4371 | public ComponentName getCallingActivity(IBinder token) { |
| 4372 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4373 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4374 | return r != null ? r.intent.getComponent() : null; |
| 4375 | } |
| 4376 | } |
| 4377 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4378 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4379 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4380 | if (r == null) { |
| 4381 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4382 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4383 | return r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4384 | } |
| 4385 | |
| 4386 | public ComponentName getActivityClassForToken(IBinder token) { |
| 4387 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4388 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4389 | if (r == null) { |
| 4390 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4391 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4392 | return r.intent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4393 | } |
| 4394 | } |
| 4395 | |
| 4396 | public String getPackageForToken(IBinder token) { |
| 4397 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4398 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4399 | if (r == null) { |
| 4400 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4401 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4402 | return r.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4403 | } |
| 4404 | } |
| 4405 | |
| 4406 | public IIntentSender getIntentSender(int type, |
| 4407 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4408 | int requestCode, Intent[] intents, String[] resolvedTypes, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4409 | int flags, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4410 | enforceNotIsolatedCaller("getIntentSender"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4411 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4412 | if (intents != null) { |
| 4413 | if (intents.length < 1) { |
| 4414 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 4415 | } |
| 4416 | for (int i=0; i<intents.length; i++) { |
| 4417 | Intent intent = intents[i]; |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4418 | if (intent != null) { |
| 4419 | if (intent.hasFileDescriptors()) { |
| 4420 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 4421 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4422 | if (type == ActivityManager.INTENT_SENDER_BROADCAST && |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4423 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 4424 | throw new IllegalArgumentException( |
| 4425 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 4426 | } |
| 4427 | intents[i] = new Intent(intent); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4428 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4429 | } |
| 4430 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4431 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4432 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4433 | } |
| 4434 | } |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4435 | if (options != null) { |
| 4436 | if (options.hasFileDescriptors()) { |
| 4437 | throw new IllegalArgumentException("File descriptors passed in options"); |
| 4438 | } |
| 4439 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4440 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4441 | synchronized(this) { |
| 4442 | int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4443 | userId = handleIncomingUserLocked(Binder.getCallingPid(), callingUid, userId, |
| 4444 | false, true, "getIntentSender", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4445 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4446 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4447 | int uid = AppGlobals.getPackageManager() |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4448 | .getPackageUid(packageName, UserHandle.getUserId(callingUid)); |
| 4449 | if (!UserHandle.isSameApp(callingUid, uid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4450 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 4451 | + Binder.getCallingPid() |
| 4452 | + ", uid=" + Binder.getCallingUid() |
| 4453 | + ", (need uid=" + uid + ")" |
| 4454 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4455 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4456 | throw new SecurityException(msg); |
| 4457 | } |
| 4458 | } |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4459 | |
| 4460 | return getIntentSenderLocked(type, packageName, callingUid, userId, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4461 | token, resultWho, requestCode, intents, resolvedTypes, flags, options); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4462 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4463 | } catch (RemoteException e) { |
| 4464 | throw new SecurityException(e); |
| 4465 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4466 | } |
| 4467 | } |
| 4468 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4469 | IIntentSender getIntentSenderLocked(int type, String packageName, |
| 4470 | int callingUid, int userId, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4471 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags, |
| 4472 | Bundle options) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4473 | if (DEBUG_MU) |
| 4474 | Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4475 | ActivityRecord activity = null; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4476 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4477 | activity = mMainStack.isInStackLocked(token); |
| 4478 | if (activity == null) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4479 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4480 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4481 | if (activity.finishing) { |
| 4482 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4483 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4484 | } |
| 4485 | |
| 4486 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 4487 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 4488 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 4489 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 4490 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 4491 | |
| 4492 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 4493 | type, packageName, activity, resultWho, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4494 | requestCode, intents, resolvedTypes, flags, options, userId); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4495 | WeakReference<PendingIntentRecord> ref; |
| 4496 | ref = mIntentSenderRecords.get(key); |
| 4497 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 4498 | if (rec != null) { |
| 4499 | if (!cancelCurrent) { |
| 4500 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4501 | if (rec.key.requestIntent != null) { |
Adam Powell | 501d4a5 | 2012-04-30 15:03:57 -0700 | [diff] [blame] | 4502 | rec.key.requestIntent.replaceExtras(intents != null ? |
| 4503 | intents[intents.length - 1] : null); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4504 | } |
| 4505 | if (intents != null) { |
| 4506 | intents[intents.length-1] = rec.key.requestIntent; |
| 4507 | rec.key.allIntents = intents; |
| 4508 | rec.key.allResolvedTypes = resolvedTypes; |
| 4509 | } else { |
| 4510 | rec.key.allIntents = null; |
| 4511 | rec.key.allResolvedTypes = null; |
| 4512 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4513 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4514 | return rec; |
| 4515 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4516 | rec.canceled = true; |
| 4517 | mIntentSenderRecords.remove(key); |
| 4518 | } |
| 4519 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4520 | return rec; |
| 4521 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4522 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4523 | mIntentSenderRecords.put(key, rec.ref); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4524 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4525 | if (activity.pendingResults == null) { |
| 4526 | activity.pendingResults |
| 4527 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4528 | } |
| 4529 | activity.pendingResults.add(rec.ref); |
| 4530 | } |
| 4531 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | public void cancelIntentSender(IIntentSender sender) { |
| 4535 | if (!(sender instanceof PendingIntentRecord)) { |
| 4536 | return; |
| 4537 | } |
| 4538 | synchronized(this) { |
| 4539 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4540 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4541 | int uid = AppGlobals.getPackageManager() |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4542 | .getPackageUid(rec.key.packageName, UserHandle.getCallingUserId()); |
| 4543 | if (!UserHandle.isSameApp(uid, Binder.getCallingUid())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4544 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4545 | + Binder.getCallingPid() |
| 4546 | + ", uid=" + Binder.getCallingUid() |
| 4547 | + " is not allowed to cancel packges " |
| 4548 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4549 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4550 | throw new SecurityException(msg); |
| 4551 | } |
| 4552 | } catch (RemoteException e) { |
| 4553 | throw new SecurityException(e); |
| 4554 | } |
| 4555 | cancelIntentSenderLocked(rec, true); |
| 4556 | } |
| 4557 | } |
| 4558 | |
| 4559 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4560 | rec.canceled = true; |
| 4561 | mIntentSenderRecords.remove(rec.key); |
| 4562 | if (cleanActivity && rec.key.activity != null) { |
| 4563 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4564 | } |
| 4565 | } |
| 4566 | |
| 4567 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4568 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4569 | return null; |
| 4570 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4571 | try { |
| 4572 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4573 | return res.key.packageName; |
| 4574 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4575 | } |
| 4576 | return null; |
| 4577 | } |
| 4578 | |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 4579 | public int getUidForIntentSender(IIntentSender sender) { |
| 4580 | if (sender instanceof PendingIntentRecord) { |
| 4581 | try { |
| 4582 | PendingIntentRecord res = (PendingIntentRecord)sender; |
| 4583 | return res.uid; |
| 4584 | } catch (ClassCastException e) { |
| 4585 | } |
| 4586 | } |
| 4587 | return -1; |
| 4588 | } |
| 4589 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 4590 | public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) { |
| 4591 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4592 | return false; |
| 4593 | } |
| 4594 | try { |
| 4595 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4596 | if (res.key.allIntents == null) { |
| 4597 | return false; |
| 4598 | } |
| 4599 | for (int i=0; i<res.key.allIntents.length; i++) { |
| 4600 | Intent intent = res.key.allIntents[i]; |
| 4601 | if (intent.getPackage() != null && intent.getComponent() != null) { |
| 4602 | return false; |
| 4603 | } |
| 4604 | } |
| 4605 | return true; |
| 4606 | } catch (ClassCastException e) { |
| 4607 | } |
| 4608 | return false; |
| 4609 | } |
| 4610 | |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 4611 | public boolean isIntentSenderAnActivity(IIntentSender pendingResult) { |
| 4612 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4613 | return false; |
| 4614 | } |
| 4615 | try { |
| 4616 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4617 | if (res.key.type == ActivityManager.INTENT_SENDER_ACTIVITY) { |
| 4618 | return true; |
| 4619 | } |
| 4620 | return false; |
| 4621 | } catch (ClassCastException e) { |
| 4622 | } |
| 4623 | return false; |
| 4624 | } |
| 4625 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4626 | public void setProcessLimit(int max) { |
| 4627 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4628 | "setProcessLimit()"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4629 | synchronized (this) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4630 | mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4631 | mProcessLimitOverride = max; |
| 4632 | } |
| 4633 | trimApplications(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | public int getProcessLimit() { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4637 | synchronized (this) { |
| 4638 | return mProcessLimitOverride; |
| 4639 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4640 | } |
| 4641 | |
| 4642 | void foregroundTokenDied(ForegroundToken token) { |
| 4643 | synchronized (ActivityManagerService.this) { |
| 4644 | synchronized (mPidsSelfLocked) { |
| 4645 | ForegroundToken cur |
| 4646 | = mForegroundProcesses.get(token.pid); |
| 4647 | if (cur != token) { |
| 4648 | return; |
| 4649 | } |
| 4650 | mForegroundProcesses.remove(token.pid); |
| 4651 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4652 | if (pr == null) { |
| 4653 | return; |
| 4654 | } |
| 4655 | pr.forcingToForeground = null; |
| 4656 | pr.foregroundServices = false; |
| 4657 | } |
| 4658 | updateOomAdjLocked(); |
| 4659 | } |
| 4660 | } |
| 4661 | |
| 4662 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4663 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4664 | "setProcessForeground()"); |
| 4665 | synchronized(this) { |
| 4666 | boolean changed = false; |
| 4667 | |
| 4668 | synchronized (mPidsSelfLocked) { |
| 4669 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4670 | if (pr == null && isForeground) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4671 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4672 | return; |
| 4673 | } |
| 4674 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4675 | if (oldToken != null) { |
| 4676 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4677 | mForegroundProcesses.remove(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4678 | if (pr != null) { |
| 4679 | pr.forcingToForeground = null; |
| 4680 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4681 | changed = true; |
| 4682 | } |
| 4683 | if (isForeground && token != null) { |
| 4684 | ForegroundToken newToken = new ForegroundToken() { |
| 4685 | public void binderDied() { |
| 4686 | foregroundTokenDied(this); |
| 4687 | } |
| 4688 | }; |
| 4689 | newToken.pid = pid; |
| 4690 | newToken.token = token; |
| 4691 | try { |
| 4692 | token.linkToDeath(newToken, 0); |
| 4693 | mForegroundProcesses.put(pid, newToken); |
| 4694 | pr.forcingToForeground = token; |
| 4695 | changed = true; |
| 4696 | } catch (RemoteException e) { |
| 4697 | // If the process died while doing this, we will later |
| 4698 | // do the cleanup with the process death link. |
| 4699 | } |
| 4700 | } |
| 4701 | } |
| 4702 | |
| 4703 | if (changed) { |
| 4704 | updateOomAdjLocked(); |
| 4705 | } |
| 4706 | } |
| 4707 | } |
| 4708 | |
| 4709 | // ========================================================= |
| 4710 | // PERMISSIONS |
| 4711 | // ========================================================= |
| 4712 | |
| 4713 | static class PermissionController extends IPermissionController.Stub { |
| 4714 | ActivityManagerService mActivityManagerService; |
| 4715 | PermissionController(ActivityManagerService activityManagerService) { |
| 4716 | mActivityManagerService = activityManagerService; |
| 4717 | } |
| 4718 | |
| 4719 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4720 | return mActivityManagerService.checkPermission(permission, pid, |
| 4721 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4722 | } |
| 4723 | } |
| 4724 | |
| 4725 | /** |
| 4726 | * This can be called with or without the global lock held. |
| 4727 | */ |
| 4728 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4729 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4730 | // We might be performing an operation on behalf of an indirect binder |
| 4731 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4732 | // client identity accordingly before proceeding. |
| 4733 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4734 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4735 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4736 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4737 | uid = tlsIdentity.uid; |
| 4738 | pid = tlsIdentity.pid; |
| 4739 | } |
| 4740 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4741 | if (pid == MY_PID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4742 | return PackageManager.PERMISSION_GRANTED; |
| 4743 | } |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4744 | |
| 4745 | return ActivityManager.checkComponentPermission(permission, uid, |
| 4746 | owningUid, exported); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | /** |
| 4750 | * As the only public entry point for permissions checking, this method |
| 4751 | * can enforce the semantic that requesting a check on a null global |
| 4752 | * permission is automatically denied. (Internally a null permission |
| 4753 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4754 | * when only uid-based security is needed.) |
| 4755 | * |
| 4756 | * This can be called with or without the global lock held. |
| 4757 | */ |
| 4758 | public int checkPermission(String permission, int pid, int uid) { |
| 4759 | if (permission == null) { |
| 4760 | return PackageManager.PERMISSION_DENIED; |
| 4761 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4762 | return checkComponentPermission(permission, pid, UserHandle.getAppId(uid), -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4763 | } |
| 4764 | |
| 4765 | /** |
| 4766 | * Binder IPC calls go through the public entry point. |
| 4767 | * This can be called with or without the global lock held. |
| 4768 | */ |
| 4769 | int checkCallingPermission(String permission) { |
| 4770 | return checkPermission(permission, |
| 4771 | Binder.getCallingPid(), |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4772 | UserHandle.getAppId(Binder.getCallingUid())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | /** |
| 4776 | * This can be called with or without the global lock held. |
| 4777 | */ |
| 4778 | void enforceCallingPermission(String permission, String func) { |
| 4779 | if (checkCallingPermission(permission) |
| 4780 | == PackageManager.PERMISSION_GRANTED) { |
| 4781 | return; |
| 4782 | } |
| 4783 | |
| 4784 | String msg = "Permission Denial: " + func + " from pid=" |
| 4785 | + Binder.getCallingPid() |
| 4786 | + ", uid=" + Binder.getCallingUid() |
| 4787 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4788 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4789 | throw new SecurityException(msg); |
| 4790 | } |
| 4791 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4792 | /** |
| 4793 | * Determine if UID is holding permissions required to access {@link Uri} in |
| 4794 | * the given {@link ProviderInfo}. Final permission checking is always done |
| 4795 | * in {@link ContentProvider}. |
| 4796 | */ |
| 4797 | private final boolean checkHoldingPermissionsLocked( |
| 4798 | IPackageManager pm, ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4799 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4800 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4801 | |
| 4802 | if (pi.applicationInfo.uid == uid) { |
| 4803 | return true; |
| 4804 | } else if (!pi.exported) { |
| 4805 | return false; |
| 4806 | } |
| 4807 | |
| 4808 | boolean readMet = (modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 4809 | boolean writeMet = (modeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4810 | try { |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4811 | // check if target holds top-level <provider> permissions |
| 4812 | if (!readMet && pi.readPermission != null |
| 4813 | && (pm.checkUidPermission(pi.readPermission, uid) == PERMISSION_GRANTED)) { |
| 4814 | readMet = true; |
| 4815 | } |
| 4816 | if (!writeMet && pi.writePermission != null |
| 4817 | && (pm.checkUidPermission(pi.writePermission, uid) == PERMISSION_GRANTED)) { |
| 4818 | writeMet = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4819 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4820 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4821 | // track if unprotected read/write is allowed; any denied |
| 4822 | // <path-permission> below removes this ability |
| 4823 | boolean allowDefaultRead = pi.readPermission == null; |
| 4824 | boolean allowDefaultWrite = pi.writePermission == null; |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4825 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4826 | // check if target holds any <path-permission> that match uri |
| 4827 | final PathPermission[] pps = pi.pathPermissions; |
| 4828 | if (pps != null) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4829 | final String path = uri.getPath(); |
| 4830 | int i = pps.length; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4831 | while (i > 0 && (!readMet || !writeMet)) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4832 | i--; |
| 4833 | PathPermission pp = pps[i]; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4834 | if (pp.match(path)) { |
| 4835 | if (!readMet) { |
| 4836 | final String pprperm = pp.getReadPermission(); |
| 4837 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 4838 | + pprperm + " for " + pp.getPath() |
| 4839 | + ": match=" + pp.match(path) |
| 4840 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 4841 | if (pprperm != null) { |
| 4842 | if (pm.checkUidPermission(pprperm, uid) == PERMISSION_GRANTED) { |
| 4843 | readMet = true; |
| 4844 | } else { |
| 4845 | allowDefaultRead = false; |
| 4846 | } |
| 4847 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4848 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4849 | if (!writeMet) { |
| 4850 | final String ppwperm = pp.getWritePermission(); |
| 4851 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 4852 | + ppwperm + " for " + pp.getPath() |
| 4853 | + ": match=" + pp.match(path) |
| 4854 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 4855 | if (ppwperm != null) { |
| 4856 | if (pm.checkUidPermission(ppwperm, uid) == PERMISSION_GRANTED) { |
| 4857 | writeMet = true; |
| 4858 | } else { |
| 4859 | allowDefaultWrite = false; |
| 4860 | } |
| 4861 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4862 | } |
| 4863 | } |
| 4864 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 4865 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4866 | |
| 4867 | // grant unprotected <provider> read/write, if not blocked by |
| 4868 | // <path-permission> above |
| 4869 | if (allowDefaultRead) readMet = true; |
| 4870 | if (allowDefaultWrite) writeMet = true; |
| 4871 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4872 | } catch (RemoteException e) { |
| 4873 | return false; |
| 4874 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4875 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4876 | return readMet && writeMet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 4880 | int modeFlags) { |
| 4881 | // Root gets to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4882 | if (uid == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4883 | return true; |
| 4884 | } |
| 4885 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 4886 | if (perms == null) return false; |
| 4887 | UriPermission perm = perms.get(uri); |
| 4888 | if (perm == null) return false; |
| 4889 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 4890 | } |
| 4891 | |
| 4892 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4893 | enforceNotIsolatedCaller("checkUriPermission"); |
| 4894 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4895 | // Another redirected-binder-call permissions check as in |
| 4896 | // {@link checkComponentPermission}. |
| 4897 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4898 | if (tlsIdentity != null) { |
| 4899 | uid = tlsIdentity.uid; |
| 4900 | pid = tlsIdentity.pid; |
| 4901 | } |
| 4902 | |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4903 | uid = UserHandle.getAppId(uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4904 | // Our own process gets to do everything. |
| 4905 | if (pid == MY_PID) { |
| 4906 | return PackageManager.PERMISSION_GRANTED; |
| 4907 | } |
| 4908 | synchronized(this) { |
| 4909 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 4910 | ? PackageManager.PERMISSION_GRANTED |
| 4911 | : PackageManager.PERMISSION_DENIED; |
| 4912 | } |
| 4913 | } |
| 4914 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4915 | /** |
| 4916 | * Check if the targetPkg can be granted permission to access uri by |
| 4917 | * the callingUid using the given modeFlags. Throws a security exception |
| 4918 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 4919 | * if the URI permission grant should be performed; returns -1 if it is not |
| 4920 | * needed (for example targetPkg already has permission to access the URI). |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4921 | * If you already know the uid of the target, you can supply it in |
| 4922 | * lastTargetUid else set that to -1. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4923 | */ |
| 4924 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4925 | Uri uri, int modeFlags, int lastTargetUid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4926 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4927 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 4928 | if (modeFlags == 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4929 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4930 | } |
| 4931 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4932 | if (targetPkg != null) { |
| 4933 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4934 | "Checking grant " + targetPkg + " permission to " + uri); |
| 4935 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4936 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4937 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4938 | |
| 4939 | // If this is not a content: uri, we can't do anything with it. |
| 4940 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4941 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 4942 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4943 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | String name = uri.getAuthority(); |
| 4947 | ProviderInfo pi = null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4948 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4949 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4950 | if (cpr != null) { |
| 4951 | pi = cpr.info; |
| 4952 | } else { |
| 4953 | try { |
| 4954 | pi = pm.resolveContentProvider(name, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4955 | PackageManager.GET_URI_PERMISSION_PATTERNS, UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4956 | } catch (RemoteException ex) { |
| 4957 | } |
| 4958 | } |
| 4959 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 4960 | Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4961 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4962 | } |
| 4963 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 4964 | int targetUid = lastTargetUid; |
| 4965 | if (targetUid < 0 && targetPkg != null) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4966 | try { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4967 | targetUid = pm.getPackageUid(targetPkg, UserHandle.getUserId(callingUid)); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4968 | if (targetUid < 0) { |
| 4969 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4970 | "Can't grant URI permission no uid for: " + targetPkg); |
| 4971 | return -1; |
| 4972 | } |
| 4973 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 4974 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4975 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4976 | } |
| 4977 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 4978 | if (targetUid >= 0) { |
| 4979 | // First... does the target actually need this permission? |
| 4980 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 4981 | // No need to grant the target this permission. |
| 4982 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4983 | "Target " + targetPkg + " already has full permission to " + uri); |
| 4984 | return -1; |
| 4985 | } |
| 4986 | } else { |
| 4987 | // First... there is no target package, so can anyone access it? |
| 4988 | boolean allowed = pi.exported; |
| 4989 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 4990 | if (pi.readPermission != null) { |
| 4991 | allowed = false; |
| 4992 | } |
| 4993 | } |
| 4994 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 4995 | if (pi.writePermission != null) { |
| 4996 | allowed = false; |
| 4997 | } |
| 4998 | } |
| 4999 | if (allowed) { |
| 5000 | return -1; |
| 5001 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5002 | } |
| 5003 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5004 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5005 | if (!pi.grantUriPermissions) { |
| 5006 | throw new SecurityException("Provider " + pi.packageName |
| 5007 | + "/" + pi.name |
| 5008 | + " does not allow granting of Uri permissions (uri " |
| 5009 | + uri + ")"); |
| 5010 | } |
| 5011 | if (pi.uriPermissionPatterns != null) { |
| 5012 | final int N = pi.uriPermissionPatterns.length; |
| 5013 | boolean allowed = false; |
| 5014 | for (int i=0; i<N; i++) { |
| 5015 | if (pi.uriPermissionPatterns[i] != null |
| 5016 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 5017 | allowed = true; |
| 5018 | break; |
| 5019 | } |
| 5020 | } |
| 5021 | if (!allowed) { |
| 5022 | throw new SecurityException("Provider " + pi.packageName |
| 5023 | + "/" + pi.name |
| 5024 | + " does not allow granting of permission to path of Uri " |
| 5025 | + uri); |
| 5026 | } |
| 5027 | } |
| 5028 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5029 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5030 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5031 | if (callingUid != Process.myUid()) { |
| 5032 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 5033 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5034 | throw new SecurityException("Uid " + callingUid |
| 5035 | + " does not have permission to uri " + uri); |
| 5036 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5037 | } |
| 5038 | } |
| 5039 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5040 | return targetUid; |
| 5041 | } |
| 5042 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5043 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 5044 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5045 | enforceNotIsolatedCaller("checkGrantUriPermission"); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5046 | synchronized(this) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5047 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5048 | } |
| 5049 | } |
| 5050 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5051 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 5052 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 5053 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5054 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5055 | if (modeFlags == 0) { |
| 5056 | return; |
| 5057 | } |
| 5058 | |
| 5059 | // 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] | 5060 | // to the uri, and the target doesn't. Let's now give this to |
| 5061 | // the target. |
| 5062 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5063 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5064 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5065 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5066 | HashMap<Uri, UriPermission> targetUris |
| 5067 | = mGrantedUriPermissions.get(targetUid); |
| 5068 | if (targetUris == null) { |
| 5069 | targetUris = new HashMap<Uri, UriPermission>(); |
| 5070 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 5071 | } |
| 5072 | |
| 5073 | UriPermission perm = targetUris.get(uri); |
| 5074 | if (perm == null) { |
| 5075 | perm = new UriPermission(targetUid, uri); |
| 5076 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5077 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5078 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5079 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5080 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5081 | perm.globalModeFlags |= modeFlags; |
Vairavan Srinivasan | 91c12c2 | 2011-01-21 18:26:06 -0800 | [diff] [blame] | 5082 | } else { |
| 5083 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 5084 | perm.readOwners.add(owner); |
| 5085 | owner.addReadPermission(perm); |
| 5086 | } |
| 5087 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 5088 | perm.writeOwners.add(owner); |
| 5089 | owner.addWritePermission(perm); |
| 5090 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5091 | } |
| 5092 | } |
| 5093 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5094 | void grantUriPermissionLocked(int callingUid, String targetPkg, Uri uri, |
| 5095 | int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5096 | if (targetPkg == null) { |
| 5097 | throw new NullPointerException("targetPkg"); |
| 5098 | } |
| 5099 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5100 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5101 | if (targetUid < 0) { |
| 5102 | return; |
| 5103 | } |
| 5104 | |
| 5105 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 5106 | } |
| 5107 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5108 | static class NeededUriGrants extends ArrayList<Uri> { |
| 5109 | final String targetPkg; |
| 5110 | final int targetUid; |
| 5111 | final int flags; |
| 5112 | |
| 5113 | NeededUriGrants(String _targetPkg, int _targetUid, int _flags) { |
| 5114 | targetPkg = _targetPkg; |
| 5115 | targetUid = _targetUid; |
| 5116 | flags = _flags; |
| 5117 | } |
| 5118 | } |
| 5119 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5120 | /** |
| 5121 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 5122 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5123 | NeededUriGrants checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 5124 | String targetPkg, Intent intent, int mode, NeededUriGrants needed) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5125 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5126 | "Checking URI perm to data=" + (intent != null ? intent.getData() : null) |
| 5127 | + " clip=" + (intent != null ? intent.getClipData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5128 | + " from " + intent + "; flags=0x" |
| 5129 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 5130 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5131 | if (targetPkg == null) { |
| 5132 | throw new NullPointerException("targetPkg"); |
| 5133 | } |
| 5134 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5135 | if (intent == null) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5136 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5137 | } |
| 5138 | Uri data = intent.getData(); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5139 | ClipData clip = intent.getClipData(); |
| 5140 | if (data == null && clip == null) { |
| 5141 | return null; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5142 | } |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5143 | if (data != null) { |
| 5144 | int target = checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 5145 | mode, needed != null ? needed.targetUid : -1); |
| 5146 | if (target > 0) { |
| 5147 | if (needed == null) { |
| 5148 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5149 | } |
| 5150 | needed.add(data); |
| 5151 | } |
| 5152 | } |
| 5153 | if (clip != null) { |
| 5154 | for (int i=0; i<clip.getItemCount(); i++) { |
| 5155 | Uri uri = clip.getItemAt(i).getUri(); |
| 5156 | if (uri != null) { |
| 5157 | int target = -1; |
| 5158 | target = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, |
| 5159 | mode, needed != null ? needed.targetUid : -1); |
| 5160 | if (target > 0) { |
| 5161 | if (needed == null) { |
| 5162 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5163 | } |
| 5164 | needed.add(uri); |
| 5165 | } |
| 5166 | } else { |
| 5167 | Intent clipIntent = clip.getItemAt(i).getIntent(); |
| 5168 | if (clipIntent != null) { |
| 5169 | NeededUriGrants newNeeded = checkGrantUriPermissionFromIntentLocked( |
| 5170 | callingUid, targetPkg, clipIntent, mode, needed); |
| 5171 | if (newNeeded != null) { |
| 5172 | needed = newNeeded; |
| 5173 | } |
| 5174 | } |
| 5175 | } |
| 5176 | } |
| 5177 | } |
| 5178 | |
| 5179 | return needed; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5180 | } |
| 5181 | |
| 5182 | /** |
| 5183 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 5184 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5185 | void grantUriPermissionUncheckedFromIntentLocked(NeededUriGrants needed, |
| 5186 | UriPermissionOwner owner) { |
| 5187 | if (needed != null) { |
| 5188 | for (int i=0; i<needed.size(); i++) { |
| 5189 | grantUriPermissionUncheckedLocked(needed.targetUid, needed.targetPkg, |
| 5190 | needed.get(i), needed.flags, owner); |
| 5191 | } |
| 5192 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5193 | } |
| 5194 | |
| 5195 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 5196 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5197 | NeededUriGrants needed = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, |
Dianne Hackborn | d9781fe | 2012-03-08 18:04:18 -0800 | [diff] [blame] | 5198 | intent, intent != null ? intent.getFlags() : 0, null); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5199 | if (needed == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5200 | return; |
| 5201 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5202 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5203 | grantUriPermissionUncheckedFromIntentLocked(needed, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5204 | } |
| 5205 | |
| 5206 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 5207 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5208 | enforceNotIsolatedCaller("grantUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5209 | synchronized(this) { |
| 5210 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5211 | if (r == null) { |
| 5212 | throw new SecurityException("Unable to find app for caller " |
| 5213 | + caller |
| 5214 | + " when granting permission to uri " + uri); |
| 5215 | } |
| 5216 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5217 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5218 | } |
| 5219 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5220 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5221 | } |
| 5222 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5223 | grantUriPermissionLocked(r.uid, targetPkg, uri, modeFlags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5224 | null); |
| 5225 | } |
| 5226 | } |
| 5227 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5228 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5229 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5230 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 5231 | HashMap<Uri, UriPermission> perms |
| 5232 | = mGrantedUriPermissions.get(perm.uid); |
| 5233 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5234 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5235 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5236 | perms.remove(perm.uri); |
| 5237 | if (perms.size() == 0) { |
| 5238 | mGrantedUriPermissions.remove(perm.uid); |
| 5239 | } |
| 5240 | } |
| 5241 | } |
| 5242 | } |
| 5243 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5244 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 5245 | int modeFlags) { |
| 5246 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5247 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5248 | if (modeFlags == 0) { |
| 5249 | return; |
| 5250 | } |
| 5251 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5252 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5253 | "Revoking all granted permissions to " + uri); |
| 5254 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5255 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5256 | |
| 5257 | final String authority = uri.getAuthority(); |
| 5258 | ProviderInfo pi = null; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5259 | int userId = UserHandle.getUserId(callingUid); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5260 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5261 | if (cpr != null) { |
| 5262 | pi = cpr.info; |
| 5263 | } else { |
| 5264 | try { |
| 5265 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5266 | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5267 | } catch (RemoteException ex) { |
| 5268 | } |
| 5269 | } |
| 5270 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5271 | 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] | 5272 | return; |
| 5273 | } |
| 5274 | |
| 5275 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5276 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5277 | // Right now, if you are not the original owner of the permission, |
| 5278 | // you are not allowed to revoke it. |
| 5279 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5280 | throw new SecurityException("Uid " + callingUid |
| 5281 | + " does not have permission to uri " + uri); |
| 5282 | //} |
| 5283 | } |
| 5284 | |
| 5285 | // Go through all of the permissions and remove any that match. |
| 5286 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 5287 | if (SEGMENTS != null) { |
| 5288 | final int NS = SEGMENTS.size(); |
| 5289 | int N = mGrantedUriPermissions.size(); |
| 5290 | for (int i=0; i<N; i++) { |
| 5291 | HashMap<Uri, UriPermission> perms |
| 5292 | = mGrantedUriPermissions.valueAt(i); |
| 5293 | Iterator<UriPermission> it = perms.values().iterator(); |
| 5294 | toploop: |
| 5295 | while (it.hasNext()) { |
| 5296 | UriPermission perm = it.next(); |
| 5297 | Uri targetUri = perm.uri; |
| 5298 | if (!authority.equals(targetUri.getAuthority())) { |
| 5299 | continue; |
| 5300 | } |
| 5301 | List<String> targetSegments = targetUri.getPathSegments(); |
| 5302 | if (targetSegments == null) { |
| 5303 | continue; |
| 5304 | } |
| 5305 | if (targetSegments.size() < NS) { |
| 5306 | continue; |
| 5307 | } |
| 5308 | for (int j=0; j<NS; j++) { |
| 5309 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 5310 | continue toploop; |
| 5311 | } |
| 5312 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5313 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5314 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5315 | perm.clearModes(modeFlags); |
| 5316 | if (perm.modeFlags == 0) { |
| 5317 | it.remove(); |
| 5318 | } |
| 5319 | } |
| 5320 | if (perms.size() == 0) { |
| 5321 | mGrantedUriPermissions.remove( |
| 5322 | mGrantedUriPermissions.keyAt(i)); |
| 5323 | N--; |
| 5324 | i--; |
| 5325 | } |
| 5326 | } |
| 5327 | } |
| 5328 | } |
| 5329 | |
| 5330 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 5331 | int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5332 | enforceNotIsolatedCaller("revokeUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5333 | synchronized(this) { |
| 5334 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5335 | if (r == null) { |
| 5336 | throw new SecurityException("Unable to find app for caller " |
| 5337 | + caller |
| 5338 | + " when revoking permission to uri " + uri); |
| 5339 | } |
| 5340 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5341 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5342 | return; |
| 5343 | } |
| 5344 | |
| 5345 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5346 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5347 | if (modeFlags == 0) { |
| 5348 | return; |
| 5349 | } |
| 5350 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5351 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5352 | |
| 5353 | final String authority = uri.getAuthority(); |
| 5354 | ProviderInfo pi = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5355 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5356 | if (cpr != null) { |
| 5357 | pi = cpr.info; |
| 5358 | } else { |
| 5359 | try { |
| 5360 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5361 | PackageManager.GET_URI_PERMISSION_PATTERNS, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5362 | } catch (RemoteException ex) { |
| 5363 | } |
| 5364 | } |
| 5365 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5366 | Slog.w(TAG, "No content provider found for permission revoke: " |
| 5367 | + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5368 | return; |
| 5369 | } |
| 5370 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5371 | revokeUriPermissionLocked(r.uid, uri, modeFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5372 | } |
| 5373 | } |
| 5374 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5375 | @Override |
| 5376 | public IBinder newUriPermissionOwner(String name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5377 | enforceNotIsolatedCaller("newUriPermissionOwner"); |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5378 | synchronized(this) { |
| 5379 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 5380 | return owner.getExternalTokenLocked(); |
| 5381 | } |
| 5382 | } |
| 5383 | |
| 5384 | @Override |
| 5385 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 5386 | Uri uri, int modeFlags) { |
| 5387 | synchronized(this) { |
| 5388 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5389 | if (owner == null) { |
| 5390 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5391 | } |
| 5392 | if (fromUid != Binder.getCallingUid()) { |
| 5393 | if (Binder.getCallingUid() != Process.myUid()) { |
| 5394 | // Only system code can grant URI permissions on behalf |
| 5395 | // of other users. |
| 5396 | throw new SecurityException("nice try"); |
| 5397 | } |
| 5398 | } |
| 5399 | if (targetPkg == null) { |
| 5400 | throw new IllegalArgumentException("null target"); |
| 5401 | } |
| 5402 | if (uri == null) { |
| 5403 | throw new IllegalArgumentException("null uri"); |
| 5404 | } |
| 5405 | |
| 5406 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 5407 | } |
| 5408 | } |
| 5409 | |
| 5410 | @Override |
| 5411 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 5412 | synchronized(this) { |
| 5413 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5414 | if (owner == null) { |
| 5415 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5416 | } |
| 5417 | |
| 5418 | if (uri == null) { |
| 5419 | owner.removeUriPermissionsLocked(mode); |
| 5420 | } else { |
| 5421 | owner.removeUriPermissionLocked(uri, mode); |
| 5422 | } |
| 5423 | } |
| 5424 | } |
| 5425 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5426 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 5427 | synchronized (this) { |
| 5428 | ProcessRecord app = |
| 5429 | who != null ? getRecordForAppLocked(who) : null; |
| 5430 | if (app == null) return; |
| 5431 | |
| 5432 | Message msg = Message.obtain(); |
| 5433 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 5434 | msg.obj = app; |
| 5435 | msg.arg1 = waiting ? 1 : 0; |
| 5436 | mHandler.sendMessage(msg); |
| 5437 | } |
| 5438 | } |
| 5439 | |
| 5440 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5441 | final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ); |
| 5442 | 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] | 5443 | outInfo.availMem = Process.getFreeMemory(); |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 5444 | outInfo.totalMem = Process.getTotalMemory(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5445 | outInfo.threshold = homeAppMem; |
| 5446 | outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2)); |
| 5447 | outInfo.hiddenAppThreshold = hiddenAppMem; |
| 5448 | outInfo.secondaryServerThreshold = mProcessList.getMemLevel( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 5449 | ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5450 | outInfo.visibleAppThreshold = mProcessList.getMemLevel( |
| 5451 | ProcessList.VISIBLE_APP_ADJ); |
| 5452 | outInfo.foregroundAppThreshold = mProcessList.getMemLevel( |
| 5453 | ProcessList.FOREGROUND_APP_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5454 | } |
| 5455 | |
| 5456 | // ========================================================= |
| 5457 | // TASK MANAGEMENT |
| 5458 | // ========================================================= |
| 5459 | |
| 5460 | public List getTasks(int maxNum, int flags, |
| 5461 | IThumbnailReceiver receiver) { |
| 5462 | ArrayList list = new ArrayList(); |
| 5463 | |
| 5464 | PendingThumbnailsRecord pending = null; |
| 5465 | IApplicationThread topThumbnail = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5466 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5467 | |
| 5468 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5469 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5470 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 5471 | + ", receiver=" + receiver); |
| 5472 | |
| 5473 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 5474 | != PackageManager.PERMISSION_GRANTED) { |
| 5475 | if (receiver != null) { |
| 5476 | // If the caller wants to wait for pending thumbnails, |
| 5477 | // it ain't gonna get them. |
| 5478 | try { |
| 5479 | receiver.finished(); |
| 5480 | } catch (RemoteException ex) { |
| 5481 | } |
| 5482 | } |
| 5483 | String msg = "Permission Denial: getTasks() from pid=" |
| 5484 | + Binder.getCallingPid() |
| 5485 | + ", uid=" + Binder.getCallingUid() |
| 5486 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5487 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5488 | throw new SecurityException(msg); |
| 5489 | } |
| 5490 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5491 | int pos = mMainStack.mHistory.size()-1; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5492 | ActivityRecord next = |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5493 | pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5494 | ActivityRecord top = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5495 | TaskRecord curTask = null; |
| 5496 | int numActivities = 0; |
| 5497 | int numRunning = 0; |
| 5498 | while (pos >= 0 && maxNum > 0) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5499 | final ActivityRecord r = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5500 | pos--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5501 | next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5502 | |
| 5503 | // Initialize state for next task if needed. |
| 5504 | if (top == null || |
| 5505 | (top.state == ActivityState.INITIALIZING |
| 5506 | && top.task == r.task)) { |
| 5507 | top = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5508 | curTask = r.task; |
| 5509 | numActivities = numRunning = 0; |
| 5510 | } |
| 5511 | |
| 5512 | // Add 'r' into the current task. |
| 5513 | numActivities++; |
| 5514 | if (r.app != null && r.app.thread != null) { |
| 5515 | numRunning++; |
| 5516 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5517 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5518 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5519 | TAG, r.intent.getComponent().flattenToShortString() |
| 5520 | + ": task=" + r.task); |
| 5521 | |
| 5522 | // If the next one is a different task, generate a new |
| 5523 | // TaskInfo entry for what we have. |
| 5524 | if (next == null || next.task != curTask) { |
| 5525 | ActivityManager.RunningTaskInfo ci |
| 5526 | = new ActivityManager.RunningTaskInfo(); |
| 5527 | ci.id = curTask.taskId; |
| 5528 | ci.baseActivity = r.intent.getComponent(); |
| 5529 | ci.topActivity = top.intent.getComponent(); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5530 | if (top.thumbHolder != null) { |
| 5531 | ci.description = top.thumbHolder.lastDescription; |
| 5532 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5533 | ci.numActivities = numActivities; |
| 5534 | ci.numRunning = numRunning; |
| 5535 | //System.out.println( |
| 5536 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 5537 | if (ci.thumbnail == null && receiver != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5538 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5539 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 5540 | + " app=" + top.app |
| 5541 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 5542 | if (top.state == ActivityState.RESUMED |
| 5543 | || top.state == ActivityState.PAUSING) { |
| 5544 | if (top.idle && top.app != null |
| 5545 | && top.app.thread != null) { |
| 5546 | topRecord = top; |
| 5547 | topThumbnail = top.app.thread; |
| 5548 | } else { |
| 5549 | top.thumbnailNeeded = true; |
| 5550 | } |
| 5551 | } |
| 5552 | if (pending == null) { |
| 5553 | pending = new PendingThumbnailsRecord(receiver); |
| 5554 | } |
| 5555 | pending.pendingRecords.add(top); |
| 5556 | } |
| 5557 | list.add(ci); |
| 5558 | maxNum--; |
| 5559 | top = null; |
| 5560 | } |
| 5561 | } |
| 5562 | |
| 5563 | if (pending != null) { |
| 5564 | mPendingThumbnails.add(pending); |
| 5565 | } |
| 5566 | } |
| 5567 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5568 | 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] | 5569 | |
| 5570 | if (topThumbnail != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5571 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5572 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5573 | topThumbnail.requestThumbnail(topRecord.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5574 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5575 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5576 | sendPendingThumbnail(null, topRecord.appToken, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5577 | } |
| 5578 | } |
| 5579 | |
| 5580 | if (pending == null && receiver != null) { |
| 5581 | // In this case all thumbnails were available and the client |
| 5582 | // is being asked to be told when the remaining ones come in... |
| 5583 | // which is unusually, since the top-most currently running |
| 5584 | // activity should never have a canned thumbnail! Oh well. |
| 5585 | try { |
| 5586 | receiver.finished(); |
| 5587 | } catch (RemoteException ex) { |
| 5588 | } |
| 5589 | } |
| 5590 | |
| 5591 | return list; |
| 5592 | } |
| 5593 | |
| 5594 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5595 | int flags, int userId) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5596 | final int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5597 | if (userId != UserHandle.getCallingUserId()) { |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5598 | // Check if the caller is holding permissions for cross-user requests. |
| 5599 | if (checkComponentPermission( |
| 5600 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 5601 | Binder.getCallingPid(), callingUid, -1, true) |
| 5602 | != PackageManager.PERMISSION_GRANTED) { |
| 5603 | String msg = "Permission Denial: " |
| 5604 | + "Request to get recent tasks for user " + userId |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5605 | + " but is calling from user " + UserHandle.getUserId(callingUid) |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5606 | + "; this requires " |
| 5607 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 5608 | Slog.w(TAG, msg); |
| 5609 | throw new SecurityException(msg); |
| 5610 | } else { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5611 | if (userId == UserHandle.USER_CURRENT) { |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5612 | userId = mCurrentUserId; |
| 5613 | } |
| 5614 | } |
| 5615 | } |
| 5616 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5617 | synchronized (this) { |
| 5618 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 5619 | "getRecentTasks()"); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5620 | final boolean detailed = checkCallingPermission( |
| 5621 | android.Manifest.permission.GET_DETAILED_TASKS) |
| 5622 | == PackageManager.PERMISSION_GRANTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5623 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5624 | IPackageManager pm = AppGlobals.getPackageManager(); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5625 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5626 | final int N = mRecentTasks.size(); |
| 5627 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 5628 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 5629 | maxNum < N ? maxNum : N); |
| 5630 | for (int i=0; i<N && maxNum > 0; i++) { |
| 5631 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5632 | // Only add calling user's recent tasks |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5633 | if (tr.userId != userId) continue; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5634 | // Return the entry if desired by the caller. We always return |
| 5635 | // the first entry, because callers always expect this to be the |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5636 | // foreground app. We may filter others if the caller has |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5637 | // not supplied RECENT_WITH_EXCLUDED and there is some reason |
| 5638 | // we should exclude the entry. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5639 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5640 | if (i == 0 |
| 5641 | || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5642 | || (tr.intent == null) |
| 5643 | || ((tr.intent.getFlags() |
| 5644 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 5645 | ActivityManager.RecentTaskInfo rti |
| 5646 | = new ActivityManager.RecentTaskInfo(); |
| 5647 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5648 | rti.persistentId = tr.taskId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5649 | rti.baseIntent = new Intent( |
| 5650 | tr.intent != null ? tr.intent : tr.affinityIntent); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5651 | if (!detailed) { |
| 5652 | rti.baseIntent.replaceExtras((Bundle)null); |
| 5653 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5654 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 5655 | rti.description = tr.lastDescription; |
| 5656 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5657 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5658 | // Check whether this activity is currently available. |
| 5659 | try { |
| 5660 | if (rti.origActivity != null) { |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5661 | if (pm.getActivityInfo(rti.origActivity, 0, userId) |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5662 | == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5663 | continue; |
| 5664 | } |
| 5665 | } else if (rti.baseIntent != null) { |
| 5666 | if (pm.queryIntentActivities(rti.baseIntent, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5667 | null, 0, userId) == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5668 | continue; |
| 5669 | } |
| 5670 | } |
| 5671 | } catch (RemoteException e) { |
| 5672 | // Will never happen. |
| 5673 | } |
| 5674 | } |
| 5675 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5676 | res.add(rti); |
| 5677 | maxNum--; |
| 5678 | } |
| 5679 | } |
| 5680 | return res; |
| 5681 | } |
| 5682 | } |
| 5683 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5684 | private TaskRecord taskForIdLocked(int id) { |
| 5685 | final int N = mRecentTasks.size(); |
| 5686 | for (int i=0; i<N; i++) { |
| 5687 | TaskRecord tr = mRecentTasks.get(i); |
| 5688 | if (tr.taskId == id) { |
| 5689 | return tr; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5690 | } |
| 5691 | } |
| 5692 | return null; |
| 5693 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5694 | |
| 5695 | public ActivityManager.TaskThumbnails getTaskThumbnails(int id) { |
| 5696 | synchronized (this) { |
| 5697 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5698 | "getTaskThumbnails()"); |
| 5699 | TaskRecord tr = taskForIdLocked(id); |
| 5700 | if (tr != null) { |
| 5701 | return mMainStack.getTaskThumbnailsLocked(tr); |
| 5702 | } |
| 5703 | } |
| 5704 | return null; |
| 5705 | } |
| 5706 | |
| 5707 | public boolean removeSubTask(int taskId, int subTaskIndex) { |
| 5708 | synchronized (this) { |
| 5709 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5710 | "removeSubTask()"); |
| 5711 | long ident = Binder.clearCallingIdentity(); |
| 5712 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5713 | return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex, |
| 5714 | true) != null; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5715 | } finally { |
| 5716 | Binder.restoreCallingIdentity(ident); |
| 5717 | } |
| 5718 | } |
| 5719 | } |
| 5720 | |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5721 | private void cleanUpRemovedTaskLocked(TaskRecord tr, int flags) { |
| 5722 | final boolean killProcesses = (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5723 | Intent baseIntent = new Intent( |
| 5724 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5725 | ComponentName component = baseIntent.getComponent(); |
| 5726 | if (component == null) { |
| 5727 | Slog.w(TAG, "Now component for base intent of task: " + tr); |
| 5728 | return; |
| 5729 | } |
| 5730 | |
| 5731 | // Find any running services associated with this app. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 5732 | mServices.cleanUpRemovedTaskLocked(tr, component, baseIntent); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5733 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5734 | if (killProcesses) { |
| 5735 | // Find any running processes associated with this app. |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5736 | final String pkg = component.getPackageName(); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5737 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5738 | HashMap<String, SparseArray<ProcessRecord>> pmap = mProcessNames.getMap(); |
| 5739 | for (SparseArray<ProcessRecord> uids : pmap.values()) { |
| 5740 | for (int i=0; i<uids.size(); i++) { |
| 5741 | ProcessRecord proc = uids.valueAt(i); |
| 5742 | if (proc.userId != tr.userId) { |
| 5743 | continue; |
| 5744 | } |
| 5745 | if (!proc.pkgList.contains(pkg)) { |
| 5746 | continue; |
| 5747 | } |
| 5748 | procs.add(proc); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5749 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5750 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5751 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5752 | // Kill the running processes. |
| 5753 | for (int i=0; i<procs.size(); i++) { |
| 5754 | ProcessRecord pr = procs.get(i); |
| 5755 | if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 5756 | Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task"); |
| 5757 | EventLog.writeEvent(EventLogTags.AM_KILL, pr.pid, |
| 5758 | pr.processName, pr.setAdj, "remove task"); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5759 | pr.killedBackground = true; |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5760 | Process.killProcessQuiet(pr.pid); |
| 5761 | } else { |
| 5762 | pr.waitingToKill = "remove task"; |
| 5763 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5764 | } |
| 5765 | } |
| 5766 | } |
| 5767 | |
| 5768 | public boolean removeTask(int taskId, int flags) { |
| 5769 | synchronized (this) { |
| 5770 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5771 | "removeTask()"); |
| 5772 | long ident = Binder.clearCallingIdentity(); |
| 5773 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5774 | ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1, |
| 5775 | false); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5776 | if (r != null) { |
| 5777 | mRecentTasks.remove(r.task); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5778 | cleanUpRemovedTaskLocked(r.task, flags); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5779 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5780 | } else { |
| 5781 | TaskRecord tr = null; |
| 5782 | int i=0; |
| 5783 | while (i < mRecentTasks.size()) { |
| 5784 | TaskRecord t = mRecentTasks.get(i); |
| 5785 | if (t.taskId == taskId) { |
| 5786 | tr = t; |
| 5787 | break; |
| 5788 | } |
| 5789 | i++; |
| 5790 | } |
| 5791 | if (tr != null) { |
| 5792 | if (tr.numActivities <= 0) { |
| 5793 | // Caller is just removing a recent task that is |
| 5794 | // not actively running. That is easy! |
| 5795 | mRecentTasks.remove(i); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5796 | cleanUpRemovedTaskLocked(tr, flags); |
| 5797 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5798 | } else { |
| 5799 | Slog.w(TAG, "removeTask: task " + taskId |
| 5800 | + " does not have activities to remove, " |
| 5801 | + " but numActivities=" + tr.numActivities |
| 5802 | + ": " + tr); |
| 5803 | } |
| 5804 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5805 | } |
| 5806 | } finally { |
| 5807 | Binder.restoreCallingIdentity(ident); |
| 5808 | } |
| 5809 | } |
| 5810 | return false; |
| 5811 | } |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5812 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5813 | private final int findAffinityTaskTopLocked(int startIndex, String affinity) { |
| 5814 | int j; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5815 | TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5816 | TaskRecord jt = startTask; |
| 5817 | |
| 5818 | // First look backwards |
| 5819 | for (j=startIndex-1; j>=0; j--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5820 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5821 | if (r.task != jt) { |
| 5822 | jt = r.task; |
| 5823 | if (affinity.equals(jt.affinity)) { |
| 5824 | return j; |
| 5825 | } |
| 5826 | } |
| 5827 | } |
| 5828 | |
| 5829 | // Now look forwards |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5830 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5831 | jt = startTask; |
| 5832 | for (j=startIndex+1; j<N; j++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5833 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5834 | if (r.task != jt) { |
| 5835 | if (affinity.equals(jt.affinity)) { |
| 5836 | return j; |
| 5837 | } |
| 5838 | jt = r.task; |
| 5839 | } |
| 5840 | } |
| 5841 | |
| 5842 | // Might it be at the top? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5843 | 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] | 5844 | return N-1; |
| 5845 | } |
| 5846 | |
| 5847 | return -1; |
| 5848 | } |
| 5849 | |
| 5850 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 5851 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5852 | */ |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5853 | public void moveTaskToFront(int task, int flags, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5854 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5855 | "moveTaskToFront()"); |
| 5856 | |
| 5857 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5858 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5859 | Binder.getCallingUid(), "Task to front")) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5860 | ActivityOptions.abort(options); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5861 | return; |
| 5862 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5863 | final long origId = Binder.clearCallingIdentity(); |
| 5864 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5865 | TaskRecord tr = taskForIdLocked(task); |
| 5866 | if (tr != null) { |
| 5867 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5868 | mMainStack.mUserLeaving = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5869 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5870 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5871 | // Caller wants the home activity moved with it. To accomplish this, |
| 5872 | // we'll just move the home task to the top first. |
| 5873 | mMainStack.moveHomeToFrontLocked(); |
| 5874 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5875 | mMainStack.moveTaskToFrontLocked(tr, null, options); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5876 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5877 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5878 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 5879 | ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5880 | if (hr.task.taskId == task) { |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5881 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 5882 | mMainStack.mUserLeaving = true; |
| 5883 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 5884 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 5885 | // Caller wants the home activity moved with it. To accomplish this, |
| 5886 | // we'll just move the home task to the top first. |
| 5887 | mMainStack.moveHomeToFrontLocked(); |
| 5888 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5889 | mMainStack.moveTaskToFrontLocked(hr.task, null, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5890 | return; |
| 5891 | } |
| 5892 | } |
| 5893 | } finally { |
| 5894 | Binder.restoreCallingIdentity(origId); |
| 5895 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 5896 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5897 | } |
| 5898 | } |
| 5899 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5900 | public void moveTaskToBack(int task) { |
| 5901 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5902 | "moveTaskToBack()"); |
| 5903 | |
| 5904 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5905 | if (mMainStack.mResumedActivity != null |
| 5906 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5907 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5908 | Binder.getCallingUid(), "Task to back")) { |
| 5909 | return; |
| 5910 | } |
| 5911 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5912 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5913 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5914 | Binder.restoreCallingIdentity(origId); |
| 5915 | } |
| 5916 | } |
| 5917 | |
| 5918 | /** |
| 5919 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 5920 | * of the history stack. The activity's order within the task is unchanged. |
| 5921 | * |
| 5922 | * @param token A reference to the activity we wish to move |
| 5923 | * @param nonRoot If false then this only works if the activity is the root |
| 5924 | * of a task; if true it will work for any activity in a task. |
| 5925 | * @return Returns true if the move completed, false if not. |
| 5926 | */ |
| 5927 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5928 | enforceNotIsolatedCaller("moveActivityTaskToBack"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5929 | synchronized(this) { |
| 5930 | final long origId = Binder.clearCallingIdentity(); |
| 5931 | int taskId = getTaskForActivityLocked(token, !nonRoot); |
| 5932 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5933 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5934 | } |
| 5935 | Binder.restoreCallingIdentity(origId); |
| 5936 | } |
| 5937 | return false; |
| 5938 | } |
| 5939 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5940 | public void moveTaskBackwards(int task) { |
| 5941 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 5942 | "moveTaskBackwards()"); |
| 5943 | |
| 5944 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 5945 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 5946 | Binder.getCallingUid(), "Task backwards")) { |
| 5947 | return; |
| 5948 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5949 | final long origId = Binder.clearCallingIdentity(); |
| 5950 | moveTaskBackwardsLocked(task); |
| 5951 | Binder.restoreCallingIdentity(origId); |
| 5952 | } |
| 5953 | } |
| 5954 | |
| 5955 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5956 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5957 | } |
| 5958 | |
| 5959 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 5960 | synchronized(this) { |
| 5961 | return getTaskForActivityLocked(token, onlyRoot); |
| 5962 | } |
| 5963 | } |
| 5964 | |
| 5965 | int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5966 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5967 | TaskRecord lastTask = null; |
| 5968 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5969 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5970 | if (r.appToken == token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5971 | if (!onlyRoot || lastTask != r.task) { |
| 5972 | return r.task.taskId; |
| 5973 | } |
| 5974 | return -1; |
| 5975 | } |
| 5976 | lastTask = r.task; |
| 5977 | } |
| 5978 | |
| 5979 | return -1; |
| 5980 | } |
| 5981 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5982 | // ========================================================= |
| 5983 | // THUMBNAILS |
| 5984 | // ========================================================= |
| 5985 | |
| 5986 | public void reportThumbnail(IBinder token, |
| 5987 | Bitmap thumbnail, CharSequence description) { |
| 5988 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 5989 | final long origId = Binder.clearCallingIdentity(); |
| 5990 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 5991 | Binder.restoreCallingIdentity(origId); |
| 5992 | } |
| 5993 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5994 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5995 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 5996 | TaskRecord task = null; |
| 5997 | ArrayList receivers = null; |
| 5998 | |
| 5999 | //System.out.println("Send pending thumbnail: " + r); |
| 6000 | |
| 6001 | synchronized(this) { |
| 6002 | if (r == null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6003 | r = mMainStack.isInStackLocked(token); |
| 6004 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6005 | return; |
| 6006 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6007 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 6008 | if (thumbnail == null && r.thumbHolder != null) { |
| 6009 | thumbnail = r.thumbHolder.lastThumbnail; |
| 6010 | description = r.thumbHolder.lastDescription; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6011 | } |
| 6012 | if (thumbnail == null && !always) { |
| 6013 | // If there is no thumbnail, and this entry is not actually |
| 6014 | // going away, then abort for now and pick up the next |
| 6015 | // thumbnail we get. |
| 6016 | return; |
| 6017 | } |
| 6018 | task = r.task; |
| 6019 | |
| 6020 | int N = mPendingThumbnails.size(); |
| 6021 | int i=0; |
| 6022 | while (i<N) { |
| 6023 | PendingThumbnailsRecord pr = |
| 6024 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 6025 | //System.out.println("Looking in " + pr.pendingRecords); |
| 6026 | if (pr.pendingRecords.remove(r)) { |
| 6027 | if (receivers == null) { |
| 6028 | receivers = new ArrayList(); |
| 6029 | } |
| 6030 | receivers.add(pr); |
| 6031 | if (pr.pendingRecords.size() == 0) { |
| 6032 | pr.finished = true; |
| 6033 | mPendingThumbnails.remove(i); |
| 6034 | N--; |
| 6035 | continue; |
| 6036 | } |
| 6037 | } |
| 6038 | i++; |
| 6039 | } |
| 6040 | } |
| 6041 | |
| 6042 | if (receivers != null) { |
| 6043 | final int N = receivers.size(); |
| 6044 | for (int i=0; i<N; i++) { |
| 6045 | try { |
| 6046 | PendingThumbnailsRecord pr = |
| 6047 | (PendingThumbnailsRecord)receivers.get(i); |
| 6048 | pr.receiver.newThumbnail( |
| 6049 | task != null ? task.taskId : -1, thumbnail, description); |
| 6050 | if (pr.finished) { |
| 6051 | pr.receiver.finished(); |
| 6052 | } |
| 6053 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6054 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6055 | } |
| 6056 | } |
| 6057 | } |
| 6058 | } |
| 6059 | |
| 6060 | // ========================================================= |
| 6061 | // CONTENT PROVIDERS |
| 6062 | // ========================================================= |
| 6063 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6064 | private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) { |
| 6065 | List<ProviderInfo> providers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6066 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6067 | providers = AppGlobals.getPackageManager(). |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6068 | queryContentProviders(app.processName, app.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6069 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6070 | } catch (RemoteException ex) { |
| 6071 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6072 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6073 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid); |
| 6074 | int userId = app.userId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6075 | if (providers != null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6076 | int N = providers.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6077 | for (int i=0; i<N; i++) { |
| 6078 | ProviderInfo cpi = |
| 6079 | (ProviderInfo)providers.get(i); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6080 | boolean singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 6081 | cpi.name, cpi.flags); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6082 | if (singleton && UserHandle.getUserId(app.uid) != 0) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6083 | // This is a singleton provider, but a user besides the |
| 6084 | // default user is asking to initialize a process it runs |
| 6085 | // in... well, no, it doesn't actually run in this process, |
| 6086 | // it runs in the process of the default user. Get rid of it. |
| 6087 | providers.remove(i); |
| 6088 | N--; |
| 6089 | continue; |
| 6090 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6091 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6092 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6093 | ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6094 | if (cpr == null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6095 | cpr = new ContentProviderRecord(this, cpi, app.info, comp, singleton); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6096 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6097 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6098 | if (DEBUG_MU) |
| 6099 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6100 | app.pubProviders.put(cpi.name, cpr); |
| 6101 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 6102 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6103 | } |
| 6104 | } |
| 6105 | return providers; |
| 6106 | } |
| 6107 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 6108 | /** |
| 6109 | * Check if {@link ProcessRecord} has a possible chance at accessing the |
| 6110 | * given {@link ProviderInfo}. Final permission checking is always done |
| 6111 | * in {@link ContentProvider}. |
| 6112 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6113 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6114 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6115 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6116 | final int callingUid = (r != null) ? r.uid : Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6117 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6118 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6119 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6120 | return null; |
| 6121 | } |
| 6122 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6123 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6124 | == PackageManager.PERMISSION_GRANTED) { |
| 6125 | return null; |
| 6126 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6127 | |
| 6128 | PathPermission[] pps = cpi.pathPermissions; |
| 6129 | if (pps != null) { |
| 6130 | int i = pps.length; |
| 6131 | while (i > 0) { |
| 6132 | i--; |
| 6133 | PathPermission pp = pps[i]; |
| 6134 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6135 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6136 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6137 | return null; |
| 6138 | } |
| 6139 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6140 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6141 | == PackageManager.PERMISSION_GRANTED) { |
| 6142 | return null; |
| 6143 | } |
| 6144 | } |
| 6145 | } |
| 6146 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6147 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 6148 | if (perms != null) { |
| 6149 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 6150 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 6151 | return null; |
| 6152 | } |
| 6153 | } |
| 6154 | } |
| 6155 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6156 | String msg; |
| 6157 | if (!cpi.exported) { |
| 6158 | msg = "Permission Denial: opening provider " + cpi.name |
| 6159 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6160 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 6161 | + cpi.applicationInfo.uid; |
| 6162 | } else { |
| 6163 | msg = "Permission Denial: opening provider " + cpi.name |
| 6164 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6165 | + ", uid=" + callingUid + ") requires " |
| 6166 | + cpi.readPermission + " or " + cpi.writePermission; |
| 6167 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6168 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6169 | return msg; |
| 6170 | } |
| 6171 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6172 | ContentProviderConnection incProviderCountLocked(ProcessRecord r, |
| 6173 | final ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6174 | if (r != null) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6175 | for (int i=0; i<r.conProviders.size(); i++) { |
| 6176 | ContentProviderConnection conn = r.conProviders.get(i); |
| 6177 | if (conn.provider == cpr) { |
| 6178 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6179 | "Adding provider requested by " |
| 6180 | + r.processName + " from process " |
| 6181 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 6182 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6183 | if (stable) { |
| 6184 | conn.stableCount++; |
| 6185 | conn.numStableIncs++; |
| 6186 | } else { |
| 6187 | conn.unstableCount++; |
| 6188 | conn.numUnstableIncs++; |
| 6189 | } |
| 6190 | return conn; |
| 6191 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6192 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6193 | ContentProviderConnection conn = new ContentProviderConnection(cpr, r); |
| 6194 | if (stable) { |
| 6195 | conn.stableCount = 1; |
| 6196 | conn.numStableIncs = 1; |
| 6197 | } else { |
| 6198 | conn.unstableCount = 1; |
| 6199 | conn.numUnstableIncs = 1; |
| 6200 | } |
| 6201 | cpr.connections.add(conn); |
| 6202 | r.conProviders.add(conn); |
| 6203 | return conn; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6204 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6205 | cpr.addExternalProcessHandleLocked(externalProcessToken); |
| 6206 | return null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6207 | } |
| 6208 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6209 | boolean decProviderCountLocked(ContentProviderConnection conn, |
| 6210 | ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
| 6211 | if (conn != null) { |
| 6212 | cpr = conn.provider; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6213 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6214 | "Removing provider requested by " |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6215 | + conn.client.processName + " from process " |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6216 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6217 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6218 | if (stable) { |
| 6219 | conn.stableCount--; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6220 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6221 | conn.unstableCount--; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6222 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6223 | if (conn.stableCount == 0 && conn.unstableCount == 0) { |
| 6224 | cpr.connections.remove(conn); |
| 6225 | conn.client.conProviders.remove(conn); |
| 6226 | return true; |
| 6227 | } |
| 6228 | return false; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6229 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6230 | cpr.removeExternalProcessHandleLocked(externalProcessToken); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6231 | return false; |
| 6232 | } |
| 6233 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6234 | private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6235 | String name, IBinder token, boolean stable, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6236 | ContentProviderRecord cpr; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6237 | ContentProviderConnection conn = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6238 | ProviderInfo cpi = null; |
| 6239 | |
| 6240 | synchronized(this) { |
| 6241 | ProcessRecord r = null; |
| 6242 | if (caller != null) { |
| 6243 | r = getRecordForAppLocked(caller); |
| 6244 | if (r == null) { |
| 6245 | throw new SecurityException( |
| 6246 | "Unable to find app for caller " + caller |
| 6247 | + " (pid=" + Binder.getCallingPid() |
| 6248 | + ") when getting content provider " + name); |
| 6249 | } |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6250 | if (r.userId != userId) { |
| 6251 | throw new SecurityException("Calling requested user " + userId |
| 6252 | + " but app is user " + r.userId); |
| 6253 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6254 | } |
| 6255 | |
| 6256 | // First check if this content provider has been published... |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6257 | cpr = mProviderMap.getProviderByName(name, userId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6258 | boolean providerRunning = cpr != null; |
| 6259 | if (providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6260 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6261 | String msg; |
| 6262 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6263 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6264 | } |
| 6265 | |
| 6266 | if (r != null && cpr.canRunHere(r)) { |
| 6267 | // This provider has been published or is in the process |
| 6268 | // of being published... but it is also allowed to run |
| 6269 | // in the caller's process, so don't make a connection |
| 6270 | // and just let the caller instantiate its own instance. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6271 | ContentProviderHolder holder = cpr.newHolder(null); |
| 6272 | // don't give caller the provider object, it needs |
| 6273 | // to make its own. |
| 6274 | holder.provider = null; |
| 6275 | return holder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6276 | } |
| 6277 | |
| 6278 | final long origId = Binder.clearCallingIdentity(); |
| 6279 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6280 | // 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] | 6281 | // return it right away. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6282 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6283 | if (conn != null && (conn.stableCount+conn.unstableCount) == 1) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6284 | if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 6285 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6286 | // make sure to count it as being accessed and thus |
| 6287 | // back up on the LRU list. This is good because |
| 6288 | // content providers are often expensive to start. |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6289 | updateLruProcessLocked(cpr.proc, false, true); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6290 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 6291 | } |
| 6292 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6293 | if (cpr.proc != null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6294 | if (false) { |
| 6295 | if (cpr.name.flattenToShortString().equals( |
| 6296 | "com.android.providers.calendar/.CalendarProvider2")) { |
| 6297 | Slog.v(TAG, "****************** KILLING " |
| 6298 | + cpr.name.flattenToShortString()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6299 | Process.killProcess(cpr.proc.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6300 | } |
| 6301 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6302 | boolean success = updateOomAdjLocked(cpr.proc); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6303 | if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success); |
| 6304 | // NOTE: there is still a race here where a signal could be |
| 6305 | // pending on the process even though we managed to update its |
| 6306 | // adj level. Not sure what to do about this, but at least |
| 6307 | // the race is now smaller. |
| 6308 | if (!success) { |
| 6309 | // Uh oh... it looks like the provider's process |
| 6310 | // has been killed on us. We need to wait for a new |
| 6311 | // process to be started, and make sure its death |
| 6312 | // doesn't kill our process. |
| 6313 | Slog.i(TAG, |
| 6314 | "Existing provider " + cpr.name.flattenToShortString() |
| 6315 | + " is crashing; detaching " + r); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6316 | boolean lastRef = decProviderCountLocked(conn, cpr, token, stable); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6317 | appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6318 | if (!lastRef) { |
| 6319 | // This wasn't the last ref our process had on |
| 6320 | // the provider... we have now been killed, bail. |
| 6321 | return null; |
| 6322 | } |
| 6323 | providerRunning = false; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6324 | conn = null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6325 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6326 | } |
| 6327 | |
| 6328 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6329 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6330 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6331 | boolean singleton; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6332 | if (!providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6333 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6334 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6335 | resolveContentProvider(name, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6336 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6337 | } catch (RemoteException ex) { |
| 6338 | } |
| 6339 | if (cpi == null) { |
| 6340 | return null; |
| 6341 | } |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6342 | singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 6343 | cpi.name, cpi.flags); |
| 6344 | if (singleton) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 6345 | userId = 0; |
| 6346 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6347 | cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6348 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6349 | String msg; |
| 6350 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6351 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6352 | } |
| 6353 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 6354 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6355 | && !cpi.processName.equals("system")) { |
| 6356 | // If this content provider does not run in the system |
| 6357 | // process, and the system is not yet ready to run other |
| 6358 | // processes, then fail fast instead of hanging. |
| 6359 | throw new IllegalArgumentException( |
| 6360 | "Attempt to launch content provider before system ready"); |
| 6361 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6362 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 6363 | // Make sure that the user who owns this provider is started. If not, |
| 6364 | // we don't want to allow it to run. |
| 6365 | if (mStartedUsers.get(userId) == null) { |
| 6366 | Slog.w(TAG, "Unable to launch app " |
| 6367 | + cpi.applicationInfo.packageName + "/" |
| 6368 | + cpi.applicationInfo.uid + " for provider " |
| 6369 | + name + ": user " + userId + " is stopped"); |
| 6370 | return null; |
| 6371 | } |
| 6372 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6373 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6374 | cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6375 | final boolean firstClass = cpr == null; |
| 6376 | if (firstClass) { |
| 6377 | try { |
| 6378 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6379 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6380 | getApplicationInfo( |
| 6381 | cpi.applicationInfo.packageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6382 | STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6383 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6384 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6385 | + cpi.name); |
| 6386 | return null; |
| 6387 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6388 | ai = getAppInfoForUser(ai, userId); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6389 | cpr = new ContentProviderRecord(this, cpi, ai, comp, singleton); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6390 | } catch (RemoteException ex) { |
| 6391 | // pm is in same process, this will never happen. |
| 6392 | } |
| 6393 | } |
| 6394 | |
| 6395 | if (r != null && cpr.canRunHere(r)) { |
| 6396 | // If this is a multiprocess provider, then just return its |
| 6397 | // info and allow the caller to instantiate it. Only do |
| 6398 | // this if the provider is the same user as the caller's |
| 6399 | // process, or can run as root (so can be in any process). |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6400 | return cpr.newHolder(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6401 | } |
| 6402 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6403 | if (DEBUG_PROVIDER) { |
| 6404 | RuntimeException e = new RuntimeException("here"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6405 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6406 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6407 | } |
| 6408 | |
| 6409 | // This is single process, and our app is now connecting to it. |
| 6410 | // See if we are already in the process of launching this |
| 6411 | // provider. |
| 6412 | final int N = mLaunchingProviders.size(); |
| 6413 | int i; |
| 6414 | for (i=0; i<N; i++) { |
| 6415 | if (mLaunchingProviders.get(i) == cpr) { |
| 6416 | break; |
| 6417 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6418 | } |
| 6419 | |
| 6420 | // If the provider is not already being launched, then get it |
| 6421 | // started. |
| 6422 | if (i >= N) { |
| 6423 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6424 | |
| 6425 | try { |
| 6426 | // Content provider is now in use, its package can't be stopped. |
| 6427 | try { |
| 6428 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6429 | cpr.appInfo.packageName, false, userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6430 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6431 | } catch (IllegalArgumentException e) { |
| 6432 | Slog.w(TAG, "Failed trying to unstop package " |
| 6433 | + cpr.appInfo.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6434 | } |
| 6435 | |
| 6436 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 6437 | cpr.appInfo, false, 0, "content provider", |
| 6438 | new ComponentName(cpi.applicationInfo.packageName, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6439 | cpi.name), false, false); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6440 | if (proc == null) { |
| 6441 | Slog.w(TAG, "Unable to launch app " |
| 6442 | + cpi.applicationInfo.packageName + "/" |
| 6443 | + cpi.applicationInfo.uid + " for provider " |
| 6444 | + name + ": process is bad"); |
| 6445 | return null; |
| 6446 | } |
| 6447 | cpr.launchingApp = proc; |
| 6448 | mLaunchingProviders.add(cpr); |
| 6449 | } finally { |
| 6450 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6451 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6452 | } |
| 6453 | |
| 6454 | // Make sure the provider is published (the same provider class |
| 6455 | // may be published under multiple names). |
| 6456 | if (firstClass) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6457 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6458 | } |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6459 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6460 | mProviderMap.putProviderByName(name, cpr); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6461 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6462 | if (conn != null) { |
| 6463 | conn.waiting = true; |
| 6464 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6465 | } |
| 6466 | } |
| 6467 | |
| 6468 | // Wait for the provider to be published... |
| 6469 | synchronized (cpr) { |
| 6470 | while (cpr.provider == null) { |
| 6471 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6472 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6473 | + cpi.applicationInfo.packageName + "/" |
| 6474 | + cpi.applicationInfo.uid + " for provider " |
| 6475 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6476 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6477 | cpi.applicationInfo.packageName, |
| 6478 | cpi.applicationInfo.uid, name); |
| 6479 | return null; |
| 6480 | } |
| 6481 | try { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6482 | if (DEBUG_MU) { |
| 6483 | Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp=" |
| 6484 | + cpr.launchingApp); |
| 6485 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6486 | if (conn != null) { |
| 6487 | conn.waiting = true; |
| 6488 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6489 | cpr.wait(); |
| 6490 | } catch (InterruptedException ex) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6491 | } finally { |
| 6492 | if (conn != null) { |
| 6493 | conn.waiting = false; |
| 6494 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6495 | } |
| 6496 | } |
| 6497 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6498 | return cpr != null ? cpr.newHolder(conn) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | public final ContentProviderHolder getContentProvider( |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6502 | IApplicationThread caller, String name, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6503 | enforceNotIsolatedCaller("getContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6504 | if (caller == null) { |
| 6505 | String msg = "null IApplicationThread when getting content provider " |
| 6506 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6507 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6508 | throw new SecurityException(msg); |
| 6509 | } |
| 6510 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6511 | return getContentProviderImpl(caller, name, null, stable, |
| 6512 | UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6513 | } |
| 6514 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6515 | public ContentProviderHolder getContentProviderExternal(String name, IBinder token) { |
| 6516 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6517 | "Do not have permission in call getContentProviderExternal()"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6518 | return getContentProviderExternalUnchecked(name, token, UserHandle.getCallingUserId()); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6519 | } |
| 6520 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6521 | private ContentProviderHolder getContentProviderExternalUnchecked(String name, |
| 6522 | IBinder token, int userId) { |
| 6523 | return getContentProviderImpl(null, name, token, true, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6524 | } |
| 6525 | |
| 6526 | /** |
| 6527 | * Drop a content provider from a ProcessRecord's bookkeeping |
| 6528 | * @param cpr |
| 6529 | */ |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6530 | public void removeContentProvider(IBinder connection, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6531 | enforceNotIsolatedCaller("removeContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6532 | synchronized (this) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6533 | ContentProviderConnection conn; |
| 6534 | try { |
| 6535 | conn = (ContentProviderConnection)connection; |
| 6536 | } catch (ClassCastException e) { |
| 6537 | String msg ="removeContentProvider: " + connection |
| 6538 | + " not a ContentProviderConnection"; |
| 6539 | Slog.w(TAG, msg); |
| 6540 | throw new IllegalArgumentException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6541 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6542 | if (conn == null) { |
| 6543 | throw new NullPointerException("connection is null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6544 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6545 | if (decProviderCountLocked(conn, null, null, stable)) { |
| 6546 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6547 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6548 | } |
| 6549 | } |
| 6550 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6551 | public void removeContentProviderExternal(String name, IBinder token) { |
| 6552 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6553 | "Do not have permission in call removeContentProviderExternal()"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6554 | removeContentProviderExternalUnchecked(name, token, UserHandle.getCallingUserId()); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6555 | } |
| 6556 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6557 | private void removeContentProviderExternalUnchecked(String name, IBinder token, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6558 | synchronized (this) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6559 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6560 | if(cpr == null) { |
| 6561 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6562 | 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] | 6563 | return; |
| 6564 | } |
| 6565 | |
| 6566 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6567 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6568 | ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, userId); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6569 | if (localCpr.hasExternalProcessHandles()) { |
| 6570 | if (localCpr.removeExternalProcessHandleLocked(token)) { |
| 6571 | updateOomAdjLocked(); |
| 6572 | } else { |
| 6573 | Slog.e(TAG, "Attmpt to remove content provider " + localCpr |
| 6574 | + " with no external reference for token: " |
| 6575 | + token + "."); |
| 6576 | } |
| 6577 | } else { |
| 6578 | Slog.e(TAG, "Attmpt to remove content provider: " + localCpr |
| 6579 | + " with no external references."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6580 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6581 | } |
| 6582 | } |
| 6583 | |
| 6584 | public final void publishContentProviders(IApplicationThread caller, |
| 6585 | List<ContentProviderHolder> providers) { |
| 6586 | if (providers == null) { |
| 6587 | return; |
| 6588 | } |
| 6589 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6590 | enforceNotIsolatedCaller("publishContentProviders"); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6591 | synchronized (this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6592 | final ProcessRecord r = getRecordForAppLocked(caller); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6593 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6594 | Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6595 | if (r == null) { |
| 6596 | throw new SecurityException( |
| 6597 | "Unable to find app for caller " + caller |
| 6598 | + " (pid=" + Binder.getCallingPid() |
| 6599 | + ") when publishing content providers"); |
| 6600 | } |
| 6601 | |
| 6602 | final long origId = Binder.clearCallingIdentity(); |
| 6603 | |
| 6604 | final int N = providers.size(); |
| 6605 | for (int i=0; i<N; i++) { |
| 6606 | ContentProviderHolder src = providers.get(i); |
| 6607 | if (src == null || src.info == null || src.provider == null) { |
| 6608 | continue; |
| 6609 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6610 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6611 | if (DEBUG_MU) |
| 6612 | Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6613 | if (dst != null) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6614 | ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6615 | mProviderMap.putProviderByClass(comp, dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6616 | String names[] = dst.info.authority.split(";"); |
| 6617 | for (int j = 0; j < names.length; j++) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6618 | mProviderMap.putProviderByName(names[j], dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6619 | } |
| 6620 | |
| 6621 | int NL = mLaunchingProviders.size(); |
| 6622 | int j; |
| 6623 | for (j=0; j<NL; j++) { |
| 6624 | if (mLaunchingProviders.get(j) == dst) { |
| 6625 | mLaunchingProviders.remove(j); |
| 6626 | j--; |
| 6627 | NL--; |
| 6628 | } |
| 6629 | } |
| 6630 | synchronized (dst) { |
| 6631 | dst.provider = src.provider; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6632 | dst.proc = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6633 | dst.notifyAll(); |
| 6634 | } |
| 6635 | updateOomAdjLocked(r); |
| 6636 | } |
| 6637 | } |
| 6638 | |
| 6639 | Binder.restoreCallingIdentity(origId); |
| 6640 | } |
| 6641 | } |
| 6642 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6643 | public boolean refContentProvider(IBinder connection, int stable, int unstable) { |
| 6644 | ContentProviderConnection conn; |
| 6645 | try { |
| 6646 | conn = (ContentProviderConnection)connection; |
| 6647 | } catch (ClassCastException e) { |
| 6648 | String msg ="refContentProvider: " + connection |
| 6649 | + " not a ContentProviderConnection"; |
| 6650 | Slog.w(TAG, msg); |
| 6651 | throw new IllegalArgumentException(msg); |
| 6652 | } |
| 6653 | if (conn == null) { |
| 6654 | throw new NullPointerException("connection is null"); |
| 6655 | } |
| 6656 | |
| 6657 | synchronized (this) { |
| 6658 | if (stable > 0) { |
| 6659 | conn.numStableIncs += stable; |
| 6660 | } |
| 6661 | stable = conn.stableCount + stable; |
| 6662 | if (stable < 0) { |
| 6663 | throw new IllegalStateException("stableCount < 0: " + stable); |
| 6664 | } |
| 6665 | |
| 6666 | if (unstable > 0) { |
| 6667 | conn.numUnstableIncs += unstable; |
| 6668 | } |
| 6669 | unstable = conn.unstableCount + unstable; |
| 6670 | if (unstable < 0) { |
| 6671 | throw new IllegalStateException("unstableCount < 0: " + unstable); |
| 6672 | } |
| 6673 | |
| 6674 | if ((stable+unstable) <= 0) { |
| 6675 | throw new IllegalStateException("ref counts can't go to zero here: stable=" |
| 6676 | + stable + " unstable=" + unstable); |
| 6677 | } |
| 6678 | conn.stableCount = stable; |
| 6679 | conn.unstableCount = unstable; |
| 6680 | return !conn.dead; |
| 6681 | } |
| 6682 | } |
| 6683 | |
| 6684 | public void unstableProviderDied(IBinder connection) { |
| 6685 | ContentProviderConnection conn; |
| 6686 | try { |
| 6687 | conn = (ContentProviderConnection)connection; |
| 6688 | } catch (ClassCastException e) { |
| 6689 | String msg ="refContentProvider: " + connection |
| 6690 | + " not a ContentProviderConnection"; |
| 6691 | Slog.w(TAG, msg); |
| 6692 | throw new IllegalArgumentException(msg); |
| 6693 | } |
| 6694 | if (conn == null) { |
| 6695 | throw new NullPointerException("connection is null"); |
| 6696 | } |
| 6697 | |
| 6698 | // Safely retrieve the content provider associated with the connection. |
| 6699 | IContentProvider provider; |
| 6700 | synchronized (this) { |
| 6701 | provider = conn.provider.provider; |
| 6702 | } |
| 6703 | |
| 6704 | if (provider == null) { |
| 6705 | // Um, yeah, we're way ahead of you. |
| 6706 | return; |
| 6707 | } |
| 6708 | |
| 6709 | // Make sure the caller is being honest with us. |
| 6710 | if (provider.asBinder().pingBinder()) { |
| 6711 | // Er, no, still looks good to us. |
| 6712 | synchronized (this) { |
| 6713 | Slog.w(TAG, "unstableProviderDied: caller " + Binder.getCallingUid() |
| 6714 | + " says " + conn + " died, but we don't agree"); |
| 6715 | return; |
| 6716 | } |
| 6717 | } |
| 6718 | |
| 6719 | // Well look at that! It's dead! |
| 6720 | synchronized (this) { |
| 6721 | if (conn.provider.provider != provider) { |
| 6722 | // But something changed... good enough. |
| 6723 | return; |
| 6724 | } |
| 6725 | |
| 6726 | ProcessRecord proc = conn.provider.proc; |
| 6727 | if (proc == null || proc.thread == null) { |
| 6728 | // Seems like the process is already cleaned up. |
| 6729 | return; |
| 6730 | } |
| 6731 | |
| 6732 | // As far as we're concerned, this is just like receiving a |
| 6733 | // death notification... just a bit prematurely. |
| 6734 | Slog.i(TAG, "Process " + proc.processName + " (pid " + proc.pid |
| 6735 | + ") early provider death"); |
Dianne Hackborn | bd145db | 2012-06-05 16:20:46 -0700 | [diff] [blame] | 6736 | final long ident = Binder.clearCallingIdentity(); |
| 6737 | try { |
| 6738 | appDiedLocked(proc, proc.pid, proc.thread); |
| 6739 | } finally { |
| 6740 | Binder.restoreCallingIdentity(ident); |
| 6741 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6742 | } |
| 6743 | } |
| 6744 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6745 | public static final void installSystemProviders() { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6746 | List<ProviderInfo> providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6747 | synchronized (mSelf) { |
| 6748 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 6749 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 6750 | if (providers != null) { |
| 6751 | for (int i=providers.size()-1; i>=0; i--) { |
| 6752 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 6753 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6754 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 6755 | + ": not system .apk"); |
| 6756 | providers.remove(i); |
| 6757 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6758 | } |
| 6759 | } |
| 6760 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6761 | if (providers != null) { |
| 6762 | mSystemThread.installSystemProviders(providers); |
| 6763 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 6764 | |
| 6765 | mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 6766 | |
| 6767 | mSelf.mUsageStatsService.monitorPackages(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6768 | } |
| 6769 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6770 | /** |
| 6771 | * Allows app to retrieve the MIME type of a URI without having permission |
| 6772 | * to access its content provider. |
| 6773 | * |
| 6774 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 6775 | * |
| 6776 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 6777 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 6778 | */ |
| 6779 | public String getProviderMimeType(Uri uri) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6780 | enforceNotIsolatedCaller("getProviderMimeType"); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6781 | final String name = uri.getAuthority(); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6782 | final int userId = UserHandle.getCallingUserId(); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6783 | final long ident = Binder.clearCallingIdentity(); |
| 6784 | ContentProviderHolder holder = null; |
| 6785 | |
| 6786 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6787 | holder = getContentProviderExternalUnchecked(name, null, userId); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6788 | if (holder != null) { |
| 6789 | return holder.provider.getType(uri); |
| 6790 | } |
| 6791 | } catch (RemoteException e) { |
| 6792 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 6793 | return null; |
| 6794 | } finally { |
| 6795 | if (holder != null) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6796 | removeContentProviderExternalUnchecked(name, null, userId); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6797 | } |
| 6798 | Binder.restoreCallingIdentity(ident); |
| 6799 | } |
| 6800 | |
| 6801 | return null; |
| 6802 | } |
| 6803 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6804 | // ========================================================= |
| 6805 | // GLOBAL MANAGEMENT |
| 6806 | // ========================================================= |
| 6807 | |
| 6808 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6809 | ApplicationInfo info, String customProcess, boolean isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6810 | String proc = customProcess != null ? customProcess : info.processName; |
| 6811 | BatteryStatsImpl.Uid.Proc ps = null; |
| 6812 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6813 | int uid = info.uid; |
| 6814 | if (isolated) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6815 | int userId = UserHandle.getUserId(uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6816 | int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1; |
| 6817 | uid = 0; |
| 6818 | while (true) { |
| 6819 | if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID |
| 6820 | || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) { |
| 6821 | mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID; |
| 6822 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6823 | uid = UserHandle.getUid(userId, mNextIsolatedProcessUid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6824 | mNextIsolatedProcessUid++; |
| 6825 | if (mIsolatedProcesses.indexOfKey(uid) < 0) { |
| 6826 | // No process for this uid, use it. |
| 6827 | break; |
| 6828 | } |
| 6829 | stepsLeft--; |
| 6830 | if (stepsLeft <= 0) { |
| 6831 | return null; |
| 6832 | } |
| 6833 | } |
| 6834 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6835 | synchronized (stats) { |
| 6836 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 6837 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6838 | return new ProcessRecord(ps, thread, info, proc, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6839 | } |
| 6840 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6841 | final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated) { |
| 6842 | ProcessRecord app; |
| 6843 | if (!isolated) { |
| 6844 | app = getProcessRecordLocked(info.processName, info.uid); |
| 6845 | } else { |
| 6846 | app = null; |
| 6847 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6848 | |
| 6849 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6850 | app = newProcessRecordLocked(null, info, null, isolated); |
| 6851 | mProcessNames.put(info.processName, app.uid, app); |
| 6852 | if (isolated) { |
| 6853 | mIsolatedProcesses.put(app.uid, app); |
| 6854 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 6855 | updateLruProcessLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6856 | } |
| 6857 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6858 | // This package really, really can not be stopped. |
| 6859 | try { |
| 6860 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6861 | info.packageName, false, UserHandle.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6862 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6863 | } catch (IllegalArgumentException e) { |
| 6864 | Slog.w(TAG, "Failed trying to unstop package " |
| 6865 | + info.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6866 | } |
| 6867 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6868 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 6869 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 6870 | app.persistent = true; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 6871 | app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6872 | } |
| 6873 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 6874 | mPersistentStartingProcesses.add(app); |
| 6875 | startProcessLocked(app, "added application", app.processName); |
| 6876 | } |
| 6877 | |
| 6878 | return app; |
| 6879 | } |
| 6880 | |
| 6881 | public void unhandledBack() { |
| 6882 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 6883 | "unhandledBack()"); |
| 6884 | |
| 6885 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6886 | int count = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6887 | if (DEBUG_SWITCH) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6888 | TAG, "Performing unhandledBack(): stack size = " + count); |
| 6889 | if (count > 1) { |
| 6890 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6891 | mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6892 | count-1, Activity.RESULT_CANCELED, null, "unhandled-back"); |
| 6893 | Binder.restoreCallingIdentity(origId); |
| 6894 | } |
| 6895 | } |
| 6896 | } |
| 6897 | |
| 6898 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6899 | enforceNotIsolatedCaller("openContentUri"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6900 | final int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6901 | String name = uri.getAuthority(); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6902 | ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6903 | ParcelFileDescriptor pfd = null; |
| 6904 | if (cph != null) { |
| 6905 | // We record the binder invoker's uid in thread-local storage before |
| 6906 | // going to the content provider to open the file. Later, in the code |
| 6907 | // that handles all permissions checks, we look for this uid and use |
| 6908 | // that rather than the Activity Manager's own uid. The effect is that |
| 6909 | // we do the check against the caller's permissions even though it looks |
| 6910 | // to the content provider like the Activity Manager itself is making |
| 6911 | // the request. |
| 6912 | sCallerIdentity.set(new Identity( |
| 6913 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 6914 | try { |
| 6915 | pfd = cph.provider.openFile(uri, "r"); |
| 6916 | } catch (FileNotFoundException e) { |
| 6917 | // do nothing; pfd will be returned null |
| 6918 | } finally { |
| 6919 | // Ensure that whatever happens, we clean up the identity state |
| 6920 | sCallerIdentity.remove(); |
| 6921 | } |
| 6922 | |
| 6923 | // We've got the fd now, so we're done with the provider. |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6924 | removeContentProviderExternalUnchecked(name, null, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6925 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6926 | 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] | 6927 | } |
| 6928 | return pfd; |
| 6929 | } |
| 6930 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6931 | // Actually is sleeping or shutting down or whatever else in the future |
| 6932 | // is an inactive state. |
| 6933 | public boolean isSleeping() { |
| 6934 | return mSleeping || mShuttingDown; |
| 6935 | } |
| 6936 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6937 | public void goingToSleep() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6938 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 6939 | != PackageManager.PERMISSION_GRANTED) { |
| 6940 | throw new SecurityException("Requires permission " |
| 6941 | + android.Manifest.permission.DEVICE_POWER); |
| 6942 | } |
| 6943 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6944 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6945 | mWentToSleep = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 6946 | updateEventDispatchingLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6947 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6948 | if (!mSleeping) { |
| 6949 | mSleeping = true; |
| 6950 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 6951 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 6952 | // Initialize the wake times of all processes. |
| 6953 | checkExcessivePowerUsageLocked(false); |
| 6954 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 6955 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 6956 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
| 6957 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6958 | } |
| 6959 | } |
| 6960 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6961 | public boolean shutdown(int timeout) { |
| 6962 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 6963 | != PackageManager.PERMISSION_GRANTED) { |
| 6964 | throw new SecurityException("Requires permission " |
| 6965 | + android.Manifest.permission.SHUTDOWN); |
| 6966 | } |
| 6967 | |
| 6968 | boolean timedout = false; |
| 6969 | |
| 6970 | synchronized(this) { |
| 6971 | mShuttingDown = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 6972 | updateEventDispatchingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6973 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6974 | if (mMainStack.mResumedActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6975 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6976 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6977 | while (mMainStack.mResumedActivity != null |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 6978 | || mMainStack.mPausingActivity != null) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6979 | long delay = endTime - System.currentTimeMillis(); |
| 6980 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6981 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 6982 | timedout = true; |
| 6983 | break; |
| 6984 | } |
| 6985 | try { |
| 6986 | this.wait(); |
| 6987 | } catch (InterruptedException e) { |
| 6988 | } |
| 6989 | } |
| 6990 | } |
| 6991 | } |
| 6992 | |
| 6993 | mUsageStatsService.shutdown(); |
| 6994 | mBatteryStatsService.shutdown(); |
| 6995 | |
| 6996 | return timedout; |
| 6997 | } |
| 6998 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 6999 | public final void activitySlept(IBinder token) { |
| 7000 | if (localLOGV) Slog.v( |
| 7001 | TAG, "Activity slept: token=" + token); |
| 7002 | |
| 7003 | ActivityRecord r = null; |
| 7004 | |
| 7005 | final long origId = Binder.clearCallingIdentity(); |
| 7006 | |
| 7007 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7008 | r = mMainStack.isInStackLocked(token); |
| 7009 | if (r != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7010 | mMainStack.activitySleptLocked(r); |
| 7011 | } |
| 7012 | } |
| 7013 | |
| 7014 | Binder.restoreCallingIdentity(origId); |
| 7015 | } |
| 7016 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7017 | private void comeOutOfSleepIfNeededLocked() { |
| 7018 | if (!mWentToSleep && !mLockScreenShown) { |
| 7019 | if (mSleeping) { |
| 7020 | mSleeping = false; |
| 7021 | mMainStack.awakeFromSleepingLocked(); |
| 7022 | mMainStack.resumeTopActivityLocked(null); |
| 7023 | } |
| 7024 | } |
| 7025 | } |
| 7026 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7027 | public void wakingUp() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7028 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7029 | != PackageManager.PERMISSION_GRANTED) { |
| 7030 | throw new SecurityException("Requires permission " |
| 7031 | + android.Manifest.permission.DEVICE_POWER); |
| 7032 | } |
| 7033 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7034 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7035 | mWentToSleep = false; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7036 | updateEventDispatchingLocked(); |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7037 | comeOutOfSleepIfNeededLocked(); |
| 7038 | } |
| 7039 | } |
| 7040 | |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7041 | private void updateEventDispatchingLocked() { |
| 7042 | mWindowManager.setEventDispatching(mBooted && !mWentToSleep && !mShuttingDown); |
| 7043 | } |
| 7044 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7045 | public void setLockScreenShown(boolean shown) { |
| 7046 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7047 | != PackageManager.PERMISSION_GRANTED) { |
| 7048 | throw new SecurityException("Requires permission " |
| 7049 | + android.Manifest.permission.DEVICE_POWER); |
| 7050 | } |
| 7051 | |
| 7052 | synchronized(this) { |
| 7053 | mLockScreenShown = shown; |
| 7054 | comeOutOfSleepIfNeededLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7055 | } |
| 7056 | } |
| 7057 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7058 | public void stopAppSwitches() { |
| 7059 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 7060 | != PackageManager.PERMISSION_GRANTED) { |
| 7061 | throw new SecurityException("Requires permission " |
| 7062 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 7063 | } |
| 7064 | |
| 7065 | synchronized(this) { |
| 7066 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 7067 | + APP_SWITCH_DELAY_TIME; |
| 7068 | mDidAppSwitch = false; |
| 7069 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 7070 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 7071 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 7072 | } |
| 7073 | } |
| 7074 | |
| 7075 | public void resumeAppSwitches() { |
| 7076 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 7077 | != PackageManager.PERMISSION_GRANTED) { |
| 7078 | throw new SecurityException("Requires permission " |
| 7079 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 7080 | } |
| 7081 | |
| 7082 | synchronized(this) { |
| 7083 | // Note that we don't execute any pending app switches... we will |
| 7084 | // let those wait until either the timeout, or the next start |
| 7085 | // activity request. |
| 7086 | mAppSwitchesAllowedTime = 0; |
| 7087 | } |
| 7088 | } |
| 7089 | |
| 7090 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 7091 | String name) { |
| 7092 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 7093 | return true; |
| 7094 | } |
| 7095 | |
| 7096 | final int perm = checkComponentPermission( |
| 7097 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 7098 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7099 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 7100 | return true; |
| 7101 | } |
| 7102 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7103 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7104 | return false; |
| 7105 | } |
| 7106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7107 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 7108 | boolean persistent) { |
| 7109 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 7110 | "setDebugApp()"); |
| 7111 | |
| 7112 | // Note that this is not really thread safe if there are multiple |
| 7113 | // callers into it at the same time, but that's not a situation we |
| 7114 | // care about. |
| 7115 | if (persistent) { |
| 7116 | final ContentResolver resolver = mContext.getContentResolver(); |
| 7117 | Settings.System.putString( |
| 7118 | resolver, Settings.System.DEBUG_APP, |
| 7119 | packageName); |
| 7120 | Settings.System.putInt( |
| 7121 | resolver, Settings.System.WAIT_FOR_DEBUGGER, |
| 7122 | waitForDebugger ? 1 : 0); |
| 7123 | } |
| 7124 | |
| 7125 | synchronized (this) { |
| 7126 | if (!persistent) { |
| 7127 | mOrigDebugApp = mDebugApp; |
| 7128 | mOrigWaitForDebugger = mWaitForDebugger; |
| 7129 | } |
| 7130 | mDebugApp = packageName; |
| 7131 | mWaitForDebugger = waitForDebugger; |
| 7132 | mDebugTransient = !persistent; |
| 7133 | if (packageName != null) { |
| 7134 | final long origId = Binder.clearCallingIdentity(); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7135 | forceStopPackageLocked(packageName, -1, false, false, true, true, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7136 | Binder.restoreCallingIdentity(origId); |
| 7137 | } |
| 7138 | } |
| 7139 | } |
| 7140 | |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 7141 | void setOpenGlTraceApp(ApplicationInfo app, String processName) { |
| 7142 | synchronized (this) { |
| 7143 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 7144 | if (!isDebuggable) { |
| 7145 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 7146 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 7147 | } |
| 7148 | } |
| 7149 | |
| 7150 | mOpenGlTraceApp = processName; |
| 7151 | } |
| 7152 | } |
| 7153 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 7154 | void setProfileApp(ApplicationInfo app, String processName, String profileFile, |
| 7155 | ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
| 7156 | synchronized (this) { |
| 7157 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 7158 | if (!isDebuggable) { |
| 7159 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 7160 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 7161 | } |
| 7162 | } |
| 7163 | mProfileApp = processName; |
| 7164 | mProfileFile = profileFile; |
| 7165 | if (mProfileFd != null) { |
| 7166 | try { |
| 7167 | mProfileFd.close(); |
| 7168 | } catch (IOException e) { |
| 7169 | } |
| 7170 | mProfileFd = null; |
| 7171 | } |
| 7172 | mProfileFd = profileFd; |
| 7173 | mProfileType = 0; |
| 7174 | mAutoStopProfiler = autoStopProfiler; |
| 7175 | } |
| 7176 | } |
| 7177 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7178 | public void setAlwaysFinish(boolean enabled) { |
| 7179 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 7180 | "setAlwaysFinish()"); |
| 7181 | |
| 7182 | Settings.System.putInt( |
| 7183 | mContext.getContentResolver(), |
| 7184 | Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
| 7185 | |
| 7186 | synchronized (this) { |
| 7187 | mAlwaysFinishActivities = enabled; |
| 7188 | } |
| 7189 | } |
| 7190 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7191 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7192 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7193 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7194 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7195 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7196 | } |
| 7197 | } |
| 7198 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 7199 | public boolean isUserAMonkey() { |
| 7200 | // For now the fact that there is a controller implies |
| 7201 | // we have a monkey. |
| 7202 | synchronized (this) { |
| 7203 | return mController != null; |
| 7204 | } |
| 7205 | } |
| 7206 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7207 | public void registerProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7208 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
| 7209 | "registerProcessObserver()"); |
| 7210 | synchronized (this) { |
| 7211 | mProcessObservers.register(observer); |
| 7212 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7213 | } |
| 7214 | |
| 7215 | public void unregisterProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7216 | synchronized (this) { |
| 7217 | mProcessObservers.unregister(observer); |
| 7218 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7219 | } |
| 7220 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7221 | public void setImmersive(IBinder token, boolean immersive) { |
| 7222 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7223 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 7224 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7225 | throw new IllegalArgumentException(); |
| 7226 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7227 | r.immersive = immersive; |
| 7228 | } |
| 7229 | } |
| 7230 | |
| 7231 | public boolean isImmersive(IBinder token) { |
| 7232 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7233 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 7234 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7235 | throw new IllegalArgumentException(); |
| 7236 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7237 | return r.immersive; |
| 7238 | } |
| 7239 | } |
| 7240 | |
| 7241 | public boolean isTopActivityImmersive() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 7242 | enforceNotIsolatedCaller("startActivity"); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7243 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7244 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7245 | return (r != null) ? r.immersive : false; |
| 7246 | } |
| 7247 | } |
| 7248 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7249 | public final void enterSafeMode() { |
| 7250 | synchronized(this) { |
| 7251 | // It only makes sense to do this before the system is ready |
| 7252 | // and started launching other packages. |
| 7253 | if (!mSystemReady) { |
| 7254 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7255 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7256 | } catch (RemoteException e) { |
| 7257 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7258 | } |
| 7259 | } |
| 7260 | } |
| 7261 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7262 | public final void showSafeModeOverlay() { |
| 7263 | View v = LayoutInflater.from(mContext).inflate( |
| 7264 | com.android.internal.R.layout.safe_mode, null); |
| 7265 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 7266 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 7267 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 7268 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
Fabrice Di Meglio | aac0d4e | 2012-07-19 19:21:26 -0700 | [diff] [blame] | 7269 | lp.gravity = Gravity.BOTTOM | Gravity.START; |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7270 | lp.format = v.getBackground().getOpacity(); |
| 7271 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 7272 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 7273 | ((WindowManager)mContext.getSystemService( |
| 7274 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 7275 | } |
| 7276 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7277 | public void noteWakeupAlarm(IIntentSender sender) { |
| 7278 | if (!(sender instanceof PendingIntentRecord)) { |
| 7279 | return; |
| 7280 | } |
| 7281 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 7282 | synchronized (stats) { |
| 7283 | if (mBatteryStatsService.isOnBattery()) { |
| 7284 | mBatteryStatsService.enforceCallingPermission(); |
| 7285 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 7286 | int MY_UID = Binder.getCallingUid(); |
| 7287 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 7288 | BatteryStatsImpl.Uid.Pkg pkg = |
| 7289 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 7290 | pkg.incWakeupsLocked(); |
| 7291 | } |
| 7292 | } |
| 7293 | } |
| 7294 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7295 | public boolean killPids(int[] pids, String pReason, boolean secure) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7296 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7297 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7298 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7299 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7300 | // XXX Note: don't acquire main activity lock here, because the window |
| 7301 | // manager calls in with its locks held. |
| 7302 | |
| 7303 | boolean killed = false; |
| 7304 | synchronized (mPidsSelfLocked) { |
| 7305 | int[] types = new int[pids.length]; |
| 7306 | int worstType = 0; |
| 7307 | for (int i=0; i<pids.length; i++) { |
| 7308 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7309 | if (proc != null) { |
| 7310 | int type = proc.setAdj; |
| 7311 | types[i] = type; |
| 7312 | if (type > worstType) { |
| 7313 | worstType = type; |
| 7314 | } |
| 7315 | } |
| 7316 | } |
| 7317 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7318 | // 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] | 7319 | // then constrain it so we will kill all hidden procs. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7320 | if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ |
| 7321 | && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7322 | worstType = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7323 | } |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7324 | |
| 7325 | // If this is not a secure call, don't let it kill processes that |
| 7326 | // are important. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7327 | if (!secure && worstType < ProcessList.SERVICE_ADJ) { |
| 7328 | worstType = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7329 | } |
| 7330 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7331 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7332 | for (int i=0; i<pids.length; i++) { |
| 7333 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7334 | if (proc == null) { |
| 7335 | continue; |
| 7336 | } |
| 7337 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7338 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7339 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7340 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid, |
| 7341 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7342 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7343 | proc.killedBackground = true; |
| 7344 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7345 | } |
| 7346 | } |
| 7347 | } |
| 7348 | return killed; |
| 7349 | } |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 7350 | |
| 7351 | @Override |
| 7352 | public boolean killProcessesBelowForeground(String reason) { |
| 7353 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7354 | throw new SecurityException("killProcessesBelowForeground() only available to system"); |
| 7355 | } |
| 7356 | |
| 7357 | return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason); |
| 7358 | } |
| 7359 | |
| 7360 | private boolean killProcessesBelowAdj(int belowAdj, String reason) { |
| 7361 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7362 | throw new SecurityException("killProcessesBelowAdj() only available to system"); |
| 7363 | } |
| 7364 | |
| 7365 | boolean killed = false; |
| 7366 | synchronized (mPidsSelfLocked) { |
| 7367 | final int size = mPidsSelfLocked.size(); |
| 7368 | for (int i = 0; i < size; i++) { |
| 7369 | final int pid = mPidsSelfLocked.keyAt(i); |
| 7370 | final ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7371 | if (proc == null) continue; |
| 7372 | |
| 7373 | final int adj = proc.setAdj; |
| 7374 | if (adj > belowAdj && !proc.killedBackground) { |
| 7375 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
| 7376 | EventLog.writeEvent( |
| 7377 | EventLogTags.AM_KILL, proc.pid, proc.processName, adj, reason); |
| 7378 | killed = true; |
| 7379 | proc.killedBackground = true; |
| 7380 | Process.killProcessQuiet(pid); |
| 7381 | } |
| 7382 | } |
| 7383 | } |
| 7384 | return killed; |
| 7385 | } |
| 7386 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7387 | public final void startRunning(String pkg, String cls, String action, |
| 7388 | String data) { |
| 7389 | synchronized(this) { |
| 7390 | if (mStartRunning) { |
| 7391 | return; |
| 7392 | } |
| 7393 | mStartRunning = true; |
| 7394 | mTopComponent = pkg != null && cls != null |
| 7395 | ? new ComponentName(pkg, cls) : null; |
| 7396 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 7397 | mTopData = data; |
| 7398 | if (!mSystemReady) { |
| 7399 | return; |
| 7400 | } |
| 7401 | } |
| 7402 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7403 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7404 | } |
| 7405 | |
| 7406 | private void retrieveSettings() { |
| 7407 | final ContentResolver resolver = mContext.getContentResolver(); |
| 7408 | String debugApp = Settings.System.getString( |
| 7409 | resolver, Settings.System.DEBUG_APP); |
| 7410 | boolean waitForDebugger = Settings.System.getInt( |
| 7411 | resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0; |
| 7412 | boolean alwaysFinishActivities = Settings.System.getInt( |
| 7413 | resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
| 7414 | |
| 7415 | Configuration configuration = new Configuration(); |
| 7416 | Settings.System.getConfiguration(resolver, configuration); |
| 7417 | |
| 7418 | synchronized (this) { |
| 7419 | mDebugApp = mOrigDebugApp = debugApp; |
| 7420 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 7421 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 7422 | // This happens before any activities are started, so we can |
| 7423 | // change mConfiguration in-place. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 7424 | updateConfigurationLocked(configuration, null, false, true); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7425 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7426 | } |
| 7427 | } |
| 7428 | |
| 7429 | public boolean testIsSystemReady() { |
| 7430 | // no need to synchronize(this) just to read & return the value |
| 7431 | return mSystemReady; |
| 7432 | } |
| 7433 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7434 | private static File getCalledPreBootReceiversFile() { |
| 7435 | File dataDir = Environment.getDataDirectory(); |
| 7436 | File systemDir = new File(dataDir, "system"); |
| 7437 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 7438 | return fname; |
| 7439 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7440 | |
| 7441 | static final int LAST_DONE_VERSION = 10000; |
| 7442 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7443 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 7444 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 7445 | File file = getCalledPreBootReceiversFile(); |
| 7446 | FileInputStream fis = null; |
| 7447 | try { |
| 7448 | fis = new FileInputStream(file); |
| 7449 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7450 | int fvers = dis.readInt(); |
| 7451 | if (fvers == LAST_DONE_VERSION) { |
| 7452 | String vers = dis.readUTF(); |
| 7453 | String codename = dis.readUTF(); |
| 7454 | String build = dis.readUTF(); |
| 7455 | if (android.os.Build.VERSION.RELEASE.equals(vers) |
| 7456 | && android.os.Build.VERSION.CODENAME.equals(codename) |
| 7457 | && android.os.Build.VERSION.INCREMENTAL.equals(build)) { |
| 7458 | int num = dis.readInt(); |
| 7459 | while (num > 0) { |
| 7460 | num--; |
| 7461 | String pkg = dis.readUTF(); |
| 7462 | String cls = dis.readUTF(); |
| 7463 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 7464 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7465 | } |
| 7466 | } |
| 7467 | } catch (FileNotFoundException e) { |
| 7468 | } catch (IOException e) { |
| 7469 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 7470 | } finally { |
| 7471 | if (fis != null) { |
| 7472 | try { |
| 7473 | fis.close(); |
| 7474 | } catch (IOException e) { |
| 7475 | } |
| 7476 | } |
| 7477 | } |
| 7478 | return lastDoneReceivers; |
| 7479 | } |
| 7480 | |
| 7481 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 7482 | File file = getCalledPreBootReceiversFile(); |
| 7483 | FileOutputStream fos = null; |
| 7484 | DataOutputStream dos = null; |
| 7485 | try { |
| 7486 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 7487 | fos = new FileOutputStream(file); |
| 7488 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7489 | dos.writeInt(LAST_DONE_VERSION); |
| 7490 | dos.writeUTF(android.os.Build.VERSION.RELEASE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7491 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7492 | dos.writeUTF(android.os.Build.VERSION.INCREMENTAL); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7493 | dos.writeInt(list.size()); |
| 7494 | for (int i=0; i<list.size(); i++) { |
| 7495 | dos.writeUTF(list.get(i).getPackageName()); |
| 7496 | dos.writeUTF(list.get(i).getClassName()); |
| 7497 | } |
| 7498 | } catch (IOException e) { |
| 7499 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 7500 | file.delete(); |
| 7501 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 7502 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7503 | if (dos != null) { |
| 7504 | try { |
| 7505 | dos.close(); |
| 7506 | } catch (IOException e) { |
| 7507 | // TODO Auto-generated catch block |
| 7508 | e.printStackTrace(); |
| 7509 | } |
| 7510 | } |
| 7511 | } |
| 7512 | } |
| 7513 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7514 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7515 | synchronized(this) { |
| 7516 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7517 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7518 | return; |
| 7519 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7520 | |
| 7521 | // Check to see if there are any update receivers to run. |
| 7522 | if (!mDidUpdate) { |
| 7523 | if (mWaitingUpdate) { |
| 7524 | return; |
| 7525 | } |
| 7526 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 7527 | List<ResolveInfo> ris = null; |
| 7528 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7529 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7530 | intent, null, 0, 0); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7531 | } catch (RemoteException e) { |
| 7532 | } |
| 7533 | if (ris != null) { |
| 7534 | for (int i=ris.size()-1; i>=0; i--) { |
| 7535 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 7536 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7537 | ris.remove(i); |
| 7538 | } |
| 7539 | } |
| 7540 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7541 | |
| 7542 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
| 7543 | |
| 7544 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7545 | for (int i=0; i<ris.size(); i++) { |
| 7546 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7547 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7548 | if (lastDoneReceivers.contains(comp)) { |
| 7549 | ris.remove(i); |
| 7550 | i--; |
| 7551 | } |
| 7552 | } |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 7553 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7554 | for (int i=0; i<ris.size(); i++) { |
| 7555 | ActivityInfo ai = ris.get(i).activityInfo; |
| 7556 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7557 | doneReceivers.add(comp); |
| 7558 | intent.setComponent(comp); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7559 | IIntentReceiver finisher = null; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 7560 | if (i == ris.size()-1) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7561 | finisher = new IIntentReceiver.Stub() { |
| 7562 | public void performReceive(Intent intent, int resultCode, |
Dianne Hackborn | 68d881c | 2009-10-05 13:58:17 -0700 | [diff] [blame] | 7563 | String data, Bundle extras, boolean ordered, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 7564 | boolean sticky, int sendingUser) { |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 7565 | // The raw IIntentReceiver interface is called |
| 7566 | // with the AM lock held, so redispatch to |
| 7567 | // execute our code without the lock. |
| 7568 | mHandler.post(new Runnable() { |
| 7569 | public void run() { |
| 7570 | synchronized (ActivityManagerService.this) { |
| 7571 | mDidUpdate = true; |
| 7572 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7573 | writeLastDonePreBootReceivers(doneReceivers); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7574 | showBootMessage(mContext.getText( |
| 7575 | R.string.android_upgrading_complete), |
| 7576 | false); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 7577 | systemReady(goingCallback); |
| 7578 | } |
| 7579 | }); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7580 | } |
| 7581 | }; |
| 7582 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7583 | Slog.i(TAG, "Sending system update to: " + intent.getComponent()); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 7584 | // XXX also need to send this to stopped users(!!!) |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7585 | broadcastIntentLocked(null, null, intent, null, finisher, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 7586 | 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 7587 | UserHandle.USER_ALL); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 7588 | if (finisher != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7589 | mWaitingUpdate = true; |
| 7590 | } |
| 7591 | } |
| 7592 | } |
| 7593 | if (mWaitingUpdate) { |
| 7594 | return; |
| 7595 | } |
| 7596 | mDidUpdate = true; |
| 7597 | } |
| 7598 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7599 | mSystemReady = true; |
| 7600 | if (!mStartRunning) { |
| 7601 | return; |
| 7602 | } |
| 7603 | } |
| 7604 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7605 | ArrayList<ProcessRecord> procsToKill = null; |
| 7606 | synchronized(mPidsSelfLocked) { |
| 7607 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 7608 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7609 | if (!isAllowedWhileBooting(proc.info)){ |
| 7610 | if (procsToKill == null) { |
| 7611 | procsToKill = new ArrayList<ProcessRecord>(); |
| 7612 | } |
| 7613 | procsToKill.add(proc); |
| 7614 | } |
| 7615 | } |
| 7616 | } |
| 7617 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7618 | synchronized(this) { |
| 7619 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7620 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 7621 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7622 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7623 | removeProcessLocked(proc, true, false, "system update done"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7624 | } |
| 7625 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7626 | |
| 7627 | // Now that we have cleaned up any update processes, we |
| 7628 | // are ready to start launching real processes and know that |
| 7629 | // we won't trample on them any more. |
| 7630 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7631 | } |
| 7632 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7633 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7634 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7635 | SystemClock.uptimeMillis()); |
| 7636 | |
| 7637 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7638 | // Make sure we have no pre-ready processes sitting around. |
| 7639 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7640 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7641 | ResolveInfo ri = mContext.getPackageManager() |
| 7642 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7643 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7644 | CharSequence errorMsg = null; |
| 7645 | if (ri != null) { |
| 7646 | ActivityInfo ai = ri.activityInfo; |
| 7647 | ApplicationInfo app = ai.applicationInfo; |
| 7648 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7649 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 7650 | mTopData = null; |
| 7651 | mTopComponent = new ComponentName(app.packageName, |
| 7652 | ai.name); |
| 7653 | } else { |
| 7654 | errorMsg = mContext.getResources().getText( |
| 7655 | com.android.internal.R.string.factorytest_not_system); |
| 7656 | } |
| 7657 | } else { |
| 7658 | errorMsg = mContext.getResources().getText( |
| 7659 | com.android.internal.R.string.factorytest_no_action); |
| 7660 | } |
| 7661 | if (errorMsg != null) { |
| 7662 | mTopAction = null; |
| 7663 | mTopData = null; |
| 7664 | mTopComponent = null; |
| 7665 | Message msg = Message.obtain(); |
| 7666 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 7667 | msg.getData().putCharSequence("msg", errorMsg); |
| 7668 | mHandler.sendMessage(msg); |
| 7669 | } |
| 7670 | } |
| 7671 | } |
| 7672 | |
| 7673 | retrieveSettings(); |
| 7674 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7675 | if (goingCallback != null) goingCallback.run(); |
| 7676 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7677 | synchronized (this) { |
| 7678 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7679 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7680 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7681 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7682 | if (apps != null) { |
| 7683 | int N = apps.size(); |
| 7684 | int i; |
| 7685 | for (i=0; i<N; i++) { |
| 7686 | ApplicationInfo info |
| 7687 | = (ApplicationInfo)apps.get(i); |
| 7688 | if (info != null && |
| 7689 | !info.packageName.equals("android")) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7690 | addAppLocked(info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7691 | } |
| 7692 | } |
| 7693 | } |
| 7694 | } catch (RemoteException ex) { |
| 7695 | // pm is in same process, this will never happen. |
| 7696 | } |
| 7697 | } |
| 7698 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7699 | // Start up initial activity. |
| 7700 | mBooting = true; |
| 7701 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7702 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7703 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7704 | Message msg = Message.obtain(); |
| 7705 | msg.what = SHOW_UID_ERROR_MSG; |
| 7706 | mHandler.sendMessage(msg); |
| 7707 | } |
| 7708 | } catch (RemoteException e) { |
| 7709 | } |
| 7710 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7711 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7712 | } |
| 7713 | } |
| 7714 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7715 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7716 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7717 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7718 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7719 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7720 | startAppProblemLocked(app); |
| 7721 | app.stopFreezingAllLocked(); |
| 7722 | return handleAppCrashLocked(app); |
| 7723 | } |
| 7724 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7725 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7726 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7727 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7728 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7729 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 7730 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7731 | startAppProblemLocked(app); |
| 7732 | app.stopFreezingAllLocked(); |
| 7733 | } |
| 7734 | |
| 7735 | /** |
| 7736 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 7737 | * |
| 7738 | * @param app The ProcessRecord in which the error occurred. |
| 7739 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 7740 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7741 | * @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] | 7742 | * @param shortMsg Short message describing the crash. |
| 7743 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7744 | * @param stackTrace Full crash stack trace, may be null. |
| 7745 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7746 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 7747 | */ |
| 7748 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7749 | 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] | 7750 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7751 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7752 | report.condition = condition; |
| 7753 | report.processName = app.processName; |
| 7754 | report.pid = app.pid; |
| 7755 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7756 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7757 | report.shortMsg = shortMsg; |
| 7758 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7759 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7760 | |
| 7761 | return report; |
| 7762 | } |
| 7763 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7764 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7765 | synchronized (this) { |
| 7766 | app.crashing = false; |
| 7767 | app.crashingReport = null; |
| 7768 | app.notResponding = false; |
| 7769 | app.notRespondingReport = null; |
| 7770 | if (app.anrDialog == fromDialog) { |
| 7771 | app.anrDialog = null; |
| 7772 | } |
| 7773 | if (app.waitDialog == fromDialog) { |
| 7774 | app.waitDialog = null; |
| 7775 | } |
| 7776 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7777 | handleAppCrashLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7778 | Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request"); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7779 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 7780 | app.processName, app.setAdj, "user's request after error"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7781 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7782 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7783 | } |
| 7784 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7785 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7786 | private boolean handleAppCrashLocked(ProcessRecord app) { |
Mike Lockwood | 86548c4 | 2011-09-13 17:21:46 -0400 | [diff] [blame] | 7787 | if (mHeadless) { |
| 7788 | Log.e(TAG, "handleAppCrashLocked: " + app.processName); |
| 7789 | return false; |
| 7790 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7791 | long now = SystemClock.uptimeMillis(); |
| 7792 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7793 | Long crashTime; |
| 7794 | if (!app.isolated) { |
| 7795 | crashTime = mProcessCrashTimes.get(app.info.processName, app.uid); |
| 7796 | } else { |
| 7797 | crashTime = null; |
| 7798 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7799 | if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7800 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7801 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7802 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7803 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7804 | app.info.processName, app.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7805 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 7806 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7807 | if (r.app == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7808 | Slog.w(TAG, " Force finishing activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7809 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7810 | 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] | 7811 | } |
| 7812 | } |
| 7813 | if (!app.persistent) { |
| 7814 | // We don't want to start this process again until the user |
| 7815 | // explicitly does so... but for persistent process, we really |
| 7816 | // need to keep it running. If a persistent process is actually |
| 7817 | // repeatedly crashing, then badness for everyone. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7818 | EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7819 | app.info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7820 | if (!app.isolated) { |
| 7821 | // XXX We don't have a way to mark isolated processes |
| 7822 | // as bad, since they don't have a peristent identity. |
| 7823 | mBadProcesses.put(app.info.processName, app.uid, now); |
| 7824 | mProcessCrashTimes.remove(app.info.processName, app.uid); |
| 7825 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7826 | app.bad = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7827 | app.removed = true; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 7828 | // Don't let services in this process be restarted and potentially |
| 7829 | // annoy the user repeatedly. Unless it is persistent, since those |
| 7830 | // processes run critical code. |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7831 | removeProcessLocked(app, false, false, "crash"); |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7832 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7833 | return false; |
| 7834 | } |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7835 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7836 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7837 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Kevin Hester-Chow | d87a9be | 2012-03-05 08:01:00 -0800 | [diff] [blame] | 7838 | if (r != null && r.app == app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7839 | // If the top running activity is from this crashing |
| 7840 | // process, then terminate it to avoid getting in a loop. |
| 7841 | Slog.w(TAG, " Force finishing activity " |
| 7842 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 7843 | int index = mMainStack.indexOfActivityLocked(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7844 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7845 | Activity.RESULT_CANCELED, null, "crashed"); |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 7846 | // Also terminate any activities below it that aren't yet |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7847 | // stopped, to avoid a situation where one will get |
| 7848 | // re-start our crashing activity once it gets resumed again. |
| 7849 | index--; |
| 7850 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7851 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7852 | if (r.state == ActivityState.RESUMED |
| 7853 | || r.state == ActivityState.PAUSING |
| 7854 | || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 7855 | if (!r.isHomeActivity || mHomeProcess != r.app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7856 | Slog.w(TAG, " Force finishing activity " |
| 7857 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7858 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 7859 | Activity.RESULT_CANCELED, null, "crashed"); |
| 7860 | } |
| 7861 | } |
| 7862 | } |
| 7863 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7864 | } |
| 7865 | |
| 7866 | // Bump up the crash count of any services currently running in the proc. |
| 7867 | if (app.services.size() != 0) { |
| 7868 | // Any services running in the application need to be placed |
| 7869 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7870 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7871 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 7872 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7873 | sr.crashCount++; |
| 7874 | } |
| 7875 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7876 | |
| 7877 | // If the crashing process is what we consider to be the "home process" and it has been |
| 7878 | // replaced by a third-party app, clear the package preferred activities from packages |
| 7879 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 7880 | // from blocking the user to manually clear the list. |
| 7881 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 7882 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7883 | Iterator it = mHomeProcess.activities.iterator(); |
| 7884 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 7885 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 7886 | if (r.isHomeActivity) { |
| 7887 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 7888 | try { |
| 7889 | ActivityThread.getPackageManager() |
| 7890 | .clearPackagePreferredActivities(r.packageName); |
| 7891 | } catch (RemoteException c) { |
| 7892 | // pm is in same process, this will never happen. |
| 7893 | } |
| 7894 | } |
| 7895 | } |
| 7896 | } |
| 7897 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7898 | if (!app.isolated) { |
| 7899 | // XXX Can't keep track of crash times for isolated processes, |
| 7900 | // because they don't have a perisistent identity. |
| 7901 | mProcessCrashTimes.put(app.info.processName, app.uid, now); |
| 7902 | } |
| 7903 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7904 | return true; |
| 7905 | } |
| 7906 | |
| 7907 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 7908 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 7909 | mContext, app.info.packageName, app.info.flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7910 | skipCurrentReceiverLocked(app); |
| 7911 | } |
| 7912 | |
| 7913 | void skipCurrentReceiverLocked(ProcessRecord app) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 7914 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 7915 | queue.skipCurrentReceiverLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7916 | } |
| 7917 | } |
| 7918 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7919 | /** |
| 7920 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 7921 | * The application process will exit immediately after this call returns. |
| 7922 | * @param app object of the crashing app, null for the system server |
| 7923 | * @param crashInfo describing the exception |
| 7924 | */ |
| 7925 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7926 | ProcessRecord r = findAppProcess(app, "Crash"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7927 | final String processName = app == null ? "system_server" |
| 7928 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7929 | |
| 7930 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7931 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 7932 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7933 | crashInfo.exceptionClassName, |
| 7934 | crashInfo.exceptionMessage, |
| 7935 | crashInfo.throwFileName, |
| 7936 | crashInfo.throwLineNumber); |
| 7937 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 7938 | addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7939 | |
| 7940 | crashApplication(r, crashInfo); |
| 7941 | } |
| 7942 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7943 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7944 | IBinder app, |
| 7945 | int violationMask, |
| 7946 | StrictMode.ViolationInfo info) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 7947 | ProcessRecord r = findAppProcess(app, "StrictMode"); |
| 7948 | if (r == null) { |
| 7949 | return; |
| 7950 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7951 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7952 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 7953 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7954 | boolean logIt = true; |
| 7955 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 7956 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 7957 | logIt = false; |
| 7958 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7959 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7960 | // the relative pain numbers, without logging all |
| 7961 | // the stack traces repeatedly. We'd want to do |
| 7962 | // likewise in the client code, which also does |
| 7963 | // dup suppression, before the Binder call. |
| 7964 | } else { |
| 7965 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 7966 | mAlreadyLoggedViolatedStacks.clear(); |
| 7967 | } |
| 7968 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 7969 | } |
| 7970 | } |
| 7971 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7972 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7973 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7974 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7975 | |
| 7976 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 7977 | AppErrorResult result = new AppErrorResult(); |
| 7978 | synchronized (this) { |
| 7979 | final long origId = Binder.clearCallingIdentity(); |
| 7980 | |
| 7981 | Message msg = Message.obtain(); |
| 7982 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 7983 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 7984 | data.put("result", result); |
| 7985 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 7986 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 7987 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7988 | msg.obj = data; |
| 7989 | mHandler.sendMessage(msg); |
| 7990 | |
| 7991 | Binder.restoreCallingIdentity(origId); |
| 7992 | } |
| 7993 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 7994 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 7995 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 7996 | } |
| 7997 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 7998 | // Depending on the policy in effect, there could be a bunch of |
| 7999 | // these in quick succession so we try to batch these together to |
| 8000 | // minimize disk writes, number of dropbox entries, and maximize |
| 8001 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8002 | private void logStrictModeViolationToDropBox( |
| 8003 | ProcessRecord process, |
| 8004 | StrictMode.ViolationInfo info) { |
| 8005 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8006 | return; |
| 8007 | } |
| 8008 | final boolean isSystemApp = process == null || |
| 8009 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 8010 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8011 | final String processName = process == null ? "unknown" : process.processName; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8012 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 8013 | final DropBoxManager dbox = (DropBoxManager) |
| 8014 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 8015 | |
| 8016 | // Exit early if the dropbox isn't configured to accept this report type. |
| 8017 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 8018 | |
| 8019 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8020 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8021 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 8022 | synchronized (sb) { |
| 8023 | bufferWasEmpty = sb.length() == 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8024 | appendDropBoxProcessHeaders(process, processName, sb); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8025 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 8026 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8027 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 8028 | if (info.violationNumThisLoop != 0) { |
| 8029 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 8030 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 8031 | if (info.numAnimationsRunning != 0) { |
| 8032 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 8033 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 8034 | if (info.broadcastIntentAction != null) { |
| 8035 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 8036 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 8037 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8038 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8039 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 8040 | if (info.numInstances != -1) { |
| 8041 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 8042 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 8043 | if (info.tags != null) { |
| 8044 | for (String tag : info.tags) { |
| 8045 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 8046 | } |
| 8047 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8048 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8049 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 8050 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8051 | } |
| 8052 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8053 | |
| 8054 | // Only buffer up to ~64k. Various logging bits truncate |
| 8055 | // things at 128k. |
| 8056 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8057 | } |
| 8058 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8059 | // Flush immediately if the buffer's grown too large, or this |
| 8060 | // is a non-system app. Non-system apps are isolated with a |
| 8061 | // different tag & policy and not batched. |
| 8062 | // |
| 8063 | // Batching is useful during internal testing with |
| 8064 | // StrictMode settings turned up high. Without batching, |
| 8065 | // thousands of separate files could be created on boot. |
| 8066 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8067 | new Thread("Error dump: " + dropboxTag) { |
| 8068 | @Override |
| 8069 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8070 | String report; |
| 8071 | synchronized (sb) { |
| 8072 | report = sb.toString(); |
| 8073 | sb.delete(0, sb.length()); |
| 8074 | sb.trimToSize(); |
| 8075 | } |
| 8076 | if (report.length() != 0) { |
| 8077 | dbox.addText(dropboxTag, report); |
| 8078 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8079 | } |
| 8080 | }.start(); |
| 8081 | return; |
| 8082 | } |
| 8083 | |
| 8084 | // System app batching: |
| 8085 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8086 | // An existing dropbox-writing thread is outstanding, so |
| 8087 | // we don't need to start it up. The existing thread will |
| 8088 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8089 | return; |
| 8090 | } |
| 8091 | |
| 8092 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 8093 | // (After this point, we shouldn't access AMS internal data structures.) |
| 8094 | new Thread("Error dump: " + dropboxTag) { |
| 8095 | @Override |
| 8096 | public void run() { |
| 8097 | // 5 second sleep to let stacks arrive and be batched together |
| 8098 | try { |
| 8099 | Thread.sleep(5000); // 5 seconds |
| 8100 | } catch (InterruptedException e) {} |
| 8101 | |
| 8102 | String errorReport; |
| 8103 | synchronized (mStrictModeBuffer) { |
| 8104 | errorReport = mStrictModeBuffer.toString(); |
| 8105 | if (errorReport.length() == 0) { |
| 8106 | return; |
| 8107 | } |
| 8108 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 8109 | mStrictModeBuffer.trimToSize(); |
| 8110 | } |
| 8111 | dbox.addText(dropboxTag, errorReport); |
| 8112 | } |
| 8113 | }.start(); |
| 8114 | } |
| 8115 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8116 | /** |
| 8117 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 8118 | * @param app object of the crashing app, null for the system server |
| 8119 | * @param tag reported by the caller |
| 8120 | * @param crashInfo describing the context of the error |
| 8121 | * @return true if the process should exit immediately (WTF is fatal) |
| 8122 | */ |
| 8123 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8124 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8125 | ProcessRecord r = findAppProcess(app, "WTF"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8126 | final String processName = app == null ? "system_server" |
| 8127 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8128 | |
| 8129 | EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8130 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 8131 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8132 | tag, crashInfo.exceptionMessage); |
| 8133 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8134 | addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8135 | |
Dianne Hackborn | 1ab4377 | 2011-03-15 14:38:02 -0700 | [diff] [blame] | 8136 | if (r != null && r.pid != Process.myPid() && |
| 8137 | Settings.Secure.getInt(mContext.getContentResolver(), |
| 8138 | Settings.Secure.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8139 | crashApplication(r, crashInfo); |
| 8140 | return true; |
| 8141 | } else { |
| 8142 | return false; |
| 8143 | } |
| 8144 | } |
| 8145 | |
| 8146 | /** |
| 8147 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 8148 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 8149 | */ |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8150 | private ProcessRecord findAppProcess(IBinder app, String reason) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8151 | if (app == null) { |
| 8152 | return null; |
| 8153 | } |
| 8154 | |
| 8155 | synchronized (this) { |
| 8156 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 8157 | final int NA = apps.size(); |
| 8158 | for (int ia=0; ia<NA; ia++) { |
| 8159 | ProcessRecord p = apps.valueAt(ia); |
| 8160 | if (p.thread != null && p.thread.asBinder() == app) { |
| 8161 | return p; |
| 8162 | } |
| 8163 | } |
| 8164 | } |
| 8165 | |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8166 | Slog.w(TAG, "Can't find mystery application for " + reason |
| 8167 | + " from pid=" + Binder.getCallingPid() |
| 8168 | + " uid=" + Binder.getCallingUid() + ": " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8169 | return null; |
| 8170 | } |
| 8171 | } |
| 8172 | |
| 8173 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8174 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 8175 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8176 | */ |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8177 | private void appendDropBoxProcessHeaders(ProcessRecord process, String processName, |
| 8178 | StringBuilder sb) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8179 | // Watchdog thread ends up invoking this function (with |
| 8180 | // a null ProcessRecord) to add the stack file to dropbox. |
| 8181 | // Do not acquire a lock on this (am) in such cases, as it |
| 8182 | // could cause a potential deadlock, if and when watchdog |
| 8183 | // is invoked due to unavailability of lock on am and it |
| 8184 | // would prevent watchdog from killing system_server. |
| 8185 | if (process == null) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8186 | sb.append("Process: ").append(processName).append("\n"); |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8187 | return; |
| 8188 | } |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 8189 | // Note: ProcessRecord 'process' is guarded by the service |
| 8190 | // instance. (notably process.pkgList, which could otherwise change |
| 8191 | // concurrently during execution of this method) |
| 8192 | synchronized (this) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8193 | sb.append("Process: ").append(processName).append("\n"); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8194 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8195 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8196 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 8197 | for (String pkg : process.pkgList) { |
| 8198 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8199 | try { |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 8200 | PackageInfo pi = pm.getPackageInfo(pkg, 0, UserHandle.getCallingUserId()); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8201 | if (pi != null) { |
| 8202 | sb.append(" v").append(pi.versionCode); |
| 8203 | if (pi.versionName != null) { |
| 8204 | sb.append(" (").append(pi.versionName).append(")"); |
| 8205 | } |
| 8206 | } |
| 8207 | } catch (RemoteException e) { |
| 8208 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8209 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8210 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8211 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8212 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8213 | } |
| 8214 | |
| 8215 | private static String processClass(ProcessRecord process) { |
| 8216 | if (process == null || process.pid == MY_PID) { |
| 8217 | return "system_server"; |
| 8218 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 8219 | return "system_app"; |
| 8220 | } else { |
| 8221 | return "data_app"; |
| 8222 | } |
| 8223 | } |
| 8224 | |
| 8225 | /** |
| 8226 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 8227 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 8228 | * @param process which caused the error, null means the system server |
| 8229 | * @param activity which triggered the error, null if unknown |
| 8230 | * @param parent activity related to the error, null if unknown |
| 8231 | * @param subject line related to the error, null if absent |
| 8232 | * @param report in long form describing the error, null if absent |
| 8233 | * @param logFile to include in the report, null if none |
| 8234 | * @param crashInfo giving an application stack trace, null if absent |
| 8235 | */ |
| 8236 | public void addErrorToDropBox(String eventType, |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8237 | ProcessRecord process, String processName, ActivityRecord activity, |
| 8238 | ActivityRecord parent, String subject, |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8239 | final String report, final File logFile, |
| 8240 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 8241 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 8242 | // otherwise the watchdog may be prevented from resetting the system. |
| 8243 | |
| 8244 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 8245 | final DropBoxManager dbox = (DropBoxManager) |
| 8246 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 8247 | |
| 8248 | // Exit early if the dropbox isn't configured to accept this report type. |
| 8249 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 8250 | |
| 8251 | final StringBuilder sb = new StringBuilder(1024); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8252 | appendDropBoxProcessHeaders(process, processName, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8253 | if (activity != null) { |
| 8254 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 8255 | } |
| 8256 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 8257 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 8258 | } |
| 8259 | if (parent != null && parent != activity) { |
| 8260 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 8261 | } |
| 8262 | if (subject != null) { |
| 8263 | sb.append("Subject: ").append(subject).append("\n"); |
| 8264 | } |
| 8265 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 8266 | if (Debug.isDebuggerConnected()) { |
| 8267 | sb.append("Debugger: Connected\n"); |
| 8268 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8269 | sb.append("\n"); |
| 8270 | |
| 8271 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 8272 | // (After this point, we shouldn't access AMS internal data structures.) |
| 8273 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 8274 | @Override |
| 8275 | public void run() { |
| 8276 | if (report != null) { |
| 8277 | sb.append(report); |
| 8278 | } |
| 8279 | if (logFile != null) { |
| 8280 | try { |
| 8281 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 8282 | } catch (IOException e) { |
| 8283 | Slog.e(TAG, "Error reading " + logFile, e); |
| 8284 | } |
| 8285 | } |
| 8286 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 8287 | sb.append(crashInfo.stackTrace); |
| 8288 | } |
| 8289 | |
| 8290 | String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 8291 | int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0); |
| 8292 | if (lines > 0) { |
| 8293 | sb.append("\n"); |
| 8294 | |
| 8295 | // Merge several logcat streams, and take the last N lines |
| 8296 | InputStreamReader input = null; |
| 8297 | try { |
| 8298 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 8299 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 8300 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 8301 | |
| 8302 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 8303 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 8304 | input = new InputStreamReader(logcat.getInputStream()); |
| 8305 | |
| 8306 | int num; |
| 8307 | char[] buf = new char[8192]; |
| 8308 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 8309 | } catch (IOException e) { |
| 8310 | Slog.e(TAG, "Error running logcat", e); |
| 8311 | } finally { |
| 8312 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 8313 | } |
| 8314 | } |
| 8315 | |
| 8316 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8317 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8318 | }; |
| 8319 | |
Dianne Hackborn | 56385cc | 2012-04-30 15:07:47 -0700 | [diff] [blame] | 8320 | if (process == null) { |
| 8321 | // If process is null, we are being called from some internal code |
| 8322 | // and may be about to die -- run this synchronously. |
| 8323 | worker.run(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8324 | } else { |
| 8325 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8326 | } |
| 8327 | } |
| 8328 | |
| 8329 | /** |
| 8330 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 8331 | * Deal with edge cases (intercepts from instrumented applications, |
| 8332 | * ActivityController, error intent receivers, that sort of thing). |
| 8333 | * @param r the application crashing |
| 8334 | * @param crashInfo describing the failure |
| 8335 | */ |
| 8336 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8337 | long timeMillis = System.currentTimeMillis(); |
| 8338 | String shortMsg = crashInfo.exceptionClassName; |
| 8339 | String longMsg = crashInfo.exceptionMessage; |
| 8340 | String stackTrace = crashInfo.stackTrace; |
| 8341 | if (shortMsg != null && longMsg != null) { |
| 8342 | longMsg = shortMsg + ": " + longMsg; |
| 8343 | } else if (shortMsg != null) { |
| 8344 | longMsg = shortMsg; |
| 8345 | } |
| 8346 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8347 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8348 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8349 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8350 | try { |
| 8351 | String name = r != null ? r.processName : null; |
| 8352 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8353 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8354 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8355 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8356 | + " at watcher's request"); |
| 8357 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8358 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8359 | } |
| 8360 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8361 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8362 | } |
| 8363 | } |
| 8364 | |
| 8365 | final long origId = Binder.clearCallingIdentity(); |
| 8366 | |
| 8367 | // If this process is running instrumentation, finish it. |
| 8368 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8369 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8370 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8371 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 8372 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8373 | Bundle info = new Bundle(); |
| 8374 | info.putString("shortMsg", shortMsg); |
| 8375 | info.putString("longMsg", longMsg); |
| 8376 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 8377 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8378 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8379 | } |
| 8380 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8381 | // If we can't identify the process or it's already exceeded its crash quota, |
| 8382 | // quit right away without showing a crash dialog. |
| 8383 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8384 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8385 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8386 | } |
| 8387 | |
| 8388 | Message msg = Message.obtain(); |
| 8389 | msg.what = SHOW_ERROR_MSG; |
| 8390 | HashMap data = new HashMap(); |
| 8391 | data.put("result", result); |
| 8392 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8393 | msg.obj = data; |
| 8394 | mHandler.sendMessage(msg); |
| 8395 | |
| 8396 | Binder.restoreCallingIdentity(origId); |
| 8397 | } |
| 8398 | |
| 8399 | int res = result.get(); |
| 8400 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8401 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8402 | synchronized (this) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8403 | if (r != null && !r.isolated) { |
| 8404 | // XXX Can't keep track of crash time for isolated processes, |
| 8405 | // since they don't have a persistent identity. |
| 8406 | mProcessCrashTimes.put(r.info.processName, r.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8407 | SystemClock.uptimeMillis()); |
| 8408 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8409 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8410 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8411 | } |
| 8412 | } |
| 8413 | |
| 8414 | if (appErrorIntent != null) { |
| 8415 | try { |
| 8416 | mContext.startActivity(appErrorIntent); |
| 8417 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8418 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8419 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8420 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8421 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8422 | |
| 8423 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 8424 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 8425 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8426 | if (report == null) { |
| 8427 | return null; |
| 8428 | } |
| 8429 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 8430 | result.setComponent(r.errorReportReceiver); |
| 8431 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 8432 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 8433 | return result; |
| 8434 | } |
| 8435 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8436 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 8437 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8438 | if (r.errorReportReceiver == null) { |
| 8439 | return null; |
| 8440 | } |
| 8441 | |
| 8442 | if (!r.crashing && !r.notResponding) { |
| 8443 | return null; |
| 8444 | } |
| 8445 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8446 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 8447 | report.packageName = r.info.packageName; |
| 8448 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 8449 | report.processName = r.processName; |
| 8450 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 8451 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8452 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8453 | if (r.crashing) { |
| 8454 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 8455 | report.crashInfo = crashInfo; |
| 8456 | } else if (r.notResponding) { |
| 8457 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 8458 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8459 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8460 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 8461 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 8462 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8463 | } |
| 8464 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8465 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8466 | } |
| 8467 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8468 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8469 | enforceNotIsolatedCaller("getProcessesInErrorState"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8470 | // assume our apps are happy - lazy create the list |
| 8471 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 8472 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8473 | final boolean allUsers = ActivityManager.checkUidPermission( |
| 8474 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 8475 | Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED; |
| 8476 | int userId = UserHandle.getUserId(Binder.getCallingUid()); |
| 8477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8478 | synchronized (this) { |
| 8479 | |
| 8480 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8481 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8482 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8483 | if (!allUsers && app.userId != userId) { |
| 8484 | continue; |
| 8485 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8486 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 8487 | // This one's in trouble, so we'll generate a report for it |
| 8488 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 8489 | ActivityManager.ProcessErrorStateInfo report = null; |
| 8490 | if (app.crashing) { |
| 8491 | report = app.crashingReport; |
| 8492 | } else if (app.notResponding) { |
| 8493 | report = app.notRespondingReport; |
| 8494 | } |
| 8495 | |
| 8496 | if (report != null) { |
| 8497 | if (errList == null) { |
| 8498 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 8499 | } |
| 8500 | errList.add(report); |
| 8501 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8502 | 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] | 8503 | " crashing = " + app.crashing + |
| 8504 | " notResponding = " + app.notResponding); |
| 8505 | } |
| 8506 | } |
| 8507 | } |
| 8508 | } |
| 8509 | |
| 8510 | return errList; |
| 8511 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8512 | |
| 8513 | static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) { |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8514 | if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8515 | if (currApp != null) { |
| 8516 | currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
| 8517 | } |
| 8518 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8519 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 8520 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8521 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 8522 | if (currApp != null) { |
| 8523 | currApp.lru = 0; |
| 8524 | } |
| 8525 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8526 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8527 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 8528 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 8529 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 8530 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 8531 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 8532 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 8533 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 8534 | } else { |
| 8535 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 8536 | } |
| 8537 | } |
| 8538 | |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8539 | private void fillInProcMemInfo(ProcessRecord app, |
| 8540 | ActivityManager.RunningAppProcessInfo outInfo) { |
| 8541 | outInfo.pid = app.pid; |
| 8542 | outInfo.uid = app.info.uid; |
| 8543 | if (mHeavyWeightProcess == app) { |
| 8544 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
| 8545 | } |
| 8546 | if (app.persistent) { |
| 8547 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 8548 | } |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8549 | if (app.hasActivities) { |
| 8550 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES; |
| 8551 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8552 | outInfo.lastTrimLevel = app.trimMemoryLevel; |
| 8553 | int adj = app.curAdj; |
| 8554 | outInfo.importance = oomAdjToImportance(adj, outInfo); |
| 8555 | outInfo.importanceReasonCode = app.adjTypeCode; |
| 8556 | } |
| 8557 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8558 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8559 | enforceNotIsolatedCaller("getRunningAppProcesses"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8560 | // Lazy instantiation of list |
| 8561 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8562 | final boolean allUsers = ActivityManager.checkUidPermission( |
| 8563 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 8564 | Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED; |
| 8565 | int userId = UserHandle.getUserId(Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8566 | synchronized (this) { |
| 8567 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8568 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8569 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8570 | if (!allUsers && app.userId != userId) { |
| 8571 | continue; |
| 8572 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8573 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 8574 | // Generate process state info for running application |
| 8575 | ActivityManager.RunningAppProcessInfo currApp = |
| 8576 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 8577 | app.pid, app.getPackageList()); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8578 | fillInProcMemInfo(app, currApp); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8579 | if (app.adjSource instanceof ProcessRecord) { |
| 8580 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8581 | currApp.importanceReasonImportance = oomAdjToImportance( |
| 8582 | app.adjSourceOom, null); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8583 | } else if (app.adjSource instanceof ActivityRecord) { |
| 8584 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8585 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 8586 | } |
| 8587 | if (app.adjTarget instanceof ComponentName) { |
| 8588 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 8589 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8590 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8591 | // + " lru=" + currApp.lru); |
| 8592 | if (runList == null) { |
| 8593 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 8594 | } |
| 8595 | runList.add(currApp); |
| 8596 | } |
| 8597 | } |
| 8598 | } |
| 8599 | return runList; |
| 8600 | } |
| 8601 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8602 | public List<ApplicationInfo> getRunningExternalApplications() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8603 | enforceNotIsolatedCaller("getRunningExternalApplications"); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8604 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 8605 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 8606 | if (runningApps != null && runningApps.size() > 0) { |
| 8607 | Set<String> extList = new HashSet<String>(); |
| 8608 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 8609 | if (app.pkgList != null) { |
| 8610 | for (String pkg : app.pkgList) { |
| 8611 | extList.add(pkg); |
| 8612 | } |
| 8613 | } |
| 8614 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8615 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8616 | for (String pkg : extList) { |
| 8617 | try { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 8618 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0, UserHandle.getCallingUserId()); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8619 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 8620 | retList.add(info); |
| 8621 | } |
| 8622 | } catch (RemoteException e) { |
| 8623 | } |
| 8624 | } |
| 8625 | } |
| 8626 | return retList; |
| 8627 | } |
| 8628 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8629 | @Override |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8630 | public void getMyMemoryState(ActivityManager.RunningAppProcessInfo outInfo) { |
| 8631 | enforceNotIsolatedCaller("getMyMemoryState"); |
| 8632 | synchronized (this) { |
| 8633 | ProcessRecord proc; |
| 8634 | synchronized (mPidsSelfLocked) { |
| 8635 | proc = mPidsSelfLocked.get(Binder.getCallingPid()); |
| 8636 | } |
| 8637 | fillInProcMemInfo(proc, outInfo); |
| 8638 | } |
| 8639 | } |
| 8640 | |
| 8641 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8642 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8643 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 8644 | != PackageManager.PERMISSION_GRANTED) { |
| 8645 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 8646 | + Binder.getCallingPid() |
| 8647 | + ", uid=" + Binder.getCallingUid() |
| 8648 | + " without permission " |
| 8649 | + android.Manifest.permission.DUMP); |
| 8650 | return; |
| 8651 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8652 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8653 | boolean dumpAll = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8654 | boolean dumpClient = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8655 | String dumpPackage = null; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8656 | |
| 8657 | int opti = 0; |
| 8658 | while (opti < args.length) { |
| 8659 | String opt = args[opti]; |
| 8660 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 8661 | break; |
| 8662 | } |
| 8663 | opti++; |
| 8664 | if ("-a".equals(opt)) { |
| 8665 | dumpAll = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8666 | } else if ("-c".equals(opt)) { |
| 8667 | dumpClient = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8668 | } else if ("-h".equals(opt)) { |
| 8669 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8670 | pw.println(" [-a] [-c] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8671 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8672 | pw.println(" a[ctivities]: activity stack state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8673 | pw.println(" b[roadcasts] [PACKAGE_NAME]: broadcast state"); |
| 8674 | pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state"); |
| 8675 | pw.println(" p[rocesses] [PACKAGE_NAME]: process state"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8676 | pw.println(" o[om]: out of memory management"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8677 | pw.println(" prov[iders] [COMP_SPEC ...]: content provider state"); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8678 | pw.println(" provider [COMP_SPEC]: provider client-side state"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8679 | pw.println(" s[ervices] [COMP_SPEC ...]: service state"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8680 | pw.println(" service [COMP_SPEC]: service client-side state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8681 | pw.println(" package [PACKAGE_NAME]: all state related to given package"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8682 | pw.println(" all: dump all activities"); |
| 8683 | pw.println(" top: dump the top activity"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8684 | pw.println(" cmd may also be a COMP_SPEC to dump activities."); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8685 | pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),"); |
| 8686 | pw.println(" a partial substring in a component name, a"); |
| 8687 | pw.println(" hex object identifier."); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8688 | pw.println(" -a: include all available server state."); |
| 8689 | pw.println(" -c: include client state."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8690 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8691 | } else { |
| 8692 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8693 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8694 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8695 | |
| 8696 | long origId = Binder.clearCallingIdentity(); |
| 8697 | boolean more = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8698 | // Is the caller requesting to dump a particular piece of data? |
| 8699 | if (opti < args.length) { |
| 8700 | String cmd = args[opti]; |
| 8701 | opti++; |
| 8702 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 8703 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8704 | dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8705 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8706 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8707 | String[] newArgs; |
| 8708 | String name; |
| 8709 | if (opti >= args.length) { |
| 8710 | name = null; |
| 8711 | newArgs = EMPTY_STRING_ARRAY; |
| 8712 | } else { |
| 8713 | name = args[opti]; |
| 8714 | opti++; |
| 8715 | newArgs = new String[args.length - opti]; |
| 8716 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8717 | args.length - opti); |
| 8718 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8719 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8720 | dumpBroadcastsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8721 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8722 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8723 | String[] newArgs; |
| 8724 | String name; |
| 8725 | if (opti >= args.length) { |
| 8726 | name = null; |
| 8727 | newArgs = EMPTY_STRING_ARRAY; |
| 8728 | } else { |
| 8729 | name = args[opti]; |
| 8730 | opti++; |
| 8731 | newArgs = new String[args.length - opti]; |
| 8732 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8733 | args.length - opti); |
| 8734 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8735 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8736 | dumpPendingIntentsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8737 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8738 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8739 | String[] newArgs; |
| 8740 | String name; |
| 8741 | if (opti >= args.length) { |
| 8742 | name = null; |
| 8743 | newArgs = EMPTY_STRING_ARRAY; |
| 8744 | } else { |
| 8745 | name = args[opti]; |
| 8746 | opti++; |
| 8747 | newArgs = new String[args.length - opti]; |
| 8748 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8749 | args.length - opti); |
| 8750 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8751 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8752 | dumpProcessesLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8753 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8754 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 8755 | synchronized (this) { |
| 8756 | dumpOomLocked(fd, pw, args, opti, true); |
| 8757 | } |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8758 | } else if ("provider".equals(cmd)) { |
| 8759 | String[] newArgs; |
| 8760 | String name; |
| 8761 | if (opti >= args.length) { |
| 8762 | name = null; |
| 8763 | newArgs = EMPTY_STRING_ARRAY; |
| 8764 | } else { |
| 8765 | name = args[opti]; |
| 8766 | opti++; |
| 8767 | newArgs = new String[args.length - opti]; |
| 8768 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 8769 | } |
| 8770 | if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) { |
| 8771 | pw.println("No providers match: " + name); |
| 8772 | pw.println("Use -h for help."); |
| 8773 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8774 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 8775 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8776 | dumpProvidersLocked(fd, pw, args, opti, true, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8777 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8778 | } else if ("service".equals(cmd)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8779 | String[] newArgs; |
| 8780 | String name; |
| 8781 | if (opti >= args.length) { |
| 8782 | name = null; |
| 8783 | newArgs = EMPTY_STRING_ARRAY; |
| 8784 | } else { |
| 8785 | name = args[opti]; |
| 8786 | opti++; |
| 8787 | newArgs = new String[args.length - opti]; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8788 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8789 | args.length - opti); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8790 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 8791 | if (!mServices.dumpService(fd, pw, name, newArgs, 0, dumpAll)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8792 | pw.println("No services match: " + name); |
| 8793 | pw.println("Use -h for help."); |
| 8794 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8795 | } else if ("package".equals(cmd)) { |
| 8796 | String[] newArgs; |
| 8797 | if (opti >= args.length) { |
| 8798 | pw.println("package: no package name specified"); |
| 8799 | pw.println("Use -h for help."); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8800 | } else { |
| 8801 | dumpPackage = args[opti]; |
| 8802 | opti++; |
| 8803 | newArgs = new String[args.length - opti]; |
| 8804 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8805 | args.length - opti); |
| 8806 | args = newArgs; |
| 8807 | opti = 0; |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8808 | more = true; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8809 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8810 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 8811 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 8812 | mServices.dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8813 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8814 | } else { |
| 8815 | // Dumping a single activity? |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8816 | if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 8817 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 8818 | pw.println("Use -h for help."); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 8819 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8820 | } |
| 8821 | if (!more) { |
| 8822 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 8823 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8824 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8825 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8826 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8827 | // No piece of data specified, dump everything. |
| 8828 | synchronized (this) { |
| 8829 | boolean needSep; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8830 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8831 | if (needSep) { |
| 8832 | pw.println(" "); |
| 8833 | } |
| 8834 | if (dumpAll) { |
| 8835 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8836 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8837 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8838 | if (needSep) { |
| 8839 | pw.println(" "); |
| 8840 | } |
| 8841 | if (dumpAll) { |
| 8842 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8843 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8844 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8845 | if (needSep) { |
| 8846 | pw.println(" "); |
| 8847 | } |
| 8848 | if (dumpAll) { |
| 8849 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8850 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 8851 | needSep = mServices.dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8852 | if (needSep) { |
| 8853 | pw.println(" "); |
| 8854 | } |
| 8855 | if (dumpAll) { |
| 8856 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8857 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8858 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8859 | if (needSep) { |
| 8860 | pw.println(" "); |
| 8861 | } |
| 8862 | if (dumpAll) { |
| 8863 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8864 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8865 | dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8866 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8867 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8868 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8869 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8870 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8871 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8872 | pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)"); |
| 8873 | pw.println(" Main stack:"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8874 | dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient, |
| 8875 | dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8876 | pw.println(" "); |
| 8877 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8878 | dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false, |
| 8879 | dumpPackage); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8880 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8881 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8882 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8883 | dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8884 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8885 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8886 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8887 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8888 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8889 | dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8890 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8891 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 8892 | if (mMainStack.mGoingToSleepActivities.size() > 0) { |
| 8893 | pw.println(" "); |
| 8894 | pw.println(" Activities waiting to sleep:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8895 | dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8896 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 8897 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8898 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8899 | pw.println(" "); |
| 8900 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8901 | dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8902 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8903 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8904 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8905 | pw.println(" "); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 8906 | if (mMainStack.mPausingActivity != null) { |
| 8907 | pw.println(" mPausingActivity: " + mMainStack.mPausingActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8908 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8909 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8910 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8911 | if (dumpAll) { |
| 8912 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
| 8913 | pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 8914 | pw.println(" mDismissKeyguardOnNextActivity: " |
| 8915 | + mMainStack.mDismissKeyguardOnNextActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8917 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8918 | if (mRecentTasks.size() > 0) { |
| 8919 | pw.println(); |
| 8920 | pw.println(" Recent tasks:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8921 | |
| 8922 | final int N = mRecentTasks.size(); |
| 8923 | for (int i=0; i<N; i++) { |
| 8924 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8925 | if (dumpPackage != null) { |
| 8926 | if (tr.realActivity == null || |
| 8927 | !dumpPackage.equals(tr.realActivity)) { |
| 8928 | continue; |
| 8929 | } |
| 8930 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8931 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 8932 | pw.println(tr); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8933 | if (dumpAll) { |
| 8934 | mRecentTasks.get(i).dump(pw, " "); |
| 8935 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8936 | } |
| 8937 | } |
| 8938 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8939 | if (dumpAll) { |
| 8940 | pw.println(" "); |
| 8941 | pw.println(" mCurTask: " + mCurTask); |
| 8942 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8943 | |
| 8944 | return true; |
| 8945 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8946 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8947 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8948 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8949 | boolean needSep = false; |
| 8950 | int numPers = 0; |
| 8951 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8952 | pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)"); |
| 8953 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8954 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8955 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 8956 | final int NA = procs.size(); |
| 8957 | for (int ia=0; ia<NA; ia++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8958 | ProcessRecord r = procs.valueAt(ia); |
| 8959 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8960 | continue; |
| 8961 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8962 | if (!needSep) { |
| 8963 | pw.println(" All known processes:"); |
| 8964 | needSep = true; |
| 8965 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 8966 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 8967 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 8968 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8969 | r.dump(pw, " "); |
| 8970 | if (r.persistent) { |
| 8971 | numPers++; |
| 8972 | } |
| 8973 | } |
| 8974 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8975 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8976 | |
| 8977 | if (mIsolatedProcesses.size() > 0) { |
| 8978 | if (needSep) pw.println(" "); |
| 8979 | needSep = true; |
| 8980 | pw.println(" Isolated process list (sorted by uid):"); |
| 8981 | for (int i=0; i<mIsolatedProcesses.size(); i++) { |
| 8982 | ProcessRecord r = mIsolatedProcesses.valueAt(i); |
| 8983 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 8984 | continue; |
| 8985 | } |
| 8986 | pw.println(String.format("%sIsolated #%2d: %s", |
| 8987 | " ", i, r.toString())); |
| 8988 | } |
| 8989 | } |
| 8990 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8991 | if (mLruProcesses.size() > 0) { |
| 8992 | if (needSep) pw.println(" "); |
| 8993 | needSep = true; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8994 | pw.println(" Process LRU list (sorted by oom_adj):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8995 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8996 | "Proc", "PERS", false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8997 | needSep = true; |
| 8998 | } |
| 8999 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9000 | if (dumpAll) { |
| 9001 | synchronized (mPidsSelfLocked) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9002 | boolean printed = false; |
| 9003 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 9004 | ProcessRecord r = mPidsSelfLocked.valueAt(i); |
| 9005 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9006 | continue; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9007 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9008 | if (!printed) { |
| 9009 | if (needSep) pw.println(" "); |
| 9010 | needSep = true; |
| 9011 | pw.println(" PID mappings:"); |
| 9012 | printed = true; |
| 9013 | } |
| 9014 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 9015 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9016 | } |
| 9017 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9018 | } |
| 9019 | |
| 9020 | if (mForegroundProcesses.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9021 | synchronized (mPidsSelfLocked) { |
| 9022 | boolean printed = false; |
| 9023 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 9024 | ProcessRecord r = mPidsSelfLocked.get( |
| 9025 | mForegroundProcesses.valueAt(i).pid); |
| 9026 | if (dumpPackage != null && (r == null |
| 9027 | || !dumpPackage.equals(r.info.packageName))) { |
| 9028 | continue; |
| 9029 | } |
| 9030 | if (!printed) { |
| 9031 | if (needSep) pw.println(" "); |
| 9032 | needSep = true; |
| 9033 | pw.println(" Foreground Processes:"); |
| 9034 | printed = true; |
| 9035 | } |
| 9036 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 9037 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
| 9038 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9039 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9040 | } |
| 9041 | |
| 9042 | if (mPersistentStartingProcesses.size() > 0) { |
| 9043 | if (needSep) pw.println(" "); |
| 9044 | needSep = true; |
| 9045 | pw.println(" Persisent processes that are starting:"); |
| 9046 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9047 | "Starting Norm", "Restarting PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9048 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9049 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9050 | if (mRemovedProcesses.size() > 0) { |
| 9051 | if (needSep) pw.println(" "); |
| 9052 | needSep = true; |
| 9053 | pw.println(" Processes that are being removed:"); |
| 9054 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9055 | "Removed Norm", "Removed PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9056 | } |
| 9057 | |
| 9058 | if (mProcessesOnHold.size() > 0) { |
| 9059 | if (needSep) pw.println(" "); |
| 9060 | needSep = true; |
| 9061 | pw.println(" Processes that are on old until the system is ready:"); |
| 9062 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9063 | "OnHold Norm", "OnHold PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9064 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9065 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9066 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9067 | |
| 9068 | if (mProcessCrashTimes.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9069 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9070 | long now = SystemClock.uptimeMillis(); |
| 9071 | for (Map.Entry<String, SparseArray<Long>> procs |
| 9072 | : mProcessCrashTimes.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9073 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9074 | SparseArray<Long> uids = procs.getValue(); |
| 9075 | final int N = uids.size(); |
| 9076 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9077 | int puid = uids.keyAt(i); |
| 9078 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 9079 | if (dumpPackage != null && (r == null |
| 9080 | || !dumpPackage.equals(r.info.packageName))) { |
| 9081 | continue; |
| 9082 | } |
| 9083 | if (!printed) { |
| 9084 | if (needSep) pw.println(" "); |
| 9085 | needSep = true; |
| 9086 | pw.println(" Time since processes crashed:"); |
| 9087 | printed = true; |
| 9088 | } |
| 9089 | pw.print(" Process "); pw.print(pname); |
| 9090 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9091 | pw.print(": last crashed "); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 9092 | TimeUtils.formatDuration(now-uids.valueAt(i), pw); |
| 9093 | pw.println(" ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9094 | } |
| 9095 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9096 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9097 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9098 | if (mBadProcesses.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9099 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9100 | for (Map.Entry<String, SparseArray<Long>> procs |
| 9101 | : mBadProcesses.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9102 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9103 | SparseArray<Long> uids = procs.getValue(); |
| 9104 | final int N = uids.size(); |
| 9105 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9106 | int puid = uids.keyAt(i); |
| 9107 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 9108 | if (dumpPackage != null && (r == null |
| 9109 | || !dumpPackage.equals(r.info.packageName))) { |
| 9110 | continue; |
| 9111 | } |
| 9112 | if (!printed) { |
| 9113 | if (needSep) pw.println(" "); |
| 9114 | needSep = true; |
| 9115 | pw.println(" Bad processes:"); |
| 9116 | } |
| 9117 | pw.print(" Bad process "); pw.print(pname); |
| 9118 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9119 | pw.print(": crashed at time "); |
| 9120 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9121 | } |
| 9122 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9123 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9124 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9125 | pw.println(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 9126 | pw.println(" mStartedUsers:"); |
| 9127 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 9128 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 9129 | pw.print(" User #"); pw.print(uss.mHandle.getIdentifier()); |
| 9130 | pw.println(":"); |
| 9131 | uss.dump(" ", pw); |
| 9132 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9133 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9134 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 9135 | if (dumpAll) { |
| 9136 | StringBuilder sb = new StringBuilder(128); |
| 9137 | sb.append(" mPreviousProcessVisibleTime: "); |
| 9138 | TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb); |
| 9139 | pw.println(sb); |
| 9140 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9141 | if (mHeavyWeightProcess != null) { |
| 9142 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 9143 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9144 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9145 | if (dumpAll) { |
| 9146 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | 3d0724d | 2011-05-12 15:39:41 -0700 | [diff] [blame] | 9147 | if (mCompatModePackages.getPackages().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9148 | boolean printed = false; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 9149 | for (Map.Entry<String, Integer> entry |
| 9150 | : mCompatModePackages.getPackages().entrySet()) { |
| 9151 | String pkg = entry.getKey(); |
| 9152 | int mode = entry.getValue(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9153 | if (dumpPackage != null && !dumpPackage.equals(pkg)) { |
| 9154 | continue; |
| 9155 | } |
| 9156 | if (!printed) { |
| 9157 | pw.println(" mScreenCompatPackages:"); |
| 9158 | printed = true; |
| 9159 | } |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 9160 | pw.print(" "); pw.print(pkg); pw.print(": "); |
| 9161 | pw.print(mode); pw.println(); |
| 9162 | } |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 9163 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9164 | } |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 9165 | if (mSleeping || mWentToSleep || mLockScreenShown) { |
| 9166 | pw.println(" mSleeping=" + mSleeping + " mWentToSleep=" + mWentToSleep |
| 9167 | + " mLockScreenShown " + mLockScreenShown); |
| 9168 | } |
| 9169 | if (mShuttingDown) { |
| 9170 | pw.println(" mShuttingDown=" + mShuttingDown); |
| 9171 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9172 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 9173 | || mOrigWaitForDebugger) { |
| 9174 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 9175 | + " mDebugTransient=" + mDebugTransient |
| 9176 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 9177 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 9178 | if (mOpenGlTraceApp != null) { |
| 9179 | pw.println(" mOpenGlTraceApp=" + mOpenGlTraceApp); |
| 9180 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 9181 | if (mProfileApp != null || mProfileProc != null || mProfileFile != null |
| 9182 | || mProfileFd != null) { |
| 9183 | pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc); |
| 9184 | pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd); |
| 9185 | pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler=" |
| 9186 | + mAutoStopProfiler); |
| 9187 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9188 | if (mAlwaysFinishActivities || mController != null) { |
| 9189 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 9190 | + " mController=" + mController); |
| 9191 | } |
| 9192 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9193 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9194 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 9195 | + " mProcessesReady=" + mProcessesReady |
| 9196 | + " mSystemReady=" + mSystemReady); |
| 9197 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9198 | + " mBooted=" + mBooted |
| 9199 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9200 | pw.print(" mLastPowerCheckRealtime="); |
| 9201 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 9202 | pw.println(""); |
| 9203 | pw.print(" mLastPowerCheckUptime="); |
| 9204 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 9205 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9206 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 9207 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 9208 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 9209 | pw.println(" mNumNonHiddenProcs=" + mNumNonHiddenProcs |
| 9210 | + " mNumHiddenProcs=" + mNumHiddenProcs |
| 9211 | + " mNumServiceProcs=" + mNumServiceProcs |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9212 | + " mNewNumServiceProcs=" + mNewNumServiceProcs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9213 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9214 | |
| 9215 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9216 | } |
| 9217 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9218 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9219 | int opti, boolean needSep, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9220 | if (mProcessesToGc.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9221 | boolean printed = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9222 | long now = SystemClock.uptimeMillis(); |
| 9223 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 9224 | ProcessRecord proc = mProcessesToGc.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9225 | if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) { |
| 9226 | continue; |
| 9227 | } |
| 9228 | if (!printed) { |
| 9229 | if (needSep) pw.println(" "); |
| 9230 | needSep = true; |
| 9231 | pw.println(" Processes that are waiting to GC:"); |
| 9232 | printed = true; |
| 9233 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9234 | pw.print(" Process "); pw.println(proc); |
| 9235 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 9236 | pw.print(", last gced="); |
| 9237 | pw.print(now-proc.lastRequestedGc); |
| 9238 | pw.print(" ms ago, last lowMem="); |
| 9239 | pw.print(now-proc.lastLowMemory); |
| 9240 | pw.println(" ms ago"); |
| 9241 | |
| 9242 | } |
| 9243 | } |
| 9244 | return needSep; |
| 9245 | } |
| 9246 | |
| 9247 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 9248 | int opti, boolean dumpAll) { |
| 9249 | boolean needSep = false; |
| 9250 | |
| 9251 | if (mLruProcesses.size() > 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9252 | if (needSep) pw.println(" "); |
| 9253 | needSep = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9254 | pw.println(" OOM levels:"); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9255 | pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9256 | pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9257 | pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ); |
| 9258 | pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ); |
| 9259 | pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9260 | pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9261 | pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9262 | pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9263 | pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9264 | pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9265 | pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9266 | 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] | 9267 | 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] | 9268 | |
| 9269 | if (needSep) pw.println(" "); |
| 9270 | needSep = true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9271 | pw.println(" Process OOM control:"); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9272 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9273 | "Proc", "PERS", true, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9274 | needSep = true; |
| 9275 | } |
| 9276 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9277 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9278 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9279 | pw.println(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9280 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9281 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9282 | if (mHeavyWeightProcess != null) { |
| 9283 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 9284 | } |
| 9285 | |
| 9286 | return true; |
| 9287 | } |
| 9288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9289 | /** |
| 9290 | * There are three ways to call this: |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9291 | * - no provider specified: dump all the providers |
| 9292 | * - a flattened component name that matched an existing provider was specified as the |
| 9293 | * first arg: dump that one provider |
| 9294 | * - the first arg isn't the flattened component name of an existing provider: |
| 9295 | * dump all providers whose component contains the first arg as a substring |
| 9296 | */ |
| 9297 | protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9298 | int opti, boolean dumpAll) { |
Marco Nelissen | de7408c | 2012-02-08 14:57:38 -0800 | [diff] [blame] | 9299 | return mProviderMap.dumpProvider(fd, pw, name, args, opti, dumpAll); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9300 | } |
| 9301 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9302 | static class ItemMatcher { |
| 9303 | ArrayList<ComponentName> components; |
| 9304 | ArrayList<String> strings; |
| 9305 | ArrayList<Integer> objects; |
| 9306 | boolean all; |
| 9307 | |
| 9308 | ItemMatcher() { |
| 9309 | all = true; |
| 9310 | } |
| 9311 | |
| 9312 | void build(String name) { |
| 9313 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 9314 | if (componentName != null) { |
| 9315 | if (components == null) { |
| 9316 | components = new ArrayList<ComponentName>(); |
| 9317 | } |
| 9318 | components.add(componentName); |
| 9319 | all = false; |
| 9320 | } else { |
| 9321 | int objectId = 0; |
| 9322 | // Not a '/' separated full component name; maybe an object ID? |
| 9323 | try { |
| 9324 | objectId = Integer.parseInt(name, 16); |
| 9325 | if (objects == null) { |
| 9326 | objects = new ArrayList<Integer>(); |
| 9327 | } |
| 9328 | objects.add(objectId); |
| 9329 | all = false; |
| 9330 | } catch (RuntimeException e) { |
| 9331 | // Not an integer; just do string match. |
| 9332 | if (strings == null) { |
| 9333 | strings = new ArrayList<String>(); |
| 9334 | } |
| 9335 | strings.add(name); |
| 9336 | all = false; |
| 9337 | } |
| 9338 | } |
| 9339 | } |
| 9340 | |
| 9341 | int build(String[] args, int opti) { |
| 9342 | for (; opti<args.length; opti++) { |
| 9343 | String name = args[opti]; |
| 9344 | if ("--".equals(name)) { |
| 9345 | return opti+1; |
| 9346 | } |
| 9347 | build(name); |
| 9348 | } |
| 9349 | return opti; |
| 9350 | } |
| 9351 | |
| 9352 | boolean match(Object object, ComponentName comp) { |
| 9353 | if (all) { |
| 9354 | return true; |
| 9355 | } |
| 9356 | if (components != null) { |
| 9357 | for (int i=0; i<components.size(); i++) { |
| 9358 | if (components.get(i).equals(comp)) { |
| 9359 | return true; |
| 9360 | } |
| 9361 | } |
| 9362 | } |
| 9363 | if (objects != null) { |
| 9364 | for (int i=0; i<objects.size(); i++) { |
| 9365 | if (System.identityHashCode(object) == objects.get(i)) { |
| 9366 | return true; |
| 9367 | } |
| 9368 | } |
| 9369 | } |
| 9370 | if (strings != null) { |
| 9371 | String flat = comp.flattenToString(); |
| 9372 | for (int i=0; i<strings.size(); i++) { |
| 9373 | if (flat.contains(strings.get(i))) { |
| 9374 | return true; |
| 9375 | } |
| 9376 | } |
| 9377 | } |
| 9378 | return false; |
| 9379 | } |
| 9380 | } |
| 9381 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9382 | /** |
| 9383 | * There are three things that cmd can be: |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9384 | * - a flattened component name that matches an existing activity |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9385 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 9386 | * dump all activity whose component contains the cmd as a substring |
| 9387 | * - A hex number of the ActivityRecord object instance. |
| 9388 | */ |
| 9389 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9390 | int opti, boolean dumpAll) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9391 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9392 | |
| 9393 | if ("all".equals(name)) { |
| 9394 | synchronized (this) { |
| 9395 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9396 | activities.add(r1); |
| 9397 | } |
| 9398 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 9399 | } else if ("top".equals(name)) { |
| 9400 | synchronized (this) { |
| 9401 | final int N = mMainStack.mHistory.size(); |
| 9402 | if (N > 0) { |
| 9403 | activities.add((ActivityRecord)mMainStack.mHistory.get(N-1)); |
| 9404 | } |
| 9405 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9406 | } else { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9407 | ItemMatcher matcher = new ItemMatcher(); |
| 9408 | matcher.build(name); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9409 | |
| 9410 | synchronized (this) { |
| 9411 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9412 | if (matcher.match(r1, r1.intent.getComponent())) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9413 | activities.add(r1); |
| 9414 | } |
| 9415 | } |
| 9416 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9417 | } |
| 9418 | |
| 9419 | if (activities.size() <= 0) { |
| 9420 | return false; |
| 9421 | } |
| 9422 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9423 | String[] newArgs = new String[args.length - opti]; |
| 9424 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 9425 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9426 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9427 | boolean needSep = false; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9428 | for (int i=activities.size()-1; i>=0; i--) { |
| 9429 | ActivityRecord r = (ActivityRecord)activities.get(i); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9430 | if (needSep) { |
| 9431 | pw.println(); |
| 9432 | } |
| 9433 | needSep = true; |
| 9434 | synchronized (this) { |
| 9435 | if (lastTask != r.task) { |
| 9436 | lastTask = r.task; |
| 9437 | pw.print("TASK "); pw.print(lastTask.affinity); |
| 9438 | pw.print(" id="); pw.println(lastTask.taskId); |
| 9439 | if (dumpAll) { |
| 9440 | lastTask.dump(pw, " "); |
| 9441 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9442 | } |
| 9443 | } |
| 9444 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9445 | } |
| 9446 | return true; |
| 9447 | } |
| 9448 | |
| 9449 | /** |
| 9450 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 9451 | * there is a thread associated with the activity. |
| 9452 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9453 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9454 | final ActivityRecord r, String[] args, boolean dumpAll) { |
| 9455 | String innerPrefix = prefix + " "; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9456 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9457 | pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName); |
| 9458 | pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 9459 | pw.print(" pid="); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9460 | if (r.app != null) pw.println(r.app.pid); |
| 9461 | else pw.println("(not running)"); |
| 9462 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9463 | r.dump(pw, innerPrefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9464 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9465 | } |
| 9466 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9467 | // flush anything that is already in the PrintWriter since the thread is going |
| 9468 | // to write to the file descriptor directly |
| 9469 | pw.flush(); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9470 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9471 | TransferPipe tp = new TransferPipe(); |
| 9472 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9473 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9474 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9475 | tp.go(fd); |
| 9476 | } finally { |
| 9477 | tp.kill(); |
| 9478 | } |
| 9479 | } catch (IOException e) { |
| 9480 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9481 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9482 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9483 | } |
| 9484 | } |
| 9485 | } |
| 9486 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9487 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9488 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9489 | boolean needSep = false; |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9490 | boolean onlyHistory = false; |
| 9491 | |
| 9492 | if ("history".equals(dumpPackage)) { |
| 9493 | onlyHistory = true; |
| 9494 | dumpPackage = null; |
| 9495 | } |
| 9496 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9497 | pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)"); |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9498 | if (!onlyHistory && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9499 | if (mRegisteredReceivers.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9500 | boolean printed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9501 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 9502 | while (it.hasNext()) { |
| 9503 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9504 | if (dumpPackage != null && (r.app == null || |
| 9505 | !dumpPackage.equals(r.app.info.packageName))) { |
| 9506 | continue; |
| 9507 | } |
| 9508 | if (!printed) { |
| 9509 | pw.println(" Registered Receivers:"); |
| 9510 | needSep = true; |
| 9511 | printed = true; |
| 9512 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9513 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9514 | r.dump(pw, " "); |
| 9515 | } |
| 9516 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9517 | |
| 9518 | if (mReceiverResolver.dump(pw, needSep ? |
| 9519 | "\n Receiver Resolver Table:" : " Receiver Resolver Table:", |
| 9520 | " ", dumpPackage, false)) { |
| 9521 | needSep = true; |
| 9522 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9523 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9524 | |
| 9525 | for (BroadcastQueue q : mBroadcastQueues) { |
| 9526 | needSep = q.dumpLocked(fd, pw, args, opti, dumpAll, dumpPackage, needSep); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9527 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9528 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9529 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9530 | |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9531 | if (!onlyHistory && mStickyBroadcasts != null && dumpPackage == null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 9532 | for (int user=0; user<mStickyBroadcasts.size(); user++) { |
| 9533 | if (needSep) { |
| 9534 | pw.println(); |
| 9535 | } |
| 9536 | needSep = true; |
| 9537 | pw.print(" Sticky broadcasts for user "); |
| 9538 | pw.print(mStickyBroadcasts.keyAt(user)); pw.println(":"); |
| 9539 | StringBuilder sb = new StringBuilder(128); |
| 9540 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 9541 | : mStickyBroadcasts.valueAt(user).entrySet()) { |
| 9542 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
| 9543 | if (dumpAll) { |
| 9544 | pw.println(":"); |
| 9545 | ArrayList<Intent> intents = ent.getValue(); |
| 9546 | final int N = intents.size(); |
| 9547 | for (int i=0; i<N; i++) { |
| 9548 | sb.setLength(0); |
| 9549 | sb.append(" Intent: "); |
| 9550 | intents.get(i).toShortString(sb, false, true, false, false); |
| 9551 | pw.println(sb.toString()); |
| 9552 | Bundle bundle = intents.get(i).getExtras(); |
| 9553 | if (bundle != null) { |
| 9554 | pw.print(" "); |
| 9555 | pw.println(bundle.toString()); |
| 9556 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9557 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 9558 | } else { |
| 9559 | pw.println(""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9560 | } |
| 9561 | } |
| 9562 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9563 | } |
| 9564 | |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9565 | if (!onlyHistory && dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9566 | pw.println(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9567 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 9568 | pw.println(" mBroadcastsScheduled [" + queue.mQueueName + "]=" |
| 9569 | + queue.mBroadcastsScheduled); |
| 9570 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9571 | pw.println(" mHandler:"); |
| 9572 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9573 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9574 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9575 | |
| 9576 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9577 | } |
| 9578 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9579 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9580 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 9581 | boolean needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9582 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9583 | ItemMatcher matcher = new ItemMatcher(); |
| 9584 | matcher.build(args, opti); |
| 9585 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9586 | pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9587 | |
| 9588 | mProviderMap.dumpProvidersLocked(pw, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9589 | |
| 9590 | if (mLaunchingProviders.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9591 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9592 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9593 | ContentProviderRecord r = mLaunchingProviders.get(i); |
| 9594 | if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) { |
| 9595 | continue; |
| 9596 | } |
| 9597 | if (!printed) { |
| 9598 | if (needSep) pw.println(" "); |
| 9599 | needSep = true; |
| 9600 | pw.println(" Launching content providers:"); |
| 9601 | printed = true; |
| 9602 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9603 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9604 | pw.println(r); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9605 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9606 | } |
| 9607 | |
| 9608 | if (mGrantedUriPermissions.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9609 | if (needSep) pw.println(); |
| 9610 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9611 | pw.println("Granted Uri Permissions:"); |
| 9612 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 9613 | int uid = mGrantedUriPermissions.keyAt(i); |
| 9614 | HashMap<Uri, UriPermission> perms |
| 9615 | = mGrantedUriPermissions.valueAt(i); |
| 9616 | pw.print(" * UID "); pw.print(uid); |
| 9617 | pw.println(" holds:"); |
| 9618 | for (UriPermission perm : perms.values()) { |
| 9619 | pw.print(" "); pw.println(perm); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9620 | if (dumpAll) { |
| 9621 | perm.dump(pw, " "); |
| 9622 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9623 | } |
| 9624 | } |
| 9625 | needSep = true; |
| 9626 | } |
| 9627 | |
| 9628 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9629 | } |
| 9630 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9631 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9632 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9633 | boolean needSep = false; |
| 9634 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9635 | if (mIntentSenderRecords.size() > 0) { |
| 9636 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9637 | Iterator<WeakReference<PendingIntentRecord>> it |
| 9638 | = mIntentSenderRecords.values().iterator(); |
| 9639 | while (it.hasNext()) { |
| 9640 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 9641 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9642 | if (dumpPackage != null && (rec == null |
| 9643 | || !dumpPackage.equals(rec.key.packageName))) { |
| 9644 | continue; |
| 9645 | } |
| 9646 | if (!printed) { |
| 9647 | pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)"); |
| 9648 | printed = true; |
| 9649 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9650 | needSep = true; |
| 9651 | if (rec != null) { |
| 9652 | pw.print(" * "); pw.println(rec); |
| 9653 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9654 | rec.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9655 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9656 | } else { |
| 9657 | pw.print(" * "); pw.println(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9658 | } |
| 9659 | } |
| 9660 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9661 | |
| 9662 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9663 | } |
| 9664 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9665 | private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9666 | String prefix, String label, boolean complete, boolean brief, boolean client, |
| 9667 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9668 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9669 | boolean needNL = false; |
| 9670 | final String innerPrefix = prefix + " "; |
| 9671 | final String[] args = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9672 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9673 | final ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9674 | if (dumpPackage != null && !dumpPackage.equals(r.packageName)) { |
| 9675 | continue; |
| 9676 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 9677 | final boolean full = !brief && (complete || !r.isInHistory()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9678 | if (needNL) { |
| 9679 | pw.println(" "); |
| 9680 | needNL = false; |
| 9681 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9682 | if (lastTask != r.task) { |
| 9683 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9684 | pw.print(prefix); |
| 9685 | pw.print(full ? "* " : " "); |
| 9686 | pw.println(lastTask); |
| 9687 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9688 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9689 | } else if (complete) { |
| 9690 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 9691 | if (lastTask.intent != null) { |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9692 | pw.print(prefix); pw.print(" "); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 9693 | pw.println(lastTask.intent.toInsecureStringWithClip()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9694 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9695 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9696 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9697 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 9698 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 9699 | pw.println(r); |
| 9700 | if (full) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9701 | r.dump(pw, innerPrefix); |
| 9702 | } else if (complete) { |
| 9703 | // Complete + brief == give a summary. Isn't that obvious?!? |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9704 | pw.print(innerPrefix); pw.println(r.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9705 | if (r.app != null) { |
| 9706 | pw.print(innerPrefix); pw.println(r.app); |
| 9707 | } |
| 9708 | } |
| 9709 | if (client && r.app != null && r.app.thread != null) { |
| 9710 | // flush anything that is already in the PrintWriter since the thread is going |
| 9711 | // to write to the file descriptor directly |
| 9712 | pw.flush(); |
| 9713 | try { |
| 9714 | TransferPipe tp = new TransferPipe(); |
| 9715 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9716 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9717 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9718 | // Short timeout, since blocking here can |
| 9719 | // deadlock with the application. |
| 9720 | tp.go(fd, 2000); |
| 9721 | } finally { |
| 9722 | tp.kill(); |
| 9723 | } |
| 9724 | } catch (IOException e) { |
| 9725 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
| 9726 | } catch (RemoteException e) { |
| 9727 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
| 9728 | } |
| 9729 | needNL = true; |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9730 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9731 | } |
| 9732 | } |
| 9733 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 9734 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 9735 | if (val == base) { |
| 9736 | if (space == null) return prefix; |
| 9737 | return prefix + " "; |
| 9738 | } |
| 9739 | return prefix + "+" + Integer.toString(val-base); |
| 9740 | } |
| 9741 | |
| 9742 | private static final int dumpProcessList(PrintWriter pw, |
| 9743 | ActivityManagerService service, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9744 | String prefix, String normalLabel, String persistentLabel, |
| 9745 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9746 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 9747 | final int N = list.size()-1; |
| 9748 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9749 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9750 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9751 | continue; |
| 9752 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9753 | pw.println(String.format("%s%s #%2d: %s", |
| 9754 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 9755 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9756 | if (r.persistent) { |
| 9757 | numPers++; |
| 9758 | } |
| 9759 | } |
| 9760 | return numPers; |
| 9761 | } |
| 9762 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9763 | private static final boolean dumpProcessOomList(PrintWriter pw, |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9764 | ActivityManagerService service, List<ProcessRecord> origList, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9765 | String prefix, String normalLabel, String persistentLabel, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9766 | boolean inclDetails, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9767 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9768 | ArrayList<Pair<ProcessRecord, Integer>> list |
| 9769 | = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size()); |
| 9770 | for (int i=0; i<origList.size(); i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9771 | ProcessRecord r = origList.get(i); |
| 9772 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9773 | continue; |
| 9774 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9775 | list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i)); |
| 9776 | } |
| 9777 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9778 | if (list.size() <= 0) { |
| 9779 | return false; |
| 9780 | } |
| 9781 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9782 | Comparator<Pair<ProcessRecord, Integer>> comparator |
| 9783 | = new Comparator<Pair<ProcessRecord, Integer>>() { |
| 9784 | @Override |
| 9785 | public int compare(Pair<ProcessRecord, Integer> object1, |
| 9786 | Pair<ProcessRecord, Integer> object2) { |
| 9787 | if (object1.first.setAdj != object2.first.setAdj) { |
| 9788 | return object1.first.setAdj > object2.first.setAdj ? -1 : 1; |
| 9789 | } |
| 9790 | if (object1.second.intValue() != object2.second.intValue()) { |
| 9791 | return object1.second.intValue() > object2.second.intValue() ? -1 : 1; |
| 9792 | } |
| 9793 | return 0; |
| 9794 | } |
| 9795 | }; |
| 9796 | |
| 9797 | Collections.sort(list, comparator); |
| 9798 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9799 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 9800 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 9801 | final long curUptime = SystemClock.uptimeMillis(); |
| 9802 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 9803 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9804 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9805 | ProcessRecord r = list.get(i).first; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9806 | String oomAdj; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9807 | if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9808 | oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9809 | } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) { |
| 9810 | oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9811 | } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) { |
| 9812 | oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9813 | } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) { |
| 9814 | oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9815 | } else if (r.setAdj >= ProcessList.SERVICE_ADJ) { |
| 9816 | oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9817 | } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 9818 | oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9819 | } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 9820 | oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9821 | } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 9822 | oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9823 | } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 9824 | oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ); |
| 9825 | } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 9826 | oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9827 | } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) { |
| 9828 | oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9829 | } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) { |
| 9830 | oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9831 | } else { |
| 9832 | oomAdj = Integer.toString(r.setAdj); |
| 9833 | } |
| 9834 | String schedGroup; |
| 9835 | switch (r.setSchedGroup) { |
| 9836 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 9837 | schedGroup = "B"; |
| 9838 | break; |
| 9839 | case Process.THREAD_GROUP_DEFAULT: |
| 9840 | schedGroup = "F"; |
| 9841 | break; |
| 9842 | default: |
| 9843 | schedGroup = Integer.toString(r.setSchedGroup); |
| 9844 | break; |
| 9845 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 9846 | String foreground; |
| 9847 | if (r.foregroundActivities) { |
| 9848 | foreground = "A"; |
| 9849 | } else if (r.foregroundServices) { |
| 9850 | foreground = "S"; |
| 9851 | } else { |
| 9852 | foreground = " "; |
| 9853 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 9854 | 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] | 9855 | prefix, (r.persistent ? persistentLabel : normalLabel), |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9856 | (origList.size()-1)-list.get(i).second, oomAdj, schedGroup, |
| 9857 | foreground, r.trimMemoryLevel, r.toShortString(), r.adjType)); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9858 | if (r.adjSource != null || r.adjTarget != null) { |
| 9859 | pw.print(prefix); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9860 | pw.print(" "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9861 | if (r.adjTarget instanceof ComponentName) { |
| 9862 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 9863 | } else if (r.adjTarget != null) { |
| 9864 | pw.print(r.adjTarget.toString()); |
| 9865 | } else { |
| 9866 | pw.print("{null}"); |
| 9867 | } |
| 9868 | pw.print("<="); |
| 9869 | if (r.adjSource instanceof ProcessRecord) { |
| 9870 | pw.print("Proc{"); |
| 9871 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 9872 | pw.println("}"); |
| 9873 | } else if (r.adjSource != null) { |
| 9874 | pw.println(r.adjSource.toString()); |
| 9875 | } else { |
| 9876 | pw.println("{null}"); |
| 9877 | } |
| 9878 | } |
| 9879 | if (inclDetails) { |
| 9880 | pw.print(prefix); |
| 9881 | pw.print(" "); |
| 9882 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 9883 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 9884 | pw.print(" empty="); pw.print(r.emptyAdj); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9885 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 9886 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 9887 | pw.print(" cur="); pw.print(r.curAdj); |
| 9888 | pw.print(" set="); pw.println(r.setAdj); |
| 9889 | pw.print(prefix); |
| 9890 | pw.print(" "); |
| 9891 | pw.print("keeping="); pw.print(r.keeping); |
| 9892 | pw.print(" hidden="); pw.print(r.hidden); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9893 | pw.print(" empty="); pw.print(r.empty); |
| 9894 | pw.print(" hasAboveClient="); pw.println(r.hasAboveClient); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9895 | |
| 9896 | if (!r.keeping) { |
| 9897 | if (r.lastWakeTime != 0) { |
| 9898 | long wtime; |
| 9899 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 9900 | synchronized (stats) { |
| 9901 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 9902 | r.pid, curRealtime); |
| 9903 | } |
| 9904 | long timeUsed = wtime - r.lastWakeTime; |
| 9905 | pw.print(prefix); |
| 9906 | pw.print(" "); |
| 9907 | pw.print("keep awake over "); |
| 9908 | TimeUtils.formatDuration(realtimeSince, pw); |
| 9909 | pw.print(" used "); |
| 9910 | TimeUtils.formatDuration(timeUsed, pw); |
| 9911 | pw.print(" ("); |
| 9912 | pw.print((timeUsed*100)/realtimeSince); |
| 9913 | pw.println("%)"); |
| 9914 | } |
| 9915 | if (r.lastCpuTime != 0) { |
| 9916 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 9917 | pw.print(prefix); |
| 9918 | pw.print(" "); |
| 9919 | pw.print("run cpu over "); |
| 9920 | TimeUtils.formatDuration(uptimeSince, pw); |
| 9921 | pw.print(" used "); |
| 9922 | TimeUtils.formatDuration(timeUsed, pw); |
| 9923 | pw.print(" ("); |
| 9924 | pw.print((timeUsed*100)/uptimeSince); |
| 9925 | pw.println("%)"); |
| 9926 | } |
| 9927 | } |
| 9928 | } |
| 9929 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9930 | return true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9931 | } |
| 9932 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9933 | ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9934 | ArrayList<ProcessRecord> procs; |
| 9935 | synchronized (this) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9936 | if (args != null && args.length > start |
| 9937 | && args[start].charAt(0) != '-') { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9938 | procs = new ArrayList<ProcessRecord>(); |
| 9939 | int pid = -1; |
| 9940 | try { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9941 | pid = Integer.parseInt(args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9942 | } catch (NumberFormatException e) { |
| 9943 | |
| 9944 | } |
| 9945 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 9946 | ProcessRecord proc = mLruProcesses.get(i); |
| 9947 | if (proc.pid == pid) { |
| 9948 | procs.add(proc); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9949 | } else if (proc.processName.equals(args[start])) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9950 | procs.add(proc); |
| 9951 | } |
| 9952 | } |
| 9953 | if (procs.size() <= 0) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9954 | pw.println("No process found for: " + args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9955 | return null; |
| 9956 | } |
| 9957 | } else { |
| 9958 | procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 9959 | } |
| 9960 | } |
| 9961 | return procs; |
| 9962 | } |
| 9963 | |
| 9964 | final void dumpGraphicsHardwareUsage(FileDescriptor fd, |
| 9965 | PrintWriter pw, String[] args) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 9966 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9967 | if (procs == null) { |
| 9968 | return; |
| 9969 | } |
| 9970 | |
| 9971 | long uptime = SystemClock.uptimeMillis(); |
| 9972 | long realtime = SystemClock.elapsedRealtime(); |
| 9973 | pw.println("Applications Graphics Acceleration Info:"); |
| 9974 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 9975 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9976 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 9977 | ProcessRecord r = procs.get(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9978 | if (r.thread != null) { |
| 9979 | pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **"); |
| 9980 | pw.flush(); |
| 9981 | try { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9982 | TransferPipe tp = new TransferPipe(); |
| 9983 | try { |
| 9984 | r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 9985 | tp.go(fd); |
| 9986 | } finally { |
| 9987 | tp.kill(); |
| 9988 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9989 | } catch (IOException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9990 | pw.println("Failure while dumping the app: " + r); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9991 | pw.flush(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9992 | } catch (RemoteException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 9993 | pw.println("Got a RemoteException while dumping the app " + r); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9994 | pw.flush(); |
| 9995 | } |
| 9996 | } |
| 9997 | } |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 9998 | } |
| 9999 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 10000 | final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 10001 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
| 10002 | if (procs == null) { |
| 10003 | return; |
| 10004 | } |
| 10005 | |
| 10006 | pw.println("Applications Database Info:"); |
| 10007 | |
| 10008 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10009 | ProcessRecord r = procs.get(i); |
| 10010 | if (r.thread != null) { |
| 10011 | pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **"); |
| 10012 | pw.flush(); |
| 10013 | try { |
| 10014 | TransferPipe tp = new TransferPipe(); |
| 10015 | try { |
| 10016 | r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 10017 | tp.go(fd); |
| 10018 | } finally { |
| 10019 | tp.kill(); |
| 10020 | } |
| 10021 | } catch (IOException e) { |
| 10022 | pw.println("Failure while dumping the app: " + r); |
| 10023 | pw.flush(); |
| 10024 | } catch (RemoteException e) { |
| 10025 | pw.println("Got a RemoteException while dumping the app " + r); |
| 10026 | pw.flush(); |
| 10027 | } |
| 10028 | } |
| 10029 | } |
| 10030 | } |
| 10031 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10032 | final static class MemItem { |
| 10033 | final String label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10034 | final String shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10035 | final long pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10036 | final int id; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10037 | ArrayList<MemItem> subitems; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10038 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10039 | public MemItem(String _label, String _shortLabel, long _pss, int _id) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10040 | label = _label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10041 | shortLabel = _shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10042 | pss = _pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10043 | id = _id; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10044 | } |
| 10045 | } |
| 10046 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10047 | static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10048 | boolean sort) { |
| 10049 | if (sort) { |
| 10050 | Collections.sort(items, new Comparator<MemItem>() { |
| 10051 | @Override |
| 10052 | public int compare(MemItem lhs, MemItem rhs) { |
| 10053 | if (lhs.pss < rhs.pss) { |
| 10054 | return 1; |
| 10055 | } else if (lhs.pss > rhs.pss) { |
| 10056 | return -1; |
| 10057 | } |
| 10058 | return 0; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10059 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10060 | }); |
| 10061 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10062 | |
| 10063 | for (int i=0; i<items.size(); i++) { |
| 10064 | MemItem mi = items.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10065 | 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] | 10066 | if (mi.subitems != null) { |
| 10067 | dumpMemItems(pw, prefix + " ", mi.subitems, true); |
| 10068 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10069 | } |
| 10070 | } |
| 10071 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10072 | // These are in KB. |
| 10073 | static final long[] DUMP_MEM_BUCKETS = new long[] { |
| 10074 | 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024, |
| 10075 | 120*1024, 160*1024, 200*1024, |
| 10076 | 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024, |
| 10077 | 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024 |
| 10078 | }; |
| 10079 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10080 | static final void appendMemBucket(StringBuilder out, long memKB, String label, |
| 10081 | boolean stackLike) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10082 | int start = label.lastIndexOf('.'); |
| 10083 | if (start >= 0) start++; |
| 10084 | else start = 0; |
| 10085 | int end = label.length(); |
| 10086 | for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) { |
| 10087 | if (DUMP_MEM_BUCKETS[i] >= memKB) { |
| 10088 | long bucket = DUMP_MEM_BUCKETS[i]/1024; |
| 10089 | out.append(bucket); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10090 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10091 | out.append(label, start, end); |
| 10092 | return; |
| 10093 | } |
| 10094 | } |
| 10095 | out.append(memKB/1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10096 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10097 | out.append(label, start, end); |
| 10098 | } |
| 10099 | |
| 10100 | static final int[] DUMP_MEM_OOM_ADJ = new int[] { |
| 10101 | ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ, |
| 10102 | ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ, |
| 10103 | ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ, |
| 10104 | ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ |
| 10105 | }; |
| 10106 | static final String[] DUMP_MEM_OOM_LABEL = new String[] { |
| 10107 | "System", "Persistent", "Foreground", |
| 10108 | "Visible", "Perceptible", "Heavy Weight", |
| 10109 | "Backup", "A Services", "Home", "Previous", |
| 10110 | "B Services", "Background" |
| 10111 | }; |
| 10112 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10113 | final void dumpApplicationMemoryUsage(FileDescriptor fd, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10114 | PrintWriter pw, String prefix, String[] args, boolean brief, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10115 | PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10116 | boolean dumpAll = false; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10117 | boolean oomOnly = false; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10118 | |
| 10119 | int opti = 0; |
| 10120 | while (opti < args.length) { |
| 10121 | String opt = args[opti]; |
| 10122 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 10123 | break; |
| 10124 | } |
| 10125 | opti++; |
| 10126 | if ("-a".equals(opt)) { |
| 10127 | dumpAll = true; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10128 | } else if ("--oom".equals(opt)) { |
| 10129 | oomOnly = true; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10130 | } else if ("-h".equals(opt)) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10131 | pw.println("meminfo dump options: [-a] [--oom] [process]"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10132 | pw.println(" -a: include all available information for each process."); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10133 | pw.println(" --oom: only show processes organized by oom adj."); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10134 | pw.println("If [process] is specified it can be the name or "); |
| 10135 | pw.println("pid of a specific process to dump."); |
| 10136 | return; |
| 10137 | } else { |
| 10138 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 10139 | } |
| 10140 | } |
| 10141 | |
| 10142 | ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10143 | if (procs == null) { |
| 10144 | return; |
| 10145 | } |
| 10146 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 10147 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10148 | long uptime = SystemClock.uptimeMillis(); |
| 10149 | long realtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10150 | |
| 10151 | if (procs.size() == 1 || isCheckinRequest) { |
| 10152 | dumpAll = true; |
| 10153 | } |
| 10154 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10155 | if (isCheckinRequest) { |
| 10156 | // short checkin version |
| 10157 | pw.println(uptime + "," + realtime); |
| 10158 | pw.flush(); |
| 10159 | } else { |
| 10160 | pw.println("Applications Memory Usage (kB):"); |
| 10161 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 10162 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10163 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10164 | String[] innerArgs = new String[args.length-opti]; |
| 10165 | System.arraycopy(args, opti, innerArgs, 0, args.length-opti); |
| 10166 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10167 | ArrayList<MemItem> procMems = new ArrayList<MemItem>(); |
| 10168 | long nativePss=0, dalvikPss=0, otherPss=0; |
| 10169 | long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS]; |
| 10170 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10171 | long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length]; |
| 10172 | ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[]) |
| 10173 | new ArrayList[DUMP_MEM_OOM_LABEL.length]; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10174 | |
| 10175 | long totalPss = 0; |
| 10176 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10177 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10178 | ProcessRecord r = procs.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10179 | if (r.thread != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10180 | if (!isCheckinRequest && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10181 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 10182 | pw.flush(); |
| 10183 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10184 | Debug.MemoryInfo mi = null; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10185 | if (dumpAll) { |
| 10186 | try { |
| 10187 | mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs); |
| 10188 | } catch (RemoteException e) { |
| 10189 | if (!isCheckinRequest) { |
| 10190 | pw.println("Got RemoteException!"); |
| 10191 | pw.flush(); |
| 10192 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10193 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10194 | } else { |
| 10195 | mi = new Debug.MemoryInfo(); |
| 10196 | Debug.getMemoryInfo(r.pid, mi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10197 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10198 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10199 | if (!isCheckinRequest && mi != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10200 | long myTotalPss = mi.getTotalPss(); |
| 10201 | totalPss += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10202 | MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")", |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10203 | r.processName, myTotalPss, 0); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10204 | procMems.add(pssItem); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10205 | |
| 10206 | nativePss += mi.nativePss; |
| 10207 | dalvikPss += mi.dalvikPss; |
| 10208 | otherPss += mi.otherPss; |
| 10209 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
| 10210 | long mem = mi.getOtherPss(j); |
| 10211 | miscPss[j] += mem; |
| 10212 | otherPss -= mem; |
| 10213 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10214 | |
| 10215 | for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10216 | if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex] |
| 10217 | || oomIndex == (oomPss.length-1)) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10218 | oomPss[oomIndex] += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10219 | if (oomProcs[oomIndex] == null) { |
| 10220 | oomProcs[oomIndex] = new ArrayList<MemItem>(); |
| 10221 | } |
| 10222 | oomProcs[oomIndex].add(pssItem); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10223 | break; |
| 10224 | } |
| 10225 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10226 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10227 | } |
| 10228 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10229 | |
| 10230 | if (!isCheckinRequest && procs.size() > 1) { |
| 10231 | ArrayList<MemItem> catMems = new ArrayList<MemItem>(); |
| 10232 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10233 | catMems.add(new MemItem("Native", "Native", nativePss, -1)); |
| 10234 | catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2)); |
| 10235 | catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10236 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10237 | String label = Debug.MemoryInfo.getOtherLabel(j); |
| 10238 | catMems.add(new MemItem(label, label, miscPss[j], j)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10239 | } |
| 10240 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10241 | ArrayList<MemItem> oomMems = new ArrayList<MemItem>(); |
| 10242 | for (int j=0; j<oomPss.length; j++) { |
| 10243 | if (oomPss[j] != 0) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10244 | String label = DUMP_MEM_OOM_LABEL[j]; |
| 10245 | MemItem item = new MemItem(label, label, oomPss[j], |
| 10246 | DUMP_MEM_OOM_ADJ[j]); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10247 | item.subitems = oomProcs[j]; |
| 10248 | oomMems.add(item); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10249 | } |
| 10250 | } |
| 10251 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10252 | if (outTag != null || outStack != null) { |
| 10253 | if (outTag != null) { |
| 10254 | appendMemBucket(outTag, totalPss, "total", false); |
| 10255 | } |
| 10256 | if (outStack != null) { |
| 10257 | appendMemBucket(outStack, totalPss, "total", true); |
| 10258 | } |
| 10259 | boolean firstLine = true; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10260 | for (int i=0; i<oomMems.size(); i++) { |
| 10261 | MemItem miCat = oomMems.get(i); |
| 10262 | if (miCat.subitems == null || miCat.subitems.size() < 1) { |
| 10263 | continue; |
| 10264 | } |
| 10265 | if (miCat.id < ProcessList.SERVICE_ADJ |
| 10266 | || miCat.id == ProcessList.HOME_APP_ADJ |
| 10267 | || miCat.id == ProcessList.PREVIOUS_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10268 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10269 | outTag.append(" / "); |
| 10270 | } |
| 10271 | if (outStack != null) { |
| 10272 | if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10273 | if (firstLine) { |
| 10274 | outStack.append(":"); |
| 10275 | firstLine = false; |
| 10276 | } |
| 10277 | outStack.append("\n\t at "); |
| 10278 | } else { |
| 10279 | outStack.append("$"); |
| 10280 | } |
| 10281 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10282 | for (int j=0; j<miCat.subitems.size(); j++) { |
| 10283 | MemItem mi = miCat.subitems.get(j); |
| 10284 | if (j > 0) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10285 | if (outTag != null) { |
| 10286 | outTag.append(" "); |
| 10287 | } |
| 10288 | if (outStack != null) { |
| 10289 | outStack.append("$"); |
| 10290 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10291 | } |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10292 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10293 | appendMemBucket(outTag, mi.pss, mi.shortLabel, false); |
| 10294 | } |
| 10295 | if (outStack != null) { |
| 10296 | appendMemBucket(outStack, mi.pss, mi.shortLabel, true); |
| 10297 | } |
| 10298 | } |
| 10299 | if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10300 | outStack.append("("); |
| 10301 | for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) { |
| 10302 | if (DUMP_MEM_OOM_ADJ[k] == miCat.id) { |
| 10303 | outStack.append(DUMP_MEM_OOM_LABEL[k]); |
| 10304 | outStack.append(":"); |
| 10305 | outStack.append(DUMP_MEM_OOM_ADJ[k]); |
| 10306 | } |
| 10307 | } |
| 10308 | outStack.append(")"); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10309 | } |
| 10310 | } |
| 10311 | } |
| 10312 | } |
| 10313 | |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10314 | if (!brief && !oomOnly) { |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10315 | pw.println(); |
| 10316 | pw.println("Total PSS by process:"); |
| 10317 | dumpMemItems(pw, " ", procMems, true); |
| 10318 | pw.println(); |
| 10319 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10320 | pw.println("Total PSS by OOM adjustment:"); |
| 10321 | dumpMemItems(pw, " ", oomMems, false); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10322 | if (!oomOnly) { |
| 10323 | PrintWriter out = categoryPw != null ? categoryPw : pw; |
| 10324 | out.println(); |
| 10325 | out.println("Total PSS by category:"); |
| 10326 | dumpMemItems(out, " ", catMems, true); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10327 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10328 | pw.println(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10329 | pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB"); |
Dianne Hackborn | d3e677b | 2012-04-05 14:58:18 -0700 | [diff] [blame] | 10330 | final int[] SINGLE_LONG_FORMAT = new int[] { |
| 10331 | Process.PROC_SPACE_TERM|Process.PROC_OUT_LONG |
| 10332 | }; |
| 10333 | long[] longOut = new long[1]; |
| 10334 | Process.readProcFile("/sys/kernel/mm/ksm/pages_shared", |
| 10335 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10336 | long shared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10337 | longOut[0] = 0; |
| 10338 | Process.readProcFile("/sys/kernel/mm/ksm/pages_sharing", |
| 10339 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10340 | long sharing = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10341 | longOut[0] = 0; |
| 10342 | Process.readProcFile("/sys/kernel/mm/ksm/pages_unshared", |
| 10343 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10344 | long unshared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10345 | longOut[0] = 0; |
| 10346 | Process.readProcFile("/sys/kernel/mm/ksm/pages_volatile", |
| 10347 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10348 | long voltile = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10349 | pw.print(" KSM: "); pw.print(sharing); pw.print(" kB saved from shared "); |
| 10350 | pw.print(shared); pw.println(" kB"); |
| 10351 | pw.print(" "); pw.print(unshared); pw.print(" kB unshared; "); |
| 10352 | pw.print(voltile); pw.println(" kB volatile"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10353 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10354 | } |
| 10355 | |
| 10356 | /** |
| 10357 | * Searches array of arguments for the specified string |
| 10358 | * @param args array of argument strings |
| 10359 | * @param value value to search for |
| 10360 | * @return true if the value is contained in the array |
| 10361 | */ |
| 10362 | private static boolean scanArgs(String[] args, String value) { |
| 10363 | if (args != null) { |
| 10364 | for (String arg : args) { |
| 10365 | if (value.equals(arg)) { |
| 10366 | return true; |
| 10367 | } |
| 10368 | } |
| 10369 | } |
| 10370 | return false; |
| 10371 | } |
| 10372 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10373 | private final boolean removeDyingProviderLocked(ProcessRecord proc, |
| 10374 | ContentProviderRecord cpr, boolean always) { |
| 10375 | final boolean inLaunching = mLaunchingProviders.contains(cpr); |
| 10376 | |
| 10377 | if (!inLaunching || always) { |
| 10378 | synchronized (cpr) { |
| 10379 | cpr.launchingApp = null; |
| 10380 | cpr.notifyAll(); |
| 10381 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10382 | mProviderMap.removeProviderByClass(cpr.name, UserHandle.getUserId(cpr.uid)); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10383 | String names[] = cpr.info.authority.split(";"); |
| 10384 | for (int j = 0; j < names.length; j++) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10385 | mProviderMap.removeProviderByName(names[j], UserHandle.getUserId(cpr.uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10386 | } |
| 10387 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10388 | |
| 10389 | for (int i=0; i<cpr.connections.size(); i++) { |
| 10390 | ContentProviderConnection conn = cpr.connections.get(i); |
| 10391 | if (conn.waiting) { |
| 10392 | // If this connection is waiting for the provider, then we don't |
| 10393 | // need to mess with its process unless we are always removing |
| 10394 | // or for some reason the provider is not currently launching. |
| 10395 | if (inLaunching && !always) { |
| 10396 | continue; |
| 10397 | } |
| 10398 | } |
| 10399 | ProcessRecord capp = conn.client; |
| 10400 | conn.dead = true; |
| 10401 | if (conn.stableCount > 0) { |
| 10402 | if (!capp.persistent && capp.thread != null |
| 10403 | && capp.pid != 0 |
| 10404 | && capp.pid != MY_PID) { |
| 10405 | Slog.i(TAG, "Kill " + capp.processName |
| 10406 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
| 10407 | + " in dying process " + (proc != null ? proc.processName : "??")); |
| 10408 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid, |
| 10409 | capp.processName, capp.setAdj, "dying provider " |
| 10410 | + cpr.name.toShortString()); |
| 10411 | Process.killProcessQuiet(capp.pid); |
| 10412 | } |
| 10413 | } else if (capp.thread != null && conn.provider.provider != null) { |
| 10414 | try { |
| 10415 | capp.thread.unstableProviderDied(conn.provider.provider.asBinder()); |
| 10416 | } catch (RemoteException e) { |
| 10417 | } |
| 10418 | // In the protocol here, we don't expect the client to correctly |
| 10419 | // clean up this connection, we'll just remove it. |
| 10420 | cpr.connections.remove(i); |
| 10421 | conn.client.conProviders.remove(conn); |
| 10422 | } |
| 10423 | } |
| 10424 | |
| 10425 | if (inLaunching && always) { |
| 10426 | mLaunchingProviders.remove(cpr); |
| 10427 | } |
| 10428 | return inLaunching; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10429 | } |
| 10430 | |
| 10431 | /** |
| 10432 | * Main code for cleaning up a process when it has gone away. This is |
| 10433 | * called both as a result of the process dying, or directly when stopping |
| 10434 | * a process when running in single process mode. |
| 10435 | */ |
| 10436 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10437 | boolean restarting, boolean allowRestart, int index) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10438 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10439 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10440 | } |
| 10441 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10442 | mProcessesToGc.remove(app); |
| 10443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10444 | // Dismiss any open dialogs. |
| 10445 | if (app.crashDialog != null) { |
| 10446 | app.crashDialog.dismiss(); |
| 10447 | app.crashDialog = null; |
| 10448 | } |
| 10449 | if (app.anrDialog != null) { |
| 10450 | app.anrDialog.dismiss(); |
| 10451 | app.anrDialog = null; |
| 10452 | } |
| 10453 | if (app.waitDialog != null) { |
| 10454 | app.waitDialog.dismiss(); |
| 10455 | app.waitDialog = null; |
| 10456 | } |
| 10457 | |
| 10458 | app.crashing = false; |
| 10459 | app.notResponding = false; |
| 10460 | |
| 10461 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 10462 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10463 | app.thread = null; |
| 10464 | app.forcingToForeground = null; |
| 10465 | app.foregroundServices = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10466 | app.foregroundActivities = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 10467 | app.hasShownUi = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10468 | app.hasAboveClient = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10469 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10470 | mServices.killServicesLocked(app, allowRestart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10471 | |
| 10472 | boolean restart = false; |
| 10473 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10474 | // Remove published content providers. |
| 10475 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10476 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10477 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10478 | ContentProviderRecord cpr = it.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10479 | |
| 10480 | final boolean always = app.bad || !allowRestart; |
| 10481 | if (removeDyingProviderLocked(app, cpr, always) || always) { |
| 10482 | // We left the provider in the launching list, need to |
| 10483 | // restart it. |
| 10484 | restart = true; |
| 10485 | } |
| 10486 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10487 | cpr.provider = null; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10488 | cpr.proc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10489 | } |
| 10490 | app.pubProviders.clear(); |
| 10491 | } |
| 10492 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10493 | // Take care of any launching providers waiting for this process. |
| 10494 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 10495 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10496 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10497 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10498 | // Unregister from connected content providers. |
| 10499 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10500 | for (int i=0; i<app.conProviders.size(); i++) { |
| 10501 | ContentProviderConnection conn = app.conProviders.get(i); |
| 10502 | conn.provider.connections.remove(conn); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10503 | } |
| 10504 | app.conProviders.clear(); |
| 10505 | } |
| 10506 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10507 | // At this point there may be remaining entries in mLaunchingProviders |
| 10508 | // where we were the only one waiting, so they are no longer of use. |
| 10509 | // Look for these and clean up if found. |
| 10510 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 10511 | // the actual situation to identify what is actually going on. |
| 10512 | if (false) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10513 | for (int i=0; i<mLaunchingProviders.size(); i++) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10514 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 10515 | mLaunchingProviders.get(i); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10516 | if (cpr.connections.size() <= 0 && !cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10517 | synchronized (cpr) { |
| 10518 | cpr.launchingApp = null; |
| 10519 | cpr.notifyAll(); |
| 10520 | } |
| 10521 | } |
| 10522 | } |
| 10523 | } |
| 10524 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10525 | skipCurrentReceiverLocked(app); |
| 10526 | |
| 10527 | // Unregister any receivers. |
| 10528 | if (app.receivers.size() > 0) { |
| 10529 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 10530 | while (it.hasNext()) { |
| 10531 | removeReceiverLocked(it.next()); |
| 10532 | } |
| 10533 | app.receivers.clear(); |
| 10534 | } |
| 10535 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10536 | // If the app is undergoing backup, tell the backup manager about it |
| 10537 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10538 | if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10539 | try { |
| 10540 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10541 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10542 | bm.agentDisconnected(app.info.packageName); |
| 10543 | } catch (RemoteException e) { |
| 10544 | // can't happen; backup manager is local |
| 10545 | } |
| 10546 | } |
| 10547 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 10548 | for (int i = mPendingProcessChanges.size()-1; i>=0; i--) { |
| 10549 | ProcessChangeItem item = mPendingProcessChanges.get(i); |
| 10550 | if (item.pid == app.pid) { |
| 10551 | mPendingProcessChanges.remove(i); |
| 10552 | mAvailProcessChanges.add(item); |
| 10553 | } |
| 10554 | } |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 10555 | mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10556 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10557 | // If the caller is restarting this app, then leave it in its |
| 10558 | // current lists and let the caller take care of it. |
| 10559 | if (restarting) { |
| 10560 | return; |
| 10561 | } |
| 10562 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10563 | if (!app.persistent || app.isolated) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10564 | if (DEBUG_PROCESSES) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10565 | "Removing non-persistent process during cleanup: " + app); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10566 | mProcessNames.remove(app.processName, app.uid); |
| 10567 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10568 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10569 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 10570 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10571 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10572 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10573 | } else if (!app.removed) { |
| 10574 | // This app is persistent, so we need to keep its record around. |
| 10575 | // If it is not already on the pending app list, add it there |
| 10576 | // and start a new process for it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10577 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 10578 | mPersistentStartingProcesses.add(app); |
| 10579 | restart = true; |
| 10580 | } |
| 10581 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10582 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 10583 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10584 | mProcessesOnHold.remove(app); |
| 10585 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 10586 | if (app == mHomeProcess) { |
| 10587 | mHomeProcess = null; |
| 10588 | } |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10589 | if (app == mPreviousProcess) { |
| 10590 | mPreviousProcess = null; |
| 10591 | } |
| 10592 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10593 | if (restart && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10594 | // We have components that still need to be running in the |
| 10595 | // process, so re-launch it. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10596 | mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10597 | startProcessLocked(app, "restart", app.processName); |
| 10598 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 10599 | // Goodbye! |
| 10600 | synchronized (mPidsSelfLocked) { |
| 10601 | mPidsSelfLocked.remove(app.pid); |
| 10602 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 10603 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10604 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10605 | } |
| 10606 | } |
| 10607 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10608 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 10609 | // Look through the content providers we are waiting to have launched, |
| 10610 | // and if any run in this process then either schedule a restart of |
| 10611 | // the process or kill the client waiting for it if this process has |
| 10612 | // gone bad. |
| 10613 | int NL = mLaunchingProviders.size(); |
| 10614 | boolean restart = false; |
| 10615 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10616 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10617 | if (cpr.launchingApp == app) { |
| 10618 | if (!alwaysBad && !app.bad) { |
| 10619 | restart = true; |
| 10620 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10621 | removeDyingProviderLocked(app, cpr, true); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10622 | NL = mLaunchingProviders.size(); |
| 10623 | } |
| 10624 | } |
| 10625 | } |
| 10626 | return restart; |
| 10627 | } |
| 10628 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10629 | // ========================================================= |
| 10630 | // SERVICES |
| 10631 | // ========================================================= |
| 10632 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10633 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 10634 | int flags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10635 | enforceNotIsolatedCaller("getServices"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10636 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10637 | return mServices.getRunningServiceInfoLocked(maxNum, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10638 | } |
| 10639 | } |
| 10640 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10641 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10642 | enforceNotIsolatedCaller("getRunningServiceControlPanel"); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10643 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10644 | return mServices.getRunningServiceControlPanelLocked(name); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10645 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10646 | } |
| 10647 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10648 | public ComponentName startService(IApplicationThread caller, Intent service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10649 | String resolvedType, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10650 | enforceNotIsolatedCaller("startService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10651 | // Refuse possible leaked file descriptors |
| 10652 | if (service != null && service.hasFileDescriptors() == true) { |
| 10653 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10654 | } |
| 10655 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10656 | if (DEBUG_SERVICE) |
| 10657 | Slog.v(TAG, "startService: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10658 | synchronized(this) { |
| 10659 | final int callingPid = Binder.getCallingPid(); |
| 10660 | final int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10661 | checkValidCaller(callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10662 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10663 | ComponentName res = mServices.startServiceLocked(caller, service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10664 | resolvedType, callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10665 | Binder.restoreCallingIdentity(origId); |
| 10666 | return res; |
| 10667 | } |
| 10668 | } |
| 10669 | |
| 10670 | ComponentName startServiceInPackage(int uid, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10671 | Intent service, String resolvedType, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10672 | synchronized(this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10673 | if (DEBUG_SERVICE) |
| 10674 | Slog.v(TAG, "startServiceInPackage: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10675 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10676 | ComponentName res = mServices.startServiceLocked(null, service, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10677 | resolvedType, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10678 | Binder.restoreCallingIdentity(origId); |
| 10679 | return res; |
| 10680 | } |
| 10681 | } |
| 10682 | |
| 10683 | public int stopService(IApplicationThread caller, Intent service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10684 | String resolvedType, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10685 | enforceNotIsolatedCaller("stopService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10686 | // Refuse possible leaked file descriptors |
| 10687 | if (service != null && service.hasFileDescriptors() == true) { |
| 10688 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10689 | } |
| 10690 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10691 | checkValidCaller(Binder.getCallingUid(), userId); |
| 10692 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10693 | synchronized(this) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10694 | return mServices.stopServiceLocked(caller, service, resolvedType, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10695 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10696 | } |
| 10697 | |
| 10698 | public IBinder peekService(Intent service, String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10699 | enforceNotIsolatedCaller("peekService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10700 | // Refuse possible leaked file descriptors |
| 10701 | if (service != null && service.hasFileDescriptors() == true) { |
| 10702 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10703 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10704 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10705 | return mServices.peekServiceLocked(service, resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10706 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10707 | } |
| 10708 | |
| 10709 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 10710 | int startId) { |
| 10711 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10712 | return mServices.stopServiceTokenLocked(className, token, startId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10713 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10714 | } |
| 10715 | |
| 10716 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10717 | int id, Notification notification, boolean removeNotification) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10718 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10719 | mServices.setServiceForegroundLocked(className, token, id, notification, |
| 10720 | removeNotification); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10721 | } |
| 10722 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10723 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10724 | public int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll, |
| 10725 | boolean requireFull, String name, String callerPackage) { |
| 10726 | synchronized(this) { |
| 10727 | return handleIncomingUserLocked(callingPid, callingUid, userId, allowAll, |
| 10728 | requireFull, name, callerPackage); |
| 10729 | } |
| 10730 | } |
| 10731 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 10732 | int handleIncomingUserLocked(int callingPid, int callingUid, int userId, boolean allowAll, |
| 10733 | boolean requireFull, String name, String callerPackage) { |
| 10734 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 10735 | if (callingUserId != userId) { |
| 10736 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
| 10737 | if ((requireFull || checkComponentPermission( |
| 10738 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 10739 | callingPid, callingUid, -1, true) != PackageManager.PERMISSION_GRANTED) |
| 10740 | && checkComponentPermission( |
| 10741 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 10742 | callingPid, callingUid, -1, true) |
| 10743 | != PackageManager.PERMISSION_GRANTED) { |
| 10744 | if (userId == UserHandle.USER_CURRENT_OR_SELF) { |
| 10745 | // In this case, they would like to just execute as their |
| 10746 | // owner user instead of failing. |
| 10747 | userId = callingUserId; |
| 10748 | } else { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10749 | StringBuilder builder = new StringBuilder(128); |
| 10750 | builder.append("Permission Denial: "); |
| 10751 | builder.append(name); |
| 10752 | if (callerPackage != null) { |
| 10753 | builder.append(" from "); |
| 10754 | builder.append(callerPackage); |
| 10755 | } |
| 10756 | builder.append(" asks to run as user "); |
| 10757 | builder.append(userId); |
| 10758 | builder.append(" but is calling from user "); |
| 10759 | builder.append(UserHandle.getUserId(callingUid)); |
| 10760 | builder.append("; this requires "); |
| 10761 | builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
| 10762 | if (!requireFull) { |
| 10763 | builder.append("or"); |
| 10764 | builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS); |
| 10765 | } |
| 10766 | String msg = builder.toString(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 10767 | Slog.w(TAG, msg); |
| 10768 | throw new SecurityException(msg); |
| 10769 | } |
| 10770 | } |
| 10771 | } |
| 10772 | if (userId == UserHandle.USER_CURRENT |
| 10773 | || userId == UserHandle.USER_CURRENT_OR_SELF) { |
| 10774 | userId = mCurrentUserId; |
| 10775 | } |
| 10776 | if (!allowAll && userId < 0) { |
| 10777 | throw new IllegalArgumentException( |
| 10778 | "Call does not support special user #" + userId); |
| 10779 | } |
| 10780 | } |
| 10781 | return userId; |
| 10782 | } |
| 10783 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 10784 | boolean isSingleton(String componentProcessName, ApplicationInfo aInfo, |
| 10785 | String className, int flags) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10786 | boolean result = false; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10787 | if (UserHandle.getAppId(aInfo.uid) >= Process.FIRST_APPLICATION_UID) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 10788 | if ((flags&ServiceInfo.FLAG_SINGLE_USER) != 0) { |
| 10789 | if (ActivityManager.checkUidPermission( |
| 10790 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 10791 | aInfo.uid) != PackageManager.PERMISSION_GRANTED) { |
| 10792 | ComponentName comp = new ComponentName(aInfo.packageName, className); |
| 10793 | String msg = "Permission Denial: Component " + comp.flattenToShortString() |
| 10794 | + " requests FLAG_SINGLE_USER, but app does not hold " |
| 10795 | + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 10796 | Slog.w(TAG, msg); |
| 10797 | throw new SecurityException(msg); |
| 10798 | } |
| 10799 | result = true; |
| 10800 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10801 | } else if (componentProcessName == aInfo.packageName) { |
| 10802 | result = (aInfo.flags & ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 10803 | } else if ("system".equals(componentProcessName)) { |
| 10804 | result = true; |
| 10805 | } |
| 10806 | if (DEBUG_MU) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 10807 | Slog.v(TAG, "isSingleton(" + componentProcessName + ", " + aInfo |
| 10808 | + ", " + className + ", 0x" + Integer.toHexString(flags) + ") = " + result); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10809 | } |
| 10810 | return result; |
| 10811 | } |
| 10812 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10813 | public int bindService(IApplicationThread caller, IBinder token, |
| 10814 | Intent service, String resolvedType, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 10815 | IServiceConnection connection, int flags, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10816 | enforceNotIsolatedCaller("bindService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10817 | // Refuse possible leaked file descriptors |
| 10818 | if (service != null && service.hasFileDescriptors() == true) { |
| 10819 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10820 | } |
| 10821 | |
| 10822 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10823 | return mServices.bindServiceLocked(caller, token, service, resolvedType, |
| 10824 | connection, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10825 | } |
| 10826 | } |
| 10827 | |
| 10828 | public boolean unbindService(IServiceConnection connection) { |
| 10829 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10830 | return mServices.unbindServiceLocked(connection); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10831 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10832 | } |
| 10833 | |
| 10834 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 10835 | // Refuse possible leaked file descriptors |
| 10836 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 10837 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10838 | } |
| 10839 | |
| 10840 | synchronized(this) { |
| 10841 | if (!(token instanceof ServiceRecord)) { |
| 10842 | throw new IllegalArgumentException("Invalid service token"); |
| 10843 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10844 | mServices.publishServiceLocked((ServiceRecord)token, intent, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10845 | } |
| 10846 | } |
| 10847 | |
| 10848 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 10849 | // Refuse possible leaked file descriptors |
| 10850 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 10851 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10852 | } |
| 10853 | |
| 10854 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10855 | mServices.unbindFinishedLocked((ServiceRecord)token, intent, doRebind); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10856 | } |
| 10857 | } |
| 10858 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 10859 | 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] | 10860 | synchronized(this) { |
| 10861 | if (!(token instanceof ServiceRecord)) { |
| 10862 | throw new IllegalArgumentException("Invalid service token"); |
| 10863 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10864 | mServices.serviceDoneExecutingLocked((ServiceRecord)token, type, startId, res); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 10865 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10866 | } |
| 10867 | |
| 10868 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10869 | // BACKUP AND RESTORE |
| 10870 | // ========================================================= |
| 10871 | |
| 10872 | // Cause the target app to be launched if necessary and its backup agent |
| 10873 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 10874 | // activity manager to announce its creation. |
| 10875 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10876 | if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10877 | enforceCallingPermission("android.permission.BACKUP", "startBackupAgent"); |
| 10878 | |
| 10879 | synchronized(this) { |
| 10880 | // !!! TODO: currently no check here that we're already bound |
| 10881 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 10882 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 10883 | synchronized (stats) { |
| 10884 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 10885 | } |
| 10886 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10887 | // Backup agent is now in use, its package can't be stopped. |
| 10888 | try { |
| 10889 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10890 | app.packageName, false, UserHandle.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10891 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 10892 | } catch (IllegalArgumentException e) { |
| 10893 | Slog.w(TAG, "Failed trying to unstop package " |
| 10894 | + app.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 10895 | } |
| 10896 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10897 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 10898 | ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) |
| 10899 | ? new ComponentName(app.packageName, app.backupAgentName) |
| 10900 | : new ComponentName("android", "FullBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10901 | // startProcessLocked() returns existing proc's record if it's already running |
| 10902 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10903 | false, 0, "backup", hostingName, false, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10904 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10905 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10906 | return false; |
| 10907 | } |
| 10908 | |
| 10909 | r.app = proc; |
| 10910 | mBackupTarget = r; |
| 10911 | mBackupAppName = app.packageName; |
| 10912 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 10913 | // Try not to kill the process during backup |
| 10914 | updateOomAdjLocked(proc); |
| 10915 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10916 | // If the process is already attached, schedule the creation of the backup agent now. |
| 10917 | // If it is not yet live, this will be done when it attaches to the framework. |
| 10918 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10919 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10920 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 10921 | proc.thread.scheduleCreateBackupAgent(app, |
| 10922 | compatibilityInfoForPackageLocked(app), backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10923 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 10924 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10925 | } |
| 10926 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10927 | 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] | 10928 | } |
| 10929 | // Invariants: at this point, the target app process exists and the application |
| 10930 | // is either already running or in the process of coming up. mBackupTarget and |
| 10931 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 10932 | // know that it's scheduled for a backup-agent operation. |
| 10933 | } |
| 10934 | |
| 10935 | return true; |
| 10936 | } |
| 10937 | |
| 10938 | // A backup agent has just come up |
| 10939 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10940 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10941 | + " = " + agent); |
| 10942 | |
| 10943 | synchronized(this) { |
| 10944 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10945 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10946 | return; |
| 10947 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 10948 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10949 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 10950 | long oldIdent = Binder.clearCallingIdentity(); |
| 10951 | try { |
| 10952 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10953 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10954 | bm.agentConnected(agentPackageName, agent); |
| 10955 | } catch (RemoteException e) { |
| 10956 | // can't happen; the backup manager service is local |
| 10957 | } catch (Exception e) { |
| 10958 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 10959 | e.printStackTrace(); |
| 10960 | } finally { |
| 10961 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10962 | } |
| 10963 | } |
| 10964 | |
| 10965 | // done with this agent |
| 10966 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10967 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10968 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10969 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10970 | return; |
| 10971 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10972 | |
| 10973 | synchronized(this) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10974 | if (mBackupAppName == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10975 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 10976 | return; |
| 10977 | } |
| 10978 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10979 | if (!mBackupAppName.equals(appInfo.packageName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10980 | 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] | 10981 | return; |
| 10982 | } |
| 10983 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 10984 | ProcessRecord proc = mBackupTarget.app; |
| 10985 | mBackupTarget = null; |
| 10986 | mBackupAppName = null; |
| 10987 | |
| 10988 | // Not backing this app up any more; reset its OOM adjustment |
| 10989 | updateOomAdjLocked(proc); |
| 10990 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 10991 | // If the app crashed during backup, 'thread' will be null here |
| 10992 | if (proc.thread != null) { |
| 10993 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 10994 | proc.thread.scheduleDestroyBackupAgent(appInfo, |
| 10995 | compatibilityInfoForPackageLocked(appInfo)); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 10996 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10997 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 10998 | e.printStackTrace(); |
| 10999 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11000 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11001 | } |
| 11002 | } |
| 11003 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11004 | // BROADCASTS |
| 11005 | // ========================================================= |
| 11006 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 11007 | private final List getStickiesLocked(String action, IntentFilter filter, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11008 | List cur, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11009 | final ContentResolver resolver = mContext.getContentResolver(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11010 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11011 | if (stickies == null) { |
| 11012 | return cur; |
| 11013 | } |
| 11014 | final ArrayList<Intent> list = stickies.get(action); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11015 | if (list == null) { |
| 11016 | return cur; |
| 11017 | } |
| 11018 | int N = list.size(); |
| 11019 | for (int i=0; i<N; i++) { |
| 11020 | Intent intent = list.get(i); |
| 11021 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 11022 | if (cur == null) { |
| 11023 | cur = new ArrayList<Intent>(); |
| 11024 | } |
| 11025 | cur.add(intent); |
| 11026 | } |
| 11027 | } |
| 11028 | return cur; |
| 11029 | } |
| 11030 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11031 | boolean isPendingBroadcastProcessLocked(int pid) { |
| 11032 | return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid) |
| 11033 | || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid); |
| 11034 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11035 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11036 | void skipPendingBroadcastLocked(int pid) { |
| 11037 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
| 11038 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 11039 | queue.skipPendingBroadcastLocked(pid); |
| 11040 | } |
| 11041 | } |
| 11042 | |
| 11043 | // The app just attached; send any pending broadcasts that it should receive |
| 11044 | boolean sendPendingBroadcastsLocked(ProcessRecord app) { |
| 11045 | boolean didSomething = false; |
| 11046 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 11047 | didSomething |= queue.sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11048 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11049 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11050 | } |
| 11051 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11052 | public Intent registerReceiver(IApplicationThread caller, String callerPackage, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11053 | IIntentReceiver receiver, IntentFilter filter, String permission, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11054 | enforceNotIsolatedCaller("registerReceiver"); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11055 | int callingUid; |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11056 | int callingPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11057 | synchronized(this) { |
| 11058 | ProcessRecord callerApp = null; |
| 11059 | if (caller != null) { |
| 11060 | callerApp = getRecordForAppLocked(caller); |
| 11061 | if (callerApp == null) { |
| 11062 | throw new SecurityException( |
| 11063 | "Unable to find app for caller " + caller |
| 11064 | + " (pid=" + Binder.getCallingPid() |
| 11065 | + ") when registering receiver " + receiver); |
| 11066 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11067 | if (callerApp.info.uid != Process.SYSTEM_UID && |
| 11068 | !callerApp.pkgList.contains(callerPackage)) { |
| 11069 | throw new SecurityException("Given caller package " + callerPackage |
| 11070 | + " is not running in process " + callerApp); |
| 11071 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11072 | callingUid = callerApp.info.uid; |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11073 | callingPid = callerApp.pid; |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11074 | } else { |
| 11075 | callerPackage = null; |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11076 | callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11077 | callingPid = Binder.getCallingPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11078 | } |
| 11079 | |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11080 | userId = this.handleIncomingUserLocked(callingPid, callingUid, userId, |
| 11081 | true, true, "registerReceiver", callerPackage); |
| 11082 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11083 | List allSticky = null; |
| 11084 | |
| 11085 | // Look for any matching sticky broadcasts... |
| 11086 | Iterator actions = filter.actionsIterator(); |
| 11087 | if (actions != null) { |
| 11088 | while (actions.hasNext()) { |
| 11089 | String action = (String)actions.next(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11090 | allSticky = getStickiesLocked(action, filter, allSticky, |
| 11091 | UserHandle.USER_ALL); |
| 11092 | allSticky = getStickiesLocked(action, filter, allSticky, |
| 11093 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11094 | } |
| 11095 | } else { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11096 | allSticky = getStickiesLocked(null, filter, allSticky, |
| 11097 | UserHandle.USER_ALL); |
| 11098 | allSticky = getStickiesLocked(null, filter, allSticky, |
| 11099 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11100 | } |
| 11101 | |
| 11102 | // The first sticky in the list is returned directly back to |
| 11103 | // the client. |
| 11104 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 11105 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11106 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11107 | + ": " + sticky); |
| 11108 | |
| 11109 | if (receiver == null) { |
| 11110 | return sticky; |
| 11111 | } |
| 11112 | |
| 11113 | ReceiverList rl |
| 11114 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 11115 | if (rl == null) { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11116 | rl = new ReceiverList(this, callerApp, callingPid, callingUid, |
| 11117 | userId, receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11118 | if (rl.app != null) { |
| 11119 | rl.app.receivers.add(rl); |
| 11120 | } else { |
| 11121 | try { |
| 11122 | receiver.asBinder().linkToDeath(rl, 0); |
| 11123 | } catch (RemoteException e) { |
| 11124 | return sticky; |
| 11125 | } |
| 11126 | rl.linkedToDeath = true; |
| 11127 | } |
| 11128 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11129 | } else if (rl.uid != callingUid) { |
| 11130 | throw new IllegalArgumentException( |
| 11131 | "Receiver requested to register for uid " + callingUid |
| 11132 | + " was previously registered for uid " + rl.uid); |
| 11133 | } else if (rl.pid != callingPid) { |
| 11134 | throw new IllegalArgumentException( |
| 11135 | "Receiver requested to register for pid " + callingPid |
| 11136 | + " was previously registered for pid " + rl.pid); |
| 11137 | } else if (rl.userId != userId) { |
| 11138 | throw new IllegalArgumentException( |
| 11139 | "Receiver requested to register for user " + userId |
| 11140 | + " was previously registered for user " + rl.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11141 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11142 | BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11143 | permission, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11144 | rl.add(bf); |
| 11145 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11146 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11147 | } |
| 11148 | mReceiverResolver.addFilter(bf); |
| 11149 | |
| 11150 | // Enqueue broadcasts for all existing stickies that match |
| 11151 | // this filter. |
| 11152 | if (allSticky != null) { |
| 11153 | ArrayList receivers = new ArrayList(); |
| 11154 | receivers.add(bf); |
| 11155 | |
| 11156 | int N = allSticky.size(); |
| 11157 | for (int i=0; i<N; i++) { |
| 11158 | Intent intent = (Intent)allSticky.get(i); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11159 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11160 | BroadcastRecord r = new BroadcastRecord(queue, intent, null, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11161 | null, -1, -1, null, receivers, null, 0, null, null, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11162 | false, true, true, -1); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11163 | queue.enqueueParallelBroadcastLocked(r); |
| 11164 | queue.scheduleBroadcastsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11165 | } |
| 11166 | } |
| 11167 | |
| 11168 | return sticky; |
| 11169 | } |
| 11170 | } |
| 11171 | |
| 11172 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11173 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11174 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11175 | final long origId = Binder.clearCallingIdentity(); |
| 11176 | try { |
| 11177 | boolean doTrim = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11178 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11179 | synchronized(this) { |
| 11180 | ReceiverList rl |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11181 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11182 | if (rl != null) { |
| 11183 | if (rl.curBroadcast != null) { |
| 11184 | BroadcastRecord r = rl.curBroadcast; |
| 11185 | final boolean doNext = finishReceiverLocked( |
| 11186 | receiver.asBinder(), r.resultCode, r.resultData, |
| 11187 | r.resultExtras, r.resultAbort, true); |
| 11188 | if (doNext) { |
| 11189 | doTrim = true; |
| 11190 | r.queue.processNextBroadcast(false); |
| 11191 | } |
| 11192 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11193 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11194 | if (rl.app != null) { |
| 11195 | rl.app.receivers.remove(rl); |
| 11196 | } |
| 11197 | removeReceiverLocked(rl); |
| 11198 | if (rl.linkedToDeath) { |
| 11199 | rl.linkedToDeath = false; |
| 11200 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 11201 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11202 | } |
| 11203 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11204 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11205 | // If we actually concluded any broadcasts, we might now be able |
| 11206 | // to trim the recipients' apps from our working set |
| 11207 | if (doTrim) { |
| 11208 | trimApplications(); |
| 11209 | return; |
| 11210 | } |
| 11211 | |
| 11212 | } finally { |
| 11213 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11214 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11215 | } |
| 11216 | |
| 11217 | void removeReceiverLocked(ReceiverList rl) { |
| 11218 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 11219 | int N = rl.size(); |
| 11220 | for (int i=0; i<N; i++) { |
| 11221 | mReceiverResolver.removeFilter(rl.get(i)); |
| 11222 | } |
| 11223 | } |
| 11224 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11225 | private final void sendPackageBroadcastLocked(int cmd, String[] packages, int userId) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11226 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 11227 | ProcessRecord r = mLruProcesses.get(i); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11228 | if (r.thread != null && (userId == UserHandle.USER_ALL || r.userId == userId)) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11229 | try { |
| 11230 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 11231 | } catch (RemoteException ex) { |
| 11232 | } |
| 11233 | } |
| 11234 | } |
| 11235 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11236 | |
| 11237 | private List<ResolveInfo> collectReceiverComponents(Intent intent, String resolvedType, |
| 11238 | int[] users) { |
| 11239 | List<ResolveInfo> receivers = null; |
| 11240 | try { |
| 11241 | HashSet<ComponentName> singleUserReceivers = null; |
| 11242 | boolean scannedFirstReceivers = false; |
| 11243 | for (int user : users) { |
| 11244 | List<ResolveInfo> newReceivers = AppGlobals.getPackageManager() |
| 11245 | .queryIntentReceivers(intent, resolvedType, STOCK_PM_FLAGS, user); |
| 11246 | if (newReceivers != null && newReceivers.size() == 0) { |
| 11247 | newReceivers = null; |
| 11248 | } |
| 11249 | if (receivers == null) { |
| 11250 | receivers = newReceivers; |
| 11251 | } else if (newReceivers != null) { |
| 11252 | // We need to concatenate the additional receivers |
| 11253 | // found with what we have do far. This would be easy, |
| 11254 | // but we also need to de-dup any receivers that are |
| 11255 | // singleUser. |
| 11256 | if (!scannedFirstReceivers) { |
| 11257 | // Collect any single user receivers we had already retrieved. |
| 11258 | scannedFirstReceivers = true; |
| 11259 | for (int i=0; i<receivers.size(); i++) { |
| 11260 | ResolveInfo ri = receivers.get(i); |
| 11261 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 11262 | ComponentName cn = new ComponentName( |
| 11263 | ri.activityInfo.packageName, ri.activityInfo.name); |
| 11264 | if (singleUserReceivers == null) { |
| 11265 | singleUserReceivers = new HashSet<ComponentName>(); |
| 11266 | } |
| 11267 | singleUserReceivers.add(cn); |
| 11268 | } |
| 11269 | } |
| 11270 | } |
| 11271 | // Add the new results to the existing results, tracking |
| 11272 | // and de-dupping single user receivers. |
| 11273 | for (int i=0; i<newReceivers.size(); i++) { |
| 11274 | ResolveInfo ri = receivers.get(i); |
| 11275 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 11276 | ComponentName cn = new ComponentName( |
| 11277 | ri.activityInfo.packageName, ri.activityInfo.name); |
| 11278 | if (singleUserReceivers == null) { |
| 11279 | singleUserReceivers = new HashSet<ComponentName>(); |
| 11280 | } |
| 11281 | if (!singleUserReceivers.contains(cn)) { |
| 11282 | singleUserReceivers.add(cn); |
| 11283 | receivers.add(ri); |
| 11284 | } |
| 11285 | } else { |
| 11286 | receivers.add(ri); |
| 11287 | } |
| 11288 | } |
| 11289 | } |
| 11290 | } |
| 11291 | } catch (RemoteException ex) { |
| 11292 | // pm is in same process, this will never happen. |
| 11293 | } |
| 11294 | return receivers; |
| 11295 | } |
| 11296 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11297 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 11298 | String callerPackage, Intent intent, String resolvedType, |
| 11299 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 11300 | Bundle map, String requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11301 | boolean ordered, boolean sticky, int callingPid, int callingUid, |
| 11302 | int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11303 | intent = new Intent(intent); |
| 11304 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11305 | // By default broadcasts do not go to stopped apps. |
| 11306 | intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES); |
| 11307 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11308 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11309 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 11310 | + " ordered=" + ordered + " userid=" + userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11311 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11312 | 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] | 11313 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11314 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11315 | userId = handleIncomingUserLocked(callingPid, callingUid, userId, |
| 11316 | true, false, "broadcast", callerPackage); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11317 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 11318 | // Make sure that the user who is receiving this broadcast is started |
| 11319 | // If not, we will just skip it. |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11320 | if (userId != UserHandle.USER_ALL && mStartedUsers.get(userId) == null) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 11321 | Slog.w(TAG, "Skipping broadcast of " + intent |
| 11322 | + ": user " + userId + " is stopped"); |
| 11323 | return ActivityManager.BROADCAST_SUCCESS; |
| 11324 | } |
| 11325 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11326 | /* |
| 11327 | * Prevent non-system code (defined here to be non-persistent |
| 11328 | * processes) from sending protected broadcasts. |
| 11329 | */ |
| 11330 | if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID |
| 11331 | || callingUid == Process.SHELL_UID || callingUid == Process.BLUETOOTH_UID || |
| 11332 | callingUid == 0) { |
| 11333 | // Always okay. |
| 11334 | } else if (callerApp == null || !callerApp.persistent) { |
| 11335 | try { |
| 11336 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
| 11337 | intent.getAction())) { |
| 11338 | String msg = "Permission Denial: not allowed to send broadcast " |
| 11339 | + intent.getAction() + " from pid=" |
| 11340 | + callingPid + ", uid=" + callingUid; |
| 11341 | Slog.w(TAG, msg); |
| 11342 | throw new SecurityException(msg); |
| 11343 | } |
| 11344 | } catch (RemoteException e) { |
| 11345 | Slog.w(TAG, "Remote exception", e); |
| 11346 | return ActivityManager.BROADCAST_SUCCESS; |
| 11347 | } |
| 11348 | } |
| 11349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11350 | // Handle special intents: if this broadcast is from the package |
| 11351 | // manager about a package being removed, we need to remove all of |
| 11352 | // its activities from the history stack. |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11353 | final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11354 | intent.getAction()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11355 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 11356 | || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11357 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11358 | || uidRemoved) { |
| 11359 | if (checkComponentPermission( |
| 11360 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11361 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11362 | == PackageManager.PERMISSION_GRANTED) { |
| 11363 | if (uidRemoved) { |
| 11364 | final Bundle intentExtras = intent.getExtras(); |
| 11365 | final int uid = intentExtras != null |
| 11366 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 11367 | if (uid >= 0) { |
| 11368 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 11369 | synchronized (bs) { |
| 11370 | bs.removeUidStatsLocked(uid); |
| 11371 | } |
| 11372 | } |
| 11373 | } else { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11374 | // If resources are unavailable just force stop all |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11375 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11376 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11377 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 11378 | if (list != null && (list.length > 0)) { |
| 11379 | for (String pkg : list) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11380 | forceStopPackageLocked(pkg, -1, false, true, true, false, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11381 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11382 | sendPackageBroadcastLocked( |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11383 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11384 | } |
| 11385 | } else { |
| 11386 | Uri data = intent.getData(); |
| 11387 | String ssp; |
| 11388 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11389 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 11390 | forceStopPackageLocked(ssp, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11391 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true, |
| 11392 | false, userId); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 11393 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11394 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11395 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11396 | new String[] {ssp}, userId); |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11397 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11398 | } |
| 11399 | } |
| 11400 | } |
| 11401 | } else { |
| 11402 | String msg = "Permission Denial: " + intent.getAction() |
| 11403 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 11404 | + ", uid=" + callingUid + ")" |
| 11405 | + " requires " |
| 11406 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11407 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11408 | throw new SecurityException(msg); |
| 11409 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11410 | |
| 11411 | // Special case for adding a package: by default turn on compatibility |
| 11412 | // mode. |
| 11413 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 11414 | Uri data = intent.getData(); |
| 11415 | String ssp; |
| 11416 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11417 | mCompatModePackages.handlePackageAddedLocked(ssp, |
| 11418 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11419 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11420 | } |
| 11421 | |
| 11422 | /* |
| 11423 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 11424 | * of all currently running processes. This message will get queued up before the broadcast |
| 11425 | * happens. |
| 11426 | */ |
| 11427 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 11428 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 11429 | } |
| 11430 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 11431 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 11432 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 11433 | } |
| 11434 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 11435 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 11436 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 11437 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 11438 | } |
| 11439 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11440 | // Add to the sticky list if requested. |
| 11441 | if (sticky) { |
| 11442 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 11443 | callingPid, callingUid) |
| 11444 | != PackageManager.PERMISSION_GRANTED) { |
| 11445 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 11446 | + callingPid + ", uid=" + callingUid |
| 11447 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11448 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11449 | throw new SecurityException(msg); |
| 11450 | } |
| 11451 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11452 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11453 | + " and enforce permission " + requiredPermission); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 11454 | return ActivityManager.BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11455 | } |
| 11456 | if (intent.getComponent() != null) { |
| 11457 | throw new SecurityException( |
| 11458 | "Sticky broadcasts can't target a specific component"); |
| 11459 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11460 | // We use userId directly here, since the "all" target is maintained |
| 11461 | // as a separate set of sticky broadcasts. |
| 11462 | if (userId != UserHandle.USER_ALL) { |
| 11463 | // But first, if this is not a broadcast to all users, then |
| 11464 | // make sure it doesn't conflict with an existing broadcast to |
| 11465 | // all users. |
| 11466 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get( |
| 11467 | UserHandle.USER_ALL); |
| 11468 | if (stickies != null) { |
| 11469 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
| 11470 | if (list != null) { |
| 11471 | int N = list.size(); |
| 11472 | int i; |
| 11473 | for (i=0; i<N; i++) { |
| 11474 | if (intent.filterEquals(list.get(i))) { |
| 11475 | throw new IllegalArgumentException( |
| 11476 | "Sticky broadcast " + intent + " for user " |
| 11477 | + userId + " conflicts with existing global broadcast"); |
| 11478 | } |
| 11479 | } |
| 11480 | } |
| 11481 | } |
| 11482 | } |
| 11483 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11484 | if (stickies == null) { |
| 11485 | stickies = new HashMap<String, ArrayList<Intent>>(); |
| 11486 | mStickyBroadcasts.put(userId, stickies); |
| 11487 | } |
| 11488 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11489 | if (list == null) { |
| 11490 | list = new ArrayList<Intent>(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11491 | stickies.put(intent.getAction(), list); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11492 | } |
| 11493 | int N = list.size(); |
| 11494 | int i; |
| 11495 | for (i=0; i<N; i++) { |
| 11496 | if (intent.filterEquals(list.get(i))) { |
| 11497 | // This sticky already exists, replace it. |
| 11498 | list.set(i, new Intent(intent)); |
| 11499 | break; |
| 11500 | } |
| 11501 | } |
| 11502 | if (i >= N) { |
| 11503 | list.add(new Intent(intent)); |
| 11504 | } |
| 11505 | } |
| 11506 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11507 | int[] users; |
| 11508 | if (userId == UserHandle.USER_ALL) { |
| 11509 | // Caller wants broadcast to go to all started users. |
| 11510 | users = new int[mStartedUsers.size()]; |
| 11511 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 11512 | users[i] = mStartedUsers.keyAt(i); |
| 11513 | } |
| 11514 | } else { |
| 11515 | // Caller wants broadcast to go to one specific user. |
| 11516 | users = new int[] {userId}; |
| 11517 | } |
| 11518 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11519 | // Figure out who all will receive this broadcast. |
| 11520 | List receivers = null; |
| 11521 | List<BroadcastFilter> registeredReceivers = null; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11522 | // Need to resolve the intent to interested receivers... |
| 11523 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 11524 | == 0) { |
| 11525 | receivers = collectReceiverComponents(intent, resolvedType, users); |
| 11526 | } |
| 11527 | if (intent.getComponent() == null) { |
| 11528 | registeredReceivers = mReceiverResolver.queryIntent(intent, |
| 11529 | resolvedType, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11530 | } |
| 11531 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11532 | final boolean replacePending = |
| 11533 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 11534 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11535 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11536 | + " replacePending=" + replacePending); |
| 11537 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11538 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 11539 | if (!ordered && NR > 0) { |
| 11540 | // If we are not serializing this broadcast, then send the |
| 11541 | // registered receivers separately so they don't wait for the |
| 11542 | // components to be launched. |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11543 | final BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11544 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11545 | callerPackage, callingPid, callingUid, requiredPermission, |
| 11546 | registeredReceivers, resultTo, resultCode, resultData, map, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11547 | ordered, sticky, false, userId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11548 | if (DEBUG_BROADCAST) Slog.v( |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11549 | TAG, "Enqueueing parallel broadcast " + r); |
| 11550 | final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11551 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11552 | queue.enqueueParallelBroadcastLocked(r); |
| 11553 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11554 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11555 | registeredReceivers = null; |
| 11556 | NR = 0; |
| 11557 | } |
| 11558 | |
| 11559 | // Merge into one list. |
| 11560 | int ir = 0; |
| 11561 | if (receivers != null) { |
| 11562 | // A special case for PACKAGE_ADDED: do not allow the package |
| 11563 | // being added to see this broadcast. This prevents them from |
| 11564 | // using this as a back door to get run as soon as they are |
| 11565 | // installed. Maybe in the future we want to have a special install |
| 11566 | // broadcast or such for apps, but we'd like to deliberately make |
| 11567 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11568 | String skipPackages[] = null; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11569 | if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 11570 | || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 11571 | || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11572 | Uri data = intent.getData(); |
| 11573 | if (data != null) { |
| 11574 | String pkgName = data.getSchemeSpecificPart(); |
| 11575 | if (pkgName != null) { |
| 11576 | skipPackages = new String[] { pkgName }; |
| 11577 | } |
| 11578 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11579 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11580 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 11581 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11582 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 11583 | for (String skipPackage : skipPackages) { |
| 11584 | if (skipPackage != null) { |
| 11585 | int NT = receivers.size(); |
| 11586 | for (int it=0; it<NT; it++) { |
| 11587 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 11588 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 11589 | receivers.remove(it); |
| 11590 | it--; |
| 11591 | NT--; |
| 11592 | } |
| 11593 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11594 | } |
| 11595 | } |
| 11596 | } |
| 11597 | |
| 11598 | int NT = receivers != null ? receivers.size() : 0; |
| 11599 | int it = 0; |
| 11600 | ResolveInfo curt = null; |
| 11601 | BroadcastFilter curr = null; |
| 11602 | while (it < NT && ir < NR) { |
| 11603 | if (curt == null) { |
| 11604 | curt = (ResolveInfo)receivers.get(it); |
| 11605 | } |
| 11606 | if (curr == null) { |
| 11607 | curr = registeredReceivers.get(ir); |
| 11608 | } |
| 11609 | if (curr.getPriority() >= curt.priority) { |
| 11610 | // Insert this broadcast record into the final list. |
| 11611 | receivers.add(it, curr); |
| 11612 | ir++; |
| 11613 | curr = null; |
| 11614 | it++; |
| 11615 | NT++; |
| 11616 | } else { |
| 11617 | // Skip to the next ResolveInfo in the final list. |
| 11618 | it++; |
| 11619 | curt = null; |
| 11620 | } |
| 11621 | } |
| 11622 | } |
| 11623 | while (ir < NR) { |
| 11624 | if (receivers == null) { |
| 11625 | receivers = new ArrayList(); |
| 11626 | } |
| 11627 | receivers.add(registeredReceivers.get(ir)); |
| 11628 | ir++; |
| 11629 | } |
| 11630 | |
| 11631 | if ((receivers != null && receivers.size() > 0) |
| 11632 | || resultTo != null) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11633 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11634 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11635 | callerPackage, callingPid, callingUid, requiredPermission, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11636 | receivers, resultTo, resultCode, resultData, map, ordered, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11637 | sticky, false, userId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11638 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11639 | TAG, "Enqueueing ordered broadcast " + r |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11640 | + ": prev had " + queue.mOrderedBroadcasts.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11641 | if (DEBUG_BROADCAST) { |
| 11642 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11643 | 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] | 11644 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11645 | boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11646 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11647 | queue.enqueueOrderedBroadcastLocked(r); |
| 11648 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11649 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11650 | } |
| 11651 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 11652 | return ActivityManager.BROADCAST_SUCCESS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11653 | } |
| 11654 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11655 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11656 | // Refuse possible leaked file descriptors |
| 11657 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11658 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11659 | } |
| 11660 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11661 | int flags = intent.getFlags(); |
| 11662 | |
| 11663 | if (!mProcessesReady) { |
| 11664 | // if the caller really truly claims to know what they're doing, go |
| 11665 | // ahead and allow the broadcast without launching any receivers |
| 11666 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 11667 | intent = new Intent(intent); |
| 11668 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 11669 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 11670 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 11671 | + " before boot completion"); |
| 11672 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 11673 | } |
| 11674 | } |
| 11675 | |
| 11676 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 11677 | throw new IllegalArgumentException( |
| 11678 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 11679 | } |
| 11680 | |
| 11681 | return intent; |
| 11682 | } |
| 11683 | |
| 11684 | public final int broadcastIntent(IApplicationThread caller, |
| 11685 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11686 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11687 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11688 | enforceNotIsolatedCaller("broadcastIntent"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11689 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11690 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 11691 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11692 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11693 | final int callingPid = Binder.getCallingPid(); |
| 11694 | final int callingUid = Binder.getCallingUid(); |
| 11695 | final long origId = Binder.clearCallingIdentity(); |
| 11696 | int res = broadcastIntentLocked(callerApp, |
| 11697 | callerApp != null ? callerApp.info.packageName : null, |
| 11698 | intent, resolvedType, resultTo, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11699 | resultCode, resultData, map, requiredPermission, serialized, sticky, |
| 11700 | callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11701 | Binder.restoreCallingIdentity(origId); |
| 11702 | return res; |
| 11703 | } |
| 11704 | } |
| 11705 | |
| 11706 | int broadcastIntentInPackage(String packageName, int uid, |
| 11707 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11708 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11709 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11710 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11711 | intent = verifyBroadcastLocked(intent); |
| 11712 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11713 | final long origId = Binder.clearCallingIdentity(); |
| 11714 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 11715 | resultTo, resultCode, resultData, map, requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11716 | serialized, sticky, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11717 | Binder.restoreCallingIdentity(origId); |
| 11718 | return res; |
| 11719 | } |
| 11720 | } |
| 11721 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11722 | 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] | 11723 | // Refuse possible leaked file descriptors |
| 11724 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11725 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11726 | } |
| 11727 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11728 | userId = handleIncomingUserLocked(Binder.getCallingPid(), |
| 11729 | Binder.getCallingUid(), userId, true, false, "removeStickyBroadcast", null); |
| 11730 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11731 | synchronized(this) { |
| 11732 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 11733 | != PackageManager.PERMISSION_GRANTED) { |
| 11734 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 11735 | + Binder.getCallingPid() |
| 11736 | + ", uid=" + Binder.getCallingUid() |
| 11737 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11738 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11739 | throw new SecurityException(msg); |
| 11740 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11741 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11742 | if (stickies != null) { |
| 11743 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
| 11744 | if (list != null) { |
| 11745 | int N = list.size(); |
| 11746 | int i; |
| 11747 | for (i=0; i<N; i++) { |
| 11748 | if (intent.filterEquals(list.get(i))) { |
| 11749 | list.remove(i); |
| 11750 | break; |
| 11751 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11752 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11753 | if (list.size() <= 0) { |
| 11754 | stickies.remove(intent.getAction()); |
| 11755 | } |
| 11756 | } |
| 11757 | if (stickies.size() <= 0) { |
| 11758 | mStickyBroadcasts.remove(userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11759 | } |
| 11760 | } |
| 11761 | } |
| 11762 | } |
| 11763 | |
| 11764 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 11765 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 11766 | boolean explicit) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11767 | final BroadcastRecord r = broadcastRecordForReceiverLocked(receiver); |
| 11768 | if (r == null) { |
| 11769 | 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] | 11770 | return false; |
| 11771 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11772 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11773 | return r.queue.finishReceiverLocked(r, resultCode, resultData, resultExtras, resultAbort, |
| 11774 | explicit); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11775 | } |
| 11776 | |
| 11777 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 11778 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11779 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11780 | |
| 11781 | // Refuse possible leaked file descriptors |
| 11782 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 11783 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 11784 | } |
| 11785 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11786 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11787 | try { |
| 11788 | boolean doNext = false; |
| 11789 | BroadcastRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11790 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11791 | synchronized(this) { |
| 11792 | r = broadcastRecordForReceiverLocked(who); |
| 11793 | if (r != null) { |
| 11794 | doNext = r.queue.finishReceiverLocked(r, resultCode, |
| 11795 | resultData, resultExtras, resultAbort, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11796 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11797 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 11798 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11799 | if (doNext) { |
| 11800 | r.queue.processNextBroadcast(false); |
| 11801 | } |
| 11802 | trimApplications(); |
| 11803 | } finally { |
| 11804 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11805 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11806 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11807 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11808 | // ========================================================= |
| 11809 | // INSTRUMENTATION |
| 11810 | // ========================================================= |
| 11811 | |
| 11812 | public boolean startInstrumentation(ComponentName className, |
| 11813 | String profileFile, int flags, Bundle arguments, |
| 11814 | IInstrumentationWatcher watcher) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11815 | enforceNotIsolatedCaller("startInstrumentation"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11816 | // Refuse possible leaked file descriptors |
| 11817 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 11818 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 11819 | } |
| 11820 | |
| 11821 | synchronized(this) { |
| 11822 | InstrumentationInfo ii = null; |
| 11823 | ApplicationInfo ai = null; |
| 11824 | try { |
| 11825 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11826 | className, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11827 | ai = mContext.getPackageManager().getApplicationInfo( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11828 | ii.targetPackage, STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11829 | } catch (PackageManager.NameNotFoundException e) { |
| 11830 | } |
| 11831 | if (ii == null) { |
| 11832 | reportStartInstrumentationFailure(watcher, className, |
| 11833 | "Unable to find instrumentation info for: " + className); |
| 11834 | return false; |
| 11835 | } |
| 11836 | if (ai == null) { |
| 11837 | reportStartInstrumentationFailure(watcher, className, |
| 11838 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 11839 | return false; |
| 11840 | } |
| 11841 | |
| 11842 | int match = mContext.getPackageManager().checkSignatures( |
| 11843 | ii.targetPackage, ii.packageName); |
| 11844 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 11845 | String msg = "Permission Denial: starting instrumentation " |
| 11846 | + className + " from pid=" |
| 11847 | + Binder.getCallingPid() |
| 11848 | + ", uid=" + Binder.getCallingPid() |
| 11849 | + " not allowed because package " + ii.packageName |
| 11850 | + " does not have a signature matching the target " |
| 11851 | + ii.targetPackage; |
| 11852 | reportStartInstrumentationFailure(watcher, className, msg); |
| 11853 | throw new SecurityException(msg); |
| 11854 | } |
| 11855 | |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 11856 | int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11857 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 11858 | // Instrumentation can kill and relaunch even persistent processes |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11859 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true, userId); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11860 | ProcessRecord app = addAppLocked(ai, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11861 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11862 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11863 | app.instrumentationProfileFile = profileFile; |
| 11864 | app.instrumentationArguments = arguments; |
| 11865 | app.instrumentationWatcher = watcher; |
| 11866 | app.instrumentationResultClass = className; |
| 11867 | Binder.restoreCallingIdentity(origId); |
| 11868 | } |
| 11869 | |
| 11870 | return true; |
| 11871 | } |
| 11872 | |
| 11873 | /** |
| 11874 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 11875 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 11876 | * the "am" command to report errors with more information. |
| 11877 | * |
| 11878 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 11879 | * @param cn The component name of the instrumentation. |
| 11880 | * @param report The error report. |
| 11881 | */ |
| 11882 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 11883 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11884 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11885 | try { |
| 11886 | if (watcher != null) { |
| 11887 | Bundle results = new Bundle(); |
| 11888 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 11889 | results.putString("Error", report); |
| 11890 | watcher.instrumentationStatus(cn, -1, results); |
| 11891 | } |
| 11892 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11893 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11894 | } |
| 11895 | } |
| 11896 | |
| 11897 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 11898 | if (app.instrumentationWatcher != null) { |
| 11899 | try { |
| 11900 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 11901 | app.instrumentationWatcher.instrumentationFinished( |
| 11902 | app.instrumentationClass, |
| 11903 | resultCode, |
| 11904 | results); |
| 11905 | } catch (RemoteException e) { |
| 11906 | } |
| 11907 | } |
| 11908 | app.instrumentationWatcher = null; |
| 11909 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 11910 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11911 | app.instrumentationProfileFile = null; |
| 11912 | app.instrumentationArguments = null; |
| 11913 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 11914 | forceStopPackageLocked(app.info.packageName, -1, false, false, true, true, app.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11915 | } |
| 11916 | |
| 11917 | public void finishInstrumentation(IApplicationThread target, |
| 11918 | int resultCode, Bundle results) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 11919 | int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11920 | // Refuse possible leaked file descriptors |
| 11921 | if (results != null && results.hasFileDescriptors()) { |
| 11922 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11923 | } |
| 11924 | |
| 11925 | synchronized(this) { |
| 11926 | ProcessRecord app = getRecordForAppLocked(target); |
| 11927 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11928 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11929 | return; |
| 11930 | } |
| 11931 | final long origId = Binder.clearCallingIdentity(); |
| 11932 | finishInstrumentationLocked(app, resultCode, results); |
| 11933 | Binder.restoreCallingIdentity(origId); |
| 11934 | } |
| 11935 | } |
| 11936 | |
| 11937 | // ========================================================= |
| 11938 | // CONFIGURATION |
| 11939 | // ========================================================= |
| 11940 | |
| 11941 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 11942 | ConfigurationInfo config = new ConfigurationInfo(); |
| 11943 | synchronized (this) { |
| 11944 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 11945 | config.reqKeyboardType = mConfiguration.keyboard; |
| 11946 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 11947 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 11948 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11949 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 11950 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 11951 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 11952 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11953 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 11954 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 11955 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11956 | } |
| 11957 | return config; |
| 11958 | } |
| 11959 | |
| 11960 | public Configuration getConfiguration() { |
| 11961 | Configuration ci; |
| 11962 | synchronized(this) { |
| 11963 | ci = new Configuration(mConfiguration); |
| 11964 | } |
| 11965 | return ci; |
| 11966 | } |
| 11967 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 11968 | public void updatePersistentConfiguration(Configuration values) { |
| 11969 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 11970 | "updateConfiguration()"); |
| 11971 | enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS, |
| 11972 | "updateConfiguration()"); |
| 11973 | if (values == null) { |
| 11974 | throw new NullPointerException("Configuration must not be null"); |
| 11975 | } |
| 11976 | |
| 11977 | synchronized(this) { |
| 11978 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 11979 | updateConfigurationLocked(values, null, true, false); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 11980 | Binder.restoreCallingIdentity(origId); |
| 11981 | } |
| 11982 | } |
| 11983 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11984 | public void updateConfiguration(Configuration values) { |
| 11985 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 11986 | "updateConfiguration()"); |
| 11987 | |
| 11988 | synchronized(this) { |
| 11989 | if (values == null && mWindowManager != null) { |
| 11990 | // sentinel: fetch the current configuration from the window manager |
| 11991 | values = mWindowManager.computeNewConfiguration(); |
| 11992 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 11993 | |
| 11994 | if (mWindowManager != null) { |
| 11995 | mProcessList.applyDisplaySize(mWindowManager); |
| 11996 | } |
| 11997 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11998 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 11999 | if (values != null) { |
| 12000 | Settings.System.clearConfiguration(values); |
| 12001 | } |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12002 | updateConfigurationLocked(values, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12003 | Binder.restoreCallingIdentity(origId); |
| 12004 | } |
| 12005 | } |
| 12006 | |
| 12007 | /** |
| 12008 | * Do either or both things: (1) change the current configuration, and (2) |
| 12009 | * make sure the given activity is running with the (now) current |
| 12010 | * configuration. Returns true if the activity has been left running, or |
| 12011 | * false if <var>starting</var> is being destroyed to match the new |
| 12012 | * configuration. |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12013 | * @param persistent TODO |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12014 | */ |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12015 | boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12016 | ActivityRecord starting, boolean persistent, boolean initLocale) { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 12017 | // do nothing if we are headless |
| 12018 | if (mHeadless) return true; |
| 12019 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12020 | int changes = 0; |
| 12021 | |
| 12022 | boolean kept = true; |
| 12023 | |
| 12024 | if (values != null) { |
| 12025 | Configuration newConfig = new Configuration(mConfiguration); |
| 12026 | changes = newConfig.updateFrom(values); |
| 12027 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12028 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12029 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12030 | } |
| 12031 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12032 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12033 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12034 | if (values.locale != null && !initLocale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12035 | saveLocaleLocked(values.locale, |
| 12036 | !values.locale.equals(mConfiguration.locale), |
| 12037 | values.userSetLocale); |
| 12038 | } |
| 12039 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12040 | mConfigurationSeq++; |
| 12041 | if (mConfigurationSeq <= 0) { |
| 12042 | mConfigurationSeq = 1; |
| 12043 | } |
| 12044 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12045 | mConfiguration = newConfig; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12046 | Slog.i(TAG, "Config changed: " + newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12047 | |
| 12048 | final Configuration configCopy = new Configuration(mConfiguration); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12049 | |
| 12050 | // TODO: If our config changes, should we auto dismiss any currently |
| 12051 | // showing dialogs? |
| 12052 | mShowDialogs = shouldShowDialogs(newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12053 | |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12054 | AttributeCache ac = AttributeCache.instance(); |
| 12055 | if (ac != null) { |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12056 | ac.updateConfiguration(configCopy); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12057 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12058 | |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12059 | // Make sure all resources in our process are updated |
| 12060 | // right now, so that anyone who is going to retrieve |
| 12061 | // resource values after we return will be sure to get |
| 12062 | // the new ones. This is especially important during |
| 12063 | // boot, where the first config change needs to guarantee |
| 12064 | // all resources have that config before following boot |
| 12065 | // code is executed. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12066 | mSystemThread.applyConfigurationToResources(configCopy); |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12067 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12068 | if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12069 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12070 | msg.obj = new Configuration(configCopy); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12071 | mHandler.sendMessage(msg); |
| 12072 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12073 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12074 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 12075 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12076 | try { |
| 12077 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12078 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12079 | + app.processName + " new config " + mConfiguration); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12080 | app.thread.scheduleConfigurationChanged(configCopy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12081 | } |
| 12082 | } catch (Exception e) { |
| 12083 | } |
| 12084 | } |
| 12085 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12086 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 12087 | | Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12088 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12089 | null, false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12090 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
| 12091 | broadcastIntentLocked(null, null, |
| 12092 | new Intent(Intent.ACTION_LOCALE_CHANGED), |
| 12093 | null, null, 0, null, null, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12094 | null, false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12095 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12096 | } |
| 12097 | } |
| 12098 | |
| 12099 | if (changes != 0 && starting == null) { |
| 12100 | // If the configuration changed, and the caller is not already |
| 12101 | // in the process of starting an activity, then find the top |
| 12102 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12103 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12104 | } |
| 12105 | |
| 12106 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12107 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 12108 | // And we need to make sure at this point that all other activities |
| 12109 | // are made visible with the correct configuration. |
| 12110 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12111 | } |
| 12112 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12113 | if (values != null && mWindowManager != null) { |
| 12114 | mWindowManager.setNewConfiguration(mConfiguration); |
| 12115 | } |
| 12116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12117 | return kept; |
| 12118 | } |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12119 | |
| 12120 | /** |
| 12121 | * Decide based on the configuration whether we should shouw the ANR, |
| 12122 | * crash, etc dialogs. The idea is that if there is no affordnace to |
| 12123 | * press the on-screen buttons, we shouldn't show the dialog. |
| 12124 | * |
| 12125 | * A thought: SystemUI might also want to get told about this, the Power |
| 12126 | * dialog / global actions also might want different behaviors. |
| 12127 | */ |
| 12128 | private static final boolean shouldShowDialogs(Configuration config) { |
| 12129 | return !(config.keyboard == Configuration.KEYBOARD_NOKEYS |
| 12130 | && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH); |
| 12131 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12132 | |
| 12133 | /** |
| 12134 | * Save the locale. You must be inside a synchronized (this) block. |
| 12135 | */ |
| 12136 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 12137 | if(isDiff) { |
| 12138 | SystemProperties.set("user.language", l.getLanguage()); |
| 12139 | SystemProperties.set("user.region", l.getCountry()); |
| 12140 | } |
| 12141 | |
| 12142 | if(isPersist) { |
| 12143 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 12144 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 12145 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 12146 | } |
| 12147 | } |
| 12148 | |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12149 | @Override |
| 12150 | public boolean targetTaskAffinityMatchesActivity(IBinder token, String destAffinity) { |
| 12151 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 12152 | return srec != null && srec.task.affinity != null && |
| 12153 | srec.task.affinity.equals(destAffinity); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12154 | } |
| 12155 | |
| 12156 | public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode, |
| 12157 | Intent resultData) { |
| 12158 | ComponentName dest = destIntent.getComponent(); |
| 12159 | |
| 12160 | synchronized (this) { |
| 12161 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 12162 | if (srec == null) { |
| 12163 | return false; |
| 12164 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12165 | ArrayList<ActivityRecord> history = srec.stack.mHistory; |
| 12166 | final int start = history.indexOf(srec); |
| 12167 | if (start < 0) { |
| 12168 | // Current activity is not in history stack; do nothing. |
| 12169 | return false; |
| 12170 | } |
| 12171 | int finishTo = start - 1; |
| 12172 | ActivityRecord parent = null; |
| 12173 | boolean foundParentInTask = false; |
| 12174 | if (dest != null) { |
| 12175 | TaskRecord tr = srec.task; |
| 12176 | for (int i = start - 1; i >= 0; i--) { |
| 12177 | ActivityRecord r = history.get(i); |
| 12178 | if (tr != r.task) { |
| 12179 | // Couldn't find parent in the same task; stop at the one above this. |
| 12180 | // (Root of current task; in-app "home" behavior) |
| 12181 | // Always at least finish the current activity. |
| 12182 | finishTo = Math.min(start - 1, i + 1); |
| 12183 | parent = history.get(finishTo); |
| 12184 | break; |
| 12185 | } else if (r.info.packageName.equals(dest.getPackageName()) && |
| 12186 | r.info.name.equals(dest.getClassName())) { |
| 12187 | finishTo = i; |
| 12188 | parent = r; |
| 12189 | foundParentInTask = true; |
| 12190 | break; |
| 12191 | } |
| 12192 | } |
| 12193 | } |
| 12194 | |
| 12195 | if (mController != null) { |
| 12196 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
| 12197 | if (next != null) { |
| 12198 | // ask watcher if this is allowed |
| 12199 | boolean resumeOK = true; |
| 12200 | try { |
| 12201 | resumeOK = mController.activityResuming(next.packageName); |
| 12202 | } catch (RemoteException e) { |
| 12203 | mController = null; |
| 12204 | } |
| 12205 | |
| 12206 | if (!resumeOK) { |
| 12207 | return false; |
| 12208 | } |
| 12209 | } |
| 12210 | } |
| 12211 | final long origId = Binder.clearCallingIdentity(); |
| 12212 | for (int i = start; i > finishTo; i--) { |
| 12213 | ActivityRecord r = history.get(i); |
| 12214 | mMainStack.requestFinishActivityLocked(r.appToken, resultCode, resultData, |
| 12215 | "navigate-up"); |
| 12216 | // Only return the supplied result for the first activity finished |
| 12217 | resultCode = Activity.RESULT_CANCELED; |
| 12218 | resultData = null; |
| 12219 | } |
| 12220 | |
| 12221 | if (parent != null && foundParentInTask) { |
| 12222 | final int parentLaunchMode = parent.info.launchMode; |
| 12223 | final int destIntentFlags = destIntent.getFlags(); |
| 12224 | if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE || |
| 12225 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK || |
| 12226 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP || |
| 12227 | (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) { |
Adam Powell | 69de7e1 | 2012-05-07 18:42:24 -0700 | [diff] [blame] | 12228 | parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12229 | } else { |
| 12230 | try { |
| 12231 | ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 12232 | destIntent.getComponent(), 0, UserHandle.getCallingUserId()); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12233 | int res = mMainStack.startActivityLocked(srec.app.thread, destIntent, |
| 12234 | null, aInfo, parent.appToken, null, |
| 12235 | 0, -1, parent.launchedFromUid, 0, null, true, null); |
| 12236 | foundParentInTask = res == ActivityManager.START_SUCCESS; |
| 12237 | } catch (RemoteException e) { |
| 12238 | foundParentInTask = false; |
| 12239 | } |
| 12240 | mMainStack.requestFinishActivityLocked(parent.appToken, resultCode, |
| 12241 | resultData, "navigate-up"); |
| 12242 | } |
| 12243 | } |
| 12244 | Binder.restoreCallingIdentity(origId); |
| 12245 | return foundParentInTask; |
| 12246 | } |
| 12247 | } |
| 12248 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 12249 | public int getLaunchedFromUid(IBinder activityToken) { |
| 12250 | ActivityRecord srec = ActivityRecord.forToken(activityToken); |
| 12251 | if (srec == null) { |
| 12252 | return -1; |
| 12253 | } |
| 12254 | return srec.launchedFromUid; |
| 12255 | } |
| 12256 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12257 | // ========================================================= |
| 12258 | // LIFETIME MANAGEMENT |
| 12259 | // ========================================================= |
| 12260 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12261 | // Returns which broadcast queue the app is the current [or imminent] receiver |
| 12262 | // on, or 'null' if the app is not an active broadcast recipient. |
| 12263 | private BroadcastQueue isReceivingBroadcast(ProcessRecord app) { |
| 12264 | BroadcastRecord r = app.curReceiver; |
| 12265 | if (r != null) { |
| 12266 | return r.queue; |
| 12267 | } |
| 12268 | |
| 12269 | // It's not the current receiver, but it might be starting up to become one |
| 12270 | synchronized (this) { |
| 12271 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 12272 | r = queue.mPendingBroadcast; |
| 12273 | if (r != null && r.curApp == app) { |
| 12274 | // found it; report which queue it's in |
| 12275 | return queue; |
| 12276 | } |
| 12277 | } |
| 12278 | } |
| 12279 | |
| 12280 | return null; |
| 12281 | } |
| 12282 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12283 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12284 | int emptyAdj, ProcessRecord TOP_APP, boolean recursed, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12285 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12286 | // This adjustment has already been computed. If we are calling |
| 12287 | // from the top, we may have already computed our adjustment with |
| 12288 | // an earlier hidden adjustment that isn't really for us... if |
| 12289 | // so, use the new hidden adjustment. |
| 12290 | if (!recursed && app.hidden) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12291 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = |
| 12292 | app.hasActivities ? hiddenAdj : emptyAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12293 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12294 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12295 | } |
| 12296 | |
| 12297 | if (app.thread == null) { |
| 12298 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12299 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12300 | return (app.curAdj=app.curRawAdj=ProcessList.HIDDEN_APP_MAX_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12301 | } |
| 12302 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12303 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
| 12304 | app.adjSource = null; |
| 12305 | app.adjTarget = null; |
| 12306 | app.empty = false; |
| 12307 | app.hidden = false; |
| 12308 | |
| 12309 | final int activitiesSize = app.activities.size(); |
| 12310 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12311 | if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12312 | // The max adjustment doesn't allow this app to be anything |
| 12313 | // below foreground, so it is not worth doing work for it. |
| 12314 | app.adjType = "fixed"; |
| 12315 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12316 | app.curRawAdj = app.nonStoppingAdj = app.maxAdj; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12317 | app.hasActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12318 | app.foregroundActivities = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12319 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12320 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12321 | // System process can do UI, and when they do we want to have |
| 12322 | // them trim their memory after the user leaves the UI. To |
| 12323 | // facilitate this, here we need to determine whether or not it |
| 12324 | // is currently showing UI. |
| 12325 | app.systemNoUi = true; |
| 12326 | if (app == TOP_APP) { |
| 12327 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12328 | app.hasActivities = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12329 | } else if (activitiesSize > 0) { |
| 12330 | for (int j = 0; j < activitiesSize; j++) { |
| 12331 | final ActivityRecord r = app.activities.get(j); |
| 12332 | if (r.visible) { |
| 12333 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12334 | } |
| 12335 | if (r.app == app) { |
| 12336 | app.hasActivities = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12337 | } |
| 12338 | } |
| 12339 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12340 | return (app.curAdj=app.maxAdj); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12341 | } |
| 12342 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12343 | app.keeping = false; |
| 12344 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12345 | app.hasActivities = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12346 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12347 | // Determine the importance of the process, starting with most |
| 12348 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12349 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12350 | int schedGroup; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12351 | boolean foregroundActivities = false; |
| 12352 | boolean interesting = false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12353 | BroadcastQueue queue; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12354 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12355 | // The last app on the list is the foreground app. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12356 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12357 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12358 | app.adjType = "top-activity"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12359 | foregroundActivities = true; |
| 12360 | interesting = true; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12361 | app.hasActivities = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12362 | } else if (app.instrumentationClass != null) { |
| 12363 | // Don't want to kill running instrumentation. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12364 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12365 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12366 | app.adjType = "instrumentation"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12367 | interesting = true; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12368 | } else if ((queue = isReceivingBroadcast(app)) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12369 | // An app that is currently receiving a broadcast also |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12370 | // counts as being in the foreground for OOM killer purposes. |
| 12371 | // It's placed in a sched group based on the nature of the |
| 12372 | // broadcast as reflected by which queue it's active in. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12373 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12374 | schedGroup = (queue == mFgBroadcastQueue) |
| 12375 | ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12376 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12377 | } else if (app.executingServices.size() > 0) { |
| 12378 | // An app that is currently executing a service callback also |
| 12379 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12380 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12381 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12382 | app.adjType = "exec-service"; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12383 | } else { |
| 12384 | // Assume process is hidden (has activities); we will correct |
| 12385 | // later if this is not the case. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12386 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12387 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12388 | app.hidden = true; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12389 | app.adjType = "bg-activities"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12390 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12391 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12392 | boolean hasStoppingActivities = false; |
| 12393 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12394 | // Examine all activities if not already foreground. |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12395 | if (!foregroundActivities && activitiesSize > 0) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12396 | for (int j = 0; j < activitiesSize; j++) { |
| 12397 | final ActivityRecord r = app.activities.get(j); |
| 12398 | if (r.visible) { |
| 12399 | // App has a visible activity; only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12400 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 12401 | adj = ProcessList.VISIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12402 | app.adjType = "visible"; |
| 12403 | } |
| 12404 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12405 | app.hidden = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12406 | app.hasActivities = true; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12407 | foregroundActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12408 | break; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12409 | } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12410 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12411 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12412 | app.adjType = "pausing"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12413 | } |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 12414 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12415 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12416 | } else if (r.state == ActivityState.STOPPING) { |
| 12417 | // We will apply the actual adjustment later, because |
| 12418 | // we want to allow this process to immediately go through |
| 12419 | // any memory trimming that is in effect. |
| 12420 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12421 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12422 | hasStoppingActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12423 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12424 | if (r.app == app) { |
| 12425 | app.hasActivities = true; |
| 12426 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12427 | } |
| 12428 | } |
| 12429 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12430 | if (adj == hiddenAdj && !app.hasActivities) { |
| 12431 | // Whoops, this process is completely empty as far as we know |
| 12432 | // at this point. |
| 12433 | adj = emptyAdj; |
| 12434 | app.empty = true; |
| 12435 | app.adjType = "bg-empty"; |
| 12436 | } |
| 12437 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12438 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12439 | if (app.foregroundServices) { |
| 12440 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12441 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12442 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12443 | app.adjType = "foreground-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12444 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12445 | } else if (app.forcingToForeground != null) { |
| 12446 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12447 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12448 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12449 | app.adjType = "force-foreground"; |
| 12450 | app.adjSource = app.forcingToForeground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12451 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12452 | } |
| 12453 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12454 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12455 | if (app.foregroundServices) { |
| 12456 | interesting = true; |
| 12457 | } |
| 12458 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12459 | if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12460 | // We don't want to kill the current heavy-weight process. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12461 | adj = ProcessList.HEAVY_WEIGHT_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12462 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12463 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12464 | app.adjType = "heavy"; |
| 12465 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12466 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12467 | if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12468 | // This process is hosting what we currently consider to be the |
| 12469 | // 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] | 12470 | adj = ProcessList.HOME_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12471 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12472 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12473 | app.adjType = "home"; |
| 12474 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12475 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 12476 | if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess |
| 12477 | && app.activities.size() > 0) { |
| 12478 | // This was the previous process that showed UI to the user. |
| 12479 | // We want to try to keep it around more aggressively, to give |
| 12480 | // a good experience around switching between two apps. |
| 12481 | adj = ProcessList.PREVIOUS_APP_ADJ; |
| 12482 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 12483 | app.hidden = false; |
| 12484 | app.adjType = "previous"; |
| 12485 | } |
| 12486 | |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12487 | if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj |
| 12488 | + " reason=" + app.adjType); |
| 12489 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12490 | // 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] | 12491 | // there are applications dependent on our services or providers, but |
| 12492 | // this gives us a baseline and makes sure we don't get into an |
| 12493 | // infinite recursion. |
| 12494 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12495 | app.curRawAdj = app.nonStoppingAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12496 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12497 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 12498 | // 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] | 12499 | if (adj > ProcessList.BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12500 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12501 | adj = ProcessList.BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12502 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12503 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12504 | } |
| 12505 | } |
| 12506 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12507 | if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12508 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12509 | final long now = SystemClock.uptimeMillis(); |
| 12510 | // This process is more important if the top activity is |
| 12511 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12512 | Iterator<ServiceRecord> jt = app.services.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12513 | while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12514 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12515 | if (s.startRequested) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12516 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12517 | // If this process has shown some UI, let it immediately |
| 12518 | // go to the LRU list because it may be pretty heavy with |
| 12519 | // UI stuff. We'll tag it with a label just to help |
| 12520 | // debug and understand what is going on. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12521 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12522 | app.adjType = "started-bg-ui-services"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12523 | } |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12524 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 12525 | if (now < (s.lastActivity + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12526 | // This service has seen some activity within |
| 12527 | // recent memory, so we will keep its process ahead |
| 12528 | // of the background processes. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12529 | if (adj > ProcessList.SERVICE_ADJ) { |
| 12530 | adj = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12531 | app.adjType = "started-services"; |
| 12532 | app.hidden = false; |
| 12533 | } |
| 12534 | } |
| 12535 | // If we have let the service slide into the background |
| 12536 | // state, still have some text describing what it is doing |
| 12537 | // even though the service no longer has an impact. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12538 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12539 | app.adjType = "started-bg-services"; |
| 12540 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12541 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12542 | // Don't kill this process because it is doing work; it |
| 12543 | // has said it is doing work. |
| 12544 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12545 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12546 | if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12547 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12548 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12549 | = s.connections.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12550 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12551 | ArrayList<ConnectionRecord> clist = kt.next(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12552 | 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] | 12553 | // XXX should compute this based on the max of |
| 12554 | // all connected clients. |
| 12555 | ConnectionRecord cr = clist.get(i); |
| 12556 | if (cr.binding.client == app) { |
| 12557 | // Binding to ourself is not interesting. |
| 12558 | continue; |
| 12559 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12560 | if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12561 | ProcessRecord client = cr.binding.client; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12562 | int clientAdj = adj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12563 | int myHiddenAdj = hiddenAdj; |
| 12564 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12565 | if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12566 | myHiddenAdj = client.hiddenAdj; |
| 12567 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12568 | myHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12569 | } |
| 12570 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12571 | int myEmptyAdj = emptyAdj; |
| 12572 | if (myEmptyAdj > client.emptyAdj) { |
| 12573 | if (client.emptyAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 12574 | myEmptyAdj = client.emptyAdj; |
| 12575 | } else { |
| 12576 | myEmptyAdj = ProcessList.VISIBLE_APP_ADJ; |
| 12577 | } |
| 12578 | } |
| 12579 | clientAdj = computeOomAdjLocked(client, myHiddenAdj, |
| 12580 | myEmptyAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12581 | String adjType = null; |
| 12582 | if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) { |
| 12583 | // Not doing bind OOM management, so treat |
| 12584 | // this guy more like a started service. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12585 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12586 | // If this process has shown some UI, let it immediately |
| 12587 | // go to the LRU list because it may be pretty heavy with |
| 12588 | // UI stuff. We'll tag it with a label just to help |
| 12589 | // debug and understand what is going on. |
| 12590 | if (adj > clientAdj) { |
| 12591 | adjType = "bound-bg-ui-services"; |
| 12592 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12593 | app.hidden = false; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12594 | clientAdj = adj; |
| 12595 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 12596 | if (now >= (s.lastActivity |
| 12597 | + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12598 | // This service has not seen activity within |
| 12599 | // recent memory, so allow it to drop to the |
| 12600 | // LRU list if there is no other reason to keep |
| 12601 | // it around. We'll also tag it with a label just |
| 12602 | // to help debug and undertand what is going on. |
| 12603 | if (adj > clientAdj) { |
| 12604 | adjType = "bound-bg-services"; |
| 12605 | } |
| 12606 | clientAdj = adj; |
| 12607 | } |
| 12608 | } |
| 12609 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12610 | if (adj > clientAdj) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12611 | // If this process has recently shown UI, and |
| 12612 | // the process that is binding to it is less |
| 12613 | // important than being visible, then we don't |
| 12614 | // care about the binding as much as we care |
| 12615 | // about letting this process get into the LRU |
| 12616 | // list to be killed and restarted if needed for |
| 12617 | // memory. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12618 | if (app.hasShownUi && app != mHomeProcess |
| 12619 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12620 | adjType = "bound-bg-ui-services"; |
| 12621 | } else { |
| 12622 | if ((cr.flags&(Context.BIND_ABOVE_CLIENT |
| 12623 | |Context.BIND_IMPORTANT)) != 0) { |
| 12624 | adj = clientAdj; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 12625 | } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0 |
| 12626 | && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ |
| 12627 | && adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12628 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 12629 | } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12630 | adj = clientAdj; |
| 12631 | } else { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 12632 | app.pendingUiClean = true; |
| 12633 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 12634 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 12635 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12636 | } |
| 12637 | if (!client.hidden) { |
| 12638 | app.hidden = false; |
| 12639 | } |
| 12640 | if (client.keeping) { |
| 12641 | app.keeping = true; |
| 12642 | } |
| 12643 | adjType = "service"; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12644 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12645 | } |
| 12646 | if (adjType != null) { |
| 12647 | app.adjType = adjType; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12648 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12649 | .REASON_SERVICE_IN_USE; |
| 12650 | app.adjSource = cr.binding.client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 12651 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12652 | app.adjTarget = s.name; |
| 12653 | } |
| 12654 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 12655 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 12656 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12657 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12658 | } |
| 12659 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12660 | if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) { |
| 12661 | ActivityRecord a = cr.activity; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12662 | if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ && |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12663 | (a.visible || a.state == ActivityState.RESUMED |
| 12664 | || a.state == ActivityState.PAUSING)) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12665 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12666 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 12667 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12668 | } |
| 12669 | app.hidden = false; |
| 12670 | app.adjType = "service"; |
| 12671 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12672 | .REASON_SERVICE_IN_USE; |
| 12673 | app.adjSource = a; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 12674 | app.adjSourceOom = adj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12675 | app.adjTarget = s.name; |
| 12676 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12677 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12678 | } |
| 12679 | } |
| 12680 | } |
| 12681 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12682 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12683 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12684 | // would like to avoid killing it unless it would prevent the current |
| 12685 | // application from running. By default we put the process in |
| 12686 | // with the rest of the background processes; as we scan through |
| 12687 | // its services we may bump it up from there. |
| 12688 | if (adj > hiddenAdj) { |
| 12689 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12690 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12691 | app.adjType = "bg-services"; |
| 12692 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12693 | } |
| 12694 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12695 | if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12696 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12697 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12698 | while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12699 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12700 | ContentProviderRecord cpr = jt.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12701 | for (int i = cpr.connections.size()-1; |
| 12702 | i >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
| 12703 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE); |
| 12704 | i--) { |
| 12705 | ContentProviderConnection conn = cpr.connections.get(i); |
| 12706 | ProcessRecord client = conn.client; |
| 12707 | if (client == app) { |
| 12708 | // Being our own client is not interesting. |
| 12709 | continue; |
| 12710 | } |
| 12711 | int myHiddenAdj = hiddenAdj; |
| 12712 | if (myHiddenAdj > client.hiddenAdj) { |
| 12713 | if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) { |
| 12714 | myHiddenAdj = client.hiddenAdj; |
| 12715 | } else { |
| 12716 | myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 12717 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12718 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12719 | int myEmptyAdj = emptyAdj; |
| 12720 | if (myEmptyAdj > client.emptyAdj) { |
| 12721 | if (client.emptyAdj > ProcessList.FOREGROUND_APP_ADJ) { |
| 12722 | myEmptyAdj = client.emptyAdj; |
| 12723 | } else { |
| 12724 | myEmptyAdj = ProcessList.FOREGROUND_APP_ADJ; |
| 12725 | } |
| 12726 | } |
| 12727 | int clientAdj = computeOomAdjLocked(client, myHiddenAdj, |
| 12728 | myEmptyAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12729 | if (adj > clientAdj) { |
| 12730 | if (app.hasShownUi && app != mHomeProcess |
| 12731 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12732 | app.adjType = "bg-ui-provider"; |
| 12733 | } else { |
| 12734 | adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ |
| 12735 | ? clientAdj : ProcessList.FOREGROUND_APP_ADJ; |
| 12736 | app.adjType = "provider"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12737 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12738 | if (!client.hidden) { |
| 12739 | app.hidden = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12740 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12741 | if (client.keeping) { |
| 12742 | app.keeping = true; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12743 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12744 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12745 | .REASON_PROVIDER_IN_USE; |
| 12746 | app.adjSource = client; |
| 12747 | app.adjSourceOom = clientAdj; |
| 12748 | app.adjTarget = cpr.name; |
| 12749 | } |
| 12750 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 12751 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12752 | } |
| 12753 | } |
| 12754 | // If the provider has external (non-framework) process |
| 12755 | // dependencies, ensure that its adjustment is at least |
| 12756 | // FOREGROUND_APP_ADJ. |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 12757 | if (cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12758 | if (adj > ProcessList.FOREGROUND_APP_ADJ) { |
| 12759 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12760 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12761 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12762 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12763 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 12764 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12765 | } |
| 12766 | } |
| 12767 | } |
| 12768 | } |
| 12769 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12770 | if (adj == ProcessList.SERVICE_ADJ) { |
| 12771 | if (doingAll) { |
| 12772 | app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3); |
| 12773 | mNewNumServiceProcs++; |
| 12774 | } |
| 12775 | if (app.serviceb) { |
| 12776 | adj = ProcessList.SERVICE_B_ADJ; |
| 12777 | } |
| 12778 | } else { |
| 12779 | app.serviceb = false; |
| 12780 | } |
| 12781 | |
| 12782 | app.nonStoppingAdj = adj; |
| 12783 | |
| 12784 | if (hasStoppingActivities) { |
| 12785 | // Only upgrade adjustment. |
| 12786 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12787 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 12788 | app.adjType = "stopping"; |
| 12789 | } |
| 12790 | } |
| 12791 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12792 | app.curRawAdj = adj; |
| 12793 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12794 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12795 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 12796 | if (adj > app.maxAdj) { |
| 12797 | adj = app.maxAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12798 | if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12799 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12800 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12801 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12802 | if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12803 | app.keeping = true; |
| 12804 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12805 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12806 | if (app.hasAboveClient) { |
| 12807 | // If this process has bound to any services with BIND_ABOVE_CLIENT, |
| 12808 | // then we need to drop its adjustment to be lower than the service's |
| 12809 | // in order to honor the request. We want to drop it by one adjustment |
| 12810 | // level... but there is special meaning applied to various levels so |
| 12811 | // we will skip some of them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12812 | if (adj < ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12813 | // System process will not get dropped, ever |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12814 | } else if (adj < ProcessList.VISIBLE_APP_ADJ) { |
| 12815 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 12816 | } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12817 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 12818 | } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 12819 | adj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12820 | } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12821 | adj++; |
| 12822 | } |
| 12823 | } |
| 12824 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12825 | int importance = app.memImportance; |
| 12826 | if (importance == 0 || adj != app.curAdj || schedGroup != app.curSchedGroup) { |
| 12827 | app.curAdj = adj; |
| 12828 | app.curSchedGroup = schedGroup; |
| 12829 | if (!interesting) { |
| 12830 | // For this reporting, if there is not something explicitly |
| 12831 | // interesting in this process then we will push it to the |
| 12832 | // background importance. |
| 12833 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 12834 | } else if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 12835 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 12836 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 12837 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 12838 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 12839 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 12840 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
| 12841 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 12842 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 12843 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 12844 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12845 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 12846 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 12847 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 12848 | } else if (adj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 12849 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 12850 | } else { |
| 12851 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERSISTENT; |
| 12852 | } |
| 12853 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12854 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12855 | int changes = importance != app.memImportance ? ProcessChangeItem.CHANGE_IMPORTANCE : 0; |
| 12856 | if (foregroundActivities != app.foregroundActivities) { |
| 12857 | changes |= ProcessChangeItem.CHANGE_ACTIVITIES; |
| 12858 | } |
| 12859 | if (changes != 0) { |
| 12860 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Changes in " + app + ": " + changes); |
| 12861 | app.memImportance = importance; |
| 12862 | app.foregroundActivities = foregroundActivities; |
| 12863 | int i = mPendingProcessChanges.size()-1; |
| 12864 | ProcessChangeItem item = null; |
| 12865 | while (i >= 0) { |
| 12866 | item = mPendingProcessChanges.get(i); |
| 12867 | if (item.pid == app.pid) { |
| 12868 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Re-using existing item: " + item); |
| 12869 | break; |
| 12870 | } |
| 12871 | i--; |
| 12872 | } |
| 12873 | if (i < 0) { |
| 12874 | // No existing item in pending changes; need a new one. |
| 12875 | final int NA = mAvailProcessChanges.size(); |
| 12876 | if (NA > 0) { |
| 12877 | item = mAvailProcessChanges.remove(NA-1); |
| 12878 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Retreiving available item: " + item); |
| 12879 | } else { |
| 12880 | item = new ProcessChangeItem(); |
| 12881 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Allocating new item: " + item); |
| 12882 | } |
| 12883 | item.changes = 0; |
| 12884 | item.pid = app.pid; |
| 12885 | item.uid = app.info.uid; |
| 12886 | if (mPendingProcessChanges.size() == 0) { |
| 12887 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, |
| 12888 | "*** Enqueueing dispatch processes changed!"); |
| 12889 | mHandler.obtainMessage(DISPATCH_PROCESSES_CHANGED).sendToTarget(); |
| 12890 | } |
| 12891 | mPendingProcessChanges.add(item); |
| 12892 | } |
| 12893 | item.changes |= changes; |
| 12894 | item.importance = importance; |
| 12895 | item.foregroundActivities = foregroundActivities; |
| 12896 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Item " |
| 12897 | + Integer.toHexString(System.identityHashCode(item)) |
| 12898 | + " " + app.toShortString() + ": changes=" + item.changes |
| 12899 | + " importance=" + item.importance |
| 12900 | + " foreground=" + item.foregroundActivities |
| 12901 | + " type=" + app.adjType + " source=" + app.adjSource |
| 12902 | + " target=" + app.adjTarget); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12903 | } |
| 12904 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12905 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12906 | } |
| 12907 | |
| 12908 | /** |
| 12909 | * Ask a given process to GC right now. |
| 12910 | */ |
| 12911 | final void performAppGcLocked(ProcessRecord app) { |
| 12912 | try { |
| 12913 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 12914 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12915 | if (app.reportLowMemory) { |
| 12916 | app.reportLowMemory = false; |
| 12917 | app.thread.scheduleLowMemory(); |
| 12918 | } else { |
| 12919 | app.thread.processInBackground(); |
| 12920 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12921 | } |
| 12922 | } catch (Exception e) { |
| 12923 | // whatever. |
| 12924 | } |
| 12925 | } |
| 12926 | |
| 12927 | /** |
| 12928 | * Returns true if things are idle enough to perform GCs. |
| 12929 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12930 | private final boolean canGcNowLocked() { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12931 | boolean processingBroadcasts = false; |
| 12932 | for (BroadcastQueue q : mBroadcastQueues) { |
| 12933 | if (q.mParallelBroadcasts.size() != 0 || q.mOrderedBroadcasts.size() != 0) { |
| 12934 | processingBroadcasts = true; |
| 12935 | } |
| 12936 | } |
| 12937 | return !processingBroadcasts |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12938 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 12939 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12940 | } |
| 12941 | |
| 12942 | /** |
| 12943 | * Perform GCs on all processes that are waiting for it, but only |
| 12944 | * if things are idle. |
| 12945 | */ |
| 12946 | final void performAppGcsLocked() { |
| 12947 | final int N = mProcessesToGc.size(); |
| 12948 | if (N <= 0) { |
| 12949 | return; |
| 12950 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12951 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12952 | while (mProcessesToGc.size() > 0) { |
| 12953 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12954 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12955 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 12956 | <= SystemClock.uptimeMillis()) { |
| 12957 | // To avoid spamming the system, we will GC processes one |
| 12958 | // at a time, waiting a few seconds between each. |
| 12959 | performAppGcLocked(proc); |
| 12960 | scheduleAppGcsLocked(); |
| 12961 | return; |
| 12962 | } else { |
| 12963 | // It hasn't been long enough since we last GCed this |
| 12964 | // process... put it in the list to wait for its time. |
| 12965 | addProcessToGcListLocked(proc); |
| 12966 | break; |
| 12967 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12968 | } |
| 12969 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12970 | |
| 12971 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12972 | } |
| 12973 | } |
| 12974 | |
| 12975 | /** |
| 12976 | * If all looks good, perform GCs on all processes waiting for them. |
| 12977 | */ |
| 12978 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 12979 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12980 | performAppGcsLocked(); |
| 12981 | return; |
| 12982 | } |
| 12983 | // Still not idle, wait some more. |
| 12984 | scheduleAppGcsLocked(); |
| 12985 | } |
| 12986 | |
| 12987 | /** |
| 12988 | * Schedule the execution of all pending app GCs. |
| 12989 | */ |
| 12990 | final void scheduleAppGcsLocked() { |
| 12991 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12992 | |
| 12993 | if (mProcessesToGc.size() > 0) { |
| 12994 | // Schedule a GC for the time to the next process. |
| 12995 | ProcessRecord proc = mProcessesToGc.get(0); |
| 12996 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 12997 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 12998 | long when = proc.lastRequestedGc + GC_MIN_INTERVAL; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12999 | long now = SystemClock.uptimeMillis(); |
| 13000 | if (when < (now+GC_TIMEOUT)) { |
| 13001 | when = now + GC_TIMEOUT; |
| 13002 | } |
| 13003 | mHandler.sendMessageAtTime(msg, when); |
| 13004 | } |
| 13005 | } |
| 13006 | |
| 13007 | /** |
| 13008 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 13009 | * list in sorted order by the last GC time. The process can't already be |
| 13010 | * on the list. |
| 13011 | */ |
| 13012 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 13013 | boolean added = false; |
| 13014 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 13015 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 13016 | proc.lastRequestedGc) { |
| 13017 | added = true; |
| 13018 | mProcessesToGc.add(i+1, proc); |
| 13019 | break; |
| 13020 | } |
| 13021 | } |
| 13022 | if (!added) { |
| 13023 | mProcessesToGc.add(0, proc); |
| 13024 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13025 | } |
| 13026 | |
| 13027 | /** |
| 13028 | * Set up to ask a process to GC itself. This will either do it |
| 13029 | * immediately, or put it on the list of processes to gc the next |
| 13030 | * time things are idle. |
| 13031 | */ |
| 13032 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 13033 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13034 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13035 | return; |
| 13036 | } |
| 13037 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13038 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13039 | scheduleAppGcsLocked(); |
| 13040 | } |
| 13041 | } |
| 13042 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13043 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 13044 | updateCpuStatsNow(); |
| 13045 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13046 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13047 | boolean doWakeKills = doKills; |
| 13048 | boolean doCpuKills = doKills; |
| 13049 | if (mLastPowerCheckRealtime == 0) { |
| 13050 | doWakeKills = false; |
| 13051 | } |
| 13052 | if (mLastPowerCheckUptime == 0) { |
| 13053 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13054 | } |
| 13055 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13056 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13057 | } |
| 13058 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13059 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 13060 | final long curUptime = SystemClock.uptimeMillis(); |
| 13061 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 13062 | mLastPowerCheckRealtime = curRealtime; |
| 13063 | mLastPowerCheckUptime = curUptime; |
| 13064 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 13065 | doWakeKills = false; |
| 13066 | } |
| 13067 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 13068 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13069 | } |
| 13070 | int i = mLruProcesses.size(); |
| 13071 | while (i > 0) { |
| 13072 | i--; |
| 13073 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13074 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13075 | long wtime; |
| 13076 | synchronized (stats) { |
| 13077 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 13078 | app.pid, curRealtime); |
| 13079 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13080 | long wtimeUsed = wtime - app.lastWakeTime; |
| 13081 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 13082 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13083 | StringBuilder sb = new StringBuilder(128); |
| 13084 | sb.append("Wake for "); |
| 13085 | app.toShortString(sb); |
| 13086 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13087 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13088 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13089 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13090 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13091 | sb.append((wtimeUsed*100)/realtimeSince); |
| 13092 | sb.append("%)"); |
| 13093 | Slog.i(TAG, sb.toString()); |
| 13094 | sb.setLength(0); |
| 13095 | sb.append("CPU for "); |
| 13096 | app.toShortString(sb); |
| 13097 | sb.append(": over "); |
| 13098 | TimeUtils.formatDuration(uptimeSince, sb); |
| 13099 | sb.append(" used "); |
| 13100 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 13101 | sb.append(" ("); |
| 13102 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13103 | sb.append("%)"); |
| 13104 | Slog.i(TAG, sb.toString()); |
| 13105 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13106 | // If a process has held a wake lock for more |
| 13107 | // than 50% of the time during this period, |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13108 | // that sounds bad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13109 | if (doWakeKills && realtimeSince > 0 |
| 13110 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 13111 | synchronized (stats) { |
| 13112 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 13113 | realtimeSince, wtimeUsed); |
| 13114 | } |
| 13115 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 13116 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 13117 | + " during " + realtimeSince); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13118 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13119 | app.processName, app.setAdj, "excessive wake lock"); |
| 13120 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13121 | } else if (doCpuKills && uptimeSince > 0 |
| 13122 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 13123 | synchronized (stats) { |
| 13124 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 13125 | uptimeSince, cputimeUsed); |
| 13126 | } |
| 13127 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 13128 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 13129 | + " during " + uptimeSince); |
| 13130 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13131 | app.processName, app.setAdj, "excessive cpu"); |
| 13132 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13133 | } else { |
| 13134 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13135 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13136 | } |
| 13137 | } |
| 13138 | } |
| 13139 | } |
| 13140 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13141 | private final boolean updateOomAdjLocked(ProcessRecord app, int hiddenAdj, |
| 13142 | int emptyAdj, ProcessRecord TOP_APP, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13143 | app.hiddenAdj = hiddenAdj; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13144 | app.emptyAdj = emptyAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13145 | |
| 13146 | if (app.thread == null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13147 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13148 | } |
| 13149 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13150 | final boolean wasKeeping = app.keeping; |
| 13151 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13152 | boolean success = true; |
| 13153 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13154 | computeOomAdjLocked(app, hiddenAdj, emptyAdj, TOP_APP, false, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13155 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13156 | if (app.curRawAdj != app.setRawAdj) { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13157 | if (wasKeeping && !app.keeping) { |
| 13158 | // This app is no longer something we want to keep. Note |
| 13159 | // its current wake lock time to later know to kill it if |
| 13160 | // it is not behaving well. |
| 13161 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 13162 | synchronized (stats) { |
| 13163 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 13164 | app.pid, SystemClock.elapsedRealtime()); |
| 13165 | } |
| 13166 | app.lastCpuTime = app.curCpuTime; |
| 13167 | } |
| 13168 | |
| 13169 | app.setRawAdj = app.curRawAdj; |
| 13170 | } |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13171 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13172 | if (app.curAdj != app.setAdj) { |
| 13173 | if (Process.setOomAdj(app.pid, app.curAdj)) { |
Dianne Hackborn | bbb09ac | 2011-11-30 11:31:29 -0800 | [diff] [blame] | 13174 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13175 | TAG, "Set " + app.pid + " " + app.processName + |
| 13176 | " adj " + app.curAdj + ": " + app.adjType); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13177 | app.setAdj = app.curAdj; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13178 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13179 | success = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13180 | Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13181 | } |
| 13182 | } |
| 13183 | if (app.setSchedGroup != app.curSchedGroup) { |
| 13184 | app.setSchedGroup = app.curSchedGroup; |
| 13185 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
| 13186 | "Setting process group of " + app.processName |
| 13187 | + " to " + app.curSchedGroup); |
| 13188 | if (app.waitingToKill != null && |
| 13189 | app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 13190 | Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill); |
| 13191 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13192 | app.processName, app.setAdj, app.waitingToKill); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 13193 | app.killedBackground = true; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13194 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13195 | success = false; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13196 | } else { |
| 13197 | if (true) { |
| 13198 | long oldId = Binder.clearCallingIdentity(); |
| 13199 | try { |
| 13200 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 13201 | } catch (Exception e) { |
| 13202 | Slog.w(TAG, "Failed setting process group of " + app.pid |
| 13203 | + " to " + app.curSchedGroup); |
| 13204 | e.printStackTrace(); |
| 13205 | } finally { |
| 13206 | Binder.restoreCallingIdentity(oldId); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13207 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13208 | } else { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13209 | if (app.thread != null) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13210 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13211 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 13212 | } catch (RemoteException e) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13213 | } |
| 13214 | } |
| 13215 | } |
| 13216 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13217 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13218 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13219 | } |
| 13220 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13221 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13222 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13223 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 13224 | resumedActivity = mMainStack.mPausingActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13225 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13226 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13227 | } |
| 13228 | } |
| 13229 | return resumedActivity; |
| 13230 | } |
| 13231 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 13232 | final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13233 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13234 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13235 | int curAdj = app.curAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13236 | final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13237 | && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13238 | |
| 13239 | mAdjSeq++; |
| 13240 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13241 | boolean success = updateOomAdjLocked(app, app.hiddenAdj, app.emptyAdj, |
| 13242 | TOP_APP, false); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13243 | final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13244 | && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13245 | if (nowHidden != wasHidden) { |
| 13246 | // Changed to/from hidden state, so apps after it in the LRU |
| 13247 | // list may also be changed. |
| 13248 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13249 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13250 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13251 | } |
| 13252 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13253 | final void updateOomAdjLocked() { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13254 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13255 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13256 | |
| 13257 | if (false) { |
| 13258 | RuntimeException e = new RuntimeException(); |
| 13259 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13260 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13261 | } |
| 13262 | |
| 13263 | mAdjSeq++; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13264 | mNewNumServiceProcs = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13265 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13266 | // Let's determine how many processes we have running vs. |
| 13267 | // how many slots we have for background processes; we may want |
| 13268 | // to put multiple processes in a slot of there are enough of |
| 13269 | // them. |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13270 | int numSlots = (ProcessList.HIDDEN_APP_MAX_ADJ |
| 13271 | - ProcessList.HIDDEN_APP_MIN_ADJ + 1) / 2; |
| 13272 | int emptyFactor = (mLruProcesses.size()-mNumNonHiddenProcs-mNumHiddenProcs)/numSlots; |
| 13273 | if (emptyFactor < 1) emptyFactor = 1; |
| 13274 | int hiddenFactor = (mNumHiddenProcs > 0 ? mNumHiddenProcs : 1)/numSlots; |
| 13275 | if (hiddenFactor < 1) hiddenFactor = 1; |
| 13276 | int stepHidden = 0; |
| 13277 | int stepEmpty = 0; |
| 13278 | final int emptyProcessLimit = mProcessLimit > 1 ? mProcessLimit / 2 : mProcessLimit; |
| 13279 | final int hiddenProcessLimit = mProcessLimit > 1 ? mProcessLimit / 2 : mProcessLimit; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13280 | int numHidden = 0; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13281 | int numEmpty = 0; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13282 | int numTrimming = 0; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13283 | |
| 13284 | mNumNonHiddenProcs = 0; |
| 13285 | mNumHiddenProcs = 0; |
| 13286 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13287 | // First update the OOM adjustment for each of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13288 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13289 | int i = mLruProcesses.size(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13290 | int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13291 | int nextHiddenAdj = curHiddenAdj+1; |
| 13292 | int curEmptyAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
| 13293 | int nextEmptyAdj = curEmptyAdj+2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13294 | while (i > 0) { |
| 13295 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13296 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13297 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13298 | updateOomAdjLocked(app, curHiddenAdj, curEmptyAdj, TOP_APP, true); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13299 | if (!app.killedBackground) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13300 | if (app.curRawAdj == curHiddenAdj && app.hasActivities) { |
| 13301 | // This process was assigned as a hidden process... step the |
| 13302 | // hidden level. |
| 13303 | mNumHiddenProcs++; |
| 13304 | if (curHiddenAdj != nextHiddenAdj) { |
| 13305 | stepHidden++; |
| 13306 | if (stepHidden >= hiddenFactor) { |
| 13307 | stepHidden = 0; |
| 13308 | curHiddenAdj = nextHiddenAdj; |
| 13309 | nextHiddenAdj += 2; |
| 13310 | if (nextHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13311 | nextHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13312 | } |
| 13313 | } |
| 13314 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13315 | numHidden++; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13316 | if (numHidden > hiddenProcessLimit) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13317 | Slog.i(TAG, "No longer want " + app.processName |
| 13318 | + " (pid " + app.pid + "): hidden #" + numHidden); |
| 13319 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13320 | app.processName, app.setAdj, "too many background"); |
| 13321 | app.killedBackground = true; |
| 13322 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13323 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13324 | } else { |
| 13325 | if (app.curRawAdj == curEmptyAdj || app.curRawAdj == curHiddenAdj) { |
| 13326 | // This process was assigned as an empty process... step the |
| 13327 | // empty level. |
| 13328 | if (curEmptyAdj != nextEmptyAdj) { |
| 13329 | stepEmpty++; |
| 13330 | if (stepEmpty >= emptyFactor) { |
| 13331 | stepEmpty = 0; |
| 13332 | curEmptyAdj = nextEmptyAdj; |
| 13333 | nextEmptyAdj += 2; |
| 13334 | if (nextEmptyAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13335 | nextEmptyAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13336 | } |
| 13337 | } |
| 13338 | } |
| 13339 | } else if (app.curRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13340 | mNumNonHiddenProcs++; |
| 13341 | } |
| 13342 | if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13343 | numEmpty++; |
| 13344 | if (numEmpty > emptyProcessLimit) { |
| 13345 | Slog.i(TAG, "No longer want " + app.processName |
| 13346 | + " (pid " + app.pid + "): empty #" + numEmpty); |
| 13347 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13348 | app.processName, app.setAdj, "too many background"); |
| 13349 | app.killedBackground = true; |
| 13350 | Process.killProcessQuiet(app.pid); |
| 13351 | } |
| 13352 | } |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13353 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13354 | if (app.isolated && app.services.size() <= 0) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13355 | // If this is an isolated process, and there are no |
| 13356 | // services running in it, then the process is no longer |
| 13357 | // needed. We agressively kill these because we can by |
| 13358 | // definition not re-use the same process again, and it is |
| 13359 | // good to avoid having whatever code was running in them |
| 13360 | // left sitting around after no longer needed. |
| 13361 | Slog.i(TAG, "Isolated process " + app.processName |
| 13362 | + " (pid " + app.pid + ") no longer needed"); |
| 13363 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13364 | app.processName, app.setAdj, "isolated not needed"); |
| 13365 | app.killedBackground = true; |
| 13366 | Process.killProcessQuiet(app.pid); |
| 13367 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13368 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 13369 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
| 13370 | && !app.killedBackground) { |
| 13371 | numTrimming++; |
| 13372 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13373 | } |
| 13374 | } |
| 13375 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13376 | mNumServiceProcs = mNewNumServiceProcs; |
| 13377 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13378 | // Now determine the memory trimming level of background processes. |
| 13379 | // Unfortunately we need to start at the back of the list to do this |
| 13380 | // properly. We only do this if the number of background apps we |
| 13381 | // are managing to keep around is less than half the maximum we desire; |
| 13382 | // if we are keeping a good number around, we'll let them use whatever |
| 13383 | // memory they want. |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13384 | if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/4) |
| 13385 | && numEmpty <= (ProcessList.MAX_HIDDEN_APPS/4)) { |
| 13386 | final int numHiddenAndEmpty = numHidden + numEmpty; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13387 | final int N = mLruProcesses.size(); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13388 | int factor = numTrimming/3; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13389 | int minFactor = 2; |
| 13390 | if (mHomeProcess != null) minFactor++; |
| 13391 | if (mPreviousProcess != null) minFactor++; |
| 13392 | if (factor < minFactor) factor = minFactor; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13393 | int step = 0; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13394 | int fgTrimLevel; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13395 | if (numHiddenAndEmpty <= (ProcessList.MAX_HIDDEN_APPS/5)) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13396 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13397 | } else if (numHiddenAndEmpty <= (ProcessList.MAX_HIDDEN_APPS/3)) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13398 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW; |
| 13399 | } else { |
| 13400 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE; |
| 13401 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13402 | int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13403 | for (i=0; i<N; i++) { |
| 13404 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13405 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 13406 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13407 | && !app.killedBackground) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13408 | if (app.trimMemoryLevel < curLevel && app.thread != null) { |
| 13409 | try { |
| 13410 | app.thread.scheduleTrimMemory(curLevel); |
| 13411 | } catch (RemoteException e) { |
| 13412 | } |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13413 | if (false) { |
| 13414 | // For now we won't do this; our memory trimming seems |
| 13415 | // to be good enough at this point that destroying |
| 13416 | // activities causes more harm than good. |
| 13417 | if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE |
| 13418 | && app != mHomeProcess && app != mPreviousProcess) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13419 | // Need to do this on its own message because the stack may not |
| 13420 | // be in a consistent state at this point. |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13421 | // For these apps we will also finish their activities |
| 13422 | // to help them free memory. |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13423 | mMainStack.scheduleDestroyActivities(app, false, "trim"); |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13424 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13425 | } |
| 13426 | } |
| 13427 | app.trimMemoryLevel = curLevel; |
| 13428 | step++; |
| 13429 | if (step >= factor) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13430 | step = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13431 | switch (curLevel) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13432 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 13433 | curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13434 | break; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13435 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 13436 | curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13437 | break; |
| 13438 | } |
| 13439 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13440 | } else if (app.nonStoppingAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13441 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13442 | && app.thread != null) { |
| 13443 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13444 | app.thread.scheduleTrimMemory( |
| 13445 | ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13446 | } catch (RemoteException e) { |
| 13447 | } |
| 13448 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13449 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13450 | } else { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13451 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13452 | && app.pendingUiClean) { |
| 13453 | // If this application is now in the background and it |
| 13454 | // had done UI, then give it the special trim level to |
| 13455 | // have it free UI resources. |
| 13456 | final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 13457 | if (app.trimMemoryLevel < level && app.thread != null) { |
| 13458 | try { |
| 13459 | app.thread.scheduleTrimMemory(level); |
| 13460 | } catch (RemoteException e) { |
| 13461 | } |
| 13462 | } |
| 13463 | app.pendingUiClean = false; |
| 13464 | } |
| 13465 | if (app.trimMemoryLevel < fgTrimLevel && app.thread != null) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13466 | try { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13467 | app.thread.scheduleTrimMemory(fgTrimLevel); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13468 | } catch (RemoteException e) { |
| 13469 | } |
| 13470 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13471 | app.trimMemoryLevel = fgTrimLevel; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13472 | } |
| 13473 | } |
| 13474 | } else { |
| 13475 | final int N = mLruProcesses.size(); |
| 13476 | for (i=0; i<N; i++) { |
| 13477 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13478 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13479 | && app.pendingUiClean) { |
| 13480 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 13481 | && app.thread != null) { |
| 13482 | try { |
| 13483 | app.thread.scheduleTrimMemory( |
| 13484 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 13485 | } catch (RemoteException e) { |
| 13486 | } |
| 13487 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13488 | app.pendingUiClean = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13489 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13490 | app.trimMemoryLevel = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13491 | } |
| 13492 | } |
| 13493 | |
| 13494 | if (mAlwaysFinishActivities) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13495 | // Need to do this on its own message because the stack may not |
| 13496 | // be in a consistent state at this point. |
| 13497 | mMainStack.scheduleDestroyActivities(null, false, "always-finish"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13498 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13499 | } |
| 13500 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13501 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13502 | synchronized (this) { |
| 13503 | int i; |
| 13504 | |
| 13505 | // First remove any unused application processes whose package |
| 13506 | // has been removed. |
| 13507 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 13508 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 13509 | if (app.activities.size() == 0 |
| 13510 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13511 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13512 | TAG, "Exiting empty application process " |
| 13513 | + app.processName + " (" |
| 13514 | + (app.thread != null ? app.thread.asBinder() : null) |
| 13515 | + ")\n"); |
| 13516 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 13517 | EventLog.writeEvent(EventLogTags.AM_KILL, app.pid, |
| 13518 | app.processName, app.setAdj, "empty"); |
| 13519 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13520 | } else { |
| 13521 | try { |
| 13522 | app.thread.scheduleExit(); |
| 13523 | } catch (Exception e) { |
| 13524 | // Ignore exceptions. |
| 13525 | } |
| 13526 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13527 | cleanUpApplicationRecordLocked(app, false, true, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13528 | mRemovedProcesses.remove(i); |
| 13529 | |
| 13530 | if (app.persistent) { |
| 13531 | if (app.persistent) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13532 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13533 | } |
| 13534 | } |
| 13535 | } |
| 13536 | } |
| 13537 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13538 | // Now update the oom adj for all processes. |
| 13539 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13540 | } |
| 13541 | } |
| 13542 | |
| 13543 | /** This method sends the specified signal to each of the persistent apps */ |
| 13544 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 13545 | if (sig != Process.SIGNAL_USR1) { |
| 13546 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 13547 | } |
| 13548 | |
| 13549 | synchronized (this) { |
| 13550 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 13551 | != PackageManager.PERMISSION_GRANTED) { |
| 13552 | throw new SecurityException("Requires permission " |
| 13553 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 13554 | } |
| 13555 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13556 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 13557 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13558 | if (r.thread != null && r.persistent) { |
| 13559 | Process.sendSignal(r.pid, sig); |
| 13560 | } |
| 13561 | } |
| 13562 | } |
| 13563 | } |
| 13564 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13565 | private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) { |
| 13566 | if (proc == null || proc == mProfileProc) { |
| 13567 | proc = mProfileProc; |
| 13568 | path = mProfileFile; |
| 13569 | profileType = mProfileType; |
| 13570 | clearProfilerLocked(); |
| 13571 | } |
| 13572 | if (proc == null) { |
| 13573 | return; |
| 13574 | } |
| 13575 | try { |
| 13576 | proc.thread.profilerControl(false, path, null, profileType); |
| 13577 | } catch (RemoteException e) { |
| 13578 | throw new IllegalStateException("Process disappeared"); |
| 13579 | } |
| 13580 | } |
| 13581 | |
| 13582 | private void clearProfilerLocked() { |
| 13583 | if (mProfileFd != null) { |
| 13584 | try { |
| 13585 | mProfileFd.close(); |
| 13586 | } catch (IOException e) { |
| 13587 | } |
| 13588 | } |
| 13589 | mProfileApp = null; |
| 13590 | mProfileProc = null; |
| 13591 | mProfileFile = null; |
| 13592 | mProfileType = 0; |
| 13593 | mAutoStopProfiler = false; |
| 13594 | } |
| 13595 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13596 | public boolean profileControl(String process, boolean start, |
Romain Guy | 7eabe55 | 2011-07-21 14:56:34 -0700 | [diff] [blame] | 13597 | String path, ParcelFileDescriptor fd, int profileType) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13598 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13599 | try { |
| 13600 | synchronized (this) { |
| 13601 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 13602 | // its own permission. |
| 13603 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 13604 | != PackageManager.PERMISSION_GRANTED) { |
| 13605 | throw new SecurityException("Requires permission " |
| 13606 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13607 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13608 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13609 | if (start && fd == null) { |
| 13610 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13611 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13612 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13613 | ProcessRecord proc = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13614 | if (process != null) { |
| 13615 | try { |
| 13616 | int pid = Integer.parseInt(process); |
| 13617 | synchronized (mPidsSelfLocked) { |
| 13618 | proc = mPidsSelfLocked.get(pid); |
| 13619 | } |
| 13620 | } catch (NumberFormatException e) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13621 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13622 | |
| 13623 | if (proc == null) { |
| 13624 | HashMap<String, SparseArray<ProcessRecord>> all |
| 13625 | = mProcessNames.getMap(); |
| 13626 | SparseArray<ProcessRecord> procs = all.get(process); |
| 13627 | if (procs != null && procs.size() > 0) { |
| 13628 | proc = procs.valueAt(0); |
| 13629 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13630 | } |
| 13631 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13632 | |
| 13633 | if (start && (proc == null || proc.thread == null)) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13634 | throw new IllegalArgumentException("Unknown process: " + process); |
| 13635 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13636 | |
| 13637 | if (start) { |
| 13638 | stopProfilerLocked(null, null, 0); |
| 13639 | setProfileApp(proc.info, proc.processName, path, fd, false); |
| 13640 | mProfileProc = proc; |
| 13641 | mProfileType = profileType; |
| 13642 | try { |
| 13643 | fd = fd.dup(); |
| 13644 | } catch (IOException e) { |
| 13645 | fd = null; |
| 13646 | } |
| 13647 | proc.thread.profilerControl(start, path, fd, profileType); |
| 13648 | fd = null; |
| 13649 | mProfileFd = null; |
| 13650 | } else { |
| 13651 | stopProfilerLocked(proc, path, profileType); |
| 13652 | if (fd != null) { |
| 13653 | try { |
| 13654 | fd.close(); |
| 13655 | } catch (IOException e) { |
| 13656 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13657 | } |
| 13658 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13659 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13660 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13661 | } |
| 13662 | } catch (RemoteException e) { |
| 13663 | throw new IllegalStateException("Process disappeared"); |
| 13664 | } finally { |
| 13665 | if (fd != null) { |
| 13666 | try { |
| 13667 | fd.close(); |
| 13668 | } catch (IOException e) { |
| 13669 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13670 | } |
| 13671 | } |
| 13672 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13673 | |
| 13674 | public boolean dumpHeap(String process, boolean managed, |
| 13675 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 13676 | |
| 13677 | try { |
| 13678 | synchronized (this) { |
| 13679 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 13680 | // its own permission (same as profileControl). |
| 13681 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 13682 | != PackageManager.PERMISSION_GRANTED) { |
| 13683 | throw new SecurityException("Requires permission " |
| 13684 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 13685 | } |
| 13686 | |
| 13687 | if (fd == null) { |
| 13688 | throw new IllegalArgumentException("null fd"); |
| 13689 | } |
| 13690 | |
| 13691 | ProcessRecord proc = null; |
| 13692 | try { |
| 13693 | int pid = Integer.parseInt(process); |
| 13694 | synchronized (mPidsSelfLocked) { |
| 13695 | proc = mPidsSelfLocked.get(pid); |
| 13696 | } |
| 13697 | } catch (NumberFormatException e) { |
| 13698 | } |
| 13699 | |
| 13700 | if (proc == null) { |
| 13701 | HashMap<String, SparseArray<ProcessRecord>> all |
| 13702 | = mProcessNames.getMap(); |
| 13703 | SparseArray<ProcessRecord> procs = all.get(process); |
| 13704 | if (procs != null && procs.size() > 0) { |
| 13705 | proc = procs.valueAt(0); |
| 13706 | } |
| 13707 | } |
| 13708 | |
| 13709 | if (proc == null || proc.thread == null) { |
| 13710 | throw new IllegalArgumentException("Unknown process: " + process); |
| 13711 | } |
| 13712 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 13713 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 13714 | if (!isDebuggable) { |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13715 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 13716 | throw new SecurityException("Process not debuggable: " + proc); |
| 13717 | } |
| 13718 | } |
| 13719 | |
| 13720 | proc.thread.dumpHeap(managed, path, fd); |
| 13721 | fd = null; |
| 13722 | return true; |
| 13723 | } |
| 13724 | } catch (RemoteException e) { |
| 13725 | throw new IllegalStateException("Process disappeared"); |
| 13726 | } finally { |
| 13727 | if (fd != null) { |
| 13728 | try { |
| 13729 | fd.close(); |
| 13730 | } catch (IOException e) { |
| 13731 | } |
| 13732 | } |
| 13733 | } |
| 13734 | } |
| 13735 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13736 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 13737 | public void monitor() { |
| 13738 | synchronized (this) { } |
| 13739 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 13740 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 13741 | void onCoreSettingsChange(Bundle settings) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 13742 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 13743 | ProcessRecord processRecord = mLruProcesses.get(i); |
| 13744 | try { |
| 13745 | if (processRecord.thread != null) { |
| 13746 | processRecord.thread.setCoreSettings(settings); |
| 13747 | } |
| 13748 | } catch (RemoteException re) { |
| 13749 | /* ignore */ |
| 13750 | } |
| 13751 | } |
| 13752 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 13753 | |
| 13754 | // Multi-user methods |
| 13755 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13756 | @Override |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13757 | public boolean switchUser(int userId) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13758 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 13759 | != PackageManager.PERMISSION_GRANTED) { |
| 13760 | String msg = "Permission Denial: switchUser() from pid=" |
| 13761 | + Binder.getCallingPid() |
| 13762 | + ", uid=" + Binder.getCallingUid() |
| 13763 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 13764 | Slog.w(TAG, msg); |
| 13765 | throw new SecurityException(msg); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13766 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13767 | synchronized (this) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13768 | if (mCurrentUserId == userId) { |
| 13769 | return true; |
| 13770 | } |
| 13771 | |
| 13772 | // If the user we are switching to is not currently started, then |
| 13773 | // we need to start it now. |
| 13774 | if (mStartedUsers.get(userId) == null) { |
| 13775 | mStartedUsers.put(userId, new UserStartedState(new UserHandle(userId), false)); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13776 | } |
| 13777 | |
| 13778 | mCurrentUserId = userId; |
| 13779 | boolean haveActivities = mMainStack.switchUser(userId); |
| 13780 | if (!haveActivities) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13781 | startHomeActivityLocked(userId, mStartedUsers.get(userId)); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13782 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 13783 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 13784 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13785 | long ident = Binder.clearCallingIdentity(); |
| 13786 | try { |
| 13787 | // Inform of user switch |
| 13788 | Intent addedIntent = new Intent(Intent.ACTION_USER_SWITCHED); |
| 13789 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 13790 | mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 13791 | android.Manifest.permission.MANAGE_USERS); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13792 | } finally { |
| 13793 | Binder.restoreCallingIdentity(ident); |
| 13794 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 13795 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 13796 | return true; |
| 13797 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13798 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13799 | void finishUserSwitch(UserStartedState uss) { |
| 13800 | synchronized (this) { |
| 13801 | if (uss.mState == UserStartedState.STATE_BOOTING |
| 13802 | && mStartedUsers.get(uss.mHandle.getIdentifier()) == uss) { |
| 13803 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 13804 | final int userId = uss.mHandle.getIdentifier(); |
| 13805 | Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 13806 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 13807 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13808 | null, null, 0, null, null, |
| 13809 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 13810 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13811 | } |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 13812 | } |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 13813 | } |
| 13814 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13815 | @Override |
| 13816 | public int stopUser(final int userId, final IStopUserCallback callback) { |
| 13817 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 13818 | != PackageManager.PERMISSION_GRANTED) { |
| 13819 | String msg = "Permission Denial: switchUser() from pid=" |
| 13820 | + Binder.getCallingPid() |
| 13821 | + ", uid=" + Binder.getCallingUid() |
| 13822 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 13823 | Slog.w(TAG, msg); |
| 13824 | throw new SecurityException(msg); |
| 13825 | } |
| 13826 | if (userId <= 0) { |
| 13827 | throw new IllegalArgumentException("Can't stop primary user " + userId); |
| 13828 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 13829 | synchronized (this) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13830 | if (mCurrentUserId == userId) { |
| 13831 | return ActivityManager.USER_OP_IS_CURRENT; |
| 13832 | } |
| 13833 | |
| 13834 | final UserStartedState uss = mStartedUsers.get(userId); |
| 13835 | if (uss == null) { |
| 13836 | // User is not started, nothing to do... but we do need to |
| 13837 | // callback if requested. |
| 13838 | if (callback != null) { |
| 13839 | mHandler.post(new Runnable() { |
| 13840 | @Override |
| 13841 | public void run() { |
| 13842 | try { |
| 13843 | callback.userStopped(userId); |
| 13844 | } catch (RemoteException e) { |
| 13845 | } |
| 13846 | } |
| 13847 | }); |
| 13848 | } |
| 13849 | return ActivityManager.USER_OP_SUCCESS; |
| 13850 | } |
| 13851 | |
| 13852 | if (callback != null) { |
| 13853 | uss.mStopCallbacks.add(callback); |
| 13854 | } |
| 13855 | |
| 13856 | if (uss.mState != UserStartedState.STATE_STOPPING) { |
| 13857 | uss.mState = UserStartedState.STATE_STOPPING; |
| 13858 | |
| 13859 | long ident = Binder.clearCallingIdentity(); |
| 13860 | try { |
| 13861 | // Inform of user switch |
| 13862 | Intent intent = new Intent(Intent.ACTION_SHUTDOWN); |
| 13863 | final IIntentReceiver resultReceiver = new IIntentReceiver.Stub() { |
| 13864 | @Override |
| 13865 | public void performReceive(Intent intent, int resultCode, String data, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 13866 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13867 | finishUserStop(uss); |
| 13868 | } |
| 13869 | }; |
| 13870 | broadcastIntentLocked(null, null, intent, |
| 13871 | null, resultReceiver, 0, null, null, null, |
| 13872 | true, false, MY_PID, Process.SYSTEM_UID, userId); |
| 13873 | } finally { |
| 13874 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 13875 | } |
| 13876 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13877 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 13878 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13879 | return ActivityManager.USER_OP_SUCCESS; |
| 13880 | } |
| 13881 | |
| 13882 | void finishUserStop(UserStartedState uss) { |
| 13883 | final int userId = uss.mHandle.getIdentifier(); |
| 13884 | boolean stopped; |
| 13885 | ArrayList<IStopUserCallback> callbacks; |
| 13886 | synchronized (this) { |
| 13887 | callbacks = new ArrayList<IStopUserCallback>(uss.mStopCallbacks); |
| 13888 | if (uss.mState != UserStartedState.STATE_STOPPING |
| 13889 | || mStartedUsers.get(userId) != uss) { |
| 13890 | stopped = false; |
| 13891 | } else { |
| 13892 | stopped = true; |
| 13893 | // User can no longer run. |
| 13894 | mStartedUsers.remove(userId); |
| 13895 | |
| 13896 | // Clean up all state and processes associated with the user. |
| 13897 | // Kill all the processes for the user. |
| 13898 | forceStopUserLocked(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 13899 | } |
| 13900 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 13901 | |
| 13902 | for (int i=0; i<callbacks.size(); i++) { |
| 13903 | try { |
| 13904 | if (stopped) callbacks.get(i).userStopped(userId); |
| 13905 | else callbacks.get(i).userStopAborted(userId); |
| 13906 | } catch (RemoteException e) { |
| 13907 | } |
| 13908 | } |
| 13909 | } |
| 13910 | |
| 13911 | @Override |
| 13912 | public UserInfo getCurrentUser() { |
| 13913 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 13914 | != PackageManager.PERMISSION_GRANTED) { |
| 13915 | String msg = "Permission Denial: getCurrentUser() from pid=" |
| 13916 | + Binder.getCallingPid() |
| 13917 | + ", uid=" + Binder.getCallingUid() |
| 13918 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 13919 | Slog.w(TAG, msg); |
| 13920 | throw new SecurityException(msg); |
| 13921 | } |
| 13922 | synchronized (this) { |
| 13923 | return getUserManager().getUserInfo(mCurrentUserId); |
| 13924 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 13925 | } |
| 13926 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13927 | private boolean userExists(int userId) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 13928 | UserInfo user = getUserManager().getUserInfo(userId); |
| 13929 | return user != null; |
| 13930 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13931 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 13932 | UserManager getUserManager() { |
| 13933 | if (mUserManager == null) { |
| 13934 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 13935 | } |
| 13936 | return mUserManager; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13937 | } |
| 13938 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 13939 | private void checkValidCaller(int uid, int userId) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 13940 | if (UserHandle.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) return; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 13941 | |
| 13942 | throw new SecurityException("Caller uid=" + uid |
| 13943 | + " is not privileged to communicate with user=" + userId); |
| 13944 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13945 | |
| 13946 | private int applyUserId(int uid, int userId) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 13947 | return UserHandle.getUid(userId, uid); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13948 | } |
| 13949 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 13950 | ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) { |
Amith Yamasani | 2f6c9eb | 2012-02-06 15:31:35 -0800 | [diff] [blame] | 13951 | if (info == null) return null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13952 | ApplicationInfo newInfo = new ApplicationInfo(info); |
| 13953 | newInfo.uid = applyUserId(info.uid, userId); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 13954 | newInfo.dataDir = USER_DATA_DIR + userId + "/" |
| 13955 | + info.packageName; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13956 | return newInfo; |
| 13957 | } |
| 13958 | |
| 13959 | ActivityInfo getActivityInfoForUser(ActivityInfo aInfo, int userId) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 13960 | if (aInfo == null |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 13961 | || (userId < 1 && aInfo.applicationInfo.uid < UserHandle.PER_USER_RANGE)) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 13962 | return aInfo; |
| 13963 | } |
| 13964 | |
| 13965 | ActivityInfo info = new ActivityInfo(aInfo); |
| 13966 | info.applicationInfo = getAppInfoForUser(info.applicationInfo, userId); |
| 13967 | return info; |
| 13968 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13969 | } |