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 | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 30 | import com.android.server.pm.UserManagerService; |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 31 | import com.android.server.wm.WindowManagerService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 33 | import dalvik.system.Zygote; |
| 34 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.app.Activity; |
| 36 | import android.app.ActivityManager; |
| 37 | import android.app.ActivityManagerNative; |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 38 | import android.app.ActivityOptions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.app.ActivityThread; |
| 40 | import android.app.AlertDialog; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 41 | import android.app.AppGlobals; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 42 | import android.app.ApplicationErrorReport; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.app.Dialog; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 44 | import android.app.IActivityController; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.app.IApplicationThread; |
| 46 | import android.app.IInstrumentationWatcher; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 47 | import android.app.INotificationManager; |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 48 | import android.app.IProcessObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.app.IServiceConnection; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 50 | import android.app.IStopUserCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.app.IThumbnailReceiver; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 52 | import android.app.IUserSwitchObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.app.Instrumentation; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 54 | import android.app.Notification; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 55 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.app.PendingIntent; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 57 | import android.app.backup.IBackupManager; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 58 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 59 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 60 | import android.content.ClipData; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 61 | import android.content.ComponentCallbacks2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | import android.content.ComponentName; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 63 | import android.content.ContentProvider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.content.ContentResolver; |
| 65 | import android.content.Context; |
Christian Mehlmauer | 7664e20 | 2010-07-20 08:46:17 +0200 | [diff] [blame] | 66 | import android.content.DialogInterface; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 67 | import android.content.IContentProvider; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 68 | import android.content.IIntentReceiver; |
| 69 | import android.content.IIntentSender; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 70 | import android.content.Intent; |
| 71 | import android.content.IntentFilter; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 72 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.content.pm.ActivityInfo; |
| 74 | import android.content.pm.ApplicationInfo; |
| 75 | import android.content.pm.ConfigurationInfo; |
| 76 | import android.content.pm.IPackageDataObserver; |
| 77 | import android.content.pm.IPackageManager; |
| 78 | import android.content.pm.InstrumentationInfo; |
Dan Egnor | 66c40e7 | 2010-01-26 16:23:11 -0800 | [diff] [blame] | 79 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.content.pm.PackageManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 81 | import android.content.pm.UserInfo; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 82 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 83 | import android.content.pm.PathPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.content.pm.ProviderInfo; |
| 85 | import android.content.pm.ResolveInfo; |
| 86 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 87 | import android.content.res.CompatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | import android.content.res.Configuration; |
| 89 | import android.graphics.Bitmap; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 90 | import android.net.Proxy; |
| 91 | import android.net.ProxyProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.net.Uri; |
| 93 | import android.os.Binder; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 94 | import android.os.Build; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 95 | import android.os.Bundle; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 96 | import android.os.Debug; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 97 | import android.os.DropBoxManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | import android.os.Environment; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 99 | import android.os.FileObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import android.os.FileUtils; |
| 101 | import android.os.Handler; |
| 102 | import android.os.IBinder; |
| 103 | import android.os.IPermissionController; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 104 | import android.os.IRemoteCallback; |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 105 | import android.os.IUserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | import android.os.Looper; |
| 107 | import android.os.Message; |
| 108 | import android.os.Parcel; |
| 109 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import android.os.Process; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 111 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | import android.os.RemoteException; |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 113 | import android.os.SELinux; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | import android.os.ServiceManager; |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 115 | import android.os.StrictMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | import android.os.SystemClock; |
| 117 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 118 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | import android.provider.Settings; |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 120 | import android.text.format.Time; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | import android.util.EventLog; |
Joe Onorato | 5d3bea6 | 2010-03-01 13:44:29 -0800 | [diff] [blame] | 122 | import android.util.Log; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 123 | import android.util.Pair; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | import android.util.PrintWriterPrinter; |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 125 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | import android.util.SparseArray; |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 127 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | import android.view.Gravity; |
| 129 | import android.view.LayoutInflater; |
| 130 | import android.view.View; |
| 131 | import android.view.WindowManager; |
| 132 | import android.view.WindowManagerPolicy; |
| 133 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 134 | import java.io.BufferedInputStream; |
| 135 | import java.io.BufferedOutputStream; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 136 | import java.io.BufferedReader; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 137 | import java.io.DataInputStream; |
| 138 | import java.io.DataOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | import java.io.File; |
| 140 | import java.io.FileDescriptor; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 141 | import java.io.FileInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | import java.io.FileNotFoundException; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 143 | import java.io.FileOutputStream; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 144 | import java.io.IOException; |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 145 | import java.io.InputStreamReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | import java.io.PrintWriter; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 147 | import java.io.StringWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | import java.lang.ref.WeakReference; |
| 149 | import java.util.ArrayList; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 150 | import java.util.Arrays; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 151 | import java.util.Collections; |
| 152 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | import java.util.HashMap; |
| 154 | import java.util.HashSet; |
| 155 | import java.util.Iterator; |
| 156 | import java.util.List; |
| 157 | import java.util.Locale; |
| 158 | import java.util.Map; |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 159 | import java.util.Set; |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 160 | import java.util.concurrent.atomic.AtomicBoolean; |
| 161 | import java.util.concurrent.atomic.AtomicLong; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 163 | public final class ActivityManagerService extends ActivityManagerNative |
| 164 | implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 165 | private static final String USER_DATA_DIR = "/data/user/"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | static final String TAG = "ActivityManager"; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 167 | static final String TAG_MU = "ActivityManagerServiceMU"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | static final boolean DEBUG = false; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 169 | static final boolean localLOGV = DEBUG; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | static final boolean DEBUG_SWITCH = localLOGV || false; |
| 171 | static final boolean DEBUG_TASKS = localLOGV || false; |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 172 | static final boolean DEBUG_THUMBNAILS = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | static final boolean DEBUG_PAUSE = localLOGV || false; |
| 174 | static final boolean DEBUG_OOM_ADJ = localLOGV || false; |
| 175 | static final boolean DEBUG_TRANSITION = localLOGV || false; |
| 176 | static final boolean DEBUG_BROADCAST = localLOGV || false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 177 | static final boolean DEBUG_BACKGROUND_BROADCAST = DEBUG_BROADCAST || false; |
Dianne Hackborn | 82f3f00 | 2009-06-16 18:49:05 -0700 | [diff] [blame] | 178 | static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | static final boolean DEBUG_SERVICE = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 180 | static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | static final boolean DEBUG_VISBILITY = localLOGV || false; |
| 182 | static final boolean DEBUG_PROCESSES = localLOGV || false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 183 | static final boolean DEBUG_PROCESS_OBSERVERS = localLOGV || false; |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 184 | static final boolean DEBUG_PROVIDER = localLOGV || false; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 185 | static final boolean DEBUG_URI_PERMISSION = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | static final boolean DEBUG_USER_LEAVING = localLOGV || false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 187 | static final boolean DEBUG_RESULTS = localLOGV || false; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 188 | static final boolean DEBUG_BACKUP = localLOGV || false; |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 189 | static final boolean DEBUG_CONFIGURATION = localLOGV || false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 190 | static final boolean DEBUG_POWER = localLOGV || false; |
| 191 | static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 192 | static final boolean DEBUG_MU = localLOGV || false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | static final boolean VALIDATE_TOKENS = false; |
| 194 | static final boolean SHOW_ACTIVITY_START_TIME = true; |
| 195 | |
| 196 | // Control over CPU and battery monitoring. |
| 197 | static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes. |
| 198 | static final boolean MONITOR_CPU_USAGE = true; |
| 199 | static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds. |
| 200 | static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample. |
| 201 | static final boolean MONITOR_THREAD_CPU_USAGE = false; |
| 202 | |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 203 | // 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] | 204 | static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 205 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 206 | private static final String SYSTEM_DEBUGGABLE = "ro.debuggable"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 208 | static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); |
| 209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | // Maximum number of recent tasks that we can remember. |
| 211 | static final int MAX_RECENT_TASKS = 20; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 212 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 213 | // Amount of time after a call to stopAppSwitches() during which we will |
| 214 | // prevent further untrusted switches from happening. |
| 215 | static final long APP_SWITCH_DELAY_TIME = 5*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | |
| 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. |
| 219 | static final int PROC_START_TIMEOUT = 10*1000; |
| 220 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 221 | // How long we wait for a launched process to attach to the activity manager |
| 222 | // before we decide it's never going to come up for real, when the process was |
| 223 | // started with a wrapper for instrumentation (such as Valgrind) because it |
| 224 | // could take much longer than usual. |
| 225 | static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000; |
| 226 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | // How long to wait after going idle before forcing apps to GC. |
| 228 | static final int GC_TIMEOUT = 5*1000; |
| 229 | |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 230 | // The minimum amount of time between successive GC requests for a process. |
| 231 | static final int GC_MIN_INTERVAL = 60*1000; |
| 232 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 233 | // The rate at which we check for apps using excessive power -- 15 mins. |
| 234 | static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000; |
| 235 | |
| 236 | // The minimum sample duration we will allow before deciding we have |
| 237 | // enough data on wake locks to start killing things. |
| 238 | static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000; |
| 239 | |
| 240 | // The minimum sample duration we will allow before deciding we have |
| 241 | // enough data on CPU usage to start killing things. |
| 242 | 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] | 243 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | // 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] | 245 | static final int BROADCAST_FG_TIMEOUT = 10*1000; |
| 246 | static final int BROADCAST_BG_TIMEOUT = 60*1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | // How long we wait until we timeout on key dispatching. |
| 249 | static final int KEY_DISPATCHING_TIMEOUT = 5*1000; |
| 250 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | // How long we wait until we timeout on key dispatching during instrumentation. |
| 252 | static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000; |
| 253 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 254 | // Amount of time we wait for observers to handle a user switch before |
| 255 | // giving up on them and unfreezing the screen. |
| 256 | static final int USER_SWITCH_TIMEOUT = 2*1000; |
| 257 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 258 | // Maximum number of users we allow to be running at a time. |
| 259 | static final int MAX_RUNNING_USERS = 3; |
| 260 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 261 | static final int MY_PID = Process.myPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | |
| 263 | static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 264 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 265 | public ActivityStack mMainStack; |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 266 | |
| 267 | private final boolean mHeadless; |
| 268 | |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 269 | // Whether we should show our dialogs (ANR, crash, etc) or just perform their |
| 270 | // default actuion automatically. Important for devices without direct input |
| 271 | // devices. |
| 272 | private boolean mShowDialogs = true; |
| 273 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 275 | * Description of a request to start a new activity, which has been held |
| 276 | * due to app switches being disabled. |
| 277 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 278 | static class PendingActivityLaunch { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 279 | ActivityRecord r; |
| 280 | ActivityRecord sourceRecord; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 281 | int startFlags; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | final ArrayList<PendingActivityLaunch> mPendingActivityLaunches |
| 285 | = new ArrayList<PendingActivityLaunch>(); |
| 286 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 288 | BroadcastQueue mFgBroadcastQueue; |
| 289 | BroadcastQueue mBgBroadcastQueue; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 290 | // Convenient for easy iteration over the queues. Foreground is first |
| 291 | // so that dispatch of foreground broadcasts gets precedence. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 292 | final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2]; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 293 | |
| 294 | BroadcastQueue broadcastQueueForIntent(Intent intent) { |
| 295 | final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0; |
| 296 | if (DEBUG_BACKGROUND_BROADCAST) { |
| 297 | Slog.i(TAG, "Broadcast intent " + intent + " on " |
| 298 | + (isFg ? "foreground" : "background") |
| 299 | + " queue"); |
| 300 | } |
| 301 | return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue; |
| 302 | } |
| 303 | |
| 304 | BroadcastRecord broadcastRecordForReceiverLocked(IBinder receiver) { |
| 305 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 306 | BroadcastRecord r = queue.getMatchingOrderedReceiver(receiver); |
| 307 | if (r != null) { |
| 308 | return r; |
| 309 | } |
| 310 | } |
| 311 | return null; |
| 312 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | |
| 314 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | * Activity we have told the window manager to have key focus. |
| 316 | */ |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 317 | ActivityRecord mFocusedActivity = null; |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 318 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | * List of intents that were used to start the most recent tasks. |
| 320 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 321 | final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | |
| 323 | /** |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 324 | * Process management. |
| 325 | */ |
| 326 | final ProcessList mProcessList = new ProcessList(); |
| 327 | |
| 328 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | * All of the applications we currently have running organized by name. |
| 330 | * The keys are strings of the application package name (as |
| 331 | * returned by the package manager), and the keys are ApplicationRecord |
| 332 | * objects. |
| 333 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 334 | final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | |
| 336 | /** |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 337 | * The currently running isolated processes. |
| 338 | */ |
| 339 | final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>(); |
| 340 | |
| 341 | /** |
| 342 | * Counter for assigning isolated process uids, to avoid frequently reusing the |
| 343 | * same ones. |
| 344 | */ |
| 345 | int mNextIsolatedProcessUid = 0; |
| 346 | |
| 347 | /** |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 348 | * The currently running heavy-weight process, if any. |
| 349 | */ |
| 350 | ProcessRecord mHeavyWeightProcess = null; |
| 351 | |
| 352 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | * The last time that various processes have crashed. |
| 354 | */ |
| 355 | final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>(); |
| 356 | |
| 357 | /** |
| 358 | * Set of applications that we consider to be bad, and will reject |
| 359 | * incoming broadcasts from (which the user has no control over). |
| 360 | * Processes are added to this set when they have crashed twice within |
| 361 | * a minimum amount of time; they are removed from it when they are |
| 362 | * later restarted (hopefully due to some user action). The value is the |
| 363 | * time it was added to the list. |
| 364 | */ |
| 365 | final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>(); |
| 366 | |
| 367 | /** |
| 368 | * All of the processes we currently have running organized by pid. |
| 369 | * The keys are the pid running the application. |
| 370 | * |
| 371 | * <p>NOTE: This object is protected by its own lock, NOT the global |
| 372 | * activity manager lock! |
| 373 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 374 | final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | |
| 376 | /** |
| 377 | * All of the processes that have been forced to be foreground. The key |
| 378 | * is the pid of the caller who requested it (we hold a death |
| 379 | * link on it). |
| 380 | */ |
| 381 | abstract class ForegroundToken implements IBinder.DeathRecipient { |
| 382 | int pid; |
| 383 | IBinder token; |
| 384 | } |
| 385 | final SparseArray<ForegroundToken> mForegroundProcesses |
| 386 | = new SparseArray<ForegroundToken>(); |
| 387 | |
| 388 | /** |
| 389 | * List of records for processes that someone had tried to start before the |
| 390 | * system was ready. We don't start them at that point, but ensure they |
| 391 | * are started by the time booting is complete. |
| 392 | */ |
| 393 | final ArrayList<ProcessRecord> mProcessesOnHold |
| 394 | = new ArrayList<ProcessRecord>(); |
| 395 | |
| 396 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | * List of persistent applications that are in the process |
| 398 | * of being started. |
| 399 | */ |
| 400 | final ArrayList<ProcessRecord> mPersistentStartingProcesses |
| 401 | = new ArrayList<ProcessRecord>(); |
| 402 | |
| 403 | /** |
| 404 | * Processes that are being forcibly torn down. |
| 405 | */ |
| 406 | final ArrayList<ProcessRecord> mRemovedProcesses |
| 407 | = new ArrayList<ProcessRecord>(); |
| 408 | |
| 409 | /** |
| 410 | * List of running applications, sorted by recent usage. |
| 411 | * The first entry in the list is the least recently used. |
| 412 | * It contains ApplicationRecord objects. This list does NOT include |
| 413 | * any persistent application records (since we never want to exit them). |
| 414 | */ |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 415 | final ArrayList<ProcessRecord> mLruProcesses |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | = new ArrayList<ProcessRecord>(); |
| 417 | |
| 418 | /** |
| 419 | * List of processes that should gc as soon as things are idle. |
| 420 | */ |
| 421 | final ArrayList<ProcessRecord> mProcessesToGc |
| 422 | = new ArrayList<ProcessRecord>(); |
| 423 | |
| 424 | /** |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 425 | * This is the process holding what we currently consider to be |
| 426 | * the "home" activity. |
| 427 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 428 | ProcessRecord mHomeProcess; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 429 | |
| 430 | /** |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 431 | * This is the process holding the activity the user last visited that |
| 432 | * is in a different process from the one they are currently in. |
| 433 | */ |
| 434 | ProcessRecord mPreviousProcess; |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 435 | |
| 436 | /** |
| 437 | * The time at which the previous process was last visible. |
| 438 | */ |
| 439 | long mPreviousProcessVisibleTime; |
| 440 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 441 | /** |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 442 | * Which uses have been started, so are allowed to run code. |
| 443 | */ |
| 444 | final SparseArray<UserStartedState> mStartedUsers = new SparseArray<UserStartedState>(); |
| 445 | |
| 446 | /** |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 447 | * LRU list of history of current users. Most recently current is at the end. |
| 448 | */ |
| 449 | final ArrayList<Integer> mUserLru = new ArrayList<Integer>(); |
| 450 | |
| 451 | /** |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 452 | * Constant array of the users that are currently started. |
| 453 | */ |
| 454 | int[] mStartedUserArray = new int[] { 0 }; |
| 455 | |
| 456 | /** |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 457 | * Registered observers of the user switching mechanics. |
| 458 | */ |
| 459 | final RemoteCallbackList<IUserSwitchObserver> mUserSwitchObservers |
| 460 | = new RemoteCallbackList<IUserSwitchObserver>(); |
| 461 | |
| 462 | /** |
| 463 | * Currently active user switch. |
| 464 | */ |
| 465 | Object mCurUserSwitchCallback; |
| 466 | |
| 467 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 468 | * Packages that the user has asked to have run in screen size |
| 469 | * compatibility mode instead of filling the screen. |
| 470 | */ |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 471 | final CompatModePackages mCompatModePackages; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 472 | |
| 473 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | * Set of PendingResultRecord objects that are currently active. |
| 475 | */ |
| 476 | final HashSet mPendingResultRecords = new HashSet(); |
| 477 | |
| 478 | /** |
| 479 | * Set of IntentSenderRecord objects that are currently active. |
| 480 | */ |
| 481 | final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords |
| 482 | = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>(); |
| 483 | |
| 484 | /** |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 485 | * Fingerprints (hashCode()) of stack traces that we've |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 486 | * already logged DropBox entries for. Guarded by itself. If |
| 487 | * something (rogue user app) forces this over |
| 488 | * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared. |
| 489 | */ |
| 490 | private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>(); |
| 491 | private static final int MAX_DUP_SUPPRESSED_STACKS = 5000; |
| 492 | |
| 493 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 494 | * Strict Mode background batched logging state. |
| 495 | * |
| 496 | * The string buffer is guarded by itself, and its lock is also |
| 497 | * used to determine if another batched write is already |
| 498 | * in-flight. |
| 499 | */ |
| 500 | private final StringBuilder mStrictModeBuffer = new StringBuilder(); |
| 501 | |
| 502 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | * Keeps track of all IIntentReceivers that have been registered for |
| 504 | * broadcasts. Hash keys are the receiver IBinder, hash value is |
| 505 | * a ReceiverList. |
| 506 | */ |
| 507 | final HashMap mRegisteredReceivers = new HashMap(); |
| 508 | |
| 509 | /** |
| 510 | * Resolver for broadcast intents to registered receivers. |
| 511 | * Holds BroadcastFilter (subclass of IntentFilter). |
| 512 | */ |
| 513 | final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver |
| 514 | = new IntentResolver<BroadcastFilter, BroadcastFilter>() { |
| 515 | @Override |
| 516 | protected boolean allowFilterResult( |
| 517 | BroadcastFilter filter, List<BroadcastFilter> dest) { |
| 518 | IBinder target = filter.receiverList.receiver.asBinder(); |
| 519 | for (int i=dest.size()-1; i>=0; i--) { |
| 520 | if (dest.get(i).receiverList.receiver.asBinder() == target) { |
| 521 | return false; |
| 522 | } |
| 523 | } |
| 524 | return true; |
| 525 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 526 | |
| 527 | @Override |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 528 | protected BroadcastFilter newResult(BroadcastFilter filter, int match, int userId) { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 529 | if (userId == UserHandle.USER_ALL || filter.owningUserId == UserHandle.USER_ALL |
| 530 | || userId == filter.owningUserId) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 531 | return super.newResult(filter, match, userId); |
| 532 | } |
| 533 | return null; |
| 534 | } |
| 535 | |
| 536 | @Override |
Dianne Hackborn | 9ec6cdd | 2012-05-31 10:57:54 -0700 | [diff] [blame] | 537 | protected BroadcastFilter[] newArray(int size) { |
| 538 | return new BroadcastFilter[size]; |
| 539 | } |
| 540 | |
| 541 | @Override |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 542 | protected String packageForFilter(BroadcastFilter filter) { |
| 543 | return filter.packageName; |
| 544 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | }; |
| 546 | |
| 547 | /** |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 548 | * 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] | 549 | * sticky Intent, values are an ArrayList of all broadcasted intents with |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 550 | * that action (which should usually be one). The SparseArray is keyed |
| 551 | * by the user ID the sticky is for, and can include UserHandle.USER_ALL |
| 552 | * for stickies that are sent to all users. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | */ |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 554 | final SparseArray<HashMap<String, ArrayList<Intent>>> mStickyBroadcasts = |
| 555 | new SparseArray<HashMap<String, ArrayList<Intent>>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 557 | final ActiveServices mServices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | |
| 559 | /** |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 560 | * Backup/restore process management |
| 561 | */ |
| 562 | String mBackupAppName = null; |
| 563 | BackupRecord mBackupTarget = null; |
| 564 | |
| 565 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | * List of PendingThumbnailsRecord objects of clients who are still |
| 567 | * waiting to receive all of the thumbnails for a task. |
| 568 | */ |
| 569 | final ArrayList mPendingThumbnails = new ArrayList(); |
| 570 | |
| 571 | /** |
| 572 | * List of HistoryRecord objects that have been finished and must |
| 573 | * still report back to a pending thumbnail receiver. |
| 574 | */ |
| 575 | final ArrayList mCancelledThumbnails = new ArrayList(); |
| 576 | |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 577 | final ProviderMap mProviderMap; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 578 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | /** |
| 580 | * List of content providers who have clients waiting for them. The |
| 581 | * application is currently being launched and the provider will be |
| 582 | * removed from this list once it is published. |
| 583 | */ |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 584 | final ArrayList<ContentProviderRecord> mLaunchingProviders |
| 585 | = new ArrayList<ContentProviderRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | |
| 587 | /** |
| 588 | * Global set of specific Uri permissions that have been granted. |
| 589 | */ |
| 590 | final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions |
| 591 | = new SparseArray<HashMap<Uri, UriPermission>>(); |
| 592 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 593 | CoreSettingsObserver mCoreSettingsObserver; |
| 594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | /** |
| 596 | * Thread-local storage used to carry caller permissions over through |
| 597 | * indirect content-provider access. |
| 598 | * @see #ActivityManagerService.openContentUri() |
| 599 | */ |
| 600 | private class Identity { |
| 601 | public int pid; |
| 602 | public int uid; |
| 603 | |
| 604 | Identity(int _pid, int _uid) { |
| 605 | pid = _pid; |
| 606 | uid = _uid; |
| 607 | } |
| 608 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 609 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>(); |
| 611 | |
| 612 | /** |
| 613 | * All information we have collected about the runtime performance of |
| 614 | * any user id that can impact battery performance. |
| 615 | */ |
| 616 | final BatteryStatsService mBatteryStatsService; |
| 617 | |
| 618 | /** |
| 619 | * information about component usage |
| 620 | */ |
| 621 | final UsageStatsService mUsageStatsService; |
| 622 | |
| 623 | /** |
| 624 | * Current configuration information. HistoryRecord objects are given |
| 625 | * a reference to this object to indicate which configuration they are |
| 626 | * currently running in, so this object must be kept immutable. |
| 627 | */ |
| 628 | Configuration mConfiguration = new Configuration(); |
| 629 | |
| 630 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 631 | * Current sequencing integer of the configuration, for skipping old |
| 632 | * configurations. |
| 633 | */ |
| 634 | int mConfigurationSeq = 0; |
| 635 | |
| 636 | /** |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 637 | * Hardware-reported OpenGLES version. |
| 638 | */ |
| 639 | final int GL_ES_VERSION; |
| 640 | |
| 641 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 642 | * List of initialization arguments to pass to all processes when binding applications to them. |
| 643 | * For example, references to the commonly used services. |
| 644 | */ |
| 645 | HashMap<String, IBinder> mAppBindArgs; |
| 646 | |
| 647 | /** |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 648 | * Temporary to avoid allocations. Protected by main lock. |
| 649 | */ |
| 650 | final StringBuilder mStringBuilder = new StringBuilder(256); |
| 651 | |
| 652 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | * Used to control how we initialize the service. |
| 654 | */ |
| 655 | boolean mStartRunning = false; |
| 656 | ComponentName mTopComponent; |
| 657 | String mTopAction; |
| 658 | String mTopData; |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 659 | boolean mProcessesReady = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | boolean mSystemReady = false; |
| 661 | boolean mBooting = false; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 662 | boolean mWaitingUpdate = false; |
| 663 | boolean mDidUpdate = false; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 664 | boolean mOnBattery = false; |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 665 | boolean mLaunchWarningShown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | |
| 667 | Context mContext; |
| 668 | |
| 669 | int mFactoryTest; |
| 670 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 671 | boolean mCheckedForSetup; |
| 672 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | /** |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 674 | * The time at which we will allow normal application switches again, |
| 675 | * after a call to {@link #stopAppSwitches()}. |
| 676 | */ |
| 677 | long mAppSwitchesAllowedTime; |
| 678 | |
| 679 | /** |
| 680 | * This is set to true after the first switch after mAppSwitchesAllowedTime |
| 681 | * is set; any switches after that will clear the time. |
| 682 | */ |
| 683 | boolean mDidAppSwitch; |
| 684 | |
| 685 | /** |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 686 | * Last time (in realtime) at which we checked for power usage. |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 687 | */ |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 688 | long mLastPowerCheckRealtime; |
| 689 | |
| 690 | /** |
| 691 | * Last time (in uptime) at which we checked for power usage. |
| 692 | */ |
| 693 | long mLastPowerCheckUptime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 694 | |
| 695 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | * Set while we are wanting to sleep, to prevent any |
| 697 | * activities from being started/resumed. |
| 698 | */ |
| 699 | boolean mSleeping = false; |
| 700 | |
| 701 | /** |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 702 | * State of external calls telling us if the device is asleep. |
| 703 | */ |
| 704 | boolean mWentToSleep = false; |
| 705 | |
| 706 | /** |
| 707 | * State of external call telling us if the lock screen is shown. |
| 708 | */ |
| 709 | boolean mLockScreenShown = false; |
| 710 | |
| 711 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 712 | * Set if we are shutting down the system, similar to sleeping. |
| 713 | */ |
| 714 | boolean mShuttingDown = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | |
| 716 | /** |
| 717 | * Task identifier that activities are currently being started |
| 718 | * in. Incremented each time a new task is created. |
| 719 | * todo: Replace this with a TokenSpace class that generates non-repeating |
| 720 | * integers that won't wrap. |
| 721 | */ |
| 722 | int mCurTask = 1; |
| 723 | |
| 724 | /** |
| 725 | * Current sequence id for oom_adj computation traversal. |
| 726 | */ |
| 727 | int mAdjSeq = 0; |
| 728 | |
| 729 | /** |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 730 | * Current sequence id for process LRU updating. |
| 731 | */ |
| 732 | int mLruSeq = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | |
| 734 | /** |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 735 | * Keep track of the non-hidden/empty process we last found, to help |
| 736 | * determine how to distribute hidden/empty processes next time. |
| 737 | */ |
| 738 | int mNumNonHiddenProcs = 0; |
| 739 | |
| 740 | /** |
| 741 | * Keep track of the number of hidden procs, to balance oom adj |
| 742 | * distribution between those and empty procs. |
| 743 | */ |
| 744 | int mNumHiddenProcs = 0; |
| 745 | |
| 746 | /** |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 747 | * Keep track of the number of service processes we last found, to |
| 748 | * determine on the next iteration which should be B services. |
| 749 | */ |
| 750 | int mNumServiceProcs = 0; |
| 751 | int mNewNumServiceProcs = 0; |
| 752 | |
| 753 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | * System monitoring: number of processes that died since the last |
| 755 | * N procs were started. |
| 756 | */ |
| 757 | int[] mProcDeaths = new int[20]; |
| 758 | |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 759 | /** |
| 760 | * This is set if we had to do a delayed dexopt of an app before launching |
| 761 | * it, to increasing the ANR timeouts in that case. |
| 762 | */ |
| 763 | boolean mDidDexOpt; |
| 764 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | String mDebugApp = null; |
| 766 | boolean mWaitForDebugger = false; |
| 767 | boolean mDebugTransient = false; |
| 768 | String mOrigDebugApp = null; |
| 769 | boolean mOrigWaitForDebugger = false; |
| 770 | boolean mAlwaysFinishActivities = false; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 771 | IActivityController mController = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 772 | String mProfileApp = null; |
| 773 | ProcessRecord mProfileProc = null; |
| 774 | String mProfileFile; |
| 775 | ParcelFileDescriptor mProfileFd; |
| 776 | int mProfileType = 0; |
| 777 | boolean mAutoStopProfiler = false; |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 778 | String mOpenGlTraceApp = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 780 | static class ProcessChangeItem { |
| 781 | static final int CHANGE_ACTIVITIES = 1<<0; |
| 782 | static final int CHANGE_IMPORTANCE= 1<<1; |
| 783 | int changes; |
| 784 | int uid; |
| 785 | int pid; |
| 786 | int importance; |
| 787 | boolean foregroundActivities; |
| 788 | } |
| 789 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 790 | final RemoteCallbackList<IProcessObserver> mProcessObservers |
| 791 | = new RemoteCallbackList<IProcessObserver>(); |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 792 | ProcessChangeItem[] mActiveProcessChanges = new ProcessChangeItem[5]; |
| 793 | |
| 794 | final ArrayList<ProcessChangeItem> mPendingProcessChanges |
| 795 | = new ArrayList<ProcessChangeItem>(); |
| 796 | final ArrayList<ProcessChangeItem> mAvailProcessChanges |
| 797 | = new ArrayList<ProcessChangeItem>(); |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 798 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 799 | /** |
| 800 | * Callback of last caller to {@link #requestPss}. |
| 801 | */ |
| 802 | Runnable mRequestPssCallback; |
| 803 | |
| 804 | /** |
| 805 | * Remaining processes for which we are waiting results from the last |
| 806 | * call to {@link #requestPss}. |
| 807 | */ |
| 808 | final ArrayList<ProcessRecord> mRequestPssList |
| 809 | = new ArrayList<ProcessRecord>(); |
| 810 | |
| 811 | /** |
| 812 | * Runtime statistics collection thread. This object's lock is used to |
| 813 | * protect all related state. |
| 814 | */ |
| 815 | final Thread mProcessStatsThread; |
| 816 | |
| 817 | /** |
| 818 | * Used to collect process stats when showing not responding dialog. |
| 819 | * Protected by mProcessStatsThread. |
| 820 | */ |
| 821 | final ProcessStats mProcessStats = new ProcessStats( |
| 822 | MONITOR_THREAD_CPU_USAGE); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 823 | final AtomicLong mLastCpuTime = new AtomicLong(0); |
| 824 | final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true); |
| 825 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | long mLastWriteTime = 0; |
| 827 | |
| 828 | /** |
| 829 | * Set to true after the system has finished booting. |
| 830 | */ |
| 831 | boolean mBooted = false; |
| 832 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 833 | int mProcessLimit = ProcessList.MAX_HIDDEN_APPS; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 834 | int mProcessLimitOverride = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | |
| 836 | WindowManagerService mWindowManager; |
| 837 | |
| 838 | static ActivityManagerService mSelf; |
| 839 | static ActivityThread mSystemThread; |
| 840 | |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 841 | private int mCurrentUserId = 0; |
| 842 | private int[] mCurrentUserArray = new int[] { 0 }; |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 843 | private UserManagerService mUserManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 844 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 845 | private final class AppDeathRecipient implements IBinder.DeathRecipient { |
| 846 | final ProcessRecord mApp; |
| 847 | final int mPid; |
| 848 | final IApplicationThread mAppThread; |
| 849 | |
| 850 | AppDeathRecipient(ProcessRecord app, int pid, |
| 851 | IApplicationThread thread) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 852 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | TAG, "New death recipient " + this |
| 854 | + " for thread " + thread.asBinder()); |
| 855 | mApp = app; |
| 856 | mPid = pid; |
| 857 | mAppThread = thread; |
| 858 | } |
| 859 | |
| 860 | public void binderDied() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 861 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | TAG, "Death received in " + this |
| 863 | + " for thread " + mAppThread.asBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | synchronized(ActivityManagerService.this) { |
| 865 | appDiedLocked(mApp, mPid, mAppThread); |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | static final int SHOW_ERROR_MSG = 1; |
| 871 | static final int SHOW_NOT_RESPONDING_MSG = 2; |
| 872 | static final int SHOW_FACTORY_ERROR_MSG = 3; |
| 873 | static final int UPDATE_CONFIGURATION_MSG = 4; |
| 874 | static final int GC_BACKGROUND_PROCESSES_MSG = 5; |
| 875 | static final int WAIT_FOR_DEBUGGER_MSG = 6; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | static final int SERVICE_TIMEOUT_MSG = 12; |
| 877 | static final int UPDATE_TIME_ZONE = 13; |
| 878 | static final int SHOW_UID_ERROR_MSG = 14; |
| 879 | static final int IM_FEELING_LUCKY_MSG = 15; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | static final int PROC_START_TIMEOUT_MSG = 20; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 881 | static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 882 | static final int KILL_APPLICATION_MSG = 22; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 883 | static final int FINALIZE_PENDING_INTENT_MSG = 23; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 884 | static final int POST_HEAVY_NOTIFICATION_MSG = 24; |
| 885 | static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 886 | static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 887 | static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 888 | static final int CLEAR_DNS_CACHE = 28; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 889 | static final int UPDATE_HTTP_PROXY = 29; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 890 | static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 891 | static final int DISPATCH_PROCESSES_CHANGED = 31; |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 892 | static final int DISPATCH_PROCESS_DIED = 32; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 893 | static final int REPORT_MEM_USAGE = 33; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 894 | static final int REPORT_USER_SWITCH_MSG = 34; |
| 895 | static final int CONTINUE_USER_SWITCH_MSG = 35; |
| 896 | static final int USER_SWITCH_TIMEOUT_MSG = 36; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 898 | static final int FIRST_ACTIVITY_STACK_MSG = 100; |
| 899 | static final int FIRST_BROADCAST_QUEUE_MSG = 200; |
| 900 | static final int FIRST_COMPAT_MODE_MSG = 300; |
| 901 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | AlertDialog mUidAlert; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 903 | CompatModeDialog mCompatModeDialog; |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 904 | long mLastMemUsageReportTime = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | |
| 906 | final Handler mHandler = new Handler() { |
| 907 | //public Handler() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 908 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | //} |
| 910 | |
| 911 | public void handleMessage(Message msg) { |
| 912 | switch (msg.what) { |
| 913 | case SHOW_ERROR_MSG: { |
| 914 | HashMap data = (HashMap) msg.obj; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 915 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 916 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | synchronized (ActivityManagerService.this) { |
| 918 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 919 | AppErrorResult res = (AppErrorResult) data.get("result"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 | if (proc != null && proc.crashDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 921 | Slog.e(TAG, "App already has crash dialog: " + proc); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 922 | if (res != null) { |
| 923 | res.set(0); |
| 924 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | return; |
| 926 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 927 | if (!showBackground && UserHandle.getAppId(proc.uid) |
| 928 | >= Process.FIRST_APPLICATION_UID && proc.userId != mCurrentUserId |
| 929 | && proc.pid != MY_PID) { |
| 930 | Slog.w(TAG, "Skipping crash dialog of " + proc + ": background"); |
| 931 | if (res != null) { |
| 932 | res.set(0); |
| 933 | } |
| 934 | return; |
| 935 | } |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 936 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 937 | Dialog d = new AppErrorDialog(mContext, |
| 938 | ActivityManagerService.this, res, proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | d.show(); |
| 940 | proc.crashDialog = d; |
| 941 | } else { |
| 942 | // The device is asleep, so just pretend that the user |
| 943 | // saw a crash dialog and hit "force quit". |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 944 | if (res != null) { |
| 945 | res.set(0); |
| 946 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | } |
| 948 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 949 | |
| 950 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | } break; |
| 952 | case SHOW_NOT_RESPONDING_MSG: { |
| 953 | synchronized (ActivityManagerService.this) { |
| 954 | HashMap data = (HashMap) msg.obj; |
| 955 | ProcessRecord proc = (ProcessRecord)data.get("app"); |
| 956 | if (proc != null && proc.anrDialog != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 957 | Slog.e(TAG, "App already has anr dialog: " + proc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | return; |
| 959 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 960 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 961 | Intent intent = new Intent("android.intent.action.ANR"); |
| 962 | if (!mProcessesReady) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 963 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 964 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 965 | } |
| 966 | broadcastIntentLocked(null, null, intent, |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 967 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 968 | 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] | 969 | |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 970 | if (mShowDialogs) { |
| 971 | Dialog d = new AppNotRespondingDialog(ActivityManagerService.this, |
Mike Lockwood | 69ccdbd | 2012-04-03 11:53:47 -0700 | [diff] [blame] | 972 | mContext, proc, (ActivityRecord)data.get("activity")); |
Justin Koh | bc52ca2 | 2012-03-29 15:11:44 -0700 | [diff] [blame] | 973 | d.show(); |
| 974 | proc.anrDialog = d; |
| 975 | } else { |
| 976 | // Just kill the app if there is no dialog to be shown. |
| 977 | killAppAtUsersRequest(proc, null); |
| 978 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 980 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 981 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | } break; |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 983 | case SHOW_STRICT_MODE_VIOLATION_MSG: { |
| 984 | HashMap<String, Object> data = (HashMap<String, Object>) msg.obj; |
| 985 | synchronized (ActivityManagerService.this) { |
| 986 | ProcessRecord proc = (ProcessRecord) data.get("app"); |
| 987 | if (proc == null) { |
| 988 | Slog.e(TAG, "App not found when showing strict mode dialog."); |
| 989 | break; |
| 990 | } |
| 991 | if (proc.crashDialog != null) { |
| 992 | Slog.e(TAG, "App already has strict mode dialog: " + proc); |
| 993 | return; |
| 994 | } |
| 995 | AppErrorResult res = (AppErrorResult) data.get("result"); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 996 | if (mShowDialogs && !mSleeping && !mShuttingDown) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 997 | Dialog d = new StrictModeViolationDialog(mContext, |
| 998 | ActivityManagerService.this, res, proc); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 999 | d.show(); |
| 1000 | proc.crashDialog = d; |
| 1001 | } else { |
| 1002 | // The device is asleep, so just pretend that the user |
| 1003 | // saw a crash dialog and hit "force quit". |
| 1004 | res.set(0); |
| 1005 | } |
| 1006 | } |
| 1007 | ensureBootCompleted(); |
| 1008 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | case SHOW_FACTORY_ERROR_MSG: { |
| 1010 | Dialog d = new FactoryErrorDialog( |
| 1011 | mContext, msg.getData().getCharSequence("msg")); |
| 1012 | d.show(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 1013 | ensureBootCompleted(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | } break; |
| 1015 | case UPDATE_CONFIGURATION_MSG: { |
| 1016 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1017 | Settings.System.putConfiguration(resolver, (Configuration)msg.obj); |
| 1018 | } break; |
| 1019 | case GC_BACKGROUND_PROCESSES_MSG: { |
| 1020 | synchronized (ActivityManagerService.this) { |
| 1021 | performAppGcsIfAppropriateLocked(); |
| 1022 | } |
| 1023 | } break; |
| 1024 | case WAIT_FOR_DEBUGGER_MSG: { |
| 1025 | synchronized (ActivityManagerService.this) { |
| 1026 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1027 | if (msg.arg1 != 0) { |
| 1028 | if (!app.waitedForDebugger) { |
| 1029 | Dialog d = new AppWaitingForDebuggerDialog( |
| 1030 | ActivityManagerService.this, |
| 1031 | mContext, app); |
| 1032 | app.waitDialog = d; |
| 1033 | app.waitedForDebugger = true; |
| 1034 | d.show(); |
| 1035 | } |
| 1036 | } else { |
| 1037 | if (app.waitDialog != null) { |
| 1038 | app.waitDialog.dismiss(); |
| 1039 | app.waitDialog = null; |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | case SERVICE_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1045 | if (mDidDexOpt) { |
| 1046 | mDidDexOpt = false; |
| 1047 | Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG); |
| 1048 | nmsg.obj = msg.obj; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1049 | mHandler.sendMessageDelayed(nmsg, ActiveServices.SERVICE_TIMEOUT); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1050 | return; |
| 1051 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1052 | mServices.serviceTimeout((ProcessRecord)msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | } break; |
| 1054 | case UPDATE_TIME_ZONE: { |
| 1055 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1056 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1057 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | if (r.thread != null) { |
| 1059 | try { |
| 1060 | r.thread.updateTimeZone(); |
| 1061 | } catch (RemoteException ex) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1062 | 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] | 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1067 | } break; |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1068 | case CLEAR_DNS_CACHE: { |
| 1069 | synchronized (ActivityManagerService.this) { |
| 1070 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1071 | ProcessRecord r = mLruProcesses.get(i); |
| 1072 | if (r.thread != null) { |
| 1073 | try { |
| 1074 | r.thread.clearDnsCache(); |
| 1075 | } catch (RemoteException ex) { |
| 1076 | Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName); |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | } break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1082 | case UPDATE_HTTP_PROXY: { |
| 1083 | ProxyProperties proxy = (ProxyProperties)msg.obj; |
| 1084 | String host = ""; |
| 1085 | String port = ""; |
| 1086 | String exclList = ""; |
| 1087 | if (proxy != null) { |
| 1088 | host = proxy.getHost(); |
| 1089 | port = Integer.toString(proxy.getPort()); |
| 1090 | exclList = proxy.getExclusionList(); |
| 1091 | } |
| 1092 | synchronized (ActivityManagerService.this) { |
| 1093 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 1094 | ProcessRecord r = mLruProcesses.get(i); |
| 1095 | if (r.thread != null) { |
| 1096 | try { |
| 1097 | r.thread.setHttpProxy(host, port, exclList); |
| 1098 | } catch (RemoteException ex) { |
| 1099 | Slog.w(TAG, "Failed to update http proxy for: " + |
| 1100 | r.info.processName); |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | } |
| 1105 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1106 | case SHOW_UID_ERROR_MSG: { |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 1107 | String title = "System UIDs Inconsistent"; |
| 1108 | String text = "UIDs on the system are inconsistent, you need to wipe your" |
| 1109 | + " data partition or your device will be unstable."; |
| 1110 | Log.e(TAG, title + ": " + text); |
| 1111 | if (mShowDialogs) { |
| 1112 | // XXX This is a temporary dialog, no need to localize. |
| 1113 | AlertDialog d = new BaseErrorDialog(mContext); |
| 1114 | d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 1115 | d.setCancelable(false); |
| 1116 | d.setTitle(title); |
| 1117 | d.setMessage(text); |
| 1118 | d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky", |
| 1119 | mHandler.obtainMessage(IM_FEELING_LUCKY_MSG)); |
| 1120 | mUidAlert = d; |
| 1121 | d.show(); |
| 1122 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1123 | } break; |
| 1124 | case IM_FEELING_LUCKY_MSG: { |
| 1125 | if (mUidAlert != null) { |
| 1126 | mUidAlert.dismiss(); |
| 1127 | mUidAlert = null; |
| 1128 | } |
| 1129 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1130 | case PROC_START_TIMEOUT_MSG: { |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1131 | if (mDidDexOpt) { |
| 1132 | mDidDexOpt = false; |
| 1133 | Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 1134 | nmsg.obj = msg.obj; |
| 1135 | mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT); |
| 1136 | return; |
| 1137 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | ProcessRecord app = (ProcessRecord)msg.obj; |
| 1139 | synchronized (ActivityManagerService.this) { |
| 1140 | processStartTimedOutLocked(app); |
| 1141 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1142 | } break; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 1143 | case DO_PENDING_ACTIVITY_LAUNCHES_MSG: { |
| 1144 | synchronized (ActivityManagerService.this) { |
| 1145 | doPendingActivityLaunchesLocked(true); |
| 1146 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 1147 | } break; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1148 | case KILL_APPLICATION_MSG: { |
| 1149 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1150 | int appid = msg.arg1; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1151 | boolean restart = (msg.arg2 == 1); |
| 1152 | String pkg = (String) msg.obj; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1153 | forceStopPackageLocked(pkg, appid, restart, false, true, false, |
| 1154 | UserHandle.USER_ALL); |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 1155 | } |
| 1156 | } break; |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 1157 | case FINALIZE_PENDING_INTENT_MSG: { |
| 1158 | ((PendingIntentRecord)msg.obj).completeFinalize(); |
| 1159 | } break; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1160 | case POST_HEAVY_NOTIFICATION_MSG: { |
| 1161 | INotificationManager inm = NotificationManager.getService(); |
| 1162 | if (inm == null) { |
| 1163 | return; |
| 1164 | } |
| 1165 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1166 | ActivityRecord root = (ActivityRecord)msg.obj; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1167 | ProcessRecord process = root.app; |
| 1168 | if (process == null) { |
| 1169 | return; |
| 1170 | } |
| 1171 | |
| 1172 | try { |
| 1173 | Context context = mContext.createPackageContext(process.info.packageName, 0); |
| 1174 | String text = mContext.getString(R.string.heavy_weight_notification, |
| 1175 | context.getApplicationInfo().loadLabel(context.getPackageManager())); |
| 1176 | Notification notification = new Notification(); |
| 1177 | notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon; |
| 1178 | notification.when = 0; |
| 1179 | notification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1180 | notification.tickerText = text; |
| 1181 | notification.defaults = 0; // please be quiet |
| 1182 | notification.sound = null; |
| 1183 | notification.vibrate = null; |
| 1184 | notification.setLatestEventInfo(context, text, |
| 1185 | mContext.getText(R.string.heavy_weight_notification_detail), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1186 | PendingIntent.getActivityAsUser(mContext, 0, root.intent, |
| 1187 | PendingIntent.FLAG_CANCEL_CURRENT, null, |
| 1188 | new UserHandle(root.userId))); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1189 | |
| 1190 | try { |
| 1191 | int[] outId = new int[1]; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1192 | inm.enqueueNotificationWithTag("android", null, |
| 1193 | R.string.heavy_weight_notification, |
| 1194 | notification, outId, root.userId); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1195 | } catch (RuntimeException e) { |
| 1196 | Slog.w(ActivityManagerService.TAG, |
| 1197 | "Error showing notification for heavy-weight app", e); |
| 1198 | } catch (RemoteException e) { |
| 1199 | } |
| 1200 | } catch (NameNotFoundException e) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 1201 | Slog.w(TAG, "Unable to create context for heavy notification", e); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1202 | } |
| 1203 | } break; |
| 1204 | case CANCEL_HEAVY_NOTIFICATION_MSG: { |
| 1205 | INotificationManager inm = NotificationManager.getService(); |
| 1206 | if (inm == null) { |
| 1207 | return; |
| 1208 | } |
| 1209 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1210 | inm.cancelNotificationWithTag("android", null, |
| 1211 | R.string.heavy_weight_notification, msg.arg1); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 1212 | } catch (RuntimeException e) { |
| 1213 | Slog.w(ActivityManagerService.TAG, |
| 1214 | "Error canceling notification for service", e); |
| 1215 | } catch (RemoteException e) { |
| 1216 | } |
| 1217 | } break; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1218 | case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: { |
| 1219 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1220 | checkExcessivePowerUsageLocked(true); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 1221 | removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1222 | Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 1223 | sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 1224 | } |
| 1225 | } break; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1226 | case SHOW_COMPAT_MODE_DIALOG_MSG: { |
| 1227 | synchronized (ActivityManagerService.this) { |
| 1228 | ActivityRecord ar = (ActivityRecord)msg.obj; |
| 1229 | if (mCompatModeDialog != null) { |
| 1230 | if (mCompatModeDialog.mAppInfo.packageName.equals( |
| 1231 | ar.info.applicationInfo.packageName)) { |
| 1232 | return; |
| 1233 | } |
| 1234 | mCompatModeDialog.dismiss(); |
| 1235 | mCompatModeDialog = null; |
| 1236 | } |
Dianne Hackborn | 2947826 | 2011-06-07 15:44:22 -0700 | [diff] [blame] | 1237 | if (ar != null && false) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 1238 | if (mCompatModePackages.getPackageAskCompatModeLocked( |
| 1239 | ar.packageName)) { |
| 1240 | int mode = mCompatModePackages.computeCompatModeLocked( |
| 1241 | ar.info.applicationInfo); |
| 1242 | if (mode == ActivityManager.COMPAT_MODE_DISABLED |
| 1243 | || mode == ActivityManager.COMPAT_MODE_ENABLED) { |
| 1244 | mCompatModeDialog = new CompatModeDialog( |
| 1245 | ActivityManagerService.this, mContext, |
| 1246 | ar.info.applicationInfo); |
| 1247 | mCompatModeDialog.show(); |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | } |
Dianne Hackborn | 36f80f3 | 2011-05-31 18:26:45 -0700 | [diff] [blame] | 1252 | break; |
| 1253 | } |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 1254 | case DISPATCH_PROCESSES_CHANGED: { |
| 1255 | dispatchProcessesChanged(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1256 | break; |
| 1257 | } |
| 1258 | case DISPATCH_PROCESS_DIED: { |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 1259 | final int pid = msg.arg1; |
| 1260 | final int uid = msg.arg2; |
| 1261 | dispatchProcessDied(pid, uid); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1262 | break; |
| 1263 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1264 | case REPORT_MEM_USAGE: { |
| 1265 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 1266 | if (!isDebuggable) { |
| 1267 | return; |
| 1268 | } |
| 1269 | synchronized (ActivityManagerService.this) { |
| 1270 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1271 | if (now < (mLastMemUsageReportTime+5*60*1000)) { |
| 1272 | // Don't report more than every 5 minutes to somewhat |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1273 | // avoid spamming. |
| 1274 | return; |
| 1275 | } |
| 1276 | mLastMemUsageReportTime = now; |
| 1277 | } |
| 1278 | Thread thread = new Thread() { |
| 1279 | @Override public void run() { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1280 | StringBuilder dropBuilder = new StringBuilder(1024); |
| 1281 | StringBuilder logBuilder = new StringBuilder(1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1282 | StringWriter oomSw = new StringWriter(); |
| 1283 | PrintWriter oomPw = new PrintWriter(oomSw); |
| 1284 | StringWriter catSw = new StringWriter(); |
| 1285 | PrintWriter catPw = new PrintWriter(catSw); |
| 1286 | String[] emptyArgs = new String[] { }; |
| 1287 | StringBuilder tag = new StringBuilder(128); |
| 1288 | StringBuilder stack = new StringBuilder(128); |
| 1289 | tag.append("Low on memory -- "); |
| 1290 | dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw, |
| 1291 | tag, stack); |
| 1292 | dropBuilder.append(stack); |
| 1293 | dropBuilder.append('\n'); |
| 1294 | dropBuilder.append('\n'); |
| 1295 | String oomString = oomSw.toString(); |
| 1296 | dropBuilder.append(oomString); |
| 1297 | dropBuilder.append('\n'); |
| 1298 | logBuilder.append(oomString); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1299 | try { |
| 1300 | java.lang.Process proc = Runtime.getRuntime().exec(new String[] { |
| 1301 | "procrank", }); |
| 1302 | final InputStreamReader converter = new InputStreamReader( |
| 1303 | proc.getInputStream()); |
| 1304 | BufferedReader in = new BufferedReader(converter); |
| 1305 | String line; |
| 1306 | while (true) { |
| 1307 | line = in.readLine(); |
| 1308 | if (line == null) { |
| 1309 | break; |
| 1310 | } |
| 1311 | if (line.length() > 0) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1312 | logBuilder.append(line); |
| 1313 | logBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1314 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1315 | dropBuilder.append(line); |
| 1316 | dropBuilder.append('\n'); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1317 | } |
| 1318 | converter.close(); |
| 1319 | } catch (IOException e) { |
| 1320 | } |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1321 | synchronized (ActivityManagerService.this) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1322 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1323 | dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1324 | catPw.println(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1325 | mServices.dumpServicesLocked(null, catPw, emptyArgs, 0, |
| 1326 | false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1327 | catPw.println(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1328 | dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null); |
Dianne Hackborn | 7aa6d31 | 2011-11-15 15:01:14 -0800 | [diff] [blame] | 1329 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1330 | dropBuilder.append(catSw.toString()); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1331 | addErrorToDropBox("lowmem", null, "system_server", null, |
| 1332 | null, tag.toString(), dropBuilder.toString(), null, null); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 1333 | Slog.i(TAG, logBuilder.toString()); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 1334 | synchronized (ActivityManagerService.this) { |
| 1335 | long now = SystemClock.uptimeMillis(); |
| 1336 | if (mLastMemUsageReportTime < now) { |
| 1337 | mLastMemUsageReportTime = now; |
| 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | }; |
| 1342 | thread.start(); |
| 1343 | break; |
| 1344 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1345 | case REPORT_USER_SWITCH_MSG: { |
| 1346 | dispatchUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1347 | break; |
| 1348 | } |
| 1349 | case CONTINUE_USER_SWITCH_MSG: { |
| 1350 | continueUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1351 | break; |
| 1352 | } |
| 1353 | case USER_SWITCH_TIMEOUT_MSG: { |
| 1354 | timeoutUserSwitch((UserStartedState)msg.obj, msg.arg1, msg.arg2); |
| 1355 | break; |
| 1356 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1357 | } |
| 1358 | } |
| 1359 | }; |
| 1360 | |
| 1361 | public static void setSystemProcess() { |
| 1362 | try { |
| 1363 | ActivityManagerService m = mSelf; |
| 1364 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 1365 | ServiceManager.addService("activity", m, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1366 | ServiceManager.addService("meminfo", new MemBinder(m)); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1367 | ServiceManager.addService("gfxinfo", new GraphicsBinder(m)); |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1368 | ServiceManager.addService("dbinfo", new DbBinder(m)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1369 | if (MONITOR_CPU_USAGE) { |
| 1370 | ServiceManager.addService("cpuinfo", new CpuBinder(m)); |
| 1371 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1372 | ServiceManager.addService("permission", new PermissionController(m)); |
| 1373 | |
| 1374 | ApplicationInfo info = |
| 1375 | mSelf.mContext.getPackageManager().getApplicationInfo( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 1376 | "android", STOCK_PM_FLAGS); |
Mike Cleron | 432b713 | 2009-09-24 15:28:29 -0700 | [diff] [blame] | 1377 | mSystemThread.installSystemApplicationInfo(info); |
| 1378 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1379 | synchronized (mSelf) { |
| 1380 | ProcessRecord app = mSelf.newProcessRecordLocked( |
| 1381 | mSystemThread.getApplicationThread(), info, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1382 | info.processName, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1383 | app.persistent = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 1384 | app.pid = MY_PID; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1385 | app.maxAdj = ProcessList.SYSTEM_ADJ; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1386 | mSelf.mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1387 | synchronized (mSelf.mPidsSelfLocked) { |
| 1388 | mSelf.mPidsSelfLocked.put(app.pid, app); |
| 1389 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1390 | mSelf.updateLruProcessLocked(app, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1391 | } |
| 1392 | } catch (PackageManager.NameNotFoundException e) { |
| 1393 | throw new RuntimeException( |
| 1394 | "Unable to find android system package", e); |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | public void setWindowManager(WindowManagerService wm) { |
| 1399 | mWindowManager = wm; |
| 1400 | } |
| 1401 | |
| 1402 | public static final Context main(int factoryTest) { |
| 1403 | AThread thr = new AThread(); |
| 1404 | thr.start(); |
| 1405 | |
| 1406 | synchronized (thr) { |
| 1407 | while (thr.mService == null) { |
| 1408 | try { |
| 1409 | thr.wait(); |
| 1410 | } catch (InterruptedException e) { |
| 1411 | } |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | ActivityManagerService m = thr.mService; |
| 1416 | mSelf = m; |
| 1417 | ActivityThread at = ActivityThread.systemMain(); |
| 1418 | mSystemThread = at; |
| 1419 | Context context = at.getSystemContext(); |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 1420 | context.setTheme(android.R.style.Theme_Holo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1421 | m.mContext = context; |
| 1422 | m.mFactoryTest = factoryTest; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1423 | m.mMainStack = new ActivityStack(m, context, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | |
| 1425 | m.mBatteryStatsService.publish(context); |
| 1426 | m.mUsageStatsService.publish(context); |
| 1427 | |
| 1428 | synchronized (thr) { |
| 1429 | thr.mReady = true; |
| 1430 | thr.notifyAll(); |
| 1431 | } |
| 1432 | |
| 1433 | m.startRunning(null, null, null, null); |
| 1434 | |
| 1435 | return context; |
| 1436 | } |
| 1437 | |
| 1438 | public static ActivityManagerService self() { |
| 1439 | return mSelf; |
| 1440 | } |
| 1441 | |
| 1442 | static class AThread extends Thread { |
| 1443 | ActivityManagerService mService; |
| 1444 | boolean mReady = false; |
| 1445 | |
| 1446 | public AThread() { |
| 1447 | super("ActivityManager"); |
| 1448 | } |
| 1449 | |
| 1450 | public void run() { |
| 1451 | Looper.prepare(); |
| 1452 | |
| 1453 | android.os.Process.setThreadPriority( |
| 1454 | android.os.Process.THREAD_PRIORITY_FOREGROUND); |
Christopher Tate | 160edb3 | 2010-06-30 17:46:30 -0700 | [diff] [blame] | 1455 | android.os.Process.setCanSelfBackground(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1456 | |
| 1457 | ActivityManagerService m = new ActivityManagerService(); |
| 1458 | |
| 1459 | synchronized (this) { |
| 1460 | mService = m; |
| 1461 | notifyAll(); |
| 1462 | } |
| 1463 | |
| 1464 | synchronized (this) { |
| 1465 | while (!mReady) { |
| 1466 | try { |
| 1467 | wait(); |
| 1468 | } catch (InterruptedException e) { |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1473 | // For debug builds, log event loop stalls to dropbox for analysis. |
| 1474 | if (StrictMode.conditionallyEnableDebugLogging()) { |
| 1475 | Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper"); |
| 1476 | } |
| 1477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | Looper.loop(); |
| 1479 | } |
| 1480 | } |
| 1481 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1482 | static class MemBinder extends Binder { |
| 1483 | ActivityManagerService mActivityManagerService; |
| 1484 | MemBinder(ActivityManagerService activityManagerService) { |
| 1485 | mActivityManagerService = activityManagerService; |
| 1486 | } |
| 1487 | |
| 1488 | @Override |
| 1489 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1490 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1491 | != PackageManager.PERMISSION_GRANTED) { |
| 1492 | pw.println("Permission Denial: can't dump meminfo from from pid=" |
| 1493 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1494 | + " without permission " + android.Manifest.permission.DUMP); |
| 1495 | return; |
| 1496 | } |
| 1497 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 1498 | mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 1499 | false, null, null, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1500 | } |
| 1501 | } |
| 1502 | |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1503 | static class GraphicsBinder extends Binder { |
| 1504 | ActivityManagerService mActivityManagerService; |
| 1505 | GraphicsBinder(ActivityManagerService activityManagerService) { |
| 1506 | mActivityManagerService = activityManagerService; |
| 1507 | } |
| 1508 | |
| 1509 | @Override |
| 1510 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1511 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1512 | != PackageManager.PERMISSION_GRANTED) { |
| 1513 | pw.println("Permission Denial: can't dump gfxinfo from from pid=" |
| 1514 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1515 | + " without permission " + android.Manifest.permission.DUMP); |
| 1516 | return; |
| 1517 | } |
| 1518 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 1519 | mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1520 | } |
| 1521 | } |
| 1522 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 1523 | static class DbBinder extends Binder { |
| 1524 | ActivityManagerService mActivityManagerService; |
| 1525 | DbBinder(ActivityManagerService activityManagerService) { |
| 1526 | mActivityManagerService = activityManagerService; |
| 1527 | } |
| 1528 | |
| 1529 | @Override |
| 1530 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1531 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1532 | != PackageManager.PERMISSION_GRANTED) { |
| 1533 | pw.println("Permission Denial: can't dump dbinfo from from pid=" |
| 1534 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1535 | + " without permission " + android.Manifest.permission.DUMP); |
| 1536 | return; |
| 1537 | } |
| 1538 | |
| 1539 | mActivityManagerService.dumpDbInfo(fd, pw, args); |
| 1540 | } |
| 1541 | } |
| 1542 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1543 | static class CpuBinder extends Binder { |
| 1544 | ActivityManagerService mActivityManagerService; |
| 1545 | CpuBinder(ActivityManagerService activityManagerService) { |
| 1546 | mActivityManagerService = activityManagerService; |
| 1547 | } |
| 1548 | |
| 1549 | @Override |
| 1550 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1551 | if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1552 | != PackageManager.PERMISSION_GRANTED) { |
| 1553 | pw.println("Permission Denial: can't dump cpuinfo from from pid=" |
| 1554 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1555 | + " without permission " + android.Manifest.permission.DUMP); |
| 1556 | return; |
| 1557 | } |
| 1558 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1559 | synchronized (mActivityManagerService.mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 1560 | pw.print(mActivityManagerService.mProcessStats.printCurrentLoad()); |
| 1561 | pw.print(mActivityManagerService.mProcessStats.printCurrentState( |
| 1562 | SystemClock.uptimeMillis())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1563 | } |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | private ActivityManagerService() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1568 | Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass()); |
Dianne Hackborn | 2c6c5e6 | 2009-10-08 17:55:49 -0700 | [diff] [blame] | 1569 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1570 | mFgBroadcastQueue = new BroadcastQueue(this, "foreground", BROADCAST_FG_TIMEOUT); |
| 1571 | mBgBroadcastQueue = new BroadcastQueue(this, "background", BROADCAST_BG_TIMEOUT); |
| 1572 | mBroadcastQueues[0] = mFgBroadcastQueue; |
| 1573 | mBroadcastQueues[1] = mBgBroadcastQueue; |
| 1574 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1575 | mServices = new ActiveServices(this); |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 1576 | mProviderMap = new ProviderMap(this); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1577 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1578 | File dataDir = Environment.getDataDirectory(); |
| 1579 | File systemDir = new File(dataDir, "system"); |
| 1580 | systemDir.mkdirs(); |
| 1581 | mBatteryStatsService = new BatteryStatsService(new File( |
| 1582 | systemDir, "batterystats.bin").toString()); |
| 1583 | mBatteryStatsService.getActiveStatistics().readLocked(); |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1584 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1585 | mOnBattery = DEBUG_POWER ? true |
| 1586 | : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1587 | mBatteryStatsService.getActiveStatistics().setCallback(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1588 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1589 | mUsageStatsService = new UsageStatsService(new File( |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1590 | systemDir, "usagestats").toString()); |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 1591 | mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1592 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1593 | // User 0 is the first and only user that runs at boot. |
| 1594 | mStartedUsers.put(0, new UserStartedState(new UserHandle(0), true)); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 1595 | mUserLru.add(Integer.valueOf(0)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 1596 | updateStartedUserArrayLocked(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1597 | |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 1598 | GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", |
| 1599 | ConfigurationInfo.GL_ES_VERSION_UNDEFINED); |
| 1600 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1601 | mConfiguration.setToDefaults(); |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 1602 | mConfiguration.setLocale(Locale.getDefault()); |
| 1603 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 1604 | mConfigurationSeq = mConfiguration.seq = 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1605 | mProcessStats.init(); |
| 1606 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 1607 | mCompatModePackages = new CompatModePackages(this, systemDir); |
| 1608 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | // Add ourself to the Watchdog monitors. |
| 1610 | Watchdog.getInstance().addMonitor(this); |
| 1611 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1612 | mProcessStatsThread = new Thread("ProcessStats") { |
| 1613 | public void run() { |
| 1614 | while (true) { |
| 1615 | try { |
| 1616 | try { |
| 1617 | synchronized(this) { |
| 1618 | final long now = SystemClock.uptimeMillis(); |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1619 | long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1621 | //Slog.i(TAG, "Cpu delay=" + nextCpuDelay |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1622 | // + ", write delay=" + nextWriteDelay); |
| 1623 | if (nextWriteDelay < nextCpuDelay) { |
| 1624 | nextCpuDelay = nextWriteDelay; |
| 1625 | } |
| 1626 | if (nextCpuDelay > 0) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1627 | mProcessStatsMutexFree.set(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1628 | this.wait(nextCpuDelay); |
| 1629 | } |
| 1630 | } |
| 1631 | } catch (InterruptedException e) { |
| 1632 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1633 | updateCpuStatsNow(); |
| 1634 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1635 | Slog.e(TAG, "Unexpected exception collecting process stats", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | } |
| 1637 | } |
| 1638 | } |
| 1639 | }; |
| 1640 | mProcessStatsThread.start(); |
| 1641 | } |
| 1642 | |
| 1643 | @Override |
| 1644 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1645 | throws RemoteException { |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 1646 | if (code == SYSPROPS_TRANSACTION) { |
| 1647 | // We need to tell all apps about the system property change. |
| 1648 | ArrayList<IBinder> procs = new ArrayList<IBinder>(); |
| 1649 | synchronized(this) { |
| 1650 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 1651 | final int NA = apps.size(); |
| 1652 | for (int ia=0; ia<NA; ia++) { |
| 1653 | ProcessRecord app = apps.valueAt(ia); |
| 1654 | if (app.thread != null) { |
| 1655 | procs.add(app.thread.asBinder()); |
| 1656 | } |
| 1657 | } |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | int N = procs.size(); |
| 1662 | for (int i=0; i<N; i++) { |
| 1663 | Parcel data2 = Parcel.obtain(); |
| 1664 | try { |
| 1665 | procs.get(i).transact(IBinder.SYSPROPS_TRANSACTION, data2, null, 0); |
| 1666 | } catch (RemoteException e) { |
| 1667 | } |
| 1668 | data2.recycle(); |
| 1669 | } |
| 1670 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1671 | try { |
| 1672 | return super.onTransact(code, data, reply, flags); |
| 1673 | } catch (RuntimeException e) { |
| 1674 | // The activity manager only throws security exceptions, so let's |
| 1675 | // log all others. |
| 1676 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1677 | Slog.e(TAG, "Activity Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1678 | } |
| 1679 | throw e; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | void updateCpuStats() { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1684 | final long now = SystemClock.uptimeMillis(); |
| 1685 | if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) { |
| 1686 | return; |
| 1687 | } |
| 1688 | if (mProcessStatsMutexFree.compareAndSet(true, false)) { |
| 1689 | synchronized (mProcessStatsThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1690 | mProcessStatsThread.notify(); |
| 1691 | } |
| 1692 | } |
| 1693 | } |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1694 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1695 | void updateCpuStatsNow() { |
| 1696 | synchronized (mProcessStatsThread) { |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1697 | mProcessStatsMutexFree.set(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1698 | final long now = SystemClock.uptimeMillis(); |
| 1699 | boolean haveNewCpuStats = false; |
Amith Yamasani | eaeb663 | 2009-06-03 15:16:10 -0700 | [diff] [blame] | 1700 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1701 | if (MONITOR_CPU_USAGE && |
Brad Fitzpatrick | 01fad4a | 2010-04-19 10:47:40 -0700 | [diff] [blame] | 1702 | mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) { |
| 1703 | mLastCpuTime.set(now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1704 | haveNewCpuStats = true; |
| 1705 | mProcessStats.update(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1706 | //Slog.i(TAG, mProcessStats.printCurrentState()); |
| 1707 | //Slog.i(TAG, "Total CPU usage: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1708 | // + mProcessStats.getTotalCpuPercent() + "%"); |
| 1709 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1710 | // Slog the cpu usage if the property is set. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1711 | if ("true".equals(SystemProperties.get("events.cpu"))) { |
| 1712 | int user = mProcessStats.getLastUserTime(); |
| 1713 | int system = mProcessStats.getLastSystemTime(); |
| 1714 | int iowait = mProcessStats.getLastIoWaitTime(); |
| 1715 | int irq = mProcessStats.getLastIrqTime(); |
| 1716 | int softIrq = mProcessStats.getLastSoftIrqTime(); |
| 1717 | int idle = mProcessStats.getLastIdleTime(); |
| 1718 | |
| 1719 | int total = user + system + iowait + irq + softIrq + idle; |
| 1720 | if (total == 0) total = 1; |
| 1721 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 1722 | EventLog.writeEvent(EventLogTags.CPU, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1723 | ((user+system+iowait+irq+softIrq) * 100) / total, |
| 1724 | (user * 100) / total, |
| 1725 | (system * 100) / total, |
| 1726 | (iowait * 100) / total, |
| 1727 | (irq * 100) / total, |
| 1728 | (softIrq * 100) / total); |
| 1729 | } |
| 1730 | } |
| 1731 | |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1732 | long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes(); |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1733 | final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics(); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1734 | synchronized(bstats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1735 | synchronized(mPidsSelfLocked) { |
| 1736 | if (haveNewCpuStats) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1737 | if (mOnBattery) { |
| 1738 | int perc = bstats.startAddingCpuLocked(); |
| 1739 | int totalUTime = 0; |
| 1740 | int totalSTime = 0; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1741 | final int N = mProcessStats.countStats(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1742 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1743 | ProcessStats.Stats st = mProcessStats.getStats(i); |
| 1744 | if (!st.working) { |
| 1745 | continue; |
| 1746 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1747 | ProcessRecord pr = mPidsSelfLocked.get(st.pid); |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1748 | int otherUTime = (st.rel_utime*perc)/100; |
| 1749 | int otherSTime = (st.rel_stime*perc)/100; |
| 1750 | totalUTime += otherUTime; |
| 1751 | totalSTime += otherSTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1752 | if (pr != null) { |
| 1753 | BatteryStatsImpl.Uid.Proc ps = pr.batteryStats; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1754 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1755 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1756 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1757 | pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10; |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1758 | } else { |
| 1759 | BatteryStatsImpl.Uid.Proc ps = |
Amith Yamasani | 819f928 | 2009-06-24 23:18:15 -0700 | [diff] [blame] | 1760 | bstats.getProcessStatsLocked(st.name, st.pid); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1761 | if (ps != null) { |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1762 | ps.addCpuTimeLocked(st.rel_utime-otherUTime, |
| 1763 | st.rel_stime-otherSTime); |
Amith Yamasani | e43530a | 2009-08-21 13:11:37 -0700 | [diff] [blame] | 1764 | ps.addSpeedStepTimes(cpuSpeedTimes); |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1765 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1766 | } |
| 1767 | } |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1768 | bstats.finishAddingCpuLocked(perc, totalUTime, |
| 1769 | totalSTime, cpuSpeedTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1770 | } |
| 1771 | } |
| 1772 | } |
Amith Yamasani | 32dbefd | 2009-06-19 09:21:17 -0700 | [diff] [blame] | 1773 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1774 | if (mLastWriteTime < (now-BATTERY_STATS_TIME)) { |
| 1775 | mLastWriteTime = now; |
Dianne Hackborn | ce2ef76 | 2010-09-20 11:39:14 -0700 | [diff] [blame] | 1776 | mBatteryStatsService.getActiveStatistics().writeAsyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1777 | } |
| 1778 | } |
| 1779 | } |
| 1780 | } |
| 1781 | |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1782 | @Override |
| 1783 | public void batteryNeedsCpuUpdate() { |
| 1784 | updateCpuStatsNow(); |
| 1785 | } |
| 1786 | |
| 1787 | @Override |
| 1788 | public void batteryPowerChanged(boolean onBattery) { |
| 1789 | // When plugging in, update the CPU stats first before changing |
| 1790 | // the plug state. |
| 1791 | updateCpuStatsNow(); |
| 1792 | synchronized (this) { |
| 1793 | synchronized(mPidsSelfLocked) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 1794 | mOnBattery = DEBUG_POWER ? true : onBattery; |
Dianne Hackborn | 0d903a8 | 2010-09-07 23:51:03 -0700 | [diff] [blame] | 1795 | } |
| 1796 | } |
| 1797 | } |
| 1798 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1799 | /** |
| 1800 | * Initialize the application bind args. These are passed to each |
| 1801 | * process when the bindApplication() IPC is sent to the process. They're |
| 1802 | * lazily setup to make sure the services are running when they're asked for. |
| 1803 | */ |
| 1804 | private HashMap<String, IBinder> getCommonServicesLocked() { |
| 1805 | if (mAppBindArgs == null) { |
| 1806 | mAppBindArgs = new HashMap<String, IBinder>(); |
| 1807 | |
| 1808 | // Setup the application init args |
| 1809 | mAppBindArgs.put("package", ServiceManager.getService("package")); |
| 1810 | mAppBindArgs.put("window", ServiceManager.getService("window")); |
| 1811 | mAppBindArgs.put(Context.ALARM_SERVICE, |
| 1812 | ServiceManager.getService(Context.ALARM_SERVICE)); |
| 1813 | } |
| 1814 | return mAppBindArgs; |
| 1815 | } |
| 1816 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1817 | final void setFocusedActivityLocked(ActivityRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1818 | if (mFocusedActivity != r) { |
| 1819 | mFocusedActivity = r; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 1820 | if (r != null) { |
| 1821 | mWindowManager.setFocusedApp(r.appToken, true); |
| 1822 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1826 | private final void updateLruProcessInternalLocked(ProcessRecord app, int bestPos) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1827 | // 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] | 1828 | int lrui = mLruProcesses.indexOf(app); |
| 1829 | if (lrui >= 0) mLruProcesses.remove(lrui); |
| 1830 | |
| 1831 | int i = mLruProcesses.size()-1; |
| 1832 | int skipTop = 0; |
| 1833 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1834 | app.lruSeq = mLruSeq; |
| 1835 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1836 | // compute the new weight for this process. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1837 | app.lastActivityTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1838 | if (app.activities.size() > 0) { |
| 1839 | // If this process has activities, we more strongly want to keep |
| 1840 | // it around. |
| 1841 | app.lruWeight = app.lastActivityTime; |
| 1842 | } else if (app.pubProviders.size() > 0) { |
| 1843 | // If this process contains content providers, we want to keep |
| 1844 | // it a little more strongly. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1845 | app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1846 | // 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] | 1847 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1848 | } else { |
| 1849 | // If this process doesn't have activities, we less strongly |
| 1850 | // want to keep it around, and generally want to avoid getting |
| 1851 | // in front of any very recently used activities. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1852 | app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1853 | // 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] | 1854 | skipTop = ProcessList.MIN_HIDDEN_APPS; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1855 | } |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 1856 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1857 | while (i >= 0) { |
| 1858 | ProcessRecord p = mLruProcesses.get(i); |
| 1859 | // If this app shouldn't be in front of the first N background |
| 1860 | // apps, then skip over that many that are currently hidden. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 1861 | if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1862 | skipTop--; |
| 1863 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1864 | if (p.lruWeight <= app.lruWeight || i < bestPos) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 1865 | mLruProcesses.add(i+1, app); |
| 1866 | break; |
| 1867 | } |
| 1868 | i--; |
| 1869 | } |
| 1870 | if (i < 0) { |
| 1871 | mLruProcesses.add(0, app); |
| 1872 | } |
| 1873 | |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1874 | // If the app is currently using a content provider or service, |
| 1875 | // bump those processes as well. |
| 1876 | if (app.connections.size() > 0) { |
| 1877 | for (ConnectionRecord cr : app.connections) { |
| 1878 | if (cr.binding != null && cr.binding.service != null |
| 1879 | && cr.binding.service.app != null |
| 1880 | && cr.binding.service.app.lruSeq != mLruSeq) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1881 | updateLruProcessInternalLocked(cr.binding.service.app, i+1); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1882 | } |
| 1883 | } |
| 1884 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1885 | for (int j=app.conProviders.size()-1; j>=0; j--) { |
| 1886 | ContentProviderRecord cpr = app.conProviders.get(j).provider; |
| 1887 | if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1888 | updateLruProcessInternalLocked(cpr.proc, i+1); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1889 | } |
| 1890 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1891 | } |
| 1892 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1893 | final void updateLruProcessLocked(ProcessRecord app, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1894 | boolean oomAdj) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1895 | mLruSeq++; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1896 | updateLruProcessInternalLocked(app, 0); |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 1897 | |
| 1898 | //Slog.i(TAG, "Putting proc to front: " + app.processName); |
| 1899 | if (oomAdj) { |
| 1900 | updateOomAdjLocked(); |
| 1901 | } |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 1902 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 1903 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1904 | final ProcessRecord getProcessRecordLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1905 | String processName, int uid) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1906 | if (uid == Process.SYSTEM_UID) { |
Amith Yamasani | 0184ce9 | 2012-03-28 22:41:41 -0700 | [diff] [blame] | 1907 | // The system gets to run in any process. If there are multiple |
| 1908 | // processes with the same uid, just pick the first (this |
| 1909 | // should never happen). |
| 1910 | SparseArray<ProcessRecord> procs = mProcessNames.getMap().get( |
| 1911 | processName); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1912 | if (procs == null) return null; |
| 1913 | final int N = procs.size(); |
| 1914 | for (int i = 0; i < N; i++) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1915 | if (UserHandle.isSameUser(procs.keyAt(i), uid)) return procs.valueAt(i); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 1916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1917 | } |
| 1918 | ProcessRecord proc = mProcessNames.get(processName, uid); |
| 1919 | return proc; |
| 1920 | } |
| 1921 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1922 | void ensurePackageDexOpt(String packageName) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1923 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 1924 | try { |
| 1925 | if (pm.performDexOpt(packageName)) { |
| 1926 | mDidDexOpt = true; |
| 1927 | } |
| 1928 | } catch (RemoteException e) { |
| 1929 | } |
| 1930 | } |
| 1931 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1932 | boolean isNextTransitionForward() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1933 | int transit = mWindowManager.getPendingAppTransition(); |
| 1934 | return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN |
| 1935 | || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN |
| 1936 | || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT; |
| 1937 | } |
| 1938 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 1939 | final ProcessRecord startProcessLocked(String processName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1940 | ApplicationInfo info, boolean knownToBeDead, int intentFlags, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1941 | String hostingType, ComponentName hostingName, boolean allowWhileBooting, |
| 1942 | boolean isolated) { |
| 1943 | ProcessRecord app; |
| 1944 | if (!isolated) { |
| 1945 | app = getProcessRecordLocked(processName, info.uid); |
| 1946 | } else { |
| 1947 | // If this is an isolated process, it can't re-use an existing process. |
| 1948 | app = null; |
| 1949 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1950 | // We don't have to do anything more if: |
| 1951 | // (1) There is an existing application record; and |
| 1952 | // (2) The caller doesn't think it is dead, OR there is no thread |
| 1953 | // object attached to it so we know it couldn't have crashed; and |
| 1954 | // (3) There is a pid assigned to it, so it is either starting or |
| 1955 | // already running. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1956 | if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1957 | + " app=" + app + " knownToBeDead=" + knownToBeDead |
| 1958 | + " thread=" + (app != null ? app.thread : null) |
| 1959 | + " pid=" + (app != null ? app.pid : -1)); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1960 | if (app != null && app.pid > 0) { |
| 1961 | if (!knownToBeDead || app.thread == null) { |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1962 | // We already have the app running, or are waiting for it to |
| 1963 | // 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] | 1964 | if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app); |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1965 | // If this is a new package in the process, add the package to the list |
| 1966 | app.addPackage(info.packageName); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1967 | return app; |
| 1968 | } else { |
| 1969 | // An application record is attached to a previous process, |
| 1970 | // clean it up now. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1971 | if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 1972 | handleAppDiedLocked(app, true, true); |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1973 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1974 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 1975 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1976 | String hostingNameStr = hostingName != null |
| 1977 | ? hostingName.flattenToShortString() : null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1978 | |
| 1979 | if (!isolated) { |
| 1980 | if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) { |
| 1981 | // If we are in the background, then check to see if this process |
| 1982 | // is bad. If so, we will just silently fail. |
| 1983 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
| 1984 | if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid |
| 1985 | + "/" + info.processName); |
| 1986 | return null; |
| 1987 | } |
| 1988 | } else { |
| 1989 | // When the user is explicitly starting a process, then clear its |
| 1990 | // crash count so that we won't make it bad until they see at |
| 1991 | // least one crash dialog again, and make the process good again |
| 1992 | // if it had been bad. |
| 1993 | if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 1994 | + "/" + info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1995 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 1996 | if (mBadProcesses.get(info.processName, info.uid) != null) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1997 | EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, |
| 1998 | UserHandle.getUserId(info.uid), info.uid, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 1999 | info.processName); |
| 2000 | mBadProcesses.remove(info.processName, info.uid); |
| 2001 | if (app != null) { |
| 2002 | app.bad = false; |
| 2003 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2004 | } |
| 2005 | } |
| 2006 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2007 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2008 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2009 | app = newProcessRecordLocked(null, info, processName, isolated); |
| 2010 | if (app == null) { |
| 2011 | Slog.w(TAG, "Failed making new process record for " |
| 2012 | + processName + "/" + info.uid + " isolated=" + isolated); |
| 2013 | return null; |
| 2014 | } |
| 2015 | mProcessNames.put(processName, app.uid, app); |
| 2016 | if (isolated) { |
| 2017 | mIsolatedProcesses.put(app.uid, app); |
| 2018 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2019 | } else { |
| 2020 | // If this is a new package in the process, add the package to the list |
| 2021 | app.addPackage(info.packageName); |
| 2022 | } |
| 2023 | |
| 2024 | // If the system is not ready yet, then hold off on starting this |
| 2025 | // process until it is. |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2026 | if (!mProcessesReady |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 2027 | && !isAllowedWhileBooting(info) |
| 2028 | && !allowWhileBooting) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2029 | if (!mProcessesOnHold.contains(app)) { |
| 2030 | mProcessesOnHold.add(app); |
| 2031 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2032 | 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] | 2033 | return app; |
| 2034 | } |
| 2035 | |
| 2036 | startProcessLocked(app, hostingType, hostingNameStr); |
| 2037 | return (app.pid != 0) ? app : null; |
| 2038 | } |
| 2039 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 2040 | boolean isAllowedWhileBooting(ApplicationInfo ai) { |
| 2041 | return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 2042 | } |
| 2043 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2044 | private final void startProcessLocked(ProcessRecord app, |
| 2045 | String hostingType, String hostingNameStr) { |
| 2046 | if (app.pid > 0 && app.pid != MY_PID) { |
| 2047 | synchronized (mPidsSelfLocked) { |
| 2048 | mPidsSelfLocked.remove(app.pid); |
| 2049 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 2050 | } |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2051 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2052 | } |
| 2053 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 2054 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 2055 | "startProcessLocked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2056 | mProcessesOnHold.remove(app); |
| 2057 | |
| 2058 | updateCpuStats(); |
| 2059 | |
| 2060 | System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1); |
| 2061 | mProcDeaths[0] = 0; |
| 2062 | |
| 2063 | try { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2064 | int uid = app.uid; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2065 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2066 | int[] gids = null; |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2067 | int mountExternal = Zygote.MOUNT_EXTERNAL_NONE; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2068 | if (!app.isolated) { |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2069 | int[] permGids = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2070 | try { |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2071 | final PackageManager pm = mContext.getPackageManager(); |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2072 | permGids = pm.getPackageGids(app.info.packageName); |
Jeff Sharkey | e217ee4 | 2012-08-28 16:23:01 -0700 | [diff] [blame] | 2073 | |
| 2074 | if (Environment.isExternalStorageEmulated()) { |
| 2075 | if (pm.checkPermission( |
| 2076 | android.Manifest.permission.ACCESS_ALL_EXTERNAL_STORAGE, |
| 2077 | app.info.packageName) == PERMISSION_GRANTED) { |
| 2078 | mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL; |
| 2079 | } else { |
| 2080 | mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER; |
| 2081 | } |
| 2082 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2083 | } catch (PackageManager.NameNotFoundException e) { |
| 2084 | Slog.w(TAG, "Unable to retrieve gids", e); |
| 2085 | } |
Kenny Root | e091f22 | 2012-09-11 15:01:26 -0700 | [diff] [blame] | 2086 | |
| 2087 | /* |
| 2088 | * Add shared application GID so applications can share some |
| 2089 | * resources like shared libraries |
| 2090 | */ |
| 2091 | if (permGids == null) { |
| 2092 | gids = new int[1]; |
| 2093 | } else { |
| 2094 | gids = new int[permGids.length + 1]; |
| 2095 | System.arraycopy(permGids, 0, gids, 1, permGids.length); |
| 2096 | } |
| 2097 | gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2098 | } |
| 2099 | if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) { |
| 2100 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2101 | && mTopComponent != null |
| 2102 | && app.processName.equals(mTopComponent.getPackageName())) { |
| 2103 | uid = 0; |
| 2104 | } |
| 2105 | if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL |
| 2106 | && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) { |
| 2107 | uid = 0; |
| 2108 | } |
| 2109 | } |
| 2110 | int debugFlags = 0; |
| 2111 | if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { |
| 2112 | debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER; |
Elliott Hughes | fa36aee | 2011-06-17 14:39:41 -0700 | [diff] [blame] | 2113 | // Also turn on CheckJNI for debuggable apps. It's quite |
| 2114 | // awkward to turn on otherwise. |
| 2115 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2116 | } |
Ben Cheng | 6c0afff | 2010-02-14 16:18:56 -0800 | [diff] [blame] | 2117 | // Run the app in safe mode if its manifest requests so or the |
| 2118 | // system is booted in safe mode. |
| 2119 | if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 || |
| 2120 | Zygote.systemInSafeMode == true) { |
Ben Cheng | 23085b7 | 2010-02-08 16:06:32 -0800 | [diff] [blame] | 2121 | debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE; |
| 2122 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2123 | if ("1".equals(SystemProperties.get("debug.checkjni"))) { |
| 2124 | debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI; |
| 2125 | } |
Elliott Hughes | ae07ecf | 2011-07-06 17:33:27 -0700 | [diff] [blame] | 2126 | if ("1".equals(SystemProperties.get("debug.jni.logging"))) { |
| 2127 | debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING; |
| 2128 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2129 | if ("1".equals(SystemProperties.get("debug.assert"))) { |
| 2130 | debugFlags |= Zygote.DEBUG_ENABLE_ASSERT; |
| 2131 | } |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2132 | |
| 2133 | // Start the process. It will either succeed and return a result containing |
| 2134 | // the PID of the new process, or else throw a RuntimeException. |
| 2135 | Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread", |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 2136 | app.processName, uid, uid, gids, debugFlags, mountExternal, |
Stephen Smalley | 83d9eda | 2012-01-13 08:34:17 -0500 | [diff] [blame] | 2137 | app.info.targetSdkVersion, null, null); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2138 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2139 | BatteryStatsImpl bs = app.batteryStats.getBatteryStats(); |
| 2140 | synchronized (bs) { |
| 2141 | if (bs.isOnBattery()) { |
| 2142 | app.batteryStats.incStartsLocked(); |
| 2143 | } |
| 2144 | } |
| 2145 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 2146 | EventLog.writeEvent(EventLogTags.AM_PROC_START, |
| 2147 | UserHandle.getUserId(uid), startResult.pid, uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2148 | app.processName, hostingType, |
| 2149 | hostingNameStr != null ? hostingNameStr : ""); |
| 2150 | |
| 2151 | if (app.persistent) { |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2152 | Watchdog.getInstance().processStarted(app.processName, startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2153 | } |
| 2154 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 2155 | StringBuilder buf = mStringBuilder; |
| 2156 | buf.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2157 | buf.append("Start proc "); |
| 2158 | buf.append(app.processName); |
| 2159 | buf.append(" for "); |
| 2160 | buf.append(hostingType); |
| 2161 | if (hostingNameStr != null) { |
| 2162 | buf.append(" "); |
| 2163 | buf.append(hostingNameStr); |
| 2164 | } |
| 2165 | buf.append(": pid="); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2166 | buf.append(startResult.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2167 | buf.append(" uid="); |
| 2168 | buf.append(uid); |
| 2169 | buf.append(" gids={"); |
| 2170 | if (gids != null) { |
| 2171 | for (int gi=0; gi<gids.length; gi++) { |
| 2172 | if (gi != 0) buf.append(", "); |
| 2173 | buf.append(gids[gi]); |
| 2174 | |
| 2175 | } |
| 2176 | } |
| 2177 | buf.append("}"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2178 | Slog.i(TAG, buf.toString()); |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2179 | app.setPid(startResult.pid); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 2180 | app.usingWrapper = startResult.usingWrapper; |
| 2181 | app.removed = false; |
| 2182 | synchronized (mPidsSelfLocked) { |
| 2183 | this.mPidsSelfLocked.put(startResult.pid, app); |
| 2184 | Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG); |
| 2185 | msg.obj = app; |
| 2186 | mHandler.sendMessageDelayed(msg, startResult.usingWrapper |
| 2187 | ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2188 | } |
| 2189 | } catch (RuntimeException e) { |
| 2190 | // XXX do better error recovery. |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2191 | app.setPid(0); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2192 | Slog.e(TAG, "Failure starting process " + app.processName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2193 | } |
| 2194 | } |
| 2195 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2196 | void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2197 | if (resumed) { |
| 2198 | mUsageStatsService.noteResumeComponent(resumedComponent.realActivity); |
| 2199 | } else { |
| 2200 | mUsageStatsService.notePauseComponent(resumedComponent.realActivity); |
| 2201 | } |
| 2202 | } |
| 2203 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2204 | boolean startHomeActivityLocked(int userId) { |
Mike Lockwood | a8f767a | 2011-08-31 14:32:37 -0400 | [diff] [blame] | 2205 | if (mHeadless) { |
| 2206 | // Added because none of the other calls to ensureBootCompleted seem to fire |
| 2207 | // when running headless. |
| 2208 | ensureBootCompleted(); |
| 2209 | return false; |
| 2210 | } |
| 2211 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2212 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL |
| 2213 | && mTopAction == null) { |
| 2214 | // We are running in factory test mode, but unable to find |
| 2215 | // the factory test app, so just sit around displaying the |
| 2216 | // error message and don't try to start anything. |
| 2217 | return false; |
| 2218 | } |
| 2219 | Intent intent = new Intent( |
| 2220 | mTopAction, |
| 2221 | mTopData != null ? Uri.parse(mTopData) : null); |
| 2222 | intent.setComponent(mTopComponent); |
| 2223 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 2224 | intent.addCategory(Intent.CATEGORY_HOME); |
| 2225 | } |
| 2226 | ActivityInfo aInfo = |
Amith Yamasani | 259d5e5 | 2012-08-31 15:11:01 -0700 | [diff] [blame] | 2227 | resolveActivityInfo(intent, STOCK_PM_FLAGS, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2228 | if (aInfo != null) { |
| 2229 | intent.setComponent(new ComponentName( |
| 2230 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2231 | // Don't do this if the home app is currently being |
| 2232 | // instrumented. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2233 | aInfo = new ActivityInfo(aInfo); |
| 2234 | aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2235 | ProcessRecord app = getProcessRecordLocked(aInfo.processName, |
| 2236 | aInfo.applicationInfo.uid); |
| 2237 | if (app == null || app.instrumentationClass == null) { |
| 2238 | intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2239 | mMainStack.startActivityLocked(null, intent, null, aInfo, |
| 2240 | null, null, 0, 0, 0, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2241 | } |
| 2242 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2243 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2244 | return true; |
| 2245 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2246 | |
Amith Yamasani | 259d5e5 | 2012-08-31 15:11:01 -0700 | [diff] [blame] | 2247 | private ActivityInfo resolveActivityInfo(Intent intent, int flags, int userId) { |
| 2248 | ActivityInfo ai = null; |
| 2249 | ComponentName comp = intent.getComponent(); |
| 2250 | try { |
| 2251 | if (comp != null) { |
| 2252 | ai = AppGlobals.getPackageManager().getActivityInfo(comp, flags, userId); |
| 2253 | } else { |
| 2254 | ResolveInfo info = AppGlobals.getPackageManager().resolveIntent( |
| 2255 | intent, |
| 2256 | intent.resolveTypeIfNeeded(mContext.getContentResolver()), |
| 2257 | flags, userId); |
| 2258 | |
| 2259 | if (info != null) { |
| 2260 | ai = info.activityInfo; |
| 2261 | } |
| 2262 | } |
| 2263 | } catch (RemoteException e) { |
| 2264 | // ignore |
| 2265 | } |
| 2266 | |
| 2267 | return ai; |
| 2268 | } |
| 2269 | |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2270 | /** |
| 2271 | * Starts the "new version setup screen" if appropriate. |
| 2272 | */ |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2273 | void startSetupActivityLocked() { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2274 | // Only do this once per boot. |
| 2275 | if (mCheckedForSetup) { |
| 2276 | return; |
| 2277 | } |
| 2278 | |
| 2279 | // We will show this screen if the current one is a different |
| 2280 | // version than the last one shown, and we are not running in |
| 2281 | // low-level factory test mode. |
| 2282 | final ContentResolver resolver = mContext.getContentResolver(); |
| 2283 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL && |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2284 | Settings.Global.getInt(resolver, |
| 2285 | Settings.Global.DEVICE_PROVISIONED, 0) != 0) { |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2286 | mCheckedForSetup = true; |
| 2287 | |
| 2288 | // See if we should be showing the platform update setup UI. |
| 2289 | Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP); |
| 2290 | List<ResolveInfo> ris = mSelf.mContext.getPackageManager() |
| 2291 | .queryIntentActivities(intent, PackageManager.GET_META_DATA); |
| 2292 | |
| 2293 | // We don't allow third party apps to replace this. |
| 2294 | ResolveInfo ri = null; |
| 2295 | for (int i=0; ris != null && i<ris.size(); i++) { |
| 2296 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 2297 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2298 | ri = ris.get(i); |
| 2299 | break; |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | if (ri != null) { |
| 2304 | String vers = ri.activityInfo.metaData != null |
| 2305 | ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION) |
| 2306 | : null; |
| 2307 | if (vers == null && ri.activityInfo.applicationInfo.metaData != null) { |
| 2308 | vers = ri.activityInfo.applicationInfo.metaData.getString( |
| 2309 | Intent.METADATA_SETUP_VERSION); |
| 2310 | } |
| 2311 | String lastVers = Settings.Secure.getString( |
| 2312 | resolver, Settings.Secure.LAST_SETUP_SHOWN); |
| 2313 | if (vers != null && !vers.equals(lastVers)) { |
| 2314 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2315 | intent.setComponent(new ComponentName( |
| 2316 | ri.activityInfo.packageName, ri.activityInfo.name)); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2317 | mMainStack.startActivityLocked(null, intent, null, ri.activityInfo, |
| 2318 | null, null, 0, 0, 0, 0, null, false, null); |
Dianne Hackborn | d7cd29d | 2009-07-01 11:22:45 -0700 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | } |
| 2322 | } |
| 2323 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2324 | CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2325 | return mCompatModePackages.compatibilityInfoForPackageLocked(ai); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2326 | } |
| 2327 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2328 | void enforceNotIsolatedCaller(String caller) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2329 | if (UserHandle.isIsolated(Binder.getCallingUid())) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2330 | throw new SecurityException("Isolated process not allowed to call " + caller); |
| 2331 | } |
| 2332 | } |
| 2333 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2334 | public int getFrontActivityScreenCompatMode() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2335 | enforceNotIsolatedCaller("getFrontActivityScreenCompatMode"); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2336 | synchronized (this) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2337 | return mCompatModePackages.getFrontActivityScreenCompatModeLocked(); |
| 2338 | } |
| 2339 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2340 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2341 | public void setFrontActivityScreenCompatMode(int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2342 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2343 | "setFrontActivityScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2344 | synchronized (this) { |
| 2345 | mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode); |
| 2346 | } |
| 2347 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2348 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2349 | public int getPackageScreenCompatMode(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2350 | enforceNotIsolatedCaller("getPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2351 | synchronized (this) { |
| 2352 | return mCompatModePackages.getPackageScreenCompatModeLocked(packageName); |
| 2353 | } |
| 2354 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2355 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2356 | public void setPackageScreenCompatMode(String packageName, int mode) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2357 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2358 | "setPackageScreenCompatMode"); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 2359 | synchronized (this) { |
| 2360 | mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2361 | } |
| 2362 | } |
| 2363 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2364 | public boolean getPackageAskScreenCompat(String packageName) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2365 | enforceNotIsolatedCaller("getPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2366 | synchronized (this) { |
| 2367 | return mCompatModePackages.getPackageAskCompatModeLocked(packageName); |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | public void setPackageAskScreenCompat(String packageName, boolean ask) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2372 | enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY, |
| 2373 | "setPackageAskScreenCompat"); |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 2374 | synchronized (this) { |
| 2375 | mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask); |
| 2376 | } |
| 2377 | } |
| 2378 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2379 | void reportResumedActivityLocked(ActivityRecord r) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2380 | //Slog.i(TAG, "**** REPORT RESUME: " + r); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2381 | updateUsageStats(r, true); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2382 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2383 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2384 | private void dispatchProcessesChanged() { |
| 2385 | int N; |
| 2386 | synchronized (this) { |
| 2387 | N = mPendingProcessChanges.size(); |
| 2388 | if (mActiveProcessChanges.length < N) { |
| 2389 | mActiveProcessChanges = new ProcessChangeItem[N]; |
| 2390 | } |
| 2391 | mPendingProcessChanges.toArray(mActiveProcessChanges); |
| 2392 | mAvailProcessChanges.addAll(mPendingProcessChanges); |
| 2393 | mPendingProcessChanges.clear(); |
| 2394 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "*** Delivering " + N + " process changes"); |
| 2395 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2396 | int i = mProcessObservers.beginBroadcast(); |
| 2397 | while (i > 0) { |
| 2398 | i--; |
| 2399 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2400 | if (observer != null) { |
| 2401 | try { |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 2402 | for (int j=0; j<N; j++) { |
| 2403 | ProcessChangeItem item = mActiveProcessChanges[j]; |
| 2404 | if ((item.changes&ProcessChangeItem.CHANGE_ACTIVITIES) != 0) { |
| 2405 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "ACTIVITIES CHANGED pid=" |
| 2406 | + item.pid + " uid=" + item.uid + ": " |
| 2407 | + item.foregroundActivities); |
| 2408 | observer.onForegroundActivitiesChanged(item.pid, item.uid, |
| 2409 | item.foregroundActivities); |
| 2410 | } |
| 2411 | if ((item.changes&ProcessChangeItem.CHANGE_IMPORTANCE) != 0) { |
| 2412 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "IMPORTANCE CHANGED pid=" |
| 2413 | + item.pid + " uid=" + item.uid + ": " + item.importance); |
| 2414 | observer.onImportanceChanged(item.pid, item.uid, |
| 2415 | item.importance); |
| 2416 | } |
| 2417 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 2418 | } catch (RemoteException e) { |
| 2419 | } |
| 2420 | } |
| 2421 | } |
| 2422 | mProcessObservers.finishBroadcast(); |
| 2423 | } |
| 2424 | |
| 2425 | private void dispatchProcessDied(int pid, int uid) { |
| 2426 | int i = mProcessObservers.beginBroadcast(); |
| 2427 | while (i > 0) { |
| 2428 | i--; |
| 2429 | final IProcessObserver observer = mProcessObservers.getBroadcastItem(i); |
| 2430 | if (observer != null) { |
| 2431 | try { |
| 2432 | observer.onProcessDied(pid, uid); |
| 2433 | } catch (RemoteException e) { |
| 2434 | } |
| 2435 | } |
| 2436 | } |
| 2437 | mProcessObservers.finishBroadcast(); |
| 2438 | } |
| 2439 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2440 | final void doPendingActivityLaunchesLocked(boolean doResume) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2441 | final int N = mPendingActivityLaunches.size(); |
| 2442 | if (N <= 0) { |
| 2443 | return; |
| 2444 | } |
| 2445 | for (int i=0; i<N; i++) { |
| 2446 | PendingActivityLaunch pal = mPendingActivityLaunches.get(i); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2447 | mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 2448 | pal.startFlags, doResume && i == (N-1), null); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 2449 | } |
| 2450 | mPendingActivityLaunches.clear(); |
| 2451 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2452 | |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2453 | public final int startActivity(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2454 | Intent intent, String resolvedType, IBinder resultTo, |
| 2455 | String resultWho, int requestCode, int startFlags, |
| 2456 | String profileFile, ParcelFileDescriptor profileFd, Bundle options) { |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 2457 | return startActivityAsUser(caller, intent, resolvedType, resultTo, resultWho, requestCode, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2458 | startFlags, profileFile, profileFd, options, UserHandle.getCallingUserId()); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | public final int startActivityAsUser(IApplicationThread caller, |
| 2462 | Intent intent, String resolvedType, IBinder resultTo, |
| 2463 | String resultWho, int requestCode, int startFlags, |
| 2464 | String profileFile, ParcelFileDescriptor profileFd, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2465 | enforceNotIsolatedCaller("startActivity"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2466 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | d888399 | 2012-09-07 15:58:52 -0700 | [diff] [blame] | 2467 | false, true, "startActivity", null); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2468 | return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2469 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
| 2470 | null, null, options, userId); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | public final WaitResult startActivityAndWait(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2474 | Intent intent, String resolvedType, IBinder resultTo, |
| 2475 | String resultWho, int requestCode, int startFlags, String profileFile, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2476 | ParcelFileDescriptor profileFd, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2477 | enforceNotIsolatedCaller("startActivityAndWait"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2478 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2479 | false, true, "startActivityAndWait", null); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2480 | WaitResult res = new WaitResult(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2481 | mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2482 | resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2483 | res, null, options, UserHandle.getCallingUserId()); |
Dianne Hackborn | 8f7f35e | 2010-02-25 18:48:12 -0800 | [diff] [blame] | 2484 | return res; |
| 2485 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 2486 | |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2487 | public final int startActivityWithConfig(IApplicationThread caller, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2488 | Intent intent, String resolvedType, IBinder resultTo, |
| 2489 | String resultWho, int requestCode, int startFlags, Configuration config, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2490 | Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2491 | enforceNotIsolatedCaller("startActivityWithConfig"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2492 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2493 | false, true, "startActivityWithConfig", null); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2494 | int ret = mMainStack.startActivityMayWait(caller, -1, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2495 | resultTo, resultWho, requestCode, startFlags, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2496 | null, null, null, config, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2497 | return ret; |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 2498 | } |
| 2499 | |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2500 | public int startActivityIntentSender(IApplicationThread caller, |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2501 | IntentSender intent, Intent fillInIntent, String resolvedType, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2502 | IBinder resultTo, String resultWho, int requestCode, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2503 | int flagsMask, int flagsValues, Bundle options) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2504 | enforceNotIsolatedCaller("startActivityIntentSender"); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2505 | // Refuse possible leaked file descriptors |
| 2506 | if (fillInIntent != null && fillInIntent.hasFileDescriptors()) { |
| 2507 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2508 | } |
| 2509 | |
| 2510 | IIntentSender sender = intent.getTarget(); |
| 2511 | if (!(sender instanceof PendingIntentRecord)) { |
| 2512 | throw new IllegalArgumentException("Bad PendingIntent object"); |
| 2513 | } |
| 2514 | |
| 2515 | PendingIntentRecord pir = (PendingIntentRecord)sender; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2516 | |
| 2517 | synchronized (this) { |
| 2518 | // If this is coming from the currently resumed activity, it is |
| 2519 | // effectively saying that app switches are allowed at this point. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2520 | if (mMainStack.mResumedActivity != null |
| 2521 | && mMainStack.mResumedActivity.info.applicationInfo.uid == |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 2522 | Binder.getCallingUid()) { |
| 2523 | mAppSwitchesAllowedTime = 0; |
| 2524 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2525 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2526 | int ret = pir.sendInner(0, fillInIntent, resolvedType, null, null, |
| 2527 | resultTo, resultWho, requestCode, flagsMask, flagsValues, options); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2528 | return ret; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2529 | } |
| 2530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2531 | public boolean startNextMatchingActivity(IBinder callingActivity, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2532 | Intent intent, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2533 | // Refuse possible leaked file descriptors |
| 2534 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 2535 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2536 | } |
| 2537 | |
| 2538 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2539 | ActivityRecord r = mMainStack.isInStackLocked(callingActivity); |
| 2540 | if (r == null) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2541 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2542 | return false; |
| 2543 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2544 | if (r.app == null || r.app.thread == null) { |
| 2545 | // The caller is not running... d'oh! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2546 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2547 | return false; |
| 2548 | } |
| 2549 | intent = new Intent(intent); |
| 2550 | // The caller is not allowed to change the data. |
| 2551 | intent.setDataAndType(r.intent.getData(), r.intent.getType()); |
| 2552 | // And we are resetting to find the next component... |
| 2553 | intent.setComponent(null); |
| 2554 | |
| 2555 | ActivityInfo aInfo = null; |
| 2556 | try { |
| 2557 | List<ResolveInfo> resolves = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2558 | AppGlobals.getPackageManager().queryIntentActivities( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2559 | intent, r.resolvedType, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 2560 | PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2561 | UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2562 | |
| 2563 | // Look for the original activity in the list... |
| 2564 | final int N = resolves != null ? resolves.size() : 0; |
| 2565 | for (int i=0; i<N; i++) { |
| 2566 | ResolveInfo rInfo = resolves.get(i); |
| 2567 | if (rInfo.activityInfo.packageName.equals(r.packageName) |
| 2568 | && rInfo.activityInfo.name.equals(r.info.name)) { |
| 2569 | // We found the current one... the next matching is |
| 2570 | // after it. |
| 2571 | i++; |
| 2572 | if (i<N) { |
| 2573 | aInfo = resolves.get(i).activityInfo; |
| 2574 | } |
| 2575 | break; |
| 2576 | } |
| 2577 | } |
| 2578 | } catch (RemoteException e) { |
| 2579 | } |
| 2580 | |
| 2581 | if (aInfo == null) { |
| 2582 | // Nobody who is next! |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 2583 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2584 | return false; |
| 2585 | } |
| 2586 | |
| 2587 | intent.setComponent(new ComponentName( |
| 2588 | aInfo.applicationInfo.packageName, aInfo.name)); |
| 2589 | intent.setFlags(intent.getFlags()&~( |
| 2590 | Intent.FLAG_ACTIVITY_FORWARD_RESULT| |
| 2591 | Intent.FLAG_ACTIVITY_CLEAR_TOP| |
| 2592 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK| |
| 2593 | Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 2594 | |
| 2595 | // Okay now we need to start the new activity, replacing the |
| 2596 | // currently running activity. This is a little tricky because |
| 2597 | // we want to start the new one as if the current one is finished, |
| 2598 | // but not finish the current one first so that there is no flicker. |
| 2599 | // And thus... |
| 2600 | final boolean wasFinishing = r.finishing; |
| 2601 | r.finishing = true; |
| 2602 | |
| 2603 | // Propagate reply information over to the new activity. |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2604 | final ActivityRecord resultTo = r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2605 | final String resultWho = r.resultWho; |
| 2606 | final int requestCode = r.requestCode; |
| 2607 | r.resultTo = null; |
| 2608 | if (resultTo != null) { |
| 2609 | resultTo.removeResultsLocked(r, resultWho, requestCode); |
| 2610 | } |
| 2611 | |
| 2612 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2613 | int res = mMainStack.startActivityLocked(r.app.thread, intent, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2614 | r.resolvedType, aInfo, resultTo != null ? resultTo.appToken : null, |
| 2615 | resultWho, requestCode, -1, r.launchedFromUid, 0, |
| 2616 | options, false, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2617 | Binder.restoreCallingIdentity(origId); |
| 2618 | |
| 2619 | r.finishing = wasFinishing; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2620 | if (res != ActivityManager.START_SUCCESS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2621 | return false; |
| 2622 | } |
| 2623 | return true; |
| 2624 | } |
| 2625 | } |
| 2626 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2627 | final int startActivityInPackage(int uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2628 | Intent intent, String resolvedType, IBinder resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2629 | String resultWho, int requestCode, int startFlags, Bundle options, int userId) { |
| 2630 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2631 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2632 | false, true, "startActivityInPackage", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2633 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2634 | int ret = mMainStack.startActivityMayWait(null, uid, intent, resolvedType, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2635 | resultTo, resultWho, requestCode, startFlags, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2636 | null, null, null, null, options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2637 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | public final int startActivities(IApplicationThread caller, |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 2641 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle options, |
| 2642 | int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 2643 | enforceNotIsolatedCaller("startActivities"); |
Amith Yamasani | 9529386 | 2012-09-24 18:14:47 -0700 | [diff] [blame] | 2644 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 2645 | false, true, "startActivity", null); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2646 | int ret = mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo, |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 2647 | options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2648 | return ret; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2649 | } |
| 2650 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2651 | final int startActivitiesInPackage(int uid, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 2652 | Intent[] intents, String[] resolvedTypes, IBinder resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2653 | Bundle options, int userId) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 2654 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 2655 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2656 | false, true, "startActivityInPackage", null); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2657 | int ret = mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2658 | options, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2659 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2660 | } |
| 2661 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2662 | final void addRecentTaskLocked(TaskRecord task) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2663 | int N = mRecentTasks.size(); |
Dianne Hackborn | 7c0e75e | 2010-12-21 19:15:40 -0800 | [diff] [blame] | 2664 | // Quick case: check if the top-most recent task is the same. |
| 2665 | if (N > 0 && mRecentTasks.get(0) == task) { |
| 2666 | return; |
| 2667 | } |
| 2668 | // 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] | 2669 | for (int i=0; i<N; i++) { |
| 2670 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2671 | if (task.userId == tr.userId |
| 2672 | && ((task.affinity != null && task.affinity.equals(tr.affinity)) |
| 2673 | || (task.intent != null && task.intent.filterEquals(tr.intent)))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2674 | mRecentTasks.remove(i); |
| 2675 | i--; |
| 2676 | N--; |
| 2677 | if (task.intent == null) { |
| 2678 | // If the new recent task we are adding is not fully |
| 2679 | // specified, then replace it with the existing recent task. |
| 2680 | task = tr; |
| 2681 | } |
| 2682 | } |
| 2683 | } |
| 2684 | if (N >= MAX_RECENT_TASKS) { |
| 2685 | mRecentTasks.remove(N-1); |
| 2686 | } |
| 2687 | mRecentTasks.add(0, task); |
| 2688 | } |
| 2689 | |
| 2690 | public void setRequestedOrientation(IBinder token, |
| 2691 | int requestedOrientation) { |
| 2692 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2693 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2694 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2695 | return; |
| 2696 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2697 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2698 | mWindowManager.setAppOrientation(r.appToken, requestedOrientation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2699 | Configuration config = mWindowManager.updateOrientationFromAppTokens( |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2700 | mConfiguration, |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2701 | r.mayFreezeScreenLocked(r.app) ? r.appToken : null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2702 | if (config != null) { |
| 2703 | r.frozenBeforeDestroy = true; |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 2704 | if (!updateConfigurationLocked(config, r, false, false)) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2705 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2706 | } |
| 2707 | } |
| 2708 | Binder.restoreCallingIdentity(origId); |
| 2709 | } |
| 2710 | } |
| 2711 | |
| 2712 | public int getRequestedOrientation(IBinder token) { |
| 2713 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2714 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 2715 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2716 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 2717 | } |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2718 | return mWindowManager.getAppOrientation(r.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2719 | } |
| 2720 | } |
| 2721 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2722 | /** |
| 2723 | * This is the internal entry point for handling Activity.finish(). |
| 2724 | * |
| 2725 | * @param token The Binder token referencing the Activity we want to finish. |
| 2726 | * @param resultCode Result code, if any, from this Activity. |
| 2727 | * @param resultData Result data (Intent), if any, from this Activity. |
| 2728 | * |
Alexey Tarasov | 83bad3d | 2009-08-12 15:05:43 +1100 | [diff] [blame] | 2729 | * @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] | 2730 | */ |
| 2731 | public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) { |
| 2732 | // Refuse possible leaked file descriptors |
| 2733 | if (resultData != null && resultData.hasFileDescriptors() == true) { |
| 2734 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 2735 | } |
| 2736 | |
| 2737 | synchronized(this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2738 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2739 | // Find the first activity that is not finishing. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2740 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2741 | if (next != null) { |
| 2742 | // ask watcher if this is allowed |
| 2743 | boolean resumeOK = true; |
| 2744 | try { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2745 | resumeOK = mController.activityResuming(next.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2746 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2747 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | if (!resumeOK) { |
| 2751 | return false; |
| 2752 | } |
| 2753 | } |
| 2754 | } |
| 2755 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2756 | boolean res = mMainStack.requestFinishActivityLocked(token, resultCode, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 2757 | resultData, "app-request", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2758 | Binder.restoreCallingIdentity(origId); |
| 2759 | return res; |
| 2760 | } |
| 2761 | } |
| 2762 | |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2763 | public final void finishHeavyWeightApp() { |
| 2764 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2765 | != PackageManager.PERMISSION_GRANTED) { |
| 2766 | String msg = "Permission Denial: finishHeavyWeightApp() from pid=" |
| 2767 | + Binder.getCallingPid() |
| 2768 | + ", uid=" + Binder.getCallingUid() |
| 2769 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2770 | Slog.w(TAG, msg); |
| 2771 | throw new SecurityException(msg); |
| 2772 | } |
| 2773 | |
| 2774 | synchronized(this) { |
| 2775 | if (mHeavyWeightProcess == null) { |
| 2776 | return; |
| 2777 | } |
| 2778 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2779 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>( |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2780 | mHeavyWeightProcess.activities); |
| 2781 | for (int i=0; i<activities.size(); i++) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 2782 | ActivityRecord r = activities.get(i); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2783 | if (!r.finishing) { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2784 | int index = mMainStack.indexOfTokenLocked(r.appToken); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2785 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2786 | mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 2787 | null, "finish-heavy", true); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2788 | } |
| 2789 | } |
| 2790 | } |
| 2791 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2792 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 2793 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2794 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 2795 | } |
| 2796 | } |
| 2797 | |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2798 | public void crashApplication(int uid, int initialPid, String packageName, |
| 2799 | String message) { |
| 2800 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 2801 | != PackageManager.PERMISSION_GRANTED) { |
| 2802 | String msg = "Permission Denial: crashApplication() from pid=" |
| 2803 | + Binder.getCallingPid() |
| 2804 | + ", uid=" + Binder.getCallingUid() |
| 2805 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
| 2806 | Slog.w(TAG, msg); |
| 2807 | throw new SecurityException(msg); |
| 2808 | } |
| 2809 | |
| 2810 | synchronized(this) { |
| 2811 | ProcessRecord proc = null; |
| 2812 | |
| 2813 | // Figure out which process to kill. We don't trust that initialPid |
| 2814 | // still has any relation to current pids, so must scan through the |
| 2815 | // list. |
| 2816 | synchronized (mPidsSelfLocked) { |
| 2817 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 2818 | ProcessRecord p = mPidsSelfLocked.valueAt(i); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 2819 | if (p.uid != uid) { |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2820 | continue; |
| 2821 | } |
| 2822 | if (p.pid == initialPid) { |
| 2823 | proc = p; |
| 2824 | break; |
| 2825 | } |
| 2826 | for (String str : p.pkgList) { |
| 2827 | if (str.equals(packageName)) { |
| 2828 | proc = p; |
| 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | if (proc == null) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 2835 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2836 | + " initialPid=" + initialPid |
| 2837 | + " packageName=" + packageName); |
| 2838 | return; |
| 2839 | } |
| 2840 | |
| 2841 | if (proc.thread != null) { |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 2842 | if (proc.pid == Process.myPid()) { |
| 2843 | Log.w(TAG, "crashApplication: trying to crash self!"); |
| 2844 | return; |
| 2845 | } |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 2846 | long ident = Binder.clearCallingIdentity(); |
| 2847 | try { |
| 2848 | proc.thread.scheduleCrash(message); |
| 2849 | } catch (RemoteException e) { |
| 2850 | } |
| 2851 | Binder.restoreCallingIdentity(ident); |
| 2852 | } |
| 2853 | } |
| 2854 | } |
| 2855 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2856 | public final void finishSubActivity(IBinder token, String resultWho, |
| 2857 | int requestCode) { |
| 2858 | synchronized(this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2859 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2860 | mMainStack.finishSubActivityLocked(token, resultWho, requestCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2861 | Binder.restoreCallingIdentity(origId); |
| 2862 | } |
| 2863 | } |
| 2864 | |
Dianne Hackborn | ecc5a9c | 2012-04-26 18:56:09 -0700 | [diff] [blame] | 2865 | public boolean finishActivityAffinity(IBinder token) { |
| 2866 | synchronized(this) { |
| 2867 | final long origId = Binder.clearCallingIdentity(); |
| 2868 | boolean res = mMainStack.finishActivityAffinityLocked(token); |
| 2869 | Binder.restoreCallingIdentity(origId); |
| 2870 | return res; |
| 2871 | } |
| 2872 | } |
| 2873 | |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2874 | public boolean willActivityBeVisible(IBinder token) { |
| 2875 | synchronized(this) { |
| 2876 | int i; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2877 | for (i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 2878 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 2879 | if (r.appToken == token) { |
Dianne Hackborn | 061d58a | 2010-03-12 15:07:06 -0800 | [diff] [blame] | 2880 | return true; |
| 2881 | } |
| 2882 | if (r.fullscreen && !r.finishing) { |
| 2883 | return false; |
| 2884 | } |
| 2885 | } |
| 2886 | return true; |
| 2887 | } |
| 2888 | } |
| 2889 | |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2890 | public void overridePendingTransition(IBinder token, String packageName, |
| 2891 | int enterAnim, int exitAnim) { |
| 2892 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2893 | ActivityRecord self = mMainStack.isInStackLocked(token); |
| 2894 | if (self == null) { |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2895 | return; |
| 2896 | } |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2897 | |
| 2898 | final long origId = Binder.clearCallingIdentity(); |
| 2899 | |
| 2900 | if (self.state == ActivityState.RESUMED |
| 2901 | || self.state == ActivityState.PAUSING) { |
| 2902 | mWindowManager.overridePendingAppTransition(packageName, |
Dianne Hackborn | 8437587 | 2012-06-01 19:03:50 -0700 | [diff] [blame] | 2903 | enterAnim, exitAnim, null); |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | Binder.restoreCallingIdentity(origId); |
| 2907 | } |
| 2908 | } |
| 2909 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2910 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2911 | * Main function for removing an existing process from the activity manager |
| 2912 | * as a result of that process going away. Clears out all connections |
| 2913 | * to the process. |
| 2914 | */ |
| 2915 | private final void handleAppDiedLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 2916 | boolean restarting, boolean allowRestart) { |
| 2917 | cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2918 | if (!restarting) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 2919 | mLruProcesses.remove(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2920 | } |
| 2921 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 2922 | if (mProfileProc == app) { |
| 2923 | clearProfilerLocked(); |
| 2924 | } |
| 2925 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2926 | // Just in case... |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 2927 | if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) { |
| 2928 | if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity); |
| 2929 | mMainStack.mPausingActivity = null; |
| 2930 | } |
| 2931 | if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) { |
| 2932 | mMainStack.mLastPausedActivity = null; |
| 2933 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2934 | |
| 2935 | // Remove this application's activities from active lists. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2936 | mMainStack.removeHistoryRecordsForAppLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2937 | |
| 2938 | boolean atTop = true; |
| 2939 | boolean hasVisibleActivities = false; |
| 2940 | |
| 2941 | // Clean out the history list. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2942 | int i = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2943 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2944 | TAG, "Removing app " + app + " from history with " + i + " entries"); |
| 2945 | while (i > 0) { |
| 2946 | i--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 2947 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2948 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2949 | TAG, "Record #" + i + " " + r + ": app=" + r.app); |
| 2950 | if (r.app == app) { |
| 2951 | if ((!r.haveState && !r.stateNotNeeded) || r.finishing) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2952 | if (ActivityStack.DEBUG_ADD_REMOVE) { |
| 2953 | RuntimeException here = new RuntimeException("here"); |
| 2954 | here.fillInStackTrace(); |
| 2955 | Slog.i(TAG, "Removing activity " + r + " from stack at " + i |
| 2956 | + ": haveState=" + r.haveState |
| 2957 | + " stateNotNeeded=" + r.stateNotNeeded |
| 2958 | + " finishing=" + r.finishing |
| 2959 | + " state=" + r.state, here); |
| 2960 | } |
| 2961 | if (!r.finishing) { |
| 2962 | Slog.w(TAG, "Force removing " + r + ": app died, no saved state"); |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 2963 | EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 2964 | r.userId, System.identityHashCode(r), |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 2965 | r.task.taskId, r.shortComponentName, |
| 2966 | "proc died without state saved"); |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2967 | } |
Dianne Hackborn | 5c60743 | 2012-02-28 14:44:19 -0800 | [diff] [blame] | 2968 | mMainStack.removeActivityFromHistoryLocked(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2969 | |
| 2970 | } else { |
| 2971 | // We have the current state for this activity, so |
| 2972 | // it can be restarted later when needed. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2973 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2974 | TAG, "Keeping entry, setting app to null"); |
| 2975 | if (r.visible) { |
| 2976 | hasVisibleActivities = true; |
| 2977 | } |
| 2978 | r.app = null; |
| 2979 | r.nowVisible = false; |
| 2980 | if (!r.haveState) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 2981 | if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG, |
| 2982 | "App died, clearing saved state of " + r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2983 | r.icicle = null; |
| 2984 | } |
| 2985 | } |
| 2986 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 2987 | r.stack.cleanUpActivityLocked(r, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2988 | } |
| 2989 | atTop = false; |
| 2990 | } |
| 2991 | |
| 2992 | app.activities.clear(); |
| 2993 | |
| 2994 | if (app.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2995 | Slog.w(TAG, "Crash of app " + app.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2996 | + " running instrumentation " + app.instrumentationClass); |
| 2997 | Bundle info = new Bundle(); |
| 2998 | info.putString("shortMsg", "Process crashed."); |
| 2999 | finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info); |
| 3000 | } |
| 3001 | |
| 3002 | if (!restarting) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3003 | if (!mMainStack.resumeTopActivityLocked(null)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3004 | // If there was nothing to resume, and we are not already |
| 3005 | // restarting this process, but there is a visible activity that |
| 3006 | // is hosted by the process... then make sure all visible |
| 3007 | // activities are running, taking care of restarting this |
| 3008 | // process. |
| 3009 | if (hasVisibleActivities) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3010 | mMainStack.ensureActivitiesVisibleLocked(null, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3011 | } |
| 3012 | } |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | private final int getLRURecordIndexForAppLocked(IApplicationThread thread) { |
| 3017 | IBinder threadBinder = thread.asBinder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3018 | // Find the application record. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3019 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3020 | ProcessRecord rec = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3021 | if (rec.thread != null && rec.thread.asBinder() == threadBinder) { |
| 3022 | return i; |
| 3023 | } |
| 3024 | } |
| 3025 | return -1; |
| 3026 | } |
| 3027 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3028 | final ProcessRecord getRecordForAppLocked( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3029 | IApplicationThread thread) { |
| 3030 | if (thread == null) { |
| 3031 | return null; |
| 3032 | } |
| 3033 | |
| 3034 | int appIndex = getLRURecordIndexForAppLocked(thread); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3035 | return appIndex >= 0 ? mLruProcesses.get(appIndex) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3036 | } |
| 3037 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3038 | final void appDiedLocked(ProcessRecord app, int pid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3039 | IApplicationThread thread) { |
| 3040 | |
| 3041 | mProcDeaths[0]++; |
| 3042 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 3043 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 3044 | synchronized (stats) { |
| 3045 | stats.noteProcessDiedLocked(app.info.uid, pid); |
| 3046 | } |
| 3047 | |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3048 | // Clean up already done if the process has been re-started. |
| 3049 | if (app.pid == pid && app.thread != null && |
| 3050 | app.thread.asBinder() == thread.asBinder()) { |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 3051 | if (!app.killedBackground) { |
| 3052 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
| 3053 | + ") has died."); |
| 3054 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3055 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.userId, app.pid, app.processName); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3056 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3057 | TAG, "Dying app: " + app + ", pid: " + pid |
| 3058 | + ", thread: " + thread.asBinder()); |
| 3059 | boolean doLowMem = app.instrumentationClass == null; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 3060 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3061 | |
| 3062 | if (doLowMem) { |
| 3063 | // If there are no longer any background processes running, |
| 3064 | // and the app that died was not running instrumentation, |
| 3065 | // then tell everyone we are now low on memory. |
| 3066 | boolean haveBg = false; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3067 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3068 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 3069 | 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] | 3070 | haveBg = true; |
| 3071 | break; |
| 3072 | } |
| 3073 | } |
| 3074 | |
| 3075 | if (!haveBg) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3076 | EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size()); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3077 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 3078 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 3079 | ProcessRecord rec = mLruProcesses.get(i); |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 3080 | if (rec != app && rec.thread != null && |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3081 | (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) { |
| 3082 | // The low memory report is overriding any current |
| 3083 | // state for a GC request. Make sure to do |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 3084 | // heavy/important/visible/foreground processes first. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 3085 | if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3086 | rec.lastRequestedGc = 0; |
| 3087 | } else { |
| 3088 | rec.lastRequestedGc = rec.lastLowMemory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3089 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3090 | rec.reportLowMemory = true; |
| 3091 | rec.lastLowMemory = now; |
| 3092 | mProcessesToGc.remove(rec); |
| 3093 | addProcessToGcListLocked(rec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3094 | } |
| 3095 | } |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 3096 | mHandler.sendEmptyMessage(REPORT_MEM_USAGE); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 3097 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3098 | } |
| 3099 | } |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3100 | } else if (app.pid != pid) { |
| 3101 | // A new process has already been started. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3102 | Slog.i(TAG, "Process " + app.processName + " (pid " + pid |
Magnus Edlund | 7bb2581 | 2010-02-24 15:45:06 +0100 | [diff] [blame] | 3103 | + ") has died and restarted (pid " + app.pid + ")."); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3104 | EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.userId, app.pid, app.processName); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3105 | } else if (DEBUG_PROCESSES) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3106 | Slog.d(TAG, "Received spurious death notification for thread " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3107 | + thread.asBinder()); |
| 3108 | } |
| 3109 | } |
| 3110 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3111 | /** |
| 3112 | * If a stack trace dump file is configured, dump process stack traces. |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 3113 | * @param clearTraces causes the dump file to be erased prior to the new |
| 3114 | * traces being written, if true; when false, the new traces will be |
| 3115 | * appended to any existing file content. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3116 | * @param firstPids of dalvik VM processes to dump stack traces for first |
| 3117 | * @param lastPids of dalvik VM processes to dump stack traces for last |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3118 | * @param nativeProcs optional list of native process names to dump stack crawls |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3119 | * @return file containing stack traces, or null if no dump file is configured |
| 3120 | */ |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3121 | public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3122 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3123 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3124 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3125 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3126 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3127 | |
| 3128 | File tracesFile = new File(tracesPath); |
| 3129 | try { |
| 3130 | File tracesDir = tracesFile.getParentFile(); |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 3131 | if (!tracesDir.exists()) { |
| 3132 | tracesFile.mkdirs(); |
| 3133 | if (!SELinux.restorecon(tracesDir)) { |
| 3134 | return null; |
| 3135 | } |
| 3136 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3137 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3138 | |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 3139 | if (clearTraces && tracesFile.exists()) tracesFile.delete(); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3140 | tracesFile.createNewFile(); |
| 3141 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3142 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3143 | Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3144 | return null; |
| 3145 | } |
| 3146 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3147 | dumpStackTraces(tracesPath, firstPids, processStats, lastPids, nativeProcs); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3148 | return tracesFile; |
| 3149 | } |
| 3150 | |
| 3151 | private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3152 | ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3153 | // Use a FileObserver to detect when traces finish writing. |
| 3154 | // The order of traces is considered important to maintain for legibility. |
| 3155 | FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) { |
| 3156 | public synchronized void onEvent(int event, String path) { notify(); } |
| 3157 | }; |
| 3158 | |
| 3159 | try { |
| 3160 | observer.startWatching(); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3161 | |
| 3162 | // First collect all of the stacks of the most important pids. |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3163 | if (firstPids != null) { |
| 3164 | try { |
| 3165 | int num = firstPids.size(); |
| 3166 | for (int i = 0; i < num; i++) { |
| 3167 | synchronized (observer) { |
| 3168 | Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT); |
| 3169 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3170 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3171 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3172 | } catch (InterruptedException e) { |
| 3173 | Log.wtf(TAG, e); |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3174 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | // Next measure CPU usage. |
| 3178 | if (processStats != null) { |
| 3179 | processStats.init(); |
| 3180 | System.gc(); |
| 3181 | processStats.update(); |
| 3182 | try { |
| 3183 | synchronized (processStats) { |
| 3184 | processStats.wait(500); // measure over 1/2 second. |
| 3185 | } |
| 3186 | } catch (InterruptedException e) { |
| 3187 | } |
| 3188 | processStats.update(); |
| 3189 | |
| 3190 | // We'll take the stack crawls of just the top apps using CPU. |
| 3191 | final int N = processStats.countWorkingStats(); |
| 3192 | int numProcs = 0; |
| 3193 | for (int i=0; i<N && numProcs<5; i++) { |
| 3194 | ProcessStats.Stats stats = processStats.getWorkingStats(i); |
| 3195 | if (lastPids.indexOfKey(stats.pid) >= 0) { |
| 3196 | numProcs++; |
| 3197 | try { |
| 3198 | synchronized (observer) { |
| 3199 | Process.sendSignal(stats.pid, Process.SIGNAL_QUIT); |
| 3200 | observer.wait(200); // Wait for write-close, give up after 200msec |
| 3201 | } |
| 3202 | } catch (InterruptedException e) { |
| 3203 | Log.wtf(TAG, e); |
| 3204 | } |
| 3205 | |
| 3206 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3207 | } |
| 3208 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3209 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3210 | } finally { |
| 3211 | observer.stopWatching(); |
| 3212 | } |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3213 | |
| 3214 | if (nativeProcs != null) { |
| 3215 | int[] pids = Process.getPidsForCommands(nativeProcs); |
| 3216 | if (pids != null) { |
| 3217 | for (int pid : pids) { |
| 3218 | Debug.dumpNativeBacktraceToFile(pid, tracesPath); |
| 3219 | } |
| 3220 | } |
| 3221 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3222 | } |
| 3223 | |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3224 | final void logAppTooSlow(ProcessRecord app, long startTime, String msg) { |
Dianne Hackborn | 69dc66e | 2012-03-26 10:50:54 -0700 | [diff] [blame] | 3225 | if (true || IS_USER_BUILD) { |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3226 | return; |
| 3227 | } |
| 3228 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 3229 | if (tracesPath == null || tracesPath.length() == 0) { |
| 3230 | return; |
| 3231 | } |
| 3232 | |
| 3233 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 3234 | StrictMode.allowThreadDiskWrites(); |
| 3235 | try { |
| 3236 | final File tracesFile = new File(tracesPath); |
| 3237 | final File tracesDir = tracesFile.getParentFile(); |
| 3238 | final File tracesTmp = new File(tracesDir, "__tmp__"); |
| 3239 | try { |
rpcraig | ec7ed14c | 2012-07-25 13:10:37 -0400 | [diff] [blame] | 3240 | if (!tracesDir.exists()) { |
| 3241 | tracesFile.mkdirs(); |
| 3242 | if (!SELinux.restorecon(tracesDir.getPath())) { |
| 3243 | return; |
| 3244 | } |
| 3245 | } |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3246 | FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x |
| 3247 | |
| 3248 | if (tracesFile.exists()) { |
| 3249 | tracesTmp.delete(); |
| 3250 | tracesFile.renameTo(tracesTmp); |
| 3251 | } |
| 3252 | StringBuilder sb = new StringBuilder(); |
| 3253 | Time tobj = new Time(); |
| 3254 | tobj.set(System.currentTimeMillis()); |
| 3255 | sb.append(tobj.format("%Y-%m-%d %H:%M:%S")); |
| 3256 | sb.append(": "); |
| 3257 | TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb); |
| 3258 | sb.append(" since "); |
| 3259 | sb.append(msg); |
| 3260 | FileOutputStream fos = new FileOutputStream(tracesFile); |
| 3261 | fos.write(sb.toString().getBytes()); |
| 3262 | if (app == null) { |
| 3263 | fos.write("\n*** No application process!".getBytes()); |
| 3264 | } |
| 3265 | fos.close(); |
| 3266 | FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw- |
| 3267 | } catch (IOException e) { |
| 3268 | Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesPath, e); |
| 3269 | return; |
| 3270 | } |
| 3271 | |
| 3272 | if (app != null) { |
| 3273 | ArrayList<Integer> firstPids = new ArrayList<Integer>(); |
| 3274 | firstPids.add(app.pid); |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3275 | dumpStackTraces(tracesPath, firstPids, null, null, null); |
Dianne Hackborn | 2a29b3a | 2012-03-15 15:48:38 -0700 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | File lastTracesFile = null; |
| 3279 | File curTracesFile = null; |
| 3280 | for (int i=9; i>=0; i--) { |
| 3281 | String name = String.format("slow%02d.txt", i); |
| 3282 | curTracesFile = new File(tracesDir, name); |
| 3283 | if (curTracesFile.exists()) { |
| 3284 | if (lastTracesFile != null) { |
| 3285 | curTracesFile.renameTo(lastTracesFile); |
| 3286 | } else { |
| 3287 | curTracesFile.delete(); |
| 3288 | } |
| 3289 | } |
| 3290 | lastTracesFile = curTracesFile; |
| 3291 | } |
| 3292 | tracesFile.renameTo(curTracesFile); |
| 3293 | if (tracesTmp.exists()) { |
| 3294 | tracesTmp.renameTo(tracesFile); |
| 3295 | } |
| 3296 | } finally { |
| 3297 | StrictMode.setThreadPolicy(oldPolicy); |
| 3298 | } |
| 3299 | } |
| 3300 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3301 | final void appNotResponding(ProcessRecord app, ActivityRecord activity, |
| 3302 | ActivityRecord parent, final String annotation) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3303 | ArrayList<Integer> firstPids = new ArrayList<Integer>(5); |
| 3304 | SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20); |
| 3305 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3306 | if (mController != null) { |
| 3307 | try { |
| 3308 | // 0 == continue, -1 = kill process immediately |
| 3309 | int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation); |
| 3310 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3311 | } catch (RemoteException e) { |
| 3312 | mController = null; |
| 3313 | } |
| 3314 | } |
| 3315 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3316 | long anrTime = SystemClock.uptimeMillis(); |
| 3317 | if (MONITOR_CPU_USAGE) { |
| 3318 | updateCpuStatsNow(); |
| 3319 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3320 | |
| 3321 | synchronized (this) { |
| 3322 | // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. |
| 3323 | if (mShuttingDown) { |
| 3324 | Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation); |
| 3325 | return; |
| 3326 | } else if (app.notResponding) { |
| 3327 | Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation); |
| 3328 | return; |
| 3329 | } else if (app.crashing) { |
| 3330 | Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation); |
| 3331 | return; |
| 3332 | } |
| 3333 | |
| 3334 | // In case we come through here for the same app before completing |
| 3335 | // this one, mark as anring now so we will bail out. |
| 3336 | app.notResponding = true; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3337 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3338 | // Log the ANR to the event log. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3339 | EventLog.writeEvent(EventLogTags.AM_ANR, app.userId, app.pid, |
| 3340 | app.processName, app.info.flags, annotation); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3341 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3342 | // Dump thread traces as quickly as we can, starting with "interesting" processes. |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3343 | firstPids.add(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3344 | |
| 3345 | int parentPid = app.pid; |
| 3346 | 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] | 3347 | if (parentPid != app.pid) firstPids.add(parentPid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3348 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3349 | if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3350 | |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3351 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 3352 | ProcessRecord r = mLruProcesses.get(i); |
| 3353 | if (r != null && r.thread != null) { |
| 3354 | int pid = r.pid; |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3355 | if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) { |
| 3356 | if (r.persistent) { |
| 3357 | firstPids.add(pid); |
| 3358 | } else { |
| 3359 | lastPids.put(pid, Boolean.TRUE); |
| 3360 | } |
| 3361 | } |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3362 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3363 | } |
| 3364 | } |
| 3365 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3366 | // Log the ANR to the main log. |
Jeff Brown | deb6ed8 | 2012-04-10 14:26:26 -0700 | [diff] [blame] | 3367 | StringBuilder info = new StringBuilder(); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 3368 | info.setLength(0); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3369 | info.append("ANR in ").append(app.processName); |
| 3370 | if (activity != null && activity.shortComponentName != null) { |
| 3371 | info.append(" (").append(activity.shortComponentName).append(")"); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 3372 | } |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3373 | info.append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3374 | if (annotation != null) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3375 | info.append("Reason: ").append(annotation).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3376 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3377 | if (parent != null && parent != activity) { |
Eric Rowe | 6f4f619 | 2010-02-17 18:29:04 -0800 | [diff] [blame] | 3378 | info.append("Parent: ").append(parent.shortComponentName).append("\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3379 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3380 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3381 | final ProcessStats processStats = new ProcessStats(true); |
| 3382 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 3383 | File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 3384 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3385 | String cpuInfo = null; |
| 3386 | if (MONITOR_CPU_USAGE) { |
| 3387 | updateCpuStatsNow(); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3388 | synchronized (mProcessStatsThread) { |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3389 | cpuInfo = mProcessStats.printCurrentState(anrTime); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3390 | } |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3391 | info.append(processStats.printCurrentLoad()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3392 | info.append(cpuInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3393 | } |
| 3394 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 3395 | info.append(processStats.printCurrentState(anrTime)); |
| 3396 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3397 | Slog.e(TAG, info.toString()); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3398 | if (tracesFile == null) { |
| 3399 | // There is no trace file, so dump (only) the alleged culprit's threads to the log |
| 3400 | Process.sendSignal(app.pid, Process.SIGNAL_QUIT); |
| 3401 | } |
| 3402 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 3403 | addErrorToDropBox("anr", app, app.processName, activity, parent, annotation, |
| 3404 | cpuInfo, tracesFile, null); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3405 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3406 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3407 | try { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3408 | // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately |
| 3409 | 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] | 3410 | if (res != 0) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3411 | if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid); |
| 3412 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3413 | } |
| 3414 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3415 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3416 | } |
| 3417 | } |
| 3418 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3419 | // Unless configured otherwise, swallow ANRs in background processes & kill the process. |
| 3420 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 3421 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3422 | |
| 3423 | synchronized (this) { |
| 3424 | if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3425 | Slog.w(TAG, "Killing " + app + ": background ANR"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 3426 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 3427 | app.processName, app.setAdj, "background ANR"); |
| 3428 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 3429 | return; |
| 3430 | } |
| 3431 | |
| 3432 | // Set the app's notResponding state, and look up the errorReportReceiver |
| 3433 | makeAppNotRespondingLocked(app, |
| 3434 | activity != null ? activity.shortComponentName : null, |
| 3435 | annotation != null ? "ANR " + annotation : "ANR", |
| 3436 | info.toString()); |
| 3437 | |
| 3438 | // Bring up the infamous App Not Responding dialog |
| 3439 | Message msg = Message.obtain(); |
| 3440 | HashMap map = new HashMap(); |
| 3441 | msg.what = SHOW_NOT_RESPONDING_MSG; |
| 3442 | msg.obj = map; |
| 3443 | map.put("app", app); |
| 3444 | if (activity != null) { |
| 3445 | map.put("activity", activity); |
| 3446 | } |
| 3447 | |
| 3448 | mHandler.sendMessage(msg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 3449 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3450 | } |
| 3451 | |
Dianne Hackborn | 0dad364 | 2010-09-09 21:25:35 -0700 | [diff] [blame] | 3452 | final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) { |
| 3453 | if (!mLaunchWarningShown) { |
| 3454 | mLaunchWarningShown = true; |
| 3455 | mHandler.post(new Runnable() { |
| 3456 | @Override |
| 3457 | public void run() { |
| 3458 | synchronized (ActivityManagerService.this) { |
| 3459 | final Dialog d = new LaunchWarningWindow(mContext, cur, next); |
| 3460 | d.show(); |
| 3461 | mHandler.postDelayed(new Runnable() { |
| 3462 | @Override |
| 3463 | public void run() { |
| 3464 | synchronized (ActivityManagerService.this) { |
| 3465 | d.dismiss(); |
| 3466 | mLaunchWarningShown = false; |
| 3467 | } |
| 3468 | } |
| 3469 | }, 4000); |
| 3470 | } |
| 3471 | } |
| 3472 | }); |
| 3473 | } |
| 3474 | } |
| 3475 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3476 | public boolean clearApplicationUserData(final String packageName, |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3477 | final IPackageDataObserver observer, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3478 | enforceNotIsolatedCaller("clearApplicationUserData"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3479 | int uid = Binder.getCallingUid(); |
| 3480 | int pid = Binder.getCallingPid(); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3481 | userId = handleIncomingUser(pid, uid, |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3482 | userId, false, true, "clearApplicationUserData", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3483 | long callingId = Binder.clearCallingIdentity(); |
| 3484 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3485 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3486 | int pkgUid = -1; |
| 3487 | synchronized(this) { |
| 3488 | try { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3489 | pkgUid = pm.getPackageUid(packageName, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3490 | } catch (RemoteException e) { |
| 3491 | } |
| 3492 | if (pkgUid == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3493 | Slog.w(TAG, "Invalid packageName:" + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3494 | return false; |
| 3495 | } |
| 3496 | if (uid == pkgUid || checkComponentPermission( |
| 3497 | android.Manifest.permission.CLEAR_APP_USER_DATA, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 3498 | pid, uid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3499 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3500 | forceStopPackageLocked(packageName, pkgUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3501 | } else { |
| 3502 | throw new SecurityException(pid+" does not have permission:"+ |
| 3503 | android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" + |
| 3504 | "for process:"+packageName); |
| 3505 | } |
| 3506 | } |
| 3507 | |
| 3508 | try { |
| 3509 | //clear application user data |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3510 | pm.clearApplicationUserData(packageName, observer, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3511 | Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED, |
| 3512 | Uri.fromParts("package", packageName, null)); |
| 3513 | intent.putExtra(Intent.EXTRA_UID, pkgUid); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3514 | broadcastIntentInPackage("android", Process.SYSTEM_UID, intent, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3515 | null, null, 0, null, null, null, false, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3516 | } catch (RemoteException e) { |
| 3517 | } |
| 3518 | } finally { |
| 3519 | Binder.restoreCallingIdentity(callingId); |
| 3520 | } |
| 3521 | return true; |
| 3522 | } |
| 3523 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3524 | public void killBackgroundProcesses(final String packageName, int userId) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3525 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3526 | != PackageManager.PERMISSION_GRANTED && |
| 3527 | checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES) |
| 3528 | != PackageManager.PERMISSION_GRANTED) { |
| 3529 | String msg = "Permission Denial: killBackgroundProcesses() from pid=" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3530 | + Binder.getCallingPid() |
| 3531 | + ", uid=" + Binder.getCallingUid() |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3532 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3533 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3534 | throw new SecurityException(msg); |
| 3535 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3536 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3537 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3538 | userId, true, true, "killBackgroundProcesses", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3539 | long callingId = Binder.clearCallingIdentity(); |
| 3540 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3541 | IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3542 | synchronized(this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3543 | int appId = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3544 | try { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3545 | appId = UserHandle.getAppId(pm.getPackageUid(packageName, 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3546 | } catch (RemoteException e) { |
| 3547 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3548 | if (appId == -1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3549 | Slog.w(TAG, "Invalid packageName: " + packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3550 | return; |
| 3551 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3552 | killPackageProcessesLocked(packageName, appId, userId, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3553 | ProcessList.SERVICE_ADJ, false, true, true, false, "kill background"); |
| 3554 | } |
| 3555 | } finally { |
| 3556 | Binder.restoreCallingIdentity(callingId); |
| 3557 | } |
| 3558 | } |
| 3559 | |
| 3560 | public void killAllBackgroundProcesses() { |
| 3561 | if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES) |
| 3562 | != PackageManager.PERMISSION_GRANTED) { |
| 3563 | String msg = "Permission Denial: killAllBackgroundProcesses() from pid=" |
| 3564 | + Binder.getCallingPid() |
| 3565 | + ", uid=" + Binder.getCallingUid() |
| 3566 | + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES; |
| 3567 | Slog.w(TAG, msg); |
| 3568 | throw new SecurityException(msg); |
| 3569 | } |
| 3570 | |
| 3571 | long callingId = Binder.clearCallingIdentity(); |
| 3572 | try { |
| 3573 | synchronized(this) { |
| 3574 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
| 3575 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3576 | final int NA = apps.size(); |
| 3577 | for (int ia=0; ia<NA; ia++) { |
| 3578 | ProcessRecord app = apps.valueAt(ia); |
| 3579 | if (app.persistent) { |
| 3580 | // we don't kill persistent processes |
| 3581 | continue; |
| 3582 | } |
| 3583 | if (app.removed) { |
| 3584 | procs.add(app); |
| 3585 | } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 3586 | app.removed = true; |
| 3587 | procs.add(app); |
| 3588 | } |
| 3589 | } |
| 3590 | } |
| 3591 | |
| 3592 | int N = procs.size(); |
| 3593 | for (int i=0; i<N; i++) { |
| 3594 | removeProcessLocked(procs.get(i), false, true, "kill all background"); |
| 3595 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3596 | } |
| 3597 | } finally { |
| 3598 | Binder.restoreCallingIdentity(callingId); |
| 3599 | } |
| 3600 | } |
| 3601 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3602 | public void forceStopPackage(final String packageName, int userId) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3603 | if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES) |
| 3604 | != PackageManager.PERMISSION_GRANTED) { |
| 3605 | String msg = "Permission Denial: forceStopPackage() from pid=" |
| 3606 | + Binder.getCallingPid() |
| 3607 | + ", uid=" + Binder.getCallingUid() |
| 3608 | + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3609 | Slog.w(TAG, msg); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3610 | throw new SecurityException(msg); |
| 3611 | } |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 3612 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3613 | userId, true, true, "forceStopPackage", null); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3614 | long callingId = Binder.clearCallingIdentity(); |
| 3615 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 3616 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3617 | synchronized(this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3618 | int[] users = userId == UserHandle.USER_ALL |
| 3619 | ? getUsersLocked() : new int[] { userId }; |
| 3620 | for (int user : users) { |
| 3621 | int pkgUid = -1; |
| 3622 | try { |
| 3623 | pkgUid = pm.getPackageUid(packageName, user); |
| 3624 | } catch (RemoteException e) { |
| 3625 | } |
| 3626 | if (pkgUid == -1) { |
| 3627 | Slog.w(TAG, "Invalid packageName: " + packageName); |
| 3628 | continue; |
| 3629 | } |
| 3630 | try { |
| 3631 | pm.setPackageStoppedState(packageName, true, user); |
| 3632 | } catch (RemoteException e) { |
| 3633 | } catch (IllegalArgumentException e) { |
| 3634 | Slog.w(TAG, "Failed trying to unstop package " |
| 3635 | + packageName + ": " + e); |
| 3636 | } |
| 3637 | if (isUserRunningLocked(user)) { |
| 3638 | forceStopPackageLocked(packageName, pkgUid); |
| 3639 | } |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 3640 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3641 | } |
| 3642 | } finally { |
| 3643 | Binder.restoreCallingIdentity(callingId); |
| 3644 | } |
| 3645 | } |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3646 | |
| 3647 | /* |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3648 | * The pkg name and app id have to be specified. |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3649 | */ |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3650 | public void killApplicationWithAppId(String pkg, int appid) { |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3651 | if (pkg == null) { |
| 3652 | return; |
| 3653 | } |
| 3654 | // Make sure the uid is valid. |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3655 | if (appid < 0) { |
| 3656 | Slog.w(TAG, "Invalid appid specified for pkg : " + pkg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3657 | return; |
| 3658 | } |
| 3659 | int callerUid = Binder.getCallingUid(); |
| 3660 | // Only the system server can kill an application |
| 3661 | if (callerUid == Process.SYSTEM_UID) { |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3662 | // Post an aysnc message to kill the application |
| 3663 | Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3664 | msg.arg1 = appid; |
Suchi Amalapurapu | d9d2576 | 2009-08-17 16:57:03 -0700 | [diff] [blame] | 3665 | msg.arg2 = 0; |
| 3666 | msg.obj = pkg; |
Suchi Amalapurapu | d50066f | 2009-08-18 16:57:41 -0700 | [diff] [blame] | 3667 | mHandler.sendMessage(msg); |
Suchi Amalapurapu | 261e66a | 2009-07-27 15:21:34 -0700 | [diff] [blame] | 3668 | } else { |
| 3669 | throw new SecurityException(callerUid + " cannot kill pkg: " + |
| 3670 | pkg); |
| 3671 | } |
| 3672 | } |
| 3673 | |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3674 | public void closeSystemDialogs(String reason) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3675 | enforceNotIsolatedCaller("closeSystemDialogs"); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3676 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3677 | final int pid = Binder.getCallingPid(); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3678 | final int uid = Binder.getCallingUid(); |
| 3679 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3680 | try { |
| 3681 | synchronized (this) { |
| 3682 | // Only allow this from foreground processes, so that background |
| 3683 | // applications can't abuse it to prevent system UI from being shown. |
| 3684 | if (uid >= Process.FIRST_APPLICATION_UID) { |
| 3685 | ProcessRecord proc; |
| 3686 | synchronized (mPidsSelfLocked) { |
| 3687 | proc = mPidsSelfLocked.get(pid); |
| 3688 | } |
| 3689 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 3690 | Slog.w(TAG, "Ignoring closeSystemDialogs " + reason |
| 3691 | + " from background process " + proc); |
| 3692 | return; |
| 3693 | } |
| 3694 | } |
| 3695 | closeSystemDialogsLocked(reason); |
| 3696 | } |
| 3697 | } finally { |
| 3698 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3699 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3700 | } |
| 3701 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3702 | void closeSystemDialogsLocked(String reason) { |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3703 | Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 3704 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 3705 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3706 | if (reason != null) { |
| 3707 | intent.putExtra("reason", reason); |
| 3708 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3709 | mWindowManager.closeSystemDialogs(reason); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3710 | |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3711 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 3712 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 3713 | if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) { |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3714 | r.stack.finishActivityLocked(r, i, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 3715 | Activity.RESULT_CANCELED, null, "close-sys", true); |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3716 | } |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3717 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3718 | |
Dianne Hackborn | b8839dd | 2012-09-04 10:55:44 -0700 | [diff] [blame] | 3719 | broadcastIntentLocked(null, null, intent, null, |
| 3720 | null, 0, null, null, null, false, false, -1, |
| 3721 | Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | a6ddc8a | 2009-07-28 17:49:55 -0700 | [diff] [blame] | 3722 | } |
Dianne Hackborn | e302a16 | 2012-05-15 14:58:32 -0700 | [diff] [blame] | 3723 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3724 | public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids) |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3725 | throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3726 | enforceNotIsolatedCaller("getProcessMemoryInfo"); |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3727 | Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length]; |
| 3728 | for (int i=pids.length-1; i>=0; i--) { |
| 3729 | infos[i] = new Debug.MemoryInfo(); |
| 3730 | Debug.getMemoryInfo(pids[i], infos[i]); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3731 | } |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 3732 | return infos; |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 3733 | } |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3734 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3735 | public long[] getProcessPss(int[] pids) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 3736 | enforceNotIsolatedCaller("getProcessPss"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 3737 | long[] pss = new long[pids.length]; |
| 3738 | for (int i=pids.length-1; i>=0; i--) { |
| 3739 | pss[i] = Debug.getPss(pids[i]); |
| 3740 | } |
| 3741 | return pss; |
| 3742 | } |
| 3743 | |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3744 | public void killApplicationProcess(String processName, int uid) { |
| 3745 | if (processName == null) { |
| 3746 | return; |
| 3747 | } |
| 3748 | |
| 3749 | int callerUid = Binder.getCallingUid(); |
| 3750 | // Only the system server can kill an application |
| 3751 | if (callerUid == Process.SYSTEM_UID) { |
| 3752 | synchronized (this) { |
| 3753 | ProcessRecord app = getProcessRecordLocked(processName, uid); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 3754 | if (app != null && app.thread != null) { |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3755 | try { |
| 3756 | app.thread.scheduleSuicide(); |
| 3757 | } catch (RemoteException e) { |
| 3758 | // If the other end already died, then our work here is done. |
| 3759 | } |
| 3760 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3761 | Slog.w(TAG, "Process/uid not found attempting kill of " |
Christopher Tate | 5e1ab33 | 2009-09-01 20:32:49 -0700 | [diff] [blame] | 3762 | + processName + " / " + uid); |
| 3763 | } |
| 3764 | } |
| 3765 | } else { |
| 3766 | throw new SecurityException(callerUid + " cannot kill app process: " + |
| 3767 | processName); |
| 3768 | } |
| 3769 | } |
| 3770 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3771 | private void forceStopPackageLocked(final String packageName, int uid) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3772 | forceStopPackageLocked(packageName, UserHandle.getAppId(uid), false, |
| 3773 | false, true, false, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3774 | Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED, |
| 3775 | Uri.fromParts("package", packageName, null)); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3776 | if (!mProcessesReady) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 3777 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 3778 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 3779 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3780 | intent.putExtra(Intent.EXTRA_UID, uid); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 3781 | intent.putExtra(Intent.EXTRA_USER_HANDLE, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3782 | broadcastIntentLocked(null, null, intent, |
| 3783 | null, null, 0, null, null, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 3784 | false, false, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3785 | MY_PID, Process.SYSTEM_UID, UserHandle.getUserId(uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3786 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3787 | |
| 3788 | private void forceStopUserLocked(int userId) { |
| 3789 | forceStopPackageLocked(null, -1, false, false, true, false, userId); |
| 3790 | Intent intent = new Intent(Intent.ACTION_USER_STOPPED); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 3791 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 3792 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3793 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 3794 | broadcastIntentLocked(null, null, intent, |
| 3795 | null, null, 0, null, null, null, |
| 3796 | false, false, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 3797 | MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3798 | } |
| 3799 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3800 | private final boolean killPackageProcessesLocked(String packageName, int appId, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3801 | int userId, int minOomAdj, boolean callerWillRestart, boolean allowRestart, |
| 3802 | boolean doit, boolean evenPersistent, String reason) { |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3803 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3804 | |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3805 | // Remove all processes this package may have touched: all with the |
| 3806 | // same UID (except for the system or root user), and all whose name |
| 3807 | // matches the package name. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3808 | final String procNamePrefix = packageName != null ? (packageName + ":") : null; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3809 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 3810 | final int NA = apps.size(); |
| 3811 | for (int ia=0; ia<NA; ia++) { |
| 3812 | ProcessRecord app = apps.valueAt(ia); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3813 | if (app.persistent && !evenPersistent) { |
Christopher Tate | 064d842 | 2011-07-26 15:38:07 -0700 | [diff] [blame] | 3814 | // we don't kill persistent processes |
| 3815 | continue; |
| 3816 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3817 | if (app.removed) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3818 | if (doit) { |
| 3819 | procs.add(app); |
| 3820 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3821 | continue; |
| 3822 | } |
| 3823 | |
| 3824 | // Skip process if it doesn't meet our oom adj requirement. |
| 3825 | if (app.setAdj < minOomAdj) { |
| 3826 | continue; |
| 3827 | } |
| 3828 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3829 | // If no package is specified, we call all processes under the |
| 3830 | // give user id. |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3831 | if (packageName == null) { |
| 3832 | if (app.userId != userId) { |
| 3833 | continue; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3834 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3835 | // Package has been specified, we want to hit all processes |
| 3836 | // that match it. We need to qualify this by the processes |
| 3837 | // that are running under the specified app and user ID. |
| 3838 | } else { |
| 3839 | if (UserHandle.getAppId(app.uid) != appId) { |
| 3840 | continue; |
| 3841 | } |
| 3842 | if (userId != UserHandle.USER_ALL && app.userId != userId) { |
| 3843 | continue; |
| 3844 | } |
| 3845 | if (!app.pkgList.contains(packageName)) { |
| 3846 | continue; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3847 | } |
| 3848 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3849 | |
| 3850 | // Process has passed all conditions, kill it! |
| 3851 | if (!doit) { |
| 3852 | return true; |
| 3853 | } |
| 3854 | app.removed = true; |
| 3855 | procs.add(app); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3856 | } |
| 3857 | } |
| 3858 | |
| 3859 | int N = procs.size(); |
| 3860 | for (int i=0; i<N; i++) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 3861 | removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason); |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3862 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3863 | return N > 0; |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3864 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 3865 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3866 | private final boolean forceStopPackageLocked(String name, int appId, |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3867 | boolean callerWillRestart, boolean purgeCache, boolean doit, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 3868 | boolean evenPersistent, int userId) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 3869 | int i; |
| 3870 | int N; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3871 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3872 | if (userId == UserHandle.USER_ALL && name == null) { |
| 3873 | Slog.w(TAG, "Can't force stop all processes of all users, that is insane!"); |
| 3874 | } |
| 3875 | |
| 3876 | if (appId < 0 && name != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3877 | try { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3878 | appId = UserHandle.getAppId( |
| 3879 | AppGlobals.getPackageManager().getPackageUid(name, 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3880 | } catch (RemoteException e) { |
| 3881 | } |
| 3882 | } |
| 3883 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3884 | if (doit) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3885 | if (name != null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3886 | Slog.i(TAG, "Force stopping package " + name + " appid=" + appId |
| 3887 | + " user=" + userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3888 | } else { |
| 3889 | Slog.i(TAG, "Force stopping user " + userId); |
| 3890 | } |
Dianne Hackborn | 03abb81 | 2010-01-04 18:43:19 -0800 | [diff] [blame] | 3891 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3892 | Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator(); |
| 3893 | while (badApps.hasNext()) { |
| 3894 | SparseArray<Long> ba = badApps.next(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3895 | for (i=ba.size()-1; i>=0; i--) { |
| 3896 | boolean remove = false; |
| 3897 | final int entUid = ba.keyAt(i); |
| 3898 | if (name != null) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3899 | if (userId == UserHandle.USER_ALL) { |
| 3900 | if (UserHandle.getAppId(entUid) == appId) { |
| 3901 | remove = true; |
| 3902 | } |
| 3903 | } else { |
| 3904 | if (entUid == UserHandle.getUid(userId, appId)) { |
| 3905 | remove = true; |
| 3906 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3907 | } |
| 3908 | } else if (UserHandle.getUserId(entUid) == userId) { |
| 3909 | remove = true; |
| 3910 | } |
| 3911 | if (remove) { |
| 3912 | ba.removeAt(i); |
| 3913 | } |
| 3914 | } |
| 3915 | if (ba.size() == 0) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3916 | badApps.remove(); |
| 3917 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3918 | } |
| 3919 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3920 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3921 | boolean didSomething = killPackageProcessesLocked(name, appId, userId, |
| 3922 | -100, callerWillRestart, false, doit, evenPersistent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3923 | name == null ? ("force stop user " + userId) : ("force stop " + name)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3924 | |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3925 | TaskRecord lastTask = null; |
| 3926 | for (i=0; i<mMainStack.mHistory.size(); i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 3927 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 3928 | final boolean samePackage = r.packageName.equals(name) |
| 3929 | || (name == null && r.userId == userId); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3930 | if ((userId == UserHandle.USER_ALL || r.userId == userId) |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 3931 | && (samePackage || r.task == lastTask) |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 3932 | && (r.app == null || evenPersistent || !r.app.persistent)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3933 | if (!doit) { |
Dianne Hackborn | 80a7ac1 | 2011-09-22 18:32:52 -0700 | [diff] [blame] | 3934 | if (r.finishing) { |
| 3935 | // If this activity is just finishing, then it is not |
| 3936 | // interesting as far as something to stop. |
| 3937 | continue; |
| 3938 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3939 | return true; |
| 3940 | } |
| 3941 | didSomething = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3942 | Slog.i(TAG, " Force finishing activity " + r); |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3943 | if (samePackage) { |
| 3944 | if (r.app != null) { |
| 3945 | r.app.removed = true; |
| 3946 | } |
| 3947 | r.app = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3948 | } |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3949 | lastTask = r.task; |
| 3950 | if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 3951 | null, "force-stop", true)) { |
Dianne Hackborn | e5ba16a | 2011-09-09 12:35:29 -0700 | [diff] [blame] | 3952 | i--; |
| 3953 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3954 | } |
| 3955 | } |
| 3956 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3957 | if (mServices.forceStopLocked(name, userId, evenPersistent, doit)) { |
| 3958 | if (!doit) { |
| 3959 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3960 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3961 | didSomething = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3962 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3963 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3964 | if (name == null) { |
| 3965 | // Remove all sticky broadcasts from this user. |
| 3966 | mStickyBroadcasts.remove(userId); |
| 3967 | } |
| 3968 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 3969 | ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>(); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 3970 | if (mProviderMap.collectForceStopProviders(name, appId, doit, evenPersistent, |
| 3971 | userId, providers)) { |
| 3972 | if (!doit) { |
| 3973 | return true; |
| 3974 | } |
| 3975 | didSomething = true; |
| 3976 | } |
| 3977 | N = providers.size(); |
| 3978 | for (i=0; i<N; i++) { |
| 3979 | removeDyingProviderLocked(null, providers.get(i), true); |
| 3980 | } |
| 3981 | |
| 3982 | if (mIntentSenderRecords.size() > 0) { |
| 3983 | Iterator<WeakReference<PendingIntentRecord>> it |
| 3984 | = mIntentSenderRecords.values().iterator(); |
| 3985 | while (it.hasNext()) { |
| 3986 | WeakReference<PendingIntentRecord> wpir = it.next(); |
| 3987 | if (wpir == null) { |
| 3988 | it.remove(); |
| 3989 | continue; |
| 3990 | } |
| 3991 | PendingIntentRecord pir = wpir.get(); |
| 3992 | if (pir == null) { |
| 3993 | it.remove(); |
| 3994 | continue; |
| 3995 | } |
| 3996 | if (name == null) { |
| 3997 | // Stopping user, remove all objects for the user. |
| 3998 | if (pir.key.userId != userId) { |
| 3999 | // Not the same user, skip it. |
| 4000 | continue; |
| 4001 | } |
| 4002 | } else { |
| 4003 | if (UserHandle.getAppId(pir.uid) != appId) { |
| 4004 | // Different app id, skip it. |
| 4005 | continue; |
| 4006 | } |
| 4007 | if (userId != UserHandle.USER_ALL && pir.key.userId != userId) { |
| 4008 | // Different user, skip it. |
| 4009 | continue; |
| 4010 | } |
| 4011 | if (!pir.key.packageName.equals(name)) { |
| 4012 | // Different package, skip it. |
| 4013 | continue; |
| 4014 | } |
| 4015 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 4016 | if (!doit) { |
| 4017 | return true; |
| 4018 | } |
| 4019 | didSomething = true; |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 4020 | it.remove(); |
| 4021 | pir.canceled = true; |
| 4022 | if (pir.key.activity != null) { |
| 4023 | pir.key.activity.pendingResults.remove(pir.ref); |
| 4024 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 4025 | } |
| 4026 | } |
| 4027 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4028 | if (doit) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4029 | if (purgeCache && name != null) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4030 | AttributeCache ac = AttributeCache.instance(); |
| 4031 | if (ac != null) { |
| 4032 | ac.removePackage(name); |
| 4033 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4034 | } |
Dianne Hackborn | 38cc896 | 2011-10-13 11:33:55 -0700 | [diff] [blame] | 4035 | if (mBooted) { |
| 4036 | mMainStack.resumeTopActivityLocked(null); |
| 4037 | mMainStack.scheduleIdleLocked(); |
| 4038 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 4039 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4040 | |
| 4041 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4042 | } |
| 4043 | |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4044 | private final boolean removeProcessLocked(ProcessRecord app, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4045 | boolean callerWillRestart, boolean allowRestart, String reason) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4046 | final String name = app.processName; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4047 | final int uid = app.uid; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4048 | if (DEBUG_PROCESSES) Slog.d( |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4049 | TAG, "Force removing proc " + app.toShortString() + " (" + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4050 | + "/" + uid + ")"); |
| 4051 | |
| 4052 | mProcessNames.remove(name, uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4053 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4054 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4055 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 4056 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4057 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4058 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4059 | boolean needRestart = false; |
| 4060 | if (app.pid > 0 && app.pid != MY_PID) { |
| 4061 | int pid = app.pid; |
| 4062 | synchronized (mPidsSelfLocked) { |
| 4063 | mPidsSelfLocked.remove(pid); |
| 4064 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 4065 | } |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4066 | Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4067 | handleAppDiedLocked(app, true, allowRestart); |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 4068 | mLruProcesses.remove(app); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 4069 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4070 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4071 | if (app.persistent && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4072 | if (!callerWillRestart) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4073 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4074 | } else { |
| 4075 | needRestart = true; |
| 4076 | } |
| 4077 | } |
| 4078 | } else { |
| 4079 | mRemovedProcesses.add(app); |
| 4080 | } |
| 4081 | |
| 4082 | return needRestart; |
| 4083 | } |
| 4084 | |
| 4085 | private final void processStartTimedOutLocked(ProcessRecord app) { |
| 4086 | final int pid = app.pid; |
| 4087 | boolean gone = false; |
| 4088 | synchronized (mPidsSelfLocked) { |
| 4089 | ProcessRecord knownApp = mPidsSelfLocked.get(pid); |
| 4090 | if (knownApp != null && knownApp.thread == null) { |
| 4091 | mPidsSelfLocked.remove(pid); |
| 4092 | gone = true; |
| 4093 | } |
| 4094 | } |
| 4095 | |
| 4096 | if (gone) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4097 | Slog.w(TAG, "Process " + app + " failed to attach"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4098 | EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, app.userId, |
| 4099 | pid, app.uid, app.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4100 | mProcessNames.remove(app.processName, app.uid); |
| 4101 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4102 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4103 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 4104 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4105 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4106 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 4107 | // Take care of any launching providers waiting for this process. |
| 4108 | checkAppInLaunchingProvidersLocked(app, true); |
| 4109 | // Take care of any services that are waiting for the process. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4110 | mServices.processStartTimedOutLocked(app); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4111 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, pid, |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4112 | app.processName, app.setAdj, "start timeout"); |
| 4113 | Process.killProcessQuiet(pid); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4114 | if (mBackupTarget != null && mBackupTarget.app.pid == pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4115 | Slog.w(TAG, "Unattached app died before backup, skipping"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4116 | try { |
| 4117 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 4118 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 4119 | bm.agentDisconnected(app.info.packageName); |
| 4120 | } catch (RemoteException e) { |
| 4121 | // Can't happen; the backup manager is local |
| 4122 | } |
| 4123 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4124 | if (isPendingBroadcastProcessLocked(pid)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4125 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4126 | skipPendingBroadcastLocked(pid); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 4127 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4128 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4129 | 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] | 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | private final boolean attachApplicationLocked(IApplicationThread thread, |
| 4134 | int pid) { |
| 4135 | |
| 4136 | // Find the application record that is being attached... either via |
| 4137 | // the pid if we are running in multiple processes, or just pull the |
| 4138 | // next app record if we are emulating process with anonymous threads. |
| 4139 | ProcessRecord app; |
| 4140 | if (pid != MY_PID && pid >= 0) { |
| 4141 | synchronized (mPidsSelfLocked) { |
| 4142 | app = mPidsSelfLocked.get(pid); |
| 4143 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4144 | } else { |
| 4145 | app = null; |
| 4146 | } |
| 4147 | |
| 4148 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4149 | 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] | 4150 | + " (IApplicationThread " + thread + "); dropping process"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4151 | EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4152 | if (pid > 0 && pid != MY_PID) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 4153 | Process.killProcessQuiet(pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4154 | } else { |
| 4155 | try { |
| 4156 | thread.scheduleExit(); |
| 4157 | } catch (Exception e) { |
| 4158 | // Ignore exceptions. |
| 4159 | } |
| 4160 | } |
| 4161 | return false; |
| 4162 | } |
| 4163 | |
| 4164 | // If this application record is still attached to a previous |
| 4165 | // process, clean it up now. |
| 4166 | if (app.thread != null) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4167 | handleAppDiedLocked(app, true, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | // Tell the process all about itself. |
| 4171 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4172 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4173 | TAG, "Binding process pid " + pid + " to record " + app); |
| 4174 | |
| 4175 | String processName = app.processName; |
| 4176 | try { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 4177 | AppDeathRecipient adr = new AppDeathRecipient( |
| 4178 | app, pid, thread); |
| 4179 | thread.asBinder().linkToDeath(adr, 0); |
| 4180 | app.deathRecipient = adr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4181 | } catch (RemoteException e) { |
| 4182 | app.resetPackageList(); |
| 4183 | startProcessLocked(app, "link fail", processName); |
| 4184 | return false; |
| 4185 | } |
| 4186 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4187 | EventLog.writeEvent(EventLogTags.AM_PROC_BOUND, app.userId, app.pid, app.processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4188 | |
| 4189 | app.thread = thread; |
| 4190 | app.curAdj = app.setAdj = -100; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 4191 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
| 4192 | app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4193 | app.forcingToForeground = null; |
| 4194 | app.foregroundServices = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 4195 | app.hasShownUi = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4196 | app.debugging = false; |
| 4197 | |
| 4198 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 4199 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4200 | boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4201 | List providers = normalMode ? generateApplicationProvidersLocked(app) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4202 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4203 | if (!normalMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4204 | Slog.i(TAG, "Launching preboot mode app: " + app); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4205 | } |
| 4206 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4207 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4208 | TAG, "New app record " + app |
| 4209 | + " thread=" + thread.asBinder() + " pid=" + pid); |
| 4210 | try { |
| 4211 | int testMode = IApplicationThread.DEBUG_OFF; |
| 4212 | if (mDebugApp != null && mDebugApp.equals(processName)) { |
| 4213 | testMode = mWaitForDebugger |
| 4214 | ? IApplicationThread.DEBUG_WAIT |
| 4215 | : IApplicationThread.DEBUG_ON; |
| 4216 | app.debugging = true; |
| 4217 | if (mDebugTransient) { |
| 4218 | mDebugApp = mOrigDebugApp; |
| 4219 | mWaitForDebugger = mOrigWaitForDebugger; |
| 4220 | } |
| 4221 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4222 | String profileFile = app.instrumentationProfileFile; |
| 4223 | ParcelFileDescriptor profileFd = null; |
| 4224 | boolean profileAutoStop = false; |
| 4225 | if (mProfileApp != null && mProfileApp.equals(processName)) { |
| 4226 | mProfileProc = app; |
| 4227 | profileFile = mProfileFile; |
| 4228 | profileFd = mProfileFd; |
| 4229 | profileAutoStop = mAutoStopProfiler; |
| 4230 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4231 | boolean enableOpenGlTrace = false; |
| 4232 | if (mOpenGlTraceApp != null && mOpenGlTraceApp.equals(processName)) { |
| 4233 | enableOpenGlTrace = true; |
| 4234 | mOpenGlTraceApp = null; |
| 4235 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4236 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4237 | // If the app is being launched for restore or full backup, set it up specially |
| 4238 | boolean isRestrictedBackupMode = false; |
| 4239 | if (mBackupTarget != null && mBackupAppName.equals(processName)) { |
| 4240 | isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE) |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 4241 | || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL) |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4242 | || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL); |
| 4243 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4244 | |
Dianne Hackborn | d7f6daa | 2009-06-22 17:06:35 -0700 | [diff] [blame] | 4245 | ensurePackageDexOpt(app.instrumentationInfo != null |
| 4246 | ? app.instrumentationInfo.packageName |
| 4247 | : app.info.packageName); |
| 4248 | if (app.instrumentationClass != null) { |
| 4249 | ensurePackageDexOpt(app.instrumentationClass.getPackageName()); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 4250 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4251 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 4252 | + processName + " with config " + mConfiguration); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4253 | ApplicationInfo appInfo = app.instrumentationInfo != null |
| 4254 | ? app.instrumentationInfo : app.info; |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 4255 | app.compat = compatibilityInfoForPackageLocked(appInfo); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4256 | if (profileFd != null) { |
| 4257 | profileFd = profileFd.dup(); |
| 4258 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4259 | thread.bindApplication(processName, appInfo, providers, |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4260 | app.instrumentationClass, profileFile, profileFd, profileAutoStop, |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 4261 | app.instrumentationArguments, app.instrumentationWatcher, testMode, |
| 4262 | enableOpenGlTrace, isRestrictedBackupMode || !normalMode, app.persistent, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 4263 | new Configuration(mConfiguration), app.compat, getCommonServicesLocked(), |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 4264 | mCoreSettingsObserver.getCoreSettingsLocked()); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 4265 | updateLruProcessLocked(app, false); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 4266 | app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4267 | } catch (Exception e) { |
| 4268 | // todo: Yikes! What should we do? For now we will try to |
| 4269 | // start another process, but that could easily get us in |
| 4270 | // an infinite loop of restarting processes... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4271 | Slog.w(TAG, "Exception thrown during bind!", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4272 | |
| 4273 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 4274 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4275 | startProcessLocked(app, "bind fail", processName); |
| 4276 | return false; |
| 4277 | } |
| 4278 | |
| 4279 | // Remove this record from the list of starting applications. |
| 4280 | mPersistentStartingProcesses.remove(app); |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4281 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 4282 | "Attach application locked removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4283 | mProcessesOnHold.remove(app); |
| 4284 | |
| 4285 | boolean badApp = false; |
| 4286 | boolean didSomething = false; |
| 4287 | |
| 4288 | // 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] | 4289 | ActivityRecord hr = mMainStack.topRunningActivityLocked(null); |
Christopher Tate | 04c0af8 | 2010-06-07 18:35:20 -0700 | [diff] [blame] | 4290 | if (hr != null && normalMode) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4291 | 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] | 4292 | && processName.equals(hr.processName)) { |
| 4293 | try { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 4294 | if (mHeadless) { |
| 4295 | Slog.e(TAG, "Starting activities not supported on headless device: " + hr); |
| 4296 | } else if (mMainStack.realStartActivityLocked(hr, app, true, true)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4297 | didSomething = true; |
| 4298 | } |
| 4299 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4300 | 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] | 4301 | + hr.intent.getComponent().flattenToShortString(), e); |
| 4302 | badApp = true; |
| 4303 | } |
| 4304 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4305 | mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4306 | } |
| 4307 | } |
| 4308 | |
| 4309 | // Find any services that should be running in this process... |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4310 | if (!badApp) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4311 | try { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 4312 | didSomething |= mServices.attachApplicationLocked(app, processName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4313 | } catch (Exception e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4314 | badApp = true; |
| 4315 | } |
| 4316 | } |
| 4317 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4318 | // Check if a next-broadcast receiver is in this process... |
| 4319 | if (!badApp && isPendingBroadcastProcessLocked(pid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4320 | try { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4321 | didSomething = sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4322 | } catch (Exception e) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 4323 | // 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] | 4324 | badApp = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4325 | } |
| 4326 | } |
| 4327 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4328 | // Check whether the next backup agent is in this process... |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4329 | if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4330 | 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] | 4331 | ensurePackageDexOpt(mBackupTarget.appInfo.packageName); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4332 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 4333 | thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, |
| 4334 | compatibilityInfoForPackageLocked(mBackupTarget.appInfo), |
| 4335 | mBackupTarget.backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4336 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4337 | Slog.w(TAG, "Exception scheduling backup agent creation: "); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 4338 | e.printStackTrace(); |
| 4339 | } |
| 4340 | } |
| 4341 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4342 | if (badApp) { |
| 4343 | // todo: Also need to kill application to deal with all |
| 4344 | // kinds of exceptions. |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 4345 | handleAppDiedLocked(app, false, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4346 | return false; |
| 4347 | } |
| 4348 | |
| 4349 | if (!didSomething) { |
| 4350 | updateOomAdjLocked(); |
| 4351 | } |
| 4352 | |
| 4353 | return true; |
| 4354 | } |
| 4355 | |
| 4356 | public final void attachApplication(IApplicationThread thread) { |
| 4357 | synchronized (this) { |
| 4358 | int callingPid = Binder.getCallingPid(); |
| 4359 | final long origId = Binder.clearCallingIdentity(); |
| 4360 | attachApplicationLocked(thread, callingPid); |
| 4361 | Binder.restoreCallingIdentity(origId); |
| 4362 | } |
| 4363 | } |
| 4364 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4365 | 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] | 4366 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 4367 | ActivityRecord r = mMainStack.activityIdleInternal(token, false, config); |
| 4368 | if (stopProfiling) { |
| 4369 | synchronized (this) { |
| 4370 | if (mProfileProc == r.app) { |
| 4371 | if (mProfileFd != null) { |
| 4372 | try { |
| 4373 | mProfileFd.close(); |
| 4374 | } catch (IOException e) { |
| 4375 | } |
| 4376 | clearProfilerLocked(); |
| 4377 | } |
| 4378 | } |
| 4379 | } |
| 4380 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4381 | Binder.restoreCallingIdentity(origId); |
| 4382 | } |
| 4383 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4384 | void enableScreenAfterBoot() { |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 4385 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN, |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4386 | SystemClock.uptimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4387 | mWindowManager.enableScreenAfterBoot(); |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 4388 | |
| 4389 | synchronized (this) { |
| 4390 | updateEventDispatchingLocked(); |
| 4391 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4392 | } |
| 4393 | |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4394 | public void showBootMessage(final CharSequence msg, final boolean always) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4395 | enforceNotIsolatedCaller("showBootMessage"); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 4396 | mWindowManager.showBootMessage(msg, always); |
| 4397 | } |
| 4398 | |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4399 | public void dismissKeyguardOnNextActivity() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4400 | enforceNotIsolatedCaller("dismissKeyguardOnNextActivity"); |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4401 | final long token = Binder.clearCallingIdentity(); |
| 4402 | try { |
| 4403 | synchronized (this) { |
| 4404 | if (mLockScreenShown) { |
| 4405 | mLockScreenShown = false; |
| 4406 | comeOutOfSleepIfNeededLocked(); |
| 4407 | } |
| 4408 | mMainStack.dismissKeyguardOnNextActivityLocked(); |
Dianne Hackborn | 1e88e98 | 2012-04-24 18:35:55 -0700 | [diff] [blame] | 4409 | } |
Jeff Sharkey | 7ffaa98 | 2012-04-30 16:59:05 -0700 | [diff] [blame] | 4410 | } finally { |
| 4411 | Binder.restoreCallingIdentity(token); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 4412 | } |
| 4413 | } |
| 4414 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4415 | final void finishBooting() { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4416 | IntentFilter pkgFilter = new IntentFilter(); |
| 4417 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 4418 | pkgFilter.addDataScheme("package"); |
| 4419 | mContext.registerReceiver(new BroadcastReceiver() { |
| 4420 | @Override |
| 4421 | public void onReceive(Context context, Intent intent) { |
| 4422 | String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| 4423 | if (pkgs != null) { |
| 4424 | for (String pkg : pkgs) { |
Vairavan Srinivasan | 61f0765 | 2010-07-22 13:36:40 -0700 | [diff] [blame] | 4425 | synchronized (ActivityManagerService.this) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 4426 | if (forceStopPackageLocked(pkg, -1, false, false, false, false, 0)) { |
| 4427 | setResultCode(Activity.RESULT_OK); |
| 4428 | return; |
| 4429 | } |
| 4430 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4431 | } |
| 4432 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4433 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4434 | }, pkgFilter); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 4435 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4436 | synchronized (this) { |
| 4437 | // Ensure that any processes we had put on hold are now started |
| 4438 | // up. |
| 4439 | final int NP = mProcessesOnHold.size(); |
| 4440 | if (NP > 0) { |
| 4441 | ArrayList<ProcessRecord> procs = |
| 4442 | new ArrayList<ProcessRecord>(mProcessesOnHold); |
| 4443 | for (int ip=0; ip<NP; ip++) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 4444 | if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: " |
| 4445 | + procs.get(ip)); |
| 4446 | startProcessLocked(procs.get(ip), "on-hold", null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4447 | } |
| 4448 | } |
| 4449 | |
| 4450 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 4451 | // Start looking for apps that are abusing wake locks. |
| 4452 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 4453 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4454 | // Tell anyone interested that we are done booting! |
Dianne Hackborn | f4c454b | 2010-08-11 12:47:41 -0700 | [diff] [blame] | 4455 | SystemProperties.set("sys.boot_completed", "1"); |
Guang Zhu | 191713a | 2012-01-12 12:02:22 -0800 | [diff] [blame] | 4456 | SystemProperties.set("dev.bootcomplete", "1"); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4457 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 4458 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 4459 | if (uss.mState == UserStartedState.STATE_BOOTING) { |
| 4460 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4461 | final int userId = mStartedUsers.keyAt(i); |
| 4462 | Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 4463 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 4464 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4465 | null, null, 0, null, null, |
| 4466 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4467 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 4468 | } |
Amith Yamasani | 4860cfc | 2012-08-08 19:14:18 -0700 | [diff] [blame] | 4469 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4470 | } |
| 4471 | } |
| 4472 | } |
| 4473 | |
| 4474 | final void ensureBootCompleted() { |
| 4475 | boolean booting; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4476 | boolean enableScreen; |
| 4477 | synchronized (this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4478 | booting = mBooting; |
| 4479 | mBooting = false; |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4480 | enableScreen = !mBooted; |
| 4481 | mBooted = true; |
| 4482 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4483 | |
| 4484 | if (booting) { |
| 4485 | finishBooting(); |
| 4486 | } |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4487 | |
| 4488 | if (enableScreen) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 4489 | enableScreenAfterBoot(); |
| 4490 | } |
| 4491 | } |
Dianne Hackborn | ad9b3211 | 2012-09-17 15:35:01 -0700 | [diff] [blame] | 4492 | |
| 4493 | public final void activityResumed(IBinder token) { |
| 4494 | final long origId = Binder.clearCallingIdentity(); |
| 4495 | mMainStack.activityResumed(token); |
| 4496 | Binder.restoreCallingIdentity(origId); |
| 4497 | } |
| 4498 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 4499 | public final void activityPaused(IBinder token) { |
| 4500 | final long origId = Binder.clearCallingIdentity(); |
| 4501 | mMainStack.activityPaused(token, false); |
| 4502 | Binder.restoreCallingIdentity(origId); |
| 4503 | } |
| 4504 | |
| 4505 | public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail, |
| 4506 | CharSequence description) { |
| 4507 | if (localLOGV) Slog.v( |
| 4508 | TAG, "Activity stopped: token=" + token); |
| 4509 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4510 | // Refuse possible leaked file descriptors |
| 4511 | if (icicle != null && icicle.hasFileDescriptors()) { |
| 4512 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 4513 | } |
| 4514 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4515 | ActivityRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4516 | |
| 4517 | final long origId = Binder.clearCallingIdentity(); |
| 4518 | |
| 4519 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4520 | r = mMainStack.isInStackLocked(token); |
| 4521 | if (r != null) { |
| 4522 | r.stack.activityStoppedLocked(r, icicle, thumbnail, description); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4523 | } |
| 4524 | } |
| 4525 | |
| 4526 | if (r != null) { |
| 4527 | sendPendingThumbnail(r, null, null, null, false); |
| 4528 | } |
| 4529 | |
| 4530 | trimApplications(); |
| 4531 | |
| 4532 | Binder.restoreCallingIdentity(origId); |
| 4533 | } |
| 4534 | |
| 4535 | public final void activityDestroyed(IBinder token) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4536 | if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 4537 | mMainStack.activityDestroyed(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4538 | } |
| 4539 | |
| 4540 | public String getCallingPackage(IBinder token) { |
| 4541 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4542 | ActivityRecord r = getCallingRecordLocked(token); |
Dianne Hackborn | 9bbcb91 | 2009-10-20 15:42:38 -0700 | [diff] [blame] | 4543 | 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] | 4544 | } |
| 4545 | } |
| 4546 | |
| 4547 | public ComponentName getCallingActivity(IBinder token) { |
| 4548 | synchronized (this) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4549 | ActivityRecord r = getCallingRecordLocked(token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4550 | return r != null ? r.intent.getComponent() : null; |
| 4551 | } |
| 4552 | } |
| 4553 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4554 | private ActivityRecord getCallingRecordLocked(IBinder token) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4555 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4556 | if (r == null) { |
| 4557 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4558 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4559 | return r.resultTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | public ComponentName getActivityClassForToken(IBinder token) { |
| 4563 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4564 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4565 | if (r == null) { |
| 4566 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4567 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4568 | return r.intent.getComponent(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4569 | } |
| 4570 | } |
| 4571 | |
| 4572 | public String getPackageForToken(IBinder token) { |
| 4573 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4574 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 4575 | if (r == null) { |
| 4576 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4577 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4578 | return r.packageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4579 | } |
| 4580 | } |
| 4581 | |
| 4582 | public IIntentSender getIntentSender(int type, |
| 4583 | String packageName, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4584 | int requestCode, Intent[] intents, String[] resolvedTypes, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4585 | int flags, Bundle options, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 4586 | enforceNotIsolatedCaller("getIntentSender"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4587 | // Refuse possible leaked file descriptors |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4588 | if (intents != null) { |
| 4589 | if (intents.length < 1) { |
| 4590 | throw new IllegalArgumentException("Intents array length must be >= 1"); |
| 4591 | } |
| 4592 | for (int i=0; i<intents.length; i++) { |
| 4593 | Intent intent = intents[i]; |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4594 | if (intent != null) { |
| 4595 | if (intent.hasFileDescriptors()) { |
| 4596 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 4597 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4598 | if (type == ActivityManager.INTENT_SENDER_BROADCAST && |
Dianne Hackborn | 52b0ce0 | 2011-04-14 13:09:32 -0700 | [diff] [blame] | 4599 | (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 4600 | throw new IllegalArgumentException( |
| 4601 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 4602 | } |
| 4603 | intents[i] = new Intent(intent); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4604 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4605 | } |
| 4606 | if (resolvedTypes != null && resolvedTypes.length != intents.length) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4607 | throw new IllegalArgumentException( |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4608 | "Intent array length does not match resolvedTypes length"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4609 | } |
| 4610 | } |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4611 | if (options != null) { |
| 4612 | if (options.hasFileDescriptors()) { |
| 4613 | throw new IllegalArgumentException("File descriptors passed in options"); |
| 4614 | } |
| 4615 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 4616 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4617 | synchronized(this) { |
| 4618 | int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4619 | int origUserId = userId; |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 4620 | userId = handleIncomingUser(Binder.getCallingPid(), callingUid, userId, |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4621 | type == ActivityManager.INTENT_SENDER_BROADCAST, true, |
| 4622 | "getIntentSender", null); |
| 4623 | if (origUserId == UserHandle.USER_CURRENT) { |
| 4624 | // We don't want to evaluate this until the pending intent is |
| 4625 | // actually executed. However, we do want to always do the |
| 4626 | // security checking for it above. |
| 4627 | userId = UserHandle.USER_CURRENT; |
| 4628 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4629 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 4630 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4631 | int uid = AppGlobals.getPackageManager() |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4632 | .getPackageUid(packageName, UserHandle.getUserId(callingUid)); |
| 4633 | if (!UserHandle.isSameApp(callingUid, uid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4634 | String msg = "Permission Denial: getIntentSender() from pid=" |
| 4635 | + Binder.getCallingPid() |
| 4636 | + ", uid=" + Binder.getCallingUid() |
| 4637 | + ", (need uid=" + uid + ")" |
| 4638 | + " is not allowed to send as package " + packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4639 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4640 | throw new SecurityException(msg); |
| 4641 | } |
| 4642 | } |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4643 | |
| 4644 | return getIntentSenderLocked(type, packageName, callingUid, userId, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4645 | token, resultWho, requestCode, intents, resolvedTypes, flags, options); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4646 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4647 | } catch (RemoteException e) { |
| 4648 | throw new SecurityException(e); |
| 4649 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4650 | } |
| 4651 | } |
| 4652 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4653 | IIntentSender getIntentSenderLocked(int type, String packageName, |
| 4654 | int callingUid, int userId, IBinder token, String resultWho, |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 4655 | int requestCode, Intent[] intents, String[] resolvedTypes, int flags, |
| 4656 | Bundle options) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 4657 | if (DEBUG_MU) |
| 4658 | Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4659 | ActivityRecord activity = null; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4660 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4661 | activity = mMainStack.isInStackLocked(token); |
| 4662 | if (activity == null) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4663 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4664 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4665 | if (activity.finishing) { |
| 4666 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4667 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4668 | } |
| 4669 | |
| 4670 | final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0; |
| 4671 | final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0; |
| 4672 | final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0; |
| 4673 | flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT |
| 4674 | |PendingIntent.FLAG_UPDATE_CURRENT); |
| 4675 | |
| 4676 | PendingIntentRecord.Key key = new PendingIntentRecord.Key( |
| 4677 | type, packageName, activity, resultWho, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4678 | requestCode, intents, resolvedTypes, flags, options, userId); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4679 | WeakReference<PendingIntentRecord> ref; |
| 4680 | ref = mIntentSenderRecords.get(key); |
| 4681 | PendingIntentRecord rec = ref != null ? ref.get() : null; |
| 4682 | if (rec != null) { |
| 4683 | if (!cancelCurrent) { |
| 4684 | if (updateCurrent) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4685 | if (rec.key.requestIntent != null) { |
Adam Powell | 501d4a5 | 2012-04-30 15:03:57 -0700 | [diff] [blame] | 4686 | rec.key.requestIntent.replaceExtras(intents != null ? |
| 4687 | intents[intents.length - 1] : null); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 4688 | } |
| 4689 | if (intents != null) { |
| 4690 | intents[intents.length-1] = rec.key.requestIntent; |
| 4691 | rec.key.allIntents = intents; |
| 4692 | rec.key.allResolvedTypes = resolvedTypes; |
| 4693 | } else { |
| 4694 | rec.key.allIntents = null; |
| 4695 | rec.key.allResolvedTypes = null; |
| 4696 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4697 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4698 | return rec; |
| 4699 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4700 | rec.canceled = true; |
| 4701 | mIntentSenderRecords.remove(key); |
| 4702 | } |
| 4703 | if (noCreate) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4704 | return rec; |
| 4705 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4706 | rec = new PendingIntentRecord(this, key, callingUid); |
| 4707 | mIntentSenderRecords.put(key, rec.ref); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 4708 | if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 4709 | if (activity.pendingResults == null) { |
| 4710 | activity.pendingResults |
| 4711 | = new HashSet<WeakReference<PendingIntentRecord>>(); |
| 4712 | } |
| 4713 | activity.pendingResults.add(rec.ref); |
| 4714 | } |
| 4715 | return rec; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4716 | } |
| 4717 | |
| 4718 | public void cancelIntentSender(IIntentSender sender) { |
| 4719 | if (!(sender instanceof PendingIntentRecord)) { |
| 4720 | return; |
| 4721 | } |
| 4722 | synchronized(this) { |
| 4723 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 4724 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 4725 | int uid = AppGlobals.getPackageManager() |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4726 | .getPackageUid(rec.key.packageName, UserHandle.getCallingUserId()); |
| 4727 | if (!UserHandle.isSameApp(uid, Binder.getCallingUid())) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4728 | String msg = "Permission Denial: cancelIntentSender() from pid=" |
| 4729 | + Binder.getCallingPid() |
| 4730 | + ", uid=" + Binder.getCallingUid() |
| 4731 | + " is not allowed to cancel packges " |
| 4732 | + rec.key.packageName; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4733 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4734 | throw new SecurityException(msg); |
| 4735 | } |
| 4736 | } catch (RemoteException e) { |
| 4737 | throw new SecurityException(e); |
| 4738 | } |
| 4739 | cancelIntentSenderLocked(rec, true); |
| 4740 | } |
| 4741 | } |
| 4742 | |
| 4743 | void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) { |
| 4744 | rec.canceled = true; |
| 4745 | mIntentSenderRecords.remove(rec.key); |
| 4746 | if (cleanActivity && rec.key.activity != null) { |
| 4747 | rec.key.activity.pendingResults.remove(rec.ref); |
| 4748 | } |
| 4749 | } |
| 4750 | |
| 4751 | public String getPackageForIntentSender(IIntentSender pendingResult) { |
| 4752 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4753 | return null; |
| 4754 | } |
Brad Fitzpatrick | b213d10 | 2010-04-19 11:58:52 -0700 | [diff] [blame] | 4755 | try { |
| 4756 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4757 | return res.key.packageName; |
| 4758 | } catch (ClassCastException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4759 | } |
| 4760 | return null; |
| 4761 | } |
| 4762 | |
Christopher Tate | c4a07d1 | 2012-04-06 14:19:13 -0700 | [diff] [blame] | 4763 | public int getUidForIntentSender(IIntentSender sender) { |
| 4764 | if (sender instanceof PendingIntentRecord) { |
| 4765 | try { |
| 4766 | PendingIntentRecord res = (PendingIntentRecord)sender; |
| 4767 | return res.uid; |
| 4768 | } catch (ClassCastException e) { |
| 4769 | } |
| 4770 | } |
| 4771 | return -1; |
| 4772 | } |
| 4773 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 4774 | public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) { |
| 4775 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4776 | return false; |
| 4777 | } |
| 4778 | try { |
| 4779 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4780 | if (res.key.allIntents == null) { |
| 4781 | return false; |
| 4782 | } |
| 4783 | for (int i=0; i<res.key.allIntents.length; i++) { |
| 4784 | Intent intent = res.key.allIntents[i]; |
| 4785 | if (intent.getPackage() != null && intent.getComponent() != null) { |
| 4786 | return false; |
| 4787 | } |
| 4788 | } |
| 4789 | return true; |
| 4790 | } catch (ClassCastException e) { |
| 4791 | } |
| 4792 | return false; |
| 4793 | } |
| 4794 | |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 4795 | public boolean isIntentSenderAnActivity(IIntentSender pendingResult) { |
| 4796 | if (!(pendingResult instanceof PendingIntentRecord)) { |
| 4797 | return false; |
| 4798 | } |
| 4799 | try { |
| 4800 | PendingIntentRecord res = (PendingIntentRecord)pendingResult; |
| 4801 | if (res.key.type == ActivityManager.INTENT_SENDER_ACTIVITY) { |
| 4802 | return true; |
| 4803 | } |
| 4804 | return false; |
| 4805 | } catch (ClassCastException e) { |
| 4806 | } |
| 4807 | return false; |
| 4808 | } |
| 4809 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4810 | public void setProcessLimit(int max) { |
| 4811 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4812 | "setProcessLimit()"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4813 | synchronized (this) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 4814 | mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4815 | mProcessLimitOverride = max; |
| 4816 | } |
| 4817 | trimApplications(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4818 | } |
| 4819 | |
| 4820 | public int getProcessLimit() { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 4821 | synchronized (this) { |
| 4822 | return mProcessLimitOverride; |
| 4823 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4824 | } |
| 4825 | |
| 4826 | void foregroundTokenDied(ForegroundToken token) { |
| 4827 | synchronized (ActivityManagerService.this) { |
| 4828 | synchronized (mPidsSelfLocked) { |
| 4829 | ForegroundToken cur |
| 4830 | = mForegroundProcesses.get(token.pid); |
| 4831 | if (cur != token) { |
| 4832 | return; |
| 4833 | } |
| 4834 | mForegroundProcesses.remove(token.pid); |
| 4835 | ProcessRecord pr = mPidsSelfLocked.get(token.pid); |
| 4836 | if (pr == null) { |
| 4837 | return; |
| 4838 | } |
| 4839 | pr.forcingToForeground = null; |
| 4840 | pr.foregroundServices = false; |
| 4841 | } |
| 4842 | updateOomAdjLocked(); |
| 4843 | } |
| 4844 | } |
| 4845 | |
| 4846 | public void setProcessForeground(IBinder token, int pid, boolean isForeground) { |
| 4847 | enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT, |
| 4848 | "setProcessForeground()"); |
| 4849 | synchronized(this) { |
| 4850 | boolean changed = false; |
| 4851 | |
| 4852 | synchronized (mPidsSelfLocked) { |
| 4853 | ProcessRecord pr = mPidsSelfLocked.get(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4854 | if (pr == null && isForeground) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4855 | Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4856 | return; |
| 4857 | } |
| 4858 | ForegroundToken oldToken = mForegroundProcesses.get(pid); |
| 4859 | if (oldToken != null) { |
| 4860 | oldToken.token.unlinkToDeath(oldToken, 0); |
| 4861 | mForegroundProcesses.remove(pid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 4862 | if (pr != null) { |
| 4863 | pr.forcingToForeground = null; |
| 4864 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4865 | changed = true; |
| 4866 | } |
| 4867 | if (isForeground && token != null) { |
| 4868 | ForegroundToken newToken = new ForegroundToken() { |
| 4869 | public void binderDied() { |
| 4870 | foregroundTokenDied(this); |
| 4871 | } |
| 4872 | }; |
| 4873 | newToken.pid = pid; |
| 4874 | newToken.token = token; |
| 4875 | try { |
| 4876 | token.linkToDeath(newToken, 0); |
| 4877 | mForegroundProcesses.put(pid, newToken); |
| 4878 | pr.forcingToForeground = token; |
| 4879 | changed = true; |
| 4880 | } catch (RemoteException e) { |
| 4881 | // If the process died while doing this, we will later |
| 4882 | // do the cleanup with the process death link. |
| 4883 | } |
| 4884 | } |
| 4885 | } |
| 4886 | |
| 4887 | if (changed) { |
| 4888 | updateOomAdjLocked(); |
| 4889 | } |
| 4890 | } |
| 4891 | } |
| 4892 | |
| 4893 | // ========================================================= |
| 4894 | // PERMISSIONS |
| 4895 | // ========================================================= |
| 4896 | |
| 4897 | static class PermissionController extends IPermissionController.Stub { |
| 4898 | ActivityManagerService mActivityManagerService; |
| 4899 | PermissionController(ActivityManagerService activityManagerService) { |
| 4900 | mActivityManagerService = activityManagerService; |
| 4901 | } |
| 4902 | |
| 4903 | public boolean checkPermission(String permission, int pid, int uid) { |
| 4904 | return mActivityManagerService.checkPermission(permission, pid, |
| 4905 | uid) == PackageManager.PERMISSION_GRANTED; |
| 4906 | } |
| 4907 | } |
| 4908 | |
| 4909 | /** |
| 4910 | * This can be called with or without the global lock held. |
| 4911 | */ |
| 4912 | int checkComponentPermission(String permission, int pid, int uid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 4913 | int owningUid, boolean exported) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4914 | // We might be performing an operation on behalf of an indirect binder |
| 4915 | // invocation, e.g. via {@link #openContentUri}. Check and adjust the |
| 4916 | // client identity accordingly before proceeding. |
| 4917 | Identity tlsIdentity = sCallerIdentity.get(); |
| 4918 | if (tlsIdentity != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4919 | Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4920 | + tlsIdentity.pid + "," + tlsIdentity.uid + "}"); |
| 4921 | uid = tlsIdentity.uid; |
| 4922 | pid = tlsIdentity.pid; |
| 4923 | } |
| 4924 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4925 | if (pid == MY_PID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4926 | return PackageManager.PERMISSION_GRANTED; |
| 4927 | } |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 4928 | |
| 4929 | return ActivityManager.checkComponentPermission(permission, uid, |
| 4930 | owningUid, exported); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4931 | } |
| 4932 | |
| 4933 | /** |
| 4934 | * As the only public entry point for permissions checking, this method |
| 4935 | * can enforce the semantic that requesting a check on a null global |
| 4936 | * permission is automatically denied. (Internally a null permission |
| 4937 | * string is used when calling {@link #checkComponentPermission} in cases |
| 4938 | * when only uid-based security is needed.) |
| 4939 | * |
| 4940 | * This can be called with or without the global lock held. |
| 4941 | */ |
| 4942 | public int checkPermission(String permission, int pid, int uid) { |
| 4943 | if (permission == null) { |
| 4944 | return PackageManager.PERMISSION_DENIED; |
| 4945 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4946 | return checkComponentPermission(permission, pid, UserHandle.getAppId(uid), -1, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4947 | } |
| 4948 | |
| 4949 | /** |
| 4950 | * Binder IPC calls go through the public entry point. |
| 4951 | * This can be called with or without the global lock held. |
| 4952 | */ |
| 4953 | int checkCallingPermission(String permission) { |
| 4954 | return checkPermission(permission, |
| 4955 | Binder.getCallingPid(), |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 4956 | UserHandle.getAppId(Binder.getCallingUid())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4957 | } |
| 4958 | |
| 4959 | /** |
| 4960 | * This can be called with or without the global lock held. |
| 4961 | */ |
| 4962 | void enforceCallingPermission(String permission, String func) { |
| 4963 | if (checkCallingPermission(permission) |
| 4964 | == PackageManager.PERMISSION_GRANTED) { |
| 4965 | return; |
| 4966 | } |
| 4967 | |
| 4968 | String msg = "Permission Denial: " + func + " from pid=" |
| 4969 | + Binder.getCallingPid() |
| 4970 | + ", uid=" + Binder.getCallingUid() |
| 4971 | + " requires " + permission; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 4972 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4973 | throw new SecurityException(msg); |
| 4974 | } |
| 4975 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4976 | /** |
| 4977 | * Determine if UID is holding permissions required to access {@link Uri} in |
| 4978 | * the given {@link ProviderInfo}. Final permission checking is always done |
| 4979 | * in {@link ContentProvider}. |
| 4980 | */ |
| 4981 | private final boolean checkHoldingPermissionsLocked( |
| 4982 | IPackageManager pm, ProviderInfo pi, Uri uri, int uid, int modeFlags) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 4983 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 4984 | "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid); |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4985 | |
| 4986 | if (pi.applicationInfo.uid == uid) { |
| 4987 | return true; |
| 4988 | } else if (!pi.exported) { |
| 4989 | return false; |
| 4990 | } |
| 4991 | |
| 4992 | boolean readMet = (modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0; |
| 4993 | 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] | 4994 | try { |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 4995 | // check if target holds top-level <provider> permissions |
| 4996 | if (!readMet && pi.readPermission != null |
| 4997 | && (pm.checkUidPermission(pi.readPermission, uid) == PERMISSION_GRANTED)) { |
| 4998 | readMet = true; |
| 4999 | } |
| 5000 | if (!writeMet && pi.writePermission != null |
| 5001 | && (pm.checkUidPermission(pi.writePermission, uid) == PERMISSION_GRANTED)) { |
| 5002 | writeMet = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5003 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5004 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5005 | // track if unprotected read/write is allowed; any denied |
| 5006 | // <path-permission> below removes this ability |
| 5007 | boolean allowDefaultRead = pi.readPermission == null; |
| 5008 | boolean allowDefaultWrite = pi.writePermission == null; |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5009 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5010 | // check if target holds any <path-permission> that match uri |
| 5011 | final PathPermission[] pps = pi.pathPermissions; |
| 5012 | if (pps != null) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5013 | final String path = uri.getPath(); |
| 5014 | int i = pps.length; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5015 | while (i > 0 && (!readMet || !writeMet)) { |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5016 | i--; |
| 5017 | PathPermission pp = pps[i]; |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5018 | if (pp.match(path)) { |
| 5019 | if (!readMet) { |
| 5020 | final String pprperm = pp.getReadPermission(); |
| 5021 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for " |
| 5022 | + pprperm + " for " + pp.getPath() |
| 5023 | + ": match=" + pp.match(path) |
| 5024 | + " check=" + pm.checkUidPermission(pprperm, uid)); |
| 5025 | if (pprperm != null) { |
| 5026 | if (pm.checkUidPermission(pprperm, uid) == PERMISSION_GRANTED) { |
| 5027 | readMet = true; |
| 5028 | } else { |
| 5029 | allowDefaultRead = false; |
| 5030 | } |
| 5031 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5032 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5033 | if (!writeMet) { |
| 5034 | final String ppwperm = pp.getWritePermission(); |
| 5035 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm " |
| 5036 | + ppwperm + " for " + pp.getPath() |
| 5037 | + ": match=" + pp.match(path) |
| 5038 | + " check=" + pm.checkUidPermission(ppwperm, uid)); |
| 5039 | if (ppwperm != null) { |
| 5040 | if (pm.checkUidPermission(ppwperm, uid) == PERMISSION_GRANTED) { |
| 5041 | writeMet = true; |
| 5042 | } else { |
| 5043 | allowDefaultWrite = false; |
| 5044 | } |
| 5045 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5046 | } |
| 5047 | } |
| 5048 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5049 | } |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5050 | |
| 5051 | // grant unprotected <provider> read/write, if not blocked by |
| 5052 | // <path-permission> above |
| 5053 | if (allowDefaultRead) readMet = true; |
| 5054 | if (allowDefaultWrite) writeMet = true; |
| 5055 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5056 | } catch (RemoteException e) { |
| 5057 | return false; |
| 5058 | } |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5059 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 5060 | return readMet && writeMet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5061 | } |
| 5062 | |
| 5063 | private final boolean checkUriPermissionLocked(Uri uri, int uid, |
| 5064 | int modeFlags) { |
| 5065 | // Root gets to do everything. |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 5066 | if (uid == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5067 | return true; |
| 5068 | } |
| 5069 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid); |
| 5070 | if (perms == null) return false; |
| 5071 | UriPermission perm = perms.get(uri); |
| 5072 | if (perm == null) return false; |
| 5073 | return (modeFlags&perm.modeFlags) == modeFlags; |
| 5074 | } |
| 5075 | |
| 5076 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5077 | enforceNotIsolatedCaller("checkUriPermission"); |
| 5078 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5079 | // Another redirected-binder-call permissions check as in |
| 5080 | // {@link checkComponentPermission}. |
| 5081 | Identity tlsIdentity = sCallerIdentity.get(); |
| 5082 | if (tlsIdentity != null) { |
| 5083 | uid = tlsIdentity.uid; |
| 5084 | pid = tlsIdentity.pid; |
| 5085 | } |
| 5086 | |
| 5087 | // Our own process gets to do everything. |
| 5088 | if (pid == MY_PID) { |
| 5089 | return PackageManager.PERMISSION_GRANTED; |
| 5090 | } |
| 5091 | synchronized(this) { |
| 5092 | return checkUriPermissionLocked(uri, uid, modeFlags) |
| 5093 | ? PackageManager.PERMISSION_GRANTED |
| 5094 | : PackageManager.PERMISSION_DENIED; |
| 5095 | } |
| 5096 | } |
| 5097 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5098 | /** |
| 5099 | * Check if the targetPkg can be granted permission to access uri by |
| 5100 | * the callingUid using the given modeFlags. Throws a security exception |
| 5101 | * if callingUid is not allowed to do this. Returns the uid of the target |
| 5102 | * if the URI permission grant should be performed; returns -1 if it is not |
| 5103 | * needed (for example targetPkg already has permission to access the URI). |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5104 | * If you already know the uid of the target, you can supply it in |
| 5105 | * lastTargetUid else set that to -1. |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5106 | */ |
| 5107 | int checkGrantUriPermissionLocked(int callingUid, String targetPkg, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5108 | Uri uri, int modeFlags, int lastTargetUid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5109 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5110 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5111 | if (modeFlags == 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5112 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5113 | } |
| 5114 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5115 | if (targetPkg != null) { |
| 5116 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5117 | "Checking grant " + targetPkg + " permission to " + uri); |
| 5118 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5119 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5120 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5121 | |
| 5122 | // If this is not a content: uri, we can't do anything with it. |
| 5123 | if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5124 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5125 | "Can't grant URI permission for non-content URI: " + uri); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5126 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5127 | } |
| 5128 | |
| 5129 | String name = uri.getAuthority(); |
| 5130 | ProviderInfo pi = null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5131 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5132 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5133 | if (cpr != null) { |
| 5134 | pi = cpr.info; |
| 5135 | } else { |
| 5136 | try { |
| 5137 | pi = pm.resolveContentProvider(name, |
Dianne Hackborn | 11f0cb7 | 2012-09-16 17:12:34 -0700 | [diff] [blame] | 5138 | PackageManager.GET_URI_PERMISSION_PATTERNS, |
| 5139 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5140 | } catch (RemoteException ex) { |
| 5141 | } |
| 5142 | } |
| 5143 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5144 | Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString()); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5145 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5146 | } |
| 5147 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5148 | int targetUid = lastTargetUid; |
| 5149 | if (targetUid < 0 && targetPkg != null) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5150 | try { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5151 | targetUid = pm.getPackageUid(targetPkg, UserHandle.getUserId(callingUid)); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5152 | if (targetUid < 0) { |
| 5153 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5154 | "Can't grant URI permission no uid for: " + targetPkg); |
| 5155 | return -1; |
| 5156 | } |
| 5157 | } catch (RemoteException ex) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5158 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5159 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5160 | } |
| 5161 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5162 | if (targetUid >= 0) { |
| 5163 | // First... does the target actually need this permission? |
| 5164 | if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) { |
| 5165 | // No need to grant the target this permission. |
| 5166 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
| 5167 | "Target " + targetPkg + " already has full permission to " + uri); |
| 5168 | return -1; |
| 5169 | } |
| 5170 | } else { |
| 5171 | // First... there is no target package, so can anyone access it? |
| 5172 | boolean allowed = pi.exported; |
| 5173 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 5174 | if (pi.readPermission != null) { |
| 5175 | allowed = false; |
| 5176 | } |
| 5177 | } |
| 5178 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 5179 | if (pi.writePermission != null) { |
| 5180 | allowed = false; |
| 5181 | } |
| 5182 | } |
| 5183 | if (allowed) { |
| 5184 | return -1; |
| 5185 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5186 | } |
| 5187 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5188 | // Second... is the provider allowing granting of URI permissions? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5189 | if (!pi.grantUriPermissions) { |
| 5190 | throw new SecurityException("Provider " + pi.packageName |
| 5191 | + "/" + pi.name |
| 5192 | + " does not allow granting of Uri permissions (uri " |
| 5193 | + uri + ")"); |
| 5194 | } |
| 5195 | if (pi.uriPermissionPatterns != null) { |
| 5196 | final int N = pi.uriPermissionPatterns.length; |
| 5197 | boolean allowed = false; |
| 5198 | for (int i=0; i<N; i++) { |
| 5199 | if (pi.uriPermissionPatterns[i] != null |
| 5200 | && pi.uriPermissionPatterns[i].match(uri.getPath())) { |
| 5201 | allowed = true; |
| 5202 | break; |
| 5203 | } |
| 5204 | } |
| 5205 | if (!allowed) { |
| 5206 | throw new SecurityException("Provider " + pi.packageName |
| 5207 | + "/" + pi.name |
| 5208 | + " does not allow granting of permission to path of Uri " |
| 5209 | + uri); |
| 5210 | } |
| 5211 | } |
| 5212 | |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5213 | // Third... does the caller itself have permission to access |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5214 | // this uri? |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5215 | if (callingUid != Process.myUid()) { |
| 5216 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
| 5217 | if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5218 | throw new SecurityException("Uid " + callingUid |
| 5219 | + " does not have permission to uri " + uri); |
| 5220 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5221 | } |
| 5222 | } |
| 5223 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5224 | return targetUid; |
| 5225 | } |
| 5226 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5227 | public int checkGrantUriPermission(int callingUid, String targetPkg, |
| 5228 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5229 | enforceNotIsolatedCaller("checkGrantUriPermission"); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5230 | synchronized(this) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5231 | return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5232 | } |
| 5233 | } |
| 5234 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5235 | void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg, |
| 5236 | Uri uri, int modeFlags, UriPermissionOwner owner) { |
| 5237 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5238 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5239 | if (modeFlags == 0) { |
| 5240 | return; |
| 5241 | } |
| 5242 | |
| 5243 | // 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] | 5244 | // to the uri, and the target doesn't. Let's now give this to |
| 5245 | // the target. |
| 5246 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5247 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5248 | "Granting " + targetPkg + "/" + targetUid + " permission to " + uri); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5249 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5250 | HashMap<Uri, UriPermission> targetUris |
| 5251 | = mGrantedUriPermissions.get(targetUid); |
| 5252 | if (targetUris == null) { |
| 5253 | targetUris = new HashMap<Uri, UriPermission>(); |
| 5254 | mGrantedUriPermissions.put(targetUid, targetUris); |
| 5255 | } |
| 5256 | |
| 5257 | UriPermission perm = targetUris.get(uri); |
| 5258 | if (perm == null) { |
| 5259 | perm = new UriPermission(targetUid, uri); |
| 5260 | targetUris.put(uri, perm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5261 | } |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5263 | perm.modeFlags |= modeFlags; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5264 | if (owner == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5265 | perm.globalModeFlags |= modeFlags; |
Vairavan Srinivasan | 91c12c2 | 2011-01-21 18:26:06 -0800 | [diff] [blame] | 5266 | } else { |
| 5267 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 5268 | perm.readOwners.add(owner); |
| 5269 | owner.addReadPermission(perm); |
| 5270 | } |
| 5271 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 5272 | perm.writeOwners.add(owner); |
| 5273 | owner.addWritePermission(perm); |
| 5274 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5275 | } |
| 5276 | } |
| 5277 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5278 | void grantUriPermissionLocked(int callingUid, String targetPkg, Uri uri, |
| 5279 | int modeFlags, UriPermissionOwner owner) { |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5280 | if (targetPkg == null) { |
| 5281 | throw new NullPointerException("targetPkg"); |
| 5282 | } |
| 5283 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5284 | int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1); |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5285 | if (targetUid < 0) { |
| 5286 | return; |
| 5287 | } |
| 5288 | |
| 5289 | grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner); |
| 5290 | } |
| 5291 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5292 | static class NeededUriGrants extends ArrayList<Uri> { |
| 5293 | final String targetPkg; |
| 5294 | final int targetUid; |
| 5295 | final int flags; |
| 5296 | |
| 5297 | NeededUriGrants(String _targetPkg, int _targetUid, int _flags) { |
| 5298 | targetPkg = _targetPkg; |
| 5299 | targetUid = _targetUid; |
| 5300 | flags = _flags; |
| 5301 | } |
| 5302 | } |
| 5303 | |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5304 | /** |
| 5305 | * Like checkGrantUriPermissionLocked, but takes an Intent. |
| 5306 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5307 | NeededUriGrants checkGrantUriPermissionFromIntentLocked(int callingUid, |
| 5308 | String targetPkg, Intent intent, int mode, NeededUriGrants needed) { |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5309 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5310 | "Checking URI perm to data=" + (intent != null ? intent.getData() : null) |
| 5311 | + " clip=" + (intent != null ? intent.getClipData() : null) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 5312 | + " from " + intent + "; flags=0x" |
| 5313 | + Integer.toHexString(intent != null ? intent.getFlags() : 0)); |
| 5314 | |
Dianne Hackborn | 90f4aaf | 2010-09-27 14:58:44 -0700 | [diff] [blame] | 5315 | if (targetPkg == null) { |
| 5316 | throw new NullPointerException("targetPkg"); |
| 5317 | } |
| 5318 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5319 | if (intent == null) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5320 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5321 | } |
| 5322 | Uri data = intent.getData(); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5323 | ClipData clip = intent.getClipData(); |
| 5324 | if (data == null && clip == null) { |
| 5325 | return null; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5326 | } |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5327 | if (data != null) { |
| 5328 | int target = checkGrantUriPermissionLocked(callingUid, targetPkg, data, |
| 5329 | mode, needed != null ? needed.targetUid : -1); |
| 5330 | if (target > 0) { |
| 5331 | if (needed == null) { |
| 5332 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5333 | } |
| 5334 | needed.add(data); |
| 5335 | } |
| 5336 | } |
| 5337 | if (clip != null) { |
| 5338 | for (int i=0; i<clip.getItemCount(); i++) { |
| 5339 | Uri uri = clip.getItemAt(i).getUri(); |
| 5340 | if (uri != null) { |
| 5341 | int target = -1; |
| 5342 | target = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, |
| 5343 | mode, needed != null ? needed.targetUid : -1); |
| 5344 | if (target > 0) { |
| 5345 | if (needed == null) { |
| 5346 | needed = new NeededUriGrants(targetPkg, target, mode); |
| 5347 | } |
| 5348 | needed.add(uri); |
| 5349 | } |
| 5350 | } else { |
| 5351 | Intent clipIntent = clip.getItemAt(i).getIntent(); |
| 5352 | if (clipIntent != null) { |
| 5353 | NeededUriGrants newNeeded = checkGrantUriPermissionFromIntentLocked( |
| 5354 | callingUid, targetPkg, clipIntent, mode, needed); |
| 5355 | if (newNeeded != null) { |
| 5356 | needed = newNeeded; |
| 5357 | } |
| 5358 | } |
| 5359 | } |
| 5360 | } |
| 5361 | } |
| 5362 | |
| 5363 | return needed; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5364 | } |
| 5365 | |
| 5366 | /** |
| 5367 | * Like grantUriPermissionUncheckedLocked, but takes an Intent. |
| 5368 | */ |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5369 | void grantUriPermissionUncheckedFromIntentLocked(NeededUriGrants needed, |
| 5370 | UriPermissionOwner owner) { |
| 5371 | if (needed != null) { |
| 5372 | for (int i=0; i<needed.size(); i++) { |
| 5373 | grantUriPermissionUncheckedLocked(needed.targetUid, needed.targetPkg, |
| 5374 | needed.get(i), needed.flags, owner); |
| 5375 | } |
| 5376 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5377 | } |
| 5378 | |
| 5379 | void grantUriPermissionFromIntentLocked(int callingUid, |
| 5380 | String targetPkg, Intent intent, UriPermissionOwner owner) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5381 | NeededUriGrants needed = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, |
Dianne Hackborn | d9781fe | 2012-03-08 18:04:18 -0800 | [diff] [blame] | 5382 | intent, intent != null ? intent.getFlags() : 0, null); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5383 | if (needed == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5384 | return; |
| 5385 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 5386 | |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 5387 | grantUriPermissionUncheckedFromIntentLocked(needed, owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | public void grantUriPermission(IApplicationThread caller, String targetPkg, |
| 5391 | Uri uri, int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5392 | enforceNotIsolatedCaller("grantUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5393 | synchronized(this) { |
| 5394 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5395 | if (r == null) { |
| 5396 | throw new SecurityException("Unable to find app for caller " |
| 5397 | + caller |
| 5398 | + " when granting permission to uri " + uri); |
| 5399 | } |
| 5400 | if (targetPkg == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5401 | throw new IllegalArgumentException("null target"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5402 | } |
| 5403 | if (uri == null) { |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5404 | throw new IllegalArgumentException("null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5405 | } |
| 5406 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5407 | grantUriPermissionLocked(r.uid, targetPkg, uri, modeFlags, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5408 | null); |
| 5409 | } |
| 5410 | } |
| 5411 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5412 | void removeUriPermissionIfNeededLocked(UriPermission perm) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5413 | if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5414 | |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) { |
| 5415 | HashMap<Uri, UriPermission> perms |
| 5416 | = mGrantedUriPermissions.get(perm.uid); |
| 5417 | if (perms != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5418 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5419 | "Removing " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5420 | perms.remove(perm.uri); |
| 5421 | if (perms.size() == 0) { |
| 5422 | mGrantedUriPermissions.remove(perm.uid); |
| 5423 | } |
| 5424 | } |
| 5425 | } |
| 5426 | } |
| 5427 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5428 | private void revokeUriPermissionLocked(int callingUid, Uri uri, |
| 5429 | int modeFlags) { |
| 5430 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5431 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5432 | if (modeFlags == 0) { |
| 5433 | return; |
| 5434 | } |
| 5435 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5436 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5437 | "Revoking all granted permissions to " + uri); |
| 5438 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5439 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5440 | |
| 5441 | final String authority = uri.getAuthority(); |
| 5442 | ProviderInfo pi = null; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5443 | int userId = UserHandle.getUserId(callingUid); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5444 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5445 | if (cpr != null) { |
| 5446 | pi = cpr.info; |
| 5447 | } else { |
| 5448 | try { |
| 5449 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5450 | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5451 | } catch (RemoteException ex) { |
| 5452 | } |
| 5453 | } |
| 5454 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5455 | 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] | 5456 | return; |
| 5457 | } |
| 5458 | |
| 5459 | // Does the caller have this permission on the URI? |
Dianne Hackborn | 48058e8 | 2010-09-27 16:53:23 -0700 | [diff] [blame] | 5460 | if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5461 | // Right now, if you are not the original owner of the permission, |
| 5462 | // you are not allowed to revoke it. |
| 5463 | //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) { |
| 5464 | throw new SecurityException("Uid " + callingUid |
| 5465 | + " does not have permission to uri " + uri); |
| 5466 | //} |
| 5467 | } |
| 5468 | |
| 5469 | // Go through all of the permissions and remove any that match. |
| 5470 | final List<String> SEGMENTS = uri.getPathSegments(); |
| 5471 | if (SEGMENTS != null) { |
| 5472 | final int NS = SEGMENTS.size(); |
| 5473 | int N = mGrantedUriPermissions.size(); |
| 5474 | for (int i=0; i<N; i++) { |
| 5475 | HashMap<Uri, UriPermission> perms |
| 5476 | = mGrantedUriPermissions.valueAt(i); |
| 5477 | Iterator<UriPermission> it = perms.values().iterator(); |
| 5478 | toploop: |
| 5479 | while (it.hasNext()) { |
| 5480 | UriPermission perm = it.next(); |
| 5481 | Uri targetUri = perm.uri; |
| 5482 | if (!authority.equals(targetUri.getAuthority())) { |
| 5483 | continue; |
| 5484 | } |
| 5485 | List<String> targetSegments = targetUri.getPathSegments(); |
| 5486 | if (targetSegments == null) { |
| 5487 | continue; |
| 5488 | } |
| 5489 | if (targetSegments.size() < NS) { |
| 5490 | continue; |
| 5491 | } |
| 5492 | for (int j=0; j<NS; j++) { |
| 5493 | if (!SEGMENTS.get(j).equals(targetSegments.get(j))) { |
| 5494 | continue toploop; |
| 5495 | } |
| 5496 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5497 | if (DEBUG_URI_PERMISSION) Slog.v(TAG, |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 5498 | "Revoking " + perm.uid + " permission to " + perm.uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5499 | perm.clearModes(modeFlags); |
| 5500 | if (perm.modeFlags == 0) { |
| 5501 | it.remove(); |
| 5502 | } |
| 5503 | } |
| 5504 | if (perms.size() == 0) { |
| 5505 | mGrantedUriPermissions.remove( |
| 5506 | mGrantedUriPermissions.keyAt(i)); |
| 5507 | N--; |
| 5508 | i--; |
| 5509 | } |
| 5510 | } |
| 5511 | } |
| 5512 | } |
| 5513 | |
| 5514 | public void revokeUriPermission(IApplicationThread caller, Uri uri, |
| 5515 | int modeFlags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5516 | enforceNotIsolatedCaller("revokeUriPermission"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5517 | synchronized(this) { |
| 5518 | final ProcessRecord r = getRecordForAppLocked(caller); |
| 5519 | if (r == null) { |
| 5520 | throw new SecurityException("Unable to find app for caller " |
| 5521 | + caller |
| 5522 | + " when revoking permission to uri " + uri); |
| 5523 | } |
| 5524 | if (uri == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5525 | Slog.w(TAG, "revokeUriPermission: null uri"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5526 | return; |
| 5527 | } |
| 5528 | |
| 5529 | modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 5530 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 5531 | if (modeFlags == 0) { |
| 5532 | return; |
| 5533 | } |
| 5534 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5535 | final IPackageManager pm = AppGlobals.getPackageManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5536 | |
| 5537 | final String authority = uri.getAuthority(); |
| 5538 | ProviderInfo pi = null; |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5539 | ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5540 | if (cpr != null) { |
| 5541 | pi = cpr.info; |
| 5542 | } else { |
| 5543 | try { |
| 5544 | pi = pm.resolveContentProvider(authority, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5545 | PackageManager.GET_URI_PERMISSION_PATTERNS, r.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5546 | } catch (RemoteException ex) { |
| 5547 | } |
| 5548 | } |
| 5549 | if (pi == null) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 5550 | Slog.w(TAG, "No content provider found for permission revoke: " |
| 5551 | + uri.toSafeString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5552 | return; |
| 5553 | } |
| 5554 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 5555 | revokeUriPermissionLocked(r.uid, uri, modeFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5556 | } |
| 5557 | } |
| 5558 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5559 | @Override |
| 5560 | public IBinder newUriPermissionOwner(String name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 5561 | enforceNotIsolatedCaller("newUriPermissionOwner"); |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 5562 | synchronized(this) { |
| 5563 | UriPermissionOwner owner = new UriPermissionOwner(this, name); |
| 5564 | return owner.getExternalTokenLocked(); |
| 5565 | } |
| 5566 | } |
| 5567 | |
| 5568 | @Override |
| 5569 | public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg, |
| 5570 | Uri uri, int modeFlags) { |
| 5571 | synchronized(this) { |
| 5572 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5573 | if (owner == null) { |
| 5574 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5575 | } |
| 5576 | if (fromUid != Binder.getCallingUid()) { |
| 5577 | if (Binder.getCallingUid() != Process.myUid()) { |
| 5578 | // Only system code can grant URI permissions on behalf |
| 5579 | // of other users. |
| 5580 | throw new SecurityException("nice try"); |
| 5581 | } |
| 5582 | } |
| 5583 | if (targetPkg == null) { |
| 5584 | throw new IllegalArgumentException("null target"); |
| 5585 | } |
| 5586 | if (uri == null) { |
| 5587 | throw new IllegalArgumentException("null uri"); |
| 5588 | } |
| 5589 | |
| 5590 | grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner); |
| 5591 | } |
| 5592 | } |
| 5593 | |
| 5594 | @Override |
| 5595 | public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) { |
| 5596 | synchronized(this) { |
| 5597 | UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token); |
| 5598 | if (owner == null) { |
| 5599 | throw new IllegalArgumentException("Unknown owner: " + token); |
| 5600 | } |
| 5601 | |
| 5602 | if (uri == null) { |
| 5603 | owner.removeUriPermissionsLocked(mode); |
| 5604 | } else { |
| 5605 | owner.removeUriPermissionLocked(uri, mode); |
| 5606 | } |
| 5607 | } |
| 5608 | } |
| 5609 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5610 | public void showWaitingForDebugger(IApplicationThread who, boolean waiting) { |
| 5611 | synchronized (this) { |
| 5612 | ProcessRecord app = |
| 5613 | who != null ? getRecordForAppLocked(who) : null; |
| 5614 | if (app == null) return; |
| 5615 | |
| 5616 | Message msg = Message.obtain(); |
| 5617 | msg.what = WAIT_FOR_DEBUGGER_MSG; |
| 5618 | msg.obj = app; |
| 5619 | msg.arg1 = waiting ? 1 : 0; |
| 5620 | mHandler.sendMessage(msg); |
| 5621 | } |
| 5622 | } |
| 5623 | |
| 5624 | public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5625 | final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ); |
| 5626 | 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] | 5627 | outInfo.availMem = Process.getFreeMemory(); |
Dianne Hackborn | 59325eb | 2012-05-09 18:45:20 -0700 | [diff] [blame] | 5628 | outInfo.totalMem = Process.getTotalMemory(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5629 | outInfo.threshold = homeAppMem; |
| 5630 | outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2)); |
| 5631 | outInfo.hiddenAppThreshold = hiddenAppMem; |
| 5632 | outInfo.secondaryServerThreshold = mProcessList.getMemLevel( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 5633 | ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 5634 | outInfo.visibleAppThreshold = mProcessList.getMemLevel( |
| 5635 | ProcessList.VISIBLE_APP_ADJ); |
| 5636 | outInfo.foregroundAppThreshold = mProcessList.getMemLevel( |
| 5637 | ProcessList.FOREGROUND_APP_ADJ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5638 | } |
| 5639 | |
| 5640 | // ========================================================= |
| 5641 | // TASK MANAGEMENT |
| 5642 | // ========================================================= |
| 5643 | |
| 5644 | public List getTasks(int maxNum, int flags, |
| 5645 | IThumbnailReceiver receiver) { |
| 5646 | ArrayList list = new ArrayList(); |
| 5647 | |
| 5648 | PendingThumbnailsRecord pending = null; |
| 5649 | IApplicationThread topThumbnail = null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5650 | ActivityRecord topRecord = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5651 | |
| 5652 | synchronized(this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5653 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5654 | TAG, "getTasks: max=" + maxNum + ", flags=" + flags |
| 5655 | + ", receiver=" + receiver); |
| 5656 | |
| 5657 | if (checkCallingPermission(android.Manifest.permission.GET_TASKS) |
| 5658 | != PackageManager.PERMISSION_GRANTED) { |
| 5659 | if (receiver != null) { |
| 5660 | // If the caller wants to wait for pending thumbnails, |
| 5661 | // it ain't gonna get them. |
| 5662 | try { |
| 5663 | receiver.finished(); |
| 5664 | } catch (RemoteException ex) { |
| 5665 | } |
| 5666 | } |
| 5667 | String msg = "Permission Denial: getTasks() from pid=" |
| 5668 | + Binder.getCallingPid() |
| 5669 | + ", uid=" + Binder.getCallingUid() |
| 5670 | + " requires " + android.Manifest.permission.GET_TASKS; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5671 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5672 | throw new SecurityException(msg); |
| 5673 | } |
| 5674 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5675 | int pos = mMainStack.mHistory.size()-1; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5676 | ActivityRecord next = |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5677 | pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5678 | ActivityRecord top = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5679 | TaskRecord curTask = null; |
| 5680 | int numActivities = 0; |
| 5681 | int numRunning = 0; |
| 5682 | while (pos >= 0 && maxNum > 0) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5683 | final ActivityRecord r = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5684 | pos--; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5685 | next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5686 | |
| 5687 | // Initialize state for next task if needed. |
| 5688 | if (top == null || |
| 5689 | (top.state == ActivityState.INITIALIZING |
| 5690 | && top.task == r.task)) { |
| 5691 | top = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5692 | curTask = r.task; |
| 5693 | numActivities = numRunning = 0; |
| 5694 | } |
| 5695 | |
| 5696 | // Add 'r' into the current task. |
| 5697 | numActivities++; |
| 5698 | if (r.app != null && r.app.thread != null) { |
| 5699 | numRunning++; |
| 5700 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5701 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5702 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5703 | TAG, r.intent.getComponent().flattenToShortString() |
| 5704 | + ": task=" + r.task); |
| 5705 | |
| 5706 | // If the next one is a different task, generate a new |
| 5707 | // TaskInfo entry for what we have. |
| 5708 | if (next == null || next.task != curTask) { |
| 5709 | ActivityManager.RunningTaskInfo ci |
| 5710 | = new ActivityManager.RunningTaskInfo(); |
| 5711 | ci.id = curTask.taskId; |
| 5712 | ci.baseActivity = r.intent.getComponent(); |
| 5713 | ci.topActivity = top.intent.getComponent(); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 5714 | if (top.thumbHolder != null) { |
| 5715 | ci.description = top.thumbHolder.lastDescription; |
| 5716 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5717 | ci.numActivities = numActivities; |
| 5718 | ci.numRunning = numRunning; |
| 5719 | //System.out.println( |
| 5720 | // "#" + maxNum + ": " + " descr=" + ci.description); |
| 5721 | if (ci.thumbnail == null && receiver != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5722 | if (localLOGV) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5723 | TAG, "State=" + top.state + "Idle=" + top.idle |
| 5724 | + " app=" + top.app |
| 5725 | + " thr=" + (top.app != null ? top.app.thread : null)); |
| 5726 | if (top.state == ActivityState.RESUMED |
| 5727 | || top.state == ActivityState.PAUSING) { |
| 5728 | if (top.idle && top.app != null |
| 5729 | && top.app.thread != null) { |
| 5730 | topRecord = top; |
| 5731 | topThumbnail = top.app.thread; |
| 5732 | } else { |
| 5733 | top.thumbnailNeeded = true; |
| 5734 | } |
| 5735 | } |
| 5736 | if (pending == null) { |
| 5737 | pending = new PendingThumbnailsRecord(receiver); |
| 5738 | } |
| 5739 | pending.pendingRecords.add(top); |
| 5740 | } |
| 5741 | list.add(ci); |
| 5742 | maxNum--; |
| 5743 | top = null; |
| 5744 | } |
| 5745 | } |
| 5746 | |
| 5747 | if (pending != null) { |
| 5748 | mPendingThumbnails.add(pending); |
| 5749 | } |
| 5750 | } |
| 5751 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5752 | 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] | 5753 | |
| 5754 | if (topThumbnail != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5755 | if (localLOGV) Slog.v(TAG, "Requesting top thumbnail"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5756 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5757 | topThumbnail.requestThumbnail(topRecord.appToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5758 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 5759 | Slog.w(TAG, "Exception thrown when requesting thumbnail", e); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 5760 | sendPendingThumbnail(null, topRecord.appToken, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5761 | } |
| 5762 | } |
| 5763 | |
| 5764 | if (pending == null && receiver != null) { |
| 5765 | // In this case all thumbnails were available and the client |
| 5766 | // is being asked to be told when the remaining ones come in... |
| 5767 | // which is unusually, since the top-most currently running |
| 5768 | // activity should never have a canned thumbnail! Oh well. |
| 5769 | try { |
| 5770 | receiver.finished(); |
| 5771 | } catch (RemoteException ex) { |
| 5772 | } |
| 5773 | } |
| 5774 | |
| 5775 | return list; |
| 5776 | } |
| 5777 | |
| 5778 | public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5779 | int flags, int userId) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 5780 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
| 5781 | false, true, "getRecentTasks", null); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5782 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5783 | synchronized (this) { |
| 5784 | enforceCallingPermission(android.Manifest.permission.GET_TASKS, |
| 5785 | "getRecentTasks()"); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5786 | final boolean detailed = checkCallingPermission( |
| 5787 | android.Manifest.permission.GET_DETAILED_TASKS) |
| 5788 | == PackageManager.PERMISSION_GRANTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5789 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 5790 | IPackageManager pm = AppGlobals.getPackageManager(); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5791 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5792 | final int N = mRecentTasks.size(); |
| 5793 | ArrayList<ActivityManager.RecentTaskInfo> res |
| 5794 | = new ArrayList<ActivityManager.RecentTaskInfo>( |
| 5795 | maxNum < N ? maxNum : N); |
| 5796 | for (int i=0; i<N && maxNum > 0; i++) { |
| 5797 | TaskRecord tr = mRecentTasks.get(i); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5798 | // Only add calling user's recent tasks |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5799 | if (tr.userId != userId) continue; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5800 | // Return the entry if desired by the caller. We always return |
| 5801 | // the first entry, because callers always expect this to be the |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5802 | // foreground app. We may filter others if the caller has |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5803 | // not supplied RECENT_WITH_EXCLUDED and there is some reason |
| 5804 | // we should exclude the entry. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 5805 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 5806 | if (i == 0 |
| 5807 | || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5808 | || (tr.intent == null) |
| 5809 | || ((tr.intent.getFlags() |
| 5810 | &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) { |
| 5811 | ActivityManager.RecentTaskInfo rti |
| 5812 | = new ActivityManager.RecentTaskInfo(); |
| 5813 | rti.id = tr.numActivities > 0 ? tr.taskId : -1; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5814 | rti.persistentId = tr.taskId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5815 | rti.baseIntent = new Intent( |
| 5816 | tr.intent != null ? tr.intent : tr.affinityIntent); |
Dianne Hackborn | 8238e71 | 2012-04-24 11:15:40 -0700 | [diff] [blame] | 5817 | if (!detailed) { |
| 5818 | rti.baseIntent.replaceExtras((Bundle)null); |
| 5819 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5820 | rti.origActivity = tr.origActivity; |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 5821 | rti.description = tr.lastDescription; |
| 5822 | |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5823 | if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) { |
| 5824 | // Check whether this activity is currently available. |
| 5825 | try { |
| 5826 | if (rti.origActivity != null) { |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5827 | if (pm.getActivityInfo(rti.origActivity, 0, userId) |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 5828 | == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5829 | continue; |
| 5830 | } |
| 5831 | } else if (rti.baseIntent != null) { |
| 5832 | if (pm.queryIntentActivities(rti.baseIntent, |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 5833 | null, 0, userId) == null) { |
Dianne Hackborn | 53d9264d | 2010-04-13 12:49:14 -0700 | [diff] [blame] | 5834 | continue; |
| 5835 | } |
| 5836 | } |
| 5837 | } catch (RemoteException e) { |
| 5838 | // Will never happen. |
| 5839 | } |
| 5840 | } |
| 5841 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5842 | res.add(rti); |
| 5843 | maxNum--; |
| 5844 | } |
| 5845 | } |
| 5846 | return res; |
| 5847 | } |
| 5848 | } |
| 5849 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5850 | private TaskRecord taskForIdLocked(int id) { |
| 5851 | final int N = mRecentTasks.size(); |
| 5852 | for (int i=0; i<N; i++) { |
| 5853 | TaskRecord tr = mRecentTasks.get(i); |
| 5854 | if (tr.taskId == id) { |
| 5855 | return tr; |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5856 | } |
| 5857 | } |
| 5858 | return null; |
| 5859 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5860 | |
| 5861 | public ActivityManager.TaskThumbnails getTaskThumbnails(int id) { |
| 5862 | synchronized (this) { |
| 5863 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5864 | "getTaskThumbnails()"); |
| 5865 | TaskRecord tr = taskForIdLocked(id); |
| 5866 | if (tr != null) { |
| 5867 | return mMainStack.getTaskThumbnailsLocked(tr); |
| 5868 | } |
| 5869 | } |
| 5870 | return null; |
| 5871 | } |
| 5872 | |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 5873 | public Bitmap getTaskTopThumbnail(int id) { |
| 5874 | synchronized (this) { |
| 5875 | enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER, |
| 5876 | "getTaskTopThumbnail()"); |
| 5877 | TaskRecord tr = taskForIdLocked(id); |
| 5878 | if (tr != null) { |
| 5879 | return mMainStack.getTaskTopThumbnailLocked(tr); |
| 5880 | } |
| 5881 | } |
| 5882 | return null; |
| 5883 | } |
| 5884 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5885 | public boolean removeSubTask(int taskId, int subTaskIndex) { |
| 5886 | synchronized (this) { |
| 5887 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5888 | "removeSubTask()"); |
| 5889 | long ident = Binder.clearCallingIdentity(); |
| 5890 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5891 | return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex, |
| 5892 | true) != null; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5893 | } finally { |
| 5894 | Binder.restoreCallingIdentity(ident); |
| 5895 | } |
| 5896 | } |
| 5897 | } |
| 5898 | |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5899 | private void cleanUpRemovedTaskLocked(TaskRecord tr, int flags) { |
| 5900 | final boolean killProcesses = (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5901 | Intent baseIntent = new Intent( |
| 5902 | tr.intent != null ? tr.intent : tr.affinityIntent); |
| 5903 | ComponentName component = baseIntent.getComponent(); |
| 5904 | if (component == null) { |
| 5905 | Slog.w(TAG, "Now component for base intent of task: " + tr); |
| 5906 | return; |
| 5907 | } |
| 5908 | |
| 5909 | // Find any running services associated with this app. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 5910 | mServices.cleanUpRemovedTaskLocked(tr, component, baseIntent); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5911 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5912 | if (killProcesses) { |
| 5913 | // Find any running processes associated with this app. |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5914 | final String pkg = component.getPackageName(); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5915 | ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5916 | HashMap<String, SparseArray<ProcessRecord>> pmap = mProcessNames.getMap(); |
| 5917 | for (SparseArray<ProcessRecord> uids : pmap.values()) { |
| 5918 | for (int i=0; i<uids.size(); i++) { |
| 5919 | ProcessRecord proc = uids.valueAt(i); |
| 5920 | if (proc.userId != tr.userId) { |
| 5921 | continue; |
| 5922 | } |
| 5923 | if (!proc.pkgList.contains(pkg)) { |
| 5924 | continue; |
| 5925 | } |
| 5926 | procs.add(proc); |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5927 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5928 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5929 | |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5930 | // Kill the running processes. |
| 5931 | for (int i=0; i<procs.size(); i++) { |
| 5932 | ProcessRecord pr = procs.get(i); |
| 5933 | if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 5934 | Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 5935 | EventLog.writeEvent(EventLogTags.AM_KILL, pr.userId, pr.pid, |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5936 | pr.processName, pr.setAdj, "remove task"); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 5937 | pr.killedBackground = true; |
Dianne Hackborn | 8894cc5 | 2011-07-01 16:28:17 -0700 | [diff] [blame] | 5938 | Process.killProcessQuiet(pr.pid); |
| 5939 | } else { |
| 5940 | pr.waitingToKill = "remove task"; |
| 5941 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5942 | } |
| 5943 | } |
| 5944 | } |
| 5945 | |
| 5946 | public boolean removeTask(int taskId, int flags) { |
| 5947 | synchronized (this) { |
| 5948 | enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS, |
| 5949 | "removeTask()"); |
| 5950 | long ident = Binder.clearCallingIdentity(); |
| 5951 | try { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5952 | ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1, |
| 5953 | false); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5954 | if (r != null) { |
| 5955 | mRecentTasks.remove(r.task); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5956 | cleanUpRemovedTaskLocked(r.task, flags); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5957 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5958 | } else { |
| 5959 | TaskRecord tr = null; |
| 5960 | int i=0; |
| 5961 | while (i < mRecentTasks.size()) { |
| 5962 | TaskRecord t = mRecentTasks.get(i); |
| 5963 | if (t.taskId == taskId) { |
| 5964 | tr = t; |
| 5965 | break; |
| 5966 | } |
| 5967 | i++; |
| 5968 | } |
| 5969 | if (tr != null) { |
| 5970 | if (tr.numActivities <= 0) { |
| 5971 | // Caller is just removing a recent task that is |
| 5972 | // not actively running. That is easy! |
| 5973 | mRecentTasks.remove(i); |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 5974 | cleanUpRemovedTaskLocked(tr, flags); |
| 5975 | return true; |
Dianne Hackborn | eeb1dca | 2011-09-08 13:30:11 -0700 | [diff] [blame] | 5976 | } else { |
| 5977 | Slog.w(TAG, "removeTask: task " + taskId |
| 5978 | + " does not have activities to remove, " |
| 5979 | + " but numActivities=" + tr.numActivities |
| 5980 | + ": " + tr); |
| 5981 | } |
| 5982 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 5983 | } |
| 5984 | } finally { |
| 5985 | Binder.restoreCallingIdentity(ident); |
| 5986 | } |
| 5987 | } |
| 5988 | return false; |
| 5989 | } |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 5990 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5991 | private final int findAffinityTaskTopLocked(int startIndex, String affinity) { |
| 5992 | int j; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5993 | TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5994 | TaskRecord jt = startTask; |
| 5995 | |
| 5996 | // First look backwards |
| 5997 | for (j=startIndex-1; j>=0; j--) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 5998 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5999 | if (r.task != jt) { |
| 6000 | jt = r.task; |
| 6001 | if (affinity.equals(jt.affinity)) { |
| 6002 | return j; |
| 6003 | } |
| 6004 | } |
| 6005 | } |
| 6006 | |
| 6007 | // Now look forwards |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6008 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6009 | jt = startTask; |
| 6010 | for (j=startIndex+1; j<N; j++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6011 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6012 | if (r.task != jt) { |
| 6013 | if (affinity.equals(jt.affinity)) { |
| 6014 | return j; |
| 6015 | } |
| 6016 | jt = r.task; |
| 6017 | } |
| 6018 | } |
| 6019 | |
| 6020 | // Might it be at the top? |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6021 | 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] | 6022 | return N-1; |
| 6023 | } |
| 6024 | |
| 6025 | return -1; |
| 6026 | } |
| 6027 | |
| 6028 | /** |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 6029 | * TODO: Add mController hook |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6030 | */ |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6031 | public void moveTaskToFront(int task, int flags, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6032 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 6033 | "moveTaskToFront()"); |
| 6034 | |
| 6035 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6036 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 6037 | Binder.getCallingUid(), "Task to front")) { |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6038 | ActivityOptions.abort(options); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6039 | return; |
| 6040 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6041 | final long origId = Binder.clearCallingIdentity(); |
| 6042 | try { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6043 | TaskRecord tr = taskForIdLocked(task); |
| 6044 | if (tr != null) { |
| 6045 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 6046 | mMainStack.mUserLeaving = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6047 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6048 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 6049 | // Caller wants the home activity moved with it. To accomplish this, |
| 6050 | // we'll just move the home task to the top first. |
| 6051 | mMainStack.moveHomeToFrontLocked(); |
| 6052 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6053 | mMainStack.moveTaskToFrontLocked(tr, null, options); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 6054 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6055 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6056 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 6057 | ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6058 | if (hr.task.taskId == task) { |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 6059 | if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) { |
| 6060 | mMainStack.mUserLeaving = true; |
| 6061 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 6062 | if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) { |
| 6063 | // Caller wants the home activity moved with it. To accomplish this, |
| 6064 | // we'll just move the home task to the top first. |
| 6065 | mMainStack.moveHomeToFrontLocked(); |
| 6066 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6067 | mMainStack.moveTaskToFrontLocked(hr.task, null, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6068 | return; |
| 6069 | } |
| 6070 | } |
| 6071 | } finally { |
| 6072 | Binder.restoreCallingIdentity(origId); |
| 6073 | } |
Dianne Hackborn | 8078d8c | 2012-03-20 11:11:26 -0700 | [diff] [blame] | 6074 | ActivityOptions.abort(options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6075 | } |
| 6076 | } |
| 6077 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6078 | public void moveTaskToBack(int task) { |
| 6079 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 6080 | "moveTaskToBack()"); |
| 6081 | |
| 6082 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6083 | if (mMainStack.mResumedActivity != null |
| 6084 | && mMainStack.mResumedActivity.task.taskId == task) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6085 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 6086 | Binder.getCallingUid(), "Task to back")) { |
| 6087 | return; |
| 6088 | } |
| 6089 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6090 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6091 | mMainStack.moveTaskToBackLocked(task, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6092 | Binder.restoreCallingIdentity(origId); |
| 6093 | } |
| 6094 | } |
| 6095 | |
| 6096 | /** |
| 6097 | * Moves an activity, and all of the other activities within the same task, to the bottom |
| 6098 | * of the history stack. The activity's order within the task is unchanged. |
| 6099 | * |
| 6100 | * @param token A reference to the activity we wish to move |
| 6101 | * @param nonRoot If false then this only works if the activity is the root |
| 6102 | * of a task; if true it will work for any activity in a task. |
| 6103 | * @return Returns true if the move completed, false if not. |
| 6104 | */ |
| 6105 | public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6106 | enforceNotIsolatedCaller("moveActivityTaskToBack"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6107 | synchronized(this) { |
| 6108 | final long origId = Binder.clearCallingIdentity(); |
| 6109 | int taskId = getTaskForActivityLocked(token, !nonRoot); |
| 6110 | if (taskId >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6111 | return mMainStack.moveTaskToBackLocked(taskId, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6112 | } |
| 6113 | Binder.restoreCallingIdentity(origId); |
| 6114 | } |
| 6115 | return false; |
| 6116 | } |
| 6117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6118 | public void moveTaskBackwards(int task) { |
| 6119 | enforceCallingPermission(android.Manifest.permission.REORDER_TASKS, |
| 6120 | "moveTaskBackwards()"); |
| 6121 | |
| 6122 | synchronized(this) { |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 6123 | if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(), |
| 6124 | Binder.getCallingUid(), "Task backwards")) { |
| 6125 | return; |
| 6126 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6127 | final long origId = Binder.clearCallingIdentity(); |
| 6128 | moveTaskBackwardsLocked(task); |
| 6129 | Binder.restoreCallingIdentity(origId); |
| 6130 | } |
| 6131 | } |
| 6132 | |
| 6133 | private final void moveTaskBackwardsLocked(int task) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6134 | Slog.e(TAG, "moveTaskBackwards not yet implemented!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6135 | } |
| 6136 | |
| 6137 | public int getTaskForActivity(IBinder token, boolean onlyRoot) { |
| 6138 | synchronized(this) { |
| 6139 | return getTaskForActivityLocked(token, onlyRoot); |
| 6140 | } |
| 6141 | } |
| 6142 | |
| 6143 | int getTaskForActivityLocked(IBinder token, boolean onlyRoot) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6144 | final int N = mMainStack.mHistory.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6145 | TaskRecord lastTask = null; |
| 6146 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 6147 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 6148 | if (r.appToken == token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6149 | if (!onlyRoot || lastTask != r.task) { |
| 6150 | return r.task.taskId; |
| 6151 | } |
| 6152 | return -1; |
| 6153 | } |
| 6154 | lastTask = r.task; |
| 6155 | } |
| 6156 | |
| 6157 | return -1; |
| 6158 | } |
| 6159 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6160 | // ========================================================= |
| 6161 | // THUMBNAILS |
| 6162 | // ========================================================= |
| 6163 | |
| 6164 | public void reportThumbnail(IBinder token, |
| 6165 | Bitmap thumbnail, CharSequence description) { |
| 6166 | //System.out.println("Report thumbnail for " + token + ": " + thumbnail); |
| 6167 | final long origId = Binder.clearCallingIdentity(); |
| 6168 | sendPendingThumbnail(null, token, thumbnail, description, true); |
| 6169 | Binder.restoreCallingIdentity(origId); |
| 6170 | } |
| 6171 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6172 | final void sendPendingThumbnail(ActivityRecord r, IBinder token, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6173 | Bitmap thumbnail, CharSequence description, boolean always) { |
| 6174 | TaskRecord task = null; |
| 6175 | ArrayList receivers = null; |
| 6176 | |
| 6177 | //System.out.println("Send pending thumbnail: " + r); |
| 6178 | |
| 6179 | synchronized(this) { |
| 6180 | if (r == null) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 6181 | r = mMainStack.isInStackLocked(token); |
| 6182 | if (r == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6183 | return; |
| 6184 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6185 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 6186 | if (thumbnail == null && r.thumbHolder != null) { |
| 6187 | thumbnail = r.thumbHolder.lastThumbnail; |
| 6188 | description = r.thumbHolder.lastDescription; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6189 | } |
| 6190 | if (thumbnail == null && !always) { |
| 6191 | // If there is no thumbnail, and this entry is not actually |
| 6192 | // going away, then abort for now and pick up the next |
| 6193 | // thumbnail we get. |
| 6194 | return; |
| 6195 | } |
| 6196 | task = r.task; |
| 6197 | |
| 6198 | int N = mPendingThumbnails.size(); |
| 6199 | int i=0; |
| 6200 | while (i<N) { |
| 6201 | PendingThumbnailsRecord pr = |
| 6202 | (PendingThumbnailsRecord)mPendingThumbnails.get(i); |
| 6203 | //System.out.println("Looking in " + pr.pendingRecords); |
| 6204 | if (pr.pendingRecords.remove(r)) { |
| 6205 | if (receivers == null) { |
| 6206 | receivers = new ArrayList(); |
| 6207 | } |
| 6208 | receivers.add(pr); |
| 6209 | if (pr.pendingRecords.size() == 0) { |
| 6210 | pr.finished = true; |
| 6211 | mPendingThumbnails.remove(i); |
| 6212 | N--; |
| 6213 | continue; |
| 6214 | } |
| 6215 | } |
| 6216 | i++; |
| 6217 | } |
| 6218 | } |
| 6219 | |
| 6220 | if (receivers != null) { |
| 6221 | final int N = receivers.size(); |
| 6222 | for (int i=0; i<N; i++) { |
| 6223 | try { |
| 6224 | PendingThumbnailsRecord pr = |
| 6225 | (PendingThumbnailsRecord)receivers.get(i); |
| 6226 | pr.receiver.newThumbnail( |
| 6227 | task != null ? task.taskId : -1, thumbnail, description); |
| 6228 | if (pr.finished) { |
| 6229 | pr.receiver.finished(); |
| 6230 | } |
| 6231 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6232 | Slog.w(TAG, "Exception thrown when sending thumbnail", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6233 | } |
| 6234 | } |
| 6235 | } |
| 6236 | } |
| 6237 | |
| 6238 | // ========================================================= |
| 6239 | // CONTENT PROVIDERS |
| 6240 | // ========================================================= |
| 6241 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6242 | private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) { |
| 6243 | List<ProviderInfo> providers = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6244 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6245 | providers = AppGlobals.getPackageManager(). |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6246 | queryContentProviders(app.processName, app.uid, |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6247 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6248 | } catch (RemoteException ex) { |
| 6249 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6250 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6251 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid); |
| 6252 | int userId = app.userId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6253 | if (providers != null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6254 | int N = providers.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6255 | for (int i=0; i<N; i++) { |
| 6256 | ProviderInfo cpi = |
| 6257 | (ProviderInfo)providers.get(i); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6258 | boolean singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 6259 | cpi.name, cpi.flags); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6260 | if (singleton && UserHandle.getUserId(app.uid) != 0) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6261 | // This is a singleton provider, but a user besides the |
| 6262 | // default user is asking to initialize a process it runs |
| 6263 | // in... well, no, it doesn't actually run in this process, |
| 6264 | // it runs in the process of the default user. Get rid of it. |
| 6265 | providers.remove(i); |
| 6266 | N--; |
| 6267 | continue; |
| 6268 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6269 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6270 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6271 | ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6272 | if (cpr == null) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6273 | cpr = new ContentProviderRecord(this, cpi, app.info, comp, singleton); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6274 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6275 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6276 | if (DEBUG_MU) |
| 6277 | Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6278 | app.pubProviders.put(cpi.name, cpr); |
| 6279 | app.addPackage(cpi.applicationInfo.packageName); |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 6280 | ensurePackageDexOpt(cpi.applicationInfo.packageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6281 | } |
| 6282 | } |
| 6283 | return providers; |
| 6284 | } |
| 6285 | |
Jeff Sharkey | 110a6b6 | 2012-03-12 11:12:41 -0700 | [diff] [blame] | 6286 | /** |
| 6287 | * Check if {@link ProcessRecord} has a possible chance at accessing the |
| 6288 | * given {@link ProviderInfo}. Final permission checking is always done |
| 6289 | * in {@link ContentProvider}. |
| 6290 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6291 | private final String checkContentProviderPermissionLocked( |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6292 | ProviderInfo cpi, ProcessRecord r) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6293 | final int callingPid = (r != null) ? r.pid : Binder.getCallingPid(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6294 | final int callingUid = (r != null) ? r.uid : Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6295 | if (checkComponentPermission(cpi.readPermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6296 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6297 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6298 | return null; |
| 6299 | } |
| 6300 | if (checkComponentPermission(cpi.writePermission, callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6301 | cpi.applicationInfo.uid, cpi.exported) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6302 | == PackageManager.PERMISSION_GRANTED) { |
| 6303 | return null; |
| 6304 | } |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6305 | |
| 6306 | PathPermission[] pps = cpi.pathPermissions; |
| 6307 | if (pps != null) { |
| 6308 | int i = pps.length; |
| 6309 | while (i > 0) { |
| 6310 | i--; |
| 6311 | PathPermission pp = pps[i]; |
| 6312 | if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6313 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6314 | == PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6315 | return null; |
| 6316 | } |
| 6317 | if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6318 | cpi.applicationInfo.uid, cpi.exported) |
Dianne Hackborn | 2af632f | 2009-07-08 14:56:37 -0700 | [diff] [blame] | 6319 | == PackageManager.PERMISSION_GRANTED) { |
| 6320 | return null; |
| 6321 | } |
| 6322 | } |
| 6323 | } |
| 6324 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6325 | HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid); |
| 6326 | if (perms != null) { |
| 6327 | for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) { |
| 6328 | if (uri.getKey().getAuthority().equals(cpi.authority)) { |
| 6329 | return null; |
| 6330 | } |
| 6331 | } |
| 6332 | } |
| 6333 | |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 6334 | String msg; |
| 6335 | if (!cpi.exported) { |
| 6336 | msg = "Permission Denial: opening provider " + cpi.name |
| 6337 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6338 | + ", uid=" + callingUid + ") that is not exported from uid " |
| 6339 | + cpi.applicationInfo.uid; |
| 6340 | } else { |
| 6341 | msg = "Permission Denial: opening provider " + cpi.name |
| 6342 | + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid |
| 6343 | + ", uid=" + callingUid + ") requires " |
| 6344 | + cpi.readPermission + " or " + cpi.writePermission; |
| 6345 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6346 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6347 | return msg; |
| 6348 | } |
| 6349 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6350 | ContentProviderConnection incProviderCountLocked(ProcessRecord r, |
| 6351 | final ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6352 | if (r != null) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6353 | for (int i=0; i<r.conProviders.size(); i++) { |
| 6354 | ContentProviderConnection conn = r.conProviders.get(i); |
| 6355 | if (conn.provider == cpr) { |
| 6356 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6357 | "Adding provider requested by " |
| 6358 | + r.processName + " from process " |
| 6359 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
| 6360 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6361 | if (stable) { |
| 6362 | conn.stableCount++; |
| 6363 | conn.numStableIncs++; |
| 6364 | } else { |
| 6365 | conn.unstableCount++; |
| 6366 | conn.numUnstableIncs++; |
| 6367 | } |
| 6368 | return conn; |
| 6369 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6370 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6371 | ContentProviderConnection conn = new ContentProviderConnection(cpr, r); |
| 6372 | if (stable) { |
| 6373 | conn.stableCount = 1; |
| 6374 | conn.numStableIncs = 1; |
| 6375 | } else { |
| 6376 | conn.unstableCount = 1; |
| 6377 | conn.numUnstableIncs = 1; |
| 6378 | } |
| 6379 | cpr.connections.add(conn); |
| 6380 | r.conProviders.add(conn); |
| 6381 | return conn; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6382 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6383 | cpr.addExternalProcessHandleLocked(externalProcessToken); |
| 6384 | return null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6385 | } |
| 6386 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6387 | boolean decProviderCountLocked(ContentProviderConnection conn, |
| 6388 | ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) { |
| 6389 | if (conn != null) { |
| 6390 | cpr = conn.provider; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6391 | if (DEBUG_PROVIDER) Slog.v(TAG, |
| 6392 | "Removing provider requested by " |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6393 | + conn.client.processName + " from process " |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6394 | + cpr.info.processName + ": " + cpr.name.flattenToShortString() |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6395 | + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount); |
| 6396 | if (stable) { |
| 6397 | conn.stableCount--; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6398 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6399 | conn.unstableCount--; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6400 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6401 | if (conn.stableCount == 0 && conn.unstableCount == 0) { |
| 6402 | cpr.connections.remove(conn); |
| 6403 | conn.client.conProviders.remove(conn); |
| 6404 | return true; |
| 6405 | } |
| 6406 | return false; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6407 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6408 | cpr.removeExternalProcessHandleLocked(externalProcessToken); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6409 | return false; |
| 6410 | } |
| 6411 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6412 | private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6413 | String name, IBinder token, boolean stable, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6414 | ContentProviderRecord cpr; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6415 | ContentProviderConnection conn = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6416 | ProviderInfo cpi = null; |
| 6417 | |
| 6418 | synchronized(this) { |
| 6419 | ProcessRecord r = null; |
| 6420 | if (caller != null) { |
| 6421 | r = getRecordForAppLocked(caller); |
| 6422 | if (r == null) { |
| 6423 | throw new SecurityException( |
| 6424 | "Unable to find app for caller " + caller |
| 6425 | + " (pid=" + Binder.getCallingPid() |
| 6426 | + ") when getting content provider " + name); |
| 6427 | } |
| 6428 | } |
| 6429 | |
| 6430 | // First check if this content provider has been published... |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6431 | cpr = mProviderMap.getProviderByName(name, userId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6432 | boolean providerRunning = cpr != null; |
| 6433 | if (providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6434 | cpi = cpr.info; |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6435 | String msg; |
| 6436 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6437 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6438 | } |
| 6439 | |
| 6440 | if (r != null && cpr.canRunHere(r)) { |
| 6441 | // This provider has been published or is in the process |
| 6442 | // of being published... but it is also allowed to run |
| 6443 | // in the caller's process, so don't make a connection |
| 6444 | // and just let the caller instantiate its own instance. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6445 | ContentProviderHolder holder = cpr.newHolder(null); |
| 6446 | // don't give caller the provider object, it needs |
| 6447 | // to make its own. |
| 6448 | holder.provider = null; |
| 6449 | return holder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6450 | } |
| 6451 | |
| 6452 | final long origId = Binder.clearCallingIdentity(); |
| 6453 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6454 | // 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] | 6455 | // return it right away. |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6456 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6457 | if (conn != null && (conn.stableCount+conn.unstableCount) == 1) { |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6458 | if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 6459 | // If this is a perceptible app accessing the provider, |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6460 | // make sure to count it as being accessed and thus |
| 6461 | // back up on the LRU list. This is good because |
| 6462 | // content providers are often expensive to start. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 6463 | updateLruProcessLocked(cpr.proc, false); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 6464 | } |
Dianne Hackborn | 6f86c0e | 2010-05-11 14:20:52 -0700 | [diff] [blame] | 6465 | } |
| 6466 | |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6467 | if (cpr.proc != null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6468 | if (false) { |
| 6469 | if (cpr.name.flattenToShortString().equals( |
| 6470 | "com.android.providers.calendar/.CalendarProvider2")) { |
| 6471 | Slog.v(TAG, "****************** KILLING " |
| 6472 | + cpr.name.flattenToShortString()); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6473 | Process.killProcess(cpr.proc.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6474 | } |
| 6475 | } |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6476 | boolean success = updateOomAdjLocked(cpr.proc); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6477 | if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success); |
| 6478 | // NOTE: there is still a race here where a signal could be |
| 6479 | // pending on the process even though we managed to update its |
| 6480 | // adj level. Not sure what to do about this, but at least |
| 6481 | // the race is now smaller. |
| 6482 | if (!success) { |
| 6483 | // Uh oh... it looks like the provider's process |
| 6484 | // has been killed on us. We need to wait for a new |
| 6485 | // process to be started, and make sure its death |
| 6486 | // doesn't kill our process. |
| 6487 | Slog.i(TAG, |
| 6488 | "Existing provider " + cpr.name.flattenToShortString() |
| 6489 | + " is crashing; detaching " + r); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6490 | boolean lastRef = decProviderCountLocked(conn, cpr, token, stable); |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6491 | appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6492 | if (!lastRef) { |
| 6493 | // This wasn't the last ref our process had on |
| 6494 | // the provider... we have now been killed, bail. |
| 6495 | return null; |
| 6496 | } |
| 6497 | providerRunning = false; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6498 | conn = null; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6499 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6500 | } |
| 6501 | |
| 6502 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6503 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6504 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6505 | boolean singleton; |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 6506 | if (!providerRunning) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6507 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6508 | cpi = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 6509 | resolveContentProvider(name, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6510 | STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6511 | } catch (RemoteException ex) { |
| 6512 | } |
| 6513 | if (cpi == null) { |
| 6514 | return null; |
| 6515 | } |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6516 | singleton = isSingleton(cpi.processName, cpi.applicationInfo, |
| 6517 | cpi.name, cpi.flags); |
| 6518 | if (singleton) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 6519 | userId = 0; |
| 6520 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6521 | cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, userId); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6522 | |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 6523 | String msg; |
| 6524 | if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) { |
| 6525 | throw new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6526 | } |
| 6527 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 6528 | if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6529 | && !cpi.processName.equals("system")) { |
| 6530 | // If this content provider does not run in the system |
| 6531 | // process, and the system is not yet ready to run other |
| 6532 | // processes, then fail fast instead of hanging. |
| 6533 | throw new IllegalArgumentException( |
| 6534 | "Attempt to launch content provider before system ready"); |
| 6535 | } |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6536 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 6537 | // Make sure that the user who owns this provider is started. If not, |
| 6538 | // we don't want to allow it to run. |
| 6539 | if (mStartedUsers.get(userId) == null) { |
| 6540 | Slog.w(TAG, "Unable to launch app " |
| 6541 | + cpi.applicationInfo.packageName + "/" |
| 6542 | + cpi.applicationInfo.uid + " for provider " |
| 6543 | + name + ": user " + userId + " is stopped"); |
| 6544 | return null; |
| 6545 | } |
| 6546 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6547 | ComponentName comp = new ComponentName(cpi.packageName, cpi.name); |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6548 | cpr = mProviderMap.getProviderByClass(comp, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6549 | final boolean firstClass = cpr == null; |
| 6550 | if (firstClass) { |
| 6551 | try { |
| 6552 | ApplicationInfo ai = |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 6553 | AppGlobals.getPackageManager(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6554 | getApplicationInfo( |
| 6555 | cpi.applicationInfo.packageName, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6556 | STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6557 | if (ai == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6558 | Slog.w(TAG, "No package info for content provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6559 | + cpi.name); |
| 6560 | return null; |
| 6561 | } |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6562 | ai = getAppInfoForUser(ai, userId); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 6563 | cpr = new ContentProviderRecord(this, cpi, ai, comp, singleton); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6564 | } catch (RemoteException ex) { |
| 6565 | // pm is in same process, this will never happen. |
| 6566 | } |
| 6567 | } |
| 6568 | |
| 6569 | if (r != null && cpr.canRunHere(r)) { |
| 6570 | // If this is a multiprocess provider, then just return its |
| 6571 | // info and allow the caller to instantiate it. Only do |
| 6572 | // this if the provider is the same user as the caller's |
| 6573 | // process, or can run as root (so can be in any process). |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6574 | return cpr.newHolder(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6575 | } |
| 6576 | |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6577 | if (DEBUG_PROVIDER) { |
| 6578 | RuntimeException e = new RuntimeException("here"); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6579 | Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.uid |
Dianne Hackborn | a1e989b | 2009-09-01 19:54:29 -0700 | [diff] [blame] | 6580 | + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6581 | } |
| 6582 | |
| 6583 | // This is single process, and our app is now connecting to it. |
| 6584 | // See if we are already in the process of launching this |
| 6585 | // provider. |
| 6586 | final int N = mLaunchingProviders.size(); |
| 6587 | int i; |
| 6588 | for (i=0; i<N; i++) { |
| 6589 | if (mLaunchingProviders.get(i) == cpr) { |
| 6590 | break; |
| 6591 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6592 | } |
| 6593 | |
| 6594 | // If the provider is not already being launched, then get it |
| 6595 | // started. |
| 6596 | if (i >= N) { |
| 6597 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6598 | |
| 6599 | try { |
| 6600 | // Content provider is now in use, its package can't be stopped. |
| 6601 | try { |
| 6602 | AppGlobals.getPackageManager().setPackageStoppedState( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 6603 | cpr.appInfo.packageName, false, userId); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6604 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 6605 | } catch (IllegalArgumentException e) { |
| 6606 | Slog.w(TAG, "Failed trying to unstop package " |
| 6607 | + cpr.appInfo.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6608 | } |
| 6609 | |
| 6610 | ProcessRecord proc = startProcessLocked(cpi.processName, |
| 6611 | cpr.appInfo, false, 0, "content provider", |
| 6612 | new ComponentName(cpi.applicationInfo.packageName, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6613 | cpi.name), false, false); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 6614 | if (proc == null) { |
| 6615 | Slog.w(TAG, "Unable to launch app " |
| 6616 | + cpi.applicationInfo.packageName + "/" |
| 6617 | + cpi.applicationInfo.uid + " for provider " |
| 6618 | + name + ": process is bad"); |
| 6619 | return null; |
| 6620 | } |
| 6621 | cpr.launchingApp = proc; |
| 6622 | mLaunchingProviders.add(cpr); |
| 6623 | } finally { |
| 6624 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6625 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6626 | } |
| 6627 | |
| 6628 | // Make sure the provider is published (the same provider class |
| 6629 | // may be published under multiple names). |
| 6630 | if (firstClass) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6631 | mProviderMap.putProviderByClass(comp, cpr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6632 | } |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6633 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6634 | mProviderMap.putProviderByName(name, cpr); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6635 | conn = incProviderCountLocked(r, cpr, token, stable); |
| 6636 | if (conn != null) { |
| 6637 | conn.waiting = true; |
| 6638 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6639 | } |
| 6640 | } |
| 6641 | |
| 6642 | // Wait for the provider to be published... |
| 6643 | synchronized (cpr) { |
| 6644 | while (cpr.provider == null) { |
| 6645 | if (cpr.launchingApp == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6646 | Slog.w(TAG, "Unable to launch app " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6647 | + cpi.applicationInfo.packageName + "/" |
| 6648 | + cpi.applicationInfo.uid + " for provider " |
| 6649 | + name + ": launching app became null"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 6650 | EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 6651 | UserHandle.getUserId(cpi.applicationInfo.uid), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6652 | cpi.applicationInfo.packageName, |
| 6653 | cpi.applicationInfo.uid, name); |
| 6654 | return null; |
| 6655 | } |
| 6656 | try { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6657 | if (DEBUG_MU) { |
| 6658 | Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp=" |
| 6659 | + cpr.launchingApp); |
| 6660 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6661 | if (conn != null) { |
| 6662 | conn.waiting = true; |
| 6663 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6664 | cpr.wait(); |
| 6665 | } catch (InterruptedException ex) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6666 | } finally { |
| 6667 | if (conn != null) { |
| 6668 | conn.waiting = false; |
| 6669 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6670 | } |
| 6671 | } |
| 6672 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6673 | return cpr != null ? cpr.newHolder(conn) : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6674 | } |
| 6675 | |
| 6676 | public final ContentProviderHolder getContentProvider( |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6677 | IApplicationThread caller, String name, int userId, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6678 | enforceNotIsolatedCaller("getContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6679 | if (caller == null) { |
| 6680 | String msg = "null IApplicationThread when getting content provider " |
| 6681 | + name; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6682 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6683 | throw new SecurityException(msg); |
| 6684 | } |
| 6685 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6686 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6687 | false, true, "getContentProvider", null); |
| 6688 | return getContentProviderImpl(caller, name, null, stable, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6689 | } |
| 6690 | |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6691 | public ContentProviderHolder getContentProviderExternal( |
| 6692 | String name, int userId, IBinder token) { |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6693 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6694 | "Do not have permission in call getContentProviderExternal()"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6695 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 6696 | false, true, "getContentProvider", null); |
| 6697 | return getContentProviderExternalUnchecked(name, token, userId); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6698 | } |
| 6699 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6700 | private ContentProviderHolder getContentProviderExternalUnchecked(String name, |
| 6701 | IBinder token, int userId) { |
| 6702 | return getContentProviderImpl(null, name, token, true, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6703 | } |
| 6704 | |
| 6705 | /** |
| 6706 | * Drop a content provider from a ProcessRecord's bookkeeping |
| 6707 | * @param cpr |
| 6708 | */ |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6709 | public void removeContentProvider(IBinder connection, boolean stable) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6710 | enforceNotIsolatedCaller("removeContentProvider"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6711 | synchronized (this) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6712 | ContentProviderConnection conn; |
| 6713 | try { |
| 6714 | conn = (ContentProviderConnection)connection; |
| 6715 | } catch (ClassCastException e) { |
| 6716 | String msg ="removeContentProvider: " + connection |
| 6717 | + " not a ContentProviderConnection"; |
| 6718 | Slog.w(TAG, msg); |
| 6719 | throw new IllegalArgumentException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6720 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6721 | if (conn == null) { |
| 6722 | throw new NullPointerException("connection is null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6723 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6724 | if (decProviderCountLocked(conn, null, null, stable)) { |
| 6725 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6726 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6727 | } |
| 6728 | } |
| 6729 | |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6730 | public void removeContentProviderExternal(String name, IBinder token) { |
| 6731 | enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY, |
| 6732 | "Do not have permission in call removeContentProviderExternal()"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6733 | removeContentProviderExternalUnchecked(name, token, UserHandle.getCallingUserId()); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6734 | } |
| 6735 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6736 | private void removeContentProviderExternalUnchecked(String name, IBinder token, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6737 | synchronized (this) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6738 | ContentProviderRecord cpr = mProviderMap.getProviderByName(name, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6739 | if(cpr == null) { |
| 6740 | //remove from mProvidersByClass |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 6741 | 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] | 6742 | return; |
| 6743 | } |
| 6744 | |
| 6745 | //update content provider record entry info |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6746 | ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6747 | ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, userId); |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 6748 | if (localCpr.hasExternalProcessHandles()) { |
| 6749 | if (localCpr.removeExternalProcessHandleLocked(token)) { |
| 6750 | updateOomAdjLocked(); |
| 6751 | } else { |
| 6752 | Slog.e(TAG, "Attmpt to remove content provider " + localCpr |
| 6753 | + " with no external reference for token: " |
| 6754 | + token + "."); |
| 6755 | } |
| 6756 | } else { |
| 6757 | Slog.e(TAG, "Attmpt to remove content provider: " + localCpr |
| 6758 | + " with no external references."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6759 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6760 | } |
| 6761 | } |
| 6762 | |
| 6763 | public final void publishContentProviders(IApplicationThread caller, |
| 6764 | List<ContentProviderHolder> providers) { |
| 6765 | if (providers == null) { |
| 6766 | return; |
| 6767 | } |
| 6768 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6769 | enforceNotIsolatedCaller("publishContentProviders"); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6770 | synchronized (this) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6771 | final ProcessRecord r = getRecordForAppLocked(caller); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6772 | if (DEBUG_MU) |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6773 | Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6774 | if (r == null) { |
| 6775 | throw new SecurityException( |
| 6776 | "Unable to find app for caller " + caller |
| 6777 | + " (pid=" + Binder.getCallingPid() |
| 6778 | + ") when publishing content providers"); |
| 6779 | } |
| 6780 | |
| 6781 | final long origId = Binder.clearCallingIdentity(); |
| 6782 | |
| 6783 | final int N = providers.size(); |
| 6784 | for (int i=0; i<N; i++) { |
| 6785 | ContentProviderHolder src = providers.get(i); |
| 6786 | if (src == null || src.info == null || src.provider == null) { |
| 6787 | continue; |
| 6788 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 6789 | ContentProviderRecord dst = r.pubProviders.get(src.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6790 | if (DEBUG_MU) |
| 6791 | Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6792 | if (dst != null) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 6793 | ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6794 | mProviderMap.putProviderByClass(comp, dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6795 | String names[] = dst.info.authority.split(";"); |
| 6796 | for (int j = 0; j < names.length; j++) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 6797 | mProviderMap.putProviderByName(names[j], dst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6798 | } |
| 6799 | |
| 6800 | int NL = mLaunchingProviders.size(); |
| 6801 | int j; |
| 6802 | for (j=0; j<NL; j++) { |
| 6803 | if (mLaunchingProviders.get(j) == dst) { |
| 6804 | mLaunchingProviders.remove(j); |
| 6805 | j--; |
| 6806 | NL--; |
| 6807 | } |
| 6808 | } |
| 6809 | synchronized (dst) { |
| 6810 | dst.provider = src.provider; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 6811 | dst.proc = r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6812 | dst.notifyAll(); |
| 6813 | } |
| 6814 | updateOomAdjLocked(r); |
| 6815 | } |
| 6816 | } |
| 6817 | |
| 6818 | Binder.restoreCallingIdentity(origId); |
| 6819 | } |
| 6820 | } |
| 6821 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6822 | public boolean refContentProvider(IBinder connection, int stable, int unstable) { |
| 6823 | ContentProviderConnection conn; |
| 6824 | try { |
| 6825 | conn = (ContentProviderConnection)connection; |
| 6826 | } catch (ClassCastException e) { |
| 6827 | String msg ="refContentProvider: " + connection |
| 6828 | + " not a ContentProviderConnection"; |
| 6829 | Slog.w(TAG, msg); |
| 6830 | throw new IllegalArgumentException(msg); |
| 6831 | } |
| 6832 | if (conn == null) { |
| 6833 | throw new NullPointerException("connection is null"); |
| 6834 | } |
| 6835 | |
| 6836 | synchronized (this) { |
| 6837 | if (stable > 0) { |
| 6838 | conn.numStableIncs += stable; |
| 6839 | } |
| 6840 | stable = conn.stableCount + stable; |
| 6841 | if (stable < 0) { |
| 6842 | throw new IllegalStateException("stableCount < 0: " + stable); |
| 6843 | } |
| 6844 | |
| 6845 | if (unstable > 0) { |
| 6846 | conn.numUnstableIncs += unstable; |
| 6847 | } |
| 6848 | unstable = conn.unstableCount + unstable; |
| 6849 | if (unstable < 0) { |
| 6850 | throw new IllegalStateException("unstableCount < 0: " + unstable); |
| 6851 | } |
| 6852 | |
| 6853 | if ((stable+unstable) <= 0) { |
| 6854 | throw new IllegalStateException("ref counts can't go to zero here: stable=" |
| 6855 | + stable + " unstable=" + unstable); |
| 6856 | } |
| 6857 | conn.stableCount = stable; |
| 6858 | conn.unstableCount = unstable; |
| 6859 | return !conn.dead; |
| 6860 | } |
| 6861 | } |
| 6862 | |
| 6863 | public void unstableProviderDied(IBinder connection) { |
| 6864 | ContentProviderConnection conn; |
| 6865 | try { |
| 6866 | conn = (ContentProviderConnection)connection; |
| 6867 | } catch (ClassCastException e) { |
| 6868 | String msg ="refContentProvider: " + connection |
| 6869 | + " not a ContentProviderConnection"; |
| 6870 | Slog.w(TAG, msg); |
| 6871 | throw new IllegalArgumentException(msg); |
| 6872 | } |
| 6873 | if (conn == null) { |
| 6874 | throw new NullPointerException("connection is null"); |
| 6875 | } |
| 6876 | |
| 6877 | // Safely retrieve the content provider associated with the connection. |
| 6878 | IContentProvider provider; |
| 6879 | synchronized (this) { |
| 6880 | provider = conn.provider.provider; |
| 6881 | } |
| 6882 | |
| 6883 | if (provider == null) { |
| 6884 | // Um, yeah, we're way ahead of you. |
| 6885 | return; |
| 6886 | } |
| 6887 | |
| 6888 | // Make sure the caller is being honest with us. |
| 6889 | if (provider.asBinder().pingBinder()) { |
| 6890 | // Er, no, still looks good to us. |
| 6891 | synchronized (this) { |
| 6892 | Slog.w(TAG, "unstableProviderDied: caller " + Binder.getCallingUid() |
| 6893 | + " says " + conn + " died, but we don't agree"); |
| 6894 | return; |
| 6895 | } |
| 6896 | } |
| 6897 | |
| 6898 | // Well look at that! It's dead! |
| 6899 | synchronized (this) { |
| 6900 | if (conn.provider.provider != provider) { |
| 6901 | // But something changed... good enough. |
| 6902 | return; |
| 6903 | } |
| 6904 | |
| 6905 | ProcessRecord proc = conn.provider.proc; |
| 6906 | if (proc == null || proc.thread == null) { |
| 6907 | // Seems like the process is already cleaned up. |
| 6908 | return; |
| 6909 | } |
| 6910 | |
| 6911 | // As far as we're concerned, this is just like receiving a |
| 6912 | // death notification... just a bit prematurely. |
| 6913 | Slog.i(TAG, "Process " + proc.processName + " (pid " + proc.pid |
| 6914 | + ") early provider death"); |
Dianne Hackborn | bd145db | 2012-06-05 16:20:46 -0700 | [diff] [blame] | 6915 | final long ident = Binder.clearCallingIdentity(); |
| 6916 | try { |
| 6917 | appDiedLocked(proc, proc.pid, proc.thread); |
| 6918 | } finally { |
| 6919 | Binder.restoreCallingIdentity(ident); |
| 6920 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 6921 | } |
| 6922 | } |
| 6923 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6924 | public static final void installSystemProviders() { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 6925 | List<ProviderInfo> providers; |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6926 | synchronized (mSelf) { |
| 6927 | ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID); |
| 6928 | providers = mSelf.generateApplicationProvidersLocked(app); |
Dianne Hackborn | 5c83a5f | 2010-03-12 16:46:46 -0800 | [diff] [blame] | 6929 | if (providers != null) { |
| 6930 | for (int i=providers.size()-1; i>=0; i--) { |
| 6931 | ProviderInfo pi = (ProviderInfo)providers.get(i); |
| 6932 | if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 6933 | Slog.w(TAG, "Not installing system proc provider " + pi.name |
| 6934 | + ": not system .apk"); |
| 6935 | providers.remove(i); |
| 6936 | } |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 6937 | } |
| 6938 | } |
| 6939 | } |
Josh Bartel | 2ecce34 | 2010-02-25 10:55:48 -0600 | [diff] [blame] | 6940 | if (providers != null) { |
| 6941 | mSystemThread.installSystemProviders(providers); |
| 6942 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 6943 | |
| 6944 | mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 6945 | |
| 6946 | mSelf.mUsageStatsService.monitorPackages(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6947 | } |
| 6948 | |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6949 | /** |
| 6950 | * Allows app to retrieve the MIME type of a URI without having permission |
| 6951 | * to access its content provider. |
| 6952 | * |
| 6953 | * CTS tests for this functionality can be run with "runtest cts-appsecurity". |
| 6954 | * |
| 6955 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 6956 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
| 6957 | */ |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 6958 | public String getProviderMimeType(Uri uri, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 6959 | enforceNotIsolatedCaller("getProviderMimeType"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 6960 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 6961 | userId, false, true, "getProviderMimeType", null); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6962 | final String name = uri.getAuthority(); |
| 6963 | final long ident = Binder.clearCallingIdentity(); |
| 6964 | ContentProviderHolder holder = null; |
| 6965 | |
| 6966 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6967 | holder = getContentProviderExternalUnchecked(name, null, userId); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6968 | if (holder != null) { |
| 6969 | return holder.provider.getType(uri); |
| 6970 | } |
| 6971 | } catch (RemoteException e) { |
| 6972 | Log.w(TAG, "Content provider dead retrieving " + uri, e); |
| 6973 | return null; |
| 6974 | } finally { |
| 6975 | if (holder != null) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 6976 | removeContentProviderExternalUnchecked(name, null, userId); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 6977 | } |
| 6978 | Binder.restoreCallingIdentity(ident); |
| 6979 | } |
| 6980 | |
| 6981 | return null; |
| 6982 | } |
| 6983 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6984 | // ========================================================= |
| 6985 | // GLOBAL MANAGEMENT |
| 6986 | // ========================================================= |
| 6987 | |
| 6988 | final ProcessRecord newProcessRecordLocked(IApplicationThread thread, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6989 | ApplicationInfo info, String customProcess, boolean isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6990 | String proc = customProcess != null ? customProcess : info.processName; |
| 6991 | BatteryStatsImpl.Uid.Proc ps = null; |
| 6992 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6993 | int uid = info.uid; |
| 6994 | if (isolated) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 6995 | int userId = UserHandle.getUserId(uid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 6996 | int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1; |
| 6997 | uid = 0; |
| 6998 | while (true) { |
| 6999 | if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID |
| 7000 | || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) { |
| 7001 | mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID; |
| 7002 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 7003 | uid = UserHandle.getUid(userId, mNextIsolatedProcessUid); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7004 | mNextIsolatedProcessUid++; |
| 7005 | if (mIsolatedProcesses.indexOfKey(uid) < 0) { |
| 7006 | // No process for this uid, use it. |
| 7007 | break; |
| 7008 | } |
| 7009 | stepsLeft--; |
| 7010 | if (stepsLeft <= 0) { |
| 7011 | return null; |
| 7012 | } |
| 7013 | } |
| 7014 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7015 | synchronized (stats) { |
| 7016 | ps = stats.getProcessStatsLocked(info.uid, proc); |
| 7017 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7018 | return new ProcessRecord(ps, thread, info, proc, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7019 | } |
| 7020 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7021 | final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated) { |
| 7022 | ProcessRecord app; |
| 7023 | if (!isolated) { |
| 7024 | app = getProcessRecordLocked(info.processName, info.uid); |
| 7025 | } else { |
| 7026 | app = null; |
| 7027 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7028 | |
| 7029 | if (app == null) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7030 | app = newProcessRecordLocked(null, info, null, isolated); |
| 7031 | mProcessNames.put(info.processName, app.uid, app); |
| 7032 | if (isolated) { |
| 7033 | mIsolatedProcesses.put(app.uid, app); |
| 7034 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7035 | updateLruProcessLocked(app, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7036 | } |
| 7037 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7038 | // This package really, really can not be stopped. |
| 7039 | try { |
| 7040 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 7041 | info.packageName, false, UserHandle.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7042 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 7043 | } catch (IllegalArgumentException e) { |
| 7044 | Slog.w(TAG, "Failed trying to unstop package " |
| 7045 | + info.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 7046 | } |
| 7047 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7048 | if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) |
| 7049 | == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { |
| 7050 | app.persistent = true; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7051 | app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7052 | } |
| 7053 | if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) { |
| 7054 | mPersistentStartingProcesses.add(app); |
| 7055 | startProcessLocked(app, "added application", app.processName); |
| 7056 | } |
| 7057 | |
| 7058 | return app; |
| 7059 | } |
| 7060 | |
| 7061 | public void unhandledBack() { |
| 7062 | enforceCallingPermission(android.Manifest.permission.FORCE_BACK, |
| 7063 | "unhandledBack()"); |
| 7064 | |
| 7065 | synchronized(this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7066 | int count = mMainStack.mHistory.size(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7067 | if (DEBUG_SWITCH) Slog.d( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7068 | TAG, "Performing unhandledBack(): stack size = " + count); |
| 7069 | if (count > 1) { |
| 7070 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7071 | mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1), |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 7072 | count-1, Activity.RESULT_CANCELED, null, "unhandled-back", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7073 | Binder.restoreCallingIdentity(origId); |
| 7074 | } |
| 7075 | } |
| 7076 | } |
| 7077 | |
| 7078 | public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 7079 | enforceNotIsolatedCaller("openContentUri"); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 7080 | final int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7081 | String name = uri.getAuthority(); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 7082 | ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7083 | ParcelFileDescriptor pfd = null; |
| 7084 | if (cph != null) { |
| 7085 | // We record the binder invoker's uid in thread-local storage before |
| 7086 | // going to the content provider to open the file. Later, in the code |
| 7087 | // that handles all permissions checks, we look for this uid and use |
| 7088 | // that rather than the Activity Manager's own uid. The effect is that |
| 7089 | // we do the check against the caller's permissions even though it looks |
| 7090 | // to the content provider like the Activity Manager itself is making |
| 7091 | // the request. |
| 7092 | sCallerIdentity.set(new Identity( |
| 7093 | Binder.getCallingPid(), Binder.getCallingUid())); |
| 7094 | try { |
| 7095 | pfd = cph.provider.openFile(uri, "r"); |
| 7096 | } catch (FileNotFoundException e) { |
| 7097 | // do nothing; pfd will be returned null |
| 7098 | } finally { |
| 7099 | // Ensure that whatever happens, we clean up the identity state |
| 7100 | sCallerIdentity.remove(); |
| 7101 | } |
| 7102 | |
| 7103 | // 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] | 7104 | removeContentProviderExternalUnchecked(name, null, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7105 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7106 | 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] | 7107 | } |
| 7108 | return pfd; |
| 7109 | } |
| 7110 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7111 | // Actually is sleeping or shutting down or whatever else in the future |
| 7112 | // is an inactive state. |
| 7113 | public boolean isSleeping() { |
| 7114 | return mSleeping || mShuttingDown; |
| 7115 | } |
| 7116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7117 | public void goingToSleep() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7118 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7119 | != PackageManager.PERMISSION_GRANTED) { |
| 7120 | throw new SecurityException("Requires permission " |
| 7121 | + android.Manifest.permission.DEVICE_POWER); |
| 7122 | } |
| 7123 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7124 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7125 | mWentToSleep = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7126 | updateEventDispatchingLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7127 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7128 | if (!mSleeping) { |
| 7129 | mSleeping = true; |
| 7130 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 7131 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7132 | // Initialize the wake times of all processes. |
| 7133 | checkExcessivePowerUsageLocked(false); |
| 7134 | mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 7135 | Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG); |
| 7136 | mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY); |
| 7137 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7138 | } |
| 7139 | } |
| 7140 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7141 | public boolean shutdown(int timeout) { |
| 7142 | if (checkCallingPermission(android.Manifest.permission.SHUTDOWN) |
| 7143 | != PackageManager.PERMISSION_GRANTED) { |
| 7144 | throw new SecurityException("Requires permission " |
| 7145 | + android.Manifest.permission.SHUTDOWN); |
| 7146 | } |
| 7147 | |
| 7148 | boolean timedout = false; |
| 7149 | |
| 7150 | synchronized(this) { |
| 7151 | mShuttingDown = true; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7152 | updateEventDispatchingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7153 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7154 | if (mMainStack.mResumedActivity != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7155 | mMainStack.stopIfSleepingLocked(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7156 | final long endTime = System.currentTimeMillis() + timeout; |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7157 | while (mMainStack.mResumedActivity != null |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 7158 | || mMainStack.mPausingActivity != null) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7159 | long delay = endTime - System.currentTimeMillis(); |
| 7160 | if (delay <= 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7161 | Slog.w(TAG, "Activity manager shutdown timed out"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 7162 | timedout = true; |
| 7163 | break; |
| 7164 | } |
| 7165 | try { |
| 7166 | this.wait(); |
| 7167 | } catch (InterruptedException e) { |
| 7168 | } |
| 7169 | } |
| 7170 | } |
| 7171 | } |
| 7172 | |
| 7173 | mUsageStatsService.shutdown(); |
| 7174 | mBatteryStatsService.shutdown(); |
| 7175 | |
| 7176 | return timedout; |
| 7177 | } |
| 7178 | |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7179 | public final void activitySlept(IBinder token) { |
| 7180 | if (localLOGV) Slog.v( |
| 7181 | TAG, "Activity slept: token=" + token); |
| 7182 | |
| 7183 | ActivityRecord r = null; |
| 7184 | |
| 7185 | final long origId = Binder.clearCallingIdentity(); |
| 7186 | |
| 7187 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7188 | r = mMainStack.isInStackLocked(token); |
| 7189 | if (r != null) { |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 7190 | mMainStack.activitySleptLocked(r); |
| 7191 | } |
| 7192 | } |
| 7193 | |
| 7194 | Binder.restoreCallingIdentity(origId); |
| 7195 | } |
| 7196 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7197 | private void comeOutOfSleepIfNeededLocked() { |
| 7198 | if (!mWentToSleep && !mLockScreenShown) { |
| 7199 | if (mSleeping) { |
| 7200 | mSleeping = false; |
| 7201 | mMainStack.awakeFromSleepingLocked(); |
| 7202 | mMainStack.resumeTopActivityLocked(null); |
| 7203 | } |
| 7204 | } |
| 7205 | } |
| 7206 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7207 | public void wakingUp() { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7208 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7209 | != PackageManager.PERMISSION_GRANTED) { |
| 7210 | throw new SecurityException("Requires permission " |
| 7211 | + android.Manifest.permission.DEVICE_POWER); |
| 7212 | } |
| 7213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7214 | synchronized(this) { |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7215 | mWentToSleep = false; |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7216 | updateEventDispatchingLocked(); |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7217 | comeOutOfSleepIfNeededLocked(); |
| 7218 | } |
| 7219 | } |
| 7220 | |
Jeff Brown | c042ee2 | 2012-05-08 13:03:42 -0700 | [diff] [blame] | 7221 | private void updateEventDispatchingLocked() { |
| 7222 | mWindowManager.setEventDispatching(mBooted && !mWentToSleep && !mShuttingDown); |
| 7223 | } |
| 7224 | |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 7225 | public void setLockScreenShown(boolean shown) { |
| 7226 | if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER) |
| 7227 | != PackageManager.PERMISSION_GRANTED) { |
| 7228 | throw new SecurityException("Requires permission " |
| 7229 | + android.Manifest.permission.DEVICE_POWER); |
| 7230 | } |
| 7231 | |
| 7232 | synchronized(this) { |
| 7233 | mLockScreenShown = shown; |
| 7234 | comeOutOfSleepIfNeededLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7235 | } |
| 7236 | } |
| 7237 | |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7238 | public void stopAppSwitches() { |
| 7239 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 7240 | != PackageManager.PERMISSION_GRANTED) { |
| 7241 | throw new SecurityException("Requires permission " |
| 7242 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 7243 | } |
| 7244 | |
| 7245 | synchronized(this) { |
| 7246 | mAppSwitchesAllowedTime = SystemClock.uptimeMillis() |
| 7247 | + APP_SWITCH_DELAY_TIME; |
| 7248 | mDidAppSwitch = false; |
| 7249 | mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 7250 | Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG); |
| 7251 | mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME); |
| 7252 | } |
| 7253 | } |
| 7254 | |
| 7255 | public void resumeAppSwitches() { |
| 7256 | if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES) |
| 7257 | != PackageManager.PERMISSION_GRANTED) { |
| 7258 | throw new SecurityException("Requires permission " |
| 7259 | + android.Manifest.permission.STOP_APP_SWITCHES); |
| 7260 | } |
| 7261 | |
| 7262 | synchronized(this) { |
| 7263 | // Note that we don't execute any pending app switches... we will |
| 7264 | // let those wait until either the timeout, or the next start |
| 7265 | // activity request. |
| 7266 | mAppSwitchesAllowedTime = 0; |
| 7267 | } |
| 7268 | } |
| 7269 | |
| 7270 | boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid, |
| 7271 | String name) { |
| 7272 | if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) { |
| 7273 | return true; |
| 7274 | } |
| 7275 | |
| 7276 | final int perm = checkComponentPermission( |
| 7277 | android.Manifest.permission.STOP_APP_SWITCHES, callingPid, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 7278 | callingUid, -1, true); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7279 | if (perm == PackageManager.PERMISSION_GRANTED) { |
| 7280 | return true; |
| 7281 | } |
| 7282 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7283 | Slog.w(TAG, name + " request from " + callingUid + " stopped"); |
Dianne Hackborn | 95fc68f | 2009-05-19 18:37:45 -0700 | [diff] [blame] | 7284 | return false; |
| 7285 | } |
| 7286 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7287 | public void setDebugApp(String packageName, boolean waitForDebugger, |
| 7288 | boolean persistent) { |
| 7289 | enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP, |
| 7290 | "setDebugApp()"); |
| 7291 | |
| 7292 | // Note that this is not really thread safe if there are multiple |
| 7293 | // callers into it at the same time, but that's not a situation we |
| 7294 | // care about. |
| 7295 | if (persistent) { |
| 7296 | final ContentResolver resolver = mContext.getContentResolver(); |
| 7297 | Settings.System.putString( |
| 7298 | resolver, Settings.System.DEBUG_APP, |
| 7299 | packageName); |
| 7300 | Settings.System.putInt( |
| 7301 | resolver, Settings.System.WAIT_FOR_DEBUGGER, |
| 7302 | waitForDebugger ? 1 : 0); |
| 7303 | } |
| 7304 | |
| 7305 | synchronized (this) { |
| 7306 | if (!persistent) { |
| 7307 | mOrigDebugApp = mDebugApp; |
| 7308 | mOrigWaitForDebugger = mWaitForDebugger; |
| 7309 | } |
| 7310 | mDebugApp = packageName; |
| 7311 | mWaitForDebugger = waitForDebugger; |
| 7312 | mDebugTransient = !persistent; |
| 7313 | if (packageName != null) { |
| 7314 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 7315 | forceStopPackageLocked(packageName, -1, false, false, true, true, |
| 7316 | UserHandle.USER_ALL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7317 | Binder.restoreCallingIdentity(origId); |
| 7318 | } |
| 7319 | } |
| 7320 | } |
| 7321 | |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 7322 | void setOpenGlTraceApp(ApplicationInfo app, String processName) { |
| 7323 | synchronized (this) { |
| 7324 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 7325 | if (!isDebuggable) { |
| 7326 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 7327 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 7328 | } |
| 7329 | } |
| 7330 | |
| 7331 | mOpenGlTraceApp = processName; |
| 7332 | } |
| 7333 | } |
| 7334 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 7335 | void setProfileApp(ApplicationInfo app, String processName, String profileFile, |
| 7336 | ParcelFileDescriptor profileFd, boolean autoStopProfiler) { |
| 7337 | synchronized (this) { |
| 7338 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 7339 | if (!isDebuggable) { |
| 7340 | if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 7341 | throw new SecurityException("Process not debuggable: " + app.packageName); |
| 7342 | } |
| 7343 | } |
| 7344 | mProfileApp = processName; |
| 7345 | mProfileFile = profileFile; |
| 7346 | if (mProfileFd != null) { |
| 7347 | try { |
| 7348 | mProfileFd.close(); |
| 7349 | } catch (IOException e) { |
| 7350 | } |
| 7351 | mProfileFd = null; |
| 7352 | } |
| 7353 | mProfileFd = profileFd; |
| 7354 | mProfileType = 0; |
| 7355 | mAutoStopProfiler = autoStopProfiler; |
| 7356 | } |
| 7357 | } |
| 7358 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7359 | public void setAlwaysFinish(boolean enabled) { |
| 7360 | enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH, |
| 7361 | "setAlwaysFinish()"); |
| 7362 | |
| 7363 | Settings.System.putInt( |
| 7364 | mContext.getContentResolver(), |
| 7365 | Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0); |
| 7366 | |
| 7367 | synchronized (this) { |
| 7368 | mAlwaysFinishActivities = enabled; |
| 7369 | } |
| 7370 | } |
| 7371 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7372 | public void setActivityController(IActivityController controller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7373 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7374 | "setActivityController()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7375 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 7376 | mController = controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7377 | } |
| 7378 | } |
| 7379 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 7380 | public boolean isUserAMonkey() { |
| 7381 | // For now the fact that there is a controller implies |
| 7382 | // we have a monkey. |
| 7383 | synchronized (this) { |
| 7384 | return mController != null; |
| 7385 | } |
| 7386 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 7387 | |
| 7388 | public void requestBugReport() { |
| 7389 | // No permission check because this can't do anything harmful -- |
| 7390 | // it will just eventually cause the user to be presented with |
| 7391 | // a UI to select where the bug report goes. |
| 7392 | SystemProperties.set("ctl.start", "bugreport"); |
| 7393 | } |
| 7394 | |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7395 | public void registerProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7396 | enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER, |
| 7397 | "registerProcessObserver()"); |
| 7398 | synchronized (this) { |
| 7399 | mProcessObservers.register(observer); |
| 7400 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7401 | } |
| 7402 | |
| 7403 | public void unregisterProcessObserver(IProcessObserver observer) { |
Dianne Hackborn | 21fbd1f | 2012-02-10 10:38:10 -0800 | [diff] [blame] | 7404 | synchronized (this) { |
| 7405 | mProcessObservers.unregister(observer); |
| 7406 | } |
Jeff Sharkey | a462079 | 2011-05-20 15:29:23 -0700 | [diff] [blame] | 7407 | } |
| 7408 | |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7409 | public void setImmersive(IBinder token, boolean immersive) { |
| 7410 | synchronized(this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7411 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 7412 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7413 | throw new IllegalArgumentException(); |
| 7414 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7415 | r.immersive = immersive; |
| 7416 | } |
| 7417 | } |
| 7418 | |
| 7419 | public boolean isImmersive(IBinder token) { |
| 7420 | synchronized (this) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 7421 | ActivityRecord r = mMainStack.isInStackLocked(token); |
| 7422 | if (r == null) { |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7423 | throw new IllegalArgumentException(); |
| 7424 | } |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7425 | return r.immersive; |
| 7426 | } |
| 7427 | } |
| 7428 | |
| 7429 | public boolean isTopActivityImmersive() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 7430 | enforceNotIsolatedCaller("startActivity"); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7431 | synchronized (this) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7432 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Daniel Sandler | 69a4817 | 2010-06-23 16:29:36 -0400 | [diff] [blame] | 7433 | return (r != null) ? r.immersive : false; |
| 7434 | } |
| 7435 | } |
| 7436 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7437 | public final void enterSafeMode() { |
| 7438 | synchronized(this) { |
| 7439 | // It only makes sense to do this before the system is ready |
| 7440 | // and started launching other packages. |
| 7441 | if (!mSystemReady) { |
| 7442 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7443 | AppGlobals.getPackageManager().enterSafeMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7444 | } catch (RemoteException e) { |
| 7445 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7446 | } |
| 7447 | } |
| 7448 | } |
| 7449 | |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7450 | public final void showSafeModeOverlay() { |
| 7451 | View v = LayoutInflater.from(mContext).inflate( |
| 7452 | com.android.internal.R.layout.safe_mode, null); |
| 7453 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); |
| 7454 | lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY; |
| 7455 | lp.width = WindowManager.LayoutParams.WRAP_CONTENT; |
| 7456 | lp.height = WindowManager.LayoutParams.WRAP_CONTENT; |
Fabrice Di Meglio | aac0d4e | 2012-07-19 19:21:26 -0700 | [diff] [blame] | 7457 | lp.gravity = Gravity.BOTTOM | Gravity.START; |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 7458 | lp.format = v.getBackground().getOpacity(); |
| 7459 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 7460 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 7461 | ((WindowManager)mContext.getSystemService( |
| 7462 | Context.WINDOW_SERVICE)).addView(v, lp); |
| 7463 | } |
| 7464 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7465 | public void noteWakeupAlarm(IIntentSender sender) { |
| 7466 | if (!(sender instanceof PendingIntentRecord)) { |
| 7467 | return; |
| 7468 | } |
| 7469 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 7470 | synchronized (stats) { |
| 7471 | if (mBatteryStatsService.isOnBattery()) { |
| 7472 | mBatteryStatsService.enforceCallingPermission(); |
| 7473 | PendingIntentRecord rec = (PendingIntentRecord)sender; |
| 7474 | int MY_UID = Binder.getCallingUid(); |
| 7475 | int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid; |
| 7476 | BatteryStatsImpl.Uid.Pkg pkg = |
| 7477 | stats.getPackageStatsLocked(uid, rec.key.packageName); |
| 7478 | pkg.incWakeupsLocked(); |
| 7479 | } |
| 7480 | } |
| 7481 | } |
| 7482 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7483 | public boolean killPids(int[] pids, String pReason, boolean secure) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7484 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7485 | throw new SecurityException("killPids only available to the system"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7486 | } |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7487 | String reason = (pReason == null) ? "Unknown" : pReason; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7488 | // XXX Note: don't acquire main activity lock here, because the window |
| 7489 | // manager calls in with its locks held. |
| 7490 | |
| 7491 | boolean killed = false; |
| 7492 | synchronized (mPidsSelfLocked) { |
| 7493 | int[] types = new int[pids.length]; |
| 7494 | int worstType = 0; |
| 7495 | for (int i=0; i<pids.length; i++) { |
| 7496 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7497 | if (proc != null) { |
| 7498 | int type = proc.setAdj; |
| 7499 | types[i] = type; |
| 7500 | if (type > worstType) { |
| 7501 | worstType = type; |
| 7502 | } |
| 7503 | } |
| 7504 | } |
| 7505 | |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7506 | // 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] | 7507 | // then constrain it so we will kill all hidden procs. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7508 | if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ |
| 7509 | && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 7510 | worstType = ProcessList.HIDDEN_APP_MIN_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7511 | } |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7512 | |
| 7513 | // If this is not a secure call, don't let it kill processes that |
| 7514 | // are important. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 7515 | if (!secure && worstType < ProcessList.SERVICE_ADJ) { |
| 7516 | worstType = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | 6482517 | 2011-03-02 21:32:58 -0800 | [diff] [blame] | 7517 | } |
| 7518 | |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7519 | Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7520 | for (int i=0; i<pids.length; i++) { |
| 7521 | ProcessRecord proc = mPidsSelfLocked.get(pids[i]); |
| 7522 | if (proc == null) { |
| 7523 | continue; |
| 7524 | } |
| 7525 | int adj = proc.setAdj; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7526 | if (adj >= worstType && !proc.killedBackground) { |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7527 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7528 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.userId, proc.pid, |
Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 7529 | proc.processName, adj, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7530 | killed = true; |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 7531 | proc.killedBackground = true; |
| 7532 | Process.killProcessQuiet(pids[i]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7533 | } |
| 7534 | } |
| 7535 | } |
| 7536 | return killed; |
| 7537 | } |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 7538 | |
| 7539 | @Override |
| 7540 | public boolean killProcessesBelowForeground(String reason) { |
| 7541 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7542 | throw new SecurityException("killProcessesBelowForeground() only available to system"); |
| 7543 | } |
| 7544 | |
| 7545 | return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason); |
| 7546 | } |
| 7547 | |
| 7548 | private boolean killProcessesBelowAdj(int belowAdj, String reason) { |
| 7549 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 7550 | throw new SecurityException("killProcessesBelowAdj() only available to system"); |
| 7551 | } |
| 7552 | |
| 7553 | boolean killed = false; |
| 7554 | synchronized (mPidsSelfLocked) { |
| 7555 | final int size = mPidsSelfLocked.size(); |
| 7556 | for (int i = 0; i < size; i++) { |
| 7557 | final int pid = mPidsSelfLocked.keyAt(i); |
| 7558 | final ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7559 | if (proc == null) continue; |
| 7560 | |
| 7561 | final int adj = proc.setAdj; |
| 7562 | if (adj > belowAdj && !proc.killedBackground) { |
| 7563 | Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7564 | EventLog.writeEvent(EventLogTags.AM_KILL, proc.userId, |
| 7565 | proc.pid, proc.processName, adj, reason); |
Jeff Sharkey | b9a0701 | 2012-03-22 17:00:04 -0700 | [diff] [blame] | 7566 | killed = true; |
| 7567 | proc.killedBackground = true; |
| 7568 | Process.killProcessQuiet(pid); |
| 7569 | } |
| 7570 | } |
| 7571 | } |
| 7572 | return killed; |
| 7573 | } |
| 7574 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7575 | public final void startRunning(String pkg, String cls, String action, |
| 7576 | String data) { |
| 7577 | synchronized(this) { |
| 7578 | if (mStartRunning) { |
| 7579 | return; |
| 7580 | } |
| 7581 | mStartRunning = true; |
| 7582 | mTopComponent = pkg != null && cls != null |
| 7583 | ? new ComponentName(pkg, cls) : null; |
| 7584 | mTopAction = action != null ? action : Intent.ACTION_MAIN; |
| 7585 | mTopData = data; |
| 7586 | if (!mSystemReady) { |
| 7587 | return; |
| 7588 | } |
| 7589 | } |
| 7590 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7591 | systemReady(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7592 | } |
| 7593 | |
| 7594 | private void retrieveSettings() { |
| 7595 | final ContentResolver resolver = mContext.getContentResolver(); |
| 7596 | String debugApp = Settings.System.getString( |
| 7597 | resolver, Settings.System.DEBUG_APP); |
| 7598 | boolean waitForDebugger = Settings.System.getInt( |
| 7599 | resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0; |
| 7600 | boolean alwaysFinishActivities = Settings.System.getInt( |
| 7601 | resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0; |
| 7602 | |
| 7603 | Configuration configuration = new Configuration(); |
| 7604 | Settings.System.getConfiguration(resolver, configuration); |
| 7605 | |
| 7606 | synchronized (this) { |
| 7607 | mDebugApp = mOrigDebugApp = debugApp; |
| 7608 | mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger; |
| 7609 | mAlwaysFinishActivities = alwaysFinishActivities; |
| 7610 | // This happens before any activities are started, so we can |
| 7611 | // change mConfiguration in-place. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 7612 | updateConfigurationLocked(configuration, null, false, true); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7613 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7614 | } |
| 7615 | } |
| 7616 | |
| 7617 | public boolean testIsSystemReady() { |
| 7618 | // no need to synchronize(this) just to read & return the value |
| 7619 | return mSystemReady; |
| 7620 | } |
| 7621 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7622 | private static File getCalledPreBootReceiversFile() { |
| 7623 | File dataDir = Environment.getDataDirectory(); |
| 7624 | File systemDir = new File(dataDir, "system"); |
| 7625 | File fname = new File(systemDir, "called_pre_boots.dat"); |
| 7626 | return fname; |
| 7627 | } |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7628 | |
| 7629 | static final int LAST_DONE_VERSION = 10000; |
| 7630 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7631 | private static ArrayList<ComponentName> readLastDonePreBootReceivers() { |
| 7632 | ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>(); |
| 7633 | File file = getCalledPreBootReceiversFile(); |
| 7634 | FileInputStream fis = null; |
| 7635 | try { |
| 7636 | fis = new FileInputStream(file); |
| 7637 | DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7638 | int fvers = dis.readInt(); |
| 7639 | if (fvers == LAST_DONE_VERSION) { |
| 7640 | String vers = dis.readUTF(); |
| 7641 | String codename = dis.readUTF(); |
| 7642 | String build = dis.readUTF(); |
| 7643 | if (android.os.Build.VERSION.RELEASE.equals(vers) |
| 7644 | && android.os.Build.VERSION.CODENAME.equals(codename) |
| 7645 | && android.os.Build.VERSION.INCREMENTAL.equals(build)) { |
| 7646 | int num = dis.readInt(); |
| 7647 | while (num > 0) { |
| 7648 | num--; |
| 7649 | String pkg = dis.readUTF(); |
| 7650 | String cls = dis.readUTF(); |
| 7651 | lastDoneReceivers.add(new ComponentName(pkg, cls)); |
| 7652 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7653 | } |
| 7654 | } |
| 7655 | } catch (FileNotFoundException e) { |
| 7656 | } catch (IOException e) { |
| 7657 | Slog.w(TAG, "Failure reading last done pre-boot receivers", e); |
| 7658 | } finally { |
| 7659 | if (fis != null) { |
| 7660 | try { |
| 7661 | fis.close(); |
| 7662 | } catch (IOException e) { |
| 7663 | } |
| 7664 | } |
| 7665 | } |
| 7666 | return lastDoneReceivers; |
| 7667 | } |
| 7668 | |
| 7669 | private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) { |
| 7670 | File file = getCalledPreBootReceiversFile(); |
| 7671 | FileOutputStream fos = null; |
| 7672 | DataOutputStream dos = null; |
| 7673 | try { |
| 7674 | Slog.i(TAG, "Writing new set of last done pre-boot receivers..."); |
| 7675 | fos = new FileOutputStream(file); |
| 7676 | dos = new DataOutputStream(new BufferedOutputStream(fos, 2048)); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7677 | dos.writeInt(LAST_DONE_VERSION); |
| 7678 | dos.writeUTF(android.os.Build.VERSION.RELEASE); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7679 | dos.writeUTF(android.os.Build.VERSION.CODENAME); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 7680 | dos.writeUTF(android.os.Build.VERSION.INCREMENTAL); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7681 | dos.writeInt(list.size()); |
| 7682 | for (int i=0; i<list.size(); i++) { |
| 7683 | dos.writeUTF(list.get(i).getPackageName()); |
| 7684 | dos.writeUTF(list.get(i).getClassName()); |
| 7685 | } |
| 7686 | } catch (IOException e) { |
| 7687 | Slog.w(TAG, "Failure writing last done pre-boot receivers", e); |
| 7688 | file.delete(); |
| 7689 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 7690 | FileUtils.sync(fos); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7691 | if (dos != null) { |
| 7692 | try { |
| 7693 | dos.close(); |
| 7694 | } catch (IOException e) { |
| 7695 | // TODO Auto-generated catch block |
| 7696 | e.printStackTrace(); |
| 7697 | } |
| 7698 | } |
| 7699 | } |
| 7700 | } |
| 7701 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7702 | public void systemReady(final Runnable goingCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7703 | synchronized(this) { |
| 7704 | if (mSystemReady) { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7705 | if (goingCallback != null) goingCallback.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7706 | return; |
| 7707 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7708 | |
| 7709 | // Check to see if there are any update receivers to run. |
| 7710 | if (!mDidUpdate) { |
| 7711 | if (mWaitingUpdate) { |
| 7712 | return; |
| 7713 | } |
| 7714 | Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED); |
| 7715 | List<ResolveInfo> ris = null; |
| 7716 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7717 | ris = AppGlobals.getPackageManager().queryIntentReceivers( |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 7718 | intent, null, 0, 0); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7719 | } catch (RemoteException e) { |
| 7720 | } |
| 7721 | if (ris != null) { |
| 7722 | for (int i=ris.size()-1; i>=0; i--) { |
| 7723 | if ((ris.get(i).activityInfo.applicationInfo.flags |
| 7724 | &ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 7725 | ris.remove(i); |
| 7726 | } |
| 7727 | } |
| 7728 | intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 7729 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7730 | ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 7731 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7732 | final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>(); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7733 | for (int i=0; i<ris.size(); i++) { |
| 7734 | ActivityInfo ai = ris.get(i).activityInfo; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7735 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7736 | if (lastDoneReceivers.contains(comp)) { |
| 7737 | ris.remove(i); |
| 7738 | i--; |
| 7739 | } |
| 7740 | } |
Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 7741 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7742 | final int[] users = getUsersLocked(); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7743 | for (int i=0; i<ris.size(); i++) { |
| 7744 | ActivityInfo ai = ris.get(i).activityInfo; |
| 7745 | ComponentName comp = new ComponentName(ai.packageName, ai.name); |
| 7746 | doneReceivers.add(comp); |
| 7747 | intent.setComponent(comp); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7748 | for (int j=0; j<users.length; j++) { |
| 7749 | IIntentReceiver finisher = null; |
| 7750 | if (i == ris.size()-1 && j == users.length-1) { |
| 7751 | finisher = new IIntentReceiver.Stub() { |
| 7752 | public void performReceive(Intent intent, int resultCode, |
| 7753 | String data, Bundle extras, boolean ordered, |
| 7754 | boolean sticky, int sendingUser) { |
| 7755 | // The raw IIntentReceiver interface is called |
| 7756 | // with the AM lock held, so redispatch to |
| 7757 | // execute our code without the lock. |
| 7758 | mHandler.post(new Runnable() { |
| 7759 | public void run() { |
| 7760 | synchronized (ActivityManagerService.this) { |
| 7761 | mDidUpdate = true; |
| 7762 | } |
| 7763 | writeLastDonePreBootReceivers(doneReceivers); |
| 7764 | showBootMessage(mContext.getText( |
| 7765 | R.string.android_upgrading_complete), |
| 7766 | false); |
| 7767 | systemReady(goingCallback); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 7768 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7769 | }); |
| 7770 | } |
| 7771 | }; |
| 7772 | } |
| 7773 | Slog.i(TAG, "Sending system update to " + intent.getComponent() |
| 7774 | + " for user " + users[j]); |
| 7775 | broadcastIntentLocked(null, null, intent, null, finisher, |
| 7776 | 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID, |
| 7777 | users[j]); |
| 7778 | if (finisher != null) { |
| 7779 | mWaitingUpdate = true; |
| 7780 | } |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7781 | } |
| 7782 | } |
| 7783 | } |
| 7784 | if (mWaitingUpdate) { |
| 7785 | return; |
| 7786 | } |
| 7787 | mDidUpdate = true; |
| 7788 | } |
| 7789 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7790 | mSystemReady = true; |
| 7791 | if (!mStartRunning) { |
| 7792 | return; |
| 7793 | } |
| 7794 | } |
| 7795 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7796 | ArrayList<ProcessRecord> procsToKill = null; |
| 7797 | synchronized(mPidsSelfLocked) { |
| 7798 | for (int i=mPidsSelfLocked.size()-1; i>=0; i--) { |
| 7799 | ProcessRecord proc = mPidsSelfLocked.valueAt(i); |
| 7800 | if (!isAllowedWhileBooting(proc.info)){ |
| 7801 | if (procsToKill == null) { |
| 7802 | procsToKill = new ArrayList<ProcessRecord>(); |
| 7803 | } |
| 7804 | procsToKill.add(proc); |
| 7805 | } |
| 7806 | } |
| 7807 | } |
| 7808 | |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7809 | synchronized(this) { |
| 7810 | if (procsToKill != null) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7811 | for (int i=procsToKill.size()-1; i>=0; i--) { |
| 7812 | ProcessRecord proc = procsToKill.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7813 | Slog.i(TAG, "Removing system update proc: " + proc); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 7814 | removeProcessLocked(proc, true, false, "system update done"); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7815 | } |
| 7816 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 7817 | |
| 7818 | // Now that we have cleaned up any update processes, we |
| 7819 | // are ready to start launching real processes and know that |
| 7820 | // we won't trample on them any more. |
| 7821 | mProcessesReady = true; |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7822 | } |
| 7823 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 7824 | Slog.i(TAG, "System now ready"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 7825 | EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7826 | SystemClock.uptimeMillis()); |
| 7827 | |
| 7828 | synchronized(this) { |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7829 | // Make sure we have no pre-ready processes sitting around. |
| 7830 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7831 | if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7832 | ResolveInfo ri = mContext.getPackageManager() |
| 7833 | .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST), |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7834 | STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7835 | CharSequence errorMsg = null; |
| 7836 | if (ri != null) { |
| 7837 | ActivityInfo ai = ri.activityInfo; |
| 7838 | ApplicationInfo app = ai.applicationInfo; |
| 7839 | if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 7840 | mTopAction = Intent.ACTION_FACTORY_TEST; |
| 7841 | mTopData = null; |
| 7842 | mTopComponent = new ComponentName(app.packageName, |
| 7843 | ai.name); |
| 7844 | } else { |
| 7845 | errorMsg = mContext.getResources().getText( |
| 7846 | com.android.internal.R.string.factorytest_not_system); |
| 7847 | } |
| 7848 | } else { |
| 7849 | errorMsg = mContext.getResources().getText( |
| 7850 | com.android.internal.R.string.factorytest_no_action); |
| 7851 | } |
| 7852 | if (errorMsg != null) { |
| 7853 | mTopAction = null; |
| 7854 | mTopData = null; |
| 7855 | mTopComponent = null; |
| 7856 | Message msg = Message.obtain(); |
| 7857 | msg.what = SHOW_FACTORY_ERROR_MSG; |
| 7858 | msg.getData().putCharSequence("msg", errorMsg); |
| 7859 | mHandler.sendMessage(msg); |
| 7860 | } |
| 7861 | } |
| 7862 | } |
| 7863 | |
| 7864 | retrieveSettings(); |
| 7865 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 7866 | if (goingCallback != null) goingCallback.run(); |
| 7867 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7868 | synchronized (this) { |
| 7869 | if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { |
| 7870 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7871 | List apps = AppGlobals.getPackageManager(). |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 7872 | getPersistentApplications(STOCK_PM_FLAGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7873 | if (apps != null) { |
| 7874 | int N = apps.size(); |
| 7875 | int i; |
| 7876 | for (i=0; i<N; i++) { |
| 7877 | ApplicationInfo info |
| 7878 | = (ApplicationInfo)apps.get(i); |
| 7879 | if (info != null && |
| 7880 | !info.packageName.equals("android")) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7881 | addAppLocked(info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7882 | } |
| 7883 | } |
| 7884 | } |
| 7885 | } catch (RemoteException ex) { |
| 7886 | // pm is in same process, this will never happen. |
| 7887 | } |
| 7888 | } |
| 7889 | |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 7890 | // Start up initial activity. |
| 7891 | mBooting = true; |
| 7892 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7893 | try { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 7894 | if (AppGlobals.getPackageManager().hasSystemUidErrors()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7895 | Message msg = Message.obtain(); |
| 7896 | msg.what = SHOW_UID_ERROR_MSG; |
| 7897 | mHandler.sendMessage(msg); |
| 7898 | } |
| 7899 | } catch (RemoteException e) { |
| 7900 | } |
| 7901 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7902 | long ident = Binder.clearCallingIdentity(); |
| 7903 | try { |
| 7904 | Intent intent = new Intent(Intent.ACTION_USER_STARTED); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 7905 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 7906 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7907 | intent.putExtra(Intent.EXTRA_USER_HANDLE, mCurrentUserId); |
| 7908 | broadcastIntentLocked(null, null, intent, |
| 7909 | null, null, 0, null, null, null, |
| 7910 | false, false, MY_PID, Process.SYSTEM_UID, mCurrentUserId); |
| 7911 | } finally { |
| 7912 | Binder.restoreCallingIdentity(ident); |
| 7913 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 7914 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 7915 | sendUserSwitchBroadcastsLocked(-1, mCurrentUserId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7916 | } |
| 7917 | } |
| 7918 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7919 | private boolean makeAppCrashingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7920 | String shortMsg, String longMsg, String stackTrace) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7921 | app.crashing = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7922 | app.crashingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7923 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7924 | startAppProblemLocked(app); |
| 7925 | app.stopFreezingAllLocked(); |
| 7926 | return handleAppCrashLocked(app); |
| 7927 | } |
| 7928 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7929 | private void makeAppNotRespondingLocked(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7930 | String activity, String shortMsg, String longMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7931 | app.notResponding = true; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7932 | app.notRespondingReport = generateProcessError(app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7933 | ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING, |
| 7934 | activity, shortMsg, longMsg, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7935 | startAppProblemLocked(app); |
| 7936 | app.stopFreezingAllLocked(); |
| 7937 | } |
| 7938 | |
| 7939 | /** |
| 7940 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 7941 | * |
| 7942 | * @param app The ProcessRecord in which the error occurred. |
| 7943 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
| 7944 | * ActivityManager.AppErrorStateInfo |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7945 | * @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] | 7946 | * @param shortMsg Short message describing the crash. |
| 7947 | * @param longMsg Long message describing the crash. |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7948 | * @param stackTrace Full crash stack trace, may be null. |
| 7949 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7950 | * @return Returns a fully-formed AppErrorStateInfo record. |
| 7951 | */ |
| 7952 | private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7953 | 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] | 7954 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7955 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7956 | report.condition = condition; |
| 7957 | report.processName = app.processName; |
| 7958 | report.pid = app.pid; |
| 7959 | report.uid = app.info.uid; |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 7960 | report.tag = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7961 | report.shortMsg = shortMsg; |
| 7962 | report.longMsg = longMsg; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7963 | report.stackTrace = stackTrace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7964 | |
| 7965 | return report; |
| 7966 | } |
| 7967 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7968 | void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7969 | synchronized (this) { |
| 7970 | app.crashing = false; |
| 7971 | app.crashingReport = null; |
| 7972 | app.notResponding = false; |
| 7973 | app.notRespondingReport = null; |
| 7974 | if (app.anrDialog == fromDialog) { |
| 7975 | app.anrDialog = null; |
| 7976 | } |
| 7977 | if (app.waitDialog == fromDialog) { |
| 7978 | app.waitDialog = null; |
| 7979 | } |
| 7980 | if (app.pid > 0 && app.pid != MY_PID) { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7981 | handleAppCrashLocked(app); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7982 | Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 7983 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 7984 | app.processName, app.setAdj, "user's request after error"); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 7985 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7986 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7987 | } |
| 7988 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 7989 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 7990 | private boolean handleAppCrashLocked(ProcessRecord app) { |
Mike Lockwood | 86548c4 | 2011-09-13 17:21:46 -0400 | [diff] [blame] | 7991 | if (mHeadless) { |
| 7992 | Log.e(TAG, "handleAppCrashLocked: " + app.processName); |
| 7993 | return false; |
| 7994 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7995 | long now = SystemClock.uptimeMillis(); |
| 7996 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 7997 | Long crashTime; |
| 7998 | if (!app.isolated) { |
| 7999 | crashTime = mProcessCrashTimes.get(app.info.processName, app.uid); |
| 8000 | } else { |
| 8001 | crashTime = null; |
| 8002 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 8003 | if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8004 | // This process loses! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8005 | Slog.w(TAG, "Process " + app.info.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8006 | + " has crashed too many times: killing!"); |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 8007 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8008 | app.userId, app.info.processName, app.uid); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8009 | for (int i=mMainStack.mHistory.size()-1; i>=0; i--) { |
| 8010 | ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8011 | if (r.app == app) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8012 | Slog.w(TAG, " Force finishing activity " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8013 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 8014 | r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, |
| 8015 | null, "crashed", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8016 | } |
| 8017 | } |
| 8018 | if (!app.persistent) { |
| 8019 | // We don't want to start this process again until the user |
| 8020 | // explicitly does so... but for persistent process, we really |
| 8021 | // need to keep it running. If a persistent process is actually |
| 8022 | // repeatedly crashing, then badness for everyone. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8023 | EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.userId, app.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8024 | app.info.processName); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8025 | if (!app.isolated) { |
| 8026 | // XXX We don't have a way to mark isolated processes |
| 8027 | // as bad, since they don't have a peristent identity. |
| 8028 | mBadProcesses.put(app.info.processName, app.uid, now); |
| 8029 | mProcessCrashTimes.remove(app.info.processName, app.uid); |
| 8030 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8031 | app.bad = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8032 | app.removed = true; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 8033 | // Don't let services in this process be restarted and potentially |
| 8034 | // annoy the user repeatedly. Unless it is persistent, since those |
| 8035 | // processes run critical code. |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 8036 | removeProcessLocked(app, false, false, "crash"); |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8037 | mMainStack.resumeTopActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8038 | return false; |
| 8039 | } |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8040 | mMainStack.resumeTopActivityLocked(null); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8041 | } else { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8042 | ActivityRecord r = mMainStack.topRunningActivityLocked(null); |
Kevin Hester-Chow | d87a9be | 2012-03-05 08:01:00 -0800 | [diff] [blame] | 8043 | if (r != null && r.app == app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8044 | // If the top running activity is from this crashing |
| 8045 | // process, then terminate it to avoid getting in a loop. |
| 8046 | Slog.w(TAG, " Force finishing activity " |
| 8047 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 8048 | int index = mMainStack.indexOfActivityLocked(r); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8049 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 8050 | Activity.RESULT_CANCELED, null, "crashed", false); |
Dianne Hackborn | 070783f | 2010-12-29 16:46:28 -0800 | [diff] [blame] | 8051 | // Also terminate any activities below it that aren't yet |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8052 | // stopped, to avoid a situation where one will get |
| 8053 | // re-start our crashing activity once it gets resumed again. |
| 8054 | index--; |
| 8055 | if (index >= 0) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8056 | r = (ActivityRecord)mMainStack.mHistory.get(index); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8057 | if (r.state == ActivityState.RESUMED |
| 8058 | || r.state == ActivityState.PAUSING |
| 8059 | || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 94cb2eb | 2011-01-13 21:09:44 -0800 | [diff] [blame] | 8060 | if (!r.isHomeActivity || mHomeProcess != r.app) { |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8061 | Slog.w(TAG, " Force finishing activity " |
| 8062 | + r.intent.getComponent().flattenToShortString()); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 8063 | r.stack.finishActivityLocked(r, index, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 8064 | Activity.RESULT_CANCELED, null, "crashed", false); |
Dianne Hackborn | f83c555 | 2010-03-31 22:19:32 -0700 | [diff] [blame] | 8065 | } |
| 8066 | } |
| 8067 | } |
| 8068 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8069 | } |
| 8070 | |
| 8071 | // Bump up the crash count of any services currently running in the proc. |
| 8072 | if (app.services.size() != 0) { |
| 8073 | // Any services running in the application need to be placed |
| 8074 | // back in the pending list. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8075 | Iterator<ServiceRecord> it = app.services.iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8076 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 8077 | ServiceRecord sr = it.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8078 | sr.crashCount++; |
| 8079 | } |
| 8080 | } |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 8081 | |
| 8082 | // If the crashing process is what we consider to be the "home process" and it has been |
| 8083 | // replaced by a third-party app, clear the package preferred activities from packages |
| 8084 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 8085 | // from blocking the user to manually clear the list. |
| 8086 | if (app == mHomeProcess && mHomeProcess.activities.size() > 0 |
| 8087 | && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 8088 | Iterator it = mHomeProcess.activities.iterator(); |
| 8089 | while (it.hasNext()) { |
Jean-Baptiste Queru | 5ea89f7 | 2010-07-30 09:30:31 -0700 | [diff] [blame] | 8090 | ActivityRecord r = (ActivityRecord)it.next(); |
Mattias Larsson | a4fd007 | 2010-06-22 22:37:03 +0200 | [diff] [blame] | 8091 | if (r.isHomeActivity) { |
| 8092 | Log.i(TAG, "Clearing package preferred activities from " + r.packageName); |
| 8093 | try { |
| 8094 | ActivityThread.getPackageManager() |
| 8095 | .clearPackagePreferredActivities(r.packageName); |
| 8096 | } catch (RemoteException c) { |
| 8097 | // pm is in same process, this will never happen. |
| 8098 | } |
| 8099 | } |
| 8100 | } |
| 8101 | } |
| 8102 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8103 | if (!app.isolated) { |
| 8104 | // XXX Can't keep track of crash times for isolated processes, |
| 8105 | // because they don't have a perisistent identity. |
| 8106 | mProcessCrashTimes.put(app.info.processName, app.uid, now); |
| 8107 | } |
| 8108 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8109 | return true; |
| 8110 | } |
| 8111 | |
| 8112 | void startAppProblemLocked(ProcessRecord app) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 8113 | if (app.userId == mCurrentUserId) { |
| 8114 | app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver( |
| 8115 | mContext, app.info.packageName, app.info.flags); |
| 8116 | } else { |
| 8117 | // If this app is not running under the current user, then we |
| 8118 | // can't give it a report button because that would require |
| 8119 | // launching the report UI under a different user. |
| 8120 | app.errorReportReceiver = null; |
| 8121 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8122 | skipCurrentReceiverLocked(app); |
| 8123 | } |
| 8124 | |
| 8125 | void skipCurrentReceiverLocked(ProcessRecord app) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 8126 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 8127 | queue.skipCurrentReceiverLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8128 | } |
| 8129 | } |
| 8130 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8131 | /** |
| 8132 | * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes. |
| 8133 | * The application process will exit immediately after this call returns. |
| 8134 | * @param app object of the crashing app, null for the system server |
| 8135 | * @param crashInfo describing the exception |
| 8136 | */ |
| 8137 | public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8138 | ProcessRecord r = findAppProcess(app, "Crash"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8139 | final String processName = app == null ? "system_server" |
| 8140 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8141 | |
| 8142 | EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(), |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8143 | UserHandle.getUserId(Binder.getCallingUid()), processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 8144 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8145 | crashInfo.exceptionClassName, |
| 8146 | crashInfo.exceptionMessage, |
| 8147 | crashInfo.throwFileName, |
| 8148 | crashInfo.throwLineNumber); |
| 8149 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8150 | addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8151 | |
| 8152 | crashApplication(r, crashInfo); |
| 8153 | } |
| 8154 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8155 | public void handleApplicationStrictModeViolation( |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8156 | IBinder app, |
| 8157 | int violationMask, |
| 8158 | StrictMode.ViolationInfo info) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8159 | ProcessRecord r = findAppProcess(app, "StrictMode"); |
| 8160 | if (r == null) { |
| 8161 | return; |
| 8162 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8163 | |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8164 | if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) { |
Brad Fitzpatrick | f3d86be | 2010-11-23 10:31:52 -0800 | [diff] [blame] | 8165 | Integer stackFingerprint = info.hashCode(); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8166 | boolean logIt = true; |
| 8167 | synchronized (mAlreadyLoggedViolatedStacks) { |
| 8168 | if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) { |
| 8169 | logIt = false; |
| 8170 | // TODO: sub-sample into EventLog for these, with |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8171 | // the info.durationMillis? Then we'd get |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8172 | // the relative pain numbers, without logging all |
| 8173 | // the stack traces repeatedly. We'd want to do |
| 8174 | // likewise in the client code, which also does |
| 8175 | // dup suppression, before the Binder call. |
| 8176 | } else { |
| 8177 | if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) { |
| 8178 | mAlreadyLoggedViolatedStacks.clear(); |
| 8179 | } |
| 8180 | mAlreadyLoggedViolatedStacks.add(stackFingerprint); |
| 8181 | } |
| 8182 | } |
| 8183 | if (logIt) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8184 | logStrictModeViolationToDropBox(r, info); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8185 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8186 | } |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8187 | |
| 8188 | if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) { |
| 8189 | AppErrorResult result = new AppErrorResult(); |
| 8190 | synchronized (this) { |
| 8191 | final long origId = Binder.clearCallingIdentity(); |
| 8192 | |
| 8193 | Message msg = Message.obtain(); |
| 8194 | msg.what = SHOW_STRICT_MODE_VIOLATION_MSG; |
| 8195 | HashMap<String, Object> data = new HashMap<String, Object>(); |
| 8196 | data.put("result", result); |
| 8197 | data.put("app", r); |
Brad Fitzpatrick | 143666f | 2010-06-14 12:40:21 -0700 | [diff] [blame] | 8198 | data.put("violationMask", violationMask); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8199 | data.put("info", info); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8200 | msg.obj = data; |
| 8201 | mHandler.sendMessage(msg); |
| 8202 | |
| 8203 | Binder.restoreCallingIdentity(origId); |
| 8204 | } |
| 8205 | int res = result.get(); |
Dianne Hackborn | b424b63 | 2010-08-18 15:59:05 -0700 | [diff] [blame] | 8206 | Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res); |
Brad Fitzpatrick | 46d4238 | 2010-06-11 13:57:58 -0700 | [diff] [blame] | 8207 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 8208 | } |
| 8209 | |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8210 | // Depending on the policy in effect, there could be a bunch of |
| 8211 | // these in quick succession so we try to batch these together to |
| 8212 | // minimize disk writes, number of dropbox entries, and maximize |
| 8213 | // compression, by having more fewer, larger records. |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8214 | private void logStrictModeViolationToDropBox( |
| 8215 | ProcessRecord process, |
| 8216 | StrictMode.ViolationInfo info) { |
| 8217 | if (info == null) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8218 | return; |
| 8219 | } |
| 8220 | final boolean isSystemApp = process == null || |
| 8221 | (process.info.flags & (ApplicationInfo.FLAG_SYSTEM | |
| 8222 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8223 | final String processName = process == null ? "unknown" : process.processName; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8224 | final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode"; |
| 8225 | final DropBoxManager dbox = (DropBoxManager) |
| 8226 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 8227 | |
| 8228 | // Exit early if the dropbox isn't configured to accept this report type. |
| 8229 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 8230 | |
| 8231 | boolean bufferWasEmpty; |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8232 | boolean needsFlush; |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8233 | final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024); |
| 8234 | synchronized (sb) { |
| 8235 | bufferWasEmpty = sb.length() == 0; |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8236 | appendDropBoxProcessHeaders(process, processName, sb); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8237 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
| 8238 | sb.append("System-App: ").append(isSystemApp).append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8239 | sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n"); |
| 8240 | if (info.violationNumThisLoop != 0) { |
| 8241 | sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n"); |
| 8242 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 8243 | if (info.numAnimationsRunning != 0) { |
| 8244 | sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n"); |
| 8245 | } |
Brad Fitzpatrick | bfb1919 | 2010-10-29 15:25:44 -0700 | [diff] [blame] | 8246 | if (info.broadcastIntentAction != null) { |
| 8247 | sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n"); |
| 8248 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 8249 | if (info.durationMillis != -1) { |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8250 | sb.append("Duration-Millis: ").append(info.durationMillis).append("\n"); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8251 | } |
Brad Fitzpatrick | bfbe577 | 2011-01-19 00:10:58 -0800 | [diff] [blame] | 8252 | if (info.numInstances != -1) { |
| 8253 | sb.append("Instance-Count: ").append(info.numInstances).append("\n"); |
| 8254 | } |
Brad Fitzpatrick | e7520d8 | 2010-11-10 18:08:36 -0800 | [diff] [blame] | 8255 | if (info.tags != null) { |
| 8256 | for (String tag : info.tags) { |
| 8257 | sb.append("Span-Tag: ").append(tag).append("\n"); |
| 8258 | } |
| 8259 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8260 | sb.append("\n"); |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 8261 | if (info.crashInfo != null && info.crashInfo.stackTrace != null) { |
| 8262 | sb.append(info.crashInfo.stackTrace); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8263 | } |
| 8264 | sb.append("\n"); |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8265 | |
| 8266 | // Only buffer up to ~64k. Various logging bits truncate |
| 8267 | // things at 128k. |
| 8268 | needsFlush = (sb.length() > 64 * 1024); |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8269 | } |
| 8270 | |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8271 | // Flush immediately if the buffer's grown too large, or this |
| 8272 | // is a non-system app. Non-system apps are isolated with a |
| 8273 | // different tag & policy and not batched. |
| 8274 | // |
| 8275 | // Batching is useful during internal testing with |
| 8276 | // StrictMode settings turned up high. Without batching, |
| 8277 | // thousands of separate files could be created on boot. |
| 8278 | if (!isSystemApp || needsFlush) { |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8279 | new Thread("Error dump: " + dropboxTag) { |
| 8280 | @Override |
| 8281 | public void run() { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8282 | String report; |
| 8283 | synchronized (sb) { |
| 8284 | report = sb.toString(); |
| 8285 | sb.delete(0, sb.length()); |
| 8286 | sb.trimToSize(); |
| 8287 | } |
| 8288 | if (report.length() != 0) { |
| 8289 | dbox.addText(dropboxTag, report); |
| 8290 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8291 | } |
| 8292 | }.start(); |
| 8293 | return; |
| 8294 | } |
| 8295 | |
| 8296 | // System app batching: |
| 8297 | if (!bufferWasEmpty) { |
Brad Fitzpatrick | e73eb53 | 2010-07-27 16:54:39 -0700 | [diff] [blame] | 8298 | // An existing dropbox-writing thread is outstanding, so |
| 8299 | // we don't need to start it up. The existing thread will |
| 8300 | // catch the buffer appends we just did. |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8301 | return; |
| 8302 | } |
| 8303 | |
| 8304 | // Worker thread to both batch writes and to avoid blocking the caller on I/O. |
| 8305 | // (After this point, we shouldn't access AMS internal data structures.) |
| 8306 | new Thread("Error dump: " + dropboxTag) { |
| 8307 | @Override |
| 8308 | public void run() { |
| 8309 | // 5 second sleep to let stacks arrive and be batched together |
| 8310 | try { |
| 8311 | Thread.sleep(5000); // 5 seconds |
| 8312 | } catch (InterruptedException e) {} |
| 8313 | |
| 8314 | String errorReport; |
| 8315 | synchronized (mStrictModeBuffer) { |
| 8316 | errorReport = mStrictModeBuffer.toString(); |
| 8317 | if (errorReport.length() == 0) { |
| 8318 | return; |
| 8319 | } |
| 8320 | mStrictModeBuffer.delete(0, mStrictModeBuffer.length()); |
| 8321 | mStrictModeBuffer.trimToSize(); |
| 8322 | } |
| 8323 | dbox.addText(dropboxTag, errorReport); |
| 8324 | } |
| 8325 | }.start(); |
| 8326 | } |
| 8327 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8328 | /** |
| 8329 | * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors. |
| 8330 | * @param app object of the crashing app, null for the system server |
| 8331 | * @param tag reported by the caller |
| 8332 | * @param crashInfo describing the context of the error |
| 8333 | * @return true if the process should exit immediately (WTF is fatal) |
| 8334 | */ |
| 8335 | public boolean handleApplicationWtf(IBinder app, String tag, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8336 | ApplicationErrorReport.CrashInfo crashInfo) { |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8337 | ProcessRecord r = findAppProcess(app, "WTF"); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8338 | final String processName = app == null ? "system_server" |
| 8339 | : (r == null ? "unknown" : r.processName); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8340 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 8341 | EventLog.writeEvent(EventLogTags.AM_WTF, |
| 8342 | UserHandle.getUserId(Binder.getCallingUid()), Binder.getCallingPid(), |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8343 | processName, |
Dan Egnor | 2780e73 | 2010-01-22 14:47:35 -0800 | [diff] [blame] | 8344 | r == null ? -1 : r.info.flags, |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8345 | tag, crashInfo.exceptionMessage); |
| 8346 | |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8347 | addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8348 | |
Dianne Hackborn | 1ab4377 | 2011-03-15 14:38:02 -0700 | [diff] [blame] | 8349 | if (r != null && r.pid != Process.myPid() && |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 8350 | Settings.Global.getInt(mContext.getContentResolver(), |
| 8351 | Settings.Global.WTF_IS_FATAL, 0) != 0) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8352 | crashApplication(r, crashInfo); |
| 8353 | return true; |
| 8354 | } else { |
| 8355 | return false; |
| 8356 | } |
| 8357 | } |
| 8358 | |
| 8359 | /** |
| 8360 | * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit}) |
| 8361 | * @return the corresponding {@link ProcessRecord} object, or null if none could be found |
| 8362 | */ |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8363 | private ProcessRecord findAppProcess(IBinder app, String reason) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8364 | if (app == null) { |
| 8365 | return null; |
| 8366 | } |
| 8367 | |
| 8368 | synchronized (this) { |
| 8369 | for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) { |
| 8370 | final int NA = apps.size(); |
| 8371 | for (int ia=0; ia<NA; ia++) { |
| 8372 | ProcessRecord p = apps.valueAt(ia); |
| 8373 | if (p.thread != null && p.thread.asBinder() == app) { |
| 8374 | return p; |
| 8375 | } |
| 8376 | } |
| 8377 | } |
| 8378 | |
Dianne Hackborn | cb44d96 | 2011-03-10 17:02:27 -0800 | [diff] [blame] | 8379 | Slog.w(TAG, "Can't find mystery application for " + reason |
| 8380 | + " from pid=" + Binder.getCallingPid() |
| 8381 | + " uid=" + Binder.getCallingUid() + ": " + app); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8382 | return null; |
| 8383 | } |
| 8384 | } |
| 8385 | |
| 8386 | /** |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8387 | * Utility function for addErrorToDropBox and handleStrictModeViolation's logging |
| 8388 | * to append various headers to the dropbox log text. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8389 | */ |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8390 | private void appendDropBoxProcessHeaders(ProcessRecord process, String processName, |
| 8391 | StringBuilder sb) { |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8392 | // Watchdog thread ends up invoking this function (with |
| 8393 | // a null ProcessRecord) to add the stack file to dropbox. |
| 8394 | // Do not acquire a lock on this (am) in such cases, as it |
| 8395 | // could cause a potential deadlock, if and when watchdog |
| 8396 | // is invoked due to unavailability of lock on am and it |
| 8397 | // would prevent watchdog from killing system_server. |
| 8398 | if (process == null) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8399 | sb.append("Process: ").append(processName).append("\n"); |
Vairavan Srinivasan | 68a4e0a | 2011-02-14 20:45:59 -0800 | [diff] [blame] | 8400 | return; |
| 8401 | } |
Brad Fitzpatrick | 1e02d36 | 2010-09-10 09:19:50 -0700 | [diff] [blame] | 8402 | // Note: ProcessRecord 'process' is guarded by the service |
| 8403 | // instance. (notably process.pkgList, which could otherwise change |
| 8404 | // concurrently during execution of this method) |
| 8405 | synchronized (this) { |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8406 | sb.append("Process: ").append(processName).append("\n"); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8407 | int flags = process.info.flags; |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8408 | IPackageManager pm = AppGlobals.getPackageManager(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8409 | sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n"); |
| 8410 | for (String pkg : process.pkgList) { |
| 8411 | sb.append("Package: ").append(pkg); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 8412 | try { |
Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 8413 | PackageInfo pi = pm.getPackageInfo(pkg, 0, UserHandle.getCallingUserId()); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8414 | if (pi != null) { |
| 8415 | sb.append(" v").append(pi.versionCode); |
| 8416 | if (pi.versionName != null) { |
| 8417 | sb.append(" (").append(pi.versionName).append(")"); |
| 8418 | } |
| 8419 | } |
| 8420 | } catch (RemoteException e) { |
| 8421 | Slog.e(TAG, "Error getting package info: " + pkg, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8422 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8423 | sb.append("\n"); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8424 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8425 | } |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8426 | } |
| 8427 | |
| 8428 | private static String processClass(ProcessRecord process) { |
| 8429 | if (process == null || process.pid == MY_PID) { |
| 8430 | return "system_server"; |
| 8431 | } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 8432 | return "system_app"; |
| 8433 | } else { |
| 8434 | return "data_app"; |
| 8435 | } |
| 8436 | } |
| 8437 | |
| 8438 | /** |
| 8439 | * Write a description of an error (crash, WTF, ANR) to the drop box. |
| 8440 | * @param eventType to include in the drop box tag ("crash", "wtf", etc.) |
| 8441 | * @param process which caused the error, null means the system server |
| 8442 | * @param activity which triggered the error, null if unknown |
| 8443 | * @param parent activity related to the error, null if unknown |
| 8444 | * @param subject line related to the error, null if absent |
| 8445 | * @param report in long form describing the error, null if absent |
| 8446 | * @param logFile to include in the report, null if none |
| 8447 | * @param crashInfo giving an application stack trace, null if absent |
| 8448 | */ |
| 8449 | public void addErrorToDropBox(String eventType, |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8450 | ProcessRecord process, String processName, ActivityRecord activity, |
| 8451 | ActivityRecord parent, String subject, |
Brad Fitzpatrick | ad13b98 | 2010-07-14 12:35:53 -0700 | [diff] [blame] | 8452 | final String report, final File logFile, |
| 8453 | final ApplicationErrorReport.CrashInfo crashInfo) { |
| 8454 | // NOTE -- this must never acquire the ActivityManagerService lock, |
| 8455 | // otherwise the watchdog may be prevented from resetting the system. |
| 8456 | |
| 8457 | final String dropboxTag = processClass(process) + "_" + eventType; |
| 8458 | final DropBoxManager dbox = (DropBoxManager) |
| 8459 | mContext.getSystemService(Context.DROPBOX_SERVICE); |
| 8460 | |
| 8461 | // Exit early if the dropbox isn't configured to accept this report type. |
| 8462 | if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return; |
| 8463 | |
| 8464 | final StringBuilder sb = new StringBuilder(1024); |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 8465 | appendDropBoxProcessHeaders(process, processName, sb); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8466 | if (activity != null) { |
| 8467 | sb.append("Activity: ").append(activity.shortComponentName).append("\n"); |
| 8468 | } |
| 8469 | if (parent != null && parent.app != null && parent.app.pid != process.pid) { |
| 8470 | sb.append("Parent-Process: ").append(parent.app.processName).append("\n"); |
| 8471 | } |
| 8472 | if (parent != null && parent != activity) { |
| 8473 | sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n"); |
| 8474 | } |
| 8475 | if (subject != null) { |
| 8476 | sb.append("Subject: ").append(subject).append("\n"); |
| 8477 | } |
| 8478 | sb.append("Build: ").append(Build.FINGERPRINT).append("\n"); |
Christian Lindeberg | 03d2ca6 | 2010-09-28 14:52:20 +0200 | [diff] [blame] | 8479 | if (Debug.isDebuggerConnected()) { |
| 8480 | sb.append("Debugger: Connected\n"); |
| 8481 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8482 | sb.append("\n"); |
| 8483 | |
| 8484 | // Do the rest in a worker thread to avoid blocking the caller on I/O |
| 8485 | // (After this point, we shouldn't access AMS internal data structures.) |
| 8486 | Thread worker = new Thread("Error dump: " + dropboxTag) { |
| 8487 | @Override |
| 8488 | public void run() { |
| 8489 | if (report != null) { |
| 8490 | sb.append(report); |
| 8491 | } |
| 8492 | if (logFile != null) { |
| 8493 | try { |
| 8494 | sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]")); |
| 8495 | } catch (IOException e) { |
| 8496 | Slog.e(TAG, "Error reading " + logFile, e); |
| 8497 | } |
| 8498 | } |
| 8499 | if (crashInfo != null && crashInfo.stackTrace != null) { |
| 8500 | sb.append(crashInfo.stackTrace); |
| 8501 | } |
| 8502 | |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 8503 | String setting = Settings.Global.ERROR_LOGCAT_PREFIX + dropboxTag; |
| 8504 | int lines = Settings.Global.getInt(mContext.getContentResolver(), setting, 0); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8505 | if (lines > 0) { |
| 8506 | sb.append("\n"); |
| 8507 | |
| 8508 | // Merge several logcat streams, and take the last N lines |
| 8509 | InputStreamReader input = null; |
| 8510 | try { |
| 8511 | java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat", |
| 8512 | "-v", "time", "-b", "events", "-b", "system", "-b", "main", |
| 8513 | "-t", String.valueOf(lines)).redirectErrorStream(true).start(); |
| 8514 | |
| 8515 | try { logcat.getOutputStream().close(); } catch (IOException e) {} |
| 8516 | try { logcat.getErrorStream().close(); } catch (IOException e) {} |
| 8517 | input = new InputStreamReader(logcat.getInputStream()); |
| 8518 | |
| 8519 | int num; |
| 8520 | char[] buf = new char[8192]; |
| 8521 | while ((num = input.read(buf)) > 0) sb.append(buf, 0, num); |
| 8522 | } catch (IOException e) { |
| 8523 | Slog.e(TAG, "Error running logcat", e); |
| 8524 | } finally { |
| 8525 | if (input != null) try { input.close(); } catch (IOException e) {} |
| 8526 | } |
| 8527 | } |
| 8528 | |
| 8529 | dbox.addText(dropboxTag, sb.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8530 | } |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8531 | }; |
| 8532 | |
Dianne Hackborn | 56385cc | 2012-04-30 15:07:47 -0700 | [diff] [blame] | 8533 | if (process == null) { |
| 8534 | // If process is null, we are being called from some internal code |
| 8535 | // and may be about to die -- run this synchronously. |
| 8536 | worker.run(); |
Dan Egnor | a455d19 | 2010-03-12 08:52:28 -0800 | [diff] [blame] | 8537 | } else { |
| 8538 | worker.start(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8539 | } |
| 8540 | } |
| 8541 | |
| 8542 | /** |
| 8543 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 8544 | * Deal with edge cases (intercepts from instrumented applications, |
| 8545 | * ActivityController, error intent receivers, that sort of thing). |
| 8546 | * @param r the application crashing |
| 8547 | * @param crashInfo describing the failure |
| 8548 | */ |
| 8549 | private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8550 | long timeMillis = System.currentTimeMillis(); |
| 8551 | String shortMsg = crashInfo.exceptionClassName; |
| 8552 | String longMsg = crashInfo.exceptionMessage; |
| 8553 | String stackTrace = crashInfo.stackTrace; |
| 8554 | if (shortMsg != null && longMsg != null) { |
| 8555 | longMsg = shortMsg + ": " + longMsg; |
| 8556 | } else if (shortMsg != null) { |
| 8557 | longMsg = shortMsg; |
| 8558 | } |
| 8559 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8560 | AppErrorResult result = new AppErrorResult(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8561 | synchronized (this) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8562 | if (mController != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8563 | try { |
| 8564 | String name = r != null ? r.processName : null; |
| 8565 | int pid = r != null ? r.pid : Binder.getCallingPid(); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8566 | if (!mController.appCrashed(name, pid, |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8567 | shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8568 | Slog.w(TAG, "Force-killing crashed app " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8569 | + " at watcher's request"); |
| 8570 | Process.killProcess(pid); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8571 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8572 | } |
| 8573 | } catch (RemoteException e) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 8574 | mController = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8575 | } |
| 8576 | } |
| 8577 | |
| 8578 | final long origId = Binder.clearCallingIdentity(); |
| 8579 | |
| 8580 | // If this process is running instrumentation, finish it. |
| 8581 | if (r != null && r.instrumentationClass != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8582 | Slog.w(TAG, "Error in app " + r.processName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8583 | + " running instrumentation " + r.instrumentationClass + ":"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8584 | if (shortMsg != null) Slog.w(TAG, " " + shortMsg); |
| 8585 | if (longMsg != null) Slog.w(TAG, " " + longMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8586 | Bundle info = new Bundle(); |
| 8587 | info.putString("shortMsg", shortMsg); |
| 8588 | info.putString("longMsg", longMsg); |
| 8589 | finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info); |
| 8590 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8591 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8592 | } |
| 8593 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 8594 | // If we can't identify the process or it's already exceeded its crash quota, |
| 8595 | // quit right away without showing a crash dialog. |
| 8596 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8597 | Binder.restoreCallingIdentity(origId); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8598 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8599 | } |
| 8600 | |
| 8601 | Message msg = Message.obtain(); |
| 8602 | msg.what = SHOW_ERROR_MSG; |
| 8603 | HashMap data = new HashMap(); |
| 8604 | data.put("result", result); |
| 8605 | data.put("app", r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8606 | msg.obj = data; |
| 8607 | mHandler.sendMessage(msg); |
| 8608 | |
| 8609 | Binder.restoreCallingIdentity(origId); |
| 8610 | } |
| 8611 | |
| 8612 | int res = result.get(); |
| 8613 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8614 | Intent appErrorIntent = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8615 | synchronized (this) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 8616 | if (r != null && !r.isolated) { |
| 8617 | // XXX Can't keep track of crash time for isolated processes, |
| 8618 | // since they don't have a persistent identity. |
| 8619 | mProcessCrashTimes.put(r.info.processName, r.uid, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8620 | SystemClock.uptimeMillis()); |
| 8621 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8622 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8623 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8624 | } |
| 8625 | } |
| 8626 | |
| 8627 | if (appErrorIntent != null) { |
| 8628 | try { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 8629 | mContext.startActivityAsUser(appErrorIntent, new UserHandle(r.userId)); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8630 | } catch (ActivityNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8631 | Slog.w(TAG, "bug report receiver dissappeared", e); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8632 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8633 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8634 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8635 | |
| 8636 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 8637 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 8638 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8639 | if (report == null) { |
| 8640 | return null; |
| 8641 | } |
| 8642 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 8643 | result.setComponent(r.errorReportReceiver); |
| 8644 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 8645 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 8646 | return result; |
| 8647 | } |
| 8648 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8649 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 8650 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8651 | if (r.errorReportReceiver == null) { |
| 8652 | return null; |
| 8653 | } |
| 8654 | |
| 8655 | if (!r.crashing && !r.notResponding) { |
| 8656 | return null; |
| 8657 | } |
| 8658 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8659 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 8660 | report.packageName = r.info.packageName; |
| 8661 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 8662 | report.processName = r.processName; |
| 8663 | report.time = timeMillis; |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 8664 | report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8665 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8666 | if (r.crashing) { |
| 8667 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 8668 | report.crashInfo = crashInfo; |
| 8669 | } else if (r.notResponding) { |
| 8670 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 8671 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8672 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8673 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 8674 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 8675 | report.anrInfo.info = r.notRespondingReport.longMsg; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8676 | } |
| 8677 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 8678 | return report; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 8679 | } |
| 8680 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8681 | public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8682 | enforceNotIsolatedCaller("getProcessesInErrorState"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8683 | // assume our apps are happy - lazy create the list |
| 8684 | List<ActivityManager.ProcessErrorStateInfo> errList = null; |
| 8685 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8686 | final boolean allUsers = ActivityManager.checkUidPermission( |
| 8687 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 8688 | Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED; |
| 8689 | int userId = UserHandle.getUserId(Binder.getCallingUid()); |
| 8690 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8691 | synchronized (this) { |
| 8692 | |
| 8693 | // iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8694 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8695 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8696 | if (!allUsers && app.userId != userId) { |
| 8697 | continue; |
| 8698 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8699 | if ((app.thread != null) && (app.crashing || app.notResponding)) { |
| 8700 | // This one's in trouble, so we'll generate a report for it |
| 8701 | // crashes are higher priority (in case there's a crash *and* an anr) |
| 8702 | ActivityManager.ProcessErrorStateInfo report = null; |
| 8703 | if (app.crashing) { |
| 8704 | report = app.crashingReport; |
| 8705 | } else if (app.notResponding) { |
| 8706 | report = app.notRespondingReport; |
| 8707 | } |
| 8708 | |
| 8709 | if (report != null) { |
| 8710 | if (errList == null) { |
| 8711 | errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1); |
| 8712 | } |
| 8713 | errList.add(report); |
| 8714 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8715 | 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] | 8716 | " crashing = " + app.crashing + |
| 8717 | " notResponding = " + app.notResponding); |
| 8718 | } |
| 8719 | } |
| 8720 | } |
| 8721 | } |
| 8722 | |
| 8723 | return errList; |
| 8724 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8725 | |
| 8726 | static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) { |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8727 | if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8728 | if (currApp != null) { |
| 8729 | currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1; |
| 8730 | } |
| 8731 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8732 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 8733 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8734 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 8735 | if (currApp != null) { |
| 8736 | currApp.lru = 0; |
| 8737 | } |
| 8738 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 8739 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8740 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 8741 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 8742 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 8743 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 8744 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 8745 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 8746 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 8747 | } else { |
| 8748 | return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 8749 | } |
| 8750 | } |
| 8751 | |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8752 | private void fillInProcMemInfo(ProcessRecord app, |
| 8753 | ActivityManager.RunningAppProcessInfo outInfo) { |
| 8754 | outInfo.pid = app.pid; |
| 8755 | outInfo.uid = app.info.uid; |
| 8756 | if (mHeavyWeightProcess == app) { |
| 8757 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE; |
| 8758 | } |
| 8759 | if (app.persistent) { |
| 8760 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT; |
| 8761 | } |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8762 | if (app.hasActivities) { |
| 8763 | outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES; |
| 8764 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8765 | outInfo.lastTrimLevel = app.trimMemoryLevel; |
| 8766 | int adj = app.curAdj; |
| 8767 | outInfo.importance = oomAdjToImportance(adj, outInfo); |
| 8768 | outInfo.importanceReasonCode = app.adjTypeCode; |
| 8769 | } |
| 8770 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8771 | public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8772 | enforceNotIsolatedCaller("getRunningAppProcesses"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8773 | // Lazy instantiation of list |
| 8774 | List<ActivityManager.RunningAppProcessInfo> runList = null; |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8775 | final boolean allUsers = ActivityManager.checkUidPermission( |
| 8776 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 8777 | Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED; |
| 8778 | int userId = UserHandle.getUserId(Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8779 | synchronized (this) { |
| 8780 | // Iterate across all processes |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 8781 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 8782 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 8783 | if (!allUsers && app.userId != userId) { |
| 8784 | continue; |
| 8785 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8786 | if ((app.thread != null) && (!app.crashing && !app.notResponding)) { |
| 8787 | // Generate process state info for running application |
| 8788 | ActivityManager.RunningAppProcessInfo currApp = |
| 8789 | new ActivityManager.RunningAppProcessInfo(app.processName, |
| 8790 | app.pid, app.getPackageList()); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8791 | fillInProcMemInfo(app, currApp); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8792 | if (app.adjSource instanceof ProcessRecord) { |
| 8793 | currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 8794 | currApp.importanceReasonImportance = oomAdjToImportance( |
| 8795 | app.adjSourceOom, null); |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8796 | } else if (app.adjSource instanceof ActivityRecord) { |
| 8797 | ActivityRecord r = (ActivityRecord)app.adjSource; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 8798 | if (r.app != null) currApp.importanceReasonPid = r.app.pid; |
| 8799 | } |
| 8800 | if (app.adjTarget instanceof ComponentName) { |
| 8801 | currApp.importanceReasonComponent = (ComponentName)app.adjTarget; |
| 8802 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 8803 | //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8804 | // + " lru=" + currApp.lru); |
| 8805 | if (runList == null) { |
| 8806 | runList = new ArrayList<ActivityManager.RunningAppProcessInfo>(); |
| 8807 | } |
| 8808 | runList.add(currApp); |
| 8809 | } |
| 8810 | } |
| 8811 | } |
| 8812 | return runList; |
| 8813 | } |
| 8814 | |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8815 | public List<ApplicationInfo> getRunningExternalApplications() { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 8816 | enforceNotIsolatedCaller("getRunningExternalApplications"); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8817 | List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses(); |
| 8818 | List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>(); |
| 8819 | if (runningApps != null && runningApps.size() > 0) { |
| 8820 | Set<String> extList = new HashSet<String>(); |
| 8821 | for (ActivityManager.RunningAppProcessInfo app : runningApps) { |
| 8822 | if (app.pkgList != null) { |
| 8823 | for (String pkg : app.pkgList) { |
| 8824 | extList.add(pkg); |
| 8825 | } |
| 8826 | } |
| 8827 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 8828 | IPackageManager pm = AppGlobals.getPackageManager(); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8829 | for (String pkg : extList) { |
| 8830 | try { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 8831 | ApplicationInfo info = pm.getApplicationInfo(pkg, 0, UserHandle.getCallingUserId()); |
Suchi Amalapurapu | f7f5dda | 2010-03-23 10:34:28 -0700 | [diff] [blame] | 8832 | if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { |
| 8833 | retList.add(info); |
| 8834 | } |
| 8835 | } catch (RemoteException e) { |
| 8836 | } |
| 8837 | } |
| 8838 | } |
| 8839 | return retList; |
| 8840 | } |
| 8841 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8842 | @Override |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 8843 | public void getMyMemoryState(ActivityManager.RunningAppProcessInfo outInfo) { |
| 8844 | enforceNotIsolatedCaller("getMyMemoryState"); |
| 8845 | synchronized (this) { |
| 8846 | ProcessRecord proc; |
| 8847 | synchronized (mPidsSelfLocked) { |
| 8848 | proc = mPidsSelfLocked.get(Binder.getCallingPid()); |
| 8849 | } |
| 8850 | fillInProcMemInfo(proc, outInfo); |
| 8851 | } |
| 8852 | } |
| 8853 | |
| 8854 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8855 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8856 | if (checkCallingPermission(android.Manifest.permission.DUMP) |
| 8857 | != PackageManager.PERMISSION_GRANTED) { |
| 8858 | pw.println("Permission Denial: can't dump ActivityManager from from pid=" |
| 8859 | + Binder.getCallingPid() |
| 8860 | + ", uid=" + Binder.getCallingUid() |
| 8861 | + " without permission " |
| 8862 | + android.Manifest.permission.DUMP); |
| 8863 | return; |
| 8864 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8865 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8866 | boolean dumpAll = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8867 | boolean dumpClient = false; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8868 | String dumpPackage = null; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8869 | |
| 8870 | int opti = 0; |
| 8871 | while (opti < args.length) { |
| 8872 | String opt = args[opti]; |
| 8873 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 8874 | break; |
| 8875 | } |
| 8876 | opti++; |
| 8877 | if ("-a".equals(opt)) { |
| 8878 | dumpAll = true; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8879 | } else if ("-c".equals(opt)) { |
| 8880 | dumpClient = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8881 | } else if ("-h".equals(opt)) { |
| 8882 | pw.println("Activity manager dump options:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8883 | pw.println(" [-a] [-c] [-h] [cmd] ..."); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8884 | pw.println(" cmd may be one of:"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8885 | pw.println(" a[ctivities]: activity stack state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8886 | pw.println(" b[roadcasts] [PACKAGE_NAME]: broadcast state"); |
| 8887 | pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state"); |
| 8888 | pw.println(" p[rocesses] [PACKAGE_NAME]: process state"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8889 | pw.println(" o[om]: out of memory management"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8890 | pw.println(" prov[iders] [COMP_SPEC ...]: content provider state"); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8891 | pw.println(" provider [COMP_SPEC]: provider client-side state"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8892 | pw.println(" s[ervices] [COMP_SPEC ...]: service state"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8893 | pw.println(" service [COMP_SPEC]: service client-side state"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8894 | pw.println(" package [PACKAGE_NAME]: all state related to given package"); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8895 | pw.println(" all: dump all activities"); |
| 8896 | pw.println(" top: dump the top activity"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8897 | pw.println(" cmd may also be a COMP_SPEC to dump activities."); |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 8898 | pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),"); |
| 8899 | pw.println(" a partial substring in a component name, a"); |
| 8900 | pw.println(" hex object identifier."); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8901 | pw.println(" -a: include all available server state."); |
| 8902 | pw.println(" -c: include client state."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8903 | return; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8904 | } else { |
| 8905 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8906 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8907 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 8908 | |
| 8909 | long origId = Binder.clearCallingIdentity(); |
| 8910 | boolean more = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8911 | // Is the caller requesting to dump a particular piece of data? |
| 8912 | if (opti < args.length) { |
| 8913 | String cmd = args[opti]; |
| 8914 | opti++; |
| 8915 | if ("activities".equals(cmd) || "a".equals(cmd)) { |
| 8916 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8917 | dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8918 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8919 | } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8920 | String[] newArgs; |
| 8921 | String name; |
| 8922 | if (opti >= args.length) { |
| 8923 | name = null; |
| 8924 | newArgs = EMPTY_STRING_ARRAY; |
| 8925 | } else { |
| 8926 | name = args[opti]; |
| 8927 | opti++; |
| 8928 | newArgs = new String[args.length - opti]; |
| 8929 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8930 | args.length - opti); |
| 8931 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8932 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8933 | dumpBroadcastsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8934 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8935 | } else if ("intents".equals(cmd) || "i".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8936 | String[] newArgs; |
| 8937 | String name; |
| 8938 | if (opti >= args.length) { |
| 8939 | name = null; |
| 8940 | newArgs = EMPTY_STRING_ARRAY; |
| 8941 | } else { |
| 8942 | name = args[opti]; |
| 8943 | opti++; |
| 8944 | newArgs = new String[args.length - opti]; |
| 8945 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8946 | args.length - opti); |
| 8947 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8948 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8949 | dumpPendingIntentsLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8950 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8951 | } else if ("processes".equals(cmd) || "p".equals(cmd)) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8952 | String[] newArgs; |
| 8953 | String name; |
| 8954 | if (opti >= args.length) { |
| 8955 | name = null; |
| 8956 | newArgs = EMPTY_STRING_ARRAY; |
| 8957 | } else { |
| 8958 | name = args[opti]; |
| 8959 | opti++; |
| 8960 | newArgs = new String[args.length - opti]; |
| 8961 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 8962 | args.length - opti); |
| 8963 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8964 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8965 | dumpProcessesLocked(fd, pw, args, opti, true, name); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8966 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 8967 | } else if ("oom".equals(cmd) || "o".equals(cmd)) { |
| 8968 | synchronized (this) { |
| 8969 | dumpOomLocked(fd, pw, args, opti, true); |
| 8970 | } |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 8971 | } else if ("provider".equals(cmd)) { |
| 8972 | String[] newArgs; |
| 8973 | String name; |
| 8974 | if (opti >= args.length) { |
| 8975 | name = null; |
| 8976 | newArgs = EMPTY_STRING_ARRAY; |
| 8977 | } else { |
| 8978 | name = args[opti]; |
| 8979 | opti++; |
| 8980 | newArgs = new String[args.length - opti]; |
| 8981 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 8982 | } |
| 8983 | if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) { |
| 8984 | pw.println("No providers match: " + name); |
| 8985 | pw.println("Use -h for help."); |
| 8986 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8987 | } else if ("providers".equals(cmd) || "prov".equals(cmd)) { |
| 8988 | synchronized (this) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 8989 | dumpProvidersLocked(fd, pw, args, opti, true, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8990 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 8991 | } else if ("service".equals(cmd)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 8992 | String[] newArgs; |
| 8993 | String name; |
| 8994 | if (opti >= args.length) { |
| 8995 | name = null; |
| 8996 | newArgs = EMPTY_STRING_ARRAY; |
| 8997 | } else { |
| 8998 | name = args[opti]; |
| 8999 | opti++; |
| 9000 | newArgs = new String[args.length - opti]; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9001 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 9002 | args.length - opti); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9003 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 9004 | if (!mServices.dumpService(fd, pw, name, newArgs, 0, dumpAll)) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9005 | pw.println("No services match: " + name); |
| 9006 | pw.println("Use -h for help."); |
| 9007 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9008 | } else if ("package".equals(cmd)) { |
| 9009 | String[] newArgs; |
| 9010 | if (opti >= args.length) { |
| 9011 | pw.println("package: no package name specified"); |
| 9012 | pw.println("Use -h for help."); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9013 | } else { |
| 9014 | dumpPackage = args[opti]; |
| 9015 | opti++; |
| 9016 | newArgs = new String[args.length - opti]; |
| 9017 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, |
| 9018 | args.length - opti); |
| 9019 | args = newArgs; |
| 9020 | opti = 0; |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9021 | more = true; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9022 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9023 | } else if ("services".equals(cmd) || "s".equals(cmd)) { |
| 9024 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 9025 | mServices.dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9026 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9027 | } else { |
| 9028 | // Dumping a single activity? |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9029 | if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) { |
| 9030 | pw.println("Bad activity command, or no activities match: " + cmd); |
| 9031 | pw.println("Use -h for help."); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9032 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9033 | } |
| 9034 | if (!more) { |
| 9035 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9036 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9037 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9038 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9039 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9040 | // No piece of data specified, dump everything. |
| 9041 | synchronized (this) { |
| 9042 | boolean needSep; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9043 | needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9044 | if (needSep) { |
| 9045 | pw.println(" "); |
| 9046 | } |
| 9047 | if (dumpAll) { |
| 9048 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9049 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9050 | needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9051 | if (needSep) { |
| 9052 | pw.println(" "); |
| 9053 | } |
| 9054 | if (dumpAll) { |
| 9055 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9056 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9057 | needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9058 | if (needSep) { |
| 9059 | pw.println(" "); |
| 9060 | } |
| 9061 | if (dumpAll) { |
| 9062 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9063 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 9064 | needSep = mServices.dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9065 | if (needSep) { |
| 9066 | pw.println(" "); |
| 9067 | } |
| 9068 | if (dumpAll) { |
| 9069 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9070 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9071 | needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9072 | if (needSep) { |
| 9073 | pw.println(" "); |
| 9074 | } |
| 9075 | if (dumpAll) { |
| 9076 | pw.println("-------------------------------------------------------------------------------"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9077 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9078 | dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9079 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9080 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9081 | } |
Amith Yamasani | 7463ada | 2012-04-11 15:02:39 -0700 | [diff] [blame] | 9082 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9083 | boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9084 | int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9085 | pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)"); |
| 9086 | pw.println(" Main stack:"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9087 | dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient, |
| 9088 | dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9089 | pw.println(" "); |
| 9090 | pw.println(" Running activities (most recent first):"); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9091 | dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false, |
| 9092 | dumpPackage); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9093 | if (mMainStack.mWaitingVisibleActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9094 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9095 | pw.println(" Activities waiting for another to become visible:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9096 | dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9097 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9098 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9099 | if (mMainStack.mStoppingActivities.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9100 | pw.println(" "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9101 | pw.println(" Activities waiting to stop:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9102 | dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9103 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9104 | } |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 9105 | if (mMainStack.mGoingToSleepActivities.size() > 0) { |
| 9106 | pw.println(" "); |
| 9107 | pw.println(" Activities waiting to sleep:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9108 | dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9109 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | 4eba96b | 2011-01-21 13:34:36 -0800 | [diff] [blame] | 9110 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9111 | if (mMainStack.mFinishingActivities.size() > 0) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9112 | pw.println(" "); |
| 9113 | pw.println(" Activities waiting to finish:"); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9114 | dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9115 | !dumpAll, false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9116 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9117 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9118 | pw.println(" "); |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 9119 | if (mMainStack.mPausingActivity != null) { |
| 9120 | pw.println(" mPausingActivity: " + mMainStack.mPausingActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9121 | } |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9122 | pw.println(" mResumedActivity: " + mMainStack.mResumedActivity); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9123 | pw.println(" mFocusedActivity: " + mFocusedActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9124 | if (dumpAll) { |
| 9125 | pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity); |
| 9126 | pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout); |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9127 | pw.println(" mDismissKeyguardOnNextActivity: " |
| 9128 | + mMainStack.mDismissKeyguardOnNextActivity); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9129 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9130 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9131 | if (mRecentTasks.size() > 0) { |
| 9132 | pw.println(); |
| 9133 | pw.println(" Recent tasks:"); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9134 | |
| 9135 | final int N = mRecentTasks.size(); |
| 9136 | for (int i=0; i<N; i++) { |
| 9137 | TaskRecord tr = mRecentTasks.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9138 | if (dumpPackage != null) { |
| 9139 | if (tr.realActivity == null || |
| 9140 | !dumpPackage.equals(tr.realActivity)) { |
| 9141 | continue; |
| 9142 | } |
| 9143 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9144 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 9145 | pw.println(tr); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9146 | if (dumpAll) { |
| 9147 | mRecentTasks.get(i).dump(pw, " "); |
| 9148 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9149 | } |
| 9150 | } |
| 9151 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9152 | if (dumpAll) { |
| 9153 | pw.println(" "); |
| 9154 | pw.println(" mCurTask: " + mCurTask); |
| 9155 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9156 | |
| 9157 | return true; |
| 9158 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9159 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9160 | boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9161 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9162 | boolean needSep = false; |
| 9163 | int numPers = 0; |
| 9164 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9165 | pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)"); |
| 9166 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9167 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9168 | for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) { |
| 9169 | final int NA = procs.size(); |
| 9170 | for (int ia=0; ia<NA; ia++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9171 | ProcessRecord r = procs.valueAt(ia); |
| 9172 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9173 | continue; |
| 9174 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9175 | if (!needSep) { |
| 9176 | pw.println(" All known processes:"); |
| 9177 | needSep = true; |
| 9178 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9179 | pw.print(r.persistent ? " *PERS*" : " *APP*"); |
| 9180 | pw.print(" UID "); pw.print(procs.keyAt(ia)); |
| 9181 | pw.print(" "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9182 | r.dump(pw, " "); |
| 9183 | if (r.persistent) { |
| 9184 | numPers++; |
| 9185 | } |
| 9186 | } |
| 9187 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9188 | } |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 9189 | |
| 9190 | if (mIsolatedProcesses.size() > 0) { |
| 9191 | if (needSep) pw.println(" "); |
| 9192 | needSep = true; |
| 9193 | pw.println(" Isolated process list (sorted by uid):"); |
| 9194 | for (int i=0; i<mIsolatedProcesses.size(); i++) { |
| 9195 | ProcessRecord r = mIsolatedProcesses.valueAt(i); |
| 9196 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9197 | continue; |
| 9198 | } |
| 9199 | pw.println(String.format("%sIsolated #%2d: %s", |
| 9200 | " ", i, r.toString())); |
| 9201 | } |
| 9202 | } |
| 9203 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9204 | if (mLruProcesses.size() > 0) { |
| 9205 | if (needSep) pw.println(" "); |
| 9206 | needSep = true; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9207 | pw.println(" Process LRU list (sorted by oom_adj):"); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9208 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9209 | "Proc", "PERS", false, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9210 | needSep = true; |
| 9211 | } |
| 9212 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9213 | if (dumpAll) { |
| 9214 | synchronized (mPidsSelfLocked) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9215 | boolean printed = false; |
| 9216 | for (int i=0; i<mPidsSelfLocked.size(); i++) { |
| 9217 | ProcessRecord r = mPidsSelfLocked.valueAt(i); |
| 9218 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9219 | continue; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9220 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9221 | if (!printed) { |
| 9222 | if (needSep) pw.println(" "); |
| 9223 | needSep = true; |
| 9224 | pw.println(" PID mappings:"); |
| 9225 | printed = true; |
| 9226 | } |
| 9227 | pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i)); |
| 9228 | pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9229 | } |
| 9230 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9231 | } |
| 9232 | |
| 9233 | if (mForegroundProcesses.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9234 | synchronized (mPidsSelfLocked) { |
| 9235 | boolean printed = false; |
| 9236 | for (int i=0; i<mForegroundProcesses.size(); i++) { |
| 9237 | ProcessRecord r = mPidsSelfLocked.get( |
| 9238 | mForegroundProcesses.valueAt(i).pid); |
| 9239 | if (dumpPackage != null && (r == null |
| 9240 | || !dumpPackage.equals(r.info.packageName))) { |
| 9241 | continue; |
| 9242 | } |
| 9243 | if (!printed) { |
| 9244 | if (needSep) pw.println(" "); |
| 9245 | needSep = true; |
| 9246 | pw.println(" Foreground Processes:"); |
| 9247 | printed = true; |
| 9248 | } |
| 9249 | pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i)); |
| 9250 | pw.print(": "); pw.println(mForegroundProcesses.valueAt(i)); |
| 9251 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9252 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9253 | } |
| 9254 | |
| 9255 | if (mPersistentStartingProcesses.size() > 0) { |
| 9256 | if (needSep) pw.println(" "); |
| 9257 | needSep = true; |
| 9258 | pw.println(" Persisent processes that are starting:"); |
| 9259 | dumpProcessList(pw, this, mPersistentStartingProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9260 | "Starting Norm", "Restarting PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9261 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9262 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9263 | if (mRemovedProcesses.size() > 0) { |
| 9264 | if (needSep) pw.println(" "); |
| 9265 | needSep = true; |
| 9266 | pw.println(" Processes that are being removed:"); |
| 9267 | dumpProcessList(pw, this, mRemovedProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9268 | "Removed Norm", "Removed PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9269 | } |
| 9270 | |
| 9271 | if (mProcessesOnHold.size() > 0) { |
| 9272 | if (needSep) pw.println(" "); |
| 9273 | needSep = true; |
| 9274 | pw.println(" Processes that are on old until the system is ready:"); |
| 9275 | dumpProcessList(pw, this, mProcessesOnHold, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9276 | "OnHold Norm", "OnHold PERS", dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9277 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9278 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9279 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9280 | |
| 9281 | if (mProcessCrashTimes.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9282 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9283 | long now = SystemClock.uptimeMillis(); |
| 9284 | for (Map.Entry<String, SparseArray<Long>> procs |
| 9285 | : mProcessCrashTimes.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9286 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9287 | SparseArray<Long> uids = procs.getValue(); |
| 9288 | final int N = uids.size(); |
| 9289 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9290 | int puid = uids.keyAt(i); |
| 9291 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 9292 | if (dumpPackage != null && (r == null |
| 9293 | || !dumpPackage.equals(r.info.packageName))) { |
| 9294 | continue; |
| 9295 | } |
| 9296 | if (!printed) { |
| 9297 | if (needSep) pw.println(" "); |
| 9298 | needSep = true; |
| 9299 | pw.println(" Time since processes crashed:"); |
| 9300 | printed = true; |
| 9301 | } |
| 9302 | pw.print(" Process "); pw.print(pname); |
| 9303 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9304 | pw.print(": last crashed "); |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 9305 | TimeUtils.formatDuration(now-uids.valueAt(i), pw); |
| 9306 | pw.println(" ago"); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 9307 | } |
| 9308 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9309 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9310 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9311 | if (mBadProcesses.getMap().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9312 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9313 | for (Map.Entry<String, SparseArray<Long>> procs |
| 9314 | : mBadProcesses.getMap().entrySet()) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9315 | String pname = procs.getKey(); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9316 | SparseArray<Long> uids = procs.getValue(); |
| 9317 | final int N = uids.size(); |
| 9318 | for (int i=0; i<N; i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9319 | int puid = uids.keyAt(i); |
| 9320 | ProcessRecord r = mProcessNames.get(pname, puid); |
| 9321 | if (dumpPackage != null && (r == null |
| 9322 | || !dumpPackage.equals(r.info.packageName))) { |
| 9323 | continue; |
| 9324 | } |
| 9325 | if (!printed) { |
| 9326 | if (needSep) pw.println(" "); |
| 9327 | needSep = true; |
| 9328 | pw.println(" Bad processes:"); |
| 9329 | } |
| 9330 | pw.print(" Bad process "); pw.print(pname); |
| 9331 | pw.print(" uid "); pw.print(puid); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9332 | pw.print(": crashed at time "); |
| 9333 | pw.println(uids.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9334 | } |
| 9335 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9336 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9337 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9338 | pw.println(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 9339 | pw.println(" mStartedUsers:"); |
| 9340 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 9341 | UserStartedState uss = mStartedUsers.valueAt(i); |
| 9342 | pw.print(" User #"); pw.print(uss.mHandle.getIdentifier()); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 9343 | pw.print(": "); uss.dump("", pw); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 9344 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 9345 | pw.print(" mUserLru: ["); |
| 9346 | for (int i=0; i<mUserLru.size(); i++) { |
| 9347 | if (i > 0) pw.print(", "); |
| 9348 | pw.print(mUserLru.get(i)); |
| 9349 | } |
| 9350 | pw.println("]"); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 9351 | if (dumpAll) { |
| 9352 | pw.print(" mStartedUserArray: "); pw.println(Arrays.toString(mStartedUserArray)); |
| 9353 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9354 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9355 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 5068560 | 2011-12-01 12:23:37 -0800 | [diff] [blame] | 9356 | if (dumpAll) { |
| 9357 | StringBuilder sb = new StringBuilder(128); |
| 9358 | sb.append(" mPreviousProcessVisibleTime: "); |
| 9359 | TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb); |
| 9360 | pw.println(sb); |
| 9361 | } |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 9362 | if (mHeavyWeightProcess != null) { |
| 9363 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 9364 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9365 | pw.println(" mConfiguration: " + mConfiguration); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9366 | if (dumpAll) { |
| 9367 | pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange); |
Dianne Hackborn | 3d0724d | 2011-05-12 15:39:41 -0700 | [diff] [blame] | 9368 | if (mCompatModePackages.getPackages().size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9369 | boolean printed = false; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 9370 | for (Map.Entry<String, Integer> entry |
| 9371 | : mCompatModePackages.getPackages().entrySet()) { |
| 9372 | String pkg = entry.getKey(); |
| 9373 | int mode = entry.getValue(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9374 | if (dumpPackage != null && !dumpPackage.equals(pkg)) { |
| 9375 | continue; |
| 9376 | } |
| 9377 | if (!printed) { |
| 9378 | pw.println(" mScreenCompatPackages:"); |
| 9379 | printed = true; |
| 9380 | } |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 9381 | pw.print(" "); pw.print(pkg); pw.print(": "); |
| 9382 | pw.print(mode); pw.println(); |
| 9383 | } |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 9384 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9385 | } |
Dianne Hackborn | ff5b158 | 2012-04-12 17:24:07 -0700 | [diff] [blame] | 9386 | if (mSleeping || mWentToSleep || mLockScreenShown) { |
| 9387 | pw.println(" mSleeping=" + mSleeping + " mWentToSleep=" + mWentToSleep |
| 9388 | + " mLockScreenShown " + mLockScreenShown); |
| 9389 | } |
| 9390 | if (mShuttingDown) { |
| 9391 | pw.println(" mShuttingDown=" + mShuttingDown); |
| 9392 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9393 | if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient |
| 9394 | || mOrigWaitForDebugger) { |
| 9395 | pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp |
| 9396 | + " mDebugTransient=" + mDebugTransient |
| 9397 | + " mOrigWaitForDebugger=" + mOrigWaitForDebugger); |
| 9398 | } |
Siva Velusamy | 92a8b22 | 2012-03-09 16:24:04 -0800 | [diff] [blame] | 9399 | if (mOpenGlTraceApp != null) { |
| 9400 | pw.println(" mOpenGlTraceApp=" + mOpenGlTraceApp); |
| 9401 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 9402 | if (mProfileApp != null || mProfileProc != null || mProfileFile != null |
| 9403 | || mProfileFd != null) { |
| 9404 | pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc); |
| 9405 | pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd); |
| 9406 | pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler=" |
| 9407 | + mAutoStopProfiler); |
| 9408 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9409 | if (mAlwaysFinishActivities || mController != null) { |
| 9410 | pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities |
| 9411 | + " mController=" + mController); |
| 9412 | } |
| 9413 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9414 | pw.println(" Total persistent processes: " + numPers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9415 | pw.println(" mStartRunning=" + mStartRunning |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 9416 | + " mProcessesReady=" + mProcessesReady |
| 9417 | + " mSystemReady=" + mSystemReady); |
| 9418 | pw.println(" mBooting=" + mBooting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9419 | + " mBooted=" + mBooted |
| 9420 | + " mFactoryTest=" + mFactoryTest); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9421 | pw.print(" mLastPowerCheckRealtime="); |
| 9422 | TimeUtils.formatDuration(mLastPowerCheckRealtime, pw); |
| 9423 | pw.println(""); |
| 9424 | pw.print(" mLastPowerCheckUptime="); |
| 9425 | TimeUtils.formatDuration(mLastPowerCheckUptime, pw); |
| 9426 | pw.println(""); |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 9427 | pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep); |
| 9428 | pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity); |
Dianne Hackborn | 906497c | 2010-05-10 15:57:38 -0700 | [diff] [blame] | 9429 | pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 9430 | pw.println(" mNumNonHiddenProcs=" + mNumNonHiddenProcs |
| 9431 | + " mNumHiddenProcs=" + mNumHiddenProcs |
| 9432 | + " mNumServiceProcs=" + mNumServiceProcs |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9433 | + " mNewNumServiceProcs=" + mNewNumServiceProcs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9434 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9435 | |
| 9436 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9437 | } |
| 9438 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9439 | boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9440 | int opti, boolean needSep, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9441 | if (mProcessesToGc.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9442 | boolean printed = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9443 | long now = SystemClock.uptimeMillis(); |
| 9444 | for (int i=0; i<mProcessesToGc.size(); i++) { |
| 9445 | ProcessRecord proc = mProcessesToGc.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9446 | if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) { |
| 9447 | continue; |
| 9448 | } |
| 9449 | if (!printed) { |
| 9450 | if (needSep) pw.println(" "); |
| 9451 | needSep = true; |
| 9452 | pw.println(" Processes that are waiting to GC:"); |
| 9453 | printed = true; |
| 9454 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9455 | pw.print(" Process "); pw.println(proc); |
| 9456 | pw.print(" lowMem="); pw.print(proc.reportLowMemory); |
| 9457 | pw.print(", last gced="); |
| 9458 | pw.print(now-proc.lastRequestedGc); |
| 9459 | pw.print(" ms ago, last lowMem="); |
| 9460 | pw.print(now-proc.lastLowMemory); |
| 9461 | pw.println(" ms ago"); |
| 9462 | |
| 9463 | } |
| 9464 | } |
| 9465 | return needSep; |
| 9466 | } |
| 9467 | |
| 9468 | boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 9469 | int opti, boolean dumpAll) { |
| 9470 | boolean needSep = false; |
| 9471 | |
| 9472 | if (mLruProcesses.size() > 0) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9473 | if (needSep) pw.println(" "); |
| 9474 | needSep = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 9475 | pw.println(" OOM levels:"); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9476 | pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9477 | pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9478 | pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ); |
| 9479 | pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ); |
| 9480 | pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ); |
| 9481 | pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 9482 | pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9483 | pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9484 | pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9485 | pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 9486 | pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 9487 | 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] | 9488 | 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] | 9489 | |
| 9490 | if (needSep) pw.println(" "); |
| 9491 | needSep = true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9492 | pw.println(" Process OOM control:"); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9493 | dumpProcessOomList(pw, this, mLruProcesses, " ", |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9494 | "Proc", "PERS", true, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9495 | needSep = true; |
| 9496 | } |
| 9497 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9498 | needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9499 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9500 | pw.println(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9501 | pw.println(" mHomeProcess: " + mHomeProcess); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 9502 | pw.println(" mPreviousProcess: " + mPreviousProcess); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9503 | if (mHeavyWeightProcess != null) { |
| 9504 | pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); |
| 9505 | } |
| 9506 | |
| 9507 | return true; |
| 9508 | } |
| 9509 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9510 | /** |
| 9511 | * There are three ways to call this: |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9512 | * - no provider specified: dump all the providers |
| 9513 | * - a flattened component name that matched an existing provider was specified as the |
| 9514 | * first arg: dump that one provider |
| 9515 | * - the first arg isn't the flattened component name of an existing provider: |
| 9516 | * dump all providers whose component contains the first arg as a substring |
| 9517 | */ |
| 9518 | protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9519 | int opti, boolean dumpAll) { |
Marco Nelissen | de7408c | 2012-02-08 14:57:38 -0800 | [diff] [blame] | 9520 | return mProviderMap.dumpProvider(fd, pw, name, args, opti, dumpAll); |
Marco Nelissen | 18cb287 | 2011-11-15 11:19:53 -0800 | [diff] [blame] | 9521 | } |
| 9522 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9523 | static class ItemMatcher { |
| 9524 | ArrayList<ComponentName> components; |
| 9525 | ArrayList<String> strings; |
| 9526 | ArrayList<Integer> objects; |
| 9527 | boolean all; |
| 9528 | |
| 9529 | ItemMatcher() { |
| 9530 | all = true; |
| 9531 | } |
| 9532 | |
| 9533 | void build(String name) { |
| 9534 | ComponentName componentName = ComponentName.unflattenFromString(name); |
| 9535 | if (componentName != null) { |
| 9536 | if (components == null) { |
| 9537 | components = new ArrayList<ComponentName>(); |
| 9538 | } |
| 9539 | components.add(componentName); |
| 9540 | all = false; |
| 9541 | } else { |
| 9542 | int objectId = 0; |
| 9543 | // Not a '/' separated full component name; maybe an object ID? |
| 9544 | try { |
| 9545 | objectId = Integer.parseInt(name, 16); |
| 9546 | if (objects == null) { |
| 9547 | objects = new ArrayList<Integer>(); |
| 9548 | } |
| 9549 | objects.add(objectId); |
| 9550 | all = false; |
| 9551 | } catch (RuntimeException e) { |
| 9552 | // Not an integer; just do string match. |
| 9553 | if (strings == null) { |
| 9554 | strings = new ArrayList<String>(); |
| 9555 | } |
| 9556 | strings.add(name); |
| 9557 | all = false; |
| 9558 | } |
| 9559 | } |
| 9560 | } |
| 9561 | |
| 9562 | int build(String[] args, int opti) { |
| 9563 | for (; opti<args.length; opti++) { |
| 9564 | String name = args[opti]; |
| 9565 | if ("--".equals(name)) { |
| 9566 | return opti+1; |
| 9567 | } |
| 9568 | build(name); |
| 9569 | } |
| 9570 | return opti; |
| 9571 | } |
| 9572 | |
| 9573 | boolean match(Object object, ComponentName comp) { |
| 9574 | if (all) { |
| 9575 | return true; |
| 9576 | } |
| 9577 | if (components != null) { |
| 9578 | for (int i=0; i<components.size(); i++) { |
| 9579 | if (components.get(i).equals(comp)) { |
| 9580 | return true; |
| 9581 | } |
| 9582 | } |
| 9583 | } |
| 9584 | if (objects != null) { |
| 9585 | for (int i=0; i<objects.size(); i++) { |
| 9586 | if (System.identityHashCode(object) == objects.get(i)) { |
| 9587 | return true; |
| 9588 | } |
| 9589 | } |
| 9590 | } |
| 9591 | if (strings != null) { |
| 9592 | String flat = comp.flattenToString(); |
| 9593 | for (int i=0; i<strings.size(); i++) { |
| 9594 | if (flat.contains(strings.get(i))) { |
| 9595 | return true; |
| 9596 | } |
| 9597 | } |
| 9598 | } |
| 9599 | return false; |
| 9600 | } |
| 9601 | } |
| 9602 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9603 | /** |
| 9604 | * There are three things that cmd can be: |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9605 | * - a flattened component name that matches an existing activity |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9606 | * - the cmd arg isn't the flattened component name of an existing activity: |
| 9607 | * dump all activity whose component contains the cmd as a substring |
| 9608 | * - A hex number of the ActivityRecord object instance. |
| 9609 | */ |
| 9610 | protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 9611 | int opti, boolean dumpAll) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9612 | ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9613 | |
| 9614 | if ("all".equals(name)) { |
| 9615 | synchronized (this) { |
| 9616 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9617 | activities.add(r1); |
| 9618 | } |
| 9619 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 9620 | } else if ("top".equals(name)) { |
| 9621 | synchronized (this) { |
| 9622 | final int N = mMainStack.mHistory.size(); |
| 9623 | if (N > 0) { |
| 9624 | activities.add((ActivityRecord)mMainStack.mHistory.get(N-1)); |
| 9625 | } |
| 9626 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9627 | } else { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9628 | ItemMatcher matcher = new ItemMatcher(); |
| 9629 | matcher.build(name); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9630 | |
| 9631 | synchronized (this) { |
| 9632 | for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) { |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9633 | if (matcher.match(r1, r1.intent.getComponent())) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9634 | activities.add(r1); |
| 9635 | } |
| 9636 | } |
| 9637 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9638 | } |
| 9639 | |
| 9640 | if (activities.size() <= 0) { |
| 9641 | return false; |
| 9642 | } |
| 9643 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9644 | String[] newArgs = new String[args.length - opti]; |
| 9645 | if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti); |
| 9646 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9647 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9648 | boolean needSep = false; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9649 | for (int i=activities.size()-1; i>=0; i--) { |
| 9650 | ActivityRecord r = (ActivityRecord)activities.get(i); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9651 | if (needSep) { |
| 9652 | pw.println(); |
| 9653 | } |
| 9654 | needSep = true; |
| 9655 | synchronized (this) { |
| 9656 | if (lastTask != r.task) { |
| 9657 | lastTask = r.task; |
| 9658 | pw.print("TASK "); pw.print(lastTask.affinity); |
| 9659 | pw.print(" id="); pw.println(lastTask.taskId); |
| 9660 | if (dumpAll) { |
| 9661 | lastTask.dump(pw, " "); |
| 9662 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9663 | } |
| 9664 | } |
| 9665 | dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9666 | } |
| 9667 | return true; |
| 9668 | } |
| 9669 | |
| 9670 | /** |
| 9671 | * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if |
| 9672 | * there is a thread associated with the activity. |
| 9673 | */ |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9674 | private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw, |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9675 | final ActivityRecord r, String[] args, boolean dumpAll) { |
| 9676 | String innerPrefix = prefix + " "; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9677 | synchronized (this) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9678 | pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName); |
| 9679 | pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 9680 | pw.print(" pid="); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 9681 | if (r.app != null) pw.println(r.app.pid); |
| 9682 | else pw.println("(not running)"); |
| 9683 | if (dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9684 | r.dump(pw, innerPrefix); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9685 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9686 | } |
| 9687 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9688 | // flush anything that is already in the PrintWriter since the thread is going |
| 9689 | // to write to the file descriptor directly |
| 9690 | pw.flush(); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9691 | try { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9692 | TransferPipe tp = new TransferPipe(); |
| 9693 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9694 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9695 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9696 | tp.go(fd); |
| 9697 | } finally { |
| 9698 | tp.kill(); |
| 9699 | } |
| 9700 | } catch (IOException e) { |
| 9701 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9702 | } catch (RemoteException e) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9703 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 9704 | } |
| 9705 | } |
| 9706 | } |
| 9707 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9708 | boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9709 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9710 | boolean needSep = false; |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9711 | boolean onlyHistory = false; |
| 9712 | |
| 9713 | if ("history".equals(dumpPackage)) { |
| 9714 | onlyHistory = true; |
| 9715 | dumpPackage = null; |
| 9716 | } |
| 9717 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9718 | pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)"); |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9719 | if (!onlyHistory && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9720 | if (mRegisteredReceivers.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9721 | boolean printed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9722 | Iterator it = mRegisteredReceivers.values().iterator(); |
| 9723 | while (it.hasNext()) { |
| 9724 | ReceiverList r = (ReceiverList)it.next(); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9725 | if (dumpPackage != null && (r.app == null || |
| 9726 | !dumpPackage.equals(r.app.info.packageName))) { |
| 9727 | continue; |
| 9728 | } |
| 9729 | if (!printed) { |
| 9730 | pw.println(" Registered Receivers:"); |
| 9731 | needSep = true; |
| 9732 | printed = true; |
| 9733 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9734 | pw.print(" * "); pw.println(r); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9735 | r.dump(pw, " "); |
| 9736 | } |
| 9737 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9738 | |
| 9739 | if (mReceiverResolver.dump(pw, needSep ? |
| 9740 | "\n Receiver Resolver Table:" : " Receiver Resolver Table:", |
| 9741 | " ", dumpPackage, false)) { |
| 9742 | needSep = true; |
| 9743 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9744 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9745 | |
| 9746 | for (BroadcastQueue q : mBroadcastQueues) { |
| 9747 | needSep = q.dumpLocked(fd, pw, args, opti, dumpAll, dumpPackage, needSep); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9748 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9749 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9750 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9751 | |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9752 | if (!onlyHistory && mStickyBroadcasts != null && dumpPackage == null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 9753 | for (int user=0; user<mStickyBroadcasts.size(); user++) { |
| 9754 | if (needSep) { |
| 9755 | pw.println(); |
| 9756 | } |
| 9757 | needSep = true; |
| 9758 | pw.print(" Sticky broadcasts for user "); |
| 9759 | pw.print(mStickyBroadcasts.keyAt(user)); pw.println(":"); |
| 9760 | StringBuilder sb = new StringBuilder(128); |
| 9761 | for (Map.Entry<String, ArrayList<Intent>> ent |
| 9762 | : mStickyBroadcasts.valueAt(user).entrySet()) { |
| 9763 | pw.print(" * Sticky action "); pw.print(ent.getKey()); |
| 9764 | if (dumpAll) { |
| 9765 | pw.println(":"); |
| 9766 | ArrayList<Intent> intents = ent.getValue(); |
| 9767 | final int N = intents.size(); |
| 9768 | for (int i=0; i<N; i++) { |
| 9769 | sb.setLength(0); |
| 9770 | sb.append(" Intent: "); |
| 9771 | intents.get(i).toShortString(sb, false, true, false, false); |
| 9772 | pw.println(sb.toString()); |
| 9773 | Bundle bundle = intents.get(i).getExtras(); |
| 9774 | if (bundle != null) { |
| 9775 | pw.print(" "); |
| 9776 | pw.println(bundle.toString()); |
| 9777 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9778 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 9779 | } else { |
| 9780 | pw.println(""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9781 | } |
| 9782 | } |
| 9783 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9784 | } |
| 9785 | |
Dianne Hackborn | 786b440 | 2012-08-27 15:14:02 -0700 | [diff] [blame] | 9786 | if (!onlyHistory && dumpAll) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9787 | pw.println(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 9788 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 9789 | pw.println(" mBroadcastsScheduled [" + queue.mQueueName + "]=" |
| 9790 | + queue.mBroadcastsScheduled); |
| 9791 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9792 | pw.println(" mHandler:"); |
| 9793 | mHandler.dump(new PrintWriterPrinter(pw), " "); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9794 | needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9795 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9796 | |
| 9797 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9798 | } |
| 9799 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9800 | boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9801 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | 9da2d40 | 2012-03-15 13:43:08 -0700 | [diff] [blame] | 9802 | boolean needSep = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9803 | |
Dianne Hackborn | 1c9b260 | 2011-08-19 14:08:43 -0700 | [diff] [blame] | 9804 | ItemMatcher matcher = new ItemMatcher(); |
| 9805 | matcher.build(args, opti); |
| 9806 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9807 | pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)"); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 9808 | |
| 9809 | mProviderMap.dumpProvidersLocked(pw, dumpAll); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9810 | |
| 9811 | if (mLaunchingProviders.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9812 | boolean printed = false; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9813 | for (int i=mLaunchingProviders.size()-1; i>=0; i--) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9814 | ContentProviderRecord r = mLaunchingProviders.get(i); |
| 9815 | if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) { |
| 9816 | continue; |
| 9817 | } |
| 9818 | if (!printed) { |
| 9819 | if (needSep) pw.println(" "); |
| 9820 | needSep = true; |
| 9821 | pw.println(" Launching content providers:"); |
| 9822 | printed = true; |
| 9823 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9824 | pw.print(" Launching #"); pw.print(i); pw.print(": "); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9825 | pw.println(r); |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9826 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9827 | } |
| 9828 | |
| 9829 | if (mGrantedUriPermissions.size() > 0) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9830 | if (needSep) pw.println(); |
| 9831 | needSep = true; |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9832 | pw.println("Granted Uri Permissions:"); |
| 9833 | for (int i=0; i<mGrantedUriPermissions.size(); i++) { |
| 9834 | int uid = mGrantedUriPermissions.keyAt(i); |
| 9835 | HashMap<Uri, UriPermission> perms |
| 9836 | = mGrantedUriPermissions.valueAt(i); |
| 9837 | pw.print(" * UID "); pw.print(uid); |
| 9838 | pw.println(" holds:"); |
| 9839 | for (UriPermission perm : perms.values()) { |
| 9840 | pw.print(" "); pw.println(perm); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9841 | if (dumpAll) { |
| 9842 | perm.dump(pw, " "); |
| 9843 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9844 | } |
| 9845 | } |
| 9846 | needSep = true; |
| 9847 | } |
| 9848 | |
| 9849 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9850 | } |
| 9851 | |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9852 | boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9853 | int opti, boolean dumpAll, String dumpPackage) { |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9854 | boolean needSep = false; |
| 9855 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9856 | if (mIntentSenderRecords.size() > 0) { |
| 9857 | boolean printed = false; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9858 | Iterator<WeakReference<PendingIntentRecord>> it |
| 9859 | = mIntentSenderRecords.values().iterator(); |
| 9860 | while (it.hasNext()) { |
| 9861 | WeakReference<PendingIntentRecord> ref = it.next(); |
| 9862 | PendingIntentRecord rec = ref != null ? ref.get(): null; |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9863 | if (dumpPackage != null && (rec == null |
| 9864 | || !dumpPackage.equals(rec.key.packageName))) { |
| 9865 | continue; |
| 9866 | } |
| 9867 | if (!printed) { |
| 9868 | pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)"); |
| 9869 | printed = true; |
| 9870 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9871 | needSep = true; |
| 9872 | if (rec != null) { |
| 9873 | pw.print(" * "); pw.println(rec); |
| 9874 | if (dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9875 | rec.dump(pw, " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9876 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9877 | } else { |
| 9878 | pw.print(" * "); pw.println(ref); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9879 | } |
| 9880 | } |
| 9881 | } |
Dianne Hackborn | c59411b | 2009-12-21 20:10:14 -0800 | [diff] [blame] | 9882 | |
| 9883 | return needSep; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9884 | } |
| 9885 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9886 | private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9887 | String prefix, String label, boolean complete, boolean brief, boolean client, |
| 9888 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9889 | TaskRecord lastTask = null; |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9890 | boolean needNL = false; |
| 9891 | final String innerPrefix = prefix + " "; |
| 9892 | final String[] args = new String[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9893 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9894 | final ActivityRecord r = (ActivityRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9895 | if (dumpPackage != null && !dumpPackage.equals(r.packageName)) { |
| 9896 | continue; |
| 9897 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 9898 | final boolean full = !brief && (complete || !r.isInHistory()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9899 | if (needNL) { |
| 9900 | pw.println(" "); |
| 9901 | needNL = false; |
| 9902 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9903 | if (lastTask != r.task) { |
| 9904 | lastTask = r.task; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9905 | pw.print(prefix); |
| 9906 | pw.print(full ? "* " : " "); |
| 9907 | pw.println(lastTask); |
| 9908 | if (full) { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9909 | lastTask.dump(pw, prefix + " "); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9910 | } else if (complete) { |
| 9911 | // Complete + brief == give a summary. Isn't that obvious?!? |
| 9912 | if (lastTask.intent != null) { |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9913 | pw.print(prefix); pw.print(" "); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 9914 | pw.println(lastTask.intent.toInsecureStringWithClip()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9915 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9917 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 9918 | pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label); |
| 9919 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 9920 | pw.println(r); |
| 9921 | if (full) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9922 | r.dump(pw, innerPrefix); |
| 9923 | } else if (complete) { |
| 9924 | // Complete + brief == give a summary. Isn't that obvious?!? |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 9925 | pw.print(innerPrefix); pw.println(r.intent.toInsecureString()); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9926 | if (r.app != null) { |
| 9927 | pw.print(innerPrefix); pw.println(r.app); |
| 9928 | } |
| 9929 | } |
| 9930 | if (client && r.app != null && r.app.thread != null) { |
| 9931 | // flush anything that is already in the PrintWriter since the thread is going |
| 9932 | // to write to the file descriptor directly |
| 9933 | pw.flush(); |
| 9934 | try { |
| 9935 | TransferPipe tp = new TransferPipe(); |
| 9936 | try { |
Dianne Hackborn | be70785 | 2011-11-11 14:32:10 -0800 | [diff] [blame] | 9937 | r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(), |
| 9938 | r.appToken, innerPrefix, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 9939 | // Short timeout, since blocking here can |
| 9940 | // deadlock with the application. |
| 9941 | tp.go(fd, 2000); |
| 9942 | } finally { |
| 9943 | tp.kill(); |
| 9944 | } |
| 9945 | } catch (IOException e) { |
| 9946 | pw.println(innerPrefix + "Failure while dumping the activity: " + e); |
| 9947 | } catch (RemoteException e) { |
| 9948 | pw.println(innerPrefix + "Got a RemoteException while dumping the activity"); |
| 9949 | } |
| 9950 | needNL = true; |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 9951 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9952 | } |
| 9953 | } |
| 9954 | |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 9955 | private static String buildOomTag(String prefix, String space, int val, int base) { |
| 9956 | if (val == base) { |
| 9957 | if (space == null) return prefix; |
| 9958 | return prefix + " "; |
| 9959 | } |
| 9960 | return prefix + "+" + Integer.toString(val-base); |
| 9961 | } |
| 9962 | |
| 9963 | private static final int dumpProcessList(PrintWriter pw, |
| 9964 | ActivityManagerService service, List list, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9965 | String prefix, String normalLabel, String persistentLabel, |
| 9966 | String dumpPackage) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9967 | int numPers = 0; |
Dianne Hackborn | 32907cf | 2010-06-10 17:50:20 -0700 | [diff] [blame] | 9968 | final int N = list.size()-1; |
| 9969 | for (int i=N; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9970 | ProcessRecord r = (ProcessRecord)list.get(i); |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9971 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9972 | continue; |
| 9973 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9974 | pw.println(String.format("%s%s #%2d: %s", |
| 9975 | prefix, (r.persistent ? persistentLabel : normalLabel), |
| 9976 | i, r.toString())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9977 | if (r.persistent) { |
| 9978 | numPers++; |
| 9979 | } |
| 9980 | } |
| 9981 | return numPers; |
| 9982 | } |
| 9983 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9984 | private static final boolean dumpProcessOomList(PrintWriter pw, |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9985 | ActivityManagerService service, List<ProcessRecord> origList, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9986 | String prefix, String normalLabel, String persistentLabel, |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9987 | boolean inclDetails, String dumpPackage) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 9988 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9989 | ArrayList<Pair<ProcessRecord, Integer>> list |
| 9990 | = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size()); |
| 9991 | for (int i=0; i<origList.size(); i++) { |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9992 | ProcessRecord r = origList.get(i); |
| 9993 | if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) { |
| 9994 | continue; |
| 9995 | } |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 9996 | list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i)); |
| 9997 | } |
| 9998 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 9999 | if (list.size() <= 0) { |
| 10000 | return false; |
| 10001 | } |
| 10002 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 10003 | Comparator<Pair<ProcessRecord, Integer>> comparator |
| 10004 | = new Comparator<Pair<ProcessRecord, Integer>>() { |
| 10005 | @Override |
| 10006 | public int compare(Pair<ProcessRecord, Integer> object1, |
| 10007 | Pair<ProcessRecord, Integer> object2) { |
| 10008 | if (object1.first.setAdj != object2.first.setAdj) { |
| 10009 | return object1.first.setAdj > object2.first.setAdj ? -1 : 1; |
| 10010 | } |
| 10011 | if (object1.second.intValue() != object2.second.intValue()) { |
| 10012 | return object1.second.intValue() > object2.second.intValue() ? -1 : 1; |
| 10013 | } |
| 10014 | return 0; |
| 10015 | } |
| 10016 | }; |
| 10017 | |
| 10018 | Collections.sort(list, comparator); |
| 10019 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10020 | final long curRealtime = SystemClock.elapsedRealtime(); |
| 10021 | final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime; |
| 10022 | final long curUptime = SystemClock.uptimeMillis(); |
| 10023 | final long uptimeSince = curUptime - service.mLastPowerCheckUptime; |
| 10024 | |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10025 | for (int i=list.size()-1; i>=0; i--) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 10026 | ProcessRecord r = list.get(i).first; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10027 | String oomAdj; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10028 | if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10029 | oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10030 | } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) { |
| 10031 | oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ); |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10032 | } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) { |
| 10033 | oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10034 | } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) { |
| 10035 | oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10036 | } else if (r.setAdj >= ProcessList.SERVICE_ADJ) { |
| 10037 | oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10038 | } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10039 | oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10040 | } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 10041 | oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ); |
| 10042 | } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 10043 | oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ); |
| 10044 | } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 10045 | oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ); |
| 10046 | } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10047 | oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ); |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 10048 | } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) { |
| 10049 | oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 10050 | } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) { |
| 10051 | oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10052 | } else { |
| 10053 | oomAdj = Integer.toString(r.setAdj); |
| 10054 | } |
| 10055 | String schedGroup; |
| 10056 | switch (r.setSchedGroup) { |
| 10057 | case Process.THREAD_GROUP_BG_NONINTERACTIVE: |
| 10058 | schedGroup = "B"; |
| 10059 | break; |
| 10060 | case Process.THREAD_GROUP_DEFAULT: |
| 10061 | schedGroup = "F"; |
| 10062 | break; |
| 10063 | default: |
| 10064 | schedGroup = Integer.toString(r.setSchedGroup); |
| 10065 | break; |
| 10066 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10067 | String foreground; |
| 10068 | if (r.foregroundActivities) { |
| 10069 | foreground = "A"; |
| 10070 | } else if (r.foregroundServices) { |
| 10071 | foreground = "S"; |
| 10072 | } else { |
| 10073 | foreground = " "; |
| 10074 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 10075 | 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] | 10076 | prefix, (r.persistent ? persistentLabel : normalLabel), |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10077 | (origList.size()-1)-list.get(i).second, oomAdj, schedGroup, |
| 10078 | foreground, r.trimMemoryLevel, r.toShortString(), r.adjType)); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10079 | if (r.adjSource != null || r.adjTarget != null) { |
| 10080 | pw.print(prefix); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10081 | pw.print(" "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10082 | if (r.adjTarget instanceof ComponentName) { |
| 10083 | pw.print(((ComponentName)r.adjTarget).flattenToShortString()); |
| 10084 | } else if (r.adjTarget != null) { |
| 10085 | pw.print(r.adjTarget.toString()); |
| 10086 | } else { |
| 10087 | pw.print("{null}"); |
| 10088 | } |
| 10089 | pw.print("<="); |
| 10090 | if (r.adjSource instanceof ProcessRecord) { |
| 10091 | pw.print("Proc{"); |
| 10092 | pw.print(((ProcessRecord)r.adjSource).toShortString()); |
| 10093 | pw.println("}"); |
| 10094 | } else if (r.adjSource != null) { |
| 10095 | pw.println(r.adjSource.toString()); |
| 10096 | } else { |
| 10097 | pw.println("{null}"); |
| 10098 | } |
| 10099 | } |
| 10100 | if (inclDetails) { |
| 10101 | pw.print(prefix); |
| 10102 | pw.print(" "); |
| 10103 | pw.print("oom: max="); pw.print(r.maxAdj); |
| 10104 | pw.print(" hidden="); pw.print(r.hiddenAdj); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 10105 | pw.print(" client="); pw.print(r.clientHiddenAdj); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 10106 | pw.print(" empty="); pw.print(r.emptyAdj); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10107 | pw.print(" curRaw="); pw.print(r.curRawAdj); |
| 10108 | pw.print(" setRaw="); pw.print(r.setRawAdj); |
| 10109 | pw.print(" cur="); pw.print(r.curAdj); |
| 10110 | pw.print(" set="); pw.println(r.setAdj); |
| 10111 | pw.print(prefix); |
| 10112 | pw.print(" "); |
| 10113 | pw.print("keeping="); pw.print(r.keeping); |
| 10114 | pw.print(" hidden="); pw.print(r.hidden); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10115 | pw.print(" empty="); pw.print(r.empty); |
| 10116 | pw.print(" hasAboveClient="); pw.println(r.hasAboveClient); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10117 | |
| 10118 | if (!r.keeping) { |
| 10119 | if (r.lastWakeTime != 0) { |
| 10120 | long wtime; |
| 10121 | BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics(); |
| 10122 | synchronized (stats) { |
| 10123 | wtime = stats.getProcessWakeTime(r.info.uid, |
| 10124 | r.pid, curRealtime); |
| 10125 | } |
| 10126 | long timeUsed = wtime - r.lastWakeTime; |
| 10127 | pw.print(prefix); |
| 10128 | pw.print(" "); |
| 10129 | pw.print("keep awake over "); |
| 10130 | TimeUtils.formatDuration(realtimeSince, pw); |
| 10131 | pw.print(" used "); |
| 10132 | TimeUtils.formatDuration(timeUsed, pw); |
| 10133 | pw.print(" ("); |
| 10134 | pw.print((timeUsed*100)/realtimeSince); |
| 10135 | pw.println("%)"); |
| 10136 | } |
| 10137 | if (r.lastCpuTime != 0) { |
| 10138 | long timeUsed = r.curCpuTime - r.lastCpuTime; |
| 10139 | pw.print(prefix); |
| 10140 | pw.print(" "); |
| 10141 | pw.print("run cpu over "); |
| 10142 | TimeUtils.formatDuration(uptimeSince, pw); |
| 10143 | pw.print(" used "); |
| 10144 | TimeUtils.formatDuration(timeUsed, pw); |
| 10145 | pw.print(" ("); |
| 10146 | pw.print((timeUsed*100)/uptimeSince); |
| 10147 | pw.println("%)"); |
| 10148 | } |
| 10149 | } |
| 10150 | } |
| 10151 | } |
Dianne Hackborn | ee9aef0 | 2011-11-16 13:21:46 -0800 | [diff] [blame] | 10152 | return true; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 10153 | } |
| 10154 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10155 | ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10156 | ArrayList<ProcessRecord> procs; |
| 10157 | synchronized (this) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10158 | if (args != null && args.length > start |
| 10159 | && args[start].charAt(0) != '-') { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10160 | procs = new ArrayList<ProcessRecord>(); |
| 10161 | int pid = -1; |
| 10162 | try { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10163 | pid = Integer.parseInt(args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10164 | } catch (NumberFormatException e) { |
| 10165 | |
| 10166 | } |
| 10167 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 10168 | ProcessRecord proc = mLruProcesses.get(i); |
| 10169 | if (proc.pid == pid) { |
| 10170 | procs.add(proc); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10171 | } else if (proc.processName.equals(args[start])) { |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10172 | procs.add(proc); |
| 10173 | } |
| 10174 | } |
| 10175 | if (procs.size() <= 0) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10176 | pw.println("No process found for: " + args[start]); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10177 | return null; |
| 10178 | } |
| 10179 | } else { |
| 10180 | procs = new ArrayList<ProcessRecord>(mLruProcesses); |
| 10181 | } |
| 10182 | } |
| 10183 | return procs; |
| 10184 | } |
| 10185 | |
| 10186 | final void dumpGraphicsHardwareUsage(FileDescriptor fd, |
| 10187 | PrintWriter pw, String[] args) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10188 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10189 | if (procs == null) { |
| 10190 | return; |
| 10191 | } |
| 10192 | |
| 10193 | long uptime = SystemClock.uptimeMillis(); |
| 10194 | long realtime = SystemClock.elapsedRealtime(); |
| 10195 | pw.println("Applications Graphics Acceleration Info:"); |
| 10196 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 10197 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10198 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10199 | ProcessRecord r = procs.get(i); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10200 | if (r.thread != null) { |
| 10201 | pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **"); |
| 10202 | pw.flush(); |
| 10203 | try { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10204 | TransferPipe tp = new TransferPipe(); |
| 10205 | try { |
| 10206 | r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 10207 | tp.go(fd); |
| 10208 | } finally { |
| 10209 | tp.kill(); |
| 10210 | } |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10211 | } catch (IOException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10212 | pw.println("Failure while dumping the app: " + r); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10213 | pw.flush(); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10214 | } catch (RemoteException e) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10215 | pw.println("Got a RemoteException while dumping the app " + r); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10216 | pw.flush(); |
| 10217 | } |
| 10218 | } |
| 10219 | } |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 10220 | } |
| 10221 | |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 10222 | final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 10223 | ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args); |
| 10224 | if (procs == null) { |
| 10225 | return; |
| 10226 | } |
| 10227 | |
| 10228 | pw.println("Applications Database Info:"); |
| 10229 | |
| 10230 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10231 | ProcessRecord r = procs.get(i); |
| 10232 | if (r.thread != null) { |
| 10233 | pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **"); |
| 10234 | pw.flush(); |
| 10235 | try { |
| 10236 | TransferPipe tp = new TransferPipe(); |
| 10237 | try { |
| 10238 | r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args); |
| 10239 | tp.go(fd); |
| 10240 | } finally { |
| 10241 | tp.kill(); |
| 10242 | } |
| 10243 | } catch (IOException e) { |
| 10244 | pw.println("Failure while dumping the app: " + r); |
| 10245 | pw.flush(); |
| 10246 | } catch (RemoteException e) { |
| 10247 | pw.println("Got a RemoteException while dumping the app " + r); |
| 10248 | pw.flush(); |
| 10249 | } |
| 10250 | } |
| 10251 | } |
| 10252 | } |
| 10253 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10254 | final static class MemItem { |
| 10255 | final String label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10256 | final String shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10257 | final long pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10258 | final int id; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10259 | ArrayList<MemItem> subitems; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10260 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10261 | public MemItem(String _label, String _shortLabel, long _pss, int _id) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10262 | label = _label; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10263 | shortLabel = _shortLabel; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10264 | pss = _pss; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10265 | id = _id; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10266 | } |
| 10267 | } |
| 10268 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10269 | static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10270 | boolean sort) { |
| 10271 | if (sort) { |
| 10272 | Collections.sort(items, new Comparator<MemItem>() { |
| 10273 | @Override |
| 10274 | public int compare(MemItem lhs, MemItem rhs) { |
| 10275 | if (lhs.pss < rhs.pss) { |
| 10276 | return 1; |
| 10277 | } else if (lhs.pss > rhs.pss) { |
| 10278 | return -1; |
| 10279 | } |
| 10280 | return 0; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10281 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10282 | }); |
| 10283 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10284 | |
| 10285 | for (int i=0; i<items.size(); i++) { |
| 10286 | MemItem mi = items.get(i); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10287 | 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] | 10288 | if (mi.subitems != null) { |
| 10289 | dumpMemItems(pw, prefix + " ", mi.subitems, true); |
| 10290 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10291 | } |
| 10292 | } |
| 10293 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10294 | // These are in KB. |
| 10295 | static final long[] DUMP_MEM_BUCKETS = new long[] { |
| 10296 | 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024, |
| 10297 | 120*1024, 160*1024, 200*1024, |
| 10298 | 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024, |
| 10299 | 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024 |
| 10300 | }; |
| 10301 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10302 | static final void appendMemBucket(StringBuilder out, long memKB, String label, |
| 10303 | boolean stackLike) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10304 | int start = label.lastIndexOf('.'); |
| 10305 | if (start >= 0) start++; |
| 10306 | else start = 0; |
| 10307 | int end = label.length(); |
| 10308 | for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) { |
| 10309 | if (DUMP_MEM_BUCKETS[i] >= memKB) { |
| 10310 | long bucket = DUMP_MEM_BUCKETS[i]/1024; |
| 10311 | out.append(bucket); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10312 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10313 | out.append(label, start, end); |
| 10314 | return; |
| 10315 | } |
| 10316 | } |
| 10317 | out.append(memKB/1024); |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10318 | out.append(stackLike ? "MB." : "MB "); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10319 | out.append(label, start, end); |
| 10320 | } |
| 10321 | |
| 10322 | static final int[] DUMP_MEM_OOM_ADJ = new int[] { |
| 10323 | ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ, |
| 10324 | ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ, |
| 10325 | ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ, |
| 10326 | ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ |
| 10327 | }; |
| 10328 | static final String[] DUMP_MEM_OOM_LABEL = new String[] { |
| 10329 | "System", "Persistent", "Foreground", |
| 10330 | "Visible", "Perceptible", "Heavy Weight", |
| 10331 | "Backup", "A Services", "Home", "Previous", |
| 10332 | "B Services", "Background" |
| 10333 | }; |
| 10334 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10335 | final void dumpApplicationMemoryUsage(FileDescriptor fd, |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10336 | PrintWriter pw, String prefix, String[] args, boolean brief, |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10337 | PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10338 | boolean dumpAll = false; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10339 | boolean oomOnly = false; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10340 | |
| 10341 | int opti = 0; |
| 10342 | while (opti < args.length) { |
| 10343 | String opt = args[opti]; |
| 10344 | if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') { |
| 10345 | break; |
| 10346 | } |
| 10347 | opti++; |
| 10348 | if ("-a".equals(opt)) { |
| 10349 | dumpAll = true; |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10350 | } else if ("--oom".equals(opt)) { |
| 10351 | oomOnly = true; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10352 | } else if ("-h".equals(opt)) { |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10353 | pw.println("meminfo dump options: [-a] [--oom] [process]"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10354 | pw.println(" -a: include all available information for each process."); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10355 | pw.println(" --oom: only show processes organized by oom adj."); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10356 | pw.println("If [process] is specified it can be the name or "); |
| 10357 | pw.println("pid of a specific process to dump."); |
| 10358 | return; |
| 10359 | } else { |
| 10360 | pw.println("Unknown argument: " + opt + "; use -h for help"); |
| 10361 | } |
| 10362 | } |
| 10363 | |
| 10364 | ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args); |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10365 | if (procs == null) { |
| 10366 | return; |
| 10367 | } |
| 10368 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 10369 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10370 | long uptime = SystemClock.uptimeMillis(); |
| 10371 | long realtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10372 | |
| 10373 | if (procs.size() == 1 || isCheckinRequest) { |
| 10374 | dumpAll = true; |
| 10375 | } |
| 10376 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10377 | if (isCheckinRequest) { |
| 10378 | // short checkin version |
| 10379 | pw.println(uptime + "," + realtime); |
| 10380 | pw.flush(); |
| 10381 | } else { |
| 10382 | pw.println("Applications Memory Usage (kB):"); |
| 10383 | pw.println("Uptime: " + uptime + " Realtime: " + realtime); |
| 10384 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10385 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10386 | String[] innerArgs = new String[args.length-opti]; |
| 10387 | System.arraycopy(args, opti, innerArgs, 0, args.length-opti); |
| 10388 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10389 | ArrayList<MemItem> procMems = new ArrayList<MemItem>(); |
| 10390 | long nativePss=0, dalvikPss=0, otherPss=0; |
| 10391 | long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS]; |
| 10392 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10393 | long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length]; |
| 10394 | ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[]) |
| 10395 | new ArrayList[DUMP_MEM_OOM_LABEL.length]; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10396 | |
| 10397 | long totalPss = 0; |
| 10398 | |
Dianne Hackborn | e17aeb3 | 2011-04-07 15:11:57 -0700 | [diff] [blame] | 10399 | for (int i = procs.size() - 1 ; i >= 0 ; i--) { |
| 10400 | ProcessRecord r = procs.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10401 | if (r.thread != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10402 | if (!isCheckinRequest && dumpAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10403 | pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **"); |
| 10404 | pw.flush(); |
| 10405 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10406 | Debug.MemoryInfo mi = null; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10407 | if (dumpAll) { |
| 10408 | try { |
| 10409 | mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs); |
| 10410 | } catch (RemoteException e) { |
| 10411 | if (!isCheckinRequest) { |
| 10412 | pw.println("Got RemoteException!"); |
| 10413 | pw.flush(); |
| 10414 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10415 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10416 | } else { |
| 10417 | mi = new Debug.MemoryInfo(); |
| 10418 | Debug.getMemoryInfo(r.pid, mi); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10419 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10420 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10421 | if (!isCheckinRequest && mi != null) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10422 | long myTotalPss = mi.getTotalPss(); |
| 10423 | totalPss += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10424 | MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")", |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10425 | r.processName, myTotalPss, 0); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10426 | procMems.add(pssItem); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10427 | |
| 10428 | nativePss += mi.nativePss; |
| 10429 | dalvikPss += mi.dalvikPss; |
| 10430 | otherPss += mi.otherPss; |
| 10431 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
| 10432 | long mem = mi.getOtherPss(j); |
| 10433 | miscPss[j] += mem; |
| 10434 | otherPss -= mem; |
| 10435 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10436 | |
| 10437 | for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10438 | if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex] |
| 10439 | || oomIndex == (oomPss.length-1)) { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10440 | oomPss[oomIndex] += myTotalPss; |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10441 | if (oomProcs[oomIndex] == null) { |
| 10442 | oomProcs[oomIndex] = new ArrayList<MemItem>(); |
| 10443 | } |
| 10444 | oomProcs[oomIndex].add(pssItem); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10445 | break; |
| 10446 | } |
| 10447 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10448 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10449 | } |
| 10450 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10451 | |
| 10452 | if (!isCheckinRequest && procs.size() > 1) { |
| 10453 | ArrayList<MemItem> catMems = new ArrayList<MemItem>(); |
| 10454 | |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10455 | catMems.add(new MemItem("Native", "Native", nativePss, -1)); |
| 10456 | catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2)); |
| 10457 | catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10458 | for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10459 | String label = Debug.MemoryInfo.getOtherLabel(j); |
| 10460 | catMems.add(new MemItem(label, label, miscPss[j], j)); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10461 | } |
| 10462 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10463 | ArrayList<MemItem> oomMems = new ArrayList<MemItem>(); |
| 10464 | for (int j=0; j<oomPss.length; j++) { |
| 10465 | if (oomPss[j] != 0) { |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10466 | String label = DUMP_MEM_OOM_LABEL[j]; |
| 10467 | MemItem item = new MemItem(label, label, oomPss[j], |
| 10468 | DUMP_MEM_OOM_ADJ[j]); |
Dianne Hackborn | a4bacb8 | 2011-08-24 15:12:38 -0700 | [diff] [blame] | 10469 | item.subitems = oomProcs[j]; |
| 10470 | oomMems.add(item); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10471 | } |
| 10472 | } |
| 10473 | |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10474 | if (outTag != null || outStack != null) { |
| 10475 | if (outTag != null) { |
| 10476 | appendMemBucket(outTag, totalPss, "total", false); |
| 10477 | } |
| 10478 | if (outStack != null) { |
| 10479 | appendMemBucket(outStack, totalPss, "total", true); |
| 10480 | } |
| 10481 | boolean firstLine = true; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10482 | for (int i=0; i<oomMems.size(); i++) { |
| 10483 | MemItem miCat = oomMems.get(i); |
| 10484 | if (miCat.subitems == null || miCat.subitems.size() < 1) { |
| 10485 | continue; |
| 10486 | } |
| 10487 | if (miCat.id < ProcessList.SERVICE_ADJ |
| 10488 | || miCat.id == ProcessList.HOME_APP_ADJ |
| 10489 | || miCat.id == ProcessList.PREVIOUS_APP_ADJ) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10490 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10491 | outTag.append(" / "); |
| 10492 | } |
| 10493 | if (outStack != null) { |
| 10494 | if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10495 | if (firstLine) { |
| 10496 | outStack.append(":"); |
| 10497 | firstLine = false; |
| 10498 | } |
| 10499 | outStack.append("\n\t at "); |
| 10500 | } else { |
| 10501 | outStack.append("$"); |
| 10502 | } |
| 10503 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10504 | for (int j=0; j<miCat.subitems.size(); j++) { |
| 10505 | MemItem mi = miCat.subitems.get(j); |
| 10506 | if (j > 0) { |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10507 | if (outTag != null) { |
| 10508 | outTag.append(" "); |
| 10509 | } |
| 10510 | if (outStack != null) { |
| 10511 | outStack.append("$"); |
| 10512 | } |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10513 | } |
Dianne Hackborn | 672342c | 2011-11-29 11:29:02 -0800 | [diff] [blame] | 10514 | if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) { |
| 10515 | appendMemBucket(outTag, mi.pss, mi.shortLabel, false); |
| 10516 | } |
| 10517 | if (outStack != null) { |
| 10518 | appendMemBucket(outStack, mi.pss, mi.shortLabel, true); |
| 10519 | } |
| 10520 | } |
| 10521 | if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) { |
| 10522 | outStack.append("("); |
| 10523 | for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) { |
| 10524 | if (DUMP_MEM_OOM_ADJ[k] == miCat.id) { |
| 10525 | outStack.append(DUMP_MEM_OOM_LABEL[k]); |
| 10526 | outStack.append(":"); |
| 10527 | outStack.append(DUMP_MEM_OOM_ADJ[k]); |
| 10528 | } |
| 10529 | } |
| 10530 | outStack.append(")"); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10531 | } |
| 10532 | } |
| 10533 | } |
| 10534 | } |
| 10535 | |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10536 | if (!brief && !oomOnly) { |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10537 | pw.println(); |
| 10538 | pw.println("Total PSS by process:"); |
| 10539 | dumpMemItems(pw, " ", procMems, true); |
| 10540 | pw.println(); |
| 10541 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10542 | pw.println("Total PSS by OOM adjustment:"); |
| 10543 | dumpMemItems(pw, " ", oomMems, false); |
Dianne Hackborn | e4d4fbc | 2011-11-08 11:53:28 -0800 | [diff] [blame] | 10544 | if (!oomOnly) { |
| 10545 | PrintWriter out = categoryPw != null ? categoryPw : pw; |
| 10546 | out.println(); |
| 10547 | out.println("Total PSS by category:"); |
| 10548 | dumpMemItems(out, " ", catMems, true); |
Dianne Hackborn | 04d6db3 | 2011-11-04 20:07:24 -0700 | [diff] [blame] | 10549 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 10550 | pw.println(); |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 10551 | pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB"); |
Dianne Hackborn | d3e677b | 2012-04-05 14:58:18 -0700 | [diff] [blame] | 10552 | final int[] SINGLE_LONG_FORMAT = new int[] { |
| 10553 | Process.PROC_SPACE_TERM|Process.PROC_OUT_LONG |
| 10554 | }; |
| 10555 | long[] longOut = new long[1]; |
| 10556 | Process.readProcFile("/sys/kernel/mm/ksm/pages_shared", |
| 10557 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10558 | long shared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10559 | longOut[0] = 0; |
| 10560 | Process.readProcFile("/sys/kernel/mm/ksm/pages_sharing", |
| 10561 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10562 | long sharing = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10563 | longOut[0] = 0; |
| 10564 | Process.readProcFile("/sys/kernel/mm/ksm/pages_unshared", |
| 10565 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10566 | long unshared = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10567 | longOut[0] = 0; |
| 10568 | Process.readProcFile("/sys/kernel/mm/ksm/pages_volatile", |
| 10569 | SINGLE_LONG_FORMAT, null, longOut, null); |
| 10570 | long voltile = longOut[0] * ProcessList.PAGE_SIZE / 1024; |
| 10571 | pw.print(" KSM: "); pw.print(sharing); pw.print(" kB saved from shared "); |
| 10572 | pw.print(shared); pw.println(" kB"); |
| 10573 | pw.print(" "); pw.print(unshared); pw.print(" kB unshared; "); |
| 10574 | pw.print(voltile); pw.println(" kB volatile"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 10575 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10576 | } |
| 10577 | |
| 10578 | /** |
| 10579 | * Searches array of arguments for the specified string |
| 10580 | * @param args array of argument strings |
| 10581 | * @param value value to search for |
| 10582 | * @return true if the value is contained in the array |
| 10583 | */ |
| 10584 | private static boolean scanArgs(String[] args, String value) { |
| 10585 | if (args != null) { |
| 10586 | for (String arg : args) { |
| 10587 | if (value.equals(arg)) { |
| 10588 | return true; |
| 10589 | } |
| 10590 | } |
| 10591 | } |
| 10592 | return false; |
| 10593 | } |
| 10594 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10595 | private final boolean removeDyingProviderLocked(ProcessRecord proc, |
| 10596 | ContentProviderRecord cpr, boolean always) { |
| 10597 | final boolean inLaunching = mLaunchingProviders.contains(cpr); |
| 10598 | |
| 10599 | if (!inLaunching || always) { |
| 10600 | synchronized (cpr) { |
| 10601 | cpr.launchingApp = null; |
| 10602 | cpr.notifyAll(); |
| 10603 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10604 | mProviderMap.removeProviderByClass(cpr.name, UserHandle.getUserId(cpr.uid)); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10605 | String names[] = cpr.info.authority.split(";"); |
| 10606 | for (int j = 0; j < names.length; j++) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 10607 | mProviderMap.removeProviderByName(names[j], UserHandle.getUserId(cpr.uid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10608 | } |
| 10609 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10610 | |
| 10611 | for (int i=0; i<cpr.connections.size(); i++) { |
| 10612 | ContentProviderConnection conn = cpr.connections.get(i); |
| 10613 | if (conn.waiting) { |
| 10614 | // If this connection is waiting for the provider, then we don't |
| 10615 | // need to mess with its process unless we are always removing |
| 10616 | // or for some reason the provider is not currently launching. |
| 10617 | if (inLaunching && !always) { |
| 10618 | continue; |
| 10619 | } |
| 10620 | } |
| 10621 | ProcessRecord capp = conn.client; |
| 10622 | conn.dead = true; |
| 10623 | if (conn.stableCount > 0) { |
| 10624 | if (!capp.persistent && capp.thread != null |
| 10625 | && capp.pid != 0 |
| 10626 | && capp.pid != MY_PID) { |
| 10627 | Slog.i(TAG, "Kill " + capp.processName |
| 10628 | + " (pid " + capp.pid + "): provider " + cpr.info.name |
| 10629 | + " in dying process " + (proc != null ? proc.processName : "??")); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 10630 | EventLog.writeEvent(EventLogTags.AM_KILL, capp.userId, capp.pid, |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10631 | capp.processName, capp.setAdj, "dying provider " |
| 10632 | + cpr.name.toShortString()); |
| 10633 | Process.killProcessQuiet(capp.pid); |
| 10634 | } |
| 10635 | } else if (capp.thread != null && conn.provider.provider != null) { |
| 10636 | try { |
| 10637 | capp.thread.unstableProviderDied(conn.provider.provider.asBinder()); |
| 10638 | } catch (RemoteException e) { |
| 10639 | } |
| 10640 | // In the protocol here, we don't expect the client to correctly |
| 10641 | // clean up this connection, we'll just remove it. |
| 10642 | cpr.connections.remove(i); |
| 10643 | conn.client.conProviders.remove(conn); |
| 10644 | } |
| 10645 | } |
| 10646 | |
| 10647 | if (inLaunching && always) { |
| 10648 | mLaunchingProviders.remove(cpr); |
| 10649 | } |
| 10650 | return inLaunching; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10651 | } |
| 10652 | |
| 10653 | /** |
| 10654 | * Main code for cleaning up a process when it has gone away. This is |
| 10655 | * called both as a result of the process dying, or directly when stopping |
| 10656 | * a process when running in single process mode. |
| 10657 | */ |
| 10658 | private final void cleanUpApplicationRecordLocked(ProcessRecord app, |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 10659 | boolean restarting, boolean allowRestart, int index) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10660 | if (index >= 0) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 10661 | mLruProcesses.remove(index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10662 | } |
| 10663 | |
Dianne Hackborn | 3612487 | 2009-10-08 16:22:03 -0700 | [diff] [blame] | 10664 | mProcessesToGc.remove(app); |
| 10665 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10666 | // Dismiss any open dialogs. |
| 10667 | if (app.crashDialog != null) { |
| 10668 | app.crashDialog.dismiss(); |
| 10669 | app.crashDialog = null; |
| 10670 | } |
| 10671 | if (app.anrDialog != null) { |
| 10672 | app.anrDialog.dismiss(); |
| 10673 | app.anrDialog = null; |
| 10674 | } |
| 10675 | if (app.waitDialog != null) { |
| 10676 | app.waitDialog.dismiss(); |
| 10677 | app.waitDialog = null; |
| 10678 | } |
| 10679 | |
| 10680 | app.crashing = false; |
| 10681 | app.notResponding = false; |
| 10682 | |
| 10683 | app.resetPackageList(); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 10684 | app.unlinkDeathRecipient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10685 | app.thread = null; |
| 10686 | app.forcingToForeground = null; |
| 10687 | app.foregroundServices = false; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10688 | app.foregroundActivities = false; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 10689 | app.hasShownUi = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 10690 | app.hasAboveClient = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10691 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10692 | mServices.killServicesLocked(app, allowRestart); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10693 | |
| 10694 | boolean restart = false; |
| 10695 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10696 | // Remove published content providers. |
| 10697 | if (!app.pubProviders.isEmpty()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10698 | Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10699 | while (it.hasNext()) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10700 | ContentProviderRecord cpr = it.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10701 | |
| 10702 | final boolean always = app.bad || !allowRestart; |
| 10703 | if (removeDyingProviderLocked(app, cpr, always) || always) { |
| 10704 | // We left the provider in the launching list, need to |
| 10705 | // restart it. |
| 10706 | restart = true; |
| 10707 | } |
| 10708 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10709 | cpr.provider = null; |
Dianne Hackborn | 2a6bcda | 2011-09-21 15:07:05 -0700 | [diff] [blame] | 10710 | cpr.proc = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10711 | } |
| 10712 | app.pubProviders.clear(); |
| 10713 | } |
| 10714 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10715 | // Take care of any launching providers waiting for this process. |
| 10716 | if (checkAppInLaunchingProvidersLocked(app, false)) { |
| 10717 | restart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10718 | } |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10719 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10720 | // Unregister from connected content providers. |
| 10721 | if (!app.conProviders.isEmpty()) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10722 | for (int i=0; i<app.conProviders.size(); i++) { |
| 10723 | ContentProviderConnection conn = app.conProviders.get(i); |
| 10724 | conn.provider.connections.remove(conn); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10725 | } |
| 10726 | app.conProviders.clear(); |
| 10727 | } |
| 10728 | |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10729 | // At this point there may be remaining entries in mLaunchingProviders |
| 10730 | // where we were the only one waiting, so they are no longer of use. |
| 10731 | // Look for these and clean up if found. |
| 10732 | // XXX Commented out for now. Trying to figure out a way to reproduce |
| 10733 | // the actual situation to identify what is actually going on. |
| 10734 | if (false) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10735 | for (int i=0; i<mLaunchingProviders.size(); i++) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10736 | ContentProviderRecord cpr = (ContentProviderRecord) |
| 10737 | mLaunchingProviders.get(i); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10738 | if (cpr.connections.size() <= 0 && !cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 10739 | synchronized (cpr) { |
| 10740 | cpr.launchingApp = null; |
| 10741 | cpr.notifyAll(); |
| 10742 | } |
| 10743 | } |
| 10744 | } |
| 10745 | } |
| 10746 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10747 | skipCurrentReceiverLocked(app); |
| 10748 | |
| 10749 | // Unregister any receivers. |
| 10750 | if (app.receivers.size() > 0) { |
| 10751 | Iterator<ReceiverList> it = app.receivers.iterator(); |
| 10752 | while (it.hasNext()) { |
| 10753 | removeReceiverLocked(it.next()); |
| 10754 | } |
| 10755 | app.receivers.clear(); |
| 10756 | } |
| 10757 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10758 | // If the app is undergoing backup, tell the backup manager about it |
| 10759 | if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10760 | if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 10761 | try { |
| 10762 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 10763 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 10764 | bm.agentDisconnected(app.info.packageName); |
| 10765 | } catch (RemoteException e) { |
| 10766 | // can't happen; backup manager is local |
| 10767 | } |
| 10768 | } |
| 10769 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 10770 | for (int i = mPendingProcessChanges.size()-1; i>=0; i--) { |
| 10771 | ProcessChangeItem item = mPendingProcessChanges.get(i); |
| 10772 | if (item.pid == app.pid) { |
| 10773 | mPendingProcessChanges.remove(i); |
| 10774 | mAvailProcessChanges.add(item); |
| 10775 | } |
| 10776 | } |
Jeff Sharkey | 287bd83 | 2011-05-28 19:36:26 -0700 | [diff] [blame] | 10777 | mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget(); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 10778 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10779 | // If the caller is restarting this app, then leave it in its |
| 10780 | // current lists and let the caller take care of it. |
| 10781 | if (restarting) { |
| 10782 | return; |
| 10783 | } |
| 10784 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10785 | if (!app.persistent || app.isolated) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 10786 | if (DEBUG_PROCESSES) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10787 | "Removing non-persistent process during cleanup: " + app); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10788 | mProcessNames.remove(app.processName, app.uid); |
| 10789 | mIsolatedProcesses.remove(app.uid); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10790 | if (mHeavyWeightProcess == app) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10791 | mHandler.sendMessage(mHandler.obtainMessage(CANCEL_HEAVY_NOTIFICATION_MSG, |
| 10792 | mHeavyWeightProcess.userId, 0)); |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10793 | mHeavyWeightProcess = null; |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10794 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10795 | } else if (!app.removed) { |
| 10796 | // This app is persistent, so we need to keep its record around. |
| 10797 | // If it is not already on the pending app list, add it there |
| 10798 | // and start a new process for it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10799 | if (mPersistentStartingProcesses.indexOf(app) < 0) { |
| 10800 | mPersistentStartingProcesses.add(app); |
| 10801 | restart = true; |
| 10802 | } |
| 10803 | } |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 10804 | if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG, |
| 10805 | "Clean-up removing on hold: " + app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10806 | mProcessesOnHold.remove(app); |
| 10807 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 10808 | if (app == mHomeProcess) { |
| 10809 | mHomeProcess = null; |
| 10810 | } |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 10811 | if (app == mPreviousProcess) { |
| 10812 | mPreviousProcess = null; |
| 10813 | } |
| 10814 | |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10815 | if (restart && !app.isolated) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10816 | // We have components that still need to be running in the |
| 10817 | // process, so re-launch it. |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 10818 | mProcessNames.put(app.processName, app.uid, app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10819 | startProcessLocked(app, "restart", app.processName); |
| 10820 | } else if (app.pid > 0 && app.pid != MY_PID) { |
| 10821 | // Goodbye! |
| 10822 | synchronized (mPidsSelfLocked) { |
| 10823 | mPidsSelfLocked.remove(app.pid); |
| 10824 | mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app); |
| 10825 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 10826 | app.setPid(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10827 | } |
| 10828 | } |
| 10829 | |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10830 | boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) { |
| 10831 | // Look through the content providers we are waiting to have launched, |
| 10832 | // and if any run in this process then either schedule a restart of |
| 10833 | // the process or kill the client waiting for it if this process has |
| 10834 | // gone bad. |
| 10835 | int NL = mLaunchingProviders.size(); |
| 10836 | boolean restart = false; |
| 10837 | for (int i=0; i<NL; i++) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 10838 | ContentProviderRecord cpr = mLaunchingProviders.get(i); |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10839 | if (cpr.launchingApp == app) { |
| 10840 | if (!alwaysBad && !app.bad) { |
| 10841 | restart = true; |
| 10842 | } else { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 10843 | removeDyingProviderLocked(app, cpr, true); |
Vairavan Srinivasan | 4ee3649 | 2012-09-03 17:09:58 -0700 | [diff] [blame] | 10844 | // cpr should have been removed from mLaunchingProviders |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10845 | NL = mLaunchingProviders.size(); |
Vairavan Srinivasan | 4ee3649 | 2012-09-03 17:09:58 -0700 | [diff] [blame] | 10846 | i--; |
Dianne Hackborn | f670ef7 | 2009-11-16 13:59:16 -0800 | [diff] [blame] | 10847 | } |
| 10848 | } |
| 10849 | } |
| 10850 | return restart; |
| 10851 | } |
| 10852 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10853 | // ========================================================= |
| 10854 | // SERVICES |
| 10855 | // ========================================================= |
| 10856 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10857 | public List<ActivityManager.RunningServiceInfo> getServices(int maxNum, |
| 10858 | int flags) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10859 | enforceNotIsolatedCaller("getServices"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10860 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10861 | return mServices.getRunningServiceInfoLocked(maxNum, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10862 | } |
| 10863 | } |
| 10864 | |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10865 | public PendingIntent getRunningServiceControlPanel(ComponentName name) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10866 | enforceNotIsolatedCaller("getRunningServiceControlPanel"); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10867 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10868 | return mServices.getRunningServiceControlPanelLocked(name); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10869 | } |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 10870 | } |
| 10871 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10872 | public ComponentName startService(IApplicationThread caller, Intent service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10873 | String resolvedType, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10874 | enforceNotIsolatedCaller("startService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10875 | // Refuse possible leaked file descriptors |
| 10876 | if (service != null && service.hasFileDescriptors() == true) { |
| 10877 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10878 | } |
| 10879 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10880 | if (DEBUG_SERVICE) |
| 10881 | Slog.v(TAG, "startService: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10882 | synchronized(this) { |
| 10883 | final int callingPid = Binder.getCallingPid(); |
| 10884 | final int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10885 | checkValidCaller(callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10886 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10887 | ComponentName res = mServices.startServiceLocked(caller, service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10888 | resolvedType, callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10889 | Binder.restoreCallingIdentity(origId); |
| 10890 | return res; |
| 10891 | } |
| 10892 | } |
| 10893 | |
| 10894 | ComponentName startServiceInPackage(int uid, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10895 | Intent service, String resolvedType, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10896 | synchronized(this) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 10897 | if (DEBUG_SERVICE) |
| 10898 | Slog.v(TAG, "startServiceInPackage: " + service + " type=" + resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10899 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10900 | ComponentName res = mServices.startServiceLocked(null, service, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10901 | resolvedType, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10902 | Binder.restoreCallingIdentity(origId); |
| 10903 | return res; |
| 10904 | } |
| 10905 | } |
| 10906 | |
| 10907 | public int stopService(IApplicationThread caller, Intent service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10908 | String resolvedType, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10909 | enforceNotIsolatedCaller("stopService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10910 | // Refuse possible leaked file descriptors |
| 10911 | if (service != null && service.hasFileDescriptors() == true) { |
| 10912 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10913 | } |
| 10914 | |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10915 | checkValidCaller(Binder.getCallingUid(), userId); |
| 10916 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10917 | synchronized(this) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 10918 | return mServices.stopServiceLocked(caller, service, resolvedType, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10919 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10920 | } |
| 10921 | |
| 10922 | public IBinder peekService(Intent service, String resolvedType) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 10923 | enforceNotIsolatedCaller("peekService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10924 | // Refuse possible leaked file descriptors |
| 10925 | if (service != null && service.hasFileDescriptors() == true) { |
| 10926 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 10927 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10928 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10929 | return mServices.peekServiceLocked(service, resolvedType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10930 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10931 | } |
| 10932 | |
| 10933 | public boolean stopServiceToken(ComponentName className, IBinder token, |
| 10934 | int startId) { |
| 10935 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10936 | return mServices.stopServiceTokenLocked(className, token, startId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10937 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10938 | } |
| 10939 | |
| 10940 | public void setServiceForeground(ComponentName className, IBinder token, |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 10941 | int id, Notification notification, boolean removeNotification) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10942 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 10943 | mServices.setServiceForegroundLocked(className, token, id, notification, |
| 10944 | removeNotification); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10945 | } |
| 10946 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 10947 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10948 | public int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll, |
| 10949 | boolean requireFull, String name, String callerPackage) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 10950 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 10951 | if (callingUserId != userId) { |
| 10952 | if (callingUid != 0 && callingUid != Process.SYSTEM_UID) { |
| 10953 | if ((requireFull || checkComponentPermission( |
| 10954 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 10955 | callingPid, callingUid, -1, true) != PackageManager.PERMISSION_GRANTED) |
| 10956 | && checkComponentPermission( |
| 10957 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 10958 | callingPid, callingUid, -1, true) |
| 10959 | != PackageManager.PERMISSION_GRANTED) { |
| 10960 | if (userId == UserHandle.USER_CURRENT_OR_SELF) { |
| 10961 | // In this case, they would like to just execute as their |
| 10962 | // owner user instead of failing. |
| 10963 | userId = callingUserId; |
| 10964 | } else { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10965 | StringBuilder builder = new StringBuilder(128); |
| 10966 | builder.append("Permission Denial: "); |
| 10967 | builder.append(name); |
| 10968 | if (callerPackage != null) { |
| 10969 | builder.append(" from "); |
| 10970 | builder.append(callerPackage); |
| 10971 | } |
| 10972 | builder.append(" asks to run as user "); |
| 10973 | builder.append(userId); |
| 10974 | builder.append(" but is calling from user "); |
| 10975 | builder.append(UserHandle.getUserId(callingUid)); |
| 10976 | builder.append("; this requires "); |
| 10977 | builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
| 10978 | if (!requireFull) { |
Dianne Hackborn | 74ee865 | 2012-09-07 18:33:18 -0700 | [diff] [blame] | 10979 | builder.append(" or "); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 10980 | builder.append(android.Manifest.permission.INTERACT_ACROSS_USERS); |
| 10981 | } |
| 10982 | String msg = builder.toString(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 10983 | Slog.w(TAG, msg); |
| 10984 | throw new SecurityException(msg); |
| 10985 | } |
| 10986 | } |
| 10987 | } |
| 10988 | if (userId == UserHandle.USER_CURRENT |
| 10989 | || userId == UserHandle.USER_CURRENT_OR_SELF) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 10990 | // Note that we may be accessing this outside of a lock... |
| 10991 | // shouldn't be a big deal, if this is being called outside |
| 10992 | // of a locked context there is intrinsically a race with |
| 10993 | // the value the caller will receive and someone else changing it. |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 10994 | userId = mCurrentUserId; |
| 10995 | } |
| 10996 | if (!allowAll && userId < 0) { |
| 10997 | throw new IllegalArgumentException( |
| 10998 | "Call does not support special user #" + userId); |
| 10999 | } |
| 11000 | } |
| 11001 | return userId; |
| 11002 | } |
| 11003 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 11004 | boolean isSingleton(String componentProcessName, ApplicationInfo aInfo, |
| 11005 | String className, int flags) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 11006 | boolean result = false; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 11007 | if (UserHandle.getAppId(aInfo.uid) >= Process.FIRST_APPLICATION_UID) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 11008 | if ((flags&ServiceInfo.FLAG_SINGLE_USER) != 0) { |
| 11009 | if (ActivityManager.checkUidPermission( |
| 11010 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 11011 | aInfo.uid) != PackageManager.PERMISSION_GRANTED) { |
| 11012 | ComponentName comp = new ComponentName(aInfo.packageName, className); |
| 11013 | String msg = "Permission Denial: Component " + comp.flattenToShortString() |
| 11014 | + " requests FLAG_SINGLE_USER, but app does not hold " |
| 11015 | + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 11016 | Slog.w(TAG, msg); |
| 11017 | throw new SecurityException(msg); |
| 11018 | } |
| 11019 | result = true; |
| 11020 | } |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 11021 | } else if (componentProcessName == aInfo.packageName) { |
| 11022 | result = (aInfo.flags & ApplicationInfo.FLAG_PERSISTENT) != 0; |
| 11023 | } else if ("system".equals(componentProcessName)) { |
| 11024 | result = true; |
| 11025 | } |
| 11026 | if (DEBUG_MU) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 11027 | Slog.v(TAG, "isSingleton(" + componentProcessName + ", " + aInfo |
| 11028 | + ", " + className + ", 0x" + Integer.toHexString(flags) + ") = " + result); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 11029 | } |
| 11030 | return result; |
| 11031 | } |
| 11032 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11033 | public int bindService(IApplicationThread caller, IBinder token, |
| 11034 | Intent service, String resolvedType, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 11035 | IServiceConnection connection, int flags, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11036 | enforceNotIsolatedCaller("bindService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11037 | // Refuse possible leaked file descriptors |
| 11038 | if (service != null && service.hasFileDescriptors() == true) { |
| 11039 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11040 | } |
| 11041 | |
| 11042 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11043 | return mServices.bindServiceLocked(caller, token, service, resolvedType, |
| 11044 | connection, flags, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11045 | } |
| 11046 | } |
| 11047 | |
| 11048 | public boolean unbindService(IServiceConnection connection) { |
| 11049 | synchronized (this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11050 | return mServices.unbindServiceLocked(connection); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11051 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11052 | } |
| 11053 | |
| 11054 | public void publishService(IBinder token, Intent intent, IBinder service) { |
| 11055 | // Refuse possible leaked file descriptors |
| 11056 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11057 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11058 | } |
| 11059 | |
| 11060 | synchronized(this) { |
| 11061 | if (!(token instanceof ServiceRecord)) { |
| 11062 | throw new IllegalArgumentException("Invalid service token"); |
| 11063 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11064 | mServices.publishServiceLocked((ServiceRecord)token, intent, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11065 | } |
| 11066 | } |
| 11067 | |
| 11068 | public void unbindFinished(IBinder token, Intent intent, boolean doRebind) { |
| 11069 | // Refuse possible leaked file descriptors |
| 11070 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11071 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11072 | } |
| 11073 | |
| 11074 | synchronized(this) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11075 | mServices.unbindFinishedLocked((ServiceRecord)token, intent, doRebind); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11076 | } |
| 11077 | } |
| 11078 | |
Dianne Hackborn | f6f9f2d | 2009-08-21 16:26:03 -0700 | [diff] [blame] | 11079 | 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] | 11080 | synchronized(this) { |
| 11081 | if (!(token instanceof ServiceRecord)) { |
| 11082 | throw new IllegalArgumentException("Invalid service token"); |
| 11083 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 11084 | mServices.serviceDoneExecutingLocked((ServiceRecord)token, type, startId, res); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 11085 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11086 | } |
| 11087 | |
| 11088 | // ========================================================= |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11089 | // BACKUP AND RESTORE |
| 11090 | // ========================================================= |
| 11091 | |
| 11092 | // Cause the target app to be launched if necessary and its backup agent |
| 11093 | // instantiated. The backup agent will invoke backupAgentCreated() on the |
| 11094 | // activity manager to announce its creation. |
| 11095 | public boolean bindBackupAgent(ApplicationInfo app, int backupMode) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11096 | if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11097 | enforceCallingPermission("android.permission.BACKUP", "startBackupAgent"); |
| 11098 | |
| 11099 | synchronized(this) { |
| 11100 | // !!! TODO: currently no check here that we're already bound |
| 11101 | BatteryStatsImpl.Uid.Pkg.Serv ss = null; |
| 11102 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 11103 | synchronized (stats) { |
| 11104 | ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name); |
| 11105 | } |
| 11106 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11107 | // Backup agent is now in use, its package can't be stopped. |
| 11108 | try { |
| 11109 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 11110 | app.packageName, false, UserHandle.getUserId(app.uid)); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11111 | } catch (RemoteException e) { |
Dianne Hackborn | a925cd4 | 2011-03-10 13:18:20 -0800 | [diff] [blame] | 11112 | } catch (IllegalArgumentException e) { |
| 11113 | Slog.w(TAG, "Failed trying to unstop package " |
| 11114 | + app.packageName + ": " + e); |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11115 | } |
| 11116 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11117 | BackupRecord r = new BackupRecord(ss, app, backupMode); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 11118 | ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) |
| 11119 | ? new ComponentName(app.packageName, app.backupAgentName) |
| 11120 | : new ComponentName("android", "FullBackupAgent"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11121 | // startProcessLocked() returns existing proc's record if it's already running |
| 11122 | ProcessRecord proc = startProcessLocked(app.processName, app, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 11123 | false, 0, "backup", hostingName, false, false); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11124 | if (proc == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11125 | Slog.e(TAG, "Unable to start backup agent process " + r); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11126 | return false; |
| 11127 | } |
| 11128 | |
| 11129 | r.app = proc; |
| 11130 | mBackupTarget = r; |
| 11131 | mBackupAppName = app.packageName; |
| 11132 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11133 | // Try not to kill the process during backup |
| 11134 | updateOomAdjLocked(proc); |
| 11135 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11136 | // If the process is already attached, schedule the creation of the backup agent now. |
| 11137 | // If it is not yet live, this will be done when it attaches to the framework. |
| 11138 | if (proc.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11139 | if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11140 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11141 | proc.thread.scheduleCreateBackupAgent(app, |
| 11142 | compatibilityInfoForPackageLocked(app), backupMode); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11143 | } catch (RemoteException e) { |
Christopher Tate | 436344a | 2009-09-30 16:17:37 -0700 | [diff] [blame] | 11144 | // Will time out on the backup manager side |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11145 | } |
| 11146 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11147 | 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] | 11148 | } |
| 11149 | // Invariants: at this point, the target app process exists and the application |
| 11150 | // is either already running or in the process of coming up. mBackupTarget and |
| 11151 | // mBackupAppName describe the app, so that when it binds back to the AM we |
| 11152 | // know that it's scheduled for a backup-agent operation. |
| 11153 | } |
| 11154 | |
| 11155 | return true; |
| 11156 | } |
| 11157 | |
| 11158 | // A backup agent has just come up |
| 11159 | public void backupAgentCreated(String agentPackageName, IBinder agent) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11160 | if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11161 | + " = " + agent); |
| 11162 | |
| 11163 | synchronized(this) { |
| 11164 | if (!agentPackageName.equals(mBackupAppName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11165 | Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!"); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11166 | return; |
| 11167 | } |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11168 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11169 | |
Dianne Hackborn | 0674069 | 2010-09-22 22:46:21 -0700 | [diff] [blame] | 11170 | long oldIdent = Binder.clearCallingIdentity(); |
| 11171 | try { |
| 11172 | IBackupManager bm = IBackupManager.Stub.asInterface( |
| 11173 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 11174 | bm.agentConnected(agentPackageName, agent); |
| 11175 | } catch (RemoteException e) { |
| 11176 | // can't happen; the backup manager service is local |
| 11177 | } catch (Exception e) { |
| 11178 | Slog.w(TAG, "Exception trying to deliver BackupAgent binding: "); |
| 11179 | e.printStackTrace(); |
| 11180 | } finally { |
| 11181 | Binder.restoreCallingIdentity(oldIdent); |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11182 | } |
| 11183 | } |
| 11184 | |
| 11185 | // done with this agent |
| 11186 | public void unbindBackupAgent(ApplicationInfo appInfo) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11187 | if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11188 | if (appInfo == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11189 | Slog.w(TAG, "unbind backup agent for null app"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11190 | return; |
| 11191 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11192 | |
| 11193 | synchronized(this) { |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11194 | if (mBackupAppName == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11195 | Slog.w(TAG, "Unbinding backup agent with no active backup"); |
Christopher Tate | 8a27f92 | 2009-06-26 11:49:18 -0700 | [diff] [blame] | 11196 | return; |
| 11197 | } |
| 11198 | |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11199 | if (!mBackupAppName.equals(appInfo.packageName)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11200 | 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] | 11201 | return; |
| 11202 | } |
| 11203 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 11204 | ProcessRecord proc = mBackupTarget.app; |
| 11205 | mBackupTarget = null; |
| 11206 | mBackupAppName = null; |
| 11207 | |
| 11208 | // Not backing this app up any more; reset its OOM adjustment |
| 11209 | updateOomAdjLocked(proc); |
| 11210 | |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11211 | // If the app crashed during backup, 'thread' will be null here |
| 11212 | if (proc.thread != null) { |
| 11213 | try { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 11214 | proc.thread.scheduleDestroyBackupAgent(appInfo, |
| 11215 | compatibilityInfoForPackageLocked(appInfo)); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11216 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11217 | Slog.e(TAG, "Exception when unbinding backup agent:"); |
Christopher Tate | c7b31e3 | 2009-06-10 15:49:30 -0700 | [diff] [blame] | 11218 | e.printStackTrace(); |
| 11219 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11220 | } |
Christopher Tate | 181fafa | 2009-05-14 11:12:14 -0700 | [diff] [blame] | 11221 | } |
| 11222 | } |
| 11223 | // ========================================================= |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11224 | // BROADCASTS |
| 11225 | // ========================================================= |
| 11226 | |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 11227 | private final List getStickiesLocked(String action, IntentFilter filter, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11228 | List cur, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11229 | final ContentResolver resolver = mContext.getContentResolver(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11230 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11231 | if (stickies == null) { |
| 11232 | return cur; |
| 11233 | } |
| 11234 | final ArrayList<Intent> list = stickies.get(action); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11235 | if (list == null) { |
| 11236 | return cur; |
| 11237 | } |
| 11238 | int N = list.size(); |
| 11239 | for (int i=0; i<N; i++) { |
| 11240 | Intent intent = list.get(i); |
| 11241 | if (filter.match(resolver, intent, true, TAG) >= 0) { |
| 11242 | if (cur == null) { |
| 11243 | cur = new ArrayList<Intent>(); |
| 11244 | } |
| 11245 | cur.add(intent); |
| 11246 | } |
| 11247 | } |
| 11248 | return cur; |
| 11249 | } |
| 11250 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11251 | boolean isPendingBroadcastProcessLocked(int pid) { |
| 11252 | return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid) |
| 11253 | || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid); |
| 11254 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11255 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11256 | void skipPendingBroadcastLocked(int pid) { |
| 11257 | Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping"); |
| 11258 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 11259 | queue.skipPendingBroadcastLocked(pid); |
| 11260 | } |
| 11261 | } |
| 11262 | |
| 11263 | // The app just attached; send any pending broadcasts that it should receive |
| 11264 | boolean sendPendingBroadcastsLocked(ProcessRecord app) { |
| 11265 | boolean didSomething = false; |
| 11266 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 11267 | didSomething |= queue.sendPendingBroadcastsLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11268 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11269 | return didSomething; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11270 | } |
| 11271 | |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11272 | public Intent registerReceiver(IApplicationThread caller, String callerPackage, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11273 | IIntentReceiver receiver, IntentFilter filter, String permission, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11274 | enforceNotIsolatedCaller("registerReceiver"); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11275 | int callingUid; |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11276 | int callingPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11277 | synchronized(this) { |
| 11278 | ProcessRecord callerApp = null; |
| 11279 | if (caller != null) { |
| 11280 | callerApp = getRecordForAppLocked(caller); |
| 11281 | if (callerApp == null) { |
| 11282 | throw new SecurityException( |
| 11283 | "Unable to find app for caller " + caller |
| 11284 | + " (pid=" + Binder.getCallingPid() |
| 11285 | + ") when registering receiver " + receiver); |
| 11286 | } |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11287 | if (callerApp.info.uid != Process.SYSTEM_UID && |
| 11288 | !callerApp.pkgList.contains(callerPackage)) { |
| 11289 | throw new SecurityException("Given caller package " + callerPackage |
| 11290 | + " is not running in process " + callerApp); |
| 11291 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11292 | callingUid = callerApp.info.uid; |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11293 | callingPid = callerApp.pid; |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 11294 | } else { |
| 11295 | callerPackage = null; |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11296 | callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11297 | callingPid = Binder.getCallingPid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11298 | } |
| 11299 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11300 | userId = this.handleIncomingUser(callingPid, callingUid, userId, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11301 | true, true, "registerReceiver", callerPackage); |
| 11302 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11303 | List allSticky = null; |
| 11304 | |
| 11305 | // Look for any matching sticky broadcasts... |
| 11306 | Iterator actions = filter.actionsIterator(); |
| 11307 | if (actions != null) { |
| 11308 | while (actions.hasNext()) { |
| 11309 | String action = (String)actions.next(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11310 | allSticky = getStickiesLocked(action, filter, allSticky, |
| 11311 | UserHandle.USER_ALL); |
| 11312 | allSticky = getStickiesLocked(action, filter, allSticky, |
| 11313 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11314 | } |
| 11315 | } else { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11316 | allSticky = getStickiesLocked(null, filter, allSticky, |
| 11317 | UserHandle.USER_ALL); |
| 11318 | allSticky = getStickiesLocked(null, filter, allSticky, |
| 11319 | UserHandle.getUserId(callingUid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11320 | } |
| 11321 | |
| 11322 | // The first sticky in the list is returned directly back to |
| 11323 | // the client. |
| 11324 | Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null; |
| 11325 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11326 | if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11327 | + ": " + sticky); |
| 11328 | |
| 11329 | if (receiver == null) { |
| 11330 | return sticky; |
| 11331 | } |
| 11332 | |
| 11333 | ReceiverList rl |
| 11334 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
| 11335 | if (rl == null) { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11336 | rl = new ReceiverList(this, callerApp, callingPid, callingUid, |
| 11337 | userId, receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11338 | if (rl.app != null) { |
| 11339 | rl.app.receivers.add(rl); |
| 11340 | } else { |
| 11341 | try { |
| 11342 | receiver.asBinder().linkToDeath(rl, 0); |
| 11343 | } catch (RemoteException e) { |
| 11344 | return sticky; |
| 11345 | } |
| 11346 | rl.linkedToDeath = true; |
| 11347 | } |
| 11348 | mRegisteredReceivers.put(receiver.asBinder(), rl); |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11349 | } else if (rl.uid != callingUid) { |
| 11350 | throw new IllegalArgumentException( |
| 11351 | "Receiver requested to register for uid " + callingUid |
| 11352 | + " was previously registered for uid " + rl.uid); |
| 11353 | } else if (rl.pid != callingPid) { |
| 11354 | throw new IllegalArgumentException( |
| 11355 | "Receiver requested to register for pid " + callingPid |
| 11356 | + " was previously registered for pid " + rl.pid); |
| 11357 | } else if (rl.userId != userId) { |
| 11358 | throw new IllegalArgumentException( |
| 11359 | "Receiver requested to register for user " + userId |
| 11360 | + " was previously registered for user " + rl.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11361 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11362 | BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 11363 | permission, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11364 | rl.add(bf); |
| 11365 | if (!bf.debugCheck()) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11366 | Slog.w(TAG, "==> For Dynamic broadast"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11367 | } |
| 11368 | mReceiverResolver.addFilter(bf); |
| 11369 | |
| 11370 | // Enqueue broadcasts for all existing stickies that match |
| 11371 | // this filter. |
| 11372 | if (allSticky != null) { |
| 11373 | ArrayList receivers = new ArrayList(); |
| 11374 | receivers.add(bf); |
| 11375 | |
| 11376 | int N = allSticky.size(); |
| 11377 | for (int i=0; i<N; i++) { |
| 11378 | Intent intent = (Intent)allSticky.get(i); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11379 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11380 | BroadcastRecord r = new BroadcastRecord(queue, intent, null, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11381 | null, -1, -1, null, receivers, null, 0, null, null, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11382 | false, true, true, -1); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11383 | queue.enqueueParallelBroadcastLocked(r); |
| 11384 | queue.scheduleBroadcastsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11385 | } |
| 11386 | } |
| 11387 | |
| 11388 | return sticky; |
| 11389 | } |
| 11390 | } |
| 11391 | |
| 11392 | public void unregisterReceiver(IIntentReceiver receiver) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11393 | if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11394 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11395 | final long origId = Binder.clearCallingIdentity(); |
| 11396 | try { |
| 11397 | boolean doTrim = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11398 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11399 | synchronized(this) { |
| 11400 | ReceiverList rl |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11401 | = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder()); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11402 | if (rl != null) { |
| 11403 | if (rl.curBroadcast != null) { |
| 11404 | BroadcastRecord r = rl.curBroadcast; |
| 11405 | final boolean doNext = finishReceiverLocked( |
| 11406 | receiver.asBinder(), r.resultCode, r.resultData, |
| 11407 | r.resultExtras, r.resultAbort, true); |
| 11408 | if (doNext) { |
| 11409 | doTrim = true; |
| 11410 | r.queue.processNextBroadcast(false); |
| 11411 | } |
| 11412 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11413 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11414 | if (rl.app != null) { |
| 11415 | rl.app.receivers.remove(rl); |
| 11416 | } |
| 11417 | removeReceiverLocked(rl); |
| 11418 | if (rl.linkedToDeath) { |
| 11419 | rl.linkedToDeath = false; |
| 11420 | rl.receiver.asBinder().unlinkToDeath(rl, 0); |
| 11421 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11422 | } |
| 11423 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11424 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11425 | // If we actually concluded any broadcasts, we might now be able |
| 11426 | // to trim the recipients' apps from our working set |
| 11427 | if (doTrim) { |
| 11428 | trimApplications(); |
| 11429 | return; |
| 11430 | } |
| 11431 | |
| 11432 | } finally { |
| 11433 | Binder.restoreCallingIdentity(origId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11434 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11435 | } |
| 11436 | |
| 11437 | void removeReceiverLocked(ReceiverList rl) { |
| 11438 | mRegisteredReceivers.remove(rl.receiver.asBinder()); |
| 11439 | int N = rl.size(); |
| 11440 | for (int i=0; i<N; i++) { |
| 11441 | mReceiverResolver.removeFilter(rl.get(i)); |
| 11442 | } |
| 11443 | } |
| 11444 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11445 | private final void sendPackageBroadcastLocked(int cmd, String[] packages, int userId) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11446 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 11447 | ProcessRecord r = mLruProcesses.get(i); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11448 | if (r.thread != null && (userId == UserHandle.USER_ALL || r.userId == userId)) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11449 | try { |
| 11450 | r.thread.dispatchPackageBroadcast(cmd, packages); |
| 11451 | } catch (RemoteException ex) { |
| 11452 | } |
| 11453 | } |
| 11454 | } |
| 11455 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11456 | |
| 11457 | private List<ResolveInfo> collectReceiverComponents(Intent intent, String resolvedType, |
| 11458 | int[] users) { |
| 11459 | List<ResolveInfo> receivers = null; |
| 11460 | try { |
| 11461 | HashSet<ComponentName> singleUserReceivers = null; |
| 11462 | boolean scannedFirstReceivers = false; |
| 11463 | for (int user : users) { |
| 11464 | List<ResolveInfo> newReceivers = AppGlobals.getPackageManager() |
| 11465 | .queryIntentReceivers(intent, resolvedType, STOCK_PM_FLAGS, user); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 11466 | if (user != 0 && newReceivers != null) { |
| 11467 | // If this is not the primary user, we need to check for |
| 11468 | // any receivers that should be filtered out. |
| 11469 | for (int i=0; i<newReceivers.size(); i++) { |
| 11470 | ResolveInfo ri = newReceivers.get(i); |
| 11471 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_PRIMARY_USER_ONLY) != 0) { |
| 11472 | newReceivers.remove(i); |
| 11473 | i--; |
| 11474 | } |
| 11475 | } |
| 11476 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11477 | if (newReceivers != null && newReceivers.size() == 0) { |
| 11478 | newReceivers = null; |
| 11479 | } |
| 11480 | if (receivers == null) { |
| 11481 | receivers = newReceivers; |
| 11482 | } else if (newReceivers != null) { |
| 11483 | // We need to concatenate the additional receivers |
| 11484 | // found with what we have do far. This would be easy, |
| 11485 | // but we also need to de-dup any receivers that are |
| 11486 | // singleUser. |
| 11487 | if (!scannedFirstReceivers) { |
| 11488 | // Collect any single user receivers we had already retrieved. |
| 11489 | scannedFirstReceivers = true; |
| 11490 | for (int i=0; i<receivers.size(); i++) { |
| 11491 | ResolveInfo ri = receivers.get(i); |
| 11492 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 11493 | ComponentName cn = new ComponentName( |
| 11494 | ri.activityInfo.packageName, ri.activityInfo.name); |
| 11495 | if (singleUserReceivers == null) { |
| 11496 | singleUserReceivers = new HashSet<ComponentName>(); |
| 11497 | } |
| 11498 | singleUserReceivers.add(cn); |
| 11499 | } |
| 11500 | } |
| 11501 | } |
| 11502 | // Add the new results to the existing results, tracking |
| 11503 | // and de-dupping single user receivers. |
| 11504 | for (int i=0; i<newReceivers.size(); i++) { |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 11505 | ResolveInfo ri = newReceivers.get(i); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11506 | if ((ri.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 11507 | ComponentName cn = new ComponentName( |
| 11508 | ri.activityInfo.packageName, ri.activityInfo.name); |
| 11509 | if (singleUserReceivers == null) { |
| 11510 | singleUserReceivers = new HashSet<ComponentName>(); |
| 11511 | } |
| 11512 | if (!singleUserReceivers.contains(cn)) { |
| 11513 | singleUserReceivers.add(cn); |
| 11514 | receivers.add(ri); |
| 11515 | } |
| 11516 | } else { |
| 11517 | receivers.add(ri); |
| 11518 | } |
| 11519 | } |
| 11520 | } |
| 11521 | } |
| 11522 | } catch (RemoteException ex) { |
| 11523 | // pm is in same process, this will never happen. |
| 11524 | } |
| 11525 | return receivers; |
| 11526 | } |
| 11527 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11528 | private final int broadcastIntentLocked(ProcessRecord callerApp, |
| 11529 | String callerPackage, Intent intent, String resolvedType, |
| 11530 | IIntentReceiver resultTo, int resultCode, String resultData, |
| 11531 | Bundle map, String requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11532 | boolean ordered, boolean sticky, int callingPid, int callingUid, |
| 11533 | int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11534 | intent = new Intent(intent); |
| 11535 | |
Dianne Hackborn | e7f9721 | 2011-02-24 14:40:20 -0800 | [diff] [blame] | 11536 | // By default broadcasts do not go to stopped apps. |
| 11537 | intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES); |
| 11538 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11539 | if (DEBUG_BROADCAST_LIGHT) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11540 | TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 11541 | + " ordered=" + ordered + " userid=" + userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11542 | if ((resultTo != null) && !ordered) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11543 | 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] | 11544 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11545 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11546 | userId = handleIncomingUser(callingPid, callingUid, userId, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11547 | true, false, "broadcast", callerPackage); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 11548 | |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 11549 | // Make sure that the user who is receiving this broadcast is started. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 11550 | // If not, we will just skip it. |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11551 | if (userId != UserHandle.USER_ALL && mStartedUsers.get(userId) == null) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 11552 | if (callingUid != Process.SYSTEM_UID || (intent.getFlags() |
| 11553 | & Intent.FLAG_RECEIVER_BOOT_UPGRADE) == 0) { |
| 11554 | Slog.w(TAG, "Skipping broadcast of " + intent |
| 11555 | + ": user " + userId + " is stopped"); |
| 11556 | return ActivityManager.BROADCAST_SUCCESS; |
| 11557 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 11558 | } |
| 11559 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11560 | /* |
| 11561 | * Prevent non-system code (defined here to be non-persistent |
| 11562 | * processes) from sending protected broadcasts. |
| 11563 | */ |
Dianne Hackborn | c7ba771 | 2012-09-26 23:22:59 -0700 | [diff] [blame] | 11564 | int callingAppId = UserHandle.getAppId(callingUid); |
| 11565 | if (callingAppId == Process.SYSTEM_UID || callingAppId == Process.PHONE_UID |
| 11566 | || callingAppId == Process.SHELL_UID || callingAppId == Process.BLUETOOTH_UID || |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11567 | callingUid == 0) { |
| 11568 | // Always okay. |
| 11569 | } else if (callerApp == null || !callerApp.persistent) { |
| 11570 | try { |
| 11571 | if (AppGlobals.getPackageManager().isProtectedBroadcast( |
| 11572 | intent.getAction())) { |
| 11573 | String msg = "Permission Denial: not allowed to send broadcast " |
| 11574 | + intent.getAction() + " from pid=" |
| 11575 | + callingPid + ", uid=" + callingUid; |
| 11576 | Slog.w(TAG, msg); |
| 11577 | throw new SecurityException(msg); |
| 11578 | } |
| 11579 | } catch (RemoteException e) { |
| 11580 | Slog.w(TAG, "Remote exception", e); |
| 11581 | return ActivityManager.BROADCAST_SUCCESS; |
| 11582 | } |
| 11583 | } |
| 11584 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11585 | // Handle special intents: if this broadcast is from the package |
| 11586 | // manager about a package being removed, we need to remove all of |
| 11587 | // its activities from the history stack. |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11588 | final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11589 | intent.getAction()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11590 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) |
| 11591 | || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11592 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11593 | || uidRemoved) { |
| 11594 | if (checkComponentPermission( |
| 11595 | android.Manifest.permission.BROADCAST_PACKAGE_REMOVED, |
Dianne Hackborn | 6c2c5fc | 2011-01-18 17:02:33 -0800 | [diff] [blame] | 11596 | callingPid, callingUid, -1, true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11597 | == PackageManager.PERMISSION_GRANTED) { |
| 11598 | if (uidRemoved) { |
| 11599 | final Bundle intentExtras = intent.getExtras(); |
| 11600 | final int uid = intentExtras != null |
| 11601 | ? intentExtras.getInt(Intent.EXTRA_UID) : -1; |
| 11602 | if (uid >= 0) { |
| 11603 | BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics(); |
| 11604 | synchronized (bs) { |
| 11605 | bs.removeUidStatsLocked(uid); |
| 11606 | } |
| 11607 | } |
| 11608 | } else { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11609 | // If resources are unavailable just force stop all |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11610 | // those packages and flush the attribute cache as well. |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 11611 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11612 | String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 11613 | if (list != null && (list.length > 0)) { |
| 11614 | for (String pkg : list) { |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11615 | forceStopPackageLocked(pkg, -1, false, true, true, false, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11616 | } |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11617 | sendPackageBroadcastLocked( |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11618 | IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list, userId); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11619 | } |
| 11620 | } else { |
| 11621 | Uri data = intent.getData(); |
| 11622 | String ssp; |
| 11623 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11624 | if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) { |
| 11625 | forceStopPackageLocked(ssp, |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 11626 | intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true, |
| 11627 | false, userId); |
Dianne Hackborn | de7faf6 | 2009-06-30 13:27:30 -0700 | [diff] [blame] | 11628 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11629 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11630 | sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11631 | new String[] {ssp}, userId); |
Dianne Hackborn | 4416c3d | 2010-05-04 17:22:49 -0700 | [diff] [blame] | 11632 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11633 | } |
| 11634 | } |
| 11635 | } |
| 11636 | } else { |
| 11637 | String msg = "Permission Denial: " + intent.getAction() |
| 11638 | + " broadcast from " + callerPackage + " (pid=" + callingPid |
| 11639 | + ", uid=" + callingUid + ")" |
| 11640 | + " requires " |
| 11641 | + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11642 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11643 | throw new SecurityException(msg); |
| 11644 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11645 | |
| 11646 | // Special case for adding a package: by default turn on compatibility |
| 11647 | // mode. |
| 11648 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { |
Dianne Hackborn | 8ea5e1d | 2011-05-27 16:45:31 -0700 | [diff] [blame] | 11649 | Uri data = intent.getData(); |
| 11650 | String ssp; |
| 11651 | if (data != null && (ssp=data.getSchemeSpecificPart()) != null) { |
| 11652 | mCompatModePackages.handlePackageAddedLocked(ssp, |
| 11653 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11654 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11655 | } |
| 11656 | |
| 11657 | /* |
| 11658 | * If this is the time zone changed action, queue up a message that will reset the timezone |
| 11659 | * of all currently running processes. This message will get queued up before the broadcast |
| 11660 | * happens. |
| 11661 | */ |
| 11662 | if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) { |
| 11663 | mHandler.sendEmptyMessage(UPDATE_TIME_ZONE); |
| 11664 | } |
| 11665 | |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 11666 | if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) { |
| 11667 | mHandler.sendEmptyMessage(CLEAR_DNS_CACHE); |
| 11668 | } |
| 11669 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 11670 | if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) { |
| 11671 | ProxyProperties proxy = intent.getParcelableExtra("proxy"); |
| 11672 | mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy)); |
| 11673 | } |
| 11674 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11675 | // Add to the sticky list if requested. |
| 11676 | if (sticky) { |
| 11677 | if (checkPermission(android.Manifest.permission.BROADCAST_STICKY, |
| 11678 | callingPid, callingUid) |
| 11679 | != PackageManager.PERMISSION_GRANTED) { |
| 11680 | String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid=" |
| 11681 | + callingPid + ", uid=" + callingUid |
| 11682 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11683 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11684 | throw new SecurityException(msg); |
| 11685 | } |
| 11686 | if (requiredPermission != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11687 | Slog.w(TAG, "Can't broadcast sticky intent " + intent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11688 | + " and enforce permission " + requiredPermission); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 11689 | return ActivityManager.BROADCAST_STICKY_CANT_HAVE_PERMISSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11690 | } |
| 11691 | if (intent.getComponent() != null) { |
| 11692 | throw new SecurityException( |
| 11693 | "Sticky broadcasts can't target a specific component"); |
| 11694 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11695 | // We use userId directly here, since the "all" target is maintained |
| 11696 | // as a separate set of sticky broadcasts. |
| 11697 | if (userId != UserHandle.USER_ALL) { |
| 11698 | // But first, if this is not a broadcast to all users, then |
| 11699 | // make sure it doesn't conflict with an existing broadcast to |
| 11700 | // all users. |
| 11701 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get( |
| 11702 | UserHandle.USER_ALL); |
| 11703 | if (stickies != null) { |
| 11704 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
| 11705 | if (list != null) { |
| 11706 | int N = list.size(); |
| 11707 | int i; |
| 11708 | for (i=0; i<N; i++) { |
| 11709 | if (intent.filterEquals(list.get(i))) { |
| 11710 | throw new IllegalArgumentException( |
| 11711 | "Sticky broadcast " + intent + " for user " |
| 11712 | + userId + " conflicts with existing global broadcast"); |
| 11713 | } |
| 11714 | } |
| 11715 | } |
| 11716 | } |
| 11717 | } |
| 11718 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11719 | if (stickies == null) { |
| 11720 | stickies = new HashMap<String, ArrayList<Intent>>(); |
| 11721 | mStickyBroadcasts.put(userId, stickies); |
| 11722 | } |
| 11723 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11724 | if (list == null) { |
| 11725 | list = new ArrayList<Intent>(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11726 | stickies.put(intent.getAction(), list); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11727 | } |
| 11728 | int N = list.size(); |
| 11729 | int i; |
| 11730 | for (i=0; i<N; i++) { |
| 11731 | if (intent.filterEquals(list.get(i))) { |
| 11732 | // This sticky already exists, replace it. |
| 11733 | list.set(i, new Intent(intent)); |
| 11734 | break; |
| 11735 | } |
| 11736 | } |
| 11737 | if (i >= N) { |
| 11738 | list.add(new Intent(intent)); |
| 11739 | } |
| 11740 | } |
| 11741 | |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11742 | int[] users; |
| 11743 | if (userId == UserHandle.USER_ALL) { |
| 11744 | // Caller wants broadcast to go to all started users. |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 11745 | users = mStartedUserArray; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11746 | } else { |
| 11747 | // Caller wants broadcast to go to one specific user. |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 11748 | users = mCurrentUserArray; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11749 | } |
| 11750 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11751 | // Figure out who all will receive this broadcast. |
| 11752 | List receivers = null; |
| 11753 | List<BroadcastFilter> registeredReceivers = null; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11754 | // Need to resolve the intent to interested receivers... |
| 11755 | if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) |
| 11756 | == 0) { |
| 11757 | receivers = collectReceiverComponents(intent, resolvedType, users); |
| 11758 | } |
| 11759 | if (intent.getComponent() == null) { |
| 11760 | registeredReceivers = mReceiverResolver.queryIntent(intent, |
| 11761 | resolvedType, false, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11762 | } |
| 11763 | |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11764 | final boolean replacePending = |
| 11765 | (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0; |
| 11766 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11767 | if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction() |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11768 | + " replacePending=" + replacePending); |
| 11769 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11770 | int NR = registeredReceivers != null ? registeredReceivers.size() : 0; |
| 11771 | if (!ordered && NR > 0) { |
| 11772 | // If we are not serializing this broadcast, then send the |
| 11773 | // registered receivers separately so they don't wait for the |
| 11774 | // components to be launched. |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11775 | final BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11776 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11777 | callerPackage, callingPid, callingUid, requiredPermission, |
| 11778 | registeredReceivers, resultTo, resultCode, resultData, map, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11779 | ordered, sticky, false, userId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11780 | if (DEBUG_BROADCAST) Slog.v( |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11781 | TAG, "Enqueueing parallel broadcast " + r); |
| 11782 | final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11783 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11784 | queue.enqueueParallelBroadcastLocked(r); |
| 11785 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11786 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11787 | registeredReceivers = null; |
| 11788 | NR = 0; |
| 11789 | } |
| 11790 | |
| 11791 | // Merge into one list. |
| 11792 | int ir = 0; |
| 11793 | if (receivers != null) { |
| 11794 | // A special case for PACKAGE_ADDED: do not allow the package |
| 11795 | // being added to see this broadcast. This prevents them from |
| 11796 | // using this as a back door to get run as soon as they are |
| 11797 | // installed. Maybe in the future we want to have a special install |
| 11798 | // broadcast or such for apps, but we'd like to deliberately make |
| 11799 | // this decision. |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11800 | String skipPackages[] = null; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11801 | if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) |
| 11802 | || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction()) |
| 11803 | || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11804 | Uri data = intent.getData(); |
| 11805 | if (data != null) { |
| 11806 | String pkgName = data.getSchemeSpecificPart(); |
| 11807 | if (pkgName != null) { |
| 11808 | skipPackages = new String[] { pkgName }; |
| 11809 | } |
| 11810 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 11811 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11812 | skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 11813 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 11814 | if (skipPackages != null && (skipPackages.length > 0)) { |
| 11815 | for (String skipPackage : skipPackages) { |
| 11816 | if (skipPackage != null) { |
| 11817 | int NT = receivers.size(); |
| 11818 | for (int it=0; it<NT; it++) { |
| 11819 | ResolveInfo curt = (ResolveInfo)receivers.get(it); |
| 11820 | if (curt.activityInfo.packageName.equals(skipPackage)) { |
| 11821 | receivers.remove(it); |
| 11822 | it--; |
| 11823 | NT--; |
| 11824 | } |
| 11825 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11826 | } |
| 11827 | } |
| 11828 | } |
| 11829 | |
| 11830 | int NT = receivers != null ? receivers.size() : 0; |
| 11831 | int it = 0; |
| 11832 | ResolveInfo curt = null; |
| 11833 | BroadcastFilter curr = null; |
| 11834 | while (it < NT && ir < NR) { |
| 11835 | if (curt == null) { |
| 11836 | curt = (ResolveInfo)receivers.get(it); |
| 11837 | } |
| 11838 | if (curr == null) { |
| 11839 | curr = registeredReceivers.get(ir); |
| 11840 | } |
| 11841 | if (curr.getPriority() >= curt.priority) { |
| 11842 | // Insert this broadcast record into the final list. |
| 11843 | receivers.add(it, curr); |
| 11844 | ir++; |
| 11845 | curr = null; |
| 11846 | it++; |
| 11847 | NT++; |
| 11848 | } else { |
| 11849 | // Skip to the next ResolveInfo in the final list. |
| 11850 | it++; |
| 11851 | curt = null; |
| 11852 | } |
| 11853 | } |
| 11854 | } |
| 11855 | while (ir < NR) { |
| 11856 | if (receivers == null) { |
| 11857 | receivers = new ArrayList(); |
| 11858 | } |
| 11859 | receivers.add(registeredReceivers.get(ir)); |
| 11860 | ir++; |
| 11861 | } |
| 11862 | |
| 11863 | if ((receivers != null && receivers.size() > 0) |
| 11864 | || resultTo != null) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11865 | BroadcastQueue queue = broadcastQueueForIntent(intent); |
| 11866 | BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11867 | callerPackage, callingPid, callingUid, requiredPermission, |
Dianne Hackborn | 12527f9 | 2009-11-11 17:39:50 -0800 | [diff] [blame] | 11868 | receivers, resultTo, resultCode, resultData, map, ordered, |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 11869 | sticky, false, userId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11870 | if (DEBUG_BROADCAST) Slog.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11871 | TAG, "Enqueueing ordered broadcast " + r |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11872 | + ": prev had " + queue.mOrderedBroadcasts.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11873 | if (DEBUG_BROADCAST) { |
| 11874 | int seq = r.intent.getIntExtra("seq", -1); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11875 | 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] | 11876 | } |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11877 | boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11878 | if (!replaced) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11879 | queue.enqueueOrderedBroadcastLocked(r); |
| 11880 | queue.scheduleBroadcastsLocked(); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 11881 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11882 | } |
| 11883 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 11884 | return ActivityManager.BROADCAST_SUCCESS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11885 | } |
| 11886 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11887 | final Intent verifyBroadcastLocked(Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11888 | // Refuse possible leaked file descriptors |
| 11889 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11890 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11891 | } |
| 11892 | |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11893 | int flags = intent.getFlags(); |
| 11894 | |
| 11895 | if (!mProcessesReady) { |
| 11896 | // if the caller really truly claims to know what they're doing, go |
| 11897 | // ahead and allow the broadcast without launching any receivers |
| 11898 | if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { |
| 11899 | intent = new Intent(intent); |
| 11900 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 11901 | } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 11902 | Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent |
| 11903 | + " before boot completion"); |
| 11904 | throw new IllegalStateException("Cannot broadcast before boot completed"); |
| 11905 | } |
| 11906 | } |
| 11907 | |
| 11908 | if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) { |
| 11909 | throw new IllegalArgumentException( |
| 11910 | "Can't use FLAG_RECEIVER_BOOT_UPGRADE here"); |
| 11911 | } |
| 11912 | |
| 11913 | return intent; |
| 11914 | } |
| 11915 | |
| 11916 | public final int broadcastIntent(IApplicationThread caller, |
| 11917 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11918 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11919 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 11920 | enforceNotIsolatedCaller("broadcastIntent"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11921 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11922 | intent = verifyBroadcastLocked(intent); |
Dianne Hackborn | 9acc030 | 2009-08-25 00:27:12 -0700 | [diff] [blame] | 11923 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11924 | final ProcessRecord callerApp = getRecordForAppLocked(caller); |
| 11925 | final int callingPid = Binder.getCallingPid(); |
| 11926 | final int callingUid = Binder.getCallingUid(); |
| 11927 | final long origId = Binder.clearCallingIdentity(); |
| 11928 | int res = broadcastIntentLocked(callerApp, |
| 11929 | callerApp != null ? callerApp.info.packageName : null, |
| 11930 | intent, resolvedType, resultTo, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11931 | resultCode, resultData, map, requiredPermission, serialized, sticky, |
| 11932 | callingPid, callingUid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11933 | Binder.restoreCallingIdentity(origId); |
| 11934 | return res; |
| 11935 | } |
| 11936 | } |
| 11937 | |
| 11938 | int broadcastIntentInPackage(String packageName, int uid, |
| 11939 | Intent intent, String resolvedType, IIntentReceiver resultTo, |
| 11940 | int resultCode, String resultData, Bundle map, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11941 | String requiredPermission, boolean serialized, boolean sticky, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11942 | synchronized(this) { |
Dianne Hackborn | 8891fdc | 2010-09-20 20:44:46 -0700 | [diff] [blame] | 11943 | intent = verifyBroadcastLocked(intent); |
| 11944 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11945 | final long origId = Binder.clearCallingIdentity(); |
| 11946 | int res = broadcastIntentLocked(null, packageName, intent, resolvedType, |
| 11947 | resultTo, resultCode, resultData, map, requiredPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11948 | serialized, sticky, -1, uid, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11949 | Binder.restoreCallingIdentity(origId); |
| 11950 | return res; |
| 11951 | } |
| 11952 | } |
| 11953 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 11954 | 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] | 11955 | // Refuse possible leaked file descriptors |
| 11956 | if (intent != null && intent.hasFileDescriptors() == true) { |
| 11957 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 11958 | } |
| 11959 | |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 11960 | userId = handleIncomingUser(Binder.getCallingPid(), |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11961 | Binder.getCallingUid(), userId, true, false, "removeStickyBroadcast", null); |
| 11962 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11963 | synchronized(this) { |
| 11964 | if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY) |
| 11965 | != PackageManager.PERMISSION_GRANTED) { |
| 11966 | String msg = "Permission Denial: unbroadcastIntent() from pid=" |
| 11967 | + Binder.getCallingPid() |
| 11968 | + ", uid=" + Binder.getCallingUid() |
| 11969 | + " requires " + android.Manifest.permission.BROADCAST_STICKY; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 11970 | Slog.w(TAG, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11971 | throw new SecurityException(msg); |
| 11972 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11973 | HashMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(userId); |
| 11974 | if (stickies != null) { |
| 11975 | ArrayList<Intent> list = stickies.get(intent.getAction()); |
| 11976 | if (list != null) { |
| 11977 | int N = list.size(); |
| 11978 | int i; |
| 11979 | for (i=0; i<N; i++) { |
| 11980 | if (intent.filterEquals(list.get(i))) { |
| 11981 | list.remove(i); |
| 11982 | break; |
| 11983 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11984 | } |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 11985 | if (list.size() <= 0) { |
| 11986 | stickies.remove(intent.getAction()); |
| 11987 | } |
| 11988 | } |
| 11989 | if (stickies.size() <= 0) { |
| 11990 | mStickyBroadcasts.remove(userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11991 | } |
| 11992 | } |
| 11993 | } |
| 11994 | } |
| 11995 | |
| 11996 | private final boolean finishReceiverLocked(IBinder receiver, int resultCode, |
| 11997 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 11998 | boolean explicit) { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 11999 | final BroadcastRecord r = broadcastRecordForReceiverLocked(receiver); |
| 12000 | if (r == null) { |
| 12001 | 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] | 12002 | return false; |
| 12003 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12004 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12005 | return r.queue.finishReceiverLocked(r, resultCode, resultData, resultExtras, resultAbort, |
| 12006 | explicit); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12007 | } |
| 12008 | |
| 12009 | public void finishReceiver(IBinder who, int resultCode, String resultData, |
| 12010 | Bundle resultExtras, boolean resultAbort) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12011 | if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12012 | |
| 12013 | // Refuse possible leaked file descriptors |
| 12014 | if (resultExtras != null && resultExtras.hasFileDescriptors()) { |
| 12015 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12016 | } |
| 12017 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12018 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12019 | try { |
| 12020 | boolean doNext = false; |
| 12021 | BroadcastRecord r = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12022 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12023 | synchronized(this) { |
| 12024 | r = broadcastRecordForReceiverLocked(who); |
| 12025 | if (r != null) { |
| 12026 | doNext = r.queue.finishReceiverLocked(r, resultCode, |
| 12027 | resultData, resultExtras, resultAbort, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12028 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12029 | } |
Jeff Brown | 4d94a76 | 2010-09-23 11:33:28 -0700 | [diff] [blame] | 12030 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12031 | if (doNext) { |
| 12032 | r.queue.processNextBroadcast(false); |
| 12033 | } |
| 12034 | trimApplications(); |
| 12035 | } finally { |
| 12036 | Binder.restoreCallingIdentity(origId); |
Dianne Hackborn | ad5499d | 2010-03-29 18:08:45 -0700 | [diff] [blame] | 12037 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12038 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12039 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12040 | // ========================================================= |
| 12041 | // INSTRUMENTATION |
| 12042 | // ========================================================= |
| 12043 | |
| 12044 | public boolean startInstrumentation(ComponentName className, |
| 12045 | String profileFile, int flags, Bundle arguments, |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12046 | IInstrumentationWatcher watcher, int userId) { |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12047 | enforceNotIsolatedCaller("startInstrumentation"); |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 12048 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12049 | userId, false, true, "startInstrumentation", null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12050 | // Refuse possible leaked file descriptors |
| 12051 | if (arguments != null && arguments.hasFileDescriptors()) { |
| 12052 | throw new IllegalArgumentException("File descriptors passed in Bundle"); |
| 12053 | } |
| 12054 | |
| 12055 | synchronized(this) { |
| 12056 | InstrumentationInfo ii = null; |
| 12057 | ApplicationInfo ai = null; |
| 12058 | try { |
| 12059 | ii = mContext.getPackageManager().getInstrumentationInfo( |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12060 | className, STOCK_PM_FLAGS); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12061 | ai = AppGlobals.getPackageManager().getApplicationInfo( |
| 12062 | ii.targetPackage, STOCK_PM_FLAGS, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12063 | } catch (PackageManager.NameNotFoundException e) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 12064 | } catch (RemoteException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12065 | } |
| 12066 | if (ii == null) { |
| 12067 | reportStartInstrumentationFailure(watcher, className, |
| 12068 | "Unable to find instrumentation info for: " + className); |
| 12069 | return false; |
| 12070 | } |
| 12071 | if (ai == null) { |
| 12072 | reportStartInstrumentationFailure(watcher, className, |
| 12073 | "Unable to find instrumentation target package: " + ii.targetPackage); |
| 12074 | return false; |
| 12075 | } |
| 12076 | |
| 12077 | int match = mContext.getPackageManager().checkSignatures( |
| 12078 | ii.targetPackage, ii.packageName); |
| 12079 | if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) { |
| 12080 | String msg = "Permission Denial: starting instrumentation " |
| 12081 | + className + " from pid=" |
| 12082 | + Binder.getCallingPid() |
| 12083 | + ", uid=" + Binder.getCallingPid() |
| 12084 | + " not allowed because package " + ii.packageName |
| 12085 | + " does not have a signature matching the target " |
| 12086 | + ii.targetPackage; |
| 12087 | reportStartInstrumentationFailure(watcher, className, msg); |
| 12088 | throw new SecurityException(msg); |
| 12089 | } |
| 12090 | |
| 12091 | final long origId = Binder.clearCallingIdentity(); |
Christopher Tate | 3dacd84 | 2011-08-19 14:56:15 -0700 | [diff] [blame] | 12092 | // Instrumentation can kill and relaunch even persistent processes |
Amith Yamasani | 483f3b0 | 2012-03-13 16:08:00 -0700 | [diff] [blame] | 12093 | forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true, userId); |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 12094 | ProcessRecord app = addAppLocked(ai, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12095 | app.instrumentationClass = className; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12096 | app.instrumentationInfo = ai; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12097 | app.instrumentationProfileFile = profileFile; |
| 12098 | app.instrumentationArguments = arguments; |
| 12099 | app.instrumentationWatcher = watcher; |
| 12100 | app.instrumentationResultClass = className; |
| 12101 | Binder.restoreCallingIdentity(origId); |
| 12102 | } |
| 12103 | |
| 12104 | return true; |
| 12105 | } |
| 12106 | |
| 12107 | /** |
| 12108 | * Report errors that occur while attempting to start Instrumentation. Always writes the |
| 12109 | * error to the logs, but if somebody is watching, send the report there too. This enables |
| 12110 | * the "am" command to report errors with more information. |
| 12111 | * |
| 12112 | * @param watcher The IInstrumentationWatcher. Null if there isn't one. |
| 12113 | * @param cn The component name of the instrumentation. |
| 12114 | * @param report The error report. |
| 12115 | */ |
| 12116 | private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher, |
| 12117 | ComponentName cn, String report) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12118 | Slog.w(TAG, report); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12119 | try { |
| 12120 | if (watcher != null) { |
| 12121 | Bundle results = new Bundle(); |
| 12122 | results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService"); |
| 12123 | results.putString("Error", report); |
| 12124 | watcher.instrumentationStatus(cn, -1, results); |
| 12125 | } |
| 12126 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12127 | Slog.w(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12128 | } |
| 12129 | } |
| 12130 | |
| 12131 | void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) { |
| 12132 | if (app.instrumentationWatcher != null) { |
| 12133 | try { |
| 12134 | // NOTE: IInstrumentationWatcher *must* be oneway here |
| 12135 | app.instrumentationWatcher.instrumentationFinished( |
| 12136 | app.instrumentationClass, |
| 12137 | resultCode, |
| 12138 | results); |
| 12139 | } catch (RemoteException e) { |
| 12140 | } |
| 12141 | } |
| 12142 | app.instrumentationWatcher = null; |
| 12143 | app.instrumentationClass = null; |
Dianne Hackborn | 1655be4 | 2009-05-08 14:29:01 -0700 | [diff] [blame] | 12144 | app.instrumentationInfo = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12145 | app.instrumentationProfileFile = null; |
| 12146 | app.instrumentationArguments = null; |
| 12147 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 12148 | 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] | 12149 | } |
| 12150 | |
| 12151 | public void finishInstrumentation(IApplicationThread target, |
| 12152 | int resultCode, Bundle results) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 12153 | int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12154 | // Refuse possible leaked file descriptors |
| 12155 | if (results != null && results.hasFileDescriptors()) { |
| 12156 | throw new IllegalArgumentException("File descriptors passed in Intent"); |
| 12157 | } |
| 12158 | |
| 12159 | synchronized(this) { |
| 12160 | ProcessRecord app = getRecordForAppLocked(target); |
| 12161 | if (app == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12162 | Slog.w(TAG, "finishInstrumentation: no app for " + target); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12163 | return; |
| 12164 | } |
| 12165 | final long origId = Binder.clearCallingIdentity(); |
| 12166 | finishInstrumentationLocked(app, resultCode, results); |
| 12167 | Binder.restoreCallingIdentity(origId); |
| 12168 | } |
| 12169 | } |
| 12170 | |
| 12171 | // ========================================================= |
| 12172 | // CONFIGURATION |
| 12173 | // ========================================================= |
| 12174 | |
| 12175 | public ConfigurationInfo getDeviceConfigurationInfo() { |
| 12176 | ConfigurationInfo config = new ConfigurationInfo(); |
| 12177 | synchronized (this) { |
| 12178 | config.reqTouchScreen = mConfiguration.touchscreen; |
| 12179 | config.reqKeyboardType = mConfiguration.keyboard; |
| 12180 | config.reqNavigation = mConfiguration.navigation; |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12181 | if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD |
| 12182 | || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12183 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV; |
| 12184 | } |
Dianne Hackborn | fae76f5 | 2009-07-16 13:41:23 -0700 | [diff] [blame] | 12185 | if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED |
| 12186 | && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12187 | config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD; |
| 12188 | } |
Jack Palevich | b90d28c | 2009-07-22 15:35:24 -0700 | [diff] [blame] | 12189 | config.reqGlEsVersion = GL_ES_VERSION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12190 | } |
| 12191 | return config; |
| 12192 | } |
| 12193 | |
| 12194 | public Configuration getConfiguration() { |
| 12195 | Configuration ci; |
| 12196 | synchronized(this) { |
| 12197 | ci = new Configuration(mConfiguration); |
| 12198 | } |
| 12199 | return ci; |
| 12200 | } |
| 12201 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12202 | public void updatePersistentConfiguration(Configuration values) { |
| 12203 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12204 | "updateConfiguration()"); |
| 12205 | enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS, |
| 12206 | "updateConfiguration()"); |
| 12207 | if (values == null) { |
| 12208 | throw new NullPointerException("Configuration must not be null"); |
| 12209 | } |
| 12210 | |
| 12211 | synchronized(this) { |
| 12212 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12213 | updateConfigurationLocked(values, null, true, false); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12214 | Binder.restoreCallingIdentity(origId); |
| 12215 | } |
| 12216 | } |
| 12217 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12218 | public void updateConfiguration(Configuration values) { |
| 12219 | enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION, |
| 12220 | "updateConfiguration()"); |
| 12221 | |
| 12222 | synchronized(this) { |
| 12223 | if (values == null && mWindowManager != null) { |
| 12224 | // sentinel: fetch the current configuration from the window manager |
| 12225 | values = mWindowManager.computeNewConfiguration(); |
| 12226 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12227 | |
| 12228 | if (mWindowManager != null) { |
| 12229 | mProcessList.applyDisplaySize(mWindowManager); |
| 12230 | } |
| 12231 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12232 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12233 | if (values != null) { |
| 12234 | Settings.System.clearConfiguration(values); |
| 12235 | } |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12236 | updateConfigurationLocked(values, null, false, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12237 | Binder.restoreCallingIdentity(origId); |
| 12238 | } |
| 12239 | } |
| 12240 | |
| 12241 | /** |
| 12242 | * Do either or both things: (1) change the current configuration, and (2) |
| 12243 | * make sure the given activity is running with the (now) current |
| 12244 | * configuration. Returns true if the activity has been left running, or |
| 12245 | * false if <var>starting</var> is being destroyed to match the new |
| 12246 | * configuration. |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12247 | * @param persistent TODO |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12248 | */ |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 12249 | boolean updateConfigurationLocked(Configuration values, |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12250 | ActivityRecord starting, boolean persistent, boolean initLocale) { |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 12251 | // do nothing if we are headless |
| 12252 | if (mHeadless) return true; |
| 12253 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12254 | int changes = 0; |
| 12255 | |
| 12256 | boolean kept = true; |
| 12257 | |
| 12258 | if (values != null) { |
| 12259 | Configuration newConfig = new Configuration(mConfiguration); |
| 12260 | changes = newConfig.updateFrom(values); |
| 12261 | if (changes != 0) { |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12262 | if (DEBUG_SWITCH || DEBUG_CONFIGURATION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12263 | Slog.i(TAG, "Updating configuration to: " + values); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12264 | } |
| 12265 | |
Doug Zongker | 2bec3d4 | 2009-12-04 12:52:44 -0800 | [diff] [blame] | 12266 | EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12267 | |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12268 | if (values.locale != null && !initLocale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12269 | saveLocaleLocked(values.locale, |
| 12270 | !values.locale.equals(mConfiguration.locale), |
| 12271 | values.userSetLocale); |
| 12272 | } |
| 12273 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12274 | mConfigurationSeq++; |
| 12275 | if (mConfigurationSeq <= 0) { |
| 12276 | mConfigurationSeq = 1; |
| 12277 | } |
| 12278 | newConfig.seq = mConfigurationSeq; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12279 | mConfiguration = newConfig; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12280 | Slog.i(TAG, "Config changed: " + newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12281 | |
| 12282 | final Configuration configCopy = new Configuration(mConfiguration); |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12283 | |
| 12284 | // TODO: If our config changes, should we auto dismiss any currently |
| 12285 | // showing dialogs? |
| 12286 | mShowDialogs = shouldShowDialogs(newConfig); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12287 | |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12288 | AttributeCache ac = AttributeCache.instance(); |
| 12289 | if (ac != null) { |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12290 | ac.updateConfiguration(configCopy); |
Dianne Hackborn | 826d17c | 2009-11-12 12:55:51 -0800 | [diff] [blame] | 12291 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12292 | |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12293 | // Make sure all resources in our process are updated |
| 12294 | // right now, so that anyone who is going to retrieve |
| 12295 | // resource values after we return will be sure to get |
| 12296 | // the new ones. This is especially important during |
| 12297 | // boot, where the first config change needs to guarantee |
| 12298 | // all resources have that config before following boot |
| 12299 | // code is executed. |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12300 | mSystemThread.applyConfigurationToResources(configCopy); |
Dianne Hackborn | 2f0b175 | 2011-05-31 17:59:49 -0700 | [diff] [blame] | 12301 | |
Dianne Hackborn | 31ca854 | 2011-07-19 14:58:28 -0700 | [diff] [blame] | 12302 | if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12303 | Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12304 | msg.obj = new Configuration(configCopy); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 12305 | mHandler.sendMessage(msg); |
| 12306 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12307 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12308 | for (int i=mLruProcesses.size()-1; i>=0; i--) { |
| 12309 | ProcessRecord app = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12310 | try { |
| 12311 | if (app.thread != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12312 | if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc " |
Dianne Hackborn | dc6b635 | 2009-09-30 14:20:09 -0700 | [diff] [blame] | 12313 | + app.processName + " new config " + mConfiguration); |
Dianne Hackborn | 813075a6 | 2011-11-14 17:45:19 -0800 | [diff] [blame] | 12314 | app.thread.scheduleConfigurationChanged(configCopy); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12315 | } |
| 12316 | } catch (Exception e) { |
| 12317 | } |
| 12318 | } |
| 12319 | Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 12320 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 12321 | | Intent.FLAG_RECEIVER_REPLACE_PENDING |
| 12322 | | Intent.FLAG_RECEIVER_FOREGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12323 | broadcastIntentLocked(null, null, intent, null, null, 0, null, null, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12324 | null, false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12325 | if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 12326 | intent = new Intent(Intent.ACTION_LOCALE_CHANGED); |
| 12327 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 12328 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12329 | null, null, 0, null, null, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 12330 | null, false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
Dianne Hackborn | 362d5b9 | 2009-11-11 18:04:39 -0800 | [diff] [blame] | 12331 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12332 | } |
| 12333 | } |
| 12334 | |
| 12335 | if (changes != 0 && starting == null) { |
| 12336 | // If the configuration changed, and the caller is not already |
| 12337 | // in the process of starting an activity, then find the top |
| 12338 | // activity to check if its configuration needs to change. |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12339 | starting = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12340 | } |
| 12341 | |
| 12342 | if (starting != null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 12343 | kept = mMainStack.ensureActivityConfigurationLocked(starting, changes); |
Dianne Hackborn | 5f4d643 | 2010-12-21 20:40:11 -0800 | [diff] [blame] | 12344 | // And we need to make sure at this point that all other activities |
| 12345 | // are made visible with the correct configuration. |
| 12346 | mMainStack.ensureActivitiesVisibleLocked(starting, changes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12347 | } |
| 12348 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 12349 | if (values != null && mWindowManager != null) { |
| 12350 | mWindowManager.setNewConfiguration(mConfiguration); |
| 12351 | } |
| 12352 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12353 | return kept; |
| 12354 | } |
Joe Onorato | 54a4a41 | 2011-11-02 20:50:08 -0700 | [diff] [blame] | 12355 | |
| 12356 | /** |
| 12357 | * Decide based on the configuration whether we should shouw the ANR, |
| 12358 | * crash, etc dialogs. The idea is that if there is no affordnace to |
| 12359 | * press the on-screen buttons, we shouldn't show the dialog. |
| 12360 | * |
| 12361 | * A thought: SystemUI might also want to get told about this, the Power |
| 12362 | * dialog / global actions also might want different behaviors. |
| 12363 | */ |
| 12364 | private static final boolean shouldShowDialogs(Configuration config) { |
| 12365 | return !(config.keyboard == Configuration.KEYBOARD_NOKEYS |
| 12366 | && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH); |
| 12367 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12368 | |
| 12369 | /** |
| 12370 | * Save the locale. You must be inside a synchronized (this) block. |
| 12371 | */ |
| 12372 | private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) { |
| 12373 | if(isDiff) { |
| 12374 | SystemProperties.set("user.language", l.getLanguage()); |
| 12375 | SystemProperties.set("user.region", l.getCountry()); |
| 12376 | } |
| 12377 | |
| 12378 | if(isPersist) { |
| 12379 | SystemProperties.set("persist.sys.language", l.getLanguage()); |
| 12380 | SystemProperties.set("persist.sys.country", l.getCountry()); |
| 12381 | SystemProperties.set("persist.sys.localevar", l.getVariant()); |
| 12382 | } |
| 12383 | } |
| 12384 | |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12385 | @Override |
| 12386 | public boolean targetTaskAffinityMatchesActivity(IBinder token, String destAffinity) { |
| 12387 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 12388 | return srec != null && srec.task.affinity != null && |
| 12389 | srec.task.affinity.equals(destAffinity); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12390 | } |
| 12391 | |
| 12392 | public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode, |
| 12393 | Intent resultData) { |
| 12394 | ComponentName dest = destIntent.getComponent(); |
| 12395 | |
| 12396 | synchronized (this) { |
| 12397 | ActivityRecord srec = ActivityRecord.forToken(token); |
Adam Powell | b71a5bc | 2012-04-24 14:20:57 -0700 | [diff] [blame] | 12398 | if (srec == null) { |
| 12399 | return false; |
| 12400 | } |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12401 | ArrayList<ActivityRecord> history = srec.stack.mHistory; |
| 12402 | final int start = history.indexOf(srec); |
| 12403 | if (start < 0) { |
| 12404 | // Current activity is not in history stack; do nothing. |
| 12405 | return false; |
| 12406 | } |
| 12407 | int finishTo = start - 1; |
| 12408 | ActivityRecord parent = null; |
| 12409 | boolean foundParentInTask = false; |
| 12410 | if (dest != null) { |
| 12411 | TaskRecord tr = srec.task; |
| 12412 | for (int i = start - 1; i >= 0; i--) { |
| 12413 | ActivityRecord r = history.get(i); |
| 12414 | if (tr != r.task) { |
| 12415 | // Couldn't find parent in the same task; stop at the one above this. |
| 12416 | // (Root of current task; in-app "home" behavior) |
| 12417 | // Always at least finish the current activity. |
| 12418 | finishTo = Math.min(start - 1, i + 1); |
| 12419 | parent = history.get(finishTo); |
| 12420 | break; |
| 12421 | } else if (r.info.packageName.equals(dest.getPackageName()) && |
| 12422 | r.info.name.equals(dest.getClassName())) { |
| 12423 | finishTo = i; |
| 12424 | parent = r; |
| 12425 | foundParentInTask = true; |
| 12426 | break; |
| 12427 | } |
| 12428 | } |
| 12429 | } |
| 12430 | |
| 12431 | if (mController != null) { |
| 12432 | ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0); |
| 12433 | if (next != null) { |
| 12434 | // ask watcher if this is allowed |
| 12435 | boolean resumeOK = true; |
| 12436 | try { |
| 12437 | resumeOK = mController.activityResuming(next.packageName); |
| 12438 | } catch (RemoteException e) { |
| 12439 | mController = null; |
| 12440 | } |
| 12441 | |
| 12442 | if (!resumeOK) { |
| 12443 | return false; |
| 12444 | } |
| 12445 | } |
| 12446 | } |
| 12447 | final long origId = Binder.clearCallingIdentity(); |
| 12448 | for (int i = start; i > finishTo; i--) { |
| 12449 | ActivityRecord r = history.get(i); |
| 12450 | mMainStack.requestFinishActivityLocked(r.appToken, resultCode, resultData, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 12451 | "navigate-up", true); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12452 | // Only return the supplied result for the first activity finished |
| 12453 | resultCode = Activity.RESULT_CANCELED; |
| 12454 | resultData = null; |
| 12455 | } |
| 12456 | |
| 12457 | if (parent != null && foundParentInTask) { |
| 12458 | final int parentLaunchMode = parent.info.launchMode; |
| 12459 | final int destIntentFlags = destIntent.getFlags(); |
| 12460 | if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE || |
| 12461 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK || |
| 12462 | parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP || |
| 12463 | (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) { |
Adam Powell | 69de7e1 | 2012-05-07 18:42:24 -0700 | [diff] [blame] | 12464 | parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12465 | } else { |
| 12466 | try { |
| 12467 | ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo( |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 12468 | destIntent.getComponent(), 0, srec.userId); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12469 | int res = mMainStack.startActivityLocked(srec.app.thread, destIntent, |
| 12470 | null, aInfo, parent.appToken, null, |
| 12471 | 0, -1, parent.launchedFromUid, 0, null, true, null); |
| 12472 | foundParentInTask = res == ActivityManager.START_SUCCESS; |
| 12473 | } catch (RemoteException e) { |
| 12474 | foundParentInTask = false; |
| 12475 | } |
| 12476 | mMainStack.requestFinishActivityLocked(parent.appToken, resultCode, |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 12477 | resultData, "navigate-up", true); |
Adam Powell | dd8fab2 | 2012-03-22 17:47:27 -0700 | [diff] [blame] | 12478 | } |
| 12479 | } |
| 12480 | Binder.restoreCallingIdentity(origId); |
| 12481 | return foundParentInTask; |
| 12482 | } |
| 12483 | } |
| 12484 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 12485 | public int getLaunchedFromUid(IBinder activityToken) { |
| 12486 | ActivityRecord srec = ActivityRecord.forToken(activityToken); |
| 12487 | if (srec == null) { |
| 12488 | return -1; |
| 12489 | } |
| 12490 | return srec.launchedFromUid; |
| 12491 | } |
| 12492 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12493 | // ========================================================= |
| 12494 | // LIFETIME MANAGEMENT |
| 12495 | // ========================================================= |
| 12496 | |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12497 | // Returns which broadcast queue the app is the current [or imminent] receiver |
| 12498 | // on, or 'null' if the app is not an active broadcast recipient. |
| 12499 | private BroadcastQueue isReceivingBroadcast(ProcessRecord app) { |
| 12500 | BroadcastRecord r = app.curReceiver; |
| 12501 | if (r != null) { |
| 12502 | return r.queue; |
| 12503 | } |
| 12504 | |
| 12505 | // It's not the current receiver, but it might be starting up to become one |
| 12506 | synchronized (this) { |
| 12507 | for (BroadcastQueue queue : mBroadcastQueues) { |
| 12508 | r = queue.mPendingBroadcast; |
| 12509 | if (r != null && r.curApp == app) { |
| 12510 | // found it; report which queue it's in |
| 12511 | return queue; |
| 12512 | } |
| 12513 | } |
| 12514 | } |
| 12515 | |
| 12516 | return null; |
| 12517 | } |
| 12518 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12519 | private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj, int clientHiddenAdj, |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12520 | int emptyAdj, ProcessRecord TOP_APP, boolean recursed, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12521 | if (mAdjSeq == app.adjSeq) { |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12522 | // This adjustment has already been computed. If we are calling |
| 12523 | // from the top, we may have already computed our adjustment with |
| 12524 | // an earlier hidden adjustment that isn't really for us... if |
| 12525 | // so, use the new hidden adjustment. |
| 12526 | if (!recursed && app.hidden) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12527 | if (app.hasActivities) { |
| 12528 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = hiddenAdj; |
| 12529 | } else if (app.hasClientActivities) { |
| 12530 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = clientHiddenAdj; |
| 12531 | } else { |
| 12532 | app.curAdj = app.curRawAdj = app.nonStoppingAdj = emptyAdj; |
| 12533 | } |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12534 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12535 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12536 | } |
| 12537 | |
| 12538 | if (app.thread == null) { |
| 12539 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12540 | app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12541 | 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] | 12542 | } |
| 12543 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12544 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN; |
| 12545 | app.adjSource = null; |
| 12546 | app.adjTarget = null; |
| 12547 | app.empty = false; |
| 12548 | app.hidden = false; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12549 | app.hasClientActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12550 | |
| 12551 | final int activitiesSize = app.activities.size(); |
| 12552 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12553 | if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12554 | // The max adjustment doesn't allow this app to be anything |
| 12555 | // below foreground, so it is not worth doing work for it. |
| 12556 | app.adjType = "fixed"; |
| 12557 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12558 | app.curRawAdj = app.nonStoppingAdj = app.maxAdj; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12559 | app.hasActivities = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12560 | app.foregroundActivities = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12561 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12562 | app.curSchedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12563 | // System process can do UI, and when they do we want to have |
| 12564 | // them trim their memory after the user leaves the UI. To |
| 12565 | // facilitate this, here we need to determine whether or not it |
| 12566 | // is currently showing UI. |
| 12567 | app.systemNoUi = true; |
| 12568 | if (app == TOP_APP) { |
| 12569 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12570 | app.hasActivities = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12571 | } else if (activitiesSize > 0) { |
| 12572 | for (int j = 0; j < activitiesSize; j++) { |
| 12573 | final ActivityRecord r = app.activities.get(j); |
| 12574 | if (r.visible) { |
| 12575 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12576 | } |
| 12577 | if (r.app == app) { |
| 12578 | app.hasActivities = true; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12579 | } |
| 12580 | } |
| 12581 | } |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12582 | return (app.curAdj=app.maxAdj); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12583 | } |
| 12584 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12585 | app.keeping = false; |
| 12586 | app.systemNoUi = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12587 | app.hasActivities = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12588 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12589 | // Determine the importance of the process, starting with most |
| 12590 | // important to least, and assign an appropriate OOM adjustment. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12591 | int adj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12592 | int schedGroup; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12593 | boolean foregroundActivities = false; |
| 12594 | boolean interesting = false; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12595 | BroadcastQueue queue; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12596 | if (app == TOP_APP) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12597 | // The last app on the list is the foreground app. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12598 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12599 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12600 | app.adjType = "top-activity"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12601 | foregroundActivities = true; |
| 12602 | interesting = true; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12603 | app.hasActivities = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12604 | } else if (app.instrumentationClass != null) { |
| 12605 | // Don't want to kill running instrumentation. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12606 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12607 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 12608 | app.adjType = "instrumentation"; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12609 | interesting = true; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12610 | } else if ((queue = isReceivingBroadcast(app)) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12611 | // An app that is currently receiving a broadcast also |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12612 | // counts as being in the foreground for OOM killer purposes. |
| 12613 | // It's placed in a sched group based on the nature of the |
| 12614 | // broadcast as reflected by which queue it's active in. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12615 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 12616 | schedGroup = (queue == mFgBroadcastQueue) |
| 12617 | ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12618 | app.adjType = "broadcast"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12619 | } else if (app.executingServices.size() > 0) { |
| 12620 | // An app that is currently executing a service callback also |
| 12621 | // counts as being in the foreground. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12622 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12623 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12624 | app.adjType = "exec-service"; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12625 | } else { |
| 12626 | // Assume process is hidden (has activities); we will correct |
| 12627 | // later if this is not the case. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12628 | adj = hiddenAdj; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12629 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12630 | app.hidden = true; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12631 | app.adjType = "bg-act"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12632 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12633 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12634 | boolean hasStoppingActivities = false; |
| 12635 | |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12636 | // Examine all activities if not already foreground. |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12637 | if (!foregroundActivities && activitiesSize > 0) { |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12638 | for (int j = 0; j < activitiesSize; j++) { |
| 12639 | final ActivityRecord r = app.activities.get(j); |
| 12640 | if (r.visible) { |
| 12641 | // App has a visible activity; only upgrade adjustment. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12642 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 12643 | adj = ProcessList.VISIBLE_APP_ADJ; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12644 | app.adjType = "visible"; |
| 12645 | } |
| 12646 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12647 | app.hidden = false; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12648 | app.hasActivities = true; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12649 | foregroundActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12650 | break; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12651 | } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12652 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12653 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12654 | app.adjType = "pausing"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12655 | } |
Dianne Hackborn | 8bf0aa9 | 2011-11-29 13:54:43 -0800 | [diff] [blame] | 12656 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12657 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12658 | } else if (r.state == ActivityState.STOPPING) { |
| 12659 | // We will apply the actual adjustment later, because |
| 12660 | // we want to allow this process to immediately go through |
| 12661 | // any memory trimming that is in effect. |
| 12662 | app.hidden = false; |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12663 | foregroundActivities = true; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12664 | hasStoppingActivities = true; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12665 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12666 | if (r.app == app) { |
| 12667 | app.hasActivities = true; |
| 12668 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12669 | } |
| 12670 | } |
| 12671 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12672 | if (adj == hiddenAdj && !app.hasActivities) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12673 | if (app.hasClientActivities) { |
| 12674 | adj = clientHiddenAdj; |
| 12675 | app.adjType = "bg-client-act"; |
| 12676 | } else { |
| 12677 | // Whoops, this process is completely empty as far as we know |
| 12678 | // at this point. |
| 12679 | adj = emptyAdj; |
| 12680 | app.empty = true; |
| 12681 | app.adjType = "bg-empty"; |
| 12682 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12683 | } |
| 12684 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12685 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12686 | if (app.foregroundServices) { |
| 12687 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12688 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12689 | app.hidden = false; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12690 | app.adjType = "fg-service"; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12691 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12692 | } else if (app.forcingToForeground != null) { |
| 12693 | // The user is aware of this app, so make it visible. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12694 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12695 | app.hidden = false; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12696 | app.adjType = "force-fg"; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12697 | app.adjSource = app.forcingToForeground; |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12698 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12699 | } |
| 12700 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 12701 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 12702 | if (app.foregroundServices) { |
| 12703 | interesting = true; |
| 12704 | } |
| 12705 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12706 | if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12707 | // We don't want to kill the current heavy-weight process. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12708 | adj = ProcessList.HEAVY_WEIGHT_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12709 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12710 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12711 | app.adjType = "heavy"; |
| 12712 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12713 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12714 | if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) { |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12715 | // This process is hosting what we currently consider to be the |
| 12716 | // 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] | 12717 | adj = ProcessList.HOME_APP_ADJ; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12718 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12719 | app.hidden = false; |
Dianne Hackborn | 83a6f45 | 2011-01-27 17:17:19 -0800 | [diff] [blame] | 12720 | app.adjType = "home"; |
| 12721 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12722 | |
Dianne Hackborn | f35fe23 | 2011-11-01 19:25:20 -0700 | [diff] [blame] | 12723 | if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess |
| 12724 | && app.activities.size() > 0) { |
| 12725 | // This was the previous process that showed UI to the user. |
| 12726 | // We want to try to keep it around more aggressively, to give |
| 12727 | // a good experience around switching between two apps. |
| 12728 | adj = ProcessList.PREVIOUS_APP_ADJ; |
| 12729 | schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE; |
| 12730 | app.hidden = false; |
| 12731 | app.adjType = "previous"; |
| 12732 | } |
| 12733 | |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12734 | if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj |
| 12735 | + " reason=" + app.adjType); |
| 12736 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 12737 | // 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] | 12738 | // there are applications dependent on our services or providers, but |
| 12739 | // this gives us a baseline and makes sure we don't get into an |
| 12740 | // infinite recursion. |
| 12741 | app.adjSeq = mAdjSeq; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 12742 | app.curRawAdj = app.nonStoppingAdj = adj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12743 | |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12744 | if (mBackupTarget != null && app == mBackupTarget.app) { |
| 12745 | // 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] | 12746 | if (adj > ProcessList.BACKUP_APP_ADJ) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 12747 | if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12748 | adj = ProcessList.BACKUP_APP_ADJ; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 12749 | app.adjType = "backup"; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12750 | app.hidden = false; |
Christopher Tate | 6fa9597 | 2009-06-05 18:43:55 -0700 | [diff] [blame] | 12751 | } |
| 12752 | } |
| 12753 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12754 | if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12755 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12756 | final long now = SystemClock.uptimeMillis(); |
| 12757 | // This process is more important if the top activity is |
| 12758 | // bound to the service. |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12759 | Iterator<ServiceRecord> jt = app.services.iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12760 | while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12761 | ServiceRecord s = jt.next(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12762 | if (s.startRequested) { |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12763 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12764 | // If this process has shown some UI, let it immediately |
| 12765 | // go to the LRU list because it may be pretty heavy with |
| 12766 | // UI stuff. We'll tag it with a label just to help |
| 12767 | // debug and understand what is going on. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12768 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12769 | app.adjType = "started-bg-ui-services"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12770 | } |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12771 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 12772 | if (now < (s.lastActivity + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12773 | // This service has seen some activity within |
| 12774 | // recent memory, so we will keep its process ahead |
| 12775 | // of the background processes. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12776 | if (adj > ProcessList.SERVICE_ADJ) { |
| 12777 | adj = ProcessList.SERVICE_ADJ; |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12778 | app.adjType = "started-services"; |
| 12779 | app.hidden = false; |
| 12780 | } |
| 12781 | } |
| 12782 | // If we have let the service slide into the background |
| 12783 | // state, still have some text describing what it is doing |
| 12784 | // even though the service no longer has an impact. |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 12785 | if (adj > ProcessList.SERVICE_ADJ) { |
Dianne Hackborn | f0754f5b | 2011-07-21 16:02:07 -0700 | [diff] [blame] | 12786 | app.adjType = "started-bg-services"; |
| 12787 | } |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 12788 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12789 | // Don't kill this process because it is doing work; it |
| 12790 | // has said it is doing work. |
| 12791 | app.keeping = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12792 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12793 | if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12794 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12795 | Iterator<ArrayList<ConnectionRecord>> kt |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12796 | = s.connections.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12797 | while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12798 | ArrayList<ConnectionRecord> clist = kt.next(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12799 | 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] | 12800 | // XXX should compute this based on the max of |
| 12801 | // all connected clients. |
| 12802 | ConnectionRecord cr = clist.get(i); |
| 12803 | if (cr.binding.client == app) { |
| 12804 | // Binding to ourself is not interesting. |
| 12805 | continue; |
| 12806 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12807 | if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12808 | ProcessRecord client = cr.binding.client; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12809 | int clientAdj = adj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12810 | int myHiddenAdj = hiddenAdj; |
| 12811 | if (myHiddenAdj > client.hiddenAdj) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12812 | if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12813 | myHiddenAdj = client.hiddenAdj; |
| 12814 | } else { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12815 | myHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12816 | } |
| 12817 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12818 | int myClientHiddenAdj = clientHiddenAdj; |
| 12819 | if (myClientHiddenAdj > client.clientHiddenAdj) { |
| 12820 | if (client.clientHiddenAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 12821 | myClientHiddenAdj = client.clientHiddenAdj; |
| 12822 | } else { |
| 12823 | myClientHiddenAdj = ProcessList.VISIBLE_APP_ADJ; |
| 12824 | } |
| 12825 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12826 | int myEmptyAdj = emptyAdj; |
| 12827 | if (myEmptyAdj > client.emptyAdj) { |
| 12828 | if (client.emptyAdj >= ProcessList.VISIBLE_APP_ADJ) { |
| 12829 | myEmptyAdj = client.emptyAdj; |
| 12830 | } else { |
| 12831 | myEmptyAdj = ProcessList.VISIBLE_APP_ADJ; |
| 12832 | } |
| 12833 | } |
| 12834 | clientAdj = computeOomAdjLocked(client, myHiddenAdj, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12835 | myClientHiddenAdj, myEmptyAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12836 | String adjType = null; |
| 12837 | if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) { |
| 12838 | // Not doing bind OOM management, so treat |
| 12839 | // this guy more like a started service. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12840 | if (app.hasShownUi && app != mHomeProcess) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12841 | // If this process has shown some UI, let it immediately |
| 12842 | // go to the LRU list because it may be pretty heavy with |
| 12843 | // UI stuff. We'll tag it with a label just to help |
| 12844 | // debug and understand what is going on. |
| 12845 | if (adj > clientAdj) { |
| 12846 | adjType = "bound-bg-ui-services"; |
| 12847 | } |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12848 | app.hidden = false; |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12849 | clientAdj = adj; |
| 12850 | } else { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 12851 | if (now >= (s.lastActivity |
| 12852 | + ActiveServices.MAX_SERVICE_INACTIVITY)) { |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12853 | // This service has not seen activity within |
| 12854 | // recent memory, so allow it to drop to the |
| 12855 | // LRU list if there is no other reason to keep |
| 12856 | // it around. We'll also tag it with a label just |
| 12857 | // to help debug and undertand what is going on. |
| 12858 | if (adj > clientAdj) { |
| 12859 | adjType = "bound-bg-services"; |
| 12860 | } |
| 12861 | clientAdj = adj; |
| 12862 | } |
| 12863 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12864 | } else if ((cr.flags&Context.BIND_AUTO_CREATE) != 0) { |
| 12865 | if ((cr.flags&Context.BIND_NOT_VISIBLE) == 0) { |
| 12866 | // If this connection is keeping the service |
| 12867 | // created, then we want to try to better follow |
| 12868 | // its memory management semantics for activities. |
| 12869 | // That is, if it is sitting in the background |
| 12870 | // LRU list as a hidden process (with activities), |
| 12871 | // we don't want the service it is connected to |
| 12872 | // to go into the empty LRU and quickly get killed, |
| 12873 | // because I'll we'll do is just end up restarting |
| 12874 | // the service. |
| 12875 | app.hasClientActivities |= client.hasActivities; |
| 12876 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12877 | } |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12878 | if (adj > clientAdj) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12879 | // If this process has recently shown UI, and |
| 12880 | // the process that is binding to it is less |
| 12881 | // important than being visible, then we don't |
| 12882 | // care about the binding as much as we care |
| 12883 | // about letting this process get into the LRU |
| 12884 | // list to be killed and restarted if needed for |
| 12885 | // memory. |
Dianne Hackborn | 98cfebc | 2011-10-18 13:17:33 -0700 | [diff] [blame] | 12886 | if (app.hasShownUi && app != mHomeProcess |
| 12887 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12888 | adjType = "bound-bg-ui-services"; |
| 12889 | } else { |
| 12890 | if ((cr.flags&(Context.BIND_ABOVE_CLIENT |
| 12891 | |Context.BIND_IMPORTANT)) != 0) { |
| 12892 | adj = clientAdj; |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 12893 | } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0 |
| 12894 | && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ |
| 12895 | && adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 12896 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 12897 | } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12898 | adj = clientAdj; |
| 12899 | } else { |
Dianne Hackborn | 2c84cfc | 2011-10-31 15:39:59 -0700 | [diff] [blame] | 12900 | app.pendingUiClean = true; |
| 12901 | if (adj > ProcessList.VISIBLE_APP_ADJ) { |
| 12902 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 12903 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12904 | } |
| 12905 | if (!client.hidden) { |
| 12906 | app.hidden = false; |
| 12907 | } |
| 12908 | if (client.keeping) { |
| 12909 | app.keeping = true; |
| 12910 | } |
| 12911 | adjType = "service"; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12912 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 12913 | } |
| 12914 | if (adjType != null) { |
| 12915 | app.adjType = adjType; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12916 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12917 | .REASON_SERVICE_IN_USE; |
| 12918 | app.adjSource = cr.binding.client; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 12919 | app.adjSourceOom = clientAdj; |
Dianne Hackborn | 43d9ac8 | 2010-08-25 15:06:25 -0700 | [diff] [blame] | 12920 | app.adjTarget = s.name; |
| 12921 | } |
| 12922 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 12923 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 12924 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12925 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12926 | } |
| 12927 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12928 | final ActivityRecord a = cr.activity; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12929 | if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12930 | if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ && |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12931 | (a.visible || a.state == ActivityState.RESUMED |
| 12932 | || a.state == ActivityState.PAUSING)) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12933 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12934 | if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) { |
| 12935 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 12936 | } |
| 12937 | app.hidden = false; |
| 12938 | app.adjType = "service"; |
| 12939 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 12940 | .REASON_SERVICE_IN_USE; |
| 12941 | app.adjSource = a; |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 12942 | app.adjSourceOom = adj; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 12943 | app.adjTarget = s.name; |
| 12944 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12945 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12946 | } |
| 12947 | } |
| 12948 | } |
| 12949 | } |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12950 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 12951 | // Finally, if this process has active services running in it, we |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12952 | // would like to avoid killing it unless it would prevent the current |
| 12953 | // application from running. By default we put the process in |
| 12954 | // with the rest of the background processes; as we scan through |
| 12955 | // its services we may bump it up from there. |
| 12956 | if (adj > hiddenAdj) { |
| 12957 | adj = hiddenAdj; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 12958 | app.hidden = false; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 12959 | app.adjType = "bg-services"; |
| 12960 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12961 | } |
| 12962 | |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12963 | if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12964 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12965 | Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 12966 | while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 12967 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) { |
Dianne Hackborn | 860755f | 2010-06-03 18:47:52 -0700 | [diff] [blame] | 12968 | ContentProviderRecord cpr = jt.next(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12969 | for (int i = cpr.connections.size()-1; |
| 12970 | i >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ |
| 12971 | || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE); |
| 12972 | i--) { |
| 12973 | ContentProviderConnection conn = cpr.connections.get(i); |
| 12974 | ProcessRecord client = conn.client; |
| 12975 | if (client == app) { |
| 12976 | // Being our own client is not interesting. |
| 12977 | continue; |
| 12978 | } |
| 12979 | int myHiddenAdj = hiddenAdj; |
| 12980 | if (myHiddenAdj > client.hiddenAdj) { |
| 12981 | if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) { |
| 12982 | myHiddenAdj = client.hiddenAdj; |
| 12983 | } else { |
| 12984 | myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 12985 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 12986 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 12987 | int myClientHiddenAdj = clientHiddenAdj; |
| 12988 | if (myClientHiddenAdj > client.clientHiddenAdj) { |
| 12989 | if (client.clientHiddenAdj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 12990 | myClientHiddenAdj = client.clientHiddenAdj; |
| 12991 | } else { |
| 12992 | myClientHiddenAdj = ProcessList.FOREGROUND_APP_ADJ; |
| 12993 | } |
| 12994 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 12995 | int myEmptyAdj = emptyAdj; |
| 12996 | if (myEmptyAdj > client.emptyAdj) { |
| 12997 | if (client.emptyAdj > ProcessList.FOREGROUND_APP_ADJ) { |
| 12998 | myEmptyAdj = client.emptyAdj; |
| 12999 | } else { |
| 13000 | myEmptyAdj = ProcessList.FOREGROUND_APP_ADJ; |
| 13001 | } |
| 13002 | } |
| 13003 | int clientAdj = computeOomAdjLocked(client, myHiddenAdj, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13004 | myClientHiddenAdj, myEmptyAdj, TOP_APP, true, doingAll); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13005 | if (adj > clientAdj) { |
| 13006 | if (app.hasShownUi && app != mHomeProcess |
| 13007 | && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13008 | app.adjType = "bg-ui-provider"; |
| 13009 | } else { |
| 13010 | adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ |
| 13011 | ? clientAdj : ProcessList.FOREGROUND_APP_ADJ; |
| 13012 | app.adjType = "provider"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13013 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13014 | if (!client.hidden) { |
| 13015 | app.hidden = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13016 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13017 | if (client.keeping) { |
| 13018 | app.keeping = true; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13019 | } |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 13020 | app.adjTypeCode = ActivityManager.RunningAppProcessInfo |
| 13021 | .REASON_PROVIDER_IN_USE; |
| 13022 | app.adjSource = client; |
| 13023 | app.adjSourceOom = clientAdj; |
| 13024 | app.adjTarget = cpr.name; |
| 13025 | } |
| 13026 | if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) { |
| 13027 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13028 | } |
| 13029 | } |
| 13030 | // If the provider has external (non-framework) process |
| 13031 | // dependencies, ensure that its adjustment is at least |
| 13032 | // FOREGROUND_APP_ADJ. |
Svetoslav Ganov | 25872aa | 2012-02-03 19:19:09 -0800 | [diff] [blame] | 13033 | if (cpr.hasExternalProcessHandles()) { |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13034 | if (adj > ProcessList.FOREGROUND_APP_ADJ) { |
| 13035 | adj = ProcessList.FOREGROUND_APP_ADJ; |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13036 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13037 | app.hidden = false; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13038 | app.keeping = true; |
Dianne Hackborn | de42bb6 | 2009-08-05 12:26:15 -0700 | [diff] [blame] | 13039 | app.adjType = "provider"; |
Dianne Hackborn | b7bb3b3 | 2010-06-06 22:47:50 -0700 | [diff] [blame] | 13040 | app.adjTarget = cpr.name; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13041 | } |
| 13042 | } |
| 13043 | } |
| 13044 | } |
| 13045 | |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13046 | if (adj == ProcessList.SERVICE_ADJ) { |
| 13047 | if (doingAll) { |
| 13048 | app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3); |
| 13049 | mNewNumServiceProcs++; |
| 13050 | } |
| 13051 | if (app.serviceb) { |
| 13052 | adj = ProcessList.SERVICE_B_ADJ; |
| 13053 | } |
| 13054 | } else { |
| 13055 | app.serviceb = false; |
| 13056 | } |
| 13057 | |
| 13058 | app.nonStoppingAdj = adj; |
| 13059 | |
| 13060 | if (hasStoppingActivities) { |
| 13061 | // Only upgrade adjustment. |
| 13062 | if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13063 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13064 | app.adjType = "stopping"; |
| 13065 | } |
| 13066 | } |
| 13067 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13068 | app.curRawAdj = adj; |
| 13069 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13070 | //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13071 | // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj); |
| 13072 | if (adj > app.maxAdj) { |
| 13073 | adj = app.maxAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13074 | if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) { |
Dianne Hackborn | 09c916b | 2009-12-08 14:50:51 -0800 | [diff] [blame] | 13075 | schedGroup = Process.THREAD_GROUP_DEFAULT; |
| 13076 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13077 | } |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13078 | if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13079 | app.keeping = true; |
| 13080 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13081 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13082 | if (app.hasAboveClient) { |
| 13083 | // If this process has bound to any services with BIND_ABOVE_CLIENT, |
| 13084 | // then we need to drop its adjustment to be lower than the service's |
| 13085 | // in order to honor the request. We want to drop it by one adjustment |
| 13086 | // level... but there is special meaning applied to various levels so |
| 13087 | // we will skip some of them. |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13088 | if (adj < ProcessList.FOREGROUND_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13089 | // System process will not get dropped, ever |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13090 | } else if (adj < ProcessList.VISIBLE_APP_ADJ) { |
| 13091 | adj = ProcessList.VISIBLE_APP_ADJ; |
| 13092 | } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13093 | adj = ProcessList.PERCEPTIBLE_APP_ADJ; |
| 13094 | } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13095 | adj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13096 | } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13097 | adj++; |
| 13098 | } |
| 13099 | } |
| 13100 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 13101 | int importance = app.memImportance; |
| 13102 | if (importance == 0 || adj != app.curAdj || schedGroup != app.curSchedGroup) { |
| 13103 | app.curAdj = adj; |
| 13104 | app.curSchedGroup = schedGroup; |
| 13105 | if (!interesting) { |
| 13106 | // For this reporting, if there is not something explicitly |
| 13107 | // interesting in this process then we will push it to the |
| 13108 | // background importance. |
| 13109 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 13110 | } else if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13111 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 13112 | } else if (adj >= ProcessList.SERVICE_B_ADJ) { |
| 13113 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 13114 | } else if (adj >= ProcessList.HOME_APP_ADJ) { |
| 13115 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; |
| 13116 | } else if (adj >= ProcessList.SERVICE_ADJ) { |
| 13117 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; |
| 13118 | } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
| 13119 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE; |
| 13120 | } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) { |
| 13121 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE; |
| 13122 | } else if (adj >= ProcessList.VISIBLE_APP_ADJ) { |
| 13123 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 13124 | } else if (adj >= ProcessList.FOREGROUND_APP_ADJ) { |
| 13125 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 13126 | } else { |
| 13127 | importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERSISTENT; |
| 13128 | } |
| 13129 | } |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13130 | |
Dianne Hackborn | a93c2c1 | 2012-05-31 15:29:36 -0700 | [diff] [blame] | 13131 | int changes = importance != app.memImportance ? ProcessChangeItem.CHANGE_IMPORTANCE : 0; |
| 13132 | if (foregroundActivities != app.foregroundActivities) { |
| 13133 | changes |= ProcessChangeItem.CHANGE_ACTIVITIES; |
| 13134 | } |
| 13135 | if (changes != 0) { |
| 13136 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Changes in " + app + ": " + changes); |
| 13137 | app.memImportance = importance; |
| 13138 | app.foregroundActivities = foregroundActivities; |
| 13139 | int i = mPendingProcessChanges.size()-1; |
| 13140 | ProcessChangeItem item = null; |
| 13141 | while (i >= 0) { |
| 13142 | item = mPendingProcessChanges.get(i); |
| 13143 | if (item.pid == app.pid) { |
| 13144 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Re-using existing item: " + item); |
| 13145 | break; |
| 13146 | } |
| 13147 | i--; |
| 13148 | } |
| 13149 | if (i < 0) { |
| 13150 | // No existing item in pending changes; need a new one. |
| 13151 | final int NA = mAvailProcessChanges.size(); |
| 13152 | if (NA > 0) { |
| 13153 | item = mAvailProcessChanges.remove(NA-1); |
| 13154 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Retreiving available item: " + item); |
| 13155 | } else { |
| 13156 | item = new ProcessChangeItem(); |
| 13157 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Allocating new item: " + item); |
| 13158 | } |
| 13159 | item.changes = 0; |
| 13160 | item.pid = app.pid; |
| 13161 | item.uid = app.info.uid; |
| 13162 | if (mPendingProcessChanges.size() == 0) { |
| 13163 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, |
| 13164 | "*** Enqueueing dispatch processes changed!"); |
| 13165 | mHandler.obtainMessage(DISPATCH_PROCESSES_CHANGED).sendToTarget(); |
| 13166 | } |
| 13167 | mPendingProcessChanges.add(item); |
| 13168 | } |
| 13169 | item.changes |= changes; |
| 13170 | item.importance = importance; |
| 13171 | item.foregroundActivities = foregroundActivities; |
| 13172 | if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Item " |
| 13173 | + Integer.toHexString(System.identityHashCode(item)) |
| 13174 | + " " + app.toShortString() + ": changes=" + item.changes |
| 13175 | + " importance=" + item.importance |
| 13176 | + " foreground=" + item.foregroundActivities |
| 13177 | + " type=" + app.adjType + " source=" + app.adjSource |
| 13178 | + " target=" + app.adjTarget); |
Jeff Sharkey | d5cdd59 | 2011-05-03 20:27:17 -0700 | [diff] [blame] | 13179 | } |
| 13180 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13181 | return app.curRawAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13182 | } |
| 13183 | |
| 13184 | /** |
| 13185 | * Ask a given process to GC right now. |
| 13186 | */ |
| 13187 | final void performAppGcLocked(ProcessRecord app) { |
| 13188 | try { |
| 13189 | app.lastRequestedGc = SystemClock.uptimeMillis(); |
| 13190 | if (app.thread != null) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13191 | if (app.reportLowMemory) { |
| 13192 | app.reportLowMemory = false; |
| 13193 | app.thread.scheduleLowMemory(); |
| 13194 | } else { |
| 13195 | app.thread.processInBackground(); |
| 13196 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13197 | } |
| 13198 | } catch (Exception e) { |
| 13199 | // whatever. |
| 13200 | } |
| 13201 | } |
| 13202 | |
| 13203 | /** |
| 13204 | * Returns true if things are idle enough to perform GCs. |
| 13205 | */ |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13206 | private final boolean canGcNowLocked() { |
Christopher Tate | f46723b | 2012-01-26 14:19:24 -0800 | [diff] [blame] | 13207 | boolean processingBroadcasts = false; |
| 13208 | for (BroadcastQueue q : mBroadcastQueues) { |
| 13209 | if (q.mParallelBroadcasts.size() != 0 || q.mOrderedBroadcasts.size() != 0) { |
| 13210 | processingBroadcasts = true; |
| 13211 | } |
| 13212 | } |
| 13213 | return !processingBroadcasts |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13214 | && (mSleeping || (mMainStack.mResumedActivity != null && |
| 13215 | mMainStack.mResumedActivity.idle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13216 | } |
| 13217 | |
| 13218 | /** |
| 13219 | * Perform GCs on all processes that are waiting for it, but only |
| 13220 | * if things are idle. |
| 13221 | */ |
| 13222 | final void performAppGcsLocked() { |
| 13223 | final int N = mProcessesToGc.size(); |
| 13224 | if (N <= 0) { |
| 13225 | return; |
| 13226 | } |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13227 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13228 | while (mProcessesToGc.size() > 0) { |
| 13229 | ProcessRecord proc = mProcessesToGc.remove(0); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13230 | if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13231 | if ((proc.lastRequestedGc+GC_MIN_INTERVAL) |
| 13232 | <= SystemClock.uptimeMillis()) { |
| 13233 | // To avoid spamming the system, we will GC processes one |
| 13234 | // at a time, waiting a few seconds between each. |
| 13235 | performAppGcLocked(proc); |
| 13236 | scheduleAppGcsLocked(); |
| 13237 | return; |
| 13238 | } else { |
| 13239 | // It hasn't been long enough since we last GCed this |
| 13240 | // process... put it in the list to wait for its time. |
| 13241 | addProcessToGcListLocked(proc); |
| 13242 | break; |
| 13243 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13244 | } |
| 13245 | } |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13246 | |
| 13247 | scheduleAppGcsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13248 | } |
| 13249 | } |
| 13250 | |
| 13251 | /** |
| 13252 | * If all looks good, perform GCs on all processes waiting for them. |
| 13253 | */ |
| 13254 | final void performAppGcsIfAppropriateLocked() { |
Josh Bartel | 7f20874 | 2010-02-25 11:01:44 -0600 | [diff] [blame] | 13255 | if (canGcNowLocked()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13256 | performAppGcsLocked(); |
| 13257 | return; |
| 13258 | } |
| 13259 | // Still not idle, wait some more. |
| 13260 | scheduleAppGcsLocked(); |
| 13261 | } |
| 13262 | |
| 13263 | /** |
| 13264 | * Schedule the execution of all pending app GCs. |
| 13265 | */ |
| 13266 | final void scheduleAppGcsLocked() { |
| 13267 | mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13268 | |
| 13269 | if (mProcessesToGc.size() > 0) { |
| 13270 | // Schedule a GC for the time to the next process. |
| 13271 | ProcessRecord proc = mProcessesToGc.get(0); |
| 13272 | Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG); |
| 13273 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13274 | long when = proc.lastRequestedGc + GC_MIN_INTERVAL; |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13275 | long now = SystemClock.uptimeMillis(); |
| 13276 | if (when < (now+GC_TIMEOUT)) { |
| 13277 | when = now + GC_TIMEOUT; |
| 13278 | } |
| 13279 | mHandler.sendMessageAtTime(msg, when); |
| 13280 | } |
| 13281 | } |
| 13282 | |
| 13283 | /** |
| 13284 | * Add a process to the array of processes waiting to be GCed. Keeps the |
| 13285 | * list in sorted order by the last GC time. The process can't already be |
| 13286 | * on the list. |
| 13287 | */ |
| 13288 | final void addProcessToGcListLocked(ProcessRecord proc) { |
| 13289 | boolean added = false; |
| 13290 | for (int i=mProcessesToGc.size()-1; i>=0; i--) { |
| 13291 | if (mProcessesToGc.get(i).lastRequestedGc < |
| 13292 | proc.lastRequestedGc) { |
| 13293 | added = true; |
| 13294 | mProcessesToGc.add(i+1, proc); |
| 13295 | break; |
| 13296 | } |
| 13297 | } |
| 13298 | if (!added) { |
| 13299 | mProcessesToGc.add(0, proc); |
| 13300 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13301 | } |
| 13302 | |
| 13303 | /** |
| 13304 | * Set up to ask a process to GC itself. This will either do it |
| 13305 | * immediately, or put it on the list of processes to gc the next |
| 13306 | * time things are idle. |
| 13307 | */ |
| 13308 | final void scheduleAppGcLocked(ProcessRecord app) { |
| 13309 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13310 | if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13311 | return; |
| 13312 | } |
| 13313 | if (!mProcessesToGc.contains(app)) { |
Dianne Hackborn | fd12af4 | 2009-08-27 00:44:33 -0700 | [diff] [blame] | 13314 | addProcessToGcListLocked(app); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13315 | scheduleAppGcsLocked(); |
| 13316 | } |
| 13317 | } |
| 13318 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13319 | final void checkExcessivePowerUsageLocked(boolean doKills) { |
| 13320 | updateCpuStatsNow(); |
| 13321 | |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13322 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13323 | boolean doWakeKills = doKills; |
| 13324 | boolean doCpuKills = doKills; |
| 13325 | if (mLastPowerCheckRealtime == 0) { |
| 13326 | doWakeKills = false; |
| 13327 | } |
| 13328 | if (mLastPowerCheckUptime == 0) { |
| 13329 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13330 | } |
| 13331 | if (stats.isScreenOn()) { |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13332 | doWakeKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13333 | } |
| 13334 | final long curRealtime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13335 | final long realtimeSince = curRealtime - mLastPowerCheckRealtime; |
| 13336 | final long curUptime = SystemClock.uptimeMillis(); |
| 13337 | final long uptimeSince = curUptime - mLastPowerCheckUptime; |
| 13338 | mLastPowerCheckRealtime = curRealtime; |
| 13339 | mLastPowerCheckUptime = curUptime; |
| 13340 | if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) { |
| 13341 | doWakeKills = false; |
| 13342 | } |
| 13343 | if (uptimeSince < CPU_MIN_CHECK_DURATION) { |
| 13344 | doCpuKills = false; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13345 | } |
| 13346 | int i = mLruProcesses.size(); |
| 13347 | while (i > 0) { |
| 13348 | i--; |
| 13349 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13350 | if (!app.keeping) { |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13351 | long wtime; |
| 13352 | synchronized (stats) { |
| 13353 | wtime = stats.getProcessWakeTime(app.info.uid, |
| 13354 | app.pid, curRealtime); |
| 13355 | } |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13356 | long wtimeUsed = wtime - app.lastWakeTime; |
| 13357 | long cputimeUsed = app.curCpuTime - app.lastCpuTime; |
| 13358 | if (DEBUG_POWER) { |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13359 | StringBuilder sb = new StringBuilder(128); |
| 13360 | sb.append("Wake for "); |
| 13361 | app.toShortString(sb); |
| 13362 | sb.append(": over "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13363 | TimeUtils.formatDuration(realtimeSince, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13364 | sb.append(" used "); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13365 | TimeUtils.formatDuration(wtimeUsed, sb); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13366 | sb.append(" ("); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13367 | sb.append((wtimeUsed*100)/realtimeSince); |
| 13368 | sb.append("%)"); |
| 13369 | Slog.i(TAG, sb.toString()); |
| 13370 | sb.setLength(0); |
| 13371 | sb.append("CPU for "); |
| 13372 | app.toShortString(sb); |
| 13373 | sb.append(": over "); |
| 13374 | TimeUtils.formatDuration(uptimeSince, sb); |
| 13375 | sb.append(" used "); |
| 13376 | TimeUtils.formatDuration(cputimeUsed, sb); |
| 13377 | sb.append(" ("); |
| 13378 | sb.append((cputimeUsed*100)/uptimeSince); |
Dianne Hackborn | 1ebccf5 | 2010-08-15 13:04:34 -0700 | [diff] [blame] | 13379 | sb.append("%)"); |
| 13380 | Slog.i(TAG, sb.toString()); |
| 13381 | } |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13382 | // If a process has held a wake lock for more |
| 13383 | // than 50% of the time during this period, |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13384 | // that sounds bad. Kill! |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13385 | if (doWakeKills && realtimeSince > 0 |
| 13386 | && ((wtimeUsed*100)/realtimeSince) >= 50) { |
| 13387 | synchronized (stats) { |
| 13388 | stats.reportExcessiveWakeLocked(app.info.uid, app.processName, |
| 13389 | realtimeSince, wtimeUsed); |
| 13390 | } |
| 13391 | Slog.w(TAG, "Excessive wake lock in " + app.processName |
| 13392 | + " (pid " + app.pid + "): held " + wtimeUsed |
| 13393 | + " during " + realtimeSince); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13394 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13395 | app.processName, app.setAdj, "excessive wake lock"); |
| 13396 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13397 | } else if (doCpuKills && uptimeSince > 0 |
| 13398 | && ((cputimeUsed*100)/uptimeSince) >= 50) { |
| 13399 | synchronized (stats) { |
| 13400 | stats.reportExcessiveCpuLocked(app.info.uid, app.processName, |
| 13401 | uptimeSince, cputimeUsed); |
| 13402 | } |
| 13403 | Slog.w(TAG, "Excessive CPU in " + app.processName |
| 13404 | + " (pid " + app.pid + "): used " + cputimeUsed |
| 13405 | + " during " + uptimeSince); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13406 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13407 | app.processName, app.setAdj, "excessive cpu"); |
| 13408 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13409 | } else { |
| 13410 | app.lastWakeTime = wtime; |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13411 | app.lastCpuTime = app.curCpuTime; |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13412 | } |
| 13413 | } |
| 13414 | } |
| 13415 | } |
| 13416 | |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13417 | private final boolean updateOomAdjLocked(ProcessRecord app, int hiddenAdj, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13418 | int clientHiddenAdj, int emptyAdj, ProcessRecord TOP_APP, boolean doingAll) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13419 | app.hiddenAdj = hiddenAdj; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13420 | app.clientHiddenAdj = clientHiddenAdj; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13421 | app.emptyAdj = emptyAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13422 | |
| 13423 | if (app.thread == null) { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13424 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13425 | } |
| 13426 | |
Dianne Hackborn | 287952c | 2010-09-22 22:34:31 -0700 | [diff] [blame] | 13427 | final boolean wasKeeping = app.keeping; |
| 13428 | |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13429 | boolean success = true; |
| 13430 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13431 | computeOomAdjLocked(app, hiddenAdj, clientHiddenAdj, emptyAdj, TOP_APP, false, doingAll); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13432 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13433 | if (app.curRawAdj != app.setRawAdj) { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13434 | if (wasKeeping && !app.keeping) { |
| 13435 | // This app is no longer something we want to keep. Note |
| 13436 | // its current wake lock time to later know to kill it if |
| 13437 | // it is not behaving well. |
| 13438 | BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); |
| 13439 | synchronized (stats) { |
| 13440 | app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, |
| 13441 | app.pid, SystemClock.elapsedRealtime()); |
| 13442 | } |
| 13443 | app.lastCpuTime = app.curCpuTime; |
| 13444 | } |
| 13445 | |
| 13446 | app.setRawAdj = app.curRawAdj; |
| 13447 | } |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13448 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13449 | if (app.curAdj != app.setAdj) { |
| 13450 | if (Process.setOomAdj(app.pid, app.curAdj)) { |
Dianne Hackborn | bbb09ac | 2011-11-30 11:31:29 -0800 | [diff] [blame] | 13451 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13452 | TAG, "Set " + app.pid + " " + app.processName + |
| 13453 | " adj " + app.curAdj + ": " + app.adjType); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13454 | app.setAdj = app.curAdj; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13455 | } else { |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13456 | success = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13457 | Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13458 | } |
| 13459 | } |
| 13460 | if (app.setSchedGroup != app.curSchedGroup) { |
| 13461 | app.setSchedGroup = app.curSchedGroup; |
| 13462 | if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, |
| 13463 | "Setting process group of " + app.processName |
| 13464 | + " to " + app.curSchedGroup); |
| 13465 | if (app.waitingToKill != null && |
| 13466 | app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { |
| 13467 | Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13468 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13469 | app.processName, app.setAdj, app.waitingToKill); |
Dianne Hackborn | 45a25bc | 2012-06-28 13:49:17 -0700 | [diff] [blame] | 13470 | app.killedBackground = true; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13471 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13472 | success = false; |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13473 | } else { |
| 13474 | if (true) { |
| 13475 | long oldId = Binder.clearCallingIdentity(); |
| 13476 | try { |
| 13477 | Process.setProcessGroup(app.pid, app.curSchedGroup); |
| 13478 | } catch (Exception e) { |
| 13479 | Slog.w(TAG, "Failed setting process group of " + app.pid |
| 13480 | + " to " + app.curSchedGroup); |
| 13481 | e.printStackTrace(); |
| 13482 | } finally { |
| 13483 | Binder.restoreCallingIdentity(oldId); |
Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 13484 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13485 | } else { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13486 | if (app.thread != null) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13487 | try { |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 13488 | app.thread.setSchedulingGroup(app.curSchedGroup); |
| 13489 | } catch (RemoteException e) { |
Dianne Hackborn | 06de2ea | 2009-05-21 12:56:43 -0700 | [diff] [blame] | 13490 | } |
| 13491 | } |
| 13492 | } |
| 13493 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13494 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13495 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13496 | } |
| 13497 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13498 | private final ActivityRecord resumedAppLocked() { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13499 | ActivityRecord resumedActivity = mMainStack.mResumedActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13500 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 621e2fe | 2012-02-16 17:07:33 -0800 | [diff] [blame] | 13501 | resumedActivity = mMainStack.mPausingActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13502 | if (resumedActivity == null || resumedActivity.app == null) { |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13503 | resumedActivity = mMainStack.topRunningActivityLocked(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13504 | } |
| 13505 | } |
| 13506 | return resumedActivity; |
| 13507 | } |
| 13508 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 13509 | final boolean updateOomAdjLocked(ProcessRecord app) { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13510 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13511 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
| 13512 | int curAdj = app.curAdj; |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13513 | final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13514 | && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13515 | |
| 13516 | mAdjSeq++; |
| 13517 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13518 | boolean success = updateOomAdjLocked(app, app.hiddenAdj, app.clientHiddenAdj, |
| 13519 | app.emptyAdj, TOP_APP, false); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13520 | final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13521 | && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13522 | if (nowHidden != wasHidden) { |
| 13523 | // Changed to/from hidden state, so apps after it in the LRU |
| 13524 | // list may also be changed. |
| 13525 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13526 | } |
Dianne Hackborn | 295e3c2 | 2011-08-25 13:19:08 -0700 | [diff] [blame] | 13527 | return success; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13528 | } |
| 13529 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13530 | final void updateOomAdjLocked() { |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 13531 | final ActivityRecord TOP_ACT = resumedAppLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13532 | final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13533 | final long oldTime = SystemClock.uptimeMillis() - ProcessList.MAX_EMPTY_TIME; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13534 | |
| 13535 | if (false) { |
| 13536 | RuntimeException e = new RuntimeException(); |
| 13537 | e.fillInStackTrace(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13538 | Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13539 | } |
| 13540 | |
| 13541 | mAdjSeq++; |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13542 | mNewNumServiceProcs = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13543 | |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13544 | final int emptyProcessLimit; |
| 13545 | final int hiddenProcessLimit; |
| 13546 | if (mProcessLimit <= 0) { |
| 13547 | emptyProcessLimit = hiddenProcessLimit = 0; |
| 13548 | } else if (mProcessLimit == 1) { |
| 13549 | emptyProcessLimit = 1; |
| 13550 | hiddenProcessLimit = 0; |
| 13551 | } else { |
| 13552 | emptyProcessLimit = (mProcessLimit*2)/3; |
| 13553 | hiddenProcessLimit = mProcessLimit - emptyProcessLimit; |
| 13554 | } |
| 13555 | |
Dianne Hackborn | 5ce7d28 | 2010-02-12 19:30:02 -0800 | [diff] [blame] | 13556 | // Let's determine how many processes we have running vs. |
| 13557 | // how many slots we have for background processes; we may want |
| 13558 | // to put multiple processes in a slot of there are enough of |
| 13559 | // them. |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13560 | int numSlots = (ProcessList.HIDDEN_APP_MAX_ADJ |
| 13561 | - ProcessList.HIDDEN_APP_MIN_ADJ + 1) / 2; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13562 | int numEmptyProcs = mLruProcesses.size()-mNumNonHiddenProcs-mNumHiddenProcs; |
| 13563 | if (numEmptyProcs > hiddenProcessLimit) { |
| 13564 | // If there are more empty processes than our limit on hidden |
| 13565 | // processes, then use the hidden process limit for the factor. |
| 13566 | // This ensures that the really old empty processes get pushed |
| 13567 | // down to the bottom, so if we are running low on memory we will |
| 13568 | // have a better chance at keeping around more hidden processes |
| 13569 | // instead of a gazillion empty processes. |
| 13570 | numEmptyProcs = hiddenProcessLimit; |
| 13571 | } |
| 13572 | int emptyFactor = numEmptyProcs/numSlots; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13573 | if (emptyFactor < 1) emptyFactor = 1; |
| 13574 | int hiddenFactor = (mNumHiddenProcs > 0 ? mNumHiddenProcs : 1)/numSlots; |
| 13575 | if (hiddenFactor < 1) hiddenFactor = 1; |
| 13576 | int stepHidden = 0; |
| 13577 | int stepEmpty = 0; |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13578 | int numHidden = 0; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13579 | int numEmpty = 0; |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13580 | int numTrimming = 0; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13581 | |
| 13582 | mNumNonHiddenProcs = 0; |
| 13583 | mNumHiddenProcs = 0; |
| 13584 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13585 | // First update the OOM adjustment for each of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13586 | // application processes based on their current state. |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13587 | int i = mLruProcesses.size(); |
Dianne Hackborn | 7d60842 | 2011-08-07 16:24:18 -0700 | [diff] [blame] | 13588 | int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13589 | int nextHiddenAdj = curHiddenAdj+1; |
| 13590 | int curEmptyAdj = ProcessList.HIDDEN_APP_MIN_ADJ; |
| 13591 | int nextEmptyAdj = curEmptyAdj+2; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13592 | int curClientHiddenAdj = curEmptyAdj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13593 | while (i > 0) { |
| 13594 | i--; |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13595 | ProcessRecord app = mLruProcesses.get(i); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13596 | //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13597 | updateOomAdjLocked(app, curHiddenAdj, curClientHiddenAdj, curEmptyAdj, TOP_APP, true); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13598 | if (!app.killedBackground) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13599 | if (app.curRawAdj == curHiddenAdj && app.hasActivities) { |
| 13600 | // This process was assigned as a hidden process... step the |
| 13601 | // hidden level. |
| 13602 | mNumHiddenProcs++; |
| 13603 | if (curHiddenAdj != nextHiddenAdj) { |
| 13604 | stepHidden++; |
| 13605 | if (stepHidden >= hiddenFactor) { |
| 13606 | stepHidden = 0; |
| 13607 | curHiddenAdj = nextHiddenAdj; |
| 13608 | nextHiddenAdj += 2; |
| 13609 | if (nextHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13610 | nextHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13611 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13612 | if (curClientHiddenAdj <= curHiddenAdj) { |
| 13613 | curClientHiddenAdj = curHiddenAdj + 1; |
| 13614 | if (curClientHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13615 | curClientHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13616 | } |
| 13617 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13618 | } |
| 13619 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13620 | numHidden++; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13621 | if (numHidden > hiddenProcessLimit) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13622 | Slog.i(TAG, "No longer want " + app.processName |
| 13623 | + " (pid " + app.pid + "): hidden #" + numHidden); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13624 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13625 | app.processName, app.setAdj, "too many background"); |
| 13626 | app.killedBackground = true; |
| 13627 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13628 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13629 | } else if (app.curRawAdj == curHiddenAdj && app.hasClientActivities) { |
| 13630 | // This process has a client that has activities. We will have |
| 13631 | // given it the current hidden adj; here we will just leave it |
| 13632 | // without stepping the hidden adj. |
| 13633 | curClientHiddenAdj++; |
| 13634 | if (curClientHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13635 | curClientHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13636 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13637 | } else { |
| 13638 | if (app.curRawAdj == curEmptyAdj || app.curRawAdj == curHiddenAdj) { |
| 13639 | // This process was assigned as an empty process... step the |
| 13640 | // empty level. |
| 13641 | if (curEmptyAdj != nextEmptyAdj) { |
| 13642 | stepEmpty++; |
| 13643 | if (stepEmpty >= emptyFactor) { |
| 13644 | stepEmpty = 0; |
| 13645 | curEmptyAdj = nextEmptyAdj; |
| 13646 | nextEmptyAdj += 2; |
| 13647 | if (nextEmptyAdj > ProcessList.HIDDEN_APP_MAX_ADJ) { |
| 13648 | nextEmptyAdj = ProcessList.HIDDEN_APP_MAX_ADJ; |
| 13649 | } |
| 13650 | } |
| 13651 | } |
| 13652 | } else if (app.curRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) { |
| 13653 | mNumNonHiddenProcs++; |
| 13654 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13655 | if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ |
| 13656 | && !app.hasClientActivities) { |
| 13657 | if (numEmpty > ProcessList.TRIM_EMPTY_APPS |
| 13658 | && app.lastActivityTime < oldTime) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13659 | Slog.i(TAG, "No longer want " + app.processName |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13660 | + " (pid " + app.pid + "): empty for " |
| 13661 | + ((oldTime+ProcessList.MAX_EMPTY_TIME-app.lastActivityTime) |
| 13662 | / 1000) + "s"); |
| 13663 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
| 13664 | app.processName, app.setAdj, "old background process"); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13665 | app.killedBackground = true; |
| 13666 | Process.killProcessQuiet(app.pid); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13667 | } else { |
| 13668 | numEmpty++; |
| 13669 | if (numEmpty > emptyProcessLimit) { |
| 13670 | Slog.i(TAG, "No longer want " + app.processName |
| 13671 | + " (pid " + app.pid + "): empty #" + numEmpty); |
| 13672 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
| 13673 | app.processName, app.setAdj, "too many background"); |
| 13674 | app.killedBackground = true; |
| 13675 | Process.killProcessQuiet(app.pid); |
| 13676 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13677 | } |
| 13678 | } |
Dianne Hackborn | 8633e68 | 2010-04-22 16:03:41 -0700 | [diff] [blame] | 13679 | } |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13680 | if (app.isolated && app.services.size() <= 0) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13681 | // If this is an isolated process, and there are no |
| 13682 | // services running in it, then the process is no longer |
| 13683 | // needed. We agressively kill these because we can by |
| 13684 | // definition not re-use the same process again, and it is |
| 13685 | // good to avoid having whatever code was running in them |
| 13686 | // left sitting around after no longer needed. |
| 13687 | Slog.i(TAG, "Isolated process " + app.processName |
| 13688 | + " (pid " + app.pid + ") no longer needed"); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13689 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13690 | app.processName, app.setAdj, "isolated not needed"); |
| 13691 | app.killedBackground = true; |
| 13692 | Process.killProcessQuiet(app.pid); |
| 13693 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13694 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 13695 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
| 13696 | && !app.killedBackground) { |
| 13697 | numTrimming++; |
| 13698 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13699 | } |
| 13700 | } |
| 13701 | |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 13702 | mNumServiceProcs = mNewNumServiceProcs; |
| 13703 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13704 | // Now determine the memory trimming level of background processes. |
| 13705 | // Unfortunately we need to start at the back of the list to do this |
| 13706 | // properly. We only do this if the number of background apps we |
| 13707 | // are managing to keep around is less than half the maximum we desire; |
| 13708 | // if we are keeping a good number around, we'll let them use whatever |
| 13709 | // memory they want. |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13710 | if (numHidden <= ProcessList.TRIM_HIDDEN_APPS |
| 13711 | && numEmpty <= ProcessList.TRIM_EMPTY_APPS) { |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13712 | final int numHiddenAndEmpty = numHidden + numEmpty; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13713 | final int N = mLruProcesses.size(); |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13714 | int factor = numTrimming/3; |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13715 | int minFactor = 2; |
| 13716 | if (mHomeProcess != null) minFactor++; |
| 13717 | if (mPreviousProcess != null) minFactor++; |
| 13718 | if (factor < minFactor) factor = minFactor; |
Dianne Hackborn | ee7621c | 2012-08-13 16:42:18 -0700 | [diff] [blame] | 13719 | int step = 0; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13720 | int fgTrimLevel; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13721 | if (numHiddenAndEmpty <= ProcessList.TRIM_CRITICAL_THRESHOLD) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13722 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13723 | } else if (numHiddenAndEmpty <= ProcessList.TRIM_LOW_THRESHOLD) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13724 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW; |
| 13725 | } else { |
| 13726 | fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE; |
| 13727 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13728 | int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13729 | for (i=0; i<N; i++) { |
| 13730 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13731 | if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ |
| 13732 | && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ |
Dianne Hackborn | d8c98fe | 2011-11-15 11:29:38 -0800 | [diff] [blame] | 13733 | && !app.killedBackground) { |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13734 | if (app.trimMemoryLevel < curLevel && app.thread != null) { |
| 13735 | try { |
| 13736 | app.thread.scheduleTrimMemory(curLevel); |
| 13737 | } catch (RemoteException e) { |
| 13738 | } |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13739 | if (false) { |
| 13740 | // For now we won't do this; our memory trimming seems |
| 13741 | // to be good enough at this point that destroying |
| 13742 | // activities causes more harm than good. |
| 13743 | if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE |
| 13744 | && app != mHomeProcess && app != mPreviousProcess) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13745 | // Need to do this on its own message because the stack may not |
| 13746 | // be in a consistent state at this point. |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13747 | // For these apps we will also finish their activities |
| 13748 | // to help them free memory. |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13749 | mMainStack.scheduleDestroyActivities(app, false, "trim"); |
Dianne Hackborn | 77eaaf0 | 2011-12-05 18:05:31 -0800 | [diff] [blame] | 13750 | } |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13751 | } |
| 13752 | } |
| 13753 | app.trimMemoryLevel = curLevel; |
| 13754 | step++; |
| 13755 | if (step >= factor) { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13756 | step = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13757 | switch (curLevel) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13758 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 13759 | curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13760 | break; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13761 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 13762 | curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13763 | break; |
| 13764 | } |
| 13765 | } |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13766 | } else if (app.nonStoppingAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13767 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13768 | && app.thread != null) { |
| 13769 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13770 | app.thread.scheduleTrimMemory( |
| 13771 | ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13772 | } catch (RemoteException e) { |
| 13773 | } |
| 13774 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13775 | app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13776 | } else { |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13777 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13778 | && app.pendingUiClean) { |
| 13779 | // If this application is now in the background and it |
| 13780 | // had done UI, then give it the special trim level to |
| 13781 | // have it free UI resources. |
| 13782 | final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; |
| 13783 | if (app.trimMemoryLevel < level && app.thread != null) { |
| 13784 | try { |
| 13785 | app.thread.scheduleTrimMemory(level); |
| 13786 | } catch (RemoteException e) { |
| 13787 | } |
| 13788 | } |
| 13789 | app.pendingUiClean = false; |
| 13790 | } |
| 13791 | if (app.trimMemoryLevel < fgTrimLevel && app.thread != null) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13792 | try { |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13793 | app.thread.scheduleTrimMemory(fgTrimLevel); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13794 | } catch (RemoteException e) { |
| 13795 | } |
| 13796 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13797 | app.trimMemoryLevel = fgTrimLevel; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13798 | } |
| 13799 | } |
| 13800 | } else { |
| 13801 | final int N = mLruProcesses.size(); |
| 13802 | for (i=0; i<N; i++) { |
| 13803 | ProcessRecord app = mLruProcesses.get(i); |
Dianne Hackborn | 162bc0e | 2012-04-09 14:06:16 -0700 | [diff] [blame] | 13804 | if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi) |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13805 | && app.pendingUiClean) { |
| 13806 | if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN |
| 13807 | && app.thread != null) { |
| 13808 | try { |
| 13809 | app.thread.scheduleTrimMemory( |
| 13810 | ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); |
| 13811 | } catch (RemoteException e) { |
| 13812 | } |
| 13813 | } |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13814 | app.pendingUiClean = false; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 13815 | } |
Dianne Hackborn | 27ff913 | 2012-03-06 14:57:58 -0800 | [diff] [blame] | 13816 | app.trimMemoryLevel = 0; |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13817 | } |
| 13818 | } |
| 13819 | |
| 13820 | if (mAlwaysFinishActivities) { |
Dianne Hackborn | 755c8bf | 2012-05-07 15:06:09 -0700 | [diff] [blame] | 13821 | // Need to do this on its own message because the stack may not |
| 13822 | // be in a consistent state at this point. |
| 13823 | mMainStack.scheduleDestroyActivities(null, false, "always-finish"); |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13824 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13825 | } |
| 13826 | |
Dianne Hackborn | 50dc3bc | 2010-06-25 10:05:59 -0700 | [diff] [blame] | 13827 | final void trimApplications() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13828 | synchronized (this) { |
| 13829 | int i; |
| 13830 | |
| 13831 | // First remove any unused application processes whose package |
| 13832 | // has been removed. |
| 13833 | for (i=mRemovedProcesses.size()-1; i>=0; i--) { |
| 13834 | final ProcessRecord app = mRemovedProcesses.get(i); |
| 13835 | if (app.activities.size() == 0 |
| 13836 | && app.curReceiver == null && app.services.size() == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 13837 | Slog.i( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13838 | TAG, "Exiting empty application process " |
| 13839 | + app.processName + " (" |
| 13840 | + (app.thread != null ? app.thread.asBinder() : null) |
| 13841 | + ")\n"); |
| 13842 | if (app.pid > 0 && app.pid != MY_PID) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 13843 | EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 13844 | app.processName, app.setAdj, "empty"); |
| 13845 | Process.killProcessQuiet(app.pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13846 | } else { |
| 13847 | try { |
| 13848 | app.thread.scheduleExit(); |
| 13849 | } catch (Exception e) { |
| 13850 | // Ignore exceptions. |
| 13851 | } |
| 13852 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 13853 | cleanUpApplicationRecordLocked(app, false, true, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13854 | mRemovedProcesses.remove(i); |
| 13855 | |
| 13856 | if (app.persistent) { |
| 13857 | if (app.persistent) { |
Dianne Hackborn | a0c283e | 2012-02-09 10:47:01 -0800 | [diff] [blame] | 13858 | addAppLocked(app.info, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13859 | } |
| 13860 | } |
| 13861 | } |
| 13862 | } |
| 13863 | |
Dianne Hackborn | ce86ba8 | 2011-07-13 19:33:41 -0700 | [diff] [blame] | 13864 | // Now update the oom adj for all processes. |
| 13865 | updateOomAdjLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13866 | } |
| 13867 | } |
| 13868 | |
| 13869 | /** This method sends the specified signal to each of the persistent apps */ |
| 13870 | public void signalPersistentProcesses(int sig) throws RemoteException { |
| 13871 | if (sig != Process.SIGNAL_USR1) { |
| 13872 | throw new SecurityException("Only SIGNAL_USR1 is allowed"); |
| 13873 | } |
| 13874 | |
| 13875 | synchronized (this) { |
| 13876 | if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES) |
| 13877 | != PackageManager.PERMISSION_GRANTED) { |
| 13878 | throw new SecurityException("Requires permission " |
| 13879 | + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES); |
| 13880 | } |
| 13881 | |
Dianne Hackborn | dd71fc8 | 2009-12-16 19:24:32 -0800 | [diff] [blame] | 13882 | for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) { |
| 13883 | ProcessRecord r = mLruProcesses.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13884 | if (r.thread != null && r.persistent) { |
| 13885 | Process.sendSignal(r.pid, sig); |
| 13886 | } |
| 13887 | } |
| 13888 | } |
| 13889 | } |
| 13890 | |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13891 | private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) { |
| 13892 | if (proc == null || proc == mProfileProc) { |
| 13893 | proc = mProfileProc; |
| 13894 | path = mProfileFile; |
| 13895 | profileType = mProfileType; |
| 13896 | clearProfilerLocked(); |
| 13897 | } |
| 13898 | if (proc == null) { |
| 13899 | return; |
| 13900 | } |
| 13901 | try { |
| 13902 | proc.thread.profilerControl(false, path, null, profileType); |
| 13903 | } catch (RemoteException e) { |
| 13904 | throw new IllegalStateException("Process disappeared"); |
| 13905 | } |
| 13906 | } |
| 13907 | |
| 13908 | private void clearProfilerLocked() { |
| 13909 | if (mProfileFd != null) { |
| 13910 | try { |
| 13911 | mProfileFd.close(); |
| 13912 | } catch (IOException e) { |
| 13913 | } |
| 13914 | } |
| 13915 | mProfileApp = null; |
| 13916 | mProfileProc = null; |
| 13917 | mProfileFile = null; |
| 13918 | mProfileType = 0; |
| 13919 | mAutoStopProfiler = false; |
| 13920 | } |
| 13921 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13922 | public boolean profileControl(String process, int userId, boolean start, |
Romain Guy | 7eabe55 | 2011-07-21 14:56:34 -0700 | [diff] [blame] | 13923 | String path, ParcelFileDescriptor fd, int profileType) throws RemoteException { |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13924 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13925 | try { |
| 13926 | synchronized (this) { |
| 13927 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 13928 | // its own permission. |
| 13929 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 13930 | != PackageManager.PERMISSION_GRANTED) { |
| 13931 | throw new SecurityException("Requires permission " |
| 13932 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13933 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13934 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13935 | if (start && fd == null) { |
| 13936 | throw new IllegalArgumentException("null fd"); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13937 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13938 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13939 | ProcessRecord proc = null; |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13940 | if (process != null) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13941 | proc = findProcessLocked(process, userId, "profileControl"); |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13942 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13943 | |
| 13944 | if (start && (proc == null || proc.thread == null)) { |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13945 | throw new IllegalArgumentException("Unknown process: " + process); |
| 13946 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13947 | |
| 13948 | if (start) { |
| 13949 | stopProfilerLocked(null, null, 0); |
| 13950 | setProfileApp(proc.info, proc.processName, path, fd, false); |
| 13951 | mProfileProc = proc; |
| 13952 | mProfileType = profileType; |
| 13953 | try { |
| 13954 | fd = fd.dup(); |
| 13955 | } catch (IOException e) { |
| 13956 | fd = null; |
| 13957 | } |
| 13958 | proc.thread.profilerControl(start, path, fd, profileType); |
| 13959 | fd = null; |
| 13960 | mProfileFd = null; |
| 13961 | } else { |
| 13962 | stopProfilerLocked(proc, path, profileType); |
| 13963 | if (fd != null) { |
| 13964 | try { |
| 13965 | fd.close(); |
| 13966 | } catch (IOException e) { |
| 13967 | } |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13968 | } |
| 13969 | } |
Dianne Hackborn | 62f20ec | 2011-08-15 17:40:28 -0700 | [diff] [blame] | 13970 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13971 | return true; |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 13972 | } |
| 13973 | } catch (RemoteException e) { |
| 13974 | throw new IllegalStateException("Process disappeared"); |
| 13975 | } finally { |
| 13976 | if (fd != null) { |
| 13977 | try { |
| 13978 | fd.close(); |
| 13979 | } catch (IOException e) { |
| 13980 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 13981 | } |
| 13982 | } |
| 13983 | } |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 13984 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13985 | private ProcessRecord findProcessLocked(String process, int userId, String callName) { |
Dianne Hackborn | 139748f | 2012-09-24 11:36:57 -0700 | [diff] [blame] | 13986 | userId = handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 13987 | userId, true, true, callName, null); |
| 13988 | ProcessRecord proc = null; |
| 13989 | try { |
| 13990 | int pid = Integer.parseInt(process); |
| 13991 | synchronized (mPidsSelfLocked) { |
| 13992 | proc = mPidsSelfLocked.get(pid); |
| 13993 | } |
| 13994 | } catch (NumberFormatException e) { |
| 13995 | } |
| 13996 | |
| 13997 | if (proc == null) { |
| 13998 | HashMap<String, SparseArray<ProcessRecord>> all |
| 13999 | = mProcessNames.getMap(); |
| 14000 | SparseArray<ProcessRecord> procs = all.get(process); |
| 14001 | if (procs != null && procs.size() > 0) { |
| 14002 | proc = procs.valueAt(0); |
| 14003 | if (userId != UserHandle.USER_ALL && proc.userId != userId) { |
| 14004 | for (int i=1; i<procs.size(); i++) { |
| 14005 | ProcessRecord thisProc = procs.valueAt(i); |
| 14006 | if (thisProc.userId == userId) { |
| 14007 | proc = thisProc; |
| 14008 | break; |
| 14009 | } |
| 14010 | } |
| 14011 | } |
| 14012 | } |
| 14013 | } |
| 14014 | |
| 14015 | return proc; |
| 14016 | } |
| 14017 | |
| 14018 | public boolean dumpHeap(String process, int userId, boolean managed, |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14019 | String path, ParcelFileDescriptor fd) throws RemoteException { |
| 14020 | |
| 14021 | try { |
| 14022 | synchronized (this) { |
| 14023 | // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to |
| 14024 | // its own permission (same as profileControl). |
| 14025 | if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) |
| 14026 | != PackageManager.PERMISSION_GRANTED) { |
| 14027 | throw new SecurityException("Requires permission " |
| 14028 | + android.Manifest.permission.SET_ACTIVITY_WATCHER); |
| 14029 | } |
| 14030 | |
| 14031 | if (fd == null) { |
| 14032 | throw new IllegalArgumentException("null fd"); |
| 14033 | } |
| 14034 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14035 | ProcessRecord proc = findProcessLocked(process, userId, "dumpHeap"); |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14036 | if (proc == null || proc.thread == null) { |
| 14037 | throw new IllegalArgumentException("Unknown process: " + process); |
| 14038 | } |
| 14039 | |
Dianne Hackborn | f02e57b | 2011-03-01 22:21:04 -0800 | [diff] [blame] | 14040 | boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")); |
| 14041 | if (!isDebuggable) { |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 14042 | if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) { |
| 14043 | throw new SecurityException("Process not debuggable: " + proc); |
| 14044 | } |
| 14045 | } |
| 14046 | |
| 14047 | proc.thread.dumpHeap(managed, path, fd); |
| 14048 | fd = null; |
| 14049 | return true; |
| 14050 | } |
| 14051 | } catch (RemoteException e) { |
| 14052 | throw new IllegalStateException("Process disappeared"); |
| 14053 | } finally { |
| 14054 | if (fd != null) { |
| 14055 | try { |
| 14056 | fd.close(); |
| 14057 | } catch (IOException e) { |
| 14058 | } |
| 14059 | } |
| 14060 | } |
| 14061 | } |
| 14062 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14063 | /** In this method we try to acquire our lock to make sure that we have not deadlocked */ |
| 14064 | public void monitor() { |
| 14065 | synchronized (this) { } |
| 14066 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 14067 | |
Dianne Hackborn | a573f6a | 2012-02-09 16:12:18 -0800 | [diff] [blame] | 14068 | void onCoreSettingsChange(Bundle settings) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 14069 | for (int i = mLruProcesses.size() - 1; i >= 0; i--) { |
| 14070 | ProcessRecord processRecord = mLruProcesses.get(i); |
| 14071 | try { |
| 14072 | if (processRecord.thread != null) { |
| 14073 | processRecord.thread.setCoreSettings(settings); |
| 14074 | } |
| 14075 | } catch (RemoteException re) { |
| 14076 | /* ignore */ |
| 14077 | } |
| 14078 | } |
| 14079 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14080 | |
| 14081 | // Multi-user methods |
| 14082 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14083 | @Override |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14084 | public boolean switchUser(int userId) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14085 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14086 | != PackageManager.PERMISSION_GRANTED) { |
| 14087 | String msg = "Permission Denial: switchUser() from pid=" |
| 14088 | + Binder.getCallingPid() |
| 14089 | + ", uid=" + Binder.getCallingUid() |
| 14090 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 14091 | Slog.w(TAG, msg); |
| 14092 | throw new SecurityException(msg); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14093 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14094 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14095 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14096 | try { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14097 | synchronized (this) { |
| 14098 | final int oldUserId = mCurrentUserId; |
| 14099 | if (oldUserId == userId) { |
| 14100 | return true; |
| 14101 | } |
| 14102 | |
| 14103 | final UserInfo userInfo = getUserManagerLocked().getUserInfo(userId); |
| 14104 | if (userInfo == null) { |
| 14105 | Slog.w(TAG, "No user info for user #" + userId); |
| 14106 | return false; |
| 14107 | } |
| 14108 | |
| 14109 | mWindowManager.startFreezingScreen(R.anim.screen_user_exit, |
| 14110 | R.anim.screen_user_enter); |
| 14111 | |
| 14112 | // If the user we are switching to is not currently started, then |
| 14113 | // we need to start it now. |
| 14114 | if (mStartedUsers.get(userId) == null) { |
| 14115 | mStartedUsers.put(userId, new UserStartedState(new UserHandle(userId), false)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14116 | updateStartedUserArrayLocked(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14117 | } |
| 14118 | |
| 14119 | mCurrentUserId = userId; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14120 | mCurrentUserArray = new int[] { userId }; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14121 | final Integer userIdInt = Integer.valueOf(userId); |
| 14122 | mUserLru.remove(userIdInt); |
| 14123 | mUserLru.add(userIdInt); |
| 14124 | |
Craig Mautner | f1b6741 | 2012-09-19 13:18:29 -0700 | [diff] [blame] | 14125 | mWindowManager.setCurrentUser(userId); |
| 14126 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14127 | final UserStartedState uss = mStartedUsers.get(userId); |
| 14128 | |
| 14129 | mHandler.removeMessages(REPORT_USER_SWITCH_MSG); |
| 14130 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 14131 | mHandler.sendMessage(mHandler.obtainMessage(REPORT_USER_SWITCH_MSG, |
| 14132 | oldUserId, userId, uss)); |
| 14133 | mHandler.sendMessageDelayed(mHandler.obtainMessage(USER_SWITCH_TIMEOUT_MSG, |
| 14134 | oldUserId, userId, uss), USER_SWITCH_TIMEOUT); |
| 14135 | Intent intent = new Intent(Intent.ACTION_USER_STARTED); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 14136 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14137 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14138 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14139 | broadcastIntentLocked(null, null, intent, |
| 14140 | null, null, 0, null, null, null, |
| 14141 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
| 14142 | |
| 14143 | if ((userInfo.flags&UserInfo.FLAG_INITIALIZED) == 0) { |
| 14144 | if (userId != 0) { |
| 14145 | intent = new Intent(Intent.ACTION_USER_INITIALIZE); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 14146 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14147 | broadcastIntentLocked(null, null, intent, null, |
| 14148 | new IIntentReceiver.Stub() { |
| 14149 | public void performReceive(Intent intent, int resultCode, |
| 14150 | String data, Bundle extras, boolean ordered, |
| 14151 | boolean sticky, int sendingUser) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 14152 | userInitialized(uss); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14153 | } |
| 14154 | }, 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID, |
| 14155 | userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 14156 | uss.initializing = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14157 | } else { |
| 14158 | getUserManagerLocked().makeInitialized(userInfo.id); |
| 14159 | } |
| 14160 | } |
| 14161 | |
| 14162 | boolean haveActivities = mMainStack.switchUserLocked(userId, uss); |
| 14163 | if (!haveActivities) { |
| 14164 | startHomeActivityLocked(userId); |
| 14165 | } |
Craig Mautner | f1b6741 | 2012-09-19 13:18:29 -0700 | [diff] [blame] | 14166 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 14167 | getUserManagerLocked().userForeground(userId); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14168 | sendUserSwitchBroadcastsLocked(oldUserId, userId); |
| 14169 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14170 | } finally { |
| 14171 | Binder.restoreCallingIdentity(ident); |
| 14172 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14173 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 14174 | return true; |
| 14175 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14176 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14177 | void sendUserSwitchBroadcastsLocked(int oldUserId, int newUserId) { |
| 14178 | long ident = Binder.clearCallingIdentity(); |
| 14179 | try { |
| 14180 | Intent intent; |
| 14181 | if (oldUserId >= 0) { |
| 14182 | intent = new Intent(Intent.ACTION_USER_BACKGROUND); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 14183 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14184 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14185 | intent.putExtra(Intent.EXTRA_USER_HANDLE, oldUserId); |
| 14186 | broadcastIntentLocked(null, null, intent, |
| 14187 | null, null, 0, null, null, null, |
| 14188 | false, false, MY_PID, Process.SYSTEM_UID, oldUserId); |
| 14189 | } |
| 14190 | if (newUserId >= 0) { |
| 14191 | intent = new Intent(Intent.ACTION_USER_FOREGROUND); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 14192 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14193 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14194 | intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId); |
| 14195 | broadcastIntentLocked(null, null, intent, |
| 14196 | null, null, 0, null, null, null, |
| 14197 | false, false, MY_PID, Process.SYSTEM_UID, newUserId); |
| 14198 | intent = new Intent(Intent.ACTION_USER_SWITCHED); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 14199 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 14200 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14201 | intent.putExtra(Intent.EXTRA_USER_HANDLE, newUserId); |
| 14202 | broadcastIntentLocked(null, null, intent, |
| 14203 | null, null, 0, null, null, |
| 14204 | android.Manifest.permission.MANAGE_USERS, |
| 14205 | false, false, MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL); |
| 14206 | } |
| 14207 | } finally { |
| 14208 | Binder.restoreCallingIdentity(ident); |
| 14209 | } |
| 14210 | } |
| 14211 | |
| 14212 | void dispatchUserSwitch(final UserStartedState uss, final int oldUserId, |
| 14213 | final int newUserId) { |
| 14214 | final int N = mUserSwitchObservers.beginBroadcast(); |
| 14215 | if (N > 0) { |
| 14216 | final IRemoteCallback callback = new IRemoteCallback.Stub() { |
| 14217 | int mCount = 0; |
| 14218 | @Override |
| 14219 | public void sendResult(Bundle data) throws RemoteException { |
| 14220 | synchronized (ActivityManagerService.this) { |
| 14221 | if (mCurUserSwitchCallback == this) { |
| 14222 | mCount++; |
| 14223 | if (mCount == N) { |
| 14224 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14225 | } |
| 14226 | } |
| 14227 | } |
| 14228 | } |
| 14229 | }; |
| 14230 | synchronized (this) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 14231 | uss.switching = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14232 | mCurUserSwitchCallback = callback; |
| 14233 | } |
| 14234 | for (int i=0; i<N; i++) { |
| 14235 | try { |
| 14236 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitching( |
| 14237 | newUserId, callback); |
| 14238 | } catch (RemoteException e) { |
| 14239 | } |
| 14240 | } |
| 14241 | } else { |
| 14242 | synchronized (this) { |
| 14243 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14244 | } |
| 14245 | } |
| 14246 | mUserSwitchObservers.finishBroadcast(); |
| 14247 | } |
| 14248 | |
| 14249 | void timeoutUserSwitch(UserStartedState uss, int oldUserId, int newUserId) { |
| 14250 | synchronized (this) { |
| 14251 | Slog.w(TAG, "User switch timeout: from " + oldUserId + " to " + newUserId); |
| 14252 | sendContinueUserSwitchLocked(uss, oldUserId, newUserId); |
| 14253 | } |
| 14254 | } |
| 14255 | |
| 14256 | void sendContinueUserSwitchLocked(UserStartedState uss, int oldUserId, int newUserId) { |
| 14257 | mCurUserSwitchCallback = null; |
| 14258 | mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG); |
| 14259 | mHandler.sendMessage(mHandler.obtainMessage(CONTINUE_USER_SWITCH_MSG, |
| 14260 | oldUserId, newUserId, uss)); |
| 14261 | } |
| 14262 | |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 14263 | void userInitialized(UserStartedState uss) { |
| 14264 | synchronized (ActivityManagerService.this) { |
| 14265 | getUserManagerLocked().makeInitialized(uss.mHandle.getIdentifier()); |
| 14266 | uss.initializing = false; |
| 14267 | completeSwitchAndInitalizeLocked(uss); |
| 14268 | } |
| 14269 | } |
| 14270 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14271 | void continueUserSwitch(UserStartedState uss, int oldUserId, int newUserId) { |
| 14272 | final int N = mUserSwitchObservers.beginBroadcast(); |
| 14273 | for (int i=0; i<N; i++) { |
| 14274 | try { |
| 14275 | mUserSwitchObservers.getBroadcastItem(i).onUserSwitchComplete(newUserId); |
| 14276 | } catch (RemoteException e) { |
| 14277 | } |
| 14278 | } |
| 14279 | mUserSwitchObservers.finishBroadcast(); |
| 14280 | synchronized (this) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame^] | 14281 | uss.switching = false; |
| 14282 | completeSwitchAndInitalizeLocked(uss); |
| 14283 | } |
| 14284 | } |
| 14285 | |
| 14286 | void completeSwitchAndInitalizeLocked(UserStartedState uss) { |
| 14287 | if (!uss.switching && !uss.initializing) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14288 | mWindowManager.stopFreezingScreen(); |
| 14289 | } |
| 14290 | } |
| 14291 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14292 | void finishUserSwitch(UserStartedState uss) { |
| 14293 | synchronized (this) { |
| 14294 | if (uss.mState == UserStartedState.STATE_BOOTING |
| 14295 | && mStartedUsers.get(uss.mHandle.getIdentifier()) == uss) { |
| 14296 | uss.mState = UserStartedState.STATE_RUNNING; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 14297 | final int userId = uss.mHandle.getIdentifier(); |
| 14298 | Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); |
| 14299 | intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 14300 | broadcastIntentLocked(null, null, intent, |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14301 | null, null, 0, null, null, |
| 14302 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 14303 | false, false, MY_PID, Process.SYSTEM_UID, userId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14304 | } |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14305 | int num = mUserLru.size(); |
| 14306 | int i = 0; |
| 14307 | while (num > MAX_RUNNING_USERS && i < mUserLru.size()) { |
| 14308 | Integer oldUserId = mUserLru.get(i); |
| 14309 | UserStartedState oldUss = mStartedUsers.get(oldUserId); |
| 14310 | if (oldUss == null) { |
| 14311 | // Shouldn't happen, but be sane if it does. |
| 14312 | mUserLru.remove(i); |
| 14313 | num--; |
| 14314 | continue; |
| 14315 | } |
| 14316 | if (oldUss.mState == UserStartedState.STATE_STOPPING) { |
| 14317 | // This user is already stopping, doesn't count. |
| 14318 | num--; |
| 14319 | i++; |
| 14320 | continue; |
| 14321 | } |
| 14322 | if (oldUserId == UserHandle.USER_OWNER || oldUserId == mCurrentUserId) { |
| 14323 | // Owner and current can't be stopped, but count as running. |
| 14324 | i++; |
| 14325 | continue; |
| 14326 | } |
| 14327 | // This is a user to be stopped. |
| 14328 | stopUserLocked(oldUserId, null); |
| 14329 | num--; |
| 14330 | i++; |
| 14331 | } |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 14332 | } |
Amith Yamasani | 52f1d75 | 2012-03-28 18:19:29 -0700 | [diff] [blame] | 14333 | } |
| 14334 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14335 | @Override |
| 14336 | public int stopUser(final int userId, final IStopUserCallback callback) { |
| 14337 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14338 | != PackageManager.PERMISSION_GRANTED) { |
| 14339 | String msg = "Permission Denial: switchUser() from pid=" |
| 14340 | + Binder.getCallingPid() |
| 14341 | + ", uid=" + Binder.getCallingUid() |
| 14342 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 14343 | Slog.w(TAG, msg); |
| 14344 | throw new SecurityException(msg); |
| 14345 | } |
| 14346 | if (userId <= 0) { |
| 14347 | throw new IllegalArgumentException("Can't stop primary user " + userId); |
| 14348 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14349 | synchronized (this) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14350 | return stopUserLocked(userId, callback); |
| 14351 | } |
| 14352 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14353 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14354 | private int stopUserLocked(final int userId, final IStopUserCallback callback) { |
| 14355 | if (mCurrentUserId == userId) { |
| 14356 | return ActivityManager.USER_OP_IS_CURRENT; |
| 14357 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14358 | |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14359 | final UserStartedState uss = mStartedUsers.get(userId); |
| 14360 | if (uss == null) { |
| 14361 | // User is not started, nothing to do... but we do need to |
| 14362 | // callback if requested. |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14363 | if (callback != null) { |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14364 | mHandler.post(new Runnable() { |
| 14365 | @Override |
| 14366 | public void run() { |
| 14367 | try { |
| 14368 | callback.userStopped(userId); |
| 14369 | } catch (RemoteException e) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14370 | } |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14371 | } |
| 14372 | }); |
| 14373 | } |
| 14374 | return ActivityManager.USER_OP_SUCCESS; |
| 14375 | } |
| 14376 | |
| 14377 | if (callback != null) { |
| 14378 | uss.mStopCallbacks.add(callback); |
| 14379 | } |
| 14380 | |
| 14381 | if (uss.mState != UserStartedState.STATE_STOPPING) { |
| 14382 | uss.mState = UserStartedState.STATE_STOPPING; |
| 14383 | |
| 14384 | long ident = Binder.clearCallingIdentity(); |
| 14385 | try { |
| 14386 | // Inform of user switch |
| 14387 | Intent intent = new Intent(Intent.ACTION_SHUTDOWN); |
| 14388 | final IIntentReceiver resultReceiver = new IIntentReceiver.Stub() { |
| 14389 | @Override |
| 14390 | public void performReceive(Intent intent, int resultCode, String data, |
| 14391 | Bundle extras, boolean ordered, boolean sticky, int sendingUser) { |
| 14392 | finishUserStop(uss); |
| 14393 | } |
| 14394 | }; |
| 14395 | broadcastIntentLocked(null, null, intent, |
| 14396 | null, resultReceiver, 0, null, null, null, |
| 14397 | true, false, MY_PID, Process.SYSTEM_UID, userId); |
| 14398 | } finally { |
| 14399 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14400 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14401 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14402 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14403 | return ActivityManager.USER_OP_SUCCESS; |
| 14404 | } |
| 14405 | |
| 14406 | void finishUserStop(UserStartedState uss) { |
| 14407 | final int userId = uss.mHandle.getIdentifier(); |
| 14408 | boolean stopped; |
| 14409 | ArrayList<IStopUserCallback> callbacks; |
| 14410 | synchronized (this) { |
| 14411 | callbacks = new ArrayList<IStopUserCallback>(uss.mStopCallbacks); |
| 14412 | if (uss.mState != UserStartedState.STATE_STOPPING |
| 14413 | || mStartedUsers.get(userId) != uss) { |
| 14414 | stopped = false; |
| 14415 | } else { |
| 14416 | stopped = true; |
| 14417 | // User can no longer run. |
| 14418 | mStartedUsers.remove(userId); |
Dianne Hackborn | bb1aeff | 2012-09-18 15:48:21 -0700 | [diff] [blame] | 14419 | mUserLru.remove(Integer.valueOf(userId)); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14420 | updateStartedUserArrayLocked(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14421 | |
| 14422 | // Clean up all state and processes associated with the user. |
| 14423 | // Kill all the processes for the user. |
| 14424 | forceStopUserLocked(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14425 | } |
| 14426 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14427 | |
| 14428 | for (int i=0; i<callbacks.size(); i++) { |
| 14429 | try { |
| 14430 | if (stopped) callbacks.get(i).userStopped(userId); |
| 14431 | else callbacks.get(i).userStopAborted(userId); |
| 14432 | } catch (RemoteException e) { |
| 14433 | } |
| 14434 | } |
| 14435 | } |
| 14436 | |
| 14437 | @Override |
| 14438 | public UserInfo getCurrentUser() { |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 14439 | if ((checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14440 | != PackageManager.PERMISSION_GRANTED) && ( |
| 14441 | checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14442 | != PackageManager.PERMISSION_GRANTED)) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14443 | String msg = "Permission Denial: getCurrentUser() from pid=" |
| 14444 | + Binder.getCallingPid() |
| 14445 | + ", uid=" + Binder.getCallingUid() |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 14446 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14447 | Slog.w(TAG, msg); |
| 14448 | throw new SecurityException(msg); |
| 14449 | } |
| 14450 | synchronized (this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14451 | return getUserManagerLocked().getUserInfo(mCurrentUserId); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 14452 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 14453 | } |
| 14454 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 14455 | int getCurrentUserIdLocked() { |
| 14456 | return mCurrentUserId; |
| 14457 | } |
| 14458 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 14459 | @Override |
| 14460 | public boolean isUserRunning(int userId) { |
| 14461 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14462 | != PackageManager.PERMISSION_GRANTED) { |
| 14463 | String msg = "Permission Denial: isUserRunning() from pid=" |
| 14464 | + Binder.getCallingPid() |
| 14465 | + ", uid=" + Binder.getCallingUid() |
| 14466 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 14467 | Slog.w(TAG, msg); |
| 14468 | throw new SecurityException(msg); |
| 14469 | } |
| 14470 | synchronized (this) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14471 | return isUserRunningLocked(userId); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 14472 | } |
| 14473 | } |
| 14474 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14475 | boolean isUserRunningLocked(int userId) { |
| 14476 | UserStartedState state = mStartedUsers.get(userId); |
| 14477 | return state != null && state.mState != UserStartedState.STATE_STOPPING; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14478 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14479 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14480 | @Override |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 14481 | public int[] getRunningUserIds() { |
| 14482 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS) |
| 14483 | != PackageManager.PERMISSION_GRANTED) { |
| 14484 | String msg = "Permission Denial: isUserRunning() from pid=" |
| 14485 | + Binder.getCallingPid() |
| 14486 | + ", uid=" + Binder.getCallingUid() |
| 14487 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS; |
| 14488 | Slog.w(TAG, msg); |
| 14489 | throw new SecurityException(msg); |
| 14490 | } |
| 14491 | synchronized (this) { |
| 14492 | return mStartedUserArray; |
| 14493 | } |
| 14494 | } |
| 14495 | |
| 14496 | private void updateStartedUserArrayLocked() { |
| 14497 | mStartedUserArray = new int[mStartedUsers.size()]; |
| 14498 | for (int i=0; i<mStartedUsers.size(); i++) { |
| 14499 | mStartedUserArray[i] = mStartedUsers.keyAt(i); |
| 14500 | } |
| 14501 | } |
| 14502 | |
| 14503 | @Override |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 14504 | public void registerUserSwitchObserver(IUserSwitchObserver observer) { |
| 14505 | if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 14506 | != PackageManager.PERMISSION_GRANTED) { |
| 14507 | String msg = "Permission Denial: registerUserSwitchObserver() from pid=" |
| 14508 | + Binder.getCallingPid() |
| 14509 | + ", uid=" + Binder.getCallingUid() |
| 14510 | + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; |
| 14511 | Slog.w(TAG, msg); |
| 14512 | throw new SecurityException(msg); |
| 14513 | } |
| 14514 | |
| 14515 | mUserSwitchObservers.register(observer); |
| 14516 | } |
| 14517 | |
| 14518 | @Override |
| 14519 | public void unregisterUserSwitchObserver(IUserSwitchObserver observer) { |
| 14520 | mUserSwitchObservers.unregister(observer); |
| 14521 | } |
| 14522 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14523 | private boolean userExists(int userId) { |
| 14524 | if (userId == 0) { |
| 14525 | return true; |
| 14526 | } |
| 14527 | UserManagerService ums = getUserManagerLocked(); |
| 14528 | return ums != null ? (ums.getUserInfo(userId) != null) : false; |
| 14529 | } |
| 14530 | |
| 14531 | int[] getUsersLocked() { |
| 14532 | UserManagerService ums = getUserManagerLocked(); |
| 14533 | return ums != null ? ums.getUserIds() : new int[] { 0 }; |
| 14534 | } |
| 14535 | |
| 14536 | UserManagerService getUserManagerLocked() { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14537 | if (mUserManager == null) { |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 14538 | IBinder b = ServiceManager.getService(Context.USER_SERVICE); |
| 14539 | mUserManager = (UserManagerService)IUserManager.Stub.asInterface(b); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 14540 | } |
| 14541 | return mUserManager; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14542 | } |
| 14543 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 14544 | private void checkValidCaller(int uid, int userId) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14545 | if (UserHandle.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) return; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 14546 | |
| 14547 | throw new SecurityException("Caller uid=" + uid |
| 14548 | + " is not privileged to communicate with user=" + userId); |
| 14549 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14550 | |
| 14551 | private int applyUserId(int uid, int userId) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14552 | return UserHandle.getUid(userId, uid); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14553 | } |
| 14554 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 14555 | ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) { |
Amith Yamasani | 2f6c9eb | 2012-02-06 15:31:35 -0800 | [diff] [blame] | 14556 | if (info == null) return null; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14557 | ApplicationInfo newInfo = new ApplicationInfo(info); |
| 14558 | newInfo.uid = applyUserId(info.uid, userId); |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 14559 | newInfo.dataDir = USER_DATA_DIR + userId + "/" |
| 14560 | + info.packageName; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14561 | return newInfo; |
| 14562 | } |
| 14563 | |
| 14564 | ActivityInfo getActivityInfoForUser(ActivityInfo aInfo, int userId) { |
Amith Yamasani | a4a54e2 | 2012-04-16 15:44:19 -0700 | [diff] [blame] | 14565 | if (aInfo == null |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 14566 | || (userId < 1 && aInfo.applicationInfo.uid < UserHandle.PER_USER_RANGE)) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 14567 | return aInfo; |
| 14568 | } |
| 14569 | |
| 14570 | ActivityInfo info = new ActivityInfo(aInfo); |
| 14571 | info.applicationInfo = getAppInfoForUser(info.applicationInfo, userId); |
| 14572 | return info; |
| 14573 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14574 | } |